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.InventoryLocationGroupCapacityEdit;
021import com.echothree.control.user.inventory.common.result.EditInventoryLocationGroupCapacityResult;
022import com.echothree.control.user.inventory.common.result.InventoryResultFactory;
023import com.echothree.control.user.inventory.common.spec.InventoryLocationGroupCapacitySpec;
024import com.echothree.model.control.inventory.server.control.InventoryLocationGroupControl;
025import com.echothree.model.control.uom.server.control.UomControl;
026import com.echothree.model.control.warehouse.server.control.WarehouseControl;
027import com.echothree.model.data.inventory.server.entity.InventoryLocationGroup;
028import com.echothree.model.data.inventory.server.entity.InventoryLocationGroupCapacity;
029import com.echothree.util.common.message.ExecutionErrors;
030import com.echothree.util.common.validation.FieldDefinition;
031import com.echothree.util.common.validation.FieldType;
032import com.echothree.util.server.control.BaseAbstractEditCommand;
033import java.util.List;
034import javax.enterprise.context.Dependent;
035import javax.inject.Inject;
036
037@Dependent
038public class EditInventoryLocationGroupCapacityCommand
039        extends BaseAbstractEditCommand<InventoryLocationGroupCapacitySpec, InventoryLocationGroupCapacityEdit, EditInventoryLocationGroupCapacityResult, InventoryLocationGroupCapacity, InventoryLocationGroup> {
040
041    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
042    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
043
044    static {
045        SPEC_FIELD_DEFINITIONS = List.of(
046                new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, true, null, null),
047                new FieldDefinition("InventoryLocationGroupName", FieldType.ENTITY_NAME, true, null, null),
048                new FieldDefinition("UnitOfMeasureKindName", FieldType.ENTITY_NAME, true, null, null),
049                new FieldDefinition("UnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null)
050        );
051
052        EDIT_FIELD_DEFINITIONS = List.of(
053                new FieldDefinition("Capacity", FieldType.UNSIGNED_LONG, true, null, null)
054        );
055    }
056
057    @Inject
058    InventoryLocationGroupControl inventoryLocationGroupControl;
059
060    @Inject
061    UomControl uomControl;
062
063    @Inject
064    WarehouseControl warehouseControl;
065
066    /** Creates a new instance of EditInventoryLocationGroupCapacityCommand */
067    public EditInventoryLocationGroupCapacityCommand() {
068        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
069    }
070    @Override
071    public EditInventoryLocationGroupCapacityResult getResult() {
072        return InventoryResultFactory.getEditInventoryLocationGroupCapacityResult();
073    }
074
075    @Override
076    public InventoryLocationGroupCapacityEdit getEdit() {
077        return InventoryEditFactory.getInventoryLocationGroupCapacityEdit();
078    }
079
080    @Override
081    public InventoryLocationGroupCapacity getEntity(EditInventoryLocationGroupCapacityResult result) {
082        InventoryLocationGroupCapacity inventoryLocationGroupCapacity = null;
083        var warehouseName = spec.getWarehouseName();
084        var warehouse = warehouseControl.getWarehouseByName(warehouseName);
085
086        if(warehouse != null) {
087            var inventoryLocationGroupName = spec.getInventoryLocationGroupName();
088            var inventoryLocationGroup = inventoryLocationGroupControl.getInventoryLocationGroupByName(warehouse.getParty(), inventoryLocationGroupName);
089
090            if(inventoryLocationGroup != null) {
091                var unitOfMeasureKindName = spec.getUnitOfMeasureKindName();
092                var unitOfMeasureKind = uomControl.getUnitOfMeasureKindByName(unitOfMeasureKindName);
093
094                if(unitOfMeasureKind != null) {
095                    var unitOfMeasureTypeName = spec.getUnitOfMeasureTypeName();
096                    var unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(unitOfMeasureKind, unitOfMeasureTypeName);
097
098                    if(unitOfMeasureType != null) {
099                        inventoryLocationGroupCapacity = inventoryLocationGroupControl.getInventoryLocationGroupCapacity(inventoryLocationGroup,
100                                unitOfMeasureType, editModeToEntityPermission(editMode));
101
102                        if(inventoryLocationGroupCapacity == null) {
103                            addExecutionError(ExecutionErrors.UnknownInventoryLocationGroupCapacity.name(),
104                                    warehouseName, inventoryLocationGroupName, unitOfMeasureKindName, unitOfMeasureTypeName);
105                        }
106                    } else {
107                        addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(),
108                                unitOfMeasureKindName, unitOfMeasureTypeName);
109                    }
110                } else {
111                    addExecutionError(ExecutionErrors.UnknownUnitOfMeasureKindName.name(), unitOfMeasureKindName);
112                }
113            } else {
114                addExecutionError(ExecutionErrors.UnknownInventoryLocationGroupName.name(), warehouseName, inventoryLocationGroupName);
115            }
116        } else {
117            addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName);
118        }
119
120        return inventoryLocationGroupCapacity;
121    }
122
123    @Override
124    public InventoryLocationGroup getLockEntity(InventoryLocationGroupCapacity inventoryLocationGroupCapacity) {
125        return inventoryLocationGroupCapacity.getInventoryLocationGroup();
126    }
127
128    @Override
129    public void fillInResult(EditInventoryLocationGroupCapacityResult result, InventoryLocationGroupCapacity inventoryLocationGroupCapacity) {
130        result.setInventoryLocationGroupCapacity(inventoryLocationGroupControl.getInventoryLocationGroupCapacityTransfer(getUserVisit(), inventoryLocationGroupCapacity));
131    }
132
133    @Override
134    public void doLock(InventoryLocationGroupCapacityEdit edit, InventoryLocationGroupCapacity inventoryLocationGroupCapacity) {
135        edit.setCapacity(inventoryLocationGroupCapacity.getCapacity().toString());
136    }
137
138    @Override
139    public void doUpdate(InventoryLocationGroupCapacity inventoryLocationGroupCapacity) {
140        var inventoryLocationGroupCapacityValue = inventoryLocationGroupCapacity.getInventoryLocationGroupCapacityValue().clone();
141
142        inventoryLocationGroupCapacityValue.setCapacity(Long.valueOf(edit.getCapacity()));
143
144        inventoryLocationGroupControl.updateInventoryLocationGroupCapacityFromValue(inventoryLocationGroupCapacityValue, getPartyPK());
145    }
146
147}