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