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