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.LocationEdit; 020import com.echothree.control.user.warehouse.common.edit.WarehouseEditFactory; 021import com.echothree.control.user.warehouse.common.result.EditLocationResult; 022import com.echothree.control.user.warehouse.common.result.WarehouseResultFactory; 023import com.echothree.control.user.warehouse.common.spec.LocationSpec; 024import com.echothree.model.control.inventory.server.control.InventoryControl; 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.warehouse.server.control.WarehouseControl; 029import com.echothree.model.control.warehouse.server.logic.LocationLogic; 030import com.echothree.model.control.warehouse.server.logic.LocationUseTypeLogic; 031import com.echothree.model.data.inventory.server.entity.InventoryLocationGroup; 032import com.echothree.model.data.warehouse.server.entity.Location; 033import com.echothree.model.data.warehouse.server.entity.LocationType; 034import com.echothree.model.data.warehouse.server.entity.LocationUseType; 035import com.echothree.model.data.warehouse.server.entity.Warehouse; 036import com.echothree.util.common.message.ExecutionErrors; 037import com.echothree.util.common.validation.FieldDefinition; 038import com.echothree.util.common.validation.FieldType; 039import com.echothree.util.server.control.BaseAbstractEditCommand; 040import com.echothree.util.server.control.CommandSecurityDefinition; 041import com.echothree.util.server.control.PartyTypeDefinition; 042import com.echothree.util.server.control.SecurityRoleDefinition; 043import java.util.List; 044import javax.enterprise.context.Dependent; 045import javax.inject.Inject; 046 047@Dependent 048public class EditLocationCommand 049 extends BaseAbstractEditCommand<LocationSpec, LocationEdit, EditLocationResult, Location, Location> { 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.Location.name(), SecurityRoles.Edit.name()) 060 )) 061 )); 062 063 SPEC_FIELD_DEFINITIONS = List.of( 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 = List.of( 069 new FieldDefinition("LocationName", FieldType.ENTITY_NAME, true, null, null), 070 new FieldDefinition("LocationTypeName", FieldType.ENTITY_NAME, true, null, null), 071 new FieldDefinition("LocationUseTypeName", FieldType.ENTITY_NAME, true, null, null), 072 new FieldDefinition("Velocity", FieldType.UNSIGNED_INTEGER, true, null, null), 073 new FieldDefinition("InventoryLocationGroupName", FieldType.ENTITY_NAME, true, null, null), 074 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 075 ); 076 } 077 078 @Inject 079 InventoryControl inventoryControl; 080 081 @Inject 082 WarehouseControl warehouseControl; 083 084 @Inject 085 LocationLogic locationLogic; 086 087 @Inject 088 LocationUseTypeLogic locationUseTypeLogic; 089 090 /** Creates a new instance of EditLocationCommand */ 091 public EditLocationCommand() { 092 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 093 } 094 095 @Override 096 public EditLocationResult getResult() { 097 return WarehouseResultFactory.getEditLocationResult(); 098 } 099 100 @Override 101 public LocationEdit getEdit() { 102 return WarehouseEditFactory.getLocationEdit(); 103 } 104 105 Warehouse warehouse; 106 107 @Override 108 public Location getEntity(EditLocationResult result) { 109 Location location = null; 110 var warehouseName = spec.getWarehouseName(); 111 112 warehouse = warehouseControl.getWarehouseByName(warehouseName); 113 114 if(warehouse != null) { 115 var warehouseParty = warehouse.getParty(); 116 var locationName = spec.getLocationName(); 117 118 location = warehouseControl.getLocationByName(warehouseParty, locationName, editModeToEntityPermission(editMode)); 119 120 if(location == null) { 121 addExecutionError(ExecutionErrors.UnknownLocationName.name(), warehouse.getWarehouseName(), locationName); 122 } 123 } else { 124 addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName); 125 } 126 127 return location; 128 } 129 130 @Override 131 public Location getLockEntity(Location location) { 132 return location; 133 } 134 135 @Override 136 public void fillInResult(EditLocationResult result, Location location) { 137 result.setLocation(warehouseControl.getLocationTransfer(getUserVisit(), location)); 138 } 139 140 @Override 141 public void doLock(LocationEdit edit, Location location) { 142 var locationDescription = warehouseControl.getLocationDescription(location, getPreferredLanguage()); 143 var locationDetail = location.getLastDetail(); 144 145 edit.setLocationName(locationDetail.getLocationName()); 146 edit.setLocationTypeName(locationDetail.getLocationType().getLastDetail().getLocationTypeName()); 147 edit.setLocationUseTypeName(locationDetail.getLocationUseType().getLocationUseTypeName()); 148 edit.setVelocity(locationDetail.getVelocity().toString()); 149 edit.setInventoryLocationGroupName(locationDetail.getInventoryLocationGroup().getLastDetail().getInventoryLocationGroupName()); 150 151 if(locationDescription != null) { 152 edit.setDescription(locationDescription.getDescription()); 153 } 154 } 155 156 LocationType locationType; 157 LocationUseType locationUseType; 158 InventoryLocationGroup inventoryLocationGroup; 159 160 @Override 161 public void canUpdate(Location location) { 162 var warehouseParty = warehouse.getParty(); 163 var locationName = edit.getLocationName(); 164 var duplicateLocation = warehouseControl.getLocationByName(warehouseParty, locationName); 165 166 if(duplicateLocation == null || location.equals(duplicateLocation)) { 167 var locationTypeName = edit.getLocationTypeName(); 168 169 locationType = warehouseControl.getLocationTypeByName(warehouseParty, locationTypeName); 170 171 if(locationType != null) { 172 locationLogic.validateLocationName(this, locationType, locationName); 173 174 if(!hasExecutionErrors()) { 175 var locationUseTypeName = edit.getLocationUseTypeName(); 176 177 locationUseType = locationUseTypeLogic.getLocationUseTypeByName(this, locationUseTypeName, null, false); 178 179 if(!hasExecutionErrors()) { 180 var multipleUseError = false; 181 182 if(!locationUseType.getAllowMultiple()) { 183 if(warehouseControl.countLocationsByLocationUseType(warehouseParty, locationUseType) != 0) { 184 multipleUseError = true; 185 } 186 } 187 188 if(!multipleUseError) { 189 var inventoryLocationGroupName = edit.getInventoryLocationGroupName(); 190 191 inventoryLocationGroup = inventoryControl.getInventoryLocationGroupByName(warehouseParty, inventoryLocationGroupName); 192 193 if(inventoryLocationGroup == null) { 194 addExecutionError(ExecutionErrors.UnknownInventoryLocationGroupName.name(), inventoryLocationGroupName); 195 } 196 } else { 197 addExecutionError(ExecutionErrors.MultipleLocationUseTypesNotAllowed.name()); 198 } 199 } 200 } 201 } else { 202 addExecutionError(ExecutionErrors.UnknownLocationTypeName.name(), warehouse.getWarehouseName(), locationTypeName); 203 } 204 } else { 205 addExecutionError(ExecutionErrors.DuplicateLocationName.name(), warehouse.getWarehouseName(), locationName); 206 } 207 } 208 209 @Override 210 public void doUpdate(Location location) { 211 var partyPK = getPartyPK(); 212 var locationDetailValue = warehouseControl.getLocationDetailValueForUpdate(location); 213 var locationDescription = warehouseControl.getLocationDescriptionForUpdate(location, getPreferredLanguage()); 214 var description = edit.getDescription(); 215 216 locationDetailValue.setLocationName(edit.getLocationName()); 217 locationDetailValue.setLocationTypePK(locationType.getPrimaryKey()); 218 locationDetailValue.setLocationUseTypePK(locationUseType.getPrimaryKey()); 219 locationDetailValue.setVelocity(Integer.valueOf(edit.getVelocity())); 220 locationDetailValue.setInventoryLocationGroupPK(inventoryLocationGroup.getPrimaryKey()); 221 222 warehouseControl.updateLocationFromValue(locationDetailValue, partyPK); 223 224 if(locationDescription == null && description != null) { 225 warehouseControl.createLocationDescription(location, getPreferredLanguage(), description, partyPK); 226 } else if(locationDescription != null && description == null) { 227 warehouseControl.deleteLocationDescription(locationDescription, partyPK); 228 } else if(locationDescription != null && description != null) { 229 var locationDescriptionValue = warehouseControl.getLocationDescriptionValue(locationDescription); 230 231 locationDescriptionValue.setDescription(description); 232 warehouseControl.updateLocationDescriptionFromValue(locationDescriptionValue, partyPK); 233 } 234 } 235 236}