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.InventoryControl;
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    /** Creates a new instance of EditInventoryLocationGroupVolumeCommand */
066    public EditInventoryLocationGroupVolumeCommand() {
067        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
068    }
069
070    @Inject
071    InventoryControl inventoryControl;
072
073    @Inject
074    UomControl uomControl;
075
076    @Inject
077    WarehouseControl warehouseControl;
078
079    @Override
080    public EditInventoryLocationGroupVolumeResult getResult() {
081        return InventoryResultFactory.getEditInventoryLocationGroupVolumeResult();
082    }
083
084    @Override
085    public InventoryLocationGroupVolumeEdit getEdit() {
086        return InventoryEditFactory.getInventoryLocationGroupVolumeEdit();
087    }
088
089    @Override
090    public InventoryLocationGroupVolume getEntity(EditInventoryLocationGroupVolumeResult result) {
091        InventoryLocationGroupVolume inventoryLocationGroupVolume = null;
092        var warehouseName = spec.getWarehouseName();
093        var warehouse = warehouseControl.getWarehouseByName(warehouseName);
094
095        if(warehouse != null) {
096            var inventoryLocationGroupName = spec.getInventoryLocationGroupName();
097            var inventoryLocationGroup = inventoryControl.getInventoryLocationGroupByName(warehouse.getParty(), inventoryLocationGroupName);
098
099            if(inventoryLocationGroup != null) {
100                if(editMode.equals(EditMode.UPDATE)) {
101                    inventoryLocationGroupVolume = inventoryControl.getInventoryLocationGroupVolumeForUpdate(inventoryLocationGroup);
102                } else {
103                    inventoryLocationGroupVolume = inventoryControl.getInventoryLocationGroupVolume(inventoryLocationGroup);
104                }
105
106                if(inventoryLocationGroupVolume == null) {
107                    addExecutionError(ExecutionErrors.UnknownInventoryLocationGroupVolume.name(), warehouseName, inventoryLocationGroupName);
108                }
109            } else {
110                addExecutionError(ExecutionErrors.UnknownInventoryLocationGroupName.name(), warehouseName, inventoryLocationGroupName);
111            }
112        } else {
113            addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName);
114        }
115
116        return inventoryLocationGroupVolume;
117    }
118
119    @Override
120    public InventoryLocationGroup getLockEntity(InventoryLocationGroupVolume inventoryLocationGroupVolume) {
121        return inventoryLocationGroupVolume.getInventoryLocationGroup();
122    }
123
124    @Override
125    public void fillInResult(EditInventoryLocationGroupVolumeResult result, InventoryLocationGroupVolume inventoryLocationGroupVolume) {
126        result.setInventoryLocationGroupVolume(inventoryControl.getInventoryLocationGroupVolumeTransfer(getUserVisit(), inventoryLocationGroupVolume));
127    }
128
129    UnitOfMeasureKind volumeUnitOfMeasureKind;
130
131    private UnitOfMeasureKind getVolumeUnitOfMeasureKind() {
132        if(volumeUnitOfMeasureKind == null) {
133            volumeUnitOfMeasureKind = uomControl.getUnitOfMeasureKindByUnitOfMeasureKindUseTypeUsingNames(UomConstants.UnitOfMeasureKindUseType_VOLUME);
134        }
135
136        return volumeUnitOfMeasureKind;
137    }
138
139    @Override
140    public void doLock(InventoryLocationGroupVolumeEdit edit, InventoryLocationGroupVolume inventoryLocationGroupVolume) {
141        var volumeUnitOfMeasureKind = getVolumeUnitOfMeasureKind();
142        var height = inventoryLocationGroupVolume.getHeight();
143        var heightConversion = height == null? null: new Conversion(uomControl, volumeUnitOfMeasureKind, height).convertToHighestUnitOfMeasureType();
144        var width = inventoryLocationGroupVolume.getWidth();
145        var widthConversion = width == null? null: new Conversion(uomControl, volumeUnitOfMeasureKind, width).convertToHighestUnitOfMeasureType();
146        var depth = inventoryLocationGroupVolume.getDepth();
147        var depthConversion = depth == null? null: new Conversion(uomControl, volumeUnitOfMeasureKind, depth).convertToHighestUnitOfMeasureType();
148
149        if(heightConversion != null) {
150            edit.setHeight(heightConversion.getQuantity().toString());
151            edit.setHeightUnitOfMeasureTypeName(heightConversion.getUnitOfMeasureType().getLastDetail().getUnitOfMeasureTypeName());
152        }
153
154        if(widthConversion != null) {
155            edit.setWidth(widthConversion.getQuantity().toString());
156            edit.setWidthUnitOfMeasureTypeName(widthConversion.getUnitOfMeasureType().getLastDetail().getUnitOfMeasureTypeName());
157        }
158
159        if(depthConversion != null) {
160            edit.setDepth(depthConversion.getQuantity().toString());
161            edit.setDepthUnitOfMeasureTypeName(depthConversion.getUnitOfMeasureType().getLastDetail().getUnitOfMeasureTypeName());
162        }
163    }
164
165    Long height;
166    Long width;
167    Long depth;
168
169    @Override
170    public void canUpdate(InventoryLocationGroupVolume inventoryLocationGroupVolume) {
171        var volumeUnitOfMeasureKind = getVolumeUnitOfMeasureKind();
172        var heightUnitOfMeasureTypeName = edit.getHeightUnitOfMeasureTypeName();
173        var heightUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind, heightUnitOfMeasureTypeName);
174
175        if(heightUnitOfMeasureType != null) {
176            var heightValue = Long.valueOf(edit.getHeight());
177
178            if(heightValue > 0) {
179                var widthUnitOfMeasureTypeName = edit.getWidthUnitOfMeasureTypeName();
180                var widthUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind, widthUnitOfMeasureTypeName);
181
182                if(widthUnitOfMeasureType != null) {
183                    var widthValue = Long.valueOf(edit.getWidth());
184
185                    if(widthValue > 0) {
186                        var depthUnitOfMeasureTypeName = edit.getDepthUnitOfMeasureTypeName();
187                        var depthUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind, depthUnitOfMeasureTypeName);
188
189                        if(depthUnitOfMeasureType != null) {
190                            var depthValue = Long.valueOf(edit.getDepth());
191
192                            if(depthValue > 0) {
193                                height = new Conversion(uomControl, heightUnitOfMeasureType, heightValue).convertToLowestUnitOfMeasureType().getQuantity();
194                                width = new Conversion(uomControl, widthUnitOfMeasureType, widthValue).convertToLowestUnitOfMeasureType().getQuantity();
195                                depth = new Conversion(uomControl, depthUnitOfMeasureType, depthValue).convertToLowestUnitOfMeasureType().getQuantity();
196                            } else {
197                                addExecutionError(ExecutionErrors.InvalidDepth.name(), depthValue);
198                            }
199                        } else {
200                            addExecutionError(ExecutionErrors.UnknownDepthUnitOfMeasureTypeName.name(), depthUnitOfMeasureTypeName);
201                        }
202                    } else {
203                        addExecutionError(ExecutionErrors.InvalidWidth.name(), widthValue);
204                    }
205                } else {
206                    addExecutionError(ExecutionErrors.UnknownWidthUnitOfMeasureTypeName.name(), widthUnitOfMeasureTypeName);
207                }
208            } else {
209                addExecutionError(ExecutionErrors.InvalidHeight.name(), heightValue);
210            }
211        } else {
212            addExecutionError(ExecutionErrors.UnknownHeightUnitOfMeasureTypeName.name(), heightUnitOfMeasureTypeName);
213        }
214    }
215
216    @Override
217    public void doUpdate(InventoryLocationGroupVolume inventoryLocationGroupVolume) {
218        var inventoryLocationGroupVolumeValue = inventoryControl.getInventoryLocationGroupVolumeValueForUpdate(inventoryLocationGroupVolume);
219
220        inventoryLocationGroupVolumeValue.setHeight(height);
221        inventoryLocationGroupVolumeValue.setWidth(width);
222        inventoryLocationGroupVolumeValue.setDepth(depth);
223
224        inventoryControl.updateInventoryLocationGroupVolumeFromValue(inventoryLocationGroupVolumeValue, getPartyPK());
225    }
226
227}