1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.search;
16  
17  import com.liferay.portal.kernel.util.StringPool;
18  import com.liferay.portal.kernel.util.Validator;
19  
20  import java.util.ArrayList;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.regex.Pattern;
25  
26  /**
27   * <a href="BaseBooleanQueryImpl.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Brian Wing Shun Chan
30   */
31  public abstract class BaseBooleanQueryImpl implements BooleanQuery{
32  
33      public void addTerms(String[] fields, String values) throws ParseException {
34          if (Validator.isNull(values)) {
35              return;
36          }
37  
38          if (fields == null) {
39              fields = new String[0];
40          }
41  
42          Map<String, List<String>> termFieldsValuesMap = getTermFieldsValuesMap(
43              fields, values);
44  
45          List<String> valuesList = termFieldsValuesMap.remove("no_field");
46  
47          if (!valuesList.isEmpty()) {
48              String value = valuesList.get(0);
49  
50              for (String field : fields) {
51                  addTerm(field, value);
52              }
53          }
54  
55          addTerms(fields, termFieldsValuesMap);
56      }
57  
58      protected void addTerms(
59              String[] fields, Map<String, List<String>> termFieldsValuesMap)
60          throws ParseException {
61  
62          for (String field : fields) {
63              List<String> valuesList = termFieldsValuesMap.get(field);
64  
65              for (String value : valuesList) {
66                  addTerm(field, value);
67              }
68          }
69      }
70  
71      protected String getTermFieldRemainderValues(
72              String field, String values, List<String> valuesList,
73              String pattern, String replacement) {
74  
75          if (Validator.isNull(values)) {
76              return values;
77          }
78  
79          if (Validator.isNull(pattern) || Validator.isNull(replacement)) {
80              return values;
81          }
82  
83          if (Validator.isNotNull(field)) {
84              field += ":";
85          }
86          else {
87              field = StringPool.BLANK;
88          }
89  
90          while (values.matches(pattern)) {
91              String value = values.replaceAll(pattern, replacement);
92  
93              valuesList.add(value);
94  
95              String duplicate =
96                  "(?i)\\s*" + Pattern.quote(field + value) + "\\s*";
97  
98              values = values.replaceAll(duplicate, StringPool.SPACE);
99              values = values.trim();
100         }
101 
102         return values;
103     }
104 
105     protected Map<String, List<String>> getTermFieldsValuesMap(
106         String[] fields, String values) {
107 
108         Map<String, List<String>> termFieldsValuesMap =
109             new HashMap<String, List<String>>();
110 
111         for (String field : fields) {
112             List<String> valuesList = new ArrayList<String>();
113 
114             values = getTermFieldRemainderValues(
115                 field, values, valuesList,
116                 "(?i)^.*" + field + ":([\"\'])(.+?)(\\1).*$", "$1$2$3");
117 
118             values = getTermFieldRemainderValues(
119                 field, values, valuesList,
120                 "(?i)^.*" + field + ":([^\\s\"']*).*$", "$1");
121 
122             termFieldsValuesMap.put(field, valuesList);
123         }
124 
125         values = values.trim();
126 
127         List<String> valuesList = new ArrayList<String>();
128 
129         if (Validator.isNotNull(values)) {
130             values = getTermFieldRemainderValues(
131                 null, values, valuesList,
132                 "^[^\"\']*([\"\'])(.+?)(\\1)[^\"\']*$", "$1$2$3");
133 
134             valuesList.add(values);
135         }
136 
137         termFieldsValuesMap.put("no_field", valuesList);
138 
139         return termFieldsValuesMap;
140     }
141 
142 }