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.GetInventoryDispositionAdjustmentsForm;
020import com.echothree.control.user.inventory.common.result.InventoryResultFactory;
021import com.echothree.model.control.inventory.server.control.InventoryDispositionAdjustmentControl;
022import com.echothree.model.control.inventory.server.control.InventoryTransactionTypeControl;
023import com.echothree.model.control.inventory.server.logic.InventoryTransactionTypeLogic;
024import com.echothree.model.control.inventory.server.logic.InventoryDispositionLogic;
025import com.echothree.model.control.party.common.PartyTypes;
026import com.echothree.model.control.security.common.SecurityRoleGroups;
027import com.echothree.model.control.security.common.SecurityRoles;
028import com.echothree.model.data.inventory.server.entity.InventoryDispositionAdjustment;
029import com.echothree.model.data.inventory.server.entity.InventoryTransactionType;
030import com.echothree.model.data.inventory.server.entity.InventoryDisposition;
031import com.echothree.model.data.inventory.server.factory.InventoryDispositionAdjustmentFactory;
032import com.echothree.util.common.command.BaseResult;
033import com.echothree.util.common.validation.FieldDefinition;
034import com.echothree.util.common.validation.FieldType;
035import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand;
036import com.echothree.util.server.control.CommandSecurityDefinition;
037import com.echothree.util.server.control.PartyTypeDefinition;
038import com.echothree.util.server.control.SecurityRoleDefinition;
039import java.util.Collection;
040import java.util.List;
041import javax.enterprise.context.Dependent;
042import javax.inject.Inject;
043
044@Dependent
045public class GetInventoryDispositionAdjustmentsCommand
046        extends BasePaginatedMultipleEntitiesCommand<InventoryDispositionAdjustment, GetInventoryDispositionAdjustmentsForm> {
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.InventoryDispositionAdjustment.name(), SecurityRoles.List.name())
056                ))
057        ));
058
059        FORM_FIELD_DEFINITIONS = List.of(
060                new FieldDefinition("InventoryTransactionTypeName", FieldType.ENTITY_NAME, true, null, null)
061        );
062    }
063
064    @Inject
065    InventoryDispositionAdjustmentControl inventoryDispositionAdjustmentControl;
066
067    @Inject
068    InventoryTransactionTypeControl inventoryTransactionTypeControl;
069
070    @Inject
071    InventoryTransactionTypeLogic inventoryTransactionTypeLogic;
072
073    @Inject
074    InventoryDispositionLogic inventoryDispositionLogic;
075
076    /** Creates a new instance of GetInventoryDispositionAdjustmentsCommand */
077    public GetInventoryDispositionAdjustmentsCommand() {
078        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
079    }
080
081    InventoryTransactionType inventoryTransactionType;
082    InventoryDisposition inventoryDisposition;
083
084    @Override
085    protected void handleForm() {
086        var inventoryTransactionTypeName = form.getInventoryTransactionTypeName();
087
088        inventoryTransactionType = inventoryTransactionTypeLogic.getInventoryTransactionTypeByName(this, inventoryTransactionTypeName);
089        if(!hasExecutionErrors()) {
090            inventoryDisposition = inventoryDispositionLogic.getInventoryDispositionByName(this, inventoryTransactionType,
091                    form.getInventoryDispositionName());
092        }
093    }
094
095    @Override
096    protected Long getTotalEntities() {
097        return hasExecutionErrors() ? null
098                : inventoryDispositionAdjustmentControl.countInventoryDispositionAdjustmentsByInventoryDisposition(inventoryDisposition);
099    }
100
101    @Override
102    protected Collection<InventoryDispositionAdjustment> getEntities() {
103        return hasExecutionErrors() ? null : inventoryDispositionAdjustmentControl.getInventoryDispositionAdjustments(inventoryDisposition);
104    }
105
106    @Override
107    protected BaseResult getResult(Collection<InventoryDispositionAdjustment> entities) {
108        var result = InventoryResultFactory.getGetInventoryDispositionAdjustmentsResult();
109
110        if(entities != null) {
111            var userVisit = getUserVisit();
112
113            result.setInventoryTransactionType(
114                    inventoryTransactionTypeControl.getInventoryTransactionTypeTransfer(userVisit, inventoryTransactionType));
115
116            if(session.hasLimit(InventoryDispositionAdjustmentFactory.class)) {
117                result.setInventoryDispositionAdjustmentCount(getTotalEntities());
118            }
119
120            result.setInventoryDispositionAdjustments(inventoryDispositionAdjustmentControl
121                    .getInventoryDispositionAdjustmentTransfers(userVisit, entities));
122        }
123
124        return result;
125    }
126    
127}