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