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