001// -------------------------------------------------------------------------------- 002// Copyright 2002-2026 Echo Three, LLC 003// 004// Licensed under the Apache License, Version 2.0 (the "License"); 005// you may not use this file except in compliance with the License. 006// You may obtain a copy of the License at 007// 008// http://www.apache.org/licenses/LICENSE-2.0 009// 010// Unless required by applicable law or agreed to in writing, software 011// distributed under the License is distributed on an "AS IS" BASIS, 012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013// See the License for the specific language governing permissions and 014// limitations under the License. 015// -------------------------------------------------------------------------------- 016 017package com.echothree.control.user.offer.server.command; 018 019import com.echothree.control.user.offer.common.form.GetOfferUsesForm; 020import com.echothree.control.user.offer.common.result.OfferResultFactory; 021import com.echothree.model.control.offer.server.control.OfferControl; 022import com.echothree.model.control.offer.server.control.OfferUseControl; 023import com.echothree.model.control.offer.server.control.UseControl; 024import com.echothree.model.control.offer.server.logic.OfferLogic; 025import com.echothree.model.control.offer.server.logic.UseLogic; 026import com.echothree.model.control.party.common.PartyTypes; 027import com.echothree.model.control.security.common.SecurityRoleGroups; 028import com.echothree.model.control.security.common.SecurityRoles; 029import com.echothree.model.data.offer.server.entity.Offer; 030import com.echothree.model.data.offer.server.entity.OfferUse; 031import com.echothree.model.data.offer.server.entity.Use; 032import com.echothree.model.data.offer.server.factory.OfferUseFactory; 033import com.echothree.util.common.command.BaseResult; 034import com.echothree.util.common.validation.FieldDefinition; 035import com.echothree.util.common.validation.FieldType; 036import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand; 037import com.echothree.util.server.control.CommandSecurityDefinition; 038import com.echothree.util.server.control.PartyTypeDefinition; 039import com.echothree.util.server.control.SecurityRoleDefinition; 040import com.echothree.util.server.validation.ParameterUtils; 041import java.util.Collection; 042import java.util.List; 043import javax.enterprise.context.Dependent; 044import javax.inject.Inject; 045 046@Dependent 047public class GetOfferUsesCommand 048 extends BasePaginatedMultipleEntitiesCommand<OfferUse, GetOfferUsesForm> { 049 050 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 051 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 052 053 static { 054 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 055 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 056 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 057 new SecurityRoleDefinition(SecurityRoleGroups.OfferUse.name(), SecurityRoles.List.name()) 058 )) 059 )); 060 061 FORM_FIELD_DEFINITIONS = List.of( 062 new FieldDefinition("OfferName", FieldType.ENTITY_NAME, false, null, 20L), 063 new FieldDefinition("UseName", FieldType.ENTITY_NAME, false, null, 20L) 064 ); 065 } 066 067 /** Creates a new instance of GetOfferUsesCommand */ 068 public GetOfferUsesCommand() { 069 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 070 } 071 072 @Inject 073 OfferControl offerControl; 074 075 @Inject 076 OfferUseControl offerUseControl; 077 078 @Inject 079 UseControl useControl; 080 081 @Inject 082 OfferLogic offerLogic; 083 084 @Inject 085 UseLogic useLogic; 086 087 private Offer offer; 088 private Use use; 089 090 @Override 091 protected void handleForm() { 092 var offerName = form.getOfferName(); 093 var useName = form.getUseName(); 094 095 if(ParameterUtils.getInstance().isExactlyOneBooleanTrue(this, offerName != null && useName == null, 096 offerName == null && useName != null, offerName == null && useName == null)) { 097 if(offerName != null) { 098 offer = offerLogic.getOfferByName(this, offerName); 099 } else if(useName != null) { 100 use = useLogic.getUseByName(this, useName); 101 } 102 } 103 } 104 105 @Override 106 protected Long getTotalEntities() { 107 Long total = null; 108 109 if(!hasExecutionErrors()) { 110 if(offer != null) { 111 total = offerUseControl.countOfferUsesByOffer(offer); 112 } else if(use != null) { 113 total = offerUseControl.countOfferUsesByUse(use); 114 } else { 115 total = offerUseControl.countOfferUses(); 116 } 117 } 118 119 return total; 120 } 121 122 @Override 123 protected Collection<OfferUse> getEntities() { 124 Collection<OfferUse> offerUses = null; 125 126 if(!hasExecutionErrors()) { 127 if(offer != null) { 128 offerUses = offerUseControl.getOfferUsesByOffer(offer); 129 } else if(use != null) { 130 offerUses = offerUseControl.getOfferUsesByUse(use); 131 } else { 132 offerUses = offerUseControl.getOfferUses(); 133 } 134 } 135 136 return offerUses; 137 } 138 139 @Override 140 protected BaseResult getResult(Collection<OfferUse> entities) { 141 var result = OfferResultFactory.getGetOfferUsesResult(); 142 143 if(entities != null) { 144 var userVisit = getUserVisit(); 145 146 if(offer != null) { 147 result.setOffer(offerControl.getOfferTransfer(userVisit, offer)); 148 } 149 150 if(use != null) { 151 result.setUse(useControl.getUseTransfer(userVisit, use)); 152 } 153 154 if(session.hasLimit(OfferUseFactory.class)) { 155 result.setOfferUseCount(getTotalEntities()); 156 } 157 158 result.setOfferUses(offerUseControl.getOfferUseTransfers(userVisit, entities)); 159 } 160 161 return result; 162 } 163 164}