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