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.edit.InventoryEditFactory;
020import com.echothree.control.user.inventory.common.edit.InventoryLocationGroupDescriptionEdit;
021import com.echothree.control.user.inventory.common.result.EditInventoryLocationGroupDescriptionResult;
022import com.echothree.control.user.inventory.common.result.InventoryResultFactory;
023import com.echothree.control.user.inventory.common.spec.InventoryLocationGroupDescriptionSpec;
024import com.echothree.model.control.inventory.server.control.InventoryControl;
025import com.echothree.model.control.party.common.PartyTypes;
026import com.echothree.model.control.party.server.control.PartyControl;
027import com.echothree.model.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.control.warehouse.server.control.WarehouseControl;
030import com.echothree.model.data.inventory.server.entity.InventoryLocationGroup;
031import com.echothree.model.data.inventory.server.entity.InventoryLocationGroupDescription;
032import com.echothree.util.common.message.ExecutionErrors;
033import com.echothree.util.common.validation.FieldDefinition;
034import com.echothree.util.common.validation.FieldType;
035import com.echothree.util.common.command.EditMode;
036import com.echothree.util.server.control.BaseAbstractEditCommand;
037import com.echothree.util.server.control.CommandSecurityDefinition;
038import com.echothree.util.server.control.PartyTypeDefinition;
039import com.echothree.util.server.control.SecurityRoleDefinition;
040import com.echothree.util.server.persistence.Session;
041import java.util.List;
042import javax.inject.Inject;
043import javax.enterprise.context.Dependent;
044
045@Dependent
046public class EditInventoryLocationGroupDescriptionCommand
047        extends BaseAbstractEditCommand<InventoryLocationGroupDescriptionSpec, InventoryLocationGroupDescriptionEdit, EditInventoryLocationGroupDescriptionResult, InventoryLocationGroupDescription, InventoryLocationGroup> {
048
049    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
050    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
051    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
052    
053    static {
054        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
055                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
056                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
057                        new SecurityRoleDefinition(SecurityRoleGroups.InventoryLocationGroup.name(), SecurityRoles.Description.name())
058                ))
059        ));
060
061        SPEC_FIELD_DEFINITIONS = List.of(
062                new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, true, null, null),
063                new FieldDefinition("InventoryLocationGroupName", FieldType.ENTITY_NAME, true, null, null),
064                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
065        );
066        
067        EDIT_FIELD_DEFINITIONS = List.of(
068                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
069        );
070    }
071    
072    /** Creates a new instance of EditInventoryLocationGroupDescriptionCommand */
073    public EditInventoryLocationGroupDescriptionCommand() {
074        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
075    }
076
077    @Inject
078    InventoryControl inventoryControl;
079
080    @Inject
081    PartyControl partyControl;
082
083    @Inject
084    WarehouseControl warehouseControl;
085    
086    @Override
087    public EditInventoryLocationGroupDescriptionResult getResult() {
088        return InventoryResultFactory.getEditInventoryLocationGroupDescriptionResult();
089    }
090
091    @Override
092    public InventoryLocationGroupDescriptionEdit getEdit() {
093        return InventoryEditFactory.getInventoryLocationGroupDescriptionEdit();
094    }
095
096    @Override
097    public InventoryLocationGroupDescription getEntity(EditInventoryLocationGroupDescriptionResult result) {
098        InventoryLocationGroupDescription inventoryLocationGroupDescription = null;
099        var warehouseName = spec.getWarehouseName();
100        var warehouse = warehouseControl.getWarehouseByName(warehouseName);
101
102        if(warehouse != null) {
103            var warehouseParty = warehouse.getParty();
104            var inventoryLocationGroupName = spec.getInventoryLocationGroupName();
105            var inventoryLocationGroup = inventoryControl.getInventoryLocationGroupByName(warehouseParty, inventoryLocationGroupName);
106
107            if(inventoryLocationGroup != null) {
108                var languageIsoName = spec.getLanguageIsoName();
109                var language = partyControl.getLanguageByIsoName(languageIsoName);
110
111                if(language != null) {
112                    if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
113                        inventoryLocationGroupDescription = inventoryControl.getInventoryLocationGroupDescription(inventoryLocationGroup, language);
114                    } else { // EditMode.UPDATE
115                        inventoryLocationGroupDescription = inventoryControl.getInventoryLocationGroupDescriptionForUpdate(inventoryLocationGroup, language);
116                    }
117
118                    if(inventoryLocationGroupDescription == null) {
119                        addExecutionError(ExecutionErrors.UnknownInventoryLocationGroupDescription.name(), warehouseName, inventoryLocationGroupName, languageIsoName);
120                    }
121                } else {
122                    addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
123                }
124            } else {
125                addExecutionError(ExecutionErrors.UnknownInventoryLocationGroupName.name(), warehouseName, inventoryLocationGroupName);
126            }
127        } else {
128            addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName);
129        }
130
131        return inventoryLocationGroupDescription;
132    }
133
134    @Override
135    public InventoryLocationGroup getLockEntity(InventoryLocationGroupDescription inventoryLocationGroupDescription) {
136        return inventoryLocationGroupDescription.getInventoryLocationGroup();
137    }
138
139    @Override
140    public void fillInResult(EditInventoryLocationGroupDescriptionResult result, InventoryLocationGroupDescription inventoryLocationGroupDescription) {
141        result.setInventoryLocationGroupDescription(inventoryControl.getInventoryLocationGroupDescriptionTransfer(getUserVisit(), inventoryLocationGroupDescription));
142    }
143
144    @Override
145    public void doLock(InventoryLocationGroupDescriptionEdit edit, InventoryLocationGroupDescription inventoryLocationGroupDescription) {
146        edit.setDescription(inventoryLocationGroupDescription.getDescription());
147    }
148
149    @Override
150    public void doUpdate(InventoryLocationGroupDescription inventoryLocationGroupDescription) {
151        var inventoryLocationGroupDescriptionValue = inventoryControl.getInventoryLocationGroupDescriptionValue(inventoryLocationGroupDescription);
152
153        inventoryLocationGroupDescriptionValue.setDescription(edit.getDescription());
154
155        inventoryControl.updateInventoryLocationGroupDescriptionFromValue(inventoryLocationGroupDescriptionValue, getPartyPK());
156    }
157    
158}