001// -------------------------------------------------------------------------------- 002// Copyright 2002-2024 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.user.common.pk.UserVisitPK; 033import com.echothree.model.data.user.server.entity.UserVisit; 034import com.echothree.util.common.command.BaseResult; 035import com.echothree.util.common.validation.FieldDefinition; 036import com.echothree.util.common.validation.FieldType; 037import com.echothree.util.server.control.BaseMultipleEntitiesCommand; 038import com.echothree.util.server.control.CommandSecurityDefinition; 039import com.echothree.util.server.control.PartyTypeDefinition; 040import com.echothree.util.server.control.SecurityRoleDefinition; 041import com.echothree.util.server.persistence.Session; 042import com.echothree.util.server.validation.ParameterUtils; 043import java.util.Collection; 044import java.util.List; 045 046public class GetOfferUsesCommand 047 extends BaseMultipleEntitiesCommand<OfferUse, GetOfferUsesForm> { 048 049 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 050 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 051 052 static { 053 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 054 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 055 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 056 new SecurityRoleDefinition(SecurityRoleGroups.OfferUse.name(), SecurityRoles.List.name()) 057 )) 058 )); 059 060 FORM_FIELD_DEFINITIONS = List.of( 061 new FieldDefinition("OfferName", FieldType.ENTITY_NAME, false, null, 20L), 062 new FieldDefinition("UseName", FieldType.ENTITY_NAME, false, null, 20L) 063 ); 064 } 065 066 /** Creates a new instance of GetOfferUsesCommand */ 067 public GetOfferUsesCommand(UserVisitPK userVisitPK, GetOfferUsesForm form) { 068 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 069 } 070 071 private Offer offer; 072 private Use use; 073 074 @Override 075 protected Collection<OfferUse> getEntities() { 076 Collection<OfferUse> offerUses = null; 077 var offerName = form.getOfferName(); 078 var useName = form.getUseName(); 079 080 if(ParameterUtils.getInstance().isExactlyOneBooleanTrue(this, offerName != null && useName == null, 081 offerName == null && useName != null, offerName == null && useName == null)) { 082 var offerUseControl = Session.getModelController(OfferUseControl.class); 083 084 if(offerName != null) { 085 offer = OfferLogic.getInstance().getOfferByName(this, offerName); 086 087 if(!hasExecutionErrors()) { 088 offerUses = offerUseControl.getOfferUsesByOffer(offer); 089 } 090 } if(useName != null) { 091 use = UseLogic.getInstance().getUseByName(this, useName); 092 093 if(!hasExecutionErrors()) { 094 offerUses = offerUseControl.getOfferUsesByUse(use); 095 } 096 } else { 097 offerUses = offerUseControl.getOfferUses(); 098 } 099 } 100 101 return offerUses; 102 } 103 104 @Override 105 protected BaseResult getResult(Collection<OfferUse> entities) { 106 var result = OfferResultFactory.getGetOfferUsesResult(); 107 108 if(entities != null) { 109 var offerUseControl = Session.getModelController(OfferUseControl.class); 110 UserVisit userVisit = getUserVisit(); 111 112 if(offer != null) { 113 var offerControl = Session.getModelController(OfferControl.class); 114 115 result.setOffer(offerControl.getOfferTransfer(userVisit, offer)); 116 } 117 118 if(use != null) { 119 var useControl = Session.getModelController(UseControl.class); 120 121 result.setUse(useControl.getUseTransfer(userVisit, use)); 122 } 123 124 result.setOfferUses(offerUseControl.getOfferUseTransfers(userVisit, entities)); 125 } 126 127 return result; 128 } 129 130}