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.control.inventory.server.logic.InventoryConditionLogic;
023import com.echothree.model.data.inventory.server.entity.InventoryCondition;
024import com.echothree.model.data.inventory.server.entity.InventoryConditionUse;
025import com.echothree.model.data.inventory.server.entity.InventoryConditionUseType;
026import com.echothree.model.data.inventory.server.factory.InventoryConditionUseFactory;
027import com.echothree.util.common.command.BaseResult;
028import com.echothree.util.common.message.ExecutionErrors;
029import com.echothree.util.common.validation.FieldDefinition;
030import com.echothree.util.common.validation.FieldType;
031import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand;
032import java.util.Collection;
033import java.util.List;
034import javax.enterprise.context.Dependent;
035import javax.inject.Inject;
036
037@Dependent
038public class GetInventoryConditionUsesCommand
039        extends BasePaginatedMultipleEntitiesCommand<InventoryConditionUse, GetInventoryConditionUsesForm> {
040    
041    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
042    
043    static {
044        FORM_FIELD_DEFINITIONS = List.of(
045                new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, false, null, null),
046                new FieldDefinition("InventoryConditionUseTypeName", FieldType.ENTITY_NAME, false, null, null)
047        );
048    }
049    
050    @Inject
051    InventoryControl inventoryControl;
052
053    @Inject
054    InventoryConditionLogic inventoryConditionLogic;
055
056    /** Creates a new instance of GetInventoryConditionUsesCommand */
057    public GetInventoryConditionUsesCommand() {
058        super(null, FORM_FIELD_DEFINITIONS, true);
059    }
060
061    private InventoryCondition inventoryCondition;
062    private InventoryConditionUseType inventoryConditionUseType;
063
064    @Override
065    protected void handleForm() {
066        var inventoryConditionName = form.getInventoryConditionName();
067        var inventoryConditionUseTypeName = form.getInventoryConditionUseTypeName();
068        var parameterCount = (inventoryConditionName == null ? 0 : 1) + (inventoryConditionUseTypeName == null ? 0 : 1);
069
070        if(parameterCount == 1) {
071            if(inventoryConditionName != null) {
072                inventoryCondition = inventoryConditionLogic.getInventoryConditionByName(this, inventoryConditionName);
073            } else {
074                inventoryConditionUseType = inventoryControl.getInventoryConditionUseTypeByName(inventoryConditionUseTypeName);
075
076                if(inventoryConditionUseType == null) {
077                    addExecutionError(ExecutionErrors.UnknownInventoryConditionUseTypeName.name(), inventoryConditionUseTypeName);
078                }
079            }
080        } else {
081            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
082        }
083    }
084
085    @Override
086    protected Long getTotalEntities() {
087        Long total = null;
088
089        if(!hasExecutionErrors()) {
090            if(inventoryCondition != null) {
091                total = inventoryControl.countInventoryConditionUsesByInventoryCondition(inventoryCondition);
092            } else {
093                total = inventoryControl.countInventoryConditionUsesByInventoryConditionUseType(inventoryConditionUseType);
094            }
095        }
096
097        return total;
098    }
099
100    @Override
101    protected Collection<InventoryConditionUse> getEntities() {
102        Collection<InventoryConditionUse> entities = null;
103
104        if(!hasExecutionErrors()) {
105            if(inventoryCondition != null) {
106                entities = inventoryControl.getInventoryConditionUsesByInventoryCondition(inventoryCondition);
107            } else {
108                entities = inventoryControl.getInventoryConditionUsesByInventoryConditionUseType(inventoryConditionUseType);
109            }
110        }
111
112        return entities;
113    }
114
115    @Override
116    protected BaseResult getResult(Collection<InventoryConditionUse> entities) {
117        var result = InventoryResultFactory.getGetInventoryConditionUsesResult();
118
119        if(entities != null) {
120            var userVisit = getUserVisit();
121
122            if(inventoryCondition != null) {
123                result.setInventoryCondition(inventoryControl.getInventoryConditionTransfer(userVisit, inventoryCondition));
124            } else {
125                result.setInventoryConditionUseType(inventoryControl.getInventoryConditionUseTypeTransfer(userVisit, inventoryConditionUseType));
126            }
127
128            if(session.hasLimit(InventoryConditionUseFactory.class)) {
129                result.setInventoryConditionUseCount(getTotalEntities());
130            }
131
132            result.setInventoryConditionUses(inventoryControl.getInventoryConditionUseTransfers(userVisit, entities));
133        }
134
135        return result;
136    }
137    
138}