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.result.EditLocationVolumeResult;
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.uom.server.entity.UnitOfMeasureKind;
032import com.echothree.model.data.warehouse.server.entity.Location;
033import com.echothree.model.data.warehouse.server.entity.LocationVolume;
034import com.echothree.util.common.command.EditMode;
035import com.echothree.util.common.message.ExecutionErrors;
036import com.echothree.util.common.validation.FieldDefinition;
037import com.echothree.util.common.validation.FieldType;
038import com.echothree.util.server.control.BaseAbstractEditCommand;
039import com.echothree.util.server.control.CommandSecurityDefinition;
040import com.echothree.util.server.control.PartyTypeDefinition;
041import com.echothree.util.server.control.SecurityRoleDefinition;
042import java.util.List;
043import javax.enterprise.context.Dependent;
044import javax.inject.Inject;
045
046@Dependent
047public class EditLocationVolumeCommand
048        extends BaseAbstractEditCommand<LocationSpec, LocationVolumeEdit, EditLocationVolumeResult, LocationVolume, Location> {
049
050    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
051    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
052    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
053    
054    static {
055        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
056                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
057                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
058                        new SecurityRoleDefinition(SecurityRoleGroups.LocationVolume.name(), SecurityRoles.Delete.name())
059                ))
060        ));
061
062        SPEC_FIELD_DEFINITIONS = List.of(
063                new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, true, null, null),
064                new FieldDefinition("LocationName", FieldType.ENTITY_NAME, true, null, null)
065        );
066        
067        EDIT_FIELD_DEFINITIONS = List.of(
068                new FieldDefinition("HeightUnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null),
069                new FieldDefinition("Height", FieldType.UNSIGNED_LONG, true, null, null),
070                new FieldDefinition("WidthUnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null),
071                new FieldDefinition("Width", FieldType.UNSIGNED_LONG, true, null, null),
072                new FieldDefinition("DepthUnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null),
073                new FieldDefinition("Depth", FieldType.UNSIGNED_LONG, true, null, null)
074        );
075    }
076
077    @Inject
078    UomControl uomControl;
079
080    @Inject
081    WarehouseControl warehouseControl;
082
083
084    /** Creates a new instance of EditLocationVolumeCommand */
085    public EditLocationVolumeCommand() {
086        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
087    }
088
089    @Override
090    protected EditLocationVolumeResult getResult() {
091        return WarehouseResultFactory.getEditLocationVolumeResult();
092    }
093
094    @Override
095    protected LocationVolumeEdit getEdit() {
096        return WarehouseEditFactory.getLocationVolumeEdit();
097    }
098
099    @Override
100    protected LocationVolume getEntity(EditLocationVolumeResult result) {
101        LocationVolume locationVolume = null;
102        var warehouseName = spec.getWarehouseName();
103        var warehouse = warehouseControl.getWarehouseByName(warehouseName);
104
105        if(warehouse != null) {
106            var locationName = spec.getLocationName();
107            var location = warehouseControl.getLocationByName(warehouse.getParty(), locationName);
108
109            if(location != null) {
110                locationVolume = warehouseControl.getLocationVolume(location, editModeToEntityPermission(editMode));
111
112                if(locationVolume == null) {
113                    addExecutionError(ExecutionErrors.UnknownLocationVolume.name(),
114                            warehouse.getWarehouseName(), location.getLastDetail().getLocationName());
115                }
116            } else {
117                addExecutionError(ExecutionErrors.UnknownLocationName.name(), warehouse.getWarehouseName(), locationName);
118            }
119        } else {
120            addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName);
121        }
122
123        return locationVolume;
124    }
125
126    @Override
127    protected Location getLockEntity(LocationVolume locationVolume) {
128        return locationVolume.getLocation();
129    }
130
131    @Override
132    protected void fillInResult(EditLocationVolumeResult result, LocationVolume locationVolume) {
133        result.setLocationVolume(warehouseControl.getLocationVolumeTransfer(getUserVisit(), locationVolume));
134    }
135
136    UnitOfMeasureKind volumeUnitOfMeasureKind;
137
138    private UnitOfMeasureKind getVolumeUnitOfMeasureKind() {
139        if(volumeUnitOfMeasureKind == null) {
140            volumeUnitOfMeasureKind = uomControl.getUnitOfMeasureKindByUnitOfMeasureKindUseTypeUsingNames(UomConstants.UnitOfMeasureKindUseType_VOLUME);
141        }
142
143        return volumeUnitOfMeasureKind;
144    }
145
146    @Override
147    protected void doLock(LocationVolumeEdit edit, LocationVolume locationVolume) {
148        var volumeUnitOfMeasureKind = getVolumeUnitOfMeasureKind();
149        var height = locationVolume.getHeight();
150        var heightConversion = height == null? null: new Conversion(uomControl, volumeUnitOfMeasureKind, height).convertToHighestUnitOfMeasureType();
151        var width = locationVolume.getWidth();
152        var widthConversion = width == null? null: new Conversion(uomControl, volumeUnitOfMeasureKind, width).convertToHighestUnitOfMeasureType();
153        var depth = locationVolume.getDepth();
154        var depthConversion = depth == null? null: new Conversion(uomControl, volumeUnitOfMeasureKind, depth).convertToHighestUnitOfMeasureType();
155
156        if(heightConversion != null) {
157            edit.setHeight(heightConversion.getQuantity().toString());
158            edit.setHeightUnitOfMeasureTypeName(heightConversion.getUnitOfMeasureType().getLastDetail().getUnitOfMeasureTypeName());
159        }
160
161        if(widthConversion != null) {
162            edit.setWidth(widthConversion.getQuantity().toString());
163            edit.setWidthUnitOfMeasureTypeName(widthConversion.getUnitOfMeasureType().getLastDetail().getUnitOfMeasureTypeName());
164        }
165
166        if(depthConversion != null) {
167            edit.setDepth(depthConversion.getQuantity().toString());
168            edit.setDepthUnitOfMeasureTypeName(depthConversion.getUnitOfMeasureType().getLastDetail().getUnitOfMeasureTypeName());
169        }
170    }
171
172    Long height;
173    Long width;
174    Long depth;
175
176    @Override
177    protected void canUpdate(LocationVolume locationVolume) {
178        var volumeUnitOfMeasureKind = getVolumeUnitOfMeasureKind();
179        var heightUnitOfMeasureTypeName = edit.getHeightUnitOfMeasureTypeName();
180        var heightUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind, heightUnitOfMeasureTypeName);
181
182        if(heightUnitOfMeasureType != null) {
183            var heightValue = Long.valueOf(edit.getHeight());
184
185            if(heightValue > 0) {
186                var widthUnitOfMeasureTypeName = edit.getWidthUnitOfMeasureTypeName();
187                var widthUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind, widthUnitOfMeasureTypeName);
188
189                if(widthUnitOfMeasureType != null) {
190                    var widthValue = Long.valueOf(edit.getWidth());
191
192                    if(widthValue > 0) {
193                        var depthUnitOfMeasureTypeName = edit.getDepthUnitOfMeasureTypeName();
194                        var depthUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind, depthUnitOfMeasureTypeName);
195
196                        if(depthUnitOfMeasureType != null) {
197                            var depthValue = Long.valueOf(edit.getDepth());
198
199                            if(depthValue > 0) {
200                                height = new Conversion(uomControl, heightUnitOfMeasureType, heightValue).convertToLowestUnitOfMeasureType().getQuantity();
201                                width = new Conversion(uomControl, widthUnitOfMeasureType, widthValue).convertToLowestUnitOfMeasureType().getQuantity();
202                                depth = new Conversion(uomControl, depthUnitOfMeasureType, depthValue).convertToLowestUnitOfMeasureType().getQuantity();
203                            } else {
204                                addExecutionError(ExecutionErrors.InvalidDepth.name(), depthValue);
205                            }
206                        } else {
207                            addExecutionError(ExecutionErrors.UnknownDepthUnitOfMeasureTypeName.name(), depthUnitOfMeasureTypeName);
208                        }
209                    } else {
210                        addExecutionError(ExecutionErrors.InvalidWidth.name(), widthValue);
211                    }
212                } else {
213                    addExecutionError(ExecutionErrors.UnknownWidthUnitOfMeasureTypeName.name(), widthUnitOfMeasureTypeName);
214                }
215            } else {
216                addExecutionError(ExecutionErrors.InvalidHeight.name(), heightValue);
217            }
218        } else {
219            addExecutionError(ExecutionErrors.UnknownHeightUnitOfMeasureTypeName.name(), heightUnitOfMeasureTypeName);
220        }
221    }
222
223    @Override
224    protected void doUpdate(LocationVolume locationVolume) {
225        var locationVolumeValue = locationVolume.getLocationVolumeValue().clone();
226
227        locationVolumeValue.setHeight(height);
228        locationVolumeValue.setWidth(width);
229        locationVolumeValue.setDepth(depth);
230
231        warehouseControl.updateLocationVolumeFromValue(locationVolumeValue, getPartyPK());
232    }
233
234}