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 java.util.List;
037import javax.enterprise.context.Dependent;
038import javax.inject.Inject;
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    @Inject
068    UomControl uomControl;
069
070    @Inject
071    WarehouseControl warehouseControl;
072
073    
074    /** Creates a new instance of CreateLocationVolumeCommand */
075    public CreateLocationVolumeCommand() {
076        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
077    }
078    
079    @Override
080    protected BaseResult execute() {
081        var warehouseName = form.getWarehouseName();
082        var warehouse = warehouseControl.getWarehouseByName(warehouseName);
083        
084        if(warehouse != null) {
085            var locationName = form.getLocationName();
086            var location = warehouseControl.getLocationByName(warehouse.getParty(), locationName);
087            
088            if(location != null) {
089                var locationVolume = warehouseControl.getLocationVolume(location);
090                
091                if(locationVolume == null) {
092                    var volumeUnitOfMeasureKind = uomControl.getUnitOfMeasureKindByUnitOfMeasureKindUseTypeUsingNames(UomConstants.UnitOfMeasureKindUseType_VOLUME);
093                    
094                    if(volumeUnitOfMeasureKind != null) {
095                        var heightUnitOfMeasureTypeName = form.getHeightUnitOfMeasureTypeName();
096                        var heightUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind,
097                                heightUnitOfMeasureTypeName);
098                        
099                        if(heightUnitOfMeasureType != null) {
100                            var height = Long.valueOf(form.getHeight());
101                            var widthUnitOfMeasureTypeName = form.getWidthUnitOfMeasureTypeName();
102                            var widthUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind,
103                                    widthUnitOfMeasureTypeName);
104                            
105                            if(widthUnitOfMeasureType != null) {
106                                var width = Long.valueOf(form.getWidth());
107                                var depthUnitOfMeasureTypeName = form.getDepthUnitOfMeasureTypeName();
108                                var depthUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind,
109                                        depthUnitOfMeasureTypeName);
110                                
111                                if(depthUnitOfMeasureType != null) {
112                                    var depth = Long.valueOf(form.getDepth());
113                                    var heightConversion = new Conversion(uomControl, heightUnitOfMeasureType, height).convertToLowestUnitOfMeasureType();
114                                    var widthConversion = new Conversion(uomControl, widthUnitOfMeasureType, width).convertToLowestUnitOfMeasureType();
115                                    var depthConversion = new Conversion(uomControl, depthUnitOfMeasureType, depth).convertToLowestUnitOfMeasureType();
116                                    
117                                    warehouseControl.createLocationVolume(location, heightConversion.getQuantity(),
118                                            widthConversion.getQuantity(), depthConversion.getQuantity(), getPartyPK());
119                                } else {
120                                    addExecutionError(ExecutionErrors.UnknownDepthUnitOfMeasureTypeName.name(), depthUnitOfMeasureTypeName);
121                                }
122                            } else {
123                                addExecutionError(ExecutionErrors.UnknownWidthUnitOfMeasureTypeName.name(), widthUnitOfMeasureTypeName);
124                            }
125                        } else {
126                            addExecutionError(ExecutionErrors.UnknownHeightUnitOfMeasureTypeName.name(), heightUnitOfMeasureTypeName);
127                        }
128                    } else {
129                        addExecutionError(ExecutionErrors.UnknownVolumeUnitOfMeasureKind.name());
130                    }
131                } else {
132                    addExecutionError(ExecutionErrors.DuplicateLocationVolume.name());
133                }
134            } else {
135                addExecutionError(ExecutionErrors.UnknownLocationName.name(), locationName);
136            }
137        } else {
138            addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName);
139        }
140        
141        return null;
142    }
143    
144}