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