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.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.EditLocationResult; 023import com.echothree.control.user.warehouse.common.result.WarehouseResultFactory; 024import com.echothree.control.user.warehouse.common.spec.LocationSpec; 025import com.echothree.model.control.inventory.server.control.InventoryControl; 026import com.echothree.model.control.party.common.PartyTypes; 027import com.echothree.model.control.security.common.SecurityRoleGroups; 028import com.echothree.model.control.security.common.SecurityRoles; 029import com.echothree.model.control.warehouse.server.control.WarehouseControl; 030import com.echothree.model.control.warehouse.server.logic.LocationLogic; 031import com.echothree.model.control.warehouse.server.logic.LocationUseTypeLogic; 032import com.echothree.model.data.inventory.server.entity.InventoryLocationGroup; 033import com.echothree.model.data.party.server.entity.Party; 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.LocationDescription; 037import com.echothree.model.data.warehouse.server.entity.LocationDetail; 038import com.echothree.model.data.warehouse.server.entity.LocationType; 039import com.echothree.model.data.warehouse.server.entity.Warehouse; 040import com.echothree.model.data.warehouse.server.value.LocationDescriptionValue; 041import com.echothree.model.data.warehouse.server.value.LocationDetailValue; 042import com.echothree.util.common.command.BaseResult; 043import com.echothree.util.common.command.EditMode; 044import com.echothree.util.common.message.ExecutionErrors; 045import com.echothree.util.common.validation.FieldDefinition; 046import com.echothree.util.common.validation.FieldType; 047import com.echothree.util.server.control.BaseEditCommand; 048import com.echothree.util.server.control.CommandSecurityDefinition; 049import com.echothree.util.server.control.PartyTypeDefinition; 050import com.echothree.util.server.control.SecurityRoleDefinition; 051import com.echothree.util.server.persistence.Session; 052import java.util.Arrays; 053import java.util.Collections; 054import java.util.List; 055 056public class EditLocationCommand 057 extends BaseEditCommand<LocationSpec, LocationEdit> { 058 059 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 060 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 061 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 062 063 static { 064 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 065 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 066 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 067 new SecurityRoleDefinition(SecurityRoleGroups.Location.name(), SecurityRoles.Edit.name()) 068 )) 069 )); 070 071 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 072 new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, true, null, null), 073 new FieldDefinition("LocationName", FieldType.ENTITY_NAME, true, null, null) 074 )); 075 076 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 077 new FieldDefinition("LocationName", FieldType.ENTITY_NAME, true, null, null), 078 new FieldDefinition("LocationTypeName", FieldType.ENTITY_NAME, true, null, null), 079 new FieldDefinition("LocationUseTypeName", FieldType.ENTITY_NAME, true, null, null), 080 new FieldDefinition("Velocity", FieldType.UNSIGNED_INTEGER, true, null, null), 081 new FieldDefinition("InventoryLocationGroupName", FieldType.ENTITY_NAME, true, null, null), 082 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 083 )); 084 } 085 086 /** Creates a new instance of EditLocationCommand */ 087 public EditLocationCommand(UserVisitPK userVisitPK, EditLocationForm form) { 088 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 089 } 090 091 @Override 092 protected BaseResult execute() { 093 var warehouseControl = Session.getModelController(WarehouseControl.class); 094 EditLocationResult result = WarehouseResultFactory.getEditLocationResult(); 095 String warehouseName = spec.getWarehouseName(); 096 Warehouse warehouse = warehouseControl.getWarehouseByName(warehouseName); 097 098 if(warehouse != null) { 099 Party warehouseParty = warehouse.getParty(); 100 if(editMode.equals(EditMode.LOCK)) { 101 String locationName = spec.getLocationName(); 102 Location location = warehouseControl.getLocationByName(warehouseParty, locationName); 103 104 if(location != null) { 105 result.setLocation(warehouseControl.getLocationTransfer(getUserVisit(), location)); 106 107 if(lockEntity(location)) { 108 LocationDescription locationDescription = warehouseControl.getLocationDescription(location, getPreferredLanguage()); 109 LocationEdit edit = WarehouseEditFactory.getLocationEdit(); 110 LocationDetail locationDetail = location.getLastDetail(); 111 112 result.setEdit(edit); 113 edit.setLocationName(locationDetail.getLocationName()); 114 edit.setLocationTypeName(locationDetail.getLocationType().getLastDetail().getLocationTypeName()); 115 edit.setLocationUseTypeName(locationDetail.getLocationUseType().getLocationUseTypeName()); 116 edit.setVelocity(locationDetail.getVelocity().toString()); 117 edit.setInventoryLocationGroupName(locationDetail.getInventoryLocationGroup().getLastDetail().getInventoryLocationGroupName()); 118 119 if(locationDescription != null) { 120 edit.setDescription(locationDescription.getDescription()); 121 } 122 } else { 123 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 124 } 125 126 result.setEntityLock(getEntityLockTransfer(location)); 127 } else { 128 addExecutionError(ExecutionErrors.UnknownLocationName.name(), locationName); 129 } 130 } else if(editMode.equals(EditMode.UPDATE)) { 131 String locationName = spec.getLocationName(); 132 Location location = warehouseControl.getLocationByNameForUpdate(warehouseParty, locationName); 133 134 if(location != null) { 135 locationName = edit.getLocationName(); 136 Location duplicateLocation = warehouseControl.getLocationByName(warehouseParty, locationName); 137 138 if(duplicateLocation == null || location.equals(duplicateLocation)) { 139 String locationTypeName = edit.getLocationTypeName(); 140 LocationType locationType = warehouseControl.getLocationTypeByName(warehouseParty, locationTypeName); 141 142 if(locationType != null) { 143 LocationLogic.getInstance().validateLocationName(this, locationType, locationName); 144 145 if(!hasExecutionErrors()) { 146 var locationUseTypeName = edit.getLocationUseTypeName(); 147 var locationUseType = LocationUseTypeLogic.getInstance().getLocationUseTypeByName(this, locationUseTypeName, null, false); 148 149 if(!hasExecutionErrors()) { 150 boolean multipleUseError = false; 151 152 if(!locationUseType.getAllowMultiple()) { 153 if(warehouseControl.countLocationsByLocationUseType(warehouseParty, locationUseType) != 0) 154 multipleUseError = true; 155 } 156 157 if(!multipleUseError) { 158 var inventoryControl = Session.getModelController(InventoryControl.class); 159 String inventoryLocationGroupName = edit.getInventoryLocationGroupName(); 160 InventoryLocationGroup inventoryLocationGroup = inventoryControl.getInventoryLocationGroupByName(warehouseParty, inventoryLocationGroupName); 161 162 if(inventoryLocationGroup != null) { 163 if(lockEntityForUpdate(location)) { 164 try { 165 var partyPK = getPartyPK(); 166 LocationDetailValue locationDetailValue = warehouseControl.getLocationDetailValueForUpdate(location); 167 LocationDescription locationDescription = warehouseControl.getLocationDescriptionForUpdate(location, getPreferredLanguage()); 168 String description = edit.getDescription(); 169 170 locationDetailValue.setLocationName(edit.getLocationName()); 171 locationDetailValue.setLocationTypePK(locationType.getPrimaryKey()); 172 locationDetailValue.setLocationUseTypePK(locationUseType.getPrimaryKey()); 173 locationDetailValue.setVelocity(Integer.valueOf(edit.getVelocity())); 174 locationDetailValue.setInventoryLocationGroupPK(inventoryLocationGroup.getPrimaryKey()); 175 176 warehouseControl.updateLocationFromValue(locationDetailValue, partyPK); 177 178 if(locationDescription == null && description != null) { 179 warehouseControl.createLocationDescription(location, getPreferredLanguage(), description, partyPK); 180 } else if(locationDescription != null && description == null) { 181 warehouseControl.deleteLocationDescription(locationDescription, partyPK); 182 } else if(locationDescription != null && description != null) { 183 LocationDescriptionValue locationDescriptionValue = warehouseControl.getLocationDescriptionValue(locationDescription); 184 185 locationDescriptionValue.setDescription(description); 186 warehouseControl.updateLocationDescriptionFromValue(locationDescriptionValue, partyPK); 187 } 188 } finally { 189 unlockEntity(location); 190 } 191 } else { 192 addExecutionError(ExecutionErrors.EntityLockStale.name()); 193 } 194 } else { 195 addExecutionError(ExecutionErrors.UnknownInventoryLocationGroupName.name(), inventoryLocationGroupName); 196 } 197 } else { 198 addExecutionError(ExecutionErrors.MultipleLocationUseTypesNotAllowed.name()); 199 } 200 } 201 } 202 } else { 203 addExecutionError(ExecutionErrors.UnknownLocationTypeName.name(), locationTypeName); 204 } 205 } else { 206 addExecutionError(ExecutionErrors.DuplicateLocationName.name(), locationName); 207 } 208 } else { 209 addExecutionError(ExecutionErrors.UnknownLocationName.name(), locationName); 210 } 211 } 212 } else { 213 addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName); 214 } 215 216 return result; 217 } 218 219}