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