001    /**
002     * Copyright (c) 2000-2011 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                    if (cur < 1) {
100                            _cur = ParamUtil.getInteger(portletRequest, _curParam, DEFAULT_CUR);
101    
102                            if (_cur < 1) {
103                                    _cur = DEFAULT_CUR;
104                            }
105                    }
106                    else {
107                            _cur = cur;
108                    }
109    
110                    if (!_curParam.equals(DEFAULT_CUR_PARAM)) {
111                            _deltaParam =
112                                    DEFAULT_DELTA_PARAM +
113                                            StringUtil.replace(
114                                                    _curParam, DEFAULT_CUR_PARAM, StringPool.BLANK);
115                    }
116    
117                    setDelta(ParamUtil.getInteger(portletRequest, _deltaParam, delta));
118    
119                    _iteratorURL = iteratorURL;
120    
121                    _iteratorURL.setParameter(_curParam, String.valueOf(_cur));
122                    _iteratorURL.setParameter(_deltaParam, String.valueOf(_delta));
123                    _iteratorURL.setParameter(
124                            DisplayTerms.KEYWORDS,
125                            ParamUtil.getString(portletRequest, DisplayTerms.KEYWORDS));
126                    _iteratorURL.setParameter(
127                            DisplayTerms.ADVANCED_SEARCH,
128                            String.valueOf(
129                                    ParamUtil.getBoolean(
130                                            portletRequest, DisplayTerms.ADVANCED_SEARCH)));
131                    _iteratorURL.setParameter(
132                            DisplayTerms.AND_OPERATOR,
133                            String.valueOf(
134                                    ParamUtil.getBoolean(
135                                            portletRequest, DisplayTerms.AND_OPERATOR, true)));
136    
137                    if (headerNames != null) {
138                            _headerNames = new ArrayList<String>(headerNames.size());
139    
140                            _headerNames.addAll(headerNames);
141                    }
142    
143                    _emptyResultsMessage = emptyResultsMessage;
144            }
145    
146            public int getCur() {
147                    return _cur;
148            }
149    
150            public String getCurParam() {
151                    return _curParam;
152            }
153    
154            /**
155             * @deprecated Use <code>getCur</code>.
156             */
157            public int getCurValue() {
158                    return getCur();
159            }
160    
161            public int getDelta() {
162                    return _delta;
163            }
164    
165            public String getDeltaParam() {
166                    return _deltaParam;
167            }
168    
169            public DisplayTerms getDisplayTerms() {
170                    return _displayTerms;
171            }
172    
173            public String getEmptyResultsMessage() {
174                    return _emptyResultsMessage;
175            }
176    
177            public int getEnd() {
178                    return _end;
179            }
180    
181            public List<String> getHeaderNames() {
182                    return _headerNames;
183            }
184    
185            public String getId() {
186                    return _id;
187            }
188    
189            public PortletURL getIteratorURL() {
190                    return _iteratorURL;
191            }
192    
193            /**
194             * @deprecated LPS-6312
195             */
196            public int getMaxPages() {
197                    return _maxPages;
198            }
199    
200            public Map<String, String> getOrderableHeaders() {
201                    return _orderableHeaders;
202            }
203    
204            public String getOrderByCol() {
205                    return _orderByCol;
206            }
207    
208            public String getOrderByColParam() {
209                    return _orderByColParam;
210            }
211    
212            public OrderByComparator getOrderByComparator() {
213                    return _orderByComparator;
214            }
215    
216            public String getOrderByType() {
217                    return _orderByType;
218            }
219    
220            public String getOrderByTypeParam() {
221                    return _orderByTypeParam;
222            }
223    
224            public PortletRequest getPortletRequest() {
225                    return _portletRequest;
226            }
227    
228            public int getResultEnd() {
229                    return _resultEnd;
230            }
231    
232            public List<ResultRow> getResultRows() {
233                    return _resultRows;
234            }
235    
236            public List<R> getResults() {
237                    return _results;
238            }
239    
240            public RowChecker getRowChecker() {
241                    return _rowChecker;
242            }
243    
244            public DisplayTerms getSearchTerms() {
245                    return _searchTerms;
246            }
247    
248            public int getStart() {
249                    return _start;
250            }
251    
252            public int getTotal() {
253                    return _total;
254            }
255    
256            public boolean isDeltaConfigurable() {
257                    return _deltaConfigurable;
258            }
259    
260            public boolean isHover() {
261                    return _hover;
262            }
263    
264            public void setDelta(int delta) {
265                    if (delta <= 0) {
266                            _delta = DEFAULT_DELTA;
267                    }
268                    else if (delta > MAX_DELTA) {
269                            _delta = MAX_DELTA;
270                    }
271                    else {
272                            _delta = delta;
273                    }
274    
275                    _calculateStartAndEnd();
276            }
277    
278            public void setDeltaConfigurable(boolean deltaConfigurable) {
279                    _deltaConfigurable = deltaConfigurable;
280            }
281    
282            public void setDeltaParam(String deltaParam) {
283                    _deltaParam = deltaParam;
284            }
285    
286            public void setEmptyResultsMessage(String emptyResultsMessage) {
287                    _emptyResultsMessage = emptyResultsMessage;
288            }
289    
290            public void setHeaderNames(List<String> headerNames) {
291                    _headerNames = headerNames;
292            }
293    
294            public void setHover(boolean hover) {
295                    _hover = hover;
296            }
297    
298            public void setId(String id) {
299                    _id = id;
300            }
301    
302            public void setIteratorURL(PortletURL iteratorURL) {
303                    _iteratorURL = iteratorURL;
304            }
305    
306            /**
307             * @deprecated LPS-6312
308             */
309            public void setMaxPages(int maxPages) {
310                    _maxPages = maxPages;
311            }
312    
313            public void setOrderableHeaders(Map<String, String> orderableHeaders) {
314                    _orderableHeaders = orderableHeaders;
315            }
316    
317            public void setOrderByCol(String orderByCol) {
318                    _orderByCol = orderByCol;
319    
320                    _iteratorURL.setParameter(_orderByColParam, _orderByCol);
321            }
322    
323            public void setOrderByColParam(String orderByColParam) {
324                    _orderByColParam = orderByColParam;
325            }
326    
327            public void setOrderByComparator(OrderByComparator orderByComparator) {
328                    _orderByComparator = orderByComparator;
329            }
330    
331            public void setOrderByType(String orderByType) {
332                    _orderByType = orderByType;
333    
334                    _iteratorURL.setParameter(_orderByTypeParam, _orderByType);
335            }
336    
337            public void setOrderByTypeParam(String orderByTypeParam) {
338                    _orderByTypeParam = orderByTypeParam;
339            }
340    
341            public void setResults(List<R> results) {
342                    _results = results;
343            }
344    
345            public void setRowChecker(RowChecker rowChecker) {
346                    _rowChecker = rowChecker;
347            }
348    
349            public void setTotal(int total) {
350                    _total = total;
351    
352                    if (((_cur - 1) * _delta) >= _total) {
353                            _cur = DEFAULT_CUR;
354                    }
355    
356                    _calculateStartAndEnd();
357            }
358    
359            private void _calculateStartAndEnd() {
360                    _start = (_cur - 1) * _delta;
361                    _end = _start + _delta;
362    
363                    _resultEnd = _end;
364    
365                    if (_resultEnd > _total) {
366                            _resultEnd = _total;
367                    }
368            }
369    
370            private int _cur;
371            private String _curParam = DEFAULT_CUR_PARAM;
372            private int _delta = DEFAULT_DELTA;
373            private boolean _deltaConfigurable = DEFAULT_DELTA_CONFIGURABLE;
374            private String _deltaParam = DEFAULT_DELTA_PARAM;
375            private DisplayTerms _displayTerms;
376            private String _emptyResultsMessage;
377            private int _end;
378            private List<String> _headerNames;
379            private boolean _hover = true;
380            private String _id;
381            private PortletURL _iteratorURL;
382    
383            /**
384             * @deprecated LPS-6312
385             */
386            private int _maxPages = DEFAULT_MAX_PAGES;
387    
388            private Map<String, String> _orderableHeaders;
389            private String _orderByCol;
390            private String _orderByColParam = DEFAULT_ORDER_BY_COL_PARAM;
391            private OrderByComparator _orderByComparator;
392            private String _orderByType;
393            private String _orderByTypeParam = DEFAULT_ORDER_BY_TYPE_PARAM;
394            private PortletRequest _portletRequest;
395            private int _resultEnd;
396            private List<ResultRow> _resultRows = new ArrayList<ResultRow>();
397            private List<R> _results = new ArrayList<R>();
398            private RowChecker _rowChecker;
399            private DisplayTerms _searchTerms;
400            private int _start;
401            private int _total;
402    
403    }