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.GetInventoryTransactionReasonDescriptionForm;
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.party.server.logic.LanguageLogic;
026import com.echothree.model.control.security.common.SecurityRoleGroups;
027import com.echothree.model.control.security.common.SecurityRoles;
028import com.echothree.model.data.inventory.server.entity.InventoryTransactionReasonDescription;
029import com.echothree.util.common.command.BaseResult;
030import com.echothree.util.common.message.ExecutionErrors;
031import com.echothree.util.common.validation.FieldDefinition;
032import com.echothree.util.common.validation.FieldType;
033import com.echothree.util.server.control.BaseSingleEntityCommand;
034import com.echothree.util.server.control.CommandSecurityDefinition;
035import com.echothree.util.server.control.PartyTypeDefinition;
036import com.echothree.util.server.control.SecurityRoleDefinition;
037import java.util.List;
038import javax.enterprise.context.Dependent;
039import javax.inject.Inject;
040
041@Dependent
042public class GetInventoryTransactionReasonDescriptionCommand
043        extends BaseSingleEntityCommand<InventoryTransactionReasonDescription, GetInventoryTransactionReasonDescriptionForm> {
044    
045    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
046    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
047    
048    static {
049        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
050                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
051                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
052                        new SecurityRoleDefinition(SecurityRoleGroups.InventoryTransactionReason.name(), SecurityRoles.Description.name())
053                ))
054        ));
055        
056        FORM_FIELD_DEFINITIONS = List.of(
057                new FieldDefinition("InventoryTransactionTypeName", FieldType.ENTITY_NAME, true, null, null),
058                new FieldDefinition("InventoryTransactionReasonName", FieldType.ENTITY_NAME, true, null, null),
059                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
060        );
061    }
062
063    @Inject
064    InventoryTransactionReasonControl inventoryTransactionReasonControl;
065
066    @Inject
067    InventoryTransactionReasonLogic inventoryTransactionReasonLogic;
068
069    @Inject
070    InventoryTransactionTypeLogic inventoryTransactionTypeLogic;
071
072    @Inject
073    LanguageLogic languageLogic;
074
075    /** Creates a new instance of GetInventoryTransactionReasonDescriptionCommand */
076    public GetInventoryTransactionReasonDescriptionCommand() {
077        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
078    }
079    
080    @Override
081    protected InventoryTransactionReasonDescription getEntity() {
082        InventoryTransactionReasonDescription inventoryTransactionReasonDescription = null;
083        var inventoryTransactionTypeName = form.getInventoryTransactionTypeName();
084        var inventoryTransactionType = inventoryTransactionTypeLogic.getInventoryTransactionTypeByName(this, inventoryTransactionTypeName);
085
086        if(!hasExecutionErrors()) {
087            var inventoryTransactionReasonName = form.getInventoryTransactionReasonName();
088            var inventoryTransactionReason = inventoryTransactionReasonLogic.getInventoryTransactionReasonByName(this,
089                    inventoryTransactionType, inventoryTransactionReasonName);
090
091            if(!hasExecutionErrors()) {
092                var languageIsoName = form.getLanguageIsoName();
093                var language = languageLogic.getLanguageByName(this, languageIsoName);
094
095                if(!hasExecutionErrors()) {
096                    inventoryTransactionReasonDescription = inventoryTransactionReasonControl.getInventoryTransactionReasonDescription(
097                            inventoryTransactionReason, language);
098
099                    if(inventoryTransactionReasonDescription == null) {
100                        addExecutionError(ExecutionErrors.UnknownInventoryTransactionReasonDescription.name(), inventoryTransactionTypeName,
101                                inventoryTransactionReasonName, languageIsoName);
102                    }
103                }
104            }
105        }
106
107        return inventoryTransactionReasonDescription;
108    }
109
110    @Override
111    protected BaseResult getResult(InventoryTransactionReasonDescription inventoryTransactionReasonDescription) {
112        var result = InventoryResultFactory.getGetInventoryTransactionReasonDescriptionResult();
113
114        if(inventoryTransactionReasonDescription != null) {
115            result.setInventoryTransactionReasonDescription(
116                    inventoryTransactionReasonControl.getInventoryTransactionReasonDescriptionTransfer(getUserVisit(),
117                            inventoryTransactionReasonDescription));
118        }
119
120        return result;
121    }
122    
123}