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.InventoryLocationGroupVolumeEdit;
021import com.echothree.control.user.inventory.common.result.EditInventoryLocationGroupVolumeResult;
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.InventoryLocationGroupControl;
025import com.echothree.model.control.uom.common.UomConstants;
026import com.echothree.model.control.uom.server.control.UomControl;
027import com.echothree.model.control.uom.server.util.Conversion;
028import com.echothree.model.control.warehouse.server.control.WarehouseControl;
029import com.echothree.model.data.inventory.server.entity.InventoryLocationGroup;
030import com.echothree.model.data.inventory.server.entity.InventoryLocationGroupVolume;
031import com.echothree.model.data.uom.server.entity.UnitOfMeasureKind;
032import com.echothree.model.data.uom.server.entity.UnitOfMeasureType;
033import com.echothree.util.common.command.EditMode;
034import com.echothree.util.common.message.ExecutionErrors;
035import com.echothree.util.common.validation.FieldDefinition;
036import com.echothree.util.common.validation.FieldType;
037import com.echothree.util.server.control.BaseAbstractEditCommand;
038import java.util.List;
039import javax.enterprise.context.Dependent;
040import javax.inject.Inject;
041
042@Dependent
043public class EditInventoryLocationGroupVolumeCommand
044        extends BaseAbstractEditCommand<InventoryLocationGroupSpec, InventoryLocationGroupVolumeEdit, EditInventoryLocationGroupVolumeResult, InventoryLocationGroupVolume, InventoryLocationGroup> {
045
046    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
047    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
048
049    static {
050        SPEC_FIELD_DEFINITIONS = List.of(
051                new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, true, null, null),
052                new FieldDefinition("InventoryLocationGroupName", FieldType.ENTITY_NAME, true, null, null)
053        );
054
055        EDIT_FIELD_DEFINITIONS = List.of(
056                new FieldDefinition("HeightUnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null),
057                new FieldDefinition("Height", FieldType.UNSIGNED_LONG, true, null, null),
058                new FieldDefinition("WidthUnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null),
059                new FieldDefinition("Width", FieldType.UNSIGNED_LONG, true, null, null),
060                new FieldDefinition("DepthUnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null),
061                new FieldDefinition("Depth", FieldType.UNSIGNED_LONG, true, null, null)
062        );
063    }
064
065    @Inject
066    InventoryLocationGroupControl inventoryLocationGroupControl;
067
068    @Inject
069    UomControl uomControl;
070
071    @Inject
072    WarehouseControl warehouseControl;
073
074    /** Creates a new instance of EditInventoryLocationGroupVolumeCommand */
075    public EditInventoryLocationGroupVolumeCommand() {
076        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
077    }
078    @Override
079    public EditInventoryLocationGroupVolumeResult getResult() {
080        return InventoryResultFactory.getEditInventoryLocationGroupVolumeResult();
081    }
082
083    @Override
084    public InventoryLocationGroupVolumeEdit getEdit() {
085        return InventoryEditFactory.getInventoryLocationGroupVolumeEdit();
086    }
087
088    @Override
089    public InventoryLocationGroupVolume getEntity(EditInventoryLocationGroupVolumeResult result) {
090        InventoryLocationGroupVolume inventoryLocationGroupVolume = null;
091        var warehouseName = spec.getWarehouseName();
092        var warehouse = warehouseControl.getWarehouseByName(warehouseName);
093
094        if(warehouse != null) {
095            var inventoryLocationGroupName = spec.getInventoryLocationGroupName();
096            var inventoryLocationGroup = inventoryLocationGroupControl.getInventoryLocationGroupByName(warehouse.getParty(), inventoryLocationGroupName);
097
098            if(inventoryLocationGroup != null) {
099                if(editMode.equals(EditMode.UPDATE)) {
100                    inventoryLocationGroupVolume = inventoryLocationGroupControl.getInventoryLocationGroupVolumeForUpdate(inventoryLocationGroup);
101                } else {
102                    inventoryLocationGroupVolume = inventoryLocationGroupControl.getInventoryLocationGroupVolume(inventoryLocationGroup);
103                }
104
105                if(inventoryLocationGroupVolume == null) {
106                    addExecutionError(ExecutionErrors.UnknownInventoryLocationGroupVolume.name(), warehouseName, inventoryLocationGroupName);
107                }
108            } else {
109                addExecutionError(ExecutionErrors.UnknownInventoryLocationGroupName.name(), warehouseName, inventoryLocationGroupName);
110            }
111        } else {
112            addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName);
113        }
114
115        return inventoryLocationGroupVolume;
116    }
117
118    @Override
119    public InventoryLocationGroup getLockEntity(InventoryLocationGroupVolume inventoryLocationGroupVolume) {
120        return inventoryLocationGroupVolume.getInventoryLocationGroup();
121    }
122
123    @Override
124    public void fillInResult(EditInventoryLocationGroupVolumeResult result, InventoryLocationGroupVolume inventoryLocationGroupVolume) {
125        result.setInventoryLocationGroupVolume(inventoryLocationGroupControl.getInventoryLocationGroupVolumeTransfer(getUserVisit(), inventoryLocationGroupVolume));
126    }
127
128    UnitOfMeasureKind volumeUnitOfMeasureKind;
129
130    private UnitOfMeasureKind getVolumeUnitOfMeasureKind() {
131        if(volumeUnitOfMeasureKind == null) {
132            volumeUnitOfMeasureKind = uomControl.getUnitOfMeasureKindByUnitOfMeasureKindUseTypeUsingNames(UomConstants.UnitOfMeasureKindUseType_VOLUME);
133        }
134
135        return volumeUnitOfMeasureKind;
136    }
137
138    @Override
139    public void doLock(InventoryLocationGroupVolumeEdit edit, InventoryLocationGroupVolume inventoryLocationGroupVolume) {
140        var volumeUnitOfMeasureKind = getVolumeUnitOfMeasureKind();
141        var height = inventoryLocationGroupVolume.getHeight();
142        var heightConversion = height == null? null: new Conversion(uomControl, volumeUnitOfMeasureKind, height).convertToHighestUnitOfMeasureType();
143        var width = inventoryLocationGroupVolume.getWidth();
144        var widthConversion = width == null? null: new Conversion(uomControl, volumeUnitOfMeasureKind, width).convertToHighestUnitOfMeasureType();
145        var depth = inventoryLocationGroupVolume.getDepth();
146        var depthConversion = depth == null? null: new Conversion(uomControl, volumeUnitOfMeasureKind, depth).convertToHighestUnitOfMeasureType();
147
148        if(heightConversion != null) {
149            edit.setHeight(heightConversion.getQuantity().toString());
150            edit.setHeightUnitOfMeasureTypeName(heightConversion.getUnitOfMeasureType().getLastDetail().getUnitOfMeasureTypeName());
151        }
152
153        if(widthConversion != null) {
154            edit.setWidth(widthConversion.getQuantity().toString());
155            edit.setWidthUnitOfMeasureTypeName(widthConversion.getUnitOfMeasureType().getLastDetail().getUnitOfMeasureTypeName());
156        }
157
158        if(depthConversion != null) {
159            edit.setDepth(depthConversion.getQuantity().toString());
160            edit.setDepthUnitOfMeasureTypeName(depthConversion.getUnitOfMeasureType().getLastDetail().getUnitOfMeasureTypeName());
161        }
162    }
163
164    Long height;
165    Long width;
166    Long depth;
167
168    @Override
169    public void canUpdate(InventoryLocationGroupVolume inventoryLocationGroupVolume) {
170        var volumeUnitOfMeasureKind = getVolumeUnitOfMeasureKind();
171        var heightUnitOfMeasureTypeName = edit.getHeightUnitOfMeasureTypeName();
172        var heightUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind, heightUnitOfMeasureTypeName);
173
174        if(heightUnitOfMeasureType != null) {
175            var heightValue = Long.valueOf(edit.getHeight());
176
177            if(heightValue > 0) {
178                var widthUnitOfMeasureTypeName = edit.getWidthUnitOfMeasureTypeName();
179                var widthUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind, widthUnitOfMeasureTypeName);
180
181                if(widthUnitOfMeasureType != null) {
182                    var widthValue = Long.valueOf(edit.getWidth());
183
184                    if(widthValue > 0) {
185                        var depthUnitOfMeasureTypeName = edit.getDepthUnitOfMeasureTypeName();
186                        var depthUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind, depthUnitOfMeasureTypeName);
187
188                        if(depthUnitOfMeasureType != null) {
189                            var depthValue = Long.valueOf(edit.getDepth());
190
191                            if(depthValue > 0) {
192                                height = new Conversion(uomControl, heightUnitOfMeasureType, heightValue).convertToLowestUnitOfMeasureType().getQuantity();
193                                width = new Conversion(uomControl, widthUnitOfMeasureType, widthValue).convertToLowestUnitOfMeasureType().getQuantity();
194                                depth = new Conversion(uomControl, depthUnitOfMeasureType, depthValue).convertToLowestUnitOfMeasureType().getQuantity();
195                            } else {
196                                addExecutionError(ExecutionErrors.InvalidDepth.name(), depthValue);
197                            }
198                        } else {
199                            addExecutionError(ExecutionErrors.UnknownDepthUnitOfMeasureTypeName.name(), depthUnitOfMeasureTypeName);
200                        }
201                    } else {
202                        addExecutionError(ExecutionErrors.InvalidWidth.name(), widthValue);
203                    }
204                } else {
205                    addExecutionError(ExecutionErrors.UnknownWidthUnitOfMeasureTypeName.name(), widthUnitOfMeasureTypeName);
206                }
207            } else {
208                addExecutionError(ExecutionErrors.InvalidHeight.name(), heightValue);
209            }
210        } else {
211            addExecutionError(ExecutionErrors.UnknownHeightUnitOfMeasureTypeName.name(), heightUnitOfMeasureTypeName);
212        }
213    }
214
215    @Override
216    public void doUpdate(InventoryLocationGroupVolume inventoryLocationGroupVolume) {
217        var inventoryLocationGroupVolumeValue = inventoryLocationGroupControl.getInventoryLocationGroupVolumeValueForUpdate(inventoryLocationGroupVolume);
218
219        inventoryLocationGroupVolumeValue.setHeight(height);
220        inventoryLocationGroupVolumeValue.setWidth(width);
221        inventoryLocationGroupVolumeValue.setDepth(depth);
222
223        inventoryLocationGroupControl.updateInventoryLocationGroupVolumeFromValue(inventoryLocationGroupVolumeValue, getPartyPK());
224    }
225
226}