001    /**
002     * Copyright (c) 2000-2013 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.DeterminateKeyGenerator;
018    import com.liferay.portal.kernel.util.FriendlyURLNormalizerUtil;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.OrderByComparator;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.PropsKeys;
023    import com.liferay.portal.kernel.util.PropsUtil;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.util.TextFormatter;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.util.PortalUtil;
029    
030    import java.util.ArrayList;
031    import java.util.List;
032    import java.util.Map;
033    
034    import javax.portlet.PortletRequest;
035    import javax.portlet.PortletURL;
036    
037    import javax.servlet.http.HttpServletRequest;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     */
042    public class SearchContainer<R> {
043    
044            public static final int DEFAULT_CUR = 1;
045    
046            public static final String DEFAULT_CUR_PARAM = "cur";
047    
048            /**
049             * @deprecated Use <code>DEFAULT_CUR</code>.
050             */
051            public static final int DEFAULT_CUR_VALUE = DEFAULT_CUR;
052    
053            public static final int DEFAULT_DELTA = GetterUtil.getInteger(
054                    PropsUtil.get(PropsKeys.SEARCH_CONTAINER_PAGE_DEFAULT_DELTA));
055    
056            public static final boolean DEFAULT_DELTA_CONFIGURABLE = true;
057    
058            public static final String DEFAULT_DELTA_PARAM = "delta";
059    
060            /**
061             * @deprecated LPS-6312
062             */
063            public static final int DEFAULT_MAX_PAGES = 25;
064    
065            public static final String DEFAULT_ORDER_BY_COL_PARAM = "orderByCol";
066    
067            public static final String DEFAULT_ORDER_BY_TYPE_PARAM = "orderByType";
068    
069            public static final int MAX_DELTA = 200;
070    
071            public SearchContainer() {
072            }
073    
074            public SearchContainer(
075                    PortletRequest portletRequest, DisplayTerms displayTerms,
076                    DisplayTerms searchTerms, String curParam, int cur, int delta,
077                    PortletURL iteratorURL, List<String> headerNames,
078                    String emptyResultsMessage) {
079    
080                    _portletRequest = portletRequest;
081                    _displayTerms = displayTerms;
082                    _searchTerms = searchTerms;
083    
084                    _curParam = curParam;
085    
086                    boolean resetCur = ParamUtil.getBoolean(portletRequest, "resetCur");
087    
088                    if (resetCur) {
089                            _cur = DEFAULT_CUR;
090                    }
091                    else {
092                            if (cur < 1) {
093                                    _cur = ParamUtil.getInteger(
094                                            portletRequest, _curParam, DEFAULT_CUR);
095    
096                                    if (_cur < 1) {
097                                            _cur = DEFAULT_CUR;
098                                    }
099                            }
100                            else {
101                                    _cur = cur;
102                            }
103                    }
104    
105                    if (!_curParam.equals(DEFAULT_CUR_PARAM)) {
106                            _deltaParam =
107                                    DEFAULT_DELTA_PARAM +
108                                            StringUtil.replace(
109                                                    _curParam, DEFAULT_CUR_PARAM, StringPool.BLANK);
110                    }
111    
112                    setDelta(ParamUtil.getInteger(portletRequest, _deltaParam, delta));
113    
114                    _iteratorURL = iteratorURL;
115    
116                    _iteratorURL.setParameter(_curParam, String.valueOf(_cur));
117                    _iteratorURL.setParameter(_deltaParam, String.valueOf(_delta));
118                    _iteratorURL.setParameter(
119                            DisplayTerms.KEYWORDS,
120                            ParamUtil.getString(portletRequest, DisplayTerms.KEYWORDS));
121                    _iteratorURL.setParameter(
122                            DisplayTerms.ADVANCED_SEARCH,
123                            String.valueOf(
124                                    ParamUtil.getBoolean(
125                                            portletRequest, DisplayTerms.ADVANCED_SEARCH)));
126                    _iteratorURL.setParameter(
127                            DisplayTerms.AND_OPERATOR,
128                            String.valueOf(
129                                    ParamUtil.getBoolean(
130                                            portletRequest, DisplayTerms.AND_OPERATOR, true)));
131    
132                    if (headerNames != null) {
133                            _headerNames = new ArrayList<String>(headerNames.size());
134    
135                            _headerNames.addAll(headerNames);
136    
137                            _buildNormalizedHeaderNames(_headerNames);
138                    }
139    
140                    _emptyResultsMessage = emptyResultsMessage;
141            }
142    
143            public SearchContainer(
144                    PortletRequest portletRequest, DisplayTerms displayTerms,
145                    DisplayTerms searchTerms, String curParam, int delta,
146                    PortletURL iteratorURL, List<String> headerNames,
147                    String emptyResultsMessage) {
148    
149                    this (
150                            portletRequest, displayTerms, searchTerms, curParam, 0, delta,
151                            iteratorURL, headerNames, emptyResultsMessage);
152            }
153    
154            public SearchContainer(
155                    PortletRequest portletRequest, PortletURL iteratorURL,
156                    List<String> headerNames, String emptyResultsMessage) {
157    
158                    this(
159                            portletRequest, null, null, DEFAULT_CUR_PARAM, DEFAULT_DELTA,
160                            iteratorURL, headerNames, emptyResultsMessage);
161            }
162    
163            public String getClassName() {
164                    return _className;
165            }
166    
167            public int getCur() {
168                    return _cur;
169            }
170    
171            public String getCurParam() {
172                    return _curParam;
173            }
174    
175            /**
176             * @deprecated Use <code>getCur</code>.
177             */
178            public int getCurValue() {
179                    return getCur();
180            }
181    
182            public int getDelta() {
183                    return _delta;
184            }
185    
186            public String getDeltaParam() {
187                    return _deltaParam;
188            }
189    
190            public DisplayTerms getDisplayTerms() {
191                    return _displayTerms;
192            }
193    
194            public String getEmptyResultsMessage() {
195                    return _emptyResultsMessage;
196            }
197    
198            public int getEnd() {
199                    return _end;
200            }
201    
202            public List<String> getHeaderNames() {
203                    return _headerNames;
204            }
205    
206            public String getId(HttpServletRequest request, String namespace) {
207                    if (_uniqueId) {
208                            return _id;
209                    }
210    
211                    if (Validator.isNotNull(_id)) {
212                            _id = PortalUtil.getUniqueElementId(request, namespace, _id);
213                            _uniqueId = true;
214    
215                            return _id;
216                    }
217    
218                    String id = null;
219    
220                    if (Validator.isNotNull(_className)) {
221                            String simpleClassName = _className;
222    
223                            int pos = simpleClassName.lastIndexOf(StringPool.PERIOD);
224    
225                            if (pos != -1) {
226                                    simpleClassName = simpleClassName.substring(pos + 1);
227                            }
228    
229                            String variableCasingSimpleClassName = TextFormatter.format(
230                                    simpleClassName, TextFormatter.I);
231    
232                            id = TextFormatter.formatPlural(variableCasingSimpleClassName);
233    
234                            id = id.concat("SearchContainer");
235    
236                            _id = PortalUtil.getUniqueElementId(request, namespace, id);
237                            _uniqueId = true;
238    
239                            return _id;
240                    }
241                    else {
242                            id = DeterminateKeyGenerator.generate("taglib_search_container");
243    
244                            _id = id.concat("SearchContainer");
245                            _uniqueId = true;
246    
247                            return _id;
248                    }
249            }
250    
251            public PortletURL getIteratorURL() {
252                    return _iteratorURL;
253            }
254    
255            /**
256             * @deprecated LPS-6312
257             */
258            public int getMaxPages() {
259                    return _maxPages;
260            }
261    
262            public List<String> getNormalizedHeaderNames() {
263                    return _normalizedHeaderNames;
264            }
265    
266            public Map<String, String> getOrderableHeaders() {
267                    return _orderableHeaders;
268            }
269    
270            public String getOrderByCol() {
271                    return _orderByCol;
272            }
273    
274            public String getOrderByColParam() {
275                    return _orderByColParam;
276            }
277    
278            public OrderByComparator getOrderByComparator() {
279                    return _orderByComparator;
280            }
281    
282            public String getOrderByJS() {
283                    return _orderByJS;
284            }
285    
286            public String getOrderByType() {
287                    return _orderByType;
288            }
289    
290            public String getOrderByTypeParam() {
291                    return _orderByTypeParam;
292            }
293    
294            public PortletRequest getPortletRequest() {
295                    return _portletRequest;
296            }
297    
298            public int getResultEnd() {
299                    return _resultEnd;
300            }
301    
302            public List<ResultRow> getResultRows() {
303                    return _resultRows;
304            }
305    
306            public List<R> getResults() {
307                    return _results;
308            }
309    
310            public RowChecker getRowChecker() {
311                    return _rowChecker;
312            }
313    
314            public DisplayTerms getSearchTerms() {
315                    return _searchTerms;
316            }
317    
318            public int getStart() {
319                    return _start;
320            }
321    
322            public int getTotal() {
323                    return _total;
324            }
325    
326            public boolean isDeltaConfigurable() {
327                    return _deltaConfigurable;
328            }
329    
330            public boolean isHover() {
331                    return _hover;
332            }
333    
334            public boolean isRecalculateCur(int total) {
335                    if (((_cur - 1) * _delta) >= total) {
336                            return true;
337                    }
338    
339                    return false;
340            }
341    
342            public boolean recalculateCur(int total) {
343                    if (isRecalculateCur(total)) {
344                            setTotal(total);
345    
346                            return true;
347                    }
348    
349                    return false;
350            }
351    
352            public void setClassName(String className) {
353                    _className = className;
354            }
355    
356            public void setDelta(int delta) {
357                    if (delta <= 0) {
358                            _delta = DEFAULT_DELTA;
359                    }
360                    else if (delta > MAX_DELTA) {
361                            _delta = MAX_DELTA;
362                    }
363                    else {
364                            _delta = delta;
365                    }
366    
367                    _calculateStartAndEnd();
368            }
369    
370            public void setDeltaConfigurable(boolean deltaConfigurable) {
371                    _deltaConfigurable = deltaConfigurable;
372            }
373    
374            public void setDeltaParam(String deltaParam) {
375                    _deltaParam = deltaParam;
376            }
377    
378            public void setEmptyResultsMessage(String emptyResultsMessage) {
379                    _emptyResultsMessage = emptyResultsMessage;
380            }
381    
382            public void setHeaderNames(List<String> headerNames) {
383                    _headerNames = headerNames;
384    
385                    _buildNormalizedHeaderNames(headerNames);
386            }
387    
388            public void setHover(boolean hover) {
389                    _hover = hover;
390            }
391    
392            public void setId(String id) {
393                    _id = id;
394            }
395    
396            public void setIteratorURL(PortletURL iteratorURL) {
397                    _iteratorURL = iteratorURL;
398            }
399    
400            /**
401             * @deprecated LPS-6312
402             */
403            public void setMaxPages(int maxPages) {
404                    _maxPages = maxPages;
405            }
406    
407            public void setOrderableHeaders(Map<String, String> orderableHeaders) {
408                    _orderableHeaders = orderableHeaders;
409            }
410    
411            public void setOrderByCol(String orderByCol) {
412                    _orderByCol = orderByCol;
413    
414                    _iteratorURL.setParameter(_orderByColParam, _orderByCol);
415            }
416    
417            public void setOrderByColParam(String orderByColParam) {
418                    _orderByColParam = orderByColParam;
419            }
420    
421            public void setOrderByComparator(OrderByComparator orderByComparator) {
422                    _orderByComparator = orderByComparator;
423            }
424    
425            public void setOrderByJS(String orderByJS) {
426                    _orderByJS = orderByJS;
427            }
428    
429            public void setOrderByType(String orderByType) {
430                    _orderByType = orderByType;
431    
432                    _iteratorURL.setParameter(_orderByTypeParam, _orderByType);
433            }
434    
435            public void setOrderByTypeParam(String orderByTypeParam) {
436                    _orderByTypeParam = orderByTypeParam;
437            }
438    
439            public void setResults(List<R> results) {
440                    _results = results;
441            }
442    
443            public void setRowChecker(RowChecker rowChecker) {
444                    _rowChecker = rowChecker;
445            }
446    
447            public void setTotal(int total) {
448                    _total = total;
449    
450                    _calculateCur();
451                    _calculateStartAndEnd();
452            }
453    
454            private void _buildNormalizedHeaderNames(List<String> headerNames) {
455                    if (headerNames == null) {
456                            return;
457                    }
458    
459                    _normalizedHeaderNames = new ArrayList<String>(headerNames.size());
460    
461                    for (String headerName : headerNames) {
462                            _normalizedHeaderNames.add(
463                                    FriendlyURLNormalizerUtil.normalize(headerName));
464                    }
465            }
466    
467            private void _calculateCur() {
468                    if (_total == 0) {
469                            _cur = DEFAULT_CUR;
470    
471                            return;
472                    }
473    
474                    if (isRecalculateCur(_total)) {
475                            if ((_total % _delta) == 0) {
476                                    _cur = (_total / _delta);
477                            }
478                            else {
479                                    _cur = (_total / _delta) + 1;
480                            }
481                    }
482            }
483    
484            private void _calculateStartAndEnd() {
485                    _start = (_cur - 1) * _delta;
486                    _end = _start + _delta;
487    
488                    _resultEnd = _end;
489    
490                    if (_resultEnd > _total) {
491                            _resultEnd = _total;
492                    }
493            }
494    
495            private String _className;
496            private int _cur;
497            private String _curParam = DEFAULT_CUR_PARAM;
498            private int _delta = DEFAULT_DELTA;
499            private boolean _deltaConfigurable = DEFAULT_DELTA_CONFIGURABLE;
500            private String _deltaParam = DEFAULT_DELTA_PARAM;
501            private DisplayTerms _displayTerms;
502            private String _emptyResultsMessage;
503            private int _end;
504            private List<String> _headerNames;
505            private boolean _hover = true;
506            private String _id;
507            private PortletURL _iteratorURL;
508    
509            /**
510             * @deprecated LPS-6312
511             */
512            private int _maxPages = DEFAULT_MAX_PAGES;
513    
514            private List<String> _normalizedHeaderNames;
515            private Map<String, String> _orderableHeaders;
516            private String _orderByCol;
517            private String _orderByColParam = DEFAULT_ORDER_BY_COL_PARAM;
518            private OrderByComparator _orderByComparator;
519            private String _orderByJS;
520            private String _orderByType;
521            private String _orderByTypeParam = DEFAULT_ORDER_BY_TYPE_PARAM;
522            private PortletRequest _portletRequest;
523            private int _resultEnd;
524            private List<ResultRow> _resultRows = new ArrayList<ResultRow>();
525            private List<R> _results = new ArrayList<R>();
526            private RowChecker _rowChecker;
527            private DisplayTerms _searchTerms;
528            private int _start;
529            private int _total;
530            private boolean _uniqueId;
531    
532    }