001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.search;
016    
017    import com.liferay.portal.kernel.search.QueryPreProcessConfiguration;
018    
019    import java.util.LinkedHashMap;
020    import java.util.Map;
021    import java.util.Set;
022    import java.util.regex.Matcher;
023    import java.util.regex.Pattern;
024    
025    /**
026     * @author Miguel Angelo Caldas Gallindo
027     */
028    public class QueryPreProcessConfigurationImpl
029            implements QueryPreProcessConfiguration {
030    
031            @Override
032            public boolean isSubstringSearchAlways(String fieldName) {
033                    if (_fieldNamePatterns.containsKey(fieldName)) {
034                            return true;
035                    }
036    
037                    for (Pattern pattern : _fieldNamePatterns.values()) {
038                            Matcher matcher = pattern.matcher(fieldName);
039    
040                            if (matcher.matches()) {
041                                    return true;
042                            }
043                    }
044    
045                    return false;
046            }
047    
048            public void setFieldNames(Set<String> fieldNames) {
049                    for (String fieldName : fieldNames) {
050                            _fieldNamePatterns.put(fieldName, Pattern.compile(fieldName));
051                    }
052            }
053    
054            private final Map<String, Pattern> _fieldNamePatterns =
055                    new LinkedHashMap<>();
056    
057    }