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.GetOfferItemPricesForm; 020import com.echothree.control.user.offer.common.result.OfferResultFactory; 021import com.echothree.model.control.item.server.control.ItemControl; 022import com.echothree.model.control.offer.server.control.OfferControl; 023import com.echothree.model.control.offer.server.control.OfferItemControl; 024import com.echothree.model.control.party.common.PartyTypes; 025import com.echothree.model.control.security.common.SecurityRoleGroups; 026import com.echothree.model.control.security.common.SecurityRoles; 027import com.echothree.model.data.offer.server.entity.OfferItem; 028import com.echothree.model.data.offer.server.entity.OfferItemPrice; 029import com.echothree.model.data.offer.server.factory.OfferItemPriceFactory; 030import com.echothree.util.common.command.BaseResult; 031import com.echothree.util.common.message.ExecutionErrors; 032import com.echothree.util.common.validation.FieldDefinition; 033import com.echothree.util.common.validation.FieldType; 034import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand; 035import com.echothree.util.server.control.CommandSecurityDefinition; 036import com.echothree.util.server.control.PartyTypeDefinition; 037import com.echothree.util.server.control.SecurityRoleDefinition; 038import java.util.Collection; 039import java.util.List; 040import javax.enterprise.context.Dependent; 041import javax.inject.Inject; 042 043@Dependent 044public class GetOfferItemPricesCommand 045 extends BasePaginatedMultipleEntitiesCommand<OfferItemPrice, GetOfferItemPricesForm> { 046 047 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 048 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 049 050 static { 051 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 052 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 053 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 054 new SecurityRoleDefinition(SecurityRoleGroups.OfferItemPrice.name(), SecurityRoles.List.name()) 055 )) 056 )); 057 058 FORM_FIELD_DEFINITIONS = List.of( 059 new FieldDefinition("OfferName", FieldType.ENTITY_NAME, true, null, 20L), 060 new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null) 061 ); 062 } 063 064 /** Creates a new instance of GetOfferItemPricesCommand */ 065 public GetOfferItemPricesCommand() { 066 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 067 } 068 069 @Inject 070 ItemControl itemControl; 071 072 @Inject 073 OfferControl offerControl; 074 075 @Inject 076 OfferItemControl offerItemControl; 077 078 private OfferItem offerItem; 079 080 @Override 081 protected void handleForm() { 082 var offerName = form.getOfferName(); 083 var offer = offerControl.getOfferByName(offerName); 084 085 if(offer != null) { 086 var itemName = form.getItemName(); 087 var item = itemControl.getItemByName(itemName); 088 089 if(item != null) { 090 offerItem = offerItemControl.getOfferItem(offer, item); 091 092 if(offerItem == null) { 093 addExecutionError(ExecutionErrors.UnknownOfferItem.name(), offer.getLastDetail().getOfferName(), 094 item.getLastDetail().getItemName()); 095 } 096 } else { 097 addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName); 098 } 099 } else { 100 addExecutionError(ExecutionErrors.UnknownOfferName.name(), offerName); 101 } 102 } 103 104 @Override 105 protected Long getTotalEntities() { 106 return offerItem != null ? offerItemControl.countOfferItemPricesByOfferItem(offerItem) : null; 107 } 108 109 @Override 110 protected Collection<OfferItemPrice> getEntities() { 111 return offerItem != null ? offerItemControl.getOfferItemPricesByOfferItem(offerItem) : null; 112 } 113 114 @Override 115 protected BaseResult getResult(Collection<OfferItemPrice> entities) { 116 var result = OfferResultFactory.getGetOfferItemPricesResult(); 117 118 if (entities != null) { 119 var userVisit = getUserVisit(); 120 121 result.setOfferItem(offerItemControl.getOfferItemTransfer(userVisit, offerItem)); 122 123 if(session.hasLimit(OfferItemPriceFactory.class)) { 124 result.setOfferItemPriceCount(getTotalEntities()); 125 } 126 127 result.setOfferItemPrices(offerItemControl.getOfferItemPriceTransfers(userVisit, entities)); 128 } 129 130 return result; 131 } 132 133}