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