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