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 /** Creates a new instance of EditLocationVolumeCommand */ 078 public EditLocationVolumeCommand() { 079 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 080 } 081 082 @Inject 083 UomControl uomControl; 084 085 @Inject 086 WarehouseControl warehouseControl; 087 088 @Override 089 protected EditLocationVolumeResult getResult() { 090 return WarehouseResultFactory.getEditLocationVolumeResult(); 091 } 092 093 @Override 094 protected LocationVolumeEdit getEdit() { 095 return WarehouseEditFactory.getLocationVolumeEdit(); 096 } 097 098 @Override 099 protected LocationVolume getEntity(EditLocationVolumeResult result) { 100 LocationVolume locationVolume = null; 101 var warehouseName = spec.getWarehouseName(); 102 var warehouse = warehouseControl.getWarehouseByName(warehouseName); 103 104 if(warehouse != null) { 105 var locationName = spec.getLocationName(); 106 var location = warehouseControl.getLocationByName(warehouse.getParty(), locationName); 107 108 if(location != null) { 109 locationVolume = warehouseControl.getLocationVolume(location, editModeToEntityPermission(editMode)); 110 111 if(locationVolume == null) { 112 addExecutionError(ExecutionErrors.UnknownLocationVolume.name(), 113 warehouse.getWarehouseName(), location.getLastDetail().getLocationName()); 114 } 115 } else { 116 addExecutionError(ExecutionErrors.UnknownLocationName.name(), warehouse.getWarehouseName(), locationName); 117 } 118 } else { 119 addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName); 120 } 121 122 return locationVolume; 123 } 124 125 @Override 126 protected Location getLockEntity(LocationVolume locationVolume) { 127 return locationVolume.getLocation(); 128 } 129 130 @Override 131 protected void fillInResult(EditLocationVolumeResult result, LocationVolume locationVolume) { 132 result.setLocationVolume(warehouseControl.getLocationVolumeTransfer(getUserVisit(), locationVolume)); 133 } 134 135 UnitOfMeasureKind volumeUnitOfMeasureKind; 136 137 private UnitOfMeasureKind getVolumeUnitOfMeasureKind() { 138 if(volumeUnitOfMeasureKind == null) { 139 volumeUnitOfMeasureKind = uomControl.getUnitOfMeasureKindByUnitOfMeasureKindUseTypeUsingNames(UomConstants.UnitOfMeasureKindUseType_VOLUME); 140 } 141 142 return volumeUnitOfMeasureKind; 143 } 144 145 @Override 146 protected void doLock(LocationVolumeEdit edit, LocationVolume locationVolume) { 147 var volumeUnitOfMeasureKind = getVolumeUnitOfMeasureKind(); 148 var height = locationVolume.getHeight(); 149 var heightConversion = height == null? null: new Conversion(uomControl, volumeUnitOfMeasureKind, height).convertToHighestUnitOfMeasureType(); 150 var width = locationVolume.getWidth(); 151 var widthConversion = width == null? null: new Conversion(uomControl, volumeUnitOfMeasureKind, width).convertToHighestUnitOfMeasureType(); 152 var depth = locationVolume.getDepth(); 153 var depthConversion = depth == null? null: new Conversion(uomControl, volumeUnitOfMeasureKind, depth).convertToHighestUnitOfMeasureType(); 154 155 if(heightConversion != null) { 156 edit.setHeight(heightConversion.getQuantity().toString()); 157 edit.setHeightUnitOfMeasureTypeName(heightConversion.getUnitOfMeasureType().getLastDetail().getUnitOfMeasureTypeName()); 158 } 159 160 if(widthConversion != null) { 161 edit.setWidth(widthConversion.getQuantity().toString()); 162 edit.setWidthUnitOfMeasureTypeName(widthConversion.getUnitOfMeasureType().getLastDetail().getUnitOfMeasureTypeName()); 163 } 164 165 if(depthConversion != null) { 166 edit.setDepth(depthConversion.getQuantity().toString()); 167 edit.setDepthUnitOfMeasureTypeName(depthConversion.getUnitOfMeasureType().getLastDetail().getUnitOfMeasureTypeName()); 168 } 169 } 170 171 Long height; 172 Long width; 173 Long depth; 174 175 @Override 176 protected void canUpdate(LocationVolume locationVolume) { 177 var volumeUnitOfMeasureKind = getVolumeUnitOfMeasureKind(); 178 var heightUnitOfMeasureTypeName = edit.getHeightUnitOfMeasureTypeName(); 179 var heightUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind, heightUnitOfMeasureTypeName); 180 181 if(heightUnitOfMeasureType != null) { 182 var heightValue = Long.valueOf(edit.getHeight()); 183 184 if(heightValue > 0) { 185 var widthUnitOfMeasureTypeName = edit.getWidthUnitOfMeasureTypeName(); 186 var widthUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind, widthUnitOfMeasureTypeName); 187 188 if(widthUnitOfMeasureType != null) { 189 var widthValue = Long.valueOf(edit.getWidth()); 190 191 if(widthValue > 0) { 192 var depthUnitOfMeasureTypeName = edit.getDepthUnitOfMeasureTypeName(); 193 var depthUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind, depthUnitOfMeasureTypeName); 194 195 if(depthUnitOfMeasureType != null) { 196 var depthValue = Long.valueOf(edit.getDepth()); 197 198 if(depthValue > 0) { 199 height = new Conversion(uomControl, heightUnitOfMeasureType, heightValue).convertToLowestUnitOfMeasureType().getQuantity(); 200 width = new Conversion(uomControl, widthUnitOfMeasureType, widthValue).convertToLowestUnitOfMeasureType().getQuantity(); 201 depth = new Conversion(uomControl, depthUnitOfMeasureType, depthValue).convertToLowestUnitOfMeasureType().getQuantity(); 202 } else { 203 addExecutionError(ExecutionErrors.InvalidDepth.name(), depthValue); 204 } 205 } else { 206 addExecutionError(ExecutionErrors.UnknownDepthUnitOfMeasureTypeName.name(), depthUnitOfMeasureTypeName); 207 } 208 } else { 209 addExecutionError(ExecutionErrors.InvalidWidth.name(), widthValue); 210 } 211 } else { 212 addExecutionError(ExecutionErrors.UnknownWidthUnitOfMeasureTypeName.name(), widthUnitOfMeasureTypeName); 213 } 214 } else { 215 addExecutionError(ExecutionErrors.InvalidHeight.name(), heightValue); 216 } 217 } else { 218 addExecutionError(ExecutionErrors.UnknownHeightUnitOfMeasureTypeName.name(), heightUnitOfMeasureTypeName); 219 } 220 } 221 222 @Override 223 protected void doUpdate(LocationVolume locationVolume) { 224 var locationVolumeValue = locationVolume.getLocationVolumeValue().clone(); 225 226 locationVolumeValue.setHeight(height); 227 locationVolumeValue.setWidth(width); 228 locationVolumeValue.setDepth(depth); 229 230 warehouseControl.updateLocationVolumeFromValue(locationVolumeValue, getPartyPK()); 231 } 232 233}