001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.dao.search;
016    
017    import com.liferay.portal.kernel.util.GetterUtil;
018    import com.liferay.portal.kernel.util.OrderByComparator;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.PropsKeys;
021    import com.liferay.portal.kernel.util.PropsUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.StringUtil;
024    
025    import java.util.ArrayList;
026    import java.util.List;
027    import java.util.Map;
028    
029    import javax.portlet.PortletRequest;
030    import javax.portlet.PortletURL;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class SearchContainer<R> {
036    
037            public static final int DEFAULT_CUR = 1;
038    
039            public static final String DEFAULT_CUR_PARAM = "cur";
040    
041            /**
042             * @deprecated Use <code>DEFAULT_CUR</code>.
043             */
044            public static final int DEFAULT_CUR_VALUE = DEFAULT_CUR;
045    
046            public static final int DEFAULT_DELTA = GetterUtil.getInteger(
047                    PropsUtil.get(PropsKeys.SEARCH_CONTAINER_PAGE_DEFAULT_DELTA));
048    
049            public static final boolean DEFAULT_DELTA_CONFIGURABLE = true;
050    
051            public static final String DEFAULT_DELTA_PARAM = "delta";
052    
053            /**
054             * @deprecated LPS-6312
055             */
056            public static final int DEFAULT_MAX_PAGES = 25;
057    
058            public static final String DEFAULT_ORDER_BY_COL_PARAM = "orderByCol";
059    
060            public static final String DEFAULT_ORDER_BY_TYPE_PARAM = "orderByType";
061    
062            public static final int MAX_DELTA = 200;
063    
064            public SearchContainer() {
065            }
066    
067            public SearchContainer(
068                    PortletRequest portletRequest, PortletURL iteratorURL,
069                    List<String> headerNames, String emptyResultsMessage) {
070    
071                    this(
072                            portletRequest, null, null, DEFAULT_CUR_PARAM, DEFAULT_DELTA,
073                            iteratorURL, headerNames, emptyResultsMessage);
074            }
075    
076            public SearchContainer(
077                    PortletRequest portletRequest, DisplayTerms displayTerms,
078                    DisplayTerms searchTerms, String curParam, int delta,
079                    PortletURL iteratorURL, List<String> headerNames,
080                    String emptyResultsMessage) {
081    
082                    this (
083                            portletRequest, displayTerms, searchTerms, curParam, 0, delta,
084                            iteratorURL, headerNames, emptyResultsMessage);
085            }
086    
087            public SearchContainer(
088                    PortletRequest portletRequest, DisplayTerms displayTerms,
089                    DisplayTerms searchTerms, String curParam, int cur, int delta,
090                    PortletURL iteratorURL, List<String> headerNames,
091                    String emptyResultsMessage) {
092    
093                    _portletRequest = portletRequest;
094                    _displayTerms = displayTerms;
095                    _searchTerms = searchTerms;
096    
097                    _curParam = curParam;
098    
099                    boolean resetCur = ParamUtil.getBoolean(portletRequest, "resetCur");
100    
101                    if (resetCur) {
102                            _cur = DEFAULT_CUR;
103                    }
104                    else {
105                            if (cur < 1) {
106                                    _cur = ParamUtil.getInteger(
107                                            portletRequest, _curParam, DEFAULT_CUR);
108    
109                                    if (_cur < 1) {
110                                            _cur = DEFAULT_CUR;
111                                    }
112                            }
113                            else {
114                                    _cur = cur;
115                            }
116                    }
117    
118                    if (!_curParam.equals(DEFAULT_CUR_PARAM)) {
119                            _deltaParam =
120                                    DEFAULT_DELTA_PARAM +
121                                            StringUtil.replace(
122                                                    _curParam, DEFAULT_CUR_PARAM, StringPool.BLANK);
123                    }
124    
125                    setDelta(ParamUtil.getInteger(portletRequest, _deltaParam, delta));
126    
127                    _iteratorURL = iteratorURL;
128    
129                    _iteratorURL.setParameter(_curParam, String.valueOf(_cur));
130                    _iteratorURL.setParameter(_deltaParam, String.valueOf(_delta));
131                    _iteratorURL.setParameter(
132                            DisplayTerms.KEYWORDS,
133                            ParamUtil.getString(portletRequest, DisplayTerms.KEYWORDS));
134                    _iteratorURL.setParameter(
135                            DisplayTerms.ADVANCED_SEARCH,
136                            String.valueOf(
137                                    ParamUtil.getBoolean(
138                                            portletRequest, DisplayTerms.ADVANCED_SEARCH)));
139                    _iteratorURL.setParameter(
140                            DisplayTerms.AND_OPERATOR,
141                            String.valueOf(
142                                    ParamUtil.getBoolean(
143                                            portletRequest, DisplayTerms.AND_OPERATOR, true)));
144    
145                    if (headerNames != null) {
146                            _headerNames = new ArrayList<String>(headerNames.size());
147    
148                            _headerNames.addAll(headerNames);
149                    }
150    
151                    _emptyResultsMessage = emptyResultsMessage;
152            }
153    
154            public int getCur() {
155                    return _cur;
156            }
157    
158            public String getCurParam() {
159                    return _curParam;
160            }
161    
162            /**
163             * @deprecated Use <code>getCur</code>.
164             */
165            public int getCurValue() {
166                    return getCur();
167            }
168    
169            public int getDelta() {
170                    return _delta;
171            }
172    
173            public String getDeltaParam() {
174                    return _deltaParam;
175            }
176    
177            public DisplayTerms getDisplayTerms() {
178                    return _displayTerms;
179            }
180    
181            public String getEmptyResultsMessage() {
182                    return _emptyResultsMessage;
183            }
184    
185            public int getEnd() {
186                    return _end;
187            }
188    
189            public List<String> getHeaderNames() {
190                    return _headerNames;
191            }
192    
193            public String getId() {
194                    return _id;
195            }
196    
197            public PortletURL getIteratorURL() {
198                    return _iteratorURL;
199            }
200    
201            /**
202             * @deprecated LPS-6312
203             */
204            public int getMaxPages() {
205                    return _maxPages;
206            }
207    
208            public Map<String, String> getOrderableHeaders() {
209                    return _orderableHeaders;
210            }
211    
212            public String getOrderByCol() {
213                    return _orderByCol;
214            }
215    
216            public String getOrderByColParam() {
217                    return _orderByColParam;
218            }
219    
220            public OrderByComparator getOrderByComparator() {
221                    return _orderByComparator;
222            }
223    
224            public String getOrderByJS() {
225                    return _orderByJS;
226            }
227    
228            public String getOrderByType() {
229                    return _orderByType;
230            }
231    
232            public String getOrderByTypeParam() {
233                    return _orderByTypeParam;
234            }
235    
236            public PortletRequest getPortletRequest() {
237                    return _portletRequest;
238            }
239    
240            public int getResultEnd() {
241                    return _resultEnd;
242            }
243    
244            public List<ResultRow> getResultRows() {
245                    return _resultRows;
246            }
247    
248            public List<R> getResults() {
249                    return _results;
250            }
251    
252            public RowChecker getRowChecker() {
253                    return _rowChecker;
254            }
255    
256            public DisplayTerms getSearchTerms() {
257                    return _searchTerms;
258            }
259    
260            public int getStart() {
261                    return _start;
262            }
263    
264            public int getTotal() {
265                    return _total;
266            }
267    
268            public boolean isDeltaConfigurable() {
269                    return _deltaConfigurable;
270            }
271    
272            public boolean isHover() {
273                    return _hover;
274            }
275    
276            public void setDelta(int delta) {
277                    if (delta <= 0) {
278                            _delta = DEFAULT_DELTA;
279                    }
280                    else if (delta > MAX_DELTA) {
281                            _delta = MAX_DELTA;
282                    }
283                    else {
284                            _delta = delta;
285                    }
286    
287                    _calculateStartAndEnd();
288            }
289    
290            public void setDeltaConfigurable(boolean deltaConfigurable) {
291                    _deltaConfigurable = deltaConfigurable;
292            }
293    
294            public void setDeltaParam(String deltaParam) {
295                    _deltaParam = deltaParam;
296            }
297    
298            public void setEmptyResultsMessage(String emptyResultsMessage) {
299                    _emptyResultsMessage = emptyResultsMessage;
300            }
301    
302            public void setHeaderNames(List<String> headerNames) {
303                    _headerNames = headerNames;
304            }
305    
306            public void setHover(boolean hover) {
307                    _hover = hover;
308            }
309    
310            public void setId(String id) {
311                    _id = id;
312            }
313    
314            public void setIteratorURL(PortletURL iteratorURL) {
315                    _iteratorURL = iteratorURL;
316            }
317    
318            /**
319             * @deprecated LPS-6312
320             */
321            public void setMaxPages(int maxPages) {
322                    _maxPages = maxPages;
323            }
324    
325            public void setOrderableHeaders(Map<String, String> orderableHeaders) {
326                    _orderableHeaders = orderableHeaders;
327            }
328    
329            public void setOrderByCol(String orderByCol) {
330                    _orderByCol = orderByCol;
331    
332                    _iteratorURL.setParameter(_orderByColParam, _orderByCol);
333            }
334    
335            public void setOrderByColParam(String orderByColParam) {
336                    _orderByColParam = orderByColParam;
337            }
338    
339            public void setOrderByComparator(OrderByComparator orderByComparator) {
340                    _orderByComparator = orderByComparator;
341            }
342    
343            public void setOrderByJS(String orderByJS) {
344                    _orderByJS = orderByJS;
345            }
346    
347            public void setOrderByType(String orderByType) {
348                    _orderByType = orderByType;
349    
350                    _iteratorURL.setParameter(_orderByTypeParam, _orderByType);
351            }
352    
353            public void setOrderByTypeParam(String orderByTypeParam) {
354                    _orderByTypeParam = orderByTypeParam;
355            }
356    
357            public void setResults(List<R> results) {
358                    _results = results;
359            }
360    
361            public void setRowChecker(RowChecker rowChecker) {
362                    _rowChecker = rowChecker;
363            }
364    
365            public void setTotal(int total) {
366                    _total = total;
367    
368                    if (((_cur - 1) * _delta) >= _total) {
369                            _cur = DEFAULT_CUR;
370                    }
371    
372                    _calculateStartAndEnd();
373            }
374    
375            private void _calculateStartAndEnd() {
376                    _start = (_cur - 1) * _delta;
377                    _end = _start + _delta;
378    
379                    _resultEnd = _end;
380    
381                    if (_resultEnd > _total) {
382                            _resultEnd = _total;
383                    }
384            }
385    
386            private int _cur;
387            private String _curParam = DEFAULT_CUR_PARAM;
388            private int _delta = DEFAULT_DELTA;
389            private boolean _deltaConfigurable = DEFAULT_DELTA_CONFIGURABLE;
390            private String _deltaParam = DEFAULT_DELTA_PARAM;
391            private DisplayTerms _displayTerms;
392            private String _emptyResultsMessage;
393            private int _end;
394            private List<String> _headerNames;
395            private boolean _hover = true;
396            private String _id;
397            private PortletURL _iteratorURL;
398    
399            /**
400             * @deprecated LPS-6312
401             */
402            private int _maxPages = DEFAULT_MAX_PAGES;
403    
404            private Map<String, String> _orderableHeaders;
405            private String _orderByCol;
406            private String _orderByColParam = DEFAULT_ORDER_BY_COL_PARAM;
407            private OrderByComparator _orderByComparator;
408            private String _orderByJS;
409            private String _orderByType;
410            private String _orderByTypeParam = DEFAULT_ORDER_BY_TYPE_PARAM;
411            private PortletRequest _portletRequest;
412            private int _resultEnd;
413            private List<ResultRow> _resultRows = new ArrayList<ResultRow>();
414            private List<R> _results = new ArrayList<R>();
415            private RowChecker _rowChecker;
416            private DisplayTerms _searchTerms;
417            private int _start;
418            private int _total;
419    
420    }