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.GetInventoryDispositionAdjustmentDescriptionsForm;
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.logic.InventoryDispositionAdjustmentLogic;
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.InventoryDispositionAdjustment;
028import com.echothree.model.data.inventory.server.entity.InventoryDispositionAdjustmentDescription;
029import com.echothree.model.data.inventory.server.factory.InventoryDispositionAdjustmentDescriptionFactory;
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 GetInventoryDispositionAdjustmentDescriptionsCommand
045        extends BasePaginatedMultipleEntitiesCommand<InventoryDispositionAdjustmentDescription,
046                GetInventoryDispositionAdjustmentDescriptionsForm> {
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.Description.name())
056                ))
057        ));
058        
059        FORM_FIELD_DEFINITIONS = List.of(
060                new FieldDefinition("InventoryTransactionTypeName", FieldType.ENTITY_NAME, true, null, null),
061                new FieldDefinition("InventoryDispositionName", FieldType.ENTITY_NAME, true, null, null),
062                new FieldDefinition("InventoryDispositionAdjustmentName", FieldType.ENTITY_NAME, true, null, null)
063        );
064    }
065
066    @Inject
067    InventoryDispositionAdjustmentControl inventoryDispositionAdjustmentControl;
068
069    @Inject
070    InventoryDispositionAdjustmentLogic inventoryDispositionAdjustmentLogic;
071
072    @Inject
073    InventoryTransactionTypeLogic inventoryTransactionTypeLogic;
074
075    /** Creates a new instance of GetInventoryDispositionAdjustmentDescriptionsCommand */
076    public GetInventoryDispositionAdjustmentDescriptionsCommand() {
077        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
078    }
079
080    InventoryDispositionAdjustment inventoryDispositionAdjustment;
081
082    @Override
083    protected void handleForm() {
084        var inventoryTransactionTypeName = form.getInventoryTransactionTypeName();
085        inventoryTransactionTypeLogic.getInventoryTransactionTypeByName(this, inventoryTransactionTypeName);
086
087        if(!hasExecutionErrors()) {
088            var inventoryDispositionAdjustmentName = form.getInventoryDispositionAdjustmentName();
089            inventoryDispositionAdjustment = inventoryDispositionAdjustmentLogic.getInventoryDispositionAdjustmentByName(this,
090                    form.getInventoryTransactionTypeName(), form.getInventoryDispositionName(), inventoryDispositionAdjustmentName);
091        }
092
093    }
094
095    @Override
096    protected Long getTotalEntities() {
097        return hasExecutionErrors() ? null
098                : inventoryDispositionAdjustmentControl.countInventoryDispositionAdjustmentDescriptionsByInventoryDispositionAdjustment(
099                        inventoryDispositionAdjustment);
100    }
101
102    @Override
103    protected Collection<InventoryDispositionAdjustmentDescription> getEntities() {
104        return hasExecutionErrors() ? null
105                : inventoryDispositionAdjustmentControl.getInventoryDispositionAdjustmentDescriptionsByInventoryDispositionAdjustment(
106                        inventoryDispositionAdjustment);
107    }
108
109    @Override
110    protected BaseResult getResult(Collection<InventoryDispositionAdjustmentDescription> entities) {
111        var result = InventoryResultFactory.getGetInventoryDispositionAdjustmentDescriptionsResult();
112
113        if(entities != null) {
114            var userVisit = getUserVisit();
115
116            result.setInventoryDispositionAdjustment(
117                    inventoryDispositionAdjustmentControl.getInventoryDispositionAdjustmentTransfer(userVisit, inventoryDispositionAdjustment));
118
119            if(session.hasLimit(InventoryDispositionAdjustmentDescriptionFactory.class)) {
120                result.setInventoryDispositionAdjustmentDescriptionCount(getTotalEntities());
121            }
122
123            result.setInventoryDispositionAdjustmentDescriptions(
124                    inventoryDispositionAdjustmentControl.getInventoryDispositionAdjustmentDescriptionTransfers(userVisit, entities));
125        }
126
127        return result;
128    }
129    
130}