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.InventoryLocationGroupEdit;
021import com.echothree.control.user.inventory.common.result.EditInventoryLocationGroupResult;
022import com.echothree.control.user.inventory.common.result.InventoryResultFactory;
023import com.echothree.control.user.inventory.common.spec.InventoryLocationGroupSpec;
024import com.echothree.model.control.inventory.server.control.InventoryControl;
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.control.warehouse.server.control.WarehouseControl;
029import com.echothree.model.data.inventory.server.entity.InventoryLocationGroup;
030import com.echothree.model.data.warehouse.server.entity.Warehouse;
031import com.echothree.util.common.command.EditMode;
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.server.control.BaseAbstractEditCommand;
036import com.echothree.util.server.control.CommandSecurityDefinition;
037import com.echothree.util.server.control.PartyTypeDefinition;
038import com.echothree.util.server.control.SecurityRoleDefinition;
039import java.util.List;
040import javax.enterprise.context.Dependent;
041import javax.inject.Inject;
042
043@Dependent
044public class EditInventoryLocationGroupCommand
045        extends BaseAbstractEditCommand<InventoryLocationGroupSpec, InventoryLocationGroupEdit, EditInventoryLocationGroupResult, InventoryLocationGroup, InventoryLocationGroup> {
046
047    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
048    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
049    private final static List<FieldDefinition> EDIT_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.InventoryLocationGroup.name(), SecurityRoles.Edit.name())
056                ))
057        ));
058
059        SPEC_FIELD_DEFINITIONS = List.of(
060                new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, true, null, null),
061                new FieldDefinition("InventoryLocationGroupName", FieldType.ENTITY_NAME, true, null, null)
062        );
063        
064        EDIT_FIELD_DEFINITIONS = List.of(
065                new FieldDefinition("InventoryLocationGroupName", FieldType.ENTITY_NAME, true, null, null),
066                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
067                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
068                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
069        );
070    }
071
072    @Inject
073    InventoryControl inventoryControl;
074
075    @Inject
076    WarehouseControl warehouseControl;
077
078    /** Creates a new instance of EditInventoryLocationGroupCommand */
079    public EditInventoryLocationGroupCommand() {
080        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
081    }
082
083    @Override
084    public EditInventoryLocationGroupResult getResult() {
085        return InventoryResultFactory.getEditInventoryLocationGroupResult();
086    }
087
088    @Override
089    public InventoryLocationGroupEdit getEdit() {
090        return InventoryEditFactory.getInventoryLocationGroupEdit();
091    }
092
093    Warehouse warehouse;
094
095    @Override
096    public InventoryLocationGroup getEntity(EditInventoryLocationGroupResult result) {
097        var warehouseName = spec.getWarehouseName();
098        InventoryLocationGroup inventoryLocationGroup = null;
099
100        warehouse = warehouseControl.getWarehouseByName(warehouseName);
101
102        if(warehouse != null) {
103            var warehouseParty = warehouse.getParty();
104            var inventoryLocationGroupName = spec.getInventoryLocationGroupName();
105
106            if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
107                inventoryLocationGroup = inventoryControl.getInventoryLocationGroupByName(warehouseParty, inventoryLocationGroupName);
108            } else { // EditMode.UPDATE
109                inventoryLocationGroup = inventoryControl.getInventoryLocationGroupByNameForUpdate(warehouseParty, inventoryLocationGroupName);
110            }
111
112            if(inventoryLocationGroup == null) {
113                addExecutionError(ExecutionErrors.UnknownInventoryLocationGroupName.name(), warehouseName, inventoryLocationGroupName);
114            }
115        } else {
116            addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName);
117        }
118
119        return inventoryLocationGroup;
120    }
121
122    @Override
123    public InventoryLocationGroup getLockEntity(InventoryLocationGroup inventoryLocationGroup) {
124        return inventoryLocationGroup;
125    }
126
127    @Override
128    public void fillInResult(EditInventoryLocationGroupResult result, InventoryLocationGroup inventoryLocationGroup) {
129        result.setInventoryLocationGroup(inventoryControl.getInventoryLocationGroupTransfer(getUserVisit(), inventoryLocationGroup));
130    }
131
132    @Override
133    public void doLock(InventoryLocationGroupEdit edit, InventoryLocationGroup inventoryLocationGroup) {
134        var inventoryLocationGroupDescription = inventoryControl.getInventoryLocationGroupDescription(inventoryLocationGroup, getPreferredLanguage());
135        var inventoryLocationGroupDetail = inventoryLocationGroup.getLastDetail();
136
137        edit.setInventoryLocationGroupName(inventoryLocationGroupDetail.getInventoryLocationGroupName());
138        edit.setIsDefault(inventoryLocationGroupDetail.getIsDefault().toString());
139        edit.setSortOrder(inventoryLocationGroupDetail.getSortOrder().toString());
140
141        if(inventoryLocationGroupDescription != null) {
142            edit.setDescription(inventoryLocationGroupDescription.getDescription());
143        }
144    }
145
146    @Override
147    public void canUpdate(InventoryLocationGroup inventoryLocationGroup) {
148        var warehouseParty = warehouse.getParty();
149        var inventoryLocationGroupName = edit.getInventoryLocationGroupName();
150        var duplicateInventoryLocationGroup = inventoryControl.getInventoryLocationGroupByName(warehouseParty, inventoryLocationGroupName);
151
152        if(duplicateInventoryLocationGroup != null && !inventoryLocationGroup.equals(duplicateInventoryLocationGroup)) {
153            addExecutionError(ExecutionErrors.DuplicateInventoryLocationGroupName.name(), inventoryLocationGroupName);
154        }
155    }
156
157    @Override
158    public void doUpdate(InventoryLocationGroup inventoryLocationGroup) {
159        var partyPK = getPartyPK();
160        var inventoryLocationGroupDetailValue = inventoryControl.getInventoryLocationGroupDetailValueForUpdate(inventoryLocationGroup);
161        var inventoryLocationGroupDescription = inventoryControl.getInventoryLocationGroupDescriptionForUpdate(inventoryLocationGroup, getPreferredLanguage());
162        var description = edit.getDescription();
163
164        inventoryLocationGroupDetailValue.setInventoryLocationGroupName(edit.getInventoryLocationGroupName());
165        inventoryLocationGroupDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
166        inventoryLocationGroupDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
167
168        inventoryControl.updateInventoryLocationGroupFromValue(inventoryLocationGroupDetailValue, partyPK);
169
170        if(inventoryLocationGroupDescription == null && description != null) {
171            inventoryControl.createInventoryLocationGroupDescription(inventoryLocationGroup, getPreferredLanguage(), description, partyPK);
172        } else if(inventoryLocationGroupDescription != null && description == null) {
173            inventoryControl.deleteInventoryLocationGroupDescription(inventoryLocationGroupDescription, partyPK);
174        } else if(inventoryLocationGroupDescription != null && description != null) {
175            var inventoryLocationGroupDescriptionValue = inventoryControl.getInventoryLocationGroupDescriptionValue(inventoryLocationGroupDescription);
176
177            inventoryLocationGroupDescriptionValue.setDescription(description);
178            inventoryControl.updateInventoryLocationGroupDescriptionFromValue(inventoryLocationGroupDescriptionValue, partyPK);
179        }
180    }
181    
182}