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