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.GetInventoryTransactionReasonDescriptionsForm;
020import com.echothree.control.user.inventory.common.result.InventoryResultFactory;
021import com.echothree.model.control.inventory.server.control.InventoryTransactionReasonControl;
022import com.echothree.model.control.inventory.server.logic.InventoryTransactionReasonLogic;
023import com.echothree.model.control.inventory.server.logic.InventoryTransactionTypeLogic;
024import com.echothree.model.control.party.common.PartyTypes;
025import com.echothree.model.control.security.common.SecurityRoleGroups;
026import com.echothree.model.control.security.common.SecurityRoles;
027import com.echothree.model.data.inventory.server.entity.InventoryTransactionReason;
028import com.echothree.model.data.inventory.server.entity.InventoryTransactionReasonDescription;
029import com.echothree.model.data.inventory.server.factory.InventoryTransactionReasonDescriptionFactory;
030import com.echothree.util.common.command.BaseResult;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand;
035import com.echothree.util.server.control.CommandSecurityDefinition;
036import com.echothree.util.server.control.PartyTypeDefinition;
037import com.echothree.util.server.control.SecurityRoleDefinition;
038import java.util.Collection;
039import java.util.List;
040import javax.enterprise.context.Dependent;
041import javax.inject.Inject;
042
043@Dependent
044public class GetInventoryTransactionReasonDescriptionsCommand
045        extends BasePaginatedMultipleEntitiesCommand<InventoryTransactionReasonDescription,
046                GetInventoryTransactionReasonDescriptionsForm> {
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.InventoryTransactionReason.name(), SecurityRoles.Description.name())
056                ))
057        ));
058        
059        FORM_FIELD_DEFINITIONS = List.of(
060                new FieldDefinition("InventoryTransactionTypeName", FieldType.ENTITY_NAME, true, null, null),
061                new FieldDefinition("InventoryTransactionReasonName", FieldType.ENTITY_NAME, true, null, null)
062        );
063    }
064
065    @Inject
066    InventoryTransactionReasonControl inventoryTransactionReasonControl;
067
068    @Inject
069    InventoryTransactionReasonLogic inventoryTransactionReasonLogic;
070
071    @Inject
072    InventoryTransactionTypeLogic inventoryTransactionTypeLogic;
073
074    /** Creates a new instance of GetInventoryTransactionReasonDescriptionsCommand */
075    public GetInventoryTransactionReasonDescriptionsCommand() {
076        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
077    }
078
079    InventoryTransactionReason inventoryTransactionReason;
080
081    @Override
082    protected void handleForm() {
083        var inventoryTransactionTypeName = form.getInventoryTransactionTypeName();
084        var inventoryTransactionType = inventoryTransactionTypeLogic.getInventoryTransactionTypeByName(this, inventoryTransactionTypeName);
085
086        if(!hasExecutionErrors()) {
087            var inventoryTransactionReasonName = form.getInventoryTransactionReasonName();
088            inventoryTransactionReason = inventoryTransactionReasonLogic.getInventoryTransactionReasonByName(this,
089                    inventoryTransactionType, inventoryTransactionReasonName);
090        }
091
092    }
093
094    @Override
095    protected Long getTotalEntities() {
096        return hasExecutionErrors() ? null
097                : inventoryTransactionReasonControl.countInventoryTransactionReasonDescriptionsByInventoryTransactionReason(
098                        inventoryTransactionReason);
099    }
100
101    @Override
102    protected Collection<InventoryTransactionReasonDescription> getEntities() {
103        return hasExecutionErrors() ? null
104                : inventoryTransactionReasonControl.getInventoryTransactionReasonDescriptionsByInventoryTransactionReason(
105                        inventoryTransactionReason);
106    }
107
108    @Override
109    protected BaseResult getResult(Collection<InventoryTransactionReasonDescription> entities) {
110        var result = InventoryResultFactory.getGetInventoryTransactionReasonDescriptionsResult();
111
112        if(entities != null) {
113            var userVisit = getUserVisit();
114
115            result.setInventoryTransactionReason(
116                    inventoryTransactionReasonControl.getInventoryTransactionReasonTransfer(userVisit, inventoryTransactionReason));
117
118            if(session.hasLimit(InventoryTransactionReasonDescriptionFactory.class)) {
119                result.setInventoryTransactionReasonDescriptionCount(getTotalEntities());
120            }
121
122            result.setInventoryTransactionReasonDescriptions(
123                    inventoryTransactionReasonControl.getInventoryTransactionReasonDescriptionTransfers(userVisit, entities));
124        }
125
126        return result;
127    }
128    
129}