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.inventory.server.command;
018
019import com.echothree.control.user.inventory.common.form.GetPartyInventoryLevelsForm;
020import com.echothree.control.user.inventory.common.result.InventoryResultFactory;
021import com.echothree.model.control.inventory.common.transfer.PartyInventoryLevelTransfer;
022import com.echothree.model.control.inventory.server.control.InventoryControl;
023import com.echothree.model.control.item.server.control.ItemControl;
024import com.echothree.model.control.party.server.control.PartyControl;
025import com.echothree.model.control.warehouse.server.control.WarehouseControl;
026import com.echothree.model.data.user.common.pk.UserVisitPK;
027import com.echothree.util.common.message.ExecutionErrors;
028import com.echothree.util.common.validation.FieldDefinition;
029import com.echothree.util.common.validation.FieldType;
030import com.echothree.util.common.command.BaseResult;
031import com.echothree.util.server.persistence.Session;
032import java.util.List;
033import javax.enterprise.context.Dependent;
034
035@Dependent
036public class GetPartyInventoryLevelsCommand
037        extends BasePartyInventoryLevelCommand<GetPartyInventoryLevelsForm> {
038    
039    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
040    
041    static {
042        FORM_FIELD_DEFINITIONS = List.of(
043                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
044                new FieldDefinition("CompanyName", FieldType.ENTITY_NAME, false, null, null),
045                new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, false, null, null),
046                new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null),
047                new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, false, null, null)
048                );
049    }
050    
051    /** Creates a new instance of GetPartyInventoryLevelsCommand */
052    public GetPartyInventoryLevelsCommand() {
053        super(FORM_FIELD_DEFINITIONS);
054    }
055    
056    @Override
057    protected BaseResult execute() {
058        var result = InventoryResultFactory.getGetPartyInventoryLevelsResult();
059        var partyName = form.getPartyName();
060        var companyName = form.getCompanyName();
061        var warehouseName = form.getWarehouseName();
062        var itemName = form.getItemName();
063        var inventoryConditionName = form.getInventoryConditionName();
064        var parameterCount = (partyName == null ? 0 : 1) + (companyName == null ? 0 : 1) + (warehouseName == null ? 0 : 1) +
065                (itemName == null ? 0 : 1) + (inventoryConditionName == null ? 0 : 1);
066        
067        if(parameterCount == 1) {
068            var inventoryControl = Session.getModelController(InventoryControl.class);
069            var userVisit = getUserVisit();
070            List<PartyInventoryLevelTransfer> partyInventoryLevels = null;
071            
072            if(itemName != null) {
073                var itemControl = Session.getModelController(ItemControl.class);
074                var item = itemControl.getItemByName(itemName);
075                
076                if(item != null) {
077                    partyInventoryLevels = inventoryControl.getPartyInventoryLevelTransfersByItem(userVisit, item);
078                    result.setItem(itemControl.getItemTransfer(userVisit, item));
079                } else {
080                    addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName);
081                }
082            } if(inventoryConditionName != null) {
083                var inventoryCondition = inventoryControl.getInventoryConditionByName(inventoryConditionName);
084                
085                if(inventoryCondition != null) {
086                    partyInventoryLevels = inventoryControl.getPartyInventoryLevelTransfersByInventoryCondition(userVisit, inventoryCondition);
087                    result.setInventoryCondition(inventoryControl.getInventoryConditionTransfer(userVisit, inventoryCondition));
088                } else {
089                    addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName);
090                }
091            }else {
092                var party = getParty(partyName, companyName, warehouseName);
093                
094                if(party != null) {
095                    var partyControl = Session.getModelController(PartyControl.class);
096                    
097                    partyInventoryLevels = inventoryControl.getPartyInventoryLevelTransfersByParty(userVisit, party);
098                    
099                    if(partyName != null) {
100                        result.setParty(partyControl.getPartyTransfer(userVisit, party));
101                    } else if(companyName != null) {
102                        result.setCompany(partyControl.getCompanyTransfer(userVisit, party));
103                    } else if(warehouseName != null) {
104                        var warehouseControl = Session.getModelController(WarehouseControl.class);
105                        
106                        result.setWarehouse(warehouseControl.getWarehouseTransfer(userVisit, party));
107                    }
108                }
109            }
110            
111            result.setPartyInventoryLevels(partyInventoryLevels);
112        }  else {
113            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
114        }
115        
116        return result;
117    }
118    
119}