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.GetPartyInventoryLevelForm;
020import com.echothree.control.user.inventory.common.result.InventoryResultFactory;
021import com.echothree.control.user.inventory.server.command.common.PartyInventoryLevelUtil;
022import com.echothree.model.control.inventory.server.control.InventoryControl;
023import com.echothree.model.control.inventory.server.logic.InventoryConditionLogic;
024import com.echothree.model.control.item.server.control.ItemControl;
025import com.echothree.model.control.item.server.logic.ItemLogic;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.control.party.server.control.PartyControl;
028import com.echothree.model.control.security.common.SecurityRoleGroups;
029import com.echothree.model.control.security.common.SecurityRoles;
030import com.echothree.model.control.warehouse.server.control.WarehouseControl;
031import com.echothree.model.data.inventory.server.entity.PartyInventoryLevel;
032import com.echothree.util.common.command.BaseResult;
033import com.echothree.util.common.message.ExecutionErrors;
034import com.echothree.util.common.validation.FieldDefinition;
035import com.echothree.util.common.validation.FieldType;
036import com.echothree.util.server.control.BaseSingleEntityCommand;
037import com.echothree.util.server.control.CommandSecurityDefinition;
038import com.echothree.util.server.control.PartyTypeDefinition;
039import com.echothree.util.server.control.SecurityRoleDefinition;
040import java.util.List;
041import javax.enterprise.context.Dependent;
042import javax.inject.Inject;
043
044@Dependent
045public class GetPartyInventoryLevelCommand
046        extends BaseSingleEntityCommand<PartyInventoryLevel, GetPartyInventoryLevelForm> {
047
048    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
049    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
050    
051    static {
052        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
053                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
054                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
055                        new SecurityRoleDefinition(SecurityRoleGroups.PartyInventoryLevel.name(), SecurityRoles.Review.name())
056                ))
057        ));
058
059        FORM_FIELD_DEFINITIONS = List.of(
060                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
061                new FieldDefinition("CompanyName", FieldType.ENTITY_NAME, false, null, null),
062                new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, false, null, null),
063                new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null),
064                new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, true, null, null)
065        );
066    }
067
068    @Inject
069    InventoryControl inventoryControl;
070
071    @Inject
072    ItemControl itemControl;
073
074    @Inject
075    PartyControl partyControl;
076
077    @Inject
078    WarehouseControl warehouseControl;
079
080    @Inject
081    InventoryConditionLogic inventoryConditionLogic;
082
083    @Inject
084    ItemLogic itemLogic;
085
086    @Inject
087    PartyInventoryLevelUtil partyInventoryLevelUtil;
088
089    /** Creates a new instance of GetPartyInventoryLevelCommand */
090    public GetPartyInventoryLevelCommand() {
091        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
092    }
093
094    @Override
095    protected PartyInventoryLevel getEntity() {
096        PartyInventoryLevel partyInventoryLevel = null;
097        var party = partyInventoryLevelUtil.getParty(this, form);
098
099        if(!hasExecutionErrors()) {
100            var itemName = form.getItemName();
101            var item = itemLogic.getItemByName(this, itemName);
102
103            if(!hasExecutionErrors()) {
104                var inventoryConditionName = form.getInventoryConditionName();
105                var inventoryCondition = inventoryConditionLogic.getInventoryConditionByName(this, inventoryConditionName);
106
107                if(!hasExecutionErrors()) {
108                    partyInventoryLevel = inventoryControl.getPartyInventoryLevel(party, item, inventoryCondition);
109
110                    if(partyInventoryLevel == null) {
111                        addExecutionError(ExecutionErrors.UnknownPartyInventoryLevel.name(), party.getLastDetail().getPartyName(), itemName, inventoryCondition.getLastDetail().getInventoryConditionName());
112                    }
113                }
114            }
115        }
116
117        return partyInventoryLevel;
118    }
119
120    @Override
121    protected BaseResult getResult(PartyInventoryLevel partyInventoryLevel) {
122        var result = InventoryResultFactory.getGetPartyInventoryLevelResult();
123
124        if(!hasExecutionErrors()) {
125            result.setPartyInventoryLevel(inventoryControl.getPartyInventoryLevelTransfer(getUserVisit(), partyInventoryLevel));
126        }
127
128        return result;
129    }
130    
131}