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