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.InventoryControl;
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    /** Creates a new instance of EditInventoryLocationGroupCapacityCommand */
058    public EditInventoryLocationGroupCapacityCommand() {
059        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
060    }
061
062    @Inject
063    InventoryControl inventoryControl;
064
065    @Inject
066    UomControl uomControl;
067
068    @Inject
069    WarehouseControl warehouseControl;
070
071    @Override
072    public EditInventoryLocationGroupCapacityResult getResult() {
073        return InventoryResultFactory.getEditInventoryLocationGroupCapacityResult();
074    }
075
076    @Override
077    public InventoryLocationGroupCapacityEdit getEdit() {
078        return InventoryEditFactory.getInventoryLocationGroupCapacityEdit();
079    }
080
081    @Override
082    public InventoryLocationGroupCapacity getEntity(EditInventoryLocationGroupCapacityResult result) {
083        InventoryLocationGroupCapacity inventoryLocationGroupCapacity = null;
084        var warehouseName = spec.getWarehouseName();
085        var warehouse = warehouseControl.getWarehouseByName(warehouseName);
086
087        if(warehouse != null) {
088            var inventoryLocationGroupName = spec.getInventoryLocationGroupName();
089            var inventoryLocationGroup = inventoryControl.getInventoryLocationGroupByName(warehouse.getParty(), inventoryLocationGroupName);
090
091            if(inventoryLocationGroup != null) {
092                var unitOfMeasureKindName = spec.getUnitOfMeasureKindName();
093                var unitOfMeasureKind = uomControl.getUnitOfMeasureKindByName(unitOfMeasureKindName);
094
095                if(unitOfMeasureKind != null) {
096                    var unitOfMeasureTypeName = spec.getUnitOfMeasureTypeName();
097                    var unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(unitOfMeasureKind, unitOfMeasureTypeName);
098
099                    if(unitOfMeasureType != null) {
100                        inventoryLocationGroupCapacity = inventoryControl.getInventoryLocationGroupCapacity(inventoryLocationGroup,
101                                unitOfMeasureType, editModeToEntityPermission(editMode));
102
103                        if(inventoryLocationGroupCapacity == null) {
104                            addExecutionError(ExecutionErrors.UnknownInventoryLocationGroupCapacity.name(),
105                                    warehouseName, inventoryLocationGroupName, unitOfMeasureKindName, unitOfMeasureTypeName);
106                        }
107                    } else {
108                        addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(),
109                                unitOfMeasureKindName, unitOfMeasureTypeName);
110                    }
111                } else {
112                    addExecutionError(ExecutionErrors.UnknownUnitOfMeasureKindName.name(), unitOfMeasureKindName);
113                }
114            } else {
115                addExecutionError(ExecutionErrors.UnknownInventoryLocationGroupName.name(), warehouseName, inventoryLocationGroupName);
116            }
117        } else {
118            addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName);
119        }
120
121        return inventoryLocationGroupCapacity;
122    }
123
124    @Override
125    public InventoryLocationGroup getLockEntity(InventoryLocationGroupCapacity inventoryLocationGroupCapacity) {
126        return inventoryLocationGroupCapacity.getInventoryLocationGroup();
127    }
128
129    @Override
130    public void fillInResult(EditInventoryLocationGroupCapacityResult result, InventoryLocationGroupCapacity inventoryLocationGroupCapacity) {
131        result.setInventoryLocationGroupCapacity(inventoryControl.getInventoryLocationGroupCapacityTransfer(getUserVisit(), inventoryLocationGroupCapacity));
132    }
133
134    @Override
135    public void doLock(InventoryLocationGroupCapacityEdit edit, InventoryLocationGroupCapacity inventoryLocationGroupCapacity) {
136        edit.setCapacity(inventoryLocationGroupCapacity.getCapacity().toString());
137    }
138
139    @Override
140    public void doUpdate(InventoryLocationGroupCapacity inventoryLocationGroupCapacity) {
141        var inventoryLocationGroupCapacityValue = inventoryLocationGroupCapacity.getInventoryLocationGroupCapacityValue().clone();
142
143        inventoryLocationGroupCapacityValue.setCapacity(Long.valueOf(edit.getCapacity()));
144
145        inventoryControl.updateInventoryLocationGroupCapacityFromValue(inventoryLocationGroupCapacityValue, getPartyPK());
146    }
147
148}