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                    _hover = false;
059                    _id = null;
060                    _iteratorURL = null;
061                    _orderByCol = null;
062                    _orderByColParam = SearchContainer.DEFAULT_ORDER_BY_COL_PARAM;
063                    _orderByComparator = null;
064                    _orderByType = null;
065                    _orderByTypeParam = SearchContainer.DEFAULT_ORDER_BY_TYPE_PARAM;
066                    _rowChecker = null;
067                    _searchContainer = null;
068                    _searchTerms = null;
069                    _total = 0;
070                    _totalVar = SearchContainer.DEFAULT_TOTAL_VAR;
071                    _var = SearchContainer.DEFAULT_VAR;
072    
073                    return EVAL_PAGE;
074            }
075    
076            @Override
077            public int doStartTag() throws JspException {
078                    try {
079                            PortletRequest portletRequest =
080                                    (PortletRequest)request.getAttribute(
081                                            JavaConstants.JAVAX_PORTLET_REQUEST);
082                            PortletResponse portletResponse =
083                                    (PortletResponse)request.getAttribute(
084                                            JavaConstants.JAVAX_PORTLET_RESPONSE);
085    
086                            if (_iteratorURL == null) {
087                                    _iteratorURL =
088                                            ((MimeResponse)portletResponse).createRenderURL();
089                            }
090    
091                            if (_searchContainer == null) {
092                                    _searchContainer = new SearchContainer<R>(
093                                            portletRequest, _displayTerms, _searchTerms, getCurParam(),
094                                            getDelta(), _iteratorURL, null, _emptyResultsMessage);
095                            }
096    
097                            _searchContainer.setDeltaConfigurable(_deltaConfigurable);
098    
099                            if (Validator.isNotNull(_emptyResultsMessage)) {
100                                    _searchContainer.setEmptyResultsMessage(_emptyResultsMessage);
101                            }
102    
103                            if (_headerNames != null) {
104                                    _searchContainer.setHeaderNames(_headerNames);
105                            }
106    
107                            _searchContainer.setHover(_hover);
108                            _searchContainer.setId(_id);
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 boolean isHover() {
264                    return _hover;
265            }
266    
267            public void setCssClass(String cssClass) {
268                    _cssClass = cssClass;
269            }
270    
271            public void setCurParam(String curParam) {
272                    _curParam = curParam;
273            }
274    
275            public void setDelta(int delta) {
276                    _delta = delta;
277            }
278    
279            public void setDeltaConfigurable(boolean deltaConfigurable) {
280                    _deltaConfigurable = deltaConfigurable;
281            }
282    
283            public void setDeltaParam(String deltaParam) {
284                    _deltaParam = deltaParam;
285            }
286    
287            public void setDisplayTerms(DisplayTerms displayTerms) {
288                    _displayTerms = displayTerms;
289            }
290    
291            public void setEmptyResultsMessage(String emptyResultsMessage) {
292                    _emptyResultsMessage = emptyResultsMessage;
293            }
294    
295            /**
296             * @deprecated As of 6.2.0, see LPS-41307
297             */
298            @Deprecated
299            public void setHasResults(boolean hasResults) {
300            }
301    
302            public void setHeaderNames(String headerNames) {
303                    _headerNames = ListUtil.toList(StringUtil.split(headerNames));
304            }
305    
306            public void setHover(boolean hover) {
307                    _hover = hover;
308            }
309    
310            public void setId(String id) {
311                    _id = id;
312            }
313    
314            public void setIteratorURL(PortletURL iteratorURL) {
315                    _iteratorURL = iteratorURL;
316            }
317    
318            public void setOrderByCol(String orderByCol) {
319                    _orderByCol = orderByCol;
320            }
321    
322            public void setOrderByColParam(String orderByColParam) {
323                    _orderByColParam = orderByColParam;
324            }
325    
326            public void setOrderByComparator(OrderByComparator<R> orderByComparator) {
327                    _orderByComparator = orderByComparator;
328            }
329    
330            public void setOrderByType(String orderByType) {
331                    _orderByType = orderByType;
332            }
333    
334            public void setOrderByTypeParam(String orderByTypeParam) {
335                    _orderByTypeParam = orderByTypeParam;
336            }
337    
338            public void setRowChecker(RowChecker rowChecker) {
339                    _rowChecker = rowChecker;
340            }
341    
342            public void setSearchContainer(SearchContainer<R> searchContainer) {
343                    _searchContainer = searchContainer;
344            }
345    
346            public void setSearchTerms(DisplayTerms searchTerms) {
347                    _searchTerms = searchTerms;
348            }
349    
350            public void setTotal(int total) {
351                    _total = total;
352            }
353    
354            public void setTotalVar(String totalVar) {
355                    _totalVar = totalVar;
356            }
357    
358            public void setVar(String var) {
359                    _var = var;
360            }
361    
362            private String _cssClass = StringPool.BLANK;
363            private String _curParam = SearchContainer.DEFAULT_CUR_PARAM;
364            private int _delta = SearchContainer.DEFAULT_DELTA;
365            private boolean _deltaConfigurable =
366                    SearchContainer.DEFAULT_DELTA_CONFIGURABLE;
367            private String _deltaParam = SearchContainer.DEFAULT_DELTA_PARAM;
368            private DisplayTerms _displayTerms;
369            private String _emptyResultsMessage;
370            private List<String> _headerNames;
371            private boolean _hover = true;
372            private String _id;
373            private PortletURL _iteratorURL;
374            private String _orderByCol;
375            private String _orderByColParam =
376                    SearchContainer.DEFAULT_ORDER_BY_COL_PARAM;
377            private OrderByComparator<R> _orderByComparator;
378            private String _orderByType;
379            private String _orderByTypeParam =
380                    SearchContainer.DEFAULT_ORDER_BY_TYPE_PARAM;
381            private RowChecker _rowChecker;
382            private SearchContainer<R> _searchContainer;
383            private DisplayTerms _searchTerms;
384            private int _total;
385            private String _totalVar = SearchContainer.DEFAULT_TOTAL_VAR;
386            private String _var = SearchContainer.DEFAULT_VAR;
387    
388    }