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