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.model.control.warehouse.server.logic; 018 019import com.echothree.control.user.inventory.common.spec.InventoryLocationSpec; 020import com.echothree.model.control.warehouse.common.exception.InvalidLocationNameException; 021import com.echothree.model.control.warehouse.server.control.WarehouseControl; 022import com.echothree.model.data.warehouse.server.entity.Location; 023import com.echothree.model.data.warehouse.server.entity.LocationType; 024import com.echothree.util.common.message.ExecutionErrors; 025import com.echothree.util.server.control.BaseLogic; 026import com.echothree.util.server.message.ExecutionErrorAccumulator; 027import java.util.regex.Pattern; 028import javax.enterprise.context.ApplicationScoped; 029import javax.inject.Inject; 030 031@ApplicationScoped 032public class LocationLogic 033 extends BaseLogic { 034 035 @Inject 036 WarehouseControl warehouseControl; 037 038 @Inject 039 WarehouseLogic warehouseLogic; 040 041 protected LocationLogic() { 042 super(); 043 } 044 045 public Location getLocation(final ExecutionErrorAccumulator eea, final String partyName, 046 final String warehouseName, final String locationName) { 047 Location location = null; 048 var warehouseParty = warehouseLogic.getWarehouseParty(eea, partyName, warehouseName); 049 050 if(warehouseParty != null) { 051 location = warehouseControl.getLocationByName(warehouseParty, locationName); 052 053 if(location == null) { 054 eea.addExecutionError(ExecutionErrors.UnknownLocationName.name(), locationName); 055 } 056 } 057 058 return location; 059 } 060 061 public Location getLocation(final ExecutionErrorAccumulator eea, final InventoryLocationSpec spec) { 062 return getLocation(eea, spec.getPartyName(), spec.getWarehouseName(), spec.getLocationName()); 063 } 064 065 public void validateLocationName(final ExecutionErrorAccumulator eea, final LocationType locationType, final String locationName) { 066 var locationNameElements = warehouseControl.getLocationNameElementsByLocationType(locationType); 067 var endIndex = 0; 068 var validLocationName = true; 069 070 for(var iter = locationNameElements.iterator(); iter.hasNext() && validLocationName;) { 071 var locationNameElement = iter.next(); 072 var locationNameElementDetail = locationNameElement.getLastDetail(); 073 var validationPattern = locationNameElementDetail.getValidationPattern(); 074 075 var beginIndex = locationNameElementDetail.getOffset(); 076 077 // LocationNameElements are sorted by their starting index, the last one will always 078 // be able to give the ending index (the required length) for the location name. 079 endIndex = beginIndex + locationNameElementDetail.getLength(); 080 081 // If there is a validation pattern for the LocationNameElement, test that substring 082 // to ensure that it matches. 083 try { 084 // Get the substring first, this will throw an exception if the string is too short 085 // and cause the validation to fail. 086 var substr = locationName.substring(beginIndex, endIndex); 087 088 if(validationPattern != null) { 089 var pattern = Pattern.compile(validationPattern); 090 var m = pattern.matcher(substr); 091 092 if(!m.matches()) { 093 validLocationName = false; 094 } 095 } 096 } catch (IndexOutOfBoundsException ioobe) { 097 validLocationName = false; 098 } 099 } 100 101 // Ensure the location name is of the appropriate length based on the final LocationNameElement. 102 if(locationName.length() > endIndex) { 103 validLocationName = false; 104 } 105 106 if(!validLocationName) { 107 handleExecutionError(InvalidLocationNameException.class, eea, ExecutionErrors.InvalidLocationName.name(), locationName); 108 } 109 } 110 111}