001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.GetOfferItemPriceForm;
020import com.echothree.control.user.offer.common.result.OfferResultFactory;
021import com.echothree.model.control.accounting.server.control.AccountingControl;
022import com.echothree.model.control.inventory.server.control.InventoryControl;
023import com.echothree.model.control.item.server.control.ItemControl;
024import com.echothree.model.control.offer.server.control.OfferControl;
025import com.echothree.model.control.offer.server.control.OfferItemControl;
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.control.security.server.logic.SecurityRoleLogic;
030import com.echothree.model.control.uom.server.control.UomControl;
031import com.echothree.model.data.offer.server.entity.OfferItemPrice;
032import com.echothree.model.data.user.common.pk.UserVisitPK;
033import com.echothree.util.common.command.BaseResult;
034import com.echothree.util.common.message.ExecutionErrors;
035import com.echothree.util.common.validation.FieldDefinition;
036import com.echothree.util.common.validation.FieldType;
037import com.echothree.util.server.control.BaseSingleEntityCommand;
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 java.util.Arrays;
043import java.util.Collections;
044import java.util.List;
045import javax.enterprise.context.RequestScoped;
046
047@RequestScoped
048public class GetOfferItemPriceCommand
049        extends BaseSingleEntityCommand<OfferItemPrice, GetOfferItemPriceForm> {
050    
051    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
052    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
053    
054    static {
055        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
056                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
057                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
058                        new SecurityRoleDefinition(SecurityRoleGroups.OfferItemPrice.name(), SecurityRoles.Review.name())
059                        )))
060                )));
061        
062        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
063                new FieldDefinition("OfferName", FieldType.ENTITY_NAME, true, null, 20L),
064                new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null),
065                new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, true, null, null),
066                new FieldDefinition("UnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null),
067                new FieldDefinition("CurrencyIsoName", FieldType.ENTITY_NAME, true, null, null),
068                new FieldDefinition("IncludeHistory", FieldType.BOOLEAN, false, null, null)
069                ));
070    }
071    
072    /** Creates a new instance of GetOfferItemPriceCommand */
073    public GetOfferItemPriceCommand() {
074        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
075    }
076    
077    @Override
078    protected boolean checkOptionalSecurityRoles() {
079        // This occurs before validation, and parseBoolean is more lax than our validation of what's permitted for a FieldType.BOOLEAN.
080        return Boolean.parseBoolean(form.getIncludeHistory()) ? SecurityRoleLogic.getInstance().hasSecurityRoleUsingNames(this, getParty(),
081                SecurityRoleGroups.OfferItemPrice.name(), SecurityRoles.History.name()) : true;
082    }
083    
084    @Override
085    protected OfferItemPrice getEntity() {
086        var offerControl = Session.getModelController(OfferControl.class);
087        var offerName = form.getOfferName();
088        var offer = offerControl.getOfferByName(offerName);
089        OfferItemPrice offerItemPrice = null;
090
091        if(offer != null) {
092            var itemControl = Session.getModelController(ItemControl.class);
093            var itemName = form.getItemName();
094            var item = itemControl.getItemByName(itemName);
095            
096            if(item != null) {
097                var offerItemControl = Session.getModelController(OfferItemControl.class);
098                var offerItem = offerItemControl.getOfferItem(offer, item);
099
100                if(offerItem != null) {
101                    var inventoryControl = Session.getModelController(InventoryControl.class);
102                    var inventoryConditionName = form.getInventoryConditionName();
103                    var inventoryCondition = inventoryControl.getInventoryConditionByName(inventoryConditionName);
104
105                    if(inventoryCondition != null) {
106                        var uomControl = Session.getModelController(UomControl.class);
107                        var unitOfMeasureKind = item.getLastDetail().getUnitOfMeasureKind();
108                        var unitOfMeasureTypeName = form.getUnitOfMeasureTypeName();
109                        var unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(unitOfMeasureKind, unitOfMeasureTypeName);
110
111                        if(unitOfMeasureType != null) {
112                            var accountingControl = Session.getModelController(AccountingControl.class);
113                            var currencyIsoName = form.getCurrencyIsoName();
114                            var currency = accountingControl.getCurrencyByIsoName(currencyIsoName);
115
116                            if(currency != null) {
117                                offerItemPrice = offerItemControl.getOfferItemPrice(offerItem, inventoryCondition, unitOfMeasureType, currency);
118
119                                if(offerItemPrice == null) {
120                                    addExecutionError(ExecutionErrors.UnknownOfferItemPrice.name(), offer.getLastDetail().getOfferName(),
121                                            item.getLastDetail().getItemName(), inventoryCondition.getLastDetail().getInventoryConditionName(),
122                                            unitOfMeasureType.getLastDetail().getUnitOfMeasureTypeName(), currency.getCurrencyIsoName());
123                                }
124                            } else {
125                                addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName);
126                            }
127                        } else {
128                            addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(),
129                                    unitOfMeasureKind.getLastDetail().getUnitOfMeasureKindName(), unitOfMeasureTypeName);
130                        }
131                    } else {
132                        addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName);
133                    }
134                } else {
135                    addExecutionError(ExecutionErrors.UnknownOfferItem.name(), offer.getLastDetail().getOfferName(),
136                            item.getLastDetail().getItemName());
137                }
138            } else {
139                addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName);
140            }
141        } else {
142            addExecutionError(ExecutionErrors.UnknownOfferName.name(), offerName);
143        }
144        
145        return offerItemPrice;
146    }
147    
148    @Override
149    protected BaseResult getResult(OfferItemPrice offerItemPrice) {
150        var result = OfferResultFactory.getGetOfferItemPriceResult();
151
152        if(offerItemPrice != null) {
153            var offerItemControl = Session.getModelController(OfferItemControl.class);
154            var userVisit = getUserVisit();
155
156            result.setOfferItemPrice(offerItemControl.getOfferItemPriceTransfer(userVisit, offerItemPrice));
157
158            if(Boolean.parseBoolean(form.getIncludeHistory())) {
159                result.setHistory(offerItemControl.getOfferItemPriceHistory(userVisit, offerItemPrice));
160            }
161        }
162        
163        return result;
164    }
165    
166}