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    @Inject
065    ItemControl itemControl;
066
067    @Inject
068    OfferControl offerControl;
069
070    @Inject
071    OfferItemControl offerItemControl;
072
073
074    /** Creates a new instance of GetOfferItemPricesCommand */
075    public GetOfferItemPricesCommand() {
076        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
077    }
078
079    private OfferItem offerItem;
080
081    @Override
082    protected void handleForm() {
083        var offerName = form.getOfferName();
084        var offer = offerControl.getOfferByName(offerName);
085
086        if(offer != null) {
087            var itemName = form.getItemName();
088            var item = itemControl.getItemByName(itemName);
089
090            if(item != null) {
091                offerItem = offerItemControl.getOfferItem(offer, item);
092
093                if(offerItem == null) {
094                    addExecutionError(ExecutionErrors.UnknownOfferItem.name(), offer.getLastDetail().getOfferName(),
095                            item.getLastDetail().getItemName());
096                }
097            } else {
098                addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName);
099            }
100        } else {
101            addExecutionError(ExecutionErrors.UnknownOfferName.name(), offerName);
102        }
103    }
104
105    @Override
106    protected Long getTotalEntities() {
107        return offerItem != null ? offerItemControl.countOfferItemPricesByOfferItem(offerItem) : null;
108    }
109
110    @Override
111    protected Collection<OfferItemPrice> getEntities() {
112        return offerItem != null ? offerItemControl.getOfferItemPricesByOfferItem(offerItem) : null;
113    }
114    
115    @Override
116    protected BaseResult getResult(Collection<OfferItemPrice> entities) {
117        var result = OfferResultFactory.getGetOfferItemPricesResult();
118        
119        if (entities != null) {
120            var userVisit = getUserVisit();
121            
122            result.setOfferItem(offerItemControl.getOfferItemTransfer(userVisit, offerItem));
123
124            if(session.hasLimit(OfferItemPriceFactory.class)) {
125                result.setOfferItemPriceCount(getTotalEntities());
126            }
127
128            result.setOfferItemPrices(offerItemControl.getOfferItemPriceTransfers(userVisit, entities));
129        }
130        
131        return result;
132    }
133    
134}