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.GetInventoryConditionUsesForm;
020import com.echothree.control.user.inventory.common.result.InventoryResultFactory;
021import com.echothree.model.control.inventory.server.control.InventoryControl;
022import com.echothree.model.data.user.common.pk.UserVisitPK;
023import com.echothree.util.common.message.ExecutionErrors;
024import com.echothree.util.common.validation.FieldDefinition;
025import com.echothree.util.common.validation.FieldType;
026import com.echothree.util.common.command.BaseResult;
027import com.echothree.util.server.control.BaseSimpleCommand;
028import com.echothree.util.server.persistence.Session;
029import java.util.List;
030import javax.enterprise.context.Dependent;
031
032@Dependent
033public class GetInventoryConditionUsesCommand
034        extends BaseSimpleCommand<GetInventoryConditionUsesForm> {
035    
036    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
037    
038    static {
039        FORM_FIELD_DEFINITIONS = List.of(
040            new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, false, null, null),
041            new FieldDefinition("InventoryConditionUseTypeName", FieldType.ENTITY_NAME, false, null, null)
042        );
043    }
044    
045    /** Creates a new instance of GetInventoryConditionUsesCommand */
046    public GetInventoryConditionUsesCommand() {
047        super(null, FORM_FIELD_DEFINITIONS, true);
048    }
049    
050    @Override
051    protected BaseResult execute() {
052        var inventoryControl = Session.getModelController(InventoryControl.class);
053        var result = InventoryResultFactory.getGetInventoryConditionUsesResult();
054        var inventoryConditionName = form.getInventoryConditionName();
055        var inventoryConditionUseTypeName = form.getInventoryConditionUseTypeName();
056        var parameterCount = (inventoryConditionName == null ? 0 : 1) + (inventoryConditionUseTypeName == null ? 0 : 1);
057        
058        if(parameterCount == 1) {
059            var userVisit = getUserVisit();
060            
061            if(inventoryConditionName != null) {
062                var inventoryCondition = inventoryControl.getInventoryConditionByName(inventoryConditionName);
063                
064                if(inventoryCondition != null) {
065                    result.setInventoryCondition(inventoryControl.getInventoryConditionTransfer(userVisit, inventoryCondition));
066                    result.setInventoryConditionUses(inventoryControl.getInventoryConditionUseTransfersByInventoryCondition(userVisit, inventoryCondition));
067                } else {
068                    addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName);
069                }
070            } else if(inventoryConditionUseTypeName != null) {
071                var inventoryConditionUseType = inventoryControl.getInventoryConditionUseTypeByName(inventoryConditionUseTypeName);
072                
073                if(inventoryConditionUseType != null) {
074                    result.setInventoryConditionUseType(inventoryControl.getInventoryConditionUseTypeTransfer(userVisit, inventoryConditionUseType));
075                    result.setInventoryConditionUses(inventoryControl.getInventoryConditionUseTransfersByInventoryConditionUseType(userVisit, inventoryConditionUseType));
076                } else {
077                    addExecutionError(ExecutionErrors.UnknownInventoryConditionUseTypeName.name(), inventoryConditionUseTypeName);
078                }
079            }
080        } else {
081            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
082        }
083        
084        return result;
085    }
086    
087}