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.form.CreateInventoryLocationGroupVolumeForm;
020import com.echothree.model.control.inventory.server.control.InventoryControl;
021import com.echothree.model.control.uom.common.UomConstants;
022import com.echothree.model.control.uom.server.control.UomControl;
023import com.echothree.model.control.uom.server.util.Conversion;
024import com.echothree.model.control.warehouse.server.control.WarehouseControl;
025import com.echothree.model.data.user.common.pk.UserVisitPK;
026import com.echothree.util.common.message.ExecutionErrors;
027import com.echothree.util.common.validation.FieldDefinition;
028import com.echothree.util.common.validation.FieldType;
029import com.echothree.util.common.command.BaseResult;
030import com.echothree.util.server.control.BaseSimpleCommand;
031import com.echothree.util.server.persistence.Session;
032import java.util.Arrays;
033import java.util.Collections;
034import java.util.List;
035import javax.enterprise.context.RequestScoped;
036
037@RequestScoped
038public class CreateInventoryLocationGroupVolumeCommand
039        extends BaseSimpleCommand<CreateInventoryLocationGroupVolumeForm> {
040    
041    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
042    
043    static {
044        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
045                new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, true, null, null),
046                new FieldDefinition("InventoryLocationGroupName", FieldType.ENTITY_NAME, true, null, null),
047                new FieldDefinition("HeightUnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null),
048                new FieldDefinition("Height", FieldType.UNSIGNED_LONG, true, null, null),
049                new FieldDefinition("WidthUnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null),
050                new FieldDefinition("Width", FieldType.UNSIGNED_LONG, true, null, null),
051                new FieldDefinition("DepthUnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null),
052                new FieldDefinition("Depth", FieldType.UNSIGNED_LONG, true, null, null)
053                ));
054    }
055    
056    /** Creates a new instance of CreateInventoryLocationGroupVolumeCommand */
057    public CreateInventoryLocationGroupVolumeCommand() {
058        super(null, FORM_FIELD_DEFINITIONS, false);
059    }
060    
061    @Override
062    protected BaseResult execute() {
063        var warehouseControl = Session.getModelController(WarehouseControl.class);
064        var warehouseName = form.getWarehouseName();
065        var warehouse = warehouseControl.getWarehouseByName(warehouseName);
066        
067        if(warehouse != null) {
068            var inventoryControl = Session.getModelController(InventoryControl.class);
069            var inventoryLocationGroupName = form.getInventoryLocationGroupName();
070            var inventoryLocationGroup = inventoryControl.getInventoryLocationGroupByName(warehouse.getParty(),
071                    inventoryLocationGroupName);
072            
073            if(inventoryLocationGroup != null) {
074                var inventoryLocationGroupVolume = inventoryControl.getInventoryLocationGroupVolume(inventoryLocationGroup);
075                
076                if(inventoryLocationGroupVolume == null) {
077                    var uomControl = Session.getModelController(UomControl.class);
078                    var volumeUnitOfMeasureKind = uomControl.getUnitOfMeasureKindByUnitOfMeasureKindUseTypeUsingNames(UomConstants.UnitOfMeasureKindUseType_VOLUME);
079                    
080                    if(volumeUnitOfMeasureKind != null) {
081                        var heightUnitOfMeasureTypeName = form.getHeightUnitOfMeasureTypeName();
082                        var heightUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind,
083                                heightUnitOfMeasureTypeName);
084                        
085                        if(heightUnitOfMeasureType != null) {
086                            var height = Long.valueOf(form.getHeight());
087                            
088                            if(height > 0) {
089                                var widthUnitOfMeasureTypeName = form.getWidthUnitOfMeasureTypeName();
090                                var widthUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind,
091                                        widthUnitOfMeasureTypeName);
092                                
093                                if(widthUnitOfMeasureType != null) {
094                                    var width = Long.valueOf(form.getWidth());
095                                    
096                                    if(width > 0) {
097                                        var depthUnitOfMeasureTypeName = form.getDepthUnitOfMeasureTypeName();
098                                        var depthUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind,
099                                                depthUnitOfMeasureTypeName);
100                                        
101                                        if(depthUnitOfMeasureType != null) {
102                                            var depth = Long.valueOf(form.getDepth());
103                                            
104                                            if(depth > 0) {
105                                                var heightConversion = new Conversion(uomControl, heightUnitOfMeasureType, height).convertToLowestUnitOfMeasureType();
106                                                var widthConversion = new Conversion(uomControl, widthUnitOfMeasureType, width).convertToLowestUnitOfMeasureType();
107                                                var depthConversion = new Conversion(uomControl, depthUnitOfMeasureType, depth).convertToLowestUnitOfMeasureType();
108                                                
109                                                inventoryControl.createInventoryLocationGroupVolume(inventoryLocationGroup, heightConversion.getQuantity(),
110                                                        widthConversion.getQuantity(), depthConversion.getQuantity(), getPartyPK());
111                                            } else {
112                                                addExecutionError(ExecutionErrors.InvalidDepth.name(), depth);
113                                            }
114                                        } else {
115                                            addExecutionError(ExecutionErrors.UnknownDepthUnitOfMeasureTypeName.name(), depthUnitOfMeasureTypeName);
116                                        }
117                                    } else {
118                                        addExecutionError(ExecutionErrors.InvalidWidth.name(), width);
119                                    }
120                                } else {
121                                    addExecutionError(ExecutionErrors.UnknownWidthUnitOfMeasureTypeName.name(), widthUnitOfMeasureTypeName);
122                                }
123                            } else {
124                                addExecutionError(ExecutionErrors.InvalidHeight.name(), height);
125                            }
126                        } else {
127                            addExecutionError(ExecutionErrors.UnknownHeightUnitOfMeasureTypeName.name(), heightUnitOfMeasureTypeName);
128                        }
129                    } else {
130                        addExecutionError(ExecutionErrors.UnknownVolumeUnitOfMeasureKind.name());
131                    }
132                } else {
133                    addExecutionError(ExecutionErrors.DuplicateInventoryLocationGroupVolume.name());
134                }
135            } else {
136                addExecutionError(ExecutionErrors.UnknownInventoryLocationGroupName.name(), inventoryLocationGroupName);
137            }
138        } else {
139            addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName);
140        }
141        
142        return null;
143    }
144    
145}