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.form.EditInventoryLocationGroupVolumeForm;
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.user.common.pk.UserVisitPK;
030import com.echothree.util.common.message.ExecutionErrors;
031import com.echothree.util.common.validation.FieldDefinition;
032import com.echothree.util.common.validation.FieldType;
033import com.echothree.util.common.command.BaseResult;
034import com.echothree.util.common.command.EditMode;
035import com.echothree.util.server.control.BaseEditCommand;
036import com.echothree.util.server.persistence.Session;
037import java.util.List;
038import javax.enterprise.context.Dependent;
039
040@Dependent
041public class EditInventoryLocationGroupVolumeCommand
042        extends BaseEditCommand<InventoryLocationGroupSpec, InventoryLocationGroupVolumeEdit> {
043    
044    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
045    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
046    
047    static {
048        SPEC_FIELD_DEFINITIONS = List.of(
049                new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, true, null, null),
050                new FieldDefinition("InventoryLocationGroupName", FieldType.ENTITY_NAME, true, null, null)
051                );
052        
053        EDIT_FIELD_DEFINITIONS = List.of(
054                new FieldDefinition("HeightUnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null),
055                new FieldDefinition("Height", FieldType.UNSIGNED_LONG, true, null, null),
056                new FieldDefinition("WidthUnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null),
057                new FieldDefinition("Width", FieldType.UNSIGNED_LONG, true, null, null),
058                new FieldDefinition("DepthUnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null),
059                new FieldDefinition("Depth", FieldType.UNSIGNED_LONG, true, null, null)
060                );
061    }
062    
063    /** Creates a new instance of EditInventoryLocationGroupVolumeCommand */
064    public EditInventoryLocationGroupVolumeCommand() {
065        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
066    }
067    
068    @Override
069    protected BaseResult execute() {
070        var warehouseControl = Session.getModelController(WarehouseControl.class);
071        var result = InventoryResultFactory.getEditInventoryLocationGroupVolumeResult();
072        var warehouseName = spec.getWarehouseName();
073        var warehouse = warehouseControl.getWarehouseByName(warehouseName);
074        
075        if(warehouse != null) {
076            var inventoryControl = Session.getModelController(InventoryControl.class);
077            var inventoryLocationGroupName = spec.getInventoryLocationGroupName();
078            var inventoryLocationGroup = inventoryControl.getInventoryLocationGroupByName(warehouse.getParty(), inventoryLocationGroupName);
079            
080            if(inventoryLocationGroup != null) {
081                var uomControl = Session.getModelController(UomControl.class);
082                var volumeUnitOfMeasureKind = uomControl.getUnitOfMeasureKindByUnitOfMeasureKindUseTypeUsingNames(UomConstants.UnitOfMeasureKindUseType_VOLUME);
083                
084                if(volumeUnitOfMeasureKind != null) {
085                    if(editMode.equals(EditMode.LOCK)) {
086                        var inventoryLocationGroupVolume = inventoryControl.getInventoryLocationGroupVolume(inventoryLocationGroup);
087                        
088                        if(inventoryLocationGroupVolume != null) {
089                            result.setInventoryLocationGroupVolume(inventoryControl.getInventoryLocationGroupVolumeTransfer(getUserVisit(), inventoryLocationGroupVolume));
090                            
091                            if(lockEntity(inventoryLocationGroup)) {
092                                var edit = InventoryEditFactory.getInventoryLocationGroupVolumeEdit();
093                                var height = inventoryLocationGroupVolume.getHeight();
094                                var heightConversion = height == null? null: new Conversion(uomControl, volumeUnitOfMeasureKind, height).convertToHighestUnitOfMeasureType();
095                                var width = inventoryLocationGroupVolume.getWidth();
096                                var widthConversion = width == null? null: new Conversion(uomControl, volumeUnitOfMeasureKind, width).convertToHighestUnitOfMeasureType();
097                                var depth = inventoryLocationGroupVolume.getDepth();
098                                var depthConversion = depth == null? null: new Conversion(uomControl, volumeUnitOfMeasureKind, depth).convertToHighestUnitOfMeasureType();
099                                
100                                result.setEdit(edit);
101                                edit.setHeight(heightConversion.getQuantity().toString());
102                                edit.setHeightUnitOfMeasureTypeName(heightConversion.getUnitOfMeasureType().getLastDetail().getUnitOfMeasureTypeName());
103                                edit.setWidth(widthConversion.getQuantity().toString());
104                                edit.setWidthUnitOfMeasureTypeName(widthConversion.getUnitOfMeasureType().getLastDetail().getUnitOfMeasureTypeName());
105                                edit.setDepth(depthConversion.getQuantity().toString());
106                                edit.setDepthUnitOfMeasureTypeName(depthConversion.getUnitOfMeasureType().getLastDetail().getUnitOfMeasureTypeName());
107                            } else {
108                                addExecutionError(ExecutionErrors.EntityLockFailed.name());
109                            }
110                            
111                            result.setEntityLock(getEntityLockTransfer(inventoryLocationGroup));
112                        } else {
113                            addExecutionError(ExecutionErrors.UnknownInventoryLocationGroupVolume.name());
114                        }
115                    } else if(editMode.equals(EditMode.UPDATE)) {
116                        var inventoryLocationGroupVolume = inventoryControl.getInventoryLocationGroupVolumeForUpdate(inventoryLocationGroup);
117                        
118                        if(inventoryLocationGroupVolume != null) {
119                            var heightUnitOfMeasureTypeName = edit.getHeightUnitOfMeasureTypeName();
120                            var heightUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind,
121                                    heightUnitOfMeasureTypeName);
122                            
123                            if(heightUnitOfMeasureType != null) {
124                                var height = Long.valueOf(edit.getHeight());
125                                
126                                if(height > 0) {
127                                    var widthUnitOfMeasureTypeName = edit.getWidthUnitOfMeasureTypeName();
128                                    var widthUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind,
129                                            widthUnitOfMeasureTypeName);
130                                    
131                                    if(widthUnitOfMeasureType != null) {
132                                        var width = Long.valueOf(edit.getWidth());
133                                        
134                                        if(width > 0) {
135                                            var depthUnitOfMeasureTypeName = edit.getDepthUnitOfMeasureTypeName();
136                                            var depthUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind,
137                                                    depthUnitOfMeasureTypeName);
138                                            
139                                            if(depthUnitOfMeasureType != null) {
140                                                var depth = Long.valueOf(edit.getDepth());
141                                                
142                                                if(depth > 0) {
143                                                    if(lockEntityForUpdate(inventoryLocationGroup)) {
144                                                        try {
145                                                            var inventoryLocationGroupVolumeValue = inventoryControl.getInventoryLocationGroupVolumeValueForUpdate(inventoryLocationGroupVolume);
146                                                            var heightConversion = new Conversion(uomControl, heightUnitOfMeasureType, height).convertToLowestUnitOfMeasureType();
147                                                            var widthConversion = new Conversion(uomControl, widthUnitOfMeasureType, width).convertToLowestUnitOfMeasureType();
148                                                            var depthConversion = new Conversion(uomControl, depthUnitOfMeasureType, depth).convertToLowestUnitOfMeasureType();
149                                                            
150                                                            inventoryLocationGroupVolumeValue.setHeight(heightConversion.getQuantity());
151                                                            inventoryLocationGroupVolumeValue.setWidth(widthConversion.getQuantity());
152                                                            inventoryLocationGroupVolumeValue.setDepth(depthConversion.getQuantity());
153                                                            
154                                                            inventoryControl.updateInventoryLocationGroupVolumeFromValue(inventoryLocationGroupVolumeValue, getPartyPK());
155                                                        } finally {
156                                                            unlockEntity(inventoryLocationGroup);
157                                                        }
158                                                    } else {
159                                                        addExecutionError(ExecutionErrors.EntityLockStale.name());
160                                                    }
161                                                } else {
162                                                    addExecutionError(ExecutionErrors.InvalidDepth.name(), depth);
163                                                }
164                                            } else {
165                                                addExecutionError(ExecutionErrors.UnknownDepthUnitOfMeasureTypeName.name(), depthUnitOfMeasureTypeName);
166                                            }
167                                        } else {
168                                            addExecutionError(ExecutionErrors.InvalidWidth.name(), width);
169                                        }
170                                    } else {
171                                        addExecutionError(ExecutionErrors.UnknownWidthUnitOfMeasureTypeName.name(), widthUnitOfMeasureTypeName);
172                                    }
173                                } else {
174                                    addExecutionError(ExecutionErrors.InvalidHeight.name(), height);
175                                }
176                            } else {
177                                addExecutionError(ExecutionErrors.UnknownHeightUnitOfMeasureTypeName.name(), heightUnitOfMeasureTypeName);
178                            }
179                        } else {
180                            addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeVolume.name());
181                        }
182
183                        if(hasExecutionErrors()) {
184                            result.setInventoryLocationGroupVolume(inventoryControl.getInventoryLocationGroupVolumeTransfer(getUserVisit(), inventoryLocationGroupVolume));
185                            result.setEntityLock(getEntityLockTransfer(inventoryLocationGroup));
186                        }
187                    }
188                } else {
189                    addExecutionError(ExecutionErrors.UnknownVolumeUnitOfMeasureKind.name());
190                }
191            } else {
192                addExecutionError(ExecutionErrors.UnknownInventoryLocationGroupName.name(), inventoryLocationGroupName);
193            }
194        } else {
195            addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName);
196        }
197        
198        return result;
199    }
200    
201}