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.inventory.server.command;
018
019import com.echothree.control.user.inventory.common.form.GetPartyInventoryLevelForm;
020import com.echothree.control.user.inventory.common.result.InventoryResultFactory;
021import com.echothree.model.control.inventory.server.control.InventoryControl;
022import com.echothree.model.control.item.server.control.ItemControl;
023import com.echothree.model.control.party.server.control.PartyControl;
024import com.echothree.model.control.warehouse.server.control.WarehouseControl;
025import com.echothree.model.data.user.common.pk.UserVisitPK;
026import com.echothree.util.common.message.ExecutionErrors;
027import com.echothree.util.common.validation.FieldDefinition;
028import com.echothree.util.common.validation.FieldType;
029import com.echothree.util.common.command.BaseResult;
030import com.echothree.util.server.persistence.Session;
031import java.util.Arrays;
032import java.util.Collections;
033import java.util.List;
034import javax.enterprise.context.RequestScoped;
035
036@RequestScoped
037public class GetPartyInventoryLevelCommand
038        extends BasePartyInventoryLevelCommand<GetPartyInventoryLevelForm> {
039    
040    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
041    
042    static {
043        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
044                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
045                new FieldDefinition("CompanyName", FieldType.ENTITY_NAME, false, null, null),
046                new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, false, null, null),
047                new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null),
048                new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, true, null, null)
049                ));
050    }
051    
052    /** Creates a new instance of GetPartyInventoryLevelCommand */
053    public GetPartyInventoryLevelCommand() {
054        super(FORM_FIELD_DEFINITIONS);
055    }
056    
057    @Override
058    protected BaseResult execute() {
059        var inventoryControl = Session.getModelController(InventoryControl.class);
060        var result = InventoryResultFactory.getGetPartyInventoryLevelResult();
061        var party = getParty(form);
062        
063        if(party != null) {
064            var itemControl = Session.getModelController(ItemControl.class);
065            var partyControl = Session.getModelController(PartyControl.class);
066            var itemName = form.getItemName();
067            var item = itemControl.getItemByName(itemName);
068            var userVisit = getUserVisit();
069            
070            if(form.getPartyName() != null) {
071                result.setParty(partyControl.getPartyTransfer(userVisit, party));
072            } else if(form.getCompanyName() != null) {
073                result.setCompany(partyControl.getCompanyTransfer(userVisit, party));
074            } else if(form.getWarehouseName() != null) {
075                var warehouseControl = Session.getModelController(WarehouseControl.class);
076                
077                result.setWarehouse(warehouseControl.getWarehouseTransfer(userVisit, party));
078            }
079            
080            if(item != null) {
081                var inventoryConditionName = form.getInventoryConditionName();
082                var inventoryCondition = inventoryControl.getInventoryConditionByName(inventoryConditionName);
083                
084                if(inventoryCondition != null) {
085                    var partyInventoryLevel = inventoryControl.getPartyInventoryLevel(party, item, inventoryCondition);
086                    
087                    if(partyInventoryLevel != null) {
088                        result.setPartyInventoryLevel(inventoryControl.getPartyInventoryLevelTransfer(userVisit, partyInventoryLevel));
089                    } else {
090                        addExecutionError(ExecutionErrors.UnknownPartyInventoryLevel.name(), party.getLastDetail().getPartyName(), itemName, inventoryConditionName);
091                    }
092                } else {
093                    addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName);
094                }
095            } else {
096                addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName);
097            }
098        }
099        
100        return result;
101    }
102    
103}