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