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                            _searchContainer.setId(_id);
107    
108                            if (Validator.isNotNull(_orderByColParam)) {
109                                    _searchContainer.setOrderByColParam(_orderByColParam);
110                            }
111    
112                            if (Validator.isNotNull(_orderByCol)) {
113                                    _searchContainer.setOrderByCol(_orderByCol);
114                            }
115                            else {
116                                    String orderByCol = ParamUtil.getString(
117                                            request, _searchContainer.getOrderByColParam(), null);
118    
119                                    if (orderByCol != null) {
120                                            _searchContainer.setOrderByCol(orderByCol);
121                                    }
122                            }
123    
124                            if (_orderByComparator != null) {
125                                    _searchContainer.setOrderByComparator(_orderByComparator);
126                            }
127    
128                            if (Validator.isNotNull(_orderByTypeParam)) {
129                                    _searchContainer.setOrderByTypeParam(_orderByTypeParam);
130                            }
131    
132                            if (Validator.isNotNull(_orderByType)) {
133                                    _searchContainer.setOrderByType(_orderByType);
134                            }
135                            else {
136                                    String orderByType = ParamUtil.getString(
137                                            request, _searchContainer.getOrderByTypeParam(), null);
138    
139                                    if (orderByType != null) {
140                                            _searchContainer.setOrderByType(orderByType);
141                                    }
142                            }
143    
144                            if (_rowChecker != null) {
145                                    _searchContainer.setRowChecker(_rowChecker);
146                            }
147    
148                            if (_total != 0) {
149                                    _searchContainer.setTotal(_total);
150                            }
151    
152                            if (Validator.isNotNull(_totalVar)) {
153                                    _searchContainer.setTotalVar(_totalVar);
154                            }
155    
156                            pageContext.setAttribute(
157                                    _searchContainer.getTotalVar(), _searchContainer.getTotal());
158                            pageContext.setAttribute(_var, _searchContainer);
159    
160                            SearchContainerReference searchContainerReference =
161                                    (SearchContainerReference)pageContext.getAttribute(
162                                            "searchContainerReference");
163    
164                            if ((searchContainerReference != null) &&
165                                    !_var.equals(SearchContainer.DEFAULT_VAR)) {
166    
167                                    searchContainerReference.register(_var, _searchContainer);
168                            }
169    
170                            return EVAL_BODY_INCLUDE;
171                    }
172                    catch (Exception e) {
173                            throw new JspException(e);
174                    }
175            }
176    
177            public String getCssClass() {
178                    return _cssClass;
179            }
180    
181            public String getCurParam() {
182                    return _curParam;
183            }
184    
185            public int getDelta() {
186                    return _delta;
187            }
188    
189            public String getDeltaParam() {
190                    return _deltaParam;
191            }
192    
193            public DisplayTerms getDisplayTerms() {
194                    return _displayTerms;
195            }
196    
197            public String getEmptyResultsMessage() {
198                    return _emptyResultsMessage;
199            }
200    
201            public PortletURL getIteratorURL() {
202                    return _iteratorURL;
203            }
204    
205            public String getOrderByCol() {
206                    return _orderByCol;
207            }
208    
209            public String getOrderByColParam() {
210                    return _orderByColParam;
211            }
212    
213            public OrderByComparator<R> getOrderByComparator() {
214                    return _orderByComparator;
215            }
216    
217            public String getOrderByType() {
218                    return _orderByType;
219            }
220    
221            public String getOrderByTypeParam() {
222                    return _orderByTypeParam;
223            }
224    
225            public RowChecker getRowChecker() {
226                    return _rowChecker;
227            }
228    
229            public SearchContainer<R> getSearchContainer() {
230                    return _searchContainer;
231            }
232    
233            public DisplayTerms getSearchTerms() {
234                    return _searchTerms;
235            }
236    
237            public int getTotal() {
238                    return _total;
239            }
240    
241            public String getTotalVar() {
242                    return _totalVar;
243            }
244    
245            public String getVar() {
246                    return _var;
247            }
248    
249            public boolean isDeltaConfigurable() {
250                    return _deltaConfigurable;
251            }
252    
253            /**
254             * @deprecated As of 6.2.0, with no direct replacement. See LPS-41307.
255             */
256            @Deprecated
257            public boolean isHasResults() {
258                    return true;
259            }
260    
261            public void setCssClass(String cssClass) {
262                    _cssClass = cssClass;
263            }
264    
265            public void setCurParam(String curParam) {
266                    _curParam = curParam;
267            }
268    
269            public void setDelta(int delta) {
270                    _delta = delta;
271            }
272    
273            public void setDeltaConfigurable(boolean deltaConfigurable) {
274                    _deltaConfigurable = deltaConfigurable;
275            }
276    
277            public void setDeltaParam(String deltaParam) {
278                    _deltaParam = deltaParam;
279            }
280    
281            public void setDisplayTerms(DisplayTerms displayTerms) {
282                    _displayTerms = displayTerms;
283            }
284    
285            public void setEmptyResultsMessage(String emptyResultsMessage) {
286                    _emptyResultsMessage = emptyResultsMessage;
287            }
288    
289            /**
290             * @deprecated As of 6.2.0, see LPS-41307
291             */
292            @Deprecated
293            public void setHasResults(boolean hasResults) {
294            }
295    
296            public void setHeaderNames(String headerNames) {
297                    _headerNames = ListUtil.toList(StringUtil.split(headerNames));
298            }
299    
300            public void setId(String id) {
301                    _id = id;
302            }
303    
304            public void setIteratorURL(PortletURL iteratorURL) {
305                    _iteratorURL = iteratorURL;
306            }
307    
308            public void setOrderByCol(String orderByCol) {
309                    _orderByCol = orderByCol;
310            }
311    
312            public void setOrderByColParam(String orderByColParam) {
313                    _orderByColParam = orderByColParam;
314            }
315    
316            public void setOrderByComparator(OrderByComparator<R> orderByComparator) {
317                    _orderByComparator = orderByComparator;
318            }
319    
320            public void setOrderByType(String orderByType) {
321                    _orderByType = orderByType;
322            }
323    
324            public void setOrderByTypeParam(String orderByTypeParam) {
325                    _orderByTypeParam = orderByTypeParam;
326            }
327    
328            public void setRowChecker(RowChecker rowChecker) {
329                    _rowChecker = rowChecker;
330            }
331    
332            public void setSearchContainer(SearchContainer<R> searchContainer) {
333                    _searchContainer = searchContainer;
334            }
335    
336            public void setSearchTerms(DisplayTerms searchTerms) {
337                    _searchTerms = searchTerms;
338            }
339    
340            public void setTotal(int total) {
341                    _total = total;
342            }
343    
344            public void setTotalVar(String totalVar) {
345                    _totalVar = totalVar;
346            }
347    
348            public void setVar(String var) {
349                    _var = var;
350            }
351    
352            private String _cssClass = StringPool.BLANK;
353            private String _curParam = SearchContainer.DEFAULT_CUR_PARAM;
354            private int _delta = SearchContainer.DEFAULT_DELTA;
355            private boolean _deltaConfigurable =
356                    SearchContainer.DEFAULT_DELTA_CONFIGURABLE;
357            private String _deltaParam = SearchContainer.DEFAULT_DELTA_PARAM;
358            private DisplayTerms _displayTerms;
359            private String _emptyResultsMessage;
360            private List<String> _headerNames;
361            private String _id;
362            private PortletURL _iteratorURL;
363            private String _orderByCol;
364            private String _orderByColParam =
365                    SearchContainer.DEFAULT_ORDER_BY_COL_PARAM;
366            private OrderByComparator<R> _orderByComparator;
367            private String _orderByType;
368            private String _orderByTypeParam =
369                    SearchContainer.DEFAULT_ORDER_BY_TYPE_PARAM;
370            private RowChecker _rowChecker;
371            private SearchContainer<R> _searchContainer;
372            private DisplayTerms _searchTerms;
373            private int _total;
374            private String _totalVar = SearchContainer.DEFAULT_TOTAL_VAR;
375            private String _var = SearchContainer.DEFAULT_VAR;
376    
377    }