1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.dao.search;
24  
25  import com.liferay.portal.kernel.util.OrderByComparator;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  
28  import java.util.ArrayList;
29  import java.util.List;
30  import java.util.Map;
31  
32  import javax.portlet.PortletRequest;
33  import javax.portlet.PortletURL;
34  
35  /**
36   * <a href="SearchContainer.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   *
40   */
41  public class SearchContainer<R> {
42  
43      public static final int DEFAULT_CUR = 1;
44  
45      public static final String DEFAULT_CUR_PARAM = "cur";
46  
47      /**
48       * @deprecated Use <code>DEFAULT_CUR</code>.
49       */
50      public static final int DEFAULT_CUR_VALUE = DEFAULT_CUR;
51  
52      public static final int DEFAULT_DELTA = 20;
53  
54      public static final String DEFAULT_DELTA_PARAM = "delta";
55  
56      public static final int DEFAULT_MAX_PAGES = 25;
57  
58      public static final String DEFAULT_ORDER_BY_COL_PARAM = "orderByCol";
59  
60      public static final String DEFAULT_ORDER_BY_TYPE_PARAM = "orderByType";
61  
62      public static final int MAX_DELTA = 200;
63  
64      public SearchContainer() {
65      }
66  
67      public SearchContainer(
68          PortletRequest portletRequest, DisplayTerms displayTerms,
69          DisplayTerms searchTerms, String curParam, int delta,
70          PortletURL iteratorURL, List<String> headerNames,
71          String emptyResultsMessage) {
72  
73          _displayTerms = displayTerms;
74          _searchTerms = searchTerms;
75  
76          _curParam = curParam;
77  
78          _cur = ParamUtil.getInteger(portletRequest, _curParam, DEFAULT_CUR);
79  
80          if (_cur < 1) {
81              _cur = DEFAULT_CUR;
82          }
83  
84          setDelta(ParamUtil.getInteger(portletRequest, _deltaParam, delta));
85  
86          _iteratorURL = iteratorURL;
87  
88          _iteratorURL.setParameter(_curParam, String.valueOf(_cur));
89          _iteratorURL.setParameter(_deltaParam, String.valueOf(_delta));
90          _iteratorURL.setParameter(
91              DisplayTerms.KEYWORDS,
92              ParamUtil.getString(portletRequest, DisplayTerms.KEYWORDS));
93          _iteratorURL.setParameter(
94              DisplayTerms.ADVANCED_SEARCH,
95              String.valueOf(
96                  ParamUtil.getBoolean(
97                      portletRequest, DisplayTerms.ADVANCED_SEARCH)));
98          _iteratorURL.setParameter(
99              DisplayTerms.AND_OPERATOR,
100             String.valueOf(
101                 ParamUtil.getBoolean(
102                     portletRequest, DisplayTerms.AND_OPERATOR, true)));
103 
104         if (headerNames != null) {
105             _headerNames = new ArrayList<String>(headerNames.size());
106 
107             _headerNames.addAll(headerNames);
108         }
109 
110         _emptyResultsMessage = emptyResultsMessage;
111     }
112 
113     public String getId() {
114         return _id;
115     }
116 
117     public void setId(String id) {
118         _id = id;
119     }
120 
121     public DisplayTerms getDisplayTerms() {
122         return _displayTerms;
123     }
124 
125     public DisplayTerms getSearchTerms() {
126         return _searchTerms;
127     }
128 
129     public int getCur() {
130         return _cur;
131     }
132 
133     public String getCurParam() {
134         return _curParam;
135     }
136 
137     /**
138      * @deprecated Use <code>getCur</code>.
139      */
140     public int getCurValue() {
141         return getCur();
142     }
143 
144     public int getDelta() {
145         return _delta;
146     }
147 
148     public void setDelta(int delta) {
149         if (delta <= 0) {
150             _delta = DEFAULT_DELTA;
151         }
152         else if (delta > MAX_DELTA) {
153             _delta = MAX_DELTA;
154         }
155         else {
156             _delta = delta;
157         }
158 
159         _calculateStartAndEnd();
160     }
161 
162     public String getDeltaParam() {
163         return _deltaParam;
164     }
165 
166     public void setDeltaParam(String deltaParam) {
167         _deltaParam = deltaParam;
168     }
169 
170     public int getMaxPages() {
171         return _maxPages;
172     }
173 
174     public void setMaxPages(int maxPages) {
175         _maxPages = maxPages;
176     }
177 
178     public int getStart() {
179         return _start;
180     }
181 
182     public int getEnd() {
183         return _end;
184     }
185 
186     public int getResultEnd() {
187         return _resultEnd;
188     }
189 
190     public int getTotal() {
191         return _total;
192     }
193 
194     public void setTotal(int total) {
195         _total = total;
196 
197         if (((_cur - 1) * _delta) > _total) {
198             _cur = DEFAULT_CUR;
199         }
200 
201         _calculateStartAndEnd();
202     }
203 
204     public List<R> getResults() {
205         return _results;
206     }
207 
208     public void setResults(List<R> results) {
209         _results = results;
210     }
211 
212     public List<ResultRow> getResultRows() {
213         return _resultRows;
214     }
215 
216     public PortletURL getIteratorURL() {
217         return _iteratorURL;
218     }
219 
220     public void setIteratorURL(PortletURL iteratorURL) {
221         _iteratorURL = iteratorURL;
222     }
223 
224     public List<String> getHeaderNames() {
225         return _headerNames;
226     }
227 
228     public void setHeaderNames(List<String> headerNames) {
229         _headerNames = headerNames;
230     }
231 
232     public Map<String, String> getOrderableHeaders() {
233         return _orderableHeaders;
234     }
235 
236     public void setOrderableHeaders(Map<String, String> orderableHeaders) {
237         _orderableHeaders = orderableHeaders;
238     }
239 
240     public String getOrderByCol() {
241         return _orderByCol;
242     }
243 
244     public void setOrderByCol(String orderByCol) {
245         _orderByCol = orderByCol;
246 
247         _iteratorURL.setParameter(_orderByColParam, _orderByCol);
248     }
249 
250     public String getOrderByColParam() {
251         return _orderByColParam;
252     }
253 
254     public void setOrderByColParam(String orderByColParam) {
255         _orderByColParam = orderByColParam;
256     }
257 
258     public String getOrderByType() {
259         return _orderByType;
260     }
261 
262     public void setOrderByType(String orderByType) {
263         _orderByType = orderByType;
264 
265         _iteratorURL.setParameter(_orderByTypeParam, _orderByType);
266     }
267 
268     public String getOrderByTypeParam() {
269         return _orderByTypeParam;
270     }
271 
272     public void setOrderByTypeParam(String orderByTypeParam) {
273         _orderByTypeParam = orderByTypeParam;
274     }
275 
276     public OrderByComparator getOrderByComparator() {
277         return _orderByComparator;
278     }
279 
280     public void setOrderByComparator(OrderByComparator orderByComparator) {
281         _orderByComparator = orderByComparator;
282     }
283 
284     public String getEmptyResultsMessage() {
285         return _emptyResultsMessage;
286     }
287 
288     public void setEmptyResultsMessage(String emptyResultsMessage) {
289         _emptyResultsMessage = emptyResultsMessage;
290     }
291 
292     public RowChecker getRowChecker() {
293         return _rowChecker;
294     }
295 
296     public void setRowChecker(RowChecker rowChecker) {
297         _rowChecker = rowChecker;
298     }
299 
300     public boolean isHover() {
301         return _hover;
302     }
303 
304     public void setHover(boolean hover) {
305         _hover = hover;
306     }
307 
308     private void _calculateStartAndEnd() {
309         _start = (_cur - 1) * _delta;
310         _end = _start + _delta;
311 
312         _resultEnd = _end;
313 
314         if (_resultEnd > _total) {
315             _resultEnd = _total;
316         }
317     }
318 
319     private String _id;
320     private DisplayTerms _displayTerms;
321     private DisplayTerms _searchTerms;
322     private int _cur;
323     private String _curParam = DEFAULT_CUR_PARAM;
324     private int _delta = DEFAULT_DELTA;
325     private String _deltaParam = DEFAULT_DELTA_PARAM;
326     private int _maxPages = DEFAULT_MAX_PAGES;
327     private int _start;
328     private int _end;
329     private int _resultEnd;
330     private int _total;
331     private List<R> _results = new ArrayList<R>();
332     private List<ResultRow> _resultRows = new ArrayList<ResultRow>();
333     private PortletURL _iteratorURL;
334     private List<String> _headerNames;
335     private Map<String, String> _orderableHeaders;
336     private String _orderByCol;
337     private String _orderByColParam = DEFAULT_ORDER_BY_COL_PARAM;
338     private String _orderByType;
339     private String _orderByTypeParam = DEFAULT_ORDER_BY_TYPE_PARAM;
340     private OrderByComparator _orderByComparator;
341     private String _emptyResultsMessage;
342     private RowChecker _rowChecker;
343     private boolean _hover = true;
344 
345 }