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