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.LocationEdit; 020import com.echothree.control.user.warehouse.common.edit.WarehouseEditFactory; 021import com.echothree.control.user.warehouse.common.form.EditLocationForm; 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.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 EditLocationCommand 049 extends BaseEditCommand<LocationSpec, LocationEdit> { 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 = 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("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 /** Creates a new instance of EditLocationCommand */ 079 public EditLocationCommand() { 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.getEditLocationResult(); 087 var warehouseName = spec.getWarehouseName(); 088 var warehouse = warehouseControl.getWarehouseByName(warehouseName); 089 090 if(warehouse != null) { 091 var warehouseParty = warehouse.getParty(); 092 if(editMode.equals(EditMode.LOCK)) { 093 var locationName = spec.getLocationName(); 094 var location = warehouseControl.getLocationByName(warehouseParty, locationName); 095 096 if(location != null) { 097 result.setLocation(warehouseControl.getLocationTransfer(getUserVisit(), location)); 098 099 if(lockEntity(location)) { 100 var locationDescription = warehouseControl.getLocationDescription(location, getPreferredLanguage()); 101 var edit = WarehouseEditFactory.getLocationEdit(); 102 var locationDetail = location.getLastDetail(); 103 104 result.setEdit(edit); 105 edit.setLocationName(locationDetail.getLocationName()); 106 edit.setLocationTypeName(locationDetail.getLocationType().getLastDetail().getLocationTypeName()); 107 edit.setLocationUseTypeName(locationDetail.getLocationUseType().getLocationUseTypeName()); 108 edit.setVelocity(locationDetail.getVelocity().toString()); 109 edit.setInventoryLocationGroupName(locationDetail.getInventoryLocationGroup().getLastDetail().getInventoryLocationGroupName()); 110 111 if(locationDescription != null) { 112 edit.setDescription(locationDescription.getDescription()); 113 } 114 } else { 115 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 116 } 117 118 result.setEntityLock(getEntityLockTransfer(location)); 119 } else { 120 addExecutionError(ExecutionErrors.UnknownLocationName.name(), locationName); 121 } 122 } else if(editMode.equals(EditMode.UPDATE)) { 123 var locationName = spec.getLocationName(); 124 var location = warehouseControl.getLocationByNameForUpdate(warehouseParty, locationName); 125 126 if(location != null) { 127 locationName = edit.getLocationName(); 128 var duplicateLocation = warehouseControl.getLocationByName(warehouseParty, locationName); 129 130 if(duplicateLocation == null || location.equals(duplicateLocation)) { 131 var locationTypeName = edit.getLocationTypeName(); 132 var locationType = warehouseControl.getLocationTypeByName(warehouseParty, locationTypeName); 133 134 if(locationType != null) { 135 LocationLogic.getInstance().validateLocationName(this, locationType, locationName); 136 137 if(!hasExecutionErrors()) { 138 var locationUseTypeName = edit.getLocationUseTypeName(); 139 var locationUseType = LocationUseTypeLogic.getInstance().getLocationUseTypeByName(this, locationUseTypeName, null, false); 140 141 if(!hasExecutionErrors()) { 142 var multipleUseError = false; 143 144 if(!locationUseType.getAllowMultiple()) { 145 if(warehouseControl.countLocationsByLocationUseType(warehouseParty, locationUseType) != 0) 146 multipleUseError = true; 147 } 148 149 if(!multipleUseError) { 150 var inventoryControl = Session.getModelController(InventoryControl.class); 151 var inventoryLocationGroupName = edit.getInventoryLocationGroupName(); 152 var inventoryLocationGroup = inventoryControl.getInventoryLocationGroupByName(warehouseParty, inventoryLocationGroupName); 153 154 if(inventoryLocationGroup != null) { 155 if(lockEntityForUpdate(location)) { 156 try { 157 var partyPK = getPartyPK(); 158 var locationDetailValue = warehouseControl.getLocationDetailValueForUpdate(location); 159 var locationDescription = warehouseControl.getLocationDescriptionForUpdate(location, getPreferredLanguage()); 160 var description = edit.getDescription(); 161 162 locationDetailValue.setLocationName(edit.getLocationName()); 163 locationDetailValue.setLocationTypePK(locationType.getPrimaryKey()); 164 locationDetailValue.setLocationUseTypePK(locationUseType.getPrimaryKey()); 165 locationDetailValue.setVelocity(Integer.valueOf(edit.getVelocity())); 166 locationDetailValue.setInventoryLocationGroupPK(inventoryLocationGroup.getPrimaryKey()); 167 168 warehouseControl.updateLocationFromValue(locationDetailValue, partyPK); 169 170 if(locationDescription == null && description != null) { 171 warehouseControl.createLocationDescription(location, getPreferredLanguage(), description, partyPK); 172 } else if(locationDescription != null && description == null) { 173 warehouseControl.deleteLocationDescription(locationDescription, partyPK); 174 } else if(locationDescription != null && description != null) { 175 var locationDescriptionValue = warehouseControl.getLocationDescriptionValue(locationDescription); 176 177 locationDescriptionValue.setDescription(description); 178 warehouseControl.updateLocationDescriptionFromValue(locationDescriptionValue, partyPK); 179 } 180 } finally { 181 unlockEntity(location); 182 } 183 } else { 184 addExecutionError(ExecutionErrors.EntityLockStale.name()); 185 } 186 } else { 187 addExecutionError(ExecutionErrors.UnknownInventoryLocationGroupName.name(), inventoryLocationGroupName); 188 } 189 } else { 190 addExecutionError(ExecutionErrors.MultipleLocationUseTypesNotAllowed.name()); 191 } 192 } 193 } 194 } else { 195 addExecutionError(ExecutionErrors.UnknownLocationTypeName.name(), locationTypeName); 196 } 197 } else { 198 addExecutionError(ExecutionErrors.DuplicateLocationName.name(), locationName); 199 } 200 } else { 201 addExecutionError(ExecutionErrors.UnknownLocationName.name(), locationName); 202 } 203 } 204 } else { 205 addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName); 206 } 207 208 return result; 209 } 210 211}