View Javadoc
1   /*
2    * Copyright 2012-2020 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.HighlightInfo;
30  import org.codelibs.fess.entity.SearchRequestParams;
31  import org.codelibs.fess.mylasta.direction.FessConfig;
32  import org.codelibs.fess.util.ComponentUtil;
33  import org.lastaflute.web.util.LaRequestUtil;
34  import org.lastaflute.web.validation.theme.conversion.ValidateTypeFailure;
35  
36  public class SearchForm extends SearchRequestParams {
37  
38      public Map<String, String[]> fields = new HashMap<>();
39  
40      public Map<String, String[]> as = new HashMap<>();
41  
42      @Size(max = 1000)
43      public String q;
44  
45      @Size(max = 1000)
46      public String sort;
47  
48      @ValidateTypeFailure
49      public Integer num;
50  
51      public String[] lang;
52  
53      public String[] ex_q;
54  
55      @ValidateTypeFailure
56      public Integer start;
57  
58      @ValidateTypeFailure
59      public Integer pn;
60  
61      @Size(max = 1000)
62      public String sdh;
63  
64      @Size(max = 100)
65      public String track_total_hits;
66  
67      // advance
68  
69      @Override
70      public int getStartPosition() {
71          final FessConfig fessConfig = ComponentUtil.getFessConfig();
72          if (start == null) {
73              start = fessConfig.getPagingSearchPageStartAsInteger();
74          }
75          return start;
76      }
77  
78      @Override
79      public int getPageSize() {
80          final FessConfig fessConfig = ComponentUtil.getFessConfig();
81          if (num == null) {
82              num = fessConfig.getPagingSearchPageSizeAsInteger();
83          } else {
84              try {
85                  if (num.intValue() > fessConfig.getPagingSearchPageMaxSizeAsInteger().intValue() || num.intValue() <= 0) {
86                      num = fessConfig.getPagingSearchPageMaxSizeAsInteger();
87                  }
88              } catch (final NumberFormatException e) {
89                  num = fessConfig.getPagingSearchPageSizeAsInteger();
90              }
91          }
92          return num;
93      }
94  
95      @Override
96      public String getQuery() {
97          return q;
98      }
99  
100     @Override
101     public String[] getExtraQueries() {
102         return stream(ex_q).get(stream -> stream.filter(StringUtil::isNotBlank).distinct().toArray(n -> new String[n]));
103     }
104 
105     @Override
106     public Map<String, String[]> getFields() {
107         return fields;
108     }
109 
110     @Override
111     public String[] getLanguages() {
112         return stream(lang).get(stream -> stream.filter(StringUtil::isNotBlank).distinct().toArray(n -> new String[n]));
113     }
114 
115     @Override
116     public GeoInfo getGeoInfo() {
117         final GeoInfo geoInfo = createGeoInfo(LaRequestUtil.getRequest());
118         if (geoInfo != null) {
119             return geoInfo;
120         }
121         return ComponentUtil.getQueryHelper().getDefaultGeoInfo();
122     }
123 
124     @Override
125     public FacetInfo getFacetInfo() {
126         return ComponentUtil.getQueryHelper().getDefaultFacetInfo();
127     }
128 
129     @Override
130     public HighlightInfo getHighlightInfo() {
131         return ComponentUtil.getViewHelper().createHighlightInfo();
132     }
133 
134     @Override
135     public String getSort() {
136         return sort;
137     }
138 
139     @Override
140     public Object getAttribute(final String name) {
141         return LaRequestUtil.getRequest().getAttribute(name);
142     }
143 
144     @Override
145     public Locale getLocale() {
146         return ComponentUtil.getRequestManager().getUserLocale();
147     }
148 
149     @Override
150     public SearchRequestType getType() {
151         return SearchRequestType.SEARCH;
152     }
153 
154     @Override
155     public String getSimilarDocHash() {
156         return sdh;
157     }
158 
159     @Override
160     public Map<String, String[]> getConditions() {
161         return as;
162     }
163 
164     @Override
165     public String getTrackTotalHits() {
166         return track_total_hits;
167     }
168 
169 }