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.edit.LocationVolumeEdit;
020import com.echothree.control.user.warehouse.common.edit.WarehouseEditFactory;
021import com.echothree.control.user.warehouse.common.form.EditLocationVolumeForm;
022import com.echothree.control.user.warehouse.common.result.WarehouseResultFactory;
023import com.echothree.control.user.warehouse.common.spec.LocationSpec;
024import com.echothree.model.control.party.common.PartyTypes;
025import com.echothree.model.control.security.common.SecurityRoleGroups;
026import com.echothree.model.control.security.common.SecurityRoles;
027import com.echothree.model.control.uom.common.UomConstants;
028import com.echothree.model.control.uom.server.control.UomControl;
029import com.echothree.model.control.uom.server.util.Conversion;
030import com.echothree.model.control.warehouse.server.control.WarehouseControl;
031import com.echothree.model.data.user.common.pk.UserVisitPK;
032import com.echothree.util.common.command.BaseResult;
033import com.echothree.util.common.command.EditMode;
034import com.echothree.util.common.message.ExecutionErrors;
035import com.echothree.util.common.validation.FieldDefinition;
036import com.echothree.util.common.validation.FieldType;
037import com.echothree.util.server.control.BaseEditCommand;
038import com.echothree.util.server.control.CommandSecurityDefinition;
039import com.echothree.util.server.control.PartyTypeDefinition;
040import com.echothree.util.server.control.SecurityRoleDefinition;
041import com.echothree.util.server.persistence.Session;
042import java.util.List;
043import javax.enterprise.context.Dependent;
044
045@Dependent
046public class EditLocationVolumeCommand
047        extends BaseEditCommand<LocationSpec, LocationVolumeEdit> {
048
049    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
050    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
051    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
052    
053    static {
054        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
055                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
056                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
057                        new SecurityRoleDefinition(SecurityRoleGroups.LocationVolume.name(), SecurityRoles.Delete.name())
058                ))
059        ));
060
061        SPEC_FIELD_DEFINITIONS = List.of(
062                new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, true, null, null),
063                new FieldDefinition("LocationName", FieldType.ENTITY_NAME, true, null, null)
064                );
065        
066        EDIT_FIELD_DEFINITIONS = List.of(
067                new FieldDefinition("HeightUnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null),
068                new FieldDefinition("Height", FieldType.UNSIGNED_LONG, true, null, null),
069                new FieldDefinition("WidthUnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null),
070                new FieldDefinition("Width", FieldType.UNSIGNED_LONG, true, null, null),
071                new FieldDefinition("DepthUnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null),
072                new FieldDefinition("Depth", FieldType.UNSIGNED_LONG, true, null, null)
073                );
074    }
075    
076    /** Creates a new instance of EditLocationVolumeCommand */
077    public EditLocationVolumeCommand() {
078        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
079    }
080    
081    @Override
082    protected BaseResult execute() {
083        var warehouseControl = Session.getModelController(WarehouseControl.class);
084        var result = WarehouseResultFactory.getEditLocationVolumeResult();
085        var warehouseName = spec.getWarehouseName();
086        var warehouse = warehouseControl.getWarehouseByName(warehouseName);
087        
088        if(warehouse != null) {
089            var locationName = spec.getLocationName();
090            var location = warehouseControl.getLocationByName(warehouse.getParty(), locationName);
091            
092            if(location != null) {
093                var uomControl = Session.getModelController(UomControl.class);
094                var volumeUnitOfMeasureKind = uomControl.getUnitOfMeasureKindByUnitOfMeasureKindUseTypeUsingNames(UomConstants.UnitOfMeasureKindUseType_VOLUME);
095                
096                if(volumeUnitOfMeasureKind != null) {
097                    if(editMode.equals(EditMode.LOCK)) {
098                        var locationVolume = warehouseControl.getLocationVolume(location);
099                        
100                        if(locationVolume != null) {
101                            result.setLocationVolume(warehouseControl.getLocationVolumeTransfer(getUserVisit(), locationVolume));
102                            
103                            if(lockEntity(location)) {
104                                var edit = WarehouseEditFactory.getLocationVolumeEdit();
105                                var height = locationVolume.getHeight();
106                                var heightConversion = height == null? null: new Conversion(uomControl, volumeUnitOfMeasureKind, height).convertToHighestUnitOfMeasureType();
107                                var width = locationVolume.getWidth();
108                                var widthConversion = width == null? null: new Conversion(uomControl, volumeUnitOfMeasureKind, width).convertToHighestUnitOfMeasureType();
109                                var depth = locationVolume.getDepth();
110                                var depthConversion = depth == null? null: new Conversion(uomControl, volumeUnitOfMeasureKind, depth).convertToHighestUnitOfMeasureType();
111                                
112                                result.setEdit(edit);
113                                edit.setHeight(heightConversion.getQuantity().toString());
114                                edit.setHeightUnitOfMeasureTypeName(heightConversion.getUnitOfMeasureType().getLastDetail().getUnitOfMeasureTypeName());
115                                edit.setWidth(widthConversion.getQuantity().toString());
116                                edit.setWidthUnitOfMeasureTypeName(widthConversion.getUnitOfMeasureType().getLastDetail().getUnitOfMeasureTypeName());
117                                edit.setDepth(depthConversion.getQuantity().toString());
118                                edit.setDepthUnitOfMeasureTypeName(depthConversion.getUnitOfMeasureType().getLastDetail().getUnitOfMeasureTypeName());
119                            } else {
120                                addExecutionError(ExecutionErrors.EntityLockFailed.name());
121                            }
122                            
123                            result.setEntityLock(getEntityLockTransfer(location));
124                        } else {
125                            addExecutionError(ExecutionErrors.UnknownLocationVolume.name());
126                        }
127                    } else if(editMode.equals(EditMode.UPDATE)) {
128                        var locationVolumeValue = warehouseControl.getLocationVolumeValueForUpdate(location);
129                        
130                        if(locationVolumeValue != null) {
131                            var heightUnitOfMeasureTypeName = edit.getHeightUnitOfMeasureTypeName();
132                            var heightUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind,
133                                    heightUnitOfMeasureTypeName);
134                            
135                            if(heightUnitOfMeasureType != null) {
136                                var height = Long.valueOf(edit.getHeight());
137                                
138                                if(height > 0) {
139                                    var widthUnitOfMeasureTypeName = edit.getWidthUnitOfMeasureTypeName();
140                                    var widthUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind,
141                                            widthUnitOfMeasureTypeName);
142                                    
143                                    if(widthUnitOfMeasureType != null) {
144                                        var width = Long.valueOf(edit.getWidth());
145                                        
146                                        if(width > 0) {
147                                            var depthUnitOfMeasureTypeName = edit.getDepthUnitOfMeasureTypeName();
148                                            var depthUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind,
149                                                    depthUnitOfMeasureTypeName);
150                                            
151                                            if(depthUnitOfMeasureType != null) {
152                                                var depth = Long.valueOf(edit.getDepth());
153                                                
154                                                if(depth > 0) {
155                                                    if(lockEntityForUpdate(location)) {
156                                                        try {
157                                                            var heightConversion = new Conversion(uomControl, heightUnitOfMeasureType, height).convertToLowestUnitOfMeasureType();
158                                                            var widthConversion = new Conversion(uomControl, widthUnitOfMeasureType, width).convertToLowestUnitOfMeasureType();
159                                                            var depthConversion = new Conversion(uomControl, depthUnitOfMeasureType, depth).convertToLowestUnitOfMeasureType();
160                                                            
161                                                            locationVolumeValue.setHeight(heightConversion.getQuantity());
162                                                            locationVolumeValue.setWidth(widthConversion.getQuantity());
163                                                            locationVolumeValue.setDepth(depthConversion.getQuantity());
164                                                            
165                                                            warehouseControl.updateLocationVolumeFromValue(locationVolumeValue, getPartyPK());
166                                                        } finally {
167                                                            unlockEntity(location);
168                                                        }
169                                                    } else {
170                                                        addExecutionError(ExecutionErrors.EntityLockStale.name());
171                                                    }
172                                                } else {
173                                                    addExecutionError(ExecutionErrors.InvalidDepth.name(), depth);
174                                                }
175                                            } else {
176                                                addExecutionError(ExecutionErrors.UnknownDepthUnitOfMeasureTypeName.name(), depthUnitOfMeasureTypeName);
177                                            }
178                                        } else {
179                                            addExecutionError(ExecutionErrors.InvalidWidth.name(), width);
180                                        }
181                                    } else {
182                                        addExecutionError(ExecutionErrors.UnknownWidthUnitOfMeasureTypeName.name(), widthUnitOfMeasureTypeName);
183                                    }
184                                } else {
185                                    addExecutionError(ExecutionErrors.InvalidHeight.name(), height);
186                                }
187                            } else {
188                                addExecutionError(ExecutionErrors.UnknownHeightUnitOfMeasureTypeName.name(), heightUnitOfMeasureTypeName);
189                            }
190                        } else {
191                            addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeVolume.name());
192                        }
193                    }
194                } else {
195                    addExecutionError(ExecutionErrors.UnknownVolumeUnitOfMeasureKind.name());
196                }
197            } else {
198                addExecutionError(ExecutionErrors.UnknownLocationName.name(), locationName);
199            }
200        } else {
201            addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName);
202        }
203        
204        return result;
205    }
206    
207}