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.InventoryLocationGroupControl;
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 java.util.List;
032import javax.inject.Inject;
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    @Inject
055    InventoryLocationGroupControl inventoryLocationGroupControl;
056
057    @Inject
058    UomControl uomControl;
059
060    @Inject
061    WarehouseControl warehouseControl;
062
063    /** Creates a new instance of CreateInventoryLocationGroupVolumeCommand */
064    public CreateInventoryLocationGroupVolumeCommand() {
065        super(null, FORM_FIELD_DEFINITIONS, false);
066    }
067
068    @Override
069    protected BaseResult execute() {
070        var warehouseName = form.getWarehouseName();
071        var warehouse = warehouseControl.getWarehouseByName(warehouseName);
072
073        if(warehouse != null) {
074            var inventoryLocationGroupName = form.getInventoryLocationGroupName();
075            var inventoryLocationGroup = inventoryLocationGroupControl.getInventoryLocationGroupByName(warehouse.getParty(),
076                    inventoryLocationGroupName);
077
078            if(inventoryLocationGroup != null) {
079                var inventoryLocationGroupVolume = inventoryLocationGroupControl.getInventoryLocationGroupVolume(inventoryLocationGroup);
080
081                if(inventoryLocationGroupVolume == null) {
082                    var volumeUnitOfMeasureKind = uomControl.getUnitOfMeasureKindByUnitOfMeasureKindUseTypeUsingNames(UomConstants.UnitOfMeasureKindUseType_VOLUME);
083
084                    if(volumeUnitOfMeasureKind != null) {
085                        var heightUnitOfMeasureTypeName = form.getHeightUnitOfMeasureTypeName();
086                        var heightUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind,
087                                heightUnitOfMeasureTypeName);
088
089                        if(heightUnitOfMeasureType != null) {
090                            var height = Long.valueOf(form.getHeight());
091
092                            if(height > 0) {
093                                var widthUnitOfMeasureTypeName = form.getWidthUnitOfMeasureTypeName();
094                                var widthUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind,
095                                        widthUnitOfMeasureTypeName);
096
097                                if(widthUnitOfMeasureType != null) {
098                                    var width = Long.valueOf(form.getWidth());
099
100                                    if(width > 0) {
101                                        var depthUnitOfMeasureTypeName = form.getDepthUnitOfMeasureTypeName();
102                                        var depthUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind,
103                                                depthUnitOfMeasureTypeName);
104
105                                        if(depthUnitOfMeasureType != null) {
106                                            var depth = Long.valueOf(form.getDepth());
107
108                                            if(depth > 0) {
109                                                var heightConversion = new Conversion(uomControl, heightUnitOfMeasureType, height).convertToLowestUnitOfMeasureType();
110                                                var widthConversion = new Conversion(uomControl, widthUnitOfMeasureType, width).convertToLowestUnitOfMeasureType();
111                                                var depthConversion = new Conversion(uomControl, depthUnitOfMeasureType, depth).convertToLowestUnitOfMeasureType();
112
113                                                inventoryLocationGroupControl.createInventoryLocationGroupVolume(inventoryLocationGroup, heightConversion.getQuantity(),
114                                                        widthConversion.getQuantity(), depthConversion.getQuantity(), getPartyPK());
115                                            } else {
116                                                addExecutionError(ExecutionErrors.InvalidDepth.name(), depth);
117                                            }
118                                        } else {
119                                            addExecutionError(ExecutionErrors.UnknownDepthUnitOfMeasureTypeName.name(), depthUnitOfMeasureTypeName);
120                                        }
121                                    } else {
122                                        addExecutionError(ExecutionErrors.InvalidWidth.name(), width);
123                                    }
124                                } else {
125                                    addExecutionError(ExecutionErrors.UnknownWidthUnitOfMeasureTypeName.name(), widthUnitOfMeasureTypeName);
126                                }
127                            } else {
128                                addExecutionError(ExecutionErrors.InvalidHeight.name(), height);
129                            }
130                        } else {
131                            addExecutionError(ExecutionErrors.UnknownHeightUnitOfMeasureTypeName.name(), heightUnitOfMeasureTypeName);
132                        }
133                    } else {
134                        addExecutionError(ExecutionErrors.UnknownVolumeUnitOfMeasureKind.name());
135                    }
136                } else {
137                    addExecutionError(ExecutionErrors.DuplicateInventoryLocationGroupVolume.name());
138                }
139            } else {
140                addExecutionError(ExecutionErrors.UnknownInventoryLocationGroupName.name(), inventoryLocationGroupName);
141            }
142        } else {
143            addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName);
144        }
145
146        return null;
147    }
148
149}