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