001    /**
002     * Copyright (c) 2000-present 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.taglib.ui;
016    
017    import com.liferay.portal.kernel.dao.search.DisplayTerms;
018    import com.liferay.portal.kernel.dao.search.RowChecker;
019    import com.liferay.portal.kernel.dao.search.SearchContainer;
020    import com.liferay.portal.kernel.util.JavaConstants;
021    import com.liferay.portal.kernel.util.ListUtil;
022    import com.liferay.portal.kernel.util.OrderByComparator;
023    import com.liferay.portal.kernel.util.ParamUtil;
024    import com.liferay.portal.kernel.util.SearchContainerReference;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.StringUtil;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.taglib.util.ParamAndPropertyAncestorTagImpl;
029    
030    import java.util.List;
031    
032    import javax.portlet.MimeResponse;
033    import javax.portlet.PortletRequest;
034    import javax.portlet.PortletResponse;
035    import javax.portlet.PortletURL;
036    
037    import javax.servlet.jsp.JspException;
038    
039    /**
040     * @author Raymond Augé
041     * @author Roberto Díaz
042     */
043    public class SearchContainerTag<R> extends ParamAndPropertyAncestorTagImpl {
044    
045            @Override
046            public int doEndTag() {
047                    pageContext.setAttribute(
048                            _searchContainer.getTotalVar(), _searchContainer.getTotal());
049    
050                    _cssClass = StringPool.BLANK;
051                    _curParam = SearchContainer.DEFAULT_CUR_PARAM;
052                    _delta = SearchContainer.DEFAULT_DELTA;
053                    _deltaConfigurable = SearchContainer.DEFAULT_DELTA_CONFIGURABLE;
054                    _deltaParam = SearchContainer.DEFAULT_DELTA_PARAM;
055                    _displayTerms = null;
056                    _emptyResultsMessage = null;
057                    _headerNames = null;
058                    _id = null;
059                    _iteratorURL = null;
060                    _orderByCol = null;
061                    _orderByColParam = SearchContainer.DEFAULT_ORDER_BY_COL_PARAM;
062                    _orderByComparator = null;
063                    _orderByType = null;
064                    _orderByTypeParam = SearchContainer.DEFAULT_ORDER_BY_TYPE_PARAM;
065                    _rowChecker = null;
066                    _searchContainer = null;
067                    _searchTerms = null;
068                    _total = 0;
069                    _totalVar = SearchContainer.DEFAULT_TOTAL_VAR;
070                    _var = SearchContainer.DEFAULT_VAR;
071    
072                    return EVAL_PAGE;
073            }
074    
075            @Override
076            public int doStartTag() throws JspException {
077                    try {
078                            PortletRequest portletRequest =
079                                    (PortletRequest)request.getAttribute(
080                                            JavaConstants.JAVAX_PORTLET_REQUEST);
081                            PortletResponse portletResponse =
082                                    (PortletResponse)request.getAttribute(
083                                            JavaConstants.JAVAX_PORTLET_RESPONSE);
084    
085                            if (_iteratorURL == null) {
086                                    _iteratorURL =
087                                            ((MimeResponse)portletResponse).createRenderURL();
088                            }
089    
090                            if (_searchContainer == null) {
091                                    _searchContainer = new SearchContainer<R>(
092                                            portletRequest, _displayTerms, _searchTerms, getCurParam(),
093                                            getDelta(), _iteratorURL, null, _emptyResultsMessage);
094                            }
095    
096                            _searchContainer.setDeltaConfigurable(_deltaConfigurable);
097    
098                            if (Validator.isNotNull(_emptyResultsMessage)) {
099                                    _searchContainer.setEmptyResultsMessage(_emptyResultsMessage);
100                            }
101    
102                            if (_headerNames != null) {
103                                    _searchContainer.setHeaderNames(_headerNames);
104                            }
105    
106                            if (Validator.isNotNull(_id)) {
107                                    _searchContainer.setId(_id);
108                            }
109    
110                            if (Validator.isNotNull(_orderByColParam)) {
111                                    _searchContainer.setOrderByColParam(_orderByColParam);
112                            }
113    
114                            if (Validator.isNotNull(_orderByCol)) {
115                                    _searchContainer.setOrderByCol(_orderByCol);
116                            }
117                            else {
118                                    String orderByCol = ParamUtil.getString(
119                                            request, _searchContainer.getOrderByColParam(), null);
120    
121                                    if (orderByCol != null) {
122                                            _searchContainer.setOrderByCol(orderByCol);
123                                    }
124                            }
125    
126                            if (_orderByComparator != null) {
127                                    _searchContainer.setOrderByComparator(_orderByComparator);
128                            }
129    
130                            if (Validator.isNotNull(_orderByTypeParam)) {
131                                    _searchContainer.setOrderByTypeParam(_orderByTypeParam);
132                            }
133    
134                            if (Validator.isNotNull(_orderByType)) {
135                                    _searchContainer.setOrderByType(_orderByType);
136                            }
137                            else {
138                                    String orderByType = ParamUtil.getString(
139                                            request, _searchContainer.getOrderByTypeParam(), null);
140    
141                                    if (orderByType != null) {
142                                            _searchContainer.setOrderByType(orderByType);
143                                    }
144                            }
145    
146                            if (_rowChecker != null) {
147                                    _searchContainer.setRowChecker(_rowChecker);
148                            }
149    
150                            if (_total != 0) {
151                                    _searchContainer.setTotal(_total);
152                            }
153    
154                            if (Validator.isNotNull(_totalVar)) {
155                                    _searchContainer.setTotalVar(_totalVar);
156                            }
157    
158                            pageContext.setAttribute(
159                                    _searchContainer.getTotalVar(), _searchContainer.getTotal());
160                            pageContext.setAttribute(_var, _searchContainer);
161    
162                            SearchContainerReference searchContainerReference =
163                                    (SearchContainerReference)pageContext.getAttribute(
164                                            "searchContainerReference");
165    
166                            if ((searchContainerReference != null) &&
167                                    !_var.equals(SearchContainer.DEFAULT_VAR)) {
168    
169                                    searchContainerReference.register(_var, _searchContainer);
170                            }
171    
172                            return EVAL_BODY_INCLUDE;
173                    }
174                    catch (Exception e) {
175                            throw new JspException(e);
176                    }
177            }
178    
179            public String getCssClass() {
180                    return _cssClass;
181            }
182    
183            public String getCurParam() {
184                    return _curParam;
185            }
186    
187            public int getDelta() {
188                    return _delta;
189            }
190    
191            public String getDeltaParam() {
192                    return _deltaParam;
193            }
194    
195            public DisplayTerms getDisplayTerms() {
196                    return _displayTerms;
197            }
198    
199            public String getEmptyResultsMessage() {
200                    return _emptyResultsMessage;
201            }
202    
203            public PortletURL getIteratorURL() {
204                    return _iteratorURL;
205            }
206    
207            public String getOrderByCol() {
208                    return _orderByCol;
209            }
210    
211            public String getOrderByColParam() {
212                    return _orderByColParam;
213            }
214    
215            public OrderByComparator<R> getOrderByComparator() {
216                    return _orderByComparator;
217            }
218    
219            public String getOrderByType() {
220                    return _orderByType;
221            }
222    
223            public String getOrderByTypeParam() {
224                    return _orderByTypeParam;
225            }
226    
227            public RowChecker getRowChecker() {
228                    return _rowChecker;
229            }
230    
231            public SearchContainer<R> getSearchContainer() {
232                    return _searchContainer;
233            }
234    
235            public DisplayTerms getSearchTerms() {
236                    return _searchTerms;
237            }
238    
239            public int getTotal() {
240                    return _total;
241            }
242    
243            public String getTotalVar() {
244                    return _totalVar;
245            }
246    
247            public String getVar() {
248                    return _var;
249            }
250    
251            public boolean isDeltaConfigurable() {
252                    return _deltaConfigurable;
253            }
254    
255            /**
256             * @deprecated As of 6.2.0, with no direct replacement. See LPS-41307.
257             */
258            @Deprecated
259            public boolean isHasResults() {
260                    return true;
261            }
262    
263            public void setCssClass(String cssClass) {
264                    _cssClass = cssClass;
265            }
266    
267            public void setCurParam(String curParam) {
268                    _curParam = curParam;
269            }
270    
271            public void setDelta(int delta) {
272                    _delta = delta;
273            }
274    
275            public void setDeltaConfigurable(boolean deltaConfigurable) {
276                    _deltaConfigurable = deltaConfigurable;
277            }
278    
279            public void setDeltaParam(String deltaParam) {
280                    _deltaParam = deltaParam;
281            }
282    
283            public void setDisplayTerms(DisplayTerms displayTerms) {
284                    _displayTerms = displayTerms;
285            }
286    
287            public void setEmptyResultsMessage(String emptyResultsMessage) {
288                    _emptyResultsMessage = emptyResultsMessage;
289            }
290    
291            /**
292             * @deprecated As of 6.2.0, see LPS-41307
293             */
294            @Deprecated
295            public void setHasResults(boolean hasResults) {
296            }
297    
298            public void setHeaderNames(String headerNames) {
299                    _headerNames = ListUtil.toList(StringUtil.split(headerNames));
300            }
301    
302            public void setId(String id) {
303                    _id = id;
304            }
305    
306            public void setIteratorURL(PortletURL iteratorURL) {
307                    _iteratorURL = iteratorURL;
308            }
309    
310            public void setOrderByCol(String orderByCol) {
311                    _orderByCol = orderByCol;
312            }
313    
314            public void setOrderByColParam(String orderByColParam) {
315                    _orderByColParam = orderByColParam;
316            }
317    
318            public void setOrderByComparator(OrderByComparator<R> orderByComparator) {
319                    _orderByComparator = orderByComparator;
320            }
321    
322            public void setOrderByType(String orderByType) {
323                    _orderByType = orderByType;
324            }
325    
326            public void setOrderByTypeParam(String orderByTypeParam) {
327                    _orderByTypeParam = orderByTypeParam;
328            }
329    
330            public void setRowChecker(RowChecker rowChecker) {
331                    _rowChecker = rowChecker;
332            }
333    
334            public void setSearchContainer(SearchContainer<R> searchContainer) {
335                    _searchContainer = searchContainer;
336            }
337    
338            public void setSearchTerms(DisplayTerms searchTerms) {
339                    _searchTerms = searchTerms;
340            }
341    
342            public void setTotal(int total) {
343                    _total = total;
344            }
345    
346            public void setTotalVar(String totalVar) {
347                    _totalVar = totalVar;
348            }
349    
350            public void setVar(String var) {
351                    _var = var;
352            }
353    
354            private String _cssClass = StringPool.BLANK;
355            private String _curParam = SearchContainer.DEFAULT_CUR_PARAM;
356            private int _delta = SearchContainer.DEFAULT_DELTA;
357            private boolean _deltaConfigurable =
358                    SearchContainer.DEFAULT_DELTA_CONFIGURABLE;
359            private String _deltaParam = SearchContainer.DEFAULT_DELTA_PARAM;
360            private DisplayTerms _displayTerms;
361            private String _emptyResultsMessage;
362            private List<String> _headerNames;
363            private String _id;
364            private PortletURL _iteratorURL;
365            private String _orderByCol;
366            private String _orderByColParam =
367                    SearchContainer.DEFAULT_ORDER_BY_COL_PARAM;
368            private OrderByComparator<R> _orderByComparator;
369            private String _orderByType;
370            private String _orderByTypeParam =
371                    SearchContainer.DEFAULT_ORDER_BY_TYPE_PARAM;
372            private RowChecker _rowChecker;
373            private SearchContainer<R> _searchContainer;
374            private DisplayTerms _searchTerms;
375            private int _total;
376            private String _totalVar = SearchContainer.DEFAULT_TOTAL_VAR;
377            private String _var = SearchContainer.DEFAULT_VAR;
378    
379    }