001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.Arrays;
038import java.util.Collections;
039import java.util.List;
040import javax.enterprise.context.RequestScoped;
041
042@RequestScoped
043public class EditInventoryLocationGroupVolumeCommand
044        extends BaseEditCommand<InventoryLocationGroupSpec, InventoryLocationGroupVolumeEdit> {
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 = Collections.unmodifiableList(Arrays.asList(
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 = Collections.unmodifiableList(Arrays.asList(
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    @Override
071    protected BaseResult execute() {
072        var warehouseControl = Session.getModelController(WarehouseControl.class);
073        var result = InventoryResultFactory.getEditInventoryLocationGroupVolumeResult();
074        var warehouseName = spec.getWarehouseName();
075        var warehouse = warehouseControl.getWarehouseByName(warehouseName);
076        
077        if(warehouse != null) {
078            var inventoryControl = Session.getModelController(InventoryControl.class);
079            var inventoryLocationGroupName = spec.getInventoryLocationGroupName();
080            var inventoryLocationGroup = inventoryControl.getInventoryLocationGroupByName(warehouse.getParty(), inventoryLocationGroupName);
081            
082            if(inventoryLocationGroup != null) {
083                var uomControl = Session.getModelController(UomControl.class);
084                var volumeUnitOfMeasureKind = uomControl.getUnitOfMeasureKindByUnitOfMeasureKindUseTypeUsingNames(UomConstants.UnitOfMeasureKindUseType_VOLUME);
085                
086                if(volumeUnitOfMeasureKind != null) {
087                    if(editMode.equals(EditMode.LOCK)) {
088                        var inventoryLocationGroupVolume = inventoryControl.getInventoryLocationGroupVolume(inventoryLocationGroup);
089                        
090                        if(inventoryLocationGroupVolume != null) {
091                            result.setInventoryLocationGroupVolume(inventoryControl.getInventoryLocationGroupVolumeTransfer(getUserVisit(), inventoryLocationGroupVolume));
092                            
093                            if(lockEntity(inventoryLocationGroup)) {
094                                var edit = InventoryEditFactory.getInventoryLocationGroupVolumeEdit();
095                                var height = inventoryLocationGroupVolume.getHeight();
096                                var heightConversion = height == null? null: new Conversion(uomControl, volumeUnitOfMeasureKind, height).convertToHighestUnitOfMeasureType();
097                                var width = inventoryLocationGroupVolume.getWidth();
098                                var widthConversion = width == null? null: new Conversion(uomControl, volumeUnitOfMeasureKind, width).convertToHighestUnitOfMeasureType();
099                                var depth = inventoryLocationGroupVolume.getDepth();
100                                var depthConversion = depth == null? null: new Conversion(uomControl, volumeUnitOfMeasureKind, depth).convertToHighestUnitOfMeasureType();
101                                
102                                result.setEdit(edit);
103                                edit.setHeight(heightConversion.getQuantity().toString());
104                                edit.setHeightUnitOfMeasureTypeName(heightConversion.getUnitOfMeasureType().getLastDetail().getUnitOfMeasureTypeName());
105                                edit.setWidth(widthConversion.getQuantity().toString());
106                                edit.setWidthUnitOfMeasureTypeName(widthConversion.getUnitOfMeasureType().getLastDetail().getUnitOfMeasureTypeName());
107                                edit.setDepth(depthConversion.getQuantity().toString());
108                                edit.setDepthUnitOfMeasureTypeName(depthConversion.getUnitOfMeasureType().getLastDetail().getUnitOfMeasureTypeName());
109                            } else {
110                                addExecutionError(ExecutionErrors.EntityLockFailed.name());
111                            }
112                            
113                            result.setEntityLock(getEntityLockTransfer(inventoryLocationGroup));
114                        } else {
115                            addExecutionError(ExecutionErrors.UnknownInventoryLocationGroupVolume.name());
116                        }
117                    } else if(editMode.equals(EditMode.UPDATE)) {
118                        var inventoryLocationGroupVolume = inventoryControl.getInventoryLocationGroupVolumeForUpdate(inventoryLocationGroup);
119                        
120                        if(inventoryLocationGroupVolume != null) {
121                            var heightUnitOfMeasureTypeName = edit.getHeightUnitOfMeasureTypeName();
122                            var heightUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind,
123                                    heightUnitOfMeasureTypeName);
124                            
125                            if(heightUnitOfMeasureType != null) {
126                                var height = Long.valueOf(edit.getHeight());
127                                
128                                if(height > 0) {
129                                    var widthUnitOfMeasureTypeName = edit.getWidthUnitOfMeasureTypeName();
130                                    var widthUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind,
131                                            widthUnitOfMeasureTypeName);
132                                    
133                                    if(widthUnitOfMeasureType != null) {
134                                        var width = Long.valueOf(edit.getWidth());
135                                        
136                                        if(width > 0) {
137                                            var depthUnitOfMeasureTypeName = edit.getDepthUnitOfMeasureTypeName();
138                                            var depthUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind,
139                                                    depthUnitOfMeasureTypeName);
140                                            
141                                            if(depthUnitOfMeasureType != null) {
142                                                var depth = Long.valueOf(edit.getDepth());
143                                                
144                                                if(depth > 0) {
145                                                    if(lockEntityForUpdate(inventoryLocationGroup)) {
146                                                        try {
147                                                            var inventoryLocationGroupVolumeValue = inventoryControl.getInventoryLocationGroupVolumeValueForUpdate(inventoryLocationGroupVolume);
148                                                            var heightConversion = new Conversion(uomControl, heightUnitOfMeasureType, height).convertToLowestUnitOfMeasureType();
149                                                            var widthConversion = new Conversion(uomControl, widthUnitOfMeasureType, width).convertToLowestUnitOfMeasureType();
150                                                            var depthConversion = new Conversion(uomControl, depthUnitOfMeasureType, depth).convertToLowestUnitOfMeasureType();
151                                                            
152                                                            inventoryLocationGroupVolumeValue.setHeight(heightConversion.getQuantity());
153                                                            inventoryLocationGroupVolumeValue.setWidth(widthConversion.getQuantity());
154                                                            inventoryLocationGroupVolumeValue.setDepth(depthConversion.getQuantity());
155                                                            
156                                                            inventoryControl.updateInventoryLocationGroupVolumeFromValue(inventoryLocationGroupVolumeValue, getPartyPK());
157                                                        } finally {
158                                                            unlockEntity(inventoryLocationGroup);
159                                                        }
160                                                    } else {
161                                                        addExecutionError(ExecutionErrors.EntityLockStale.name());
162                                                    }
163                                                } else {
164                                                    addExecutionError(ExecutionErrors.InvalidDepth.name(), depth);
165                                                }
166                                            } else {
167                                                addExecutionError(ExecutionErrors.UnknownDepthUnitOfMeasureTypeName.name(), depthUnitOfMeasureTypeName);
168                                            }
169                                        } else {
170                                            addExecutionError(ExecutionErrors.InvalidWidth.name(), width);
171                                        }
172                                    } else {
173                                        addExecutionError(ExecutionErrors.UnknownWidthUnitOfMeasureTypeName.name(), widthUnitOfMeasureTypeName);
174                                    }
175                                } else {
176                                    addExecutionError(ExecutionErrors.InvalidHeight.name(), height);
177                                }
178                            } else {
179                                addExecutionError(ExecutionErrors.UnknownHeightUnitOfMeasureTypeName.name(), heightUnitOfMeasureTypeName);
180                            }
181                        } else {
182                            addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeVolume.name());
183                        }
184
185                        if(hasExecutionErrors()) {
186                            result.setInventoryLocationGroupVolume(inventoryControl.getInventoryLocationGroupVolumeTransfer(getUserVisit(), inventoryLocationGroupVolume));
187                            result.setEntityLock(getEntityLockTransfer(inventoryLocationGroup));
188                        }
189                    }
190                } else {
191                    addExecutionError(ExecutionErrors.UnknownVolumeUnitOfMeasureKind.name());
192                }
193            } else {
194                addExecutionError(ExecutionErrors.UnknownInventoryLocationGroupName.name(), inventoryLocationGroupName);
195            }
196        } else {
197            addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName);
198        }
199        
200        return result;
201    }
202    
203}