001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.search;
016    
017    import com.liferay.portal.kernel.util.StringPool;
018    import com.liferay.portal.kernel.util.Validator;
019    
020    import java.util.ArrayList;
021    import java.util.HashMap;
022    import java.util.List;
023    import java.util.Map;
024    import java.util.regex.Pattern;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public abstract class BaseBooleanQueryImpl implements BooleanQuery{
030    
031            public void addTerms(String[] fields, String values) throws ParseException {
032                    if (Validator.isNull(values)) {
033                            return;
034                    }
035    
036                    if (fields == null) {
037                            fields = new String[0];
038                    }
039    
040                    Map<String, List<String>> termFieldsValuesMap = getTermFieldsValuesMap(
041                            fields, values);
042    
043                    List<String> valuesList = termFieldsValuesMap.remove("no_field");
044    
045                    if (!valuesList.isEmpty()) {
046                            String value = valuesList.get(0);
047    
048                            for (String field : fields) {
049                                    addTerm(field, value);
050                            }
051                    }
052    
053                    addTerms(fields, termFieldsValuesMap);
054            }
055    
056            protected void addTerms(
057                            String[] fields, Map<String, List<String>> termFieldsValuesMap)
058                    throws ParseException {
059    
060                    for (String field : fields) {
061                            List<String> valuesList = termFieldsValuesMap.get(field);
062    
063                            for (String value : valuesList) {
064                                    addTerm(field, value);
065                            }
066                    }
067            }
068    
069            protected String getTermFieldRemainderValues(
070                            String field, String values, List<String> valuesList,
071                            String pattern, String replacement) {
072    
073                    if (Validator.isNull(values)) {
074                            return values;
075                    }
076    
077                    if (Validator.isNull(pattern) || Validator.isNull(replacement)) {
078                            return values;
079                    }
080    
081                    if (Validator.isNotNull(field)) {
082                            field += ":";
083                    }
084                    else {
085                            field = StringPool.BLANK;
086                    }
087    
088                    while (values.matches(pattern)) {
089                            String value = values.replaceAll(pattern, replacement);
090    
091                            valuesList.add(value);
092    
093                            String duplicate =
094                                    "(?i)\\s*" + Pattern.quote(field + value) + "\\s*";
095    
096                            values = values.replaceAll(duplicate, StringPool.SPACE);
097                            values = values.trim();
098                    }
099    
100                    return values;
101            }
102    
103            protected Map<String, List<String>> getTermFieldsValuesMap(
104                    String[] fields, String values) {
105    
106                    Map<String, List<String>> termFieldsValuesMap =
107                            new HashMap<String, List<String>>();
108    
109                    for (String field : fields) {
110                            List<String> valuesList = new ArrayList<String>();
111    
112                            values = getTermFieldRemainderValues(
113                                    field, values, valuesList,
114                                    "(?i)^.*" + field + ":([\"\'])(.+?)(\\1).*$", "$1$2$3");
115    
116                            values = getTermFieldRemainderValues(
117                                    field, values, valuesList,
118                                    "(?i)^.*" + field + ":([^\\s\"']*).*$", "$1");
119    
120                            termFieldsValuesMap.put(field, valuesList);
121                    }
122    
123                    values = values.trim();
124    
125                    List<String> valuesList = new ArrayList<String>();
126    
127                    if (Validator.isNotNull(values)) {
128                            values = getTermFieldRemainderValues(
129                                    null, values, valuesList,
130                                    "^[^\"\']*([\"\'])(.+?)(\\1)[^\"\']*$", "$1$2$3");
131    
132                            valuesList.add(values);
133                    }
134    
135                    termFieldsValuesMap.put("no_field", valuesList);
136    
137                    return termFieldsValuesMap;
138            }
139    
140    }