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.mylasta.direction.FessConfig;
24  
25  public class QueryStringBuilder {
26      private String query;
27  
28      private String[] extraQueries;
29  
30      private Map<String, String[]> fieldMap;
31  
32      public String build() {
33          final FessConfig fessConfig = ComponentUtil.getFessConfig();
34          final StringBuilder queryBuf = new StringBuilder(255);
35          if (StringUtil.isNotBlank(query)) {
36              queryBuf.append('(').append(query).append(')');
37          }
38          stream(extraQueries).of(
39                  stream -> stream.filter(q -> StringUtil.isNotBlank(q) && q.length() <= fessConfig.getQueryMaxLengthAsInteger().intValue())
40                          .forEach(q -> queryBuf.append(' ').append(q)));
41          stream(fieldMap).of(stream -> stream.forEach(entry -> {
42              final String key = entry.getKey();
43              final String[] values = entry.getValue();
44              if (values == null) {
45                  // nothing
46              } else if (values.length == 1) {
47                  queryBuf.append(' ').append(key).append(":\"").append(values[0]).append('\"');
48              } else if (values.length > 1) {
49                  boolean first = true;
50                  queryBuf.append(" (");
51                  for (final String value : values) {
52                      if (first) {
53                          first = false;
54                      } else {
55                          queryBuf.append(" OR ");
56                      }
57                      queryBuf.append(key).append(":\"").append(value).append('\"');
58                  }
59                  queryBuf.append(')');
60              }
61          }));
62          return queryBuf.toString();
63      }
64  
65      public static QueryStringBuilder query(final String query) {
66          final QueryStringBuilder builder = new QueryStringBuilder();
67          builder.query = query;
68          return builder;
69      }
70  
71      public QueryStringBuilder extraQueries(final String[] extraQueries) {
72          this.extraQueries = extraQueries;
73          return this;
74      }
75  
76      public QueryStringBuilder fields(final Map<String, String[]> fieldMap) {
77          this.fieldMap = fieldMap;
78          return this;
79      }
80  }