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.app.web.base;
17  
18  import static org.codelibs.core.stream.StreamUtil.stream;
19  
20  import java.util.HashMap;
21  import java.util.Locale;
22  import java.util.Map;
23  
24  import javax.validation.constraints.Size;
25  
26  import org.codelibs.core.lang.StringUtil;
27  import org.codelibs.fess.entity.FacetInfo;
28  import org.codelibs.fess.entity.GeoInfo;
29  import org.codelibs.fess.entity.SearchRequestParams;
30  import org.codelibs.fess.mylasta.direction.FessConfig;
31  import org.codelibs.fess.util.ComponentUtil;
32  import org.lastaflute.web.util.LaRequestUtil;
33  import org.lastaflute.web.validation.theme.conversion.ValidateTypeFailure;
34  
35  public class SearchForm implements SearchRequestParams {
36  
37      public Map<String, String[]> fields = new HashMap<>();
38  
39      @Size(max = 1000)
40      public String q;
41  
42      @Size(max = 1000)
43      public String sort;
44  
45      @ValidateTypeFailure
46      public Integer num;
47  
48      public String[] lang;
49  
50      public String[] ex_q;
51  
52      @ValidateTypeFailure
53      public Integer start;
54  
55      @ValidateTypeFailure
56      public Integer pn;
57  
58      @Size(max = 1000)
59      public String sdh;
60  
61      // advance
62  
63      @Override
64      public int getStartPosition() {
65          final FessConfig fessConfig = ComponentUtil.getFessConfig();
66          if (start == null) {
67              start = fessConfig.getPagingSearchPageStartAsInteger();
68          }
69          return start;
70      }
71  
72      @Override
73      public int getPageSize() {
74          final FessConfig fessConfig = ComponentUtil.getFessConfig();
75          if (num == null) {
76              num = fessConfig.getPagingSearchPageSizeAsInteger();
77          } else {
78              try {
79                  if (num.intValue() > fessConfig.getPagingSearchPageMaxSizeAsInteger().intValue() || num.intValue() <= 0) {
80                      num = fessConfig.getPagingSearchPageMaxSizeAsInteger();
81                  }
82              } catch (final NumberFormatException e) {
83                  num = fessConfig.getPagingSearchPageSizeAsInteger();
84              }
85          }
86          return num;
87      }
88  
89      @Override
90      public String getQuery() {
91          return q;
92      }
93  
94      @Override
95      public String[] getExtraQueries() {
96          return stream(ex_q).get(stream -> stream.filter(StringUtil::isNotBlank).distinct().toArray(n -> new String[n]));
97      }
98  
99      @Override
100     public Map<String, String[]> getFields() {
101         return fields;
102     }
103 
104     @Override
105     public String[] getLanguages() {
106         return stream(lang).get(stream -> stream.filter(StringUtil::isNotBlank).distinct().toArray(n -> new String[n]));
107     }
108 
109     @Override
110     public GeoInfo getGeoInfo() {
111         final GeoInfo geoInfo = createGeoInfo(LaRequestUtil.getRequest());
112         if (geoInfo != null) {
113             return geoInfo;
114         }
115         return ComponentUtil.getQueryHelper().getDefaultGeoInfo();
116     }
117 
118     @Override
119     public FacetInfo getFacetInfo() {
120         return ComponentUtil.getQueryHelper().getDefaultFacetInfo();
121     }
122 
123     @Override
124     public String getSort() {
125         return sort;
126     }
127 
128     @Override
129     public Object getAttribute(final String name) {
130         return LaRequestUtil.getRequest().getAttribute(name);
131     }
132 
133     @Override
134     public Locale getLocale() {
135         return ComponentUtil.getRequestManager().getUserLocale();
136     }
137 
138     @Override
139     public SearchRequestType getType() {
140         return SearchRequestType.SEARCH;
141     }
142 
143     @Override
144     public String getSimilarDocHash() {
145         return sdh;
146     }
147 }