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.GetPartyInventoryLevelForm;
020import com.echothree.control.user.inventory.common.result.GetPartyInventoryLevelResult;
021import com.echothree.control.user.inventory.common.result.InventoryResultFactory;
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.inventory.server.entity.InventoryCondition;
027import com.echothree.model.data.inventory.server.entity.PartyInventoryLevel;
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 GetPartyInventoryLevelCommand
042        extends BasePartyInventoryLevelCommand<GetPartyInventoryLevelForm> {
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, true, null, null)
053                ));
054    }
055    
056    /** Creates a new instance of GetPartyInventoryLevelCommand */
057    public GetPartyInventoryLevelCommand(UserVisitPK userVisitPK, GetPartyInventoryLevelForm form) {
058        super(userVisitPK, form, FORM_FIELD_DEFINITIONS);
059    }
060    
061    @Override
062    protected BaseResult execute() {
063        var inventoryControl = Session.getModelController(InventoryControl.class);
064        GetPartyInventoryLevelResult result = InventoryResultFactory.getGetPartyInventoryLevelResult();
065        Party party = getParty(form);
066        
067        if(party != null) {
068            var itemControl = Session.getModelController(ItemControl.class);
069            var partyControl = Session.getModelController(PartyControl.class);
070            String itemName = form.getItemName();
071            Item item = itemControl.getItemByName(itemName);
072            UserVisit userVisit = getUserVisit();
073            
074            if(form.getPartyName() != null) {
075                result.setParty(partyControl.getPartyTransfer(userVisit, party));
076            } else if(form.getCompanyName() != null) {
077                result.setCompany(partyControl.getCompanyTransfer(userVisit, party));
078            } else if(form.getWarehouseName() != null) {
079                var warehouseControl = Session.getModelController(WarehouseControl.class);
080                
081                result.setWarehouse(warehouseControl.getWarehouseTransfer(userVisit, party));
082            }
083            
084            if(item != null) {
085                String inventoryConditionName = form.getInventoryConditionName();
086                InventoryCondition inventoryCondition = inventoryControl.getInventoryConditionByName(inventoryConditionName);
087                
088                if(inventoryCondition != null) {
089                    PartyInventoryLevel partyInventoryLevel = inventoryControl.getPartyInventoryLevel(party, item, inventoryCondition);
090                    
091                    if(partyInventoryLevel != null) {
092                        result.setPartyInventoryLevel(inventoryControl.getPartyInventoryLevelTransfer(userVisit, partyInventoryLevel));
093                    } else {
094                        addExecutionError(ExecutionErrors.UnknownPartyInventoryLevel.name(), party.getLastDetail().getPartyName(), itemName, inventoryConditionName);
095                    }
096                } else {
097                    addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName);
098                }
099            } else {
100                addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName);
101            }
102        }
103        
104        return result;
105    }
106    
107}