View Javadoc
1   /*
2    * Copyright 2012-2025 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.admin.searchlist;
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 org.codelibs.core.lang.StringUtil;
25  import org.codelibs.fess.entity.FacetInfo;
26  import org.codelibs.fess.entity.GeoInfo;
27  import org.codelibs.fess.entity.HighlightInfo;
28  import org.codelibs.fess.entity.SearchRequestParams;
29  import org.codelibs.fess.mylasta.direction.FessConfig;
30  import org.codelibs.fess.util.ComponentUtil;
31  import org.lastaflute.web.util.LaRequestUtil;
32  import org.lastaflute.web.validation.theme.conversion.ValidateTypeFailure;
33  
34  import jakarta.validation.constraints.Size;
35  
36  /**
37   * The list form for Search List.
38   */
39  public class ListForm extends SearchRequestParams {
40  
41      /**
42       * Default constructor.
43       */
44      public ListForm() {
45          super();
46      }
47  
48      /** The search query string. */
49      @Size(max = 1000)
50      public String q;
51  
52      /** The sort field and direction. */
53      public String sort;
54  
55      /** The start position for search results. */
56      @ValidateTypeFailure
57      public Integer start;
58  
59      /** The offset for pagination. */
60      @ValidateTypeFailure
61      public Integer offset;
62  
63      /** The page number. */
64      @ValidateTypeFailure
65      public Integer pn;
66  
67      /** The number of results to display. */
68      @ValidateTypeFailure
69      public Integer num;
70  
71      /** The languages. */
72      public String[] lang;
73  
74      /** The fields. */
75      public Map<String, String[]> fields = new HashMap<>();
76  
77      /** The conditions. */
78      public Map<String, String[]> as = new HashMap<>();
79  
80      /** The extra queries. */
81      public String[] ex_q;
82  
83      /** The similar document hash. */
84      public String sdh;
85  
86      @Override
87      public String getQuery() {
88          return q;
89      }
90  
91      @Override
92      public String[] getExtraQueries() {
93          return stream(ex_q).get(stream -> stream.filter(StringUtil::isNotBlank).distinct().toArray(n -> new String[n]));
94      }
95  
96      @Override
97      public Map<String, String[]> getFields() {
98          return fields;
99      }
100 
101     @Override
102     public Map<String, String[]> getConditions() {
103         return as;
104     }
105 
106     @Override
107     public int getStartPosition() {
108         if (start == null) {
109             start = ComponentUtil.getFessConfig().getPagingSearchPageStartAsInteger();
110         }
111         return start;
112     }
113 
114     @Override
115     public int getOffset() {
116         if (offset == null) {
117             offset = 0;
118         }
119         return offset;
120     }
121 
122     @Override
123     public int getPageSize() {
124         final FessConfig fessConfig = ComponentUtil.getFessConfig();
125         if (num == null) {
126             num = fessConfig.getPagingSearchPageSizeAsInteger();
127         }
128         if (num > fessConfig.getPagingSearchPageMaxSizeAsInteger().intValue() || num <= 0) {
129             num = fessConfig.getPagingSearchPageMaxSizeAsInteger();
130         }
131         return num;
132     }
133 
134     @Override
135     public String[] getLanguages() {
136         return stream(lang).get(stream -> stream.filter(StringUtil::isNotBlank).distinct().toArray(n -> new String[n]));
137     }
138 
139     @Override
140     public GeoInfo getGeoInfo() {
141         return null;
142     }
143 
144     @Override
145     public FacetInfo getFacetInfo() {
146         return null;
147     }
148 
149     @Override
150     public HighlightInfo getHighlightInfo() {
151         return new HighlightInfo();
152     }
153 
154     @Override
155     public String getSort() {
156         return sort;
157     }
158 
159     /**
160      * Initializes the form with default values from configuration.
161      */
162     public void initialize() {
163         final FessConfig fessConfig = ComponentUtil.getFessConfig();
164         if (start == null) {
165             start = fessConfig.getPagingSearchPageStartAsInteger();
166         }
167         if (num == null) {
168             num = fessConfig.getPagingSearchPageSizeAsInteger();
169         } else if (num > fessConfig.getPagingSearchPageMaxSizeAsInteger().intValue()) {
170             num = fessConfig.getPagingSearchPageMaxSizeAsInteger();
171         }
172     }
173 
174     @Override
175     public Object getAttribute(final String name) {
176         return LaRequestUtil.getOptionalRequest().map(req -> req.getAttribute(name)).orElse(null);
177     }
178 
179     @Override
180     public Locale getLocale() {
181         return ComponentUtil.getRequestManager().getUserLocale();
182     }
183 
184     @Override
185     public SearchRequestType getType() {
186         return SearchRequestType.ADMIN_SEARCH;
187     }
188 
189     @Override
190     public String getSimilarDocHash() {
191         return sdh;
192     }
193 
194     @Override
195     public String getTrackTotalHits() {
196         final String value = ComponentUtil.getFessConfig().getPageSearchlistTrackTotalHits();
197         if (StringUtil.isNotBlank(value)) {
198             return value;
199         }
200         return null;
201     }
202 }