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