View Javadoc
1   /*
2    * Copyright 2012-2017 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.util;
17  
18  import static org.codelibs.core.stream.StreamUtil.stream;
19  
20  import java.util.Map;
21  
22  import org.codelibs.core.lang.StringUtil;
23  import org.codelibs.fess.helper.RelatedQueryHelper;
24  import org.codelibs.fess.mylasta.direction.FessConfig;
25  
26  public class QueryStringBuilder {
27      private String query;
28  
29      private String[] extraQueries;
30  
31      private Map<String, String[]> fieldMap;
32  
33      protected String quote(final String value) {
34          if (value.split("\\s").length > 1) {
35              return new StringBuilder().append('"').append(value.replace('"', ' ')).append('"').toString();
36          }
37          return value;
38      }
39  
40      public String build() {
41          final FessConfig fessConfig = ComponentUtil.getFessConfig();
42          final StringBuilder queryBuf = new StringBuilder(255);
43          if (StringUtil.isNotBlank(query)) {
44              final RelatedQueryHelper relatedQueryHelper = ComponentUtil.getRelatedQueryHelper();
45              final String[] relatedQueries = relatedQueryHelper.getRelatedQueries(query);
46              if (relatedQueries.length == 0) {
47                  queryBuf.append('(').append(query).append(')');
48              } else {
49                  queryBuf.append('(');
50                  queryBuf.append(quote(query));
51                  for (final String s : relatedQueries) {
52                      queryBuf.append(" OR ");
53                      queryBuf.append(quote(s));
54                  }
55                  queryBuf.append(')');
56              }
57          }
58          stream(extraQueries).of(
59                  stream -> stream.filter(q -> StringUtil.isNotBlank(q) && q.length() <= fessConfig.getQueryMaxLengthAsInteger().intValue())
60                          .forEach(q -> queryBuf.append(' ').append(q)));
61          stream(fieldMap).of(stream -> stream.forEach(entry -> {
62              final String key = entry.getKey();
63              final String[] values = entry.getValue();
64              if (values == null) {
65                  // nothing
66              } else if (values.length == 1) {
67                  queryBuf.append(' ').append(key).append(":\"").append(values[0]).append('\"');
68              } else if (values.length > 1) {
69                  boolean first = true;
70                  queryBuf.append(" (");
71                  for (final String value : values) {
72                      if (first) {
73                          first = false;
74                      } else {
75                          queryBuf.append(" OR ");
76                      }
77                      queryBuf.append(key).append(":\"").append(value).append('\"');
78                  }
79                  queryBuf.append(')');
80              }
81          }));
82          return queryBuf.toString();
83      }
84  
85      public static QueryStringBuilder query(final String query) {
86          final QueryStringBuilder builder = new QueryStringBuilder();
87          builder.query = query;
88          return builder;
89      }
90  
91      public QueryStringBuilder extraQueries(final String[] extraQueries) {
92          this.extraQueries = extraQueries;
93          return this;
94      }
95  
96      public QueryStringBuilder fields(final Map<String, String[]> fieldMap) {
97          this.fieldMap = fieldMap;
98          return this;
99      }
100 }