1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.dao.search;
16  
17  import com.liferay.portal.kernel.util.OrderByComparator;
18  import com.liferay.portal.kernel.util.ParamUtil;
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  import java.util.Map;
23  
24  import javax.portlet.PortletRequest;
25  import javax.portlet.PortletURL;
26  
27  /**
28   * <a href="SearchContainer.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Brian Wing Shun Chan
31   */
32  public class SearchContainer<R> {
33  
34      public static final int DEFAULT_CUR = 1;
35  
36      public static final String DEFAULT_CUR_PARAM = "cur";
37  
38      /**
39       * @deprecated Use <code>DEFAULT_CUR</code>.
40       */
41      public static final int DEFAULT_CUR_VALUE = DEFAULT_CUR;
42  
43      public static final int DEFAULT_DELTA = 20;
44  
45      public static final boolean DEFAULT_DELTA_CONFIGURABLE = true;
46  
47      public static final String DEFAULT_DELTA_PARAM = "delta";
48  
49      /**
50       * @deprecated LPS-6312
51       */
52      public static final int DEFAULT_MAX_PAGES = 25;
53  
54      public static final String DEFAULT_ORDER_BY_COL_PARAM = "orderByCol";
55  
56      public static final String DEFAULT_ORDER_BY_TYPE_PARAM = "orderByType";
57  
58      public static final int MAX_DELTA = 200;
59  
60      public SearchContainer() {
61      }
62  
63      public SearchContainer(
64          PortletRequest portletRequest, PortletURL iteratorURL,
65          List<String> headerNames, String emptyResultsMessage) {
66  
67          this(
68              portletRequest, null, null, DEFAULT_CUR_PARAM, DEFAULT_DELTA,
69              iteratorURL, headerNames, emptyResultsMessage);
70      }
71  
72      public SearchContainer(
73          PortletRequest portletRequest, DisplayTerms displayTerms,
74          DisplayTerms searchTerms, String curParam, int delta,
75          PortletURL iteratorURL, List<String> headerNames,
76          String emptyResultsMessage) {
77  
78          this (
79              portletRequest, displayTerms, searchTerms, curParam, 0, delta,
80              iteratorURL, headerNames, emptyResultsMessage);
81      }
82  
83      public SearchContainer(
84          PortletRequest portletRequest, DisplayTerms displayTerms,
85          DisplayTerms searchTerms, String curParam, int cur, int delta,
86          PortletURL iteratorURL, List<String> headerNames,
87          String emptyResultsMessage) {
88  
89          _portletRequest = portletRequest;
90          _displayTerms = displayTerms;
91          _searchTerms = searchTerms;
92  
93          _curParam = curParam;
94  
95          if (cur < 1) {
96              _cur = ParamUtil.getInteger(portletRequest, _curParam, DEFAULT_CUR);
97  
98              if (_cur < 1) {
99                  _cur = DEFAULT_CUR;
100             }
101         }
102         else {
103             _cur = cur;
104         }
105 
106         setDelta(ParamUtil.getInteger(portletRequest, _deltaParam, delta));
107 
108         _iteratorURL = iteratorURL;
109 
110         _iteratorURL.setParameter(_curParam, String.valueOf(_cur));
111         _iteratorURL.setParameter(_deltaParam, String.valueOf(_delta));
112         _iteratorURL.setParameter(
113             DisplayTerms.KEYWORDS,
114             ParamUtil.getString(portletRequest, DisplayTerms.KEYWORDS));
115         _iteratorURL.setParameter(
116             DisplayTerms.ADVANCED_SEARCH,
117             String.valueOf(
118                 ParamUtil.getBoolean(
119                     portletRequest, DisplayTerms.ADVANCED_SEARCH)));
120         _iteratorURL.setParameter(
121             DisplayTerms.AND_OPERATOR,
122             String.valueOf(
123                 ParamUtil.getBoolean(
124                     portletRequest, DisplayTerms.AND_OPERATOR, true)));
125 
126         if (headerNames != null) {
127             _headerNames = new ArrayList<String>(headerNames.size());
128 
129             _headerNames.addAll(headerNames);
130         }
131 
132         _emptyResultsMessage = emptyResultsMessage;
133     }
134 
135     public int getCur() {
136         return _cur;
137     }
138 
139     public String getCurParam() {
140         return _curParam;
141     }
142 
143     /**
144      * @deprecated Use <code>getCur</code>.
145      */
146     public int getCurValue() {
147         return getCur();
148     }
149 
150     public int getDelta() {
151         return _delta;
152     }
153 
154     public String getDeltaParam() {
155         return _deltaParam;
156     }
157 
158     public DisplayTerms getDisplayTerms() {
159         return _displayTerms;
160     }
161 
162     public String getEmptyResultsMessage() {
163         return _emptyResultsMessage;
164     }
165 
166     public int getEnd() {
167         return _end;
168     }
169 
170     public List<String> getHeaderNames() {
171         return _headerNames;
172     }
173 
174     public String getId() {
175         return _id;
176     }
177 
178     public PortletURL getIteratorURL() {
179         return _iteratorURL;
180     }
181 
182     /**
183      * @deprecated LPS-6312
184      */
185     public int getMaxPages() {
186         return _maxPages;
187     }
188 
189     public Map<String, String> getOrderableHeaders() {
190         return _orderableHeaders;
191     }
192 
193     public String getOrderByCol() {
194         return _orderByCol;
195     }
196 
197     public String getOrderByColParam() {
198         return _orderByColParam;
199     }
200 
201     public OrderByComparator getOrderByComparator() {
202         return _orderByComparator;
203     }
204 
205     public String getOrderByType() {
206         return _orderByType;
207     }
208 
209     public String getOrderByTypeParam() {
210         return _orderByTypeParam;
211     }
212 
213     public PortletRequest getPortletRequest() {
214         return _portletRequest;
215     }
216 
217     public int getResultEnd() {
218         return _resultEnd;
219     }
220 
221     public List<ResultRow> getResultRows() {
222         return _resultRows;
223     }
224 
225     public List<R> getResults() {
226         return _results;
227     }
228 
229     public RowChecker getRowChecker() {
230         return _rowChecker;
231     }
232 
233     public DisplayTerms getSearchTerms() {
234         return _searchTerms;
235     }
236 
237     public int getStart() {
238         return _start;
239     }
240 
241     public int getTotal() {
242         return _total;
243     }
244 
245     public boolean isDeltaConfigurable() {
246         return _deltaConfigurable;
247     }
248 
249     public boolean isHover() {
250         return _hover;
251     }
252 
253     public void setDelta(int delta) {
254         if (delta <= 0) {
255             _delta = DEFAULT_DELTA;
256         }
257         else if (delta > MAX_DELTA) {
258             _delta = MAX_DELTA;
259         }
260         else {
261             _delta = delta;
262         }
263 
264         _calculateStartAndEnd();
265     }
266 
267     public void setDeltaConfigurable(boolean deltaConfigurable) {
268         _deltaConfigurable = deltaConfigurable;
269     }
270 
271     public void setDeltaParam(String deltaParam) {
272         _deltaParam = deltaParam;
273     }
274 
275     public void setEmptyResultsMessage(String emptyResultsMessage) {
276         _emptyResultsMessage = emptyResultsMessage;
277     }
278 
279     public void setHeaderNames(List<String> headerNames) {
280         _headerNames = headerNames;
281     }
282 
283     public void setHover(boolean hover) {
284         _hover = hover;
285     }
286 
287     public void setId(String id) {
288         _id = id;
289     }
290 
291     public void setIteratorURL(PortletURL iteratorURL) {
292         _iteratorURL = iteratorURL;
293     }
294 
295     /**
296      * @deprecated LPS-6312
297      */
298     public void setMaxPages(int maxPages) {
299         _maxPages = maxPages;
300     }
301 
302     public void setOrderableHeaders(Map<String, String> orderableHeaders) {
303         _orderableHeaders = orderableHeaders;
304     }
305 
306     public void setOrderByCol(String orderByCol) {
307         _orderByCol = orderByCol;
308 
309         _iteratorURL.setParameter(_orderByColParam, _orderByCol);
310     }
311 
312     public void setOrderByColParam(String orderByColParam) {
313         _orderByColParam = orderByColParam;
314     }
315 
316     public void setOrderByComparator(OrderByComparator orderByComparator) {
317         _orderByComparator = orderByComparator;
318     }
319 
320     public void setOrderByType(String orderByType) {
321         _orderByType = orderByType;
322 
323         _iteratorURL.setParameter(_orderByTypeParam, _orderByType);
324     }
325 
326     public void setOrderByTypeParam(String orderByTypeParam) {
327         _orderByTypeParam = orderByTypeParam;
328     }
329 
330     public void setResults(List<R> results) {
331         _results = results;
332     }
333 
334     public void setRowChecker(RowChecker rowChecker) {
335         _rowChecker = rowChecker;
336     }
337 
338     public void setTotal(int total) {
339         _total = total;
340 
341         if (((_cur - 1) * _delta) > _total) {
342             _cur = DEFAULT_CUR;
343         }
344 
345         _calculateStartAndEnd();
346     }
347 
348     private void _calculateStartAndEnd() {
349         _start = (_cur - 1) * _delta;
350         _end = _start + _delta;
351 
352         _resultEnd = _end;
353 
354         if (_resultEnd > _total) {
355             _resultEnd = _total;
356         }
357     }
358 
359     private int _cur;
360     private String _curParam = DEFAULT_CUR_PARAM;
361     private int _delta = DEFAULT_DELTA;
362     private boolean _deltaConfigurable = DEFAULT_DELTA_CONFIGURABLE;
363     private String _deltaParam = DEFAULT_DELTA_PARAM;
364     private DisplayTerms _displayTerms;
365     private String _emptyResultsMessage;
366     private int _end;
367     private List<String> _headerNames;
368     private boolean _hover = true;
369     private String _id;
370     private PortletURL _iteratorURL;
371 
372     /**
373      * @deprecated LPS-6312
374      */
375     private int _maxPages = DEFAULT_MAX_PAGES;
376 
377     private Map<String, String> _orderableHeaders;
378     private String _orderByCol;
379     private String _orderByColParam = DEFAULT_ORDER_BY_COL_PARAM;
380     private OrderByComparator _orderByComparator;
381     private String _orderByType;
382     private String _orderByTypeParam = DEFAULT_ORDER_BY_TYPE_PARAM;
383     private PortletRequest _portletRequest;
384     private int _resultEnd;
385     private List<ResultRow> _resultRows = new ArrayList<ResultRow>();
386     private List<R> _results = new ArrayList<R>();
387     private RowChecker _rowChecker;
388     private DisplayTerms _searchTerms;
389     private int _start;
390     private int _total;
391 
392 }