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                            pageContext.setAttribute(_var, _searchContainer);
124    
125                            return EVAL_BODY_INCLUDE;
126                    }
127                    catch (Exception e) {
128                            throw new JspException(e);
129                    }
130            }
131    
132            public String getCurParam() {
133                    return _curParam;
134            }
135    
136            public int getDelta() {
137                    return _delta;
138            }
139    
140            public String getDeltaParam() {
141                    return _deltaParam;
142            }
143    
144            public DisplayTerms getDisplayTerms() {
145                    return _displayTerms;
146            }
147    
148            public String getEmptyResultsMessage() {
149                    return _emptyResultsMessage;
150            }
151    
152            public PortletURL getIteratorURL() {
153                    return _iteratorURL;
154            }
155    
156            public String getOrderByCol() {
157                    return _orderByCol;
158            }
159    
160            public String getOrderByColParam() {
161                    return _orderByColParam;
162            }
163    
164            public OrderByComparator getOrderByComparator() {
165                    return _orderByComparator;
166            }
167    
168            public String getOrderByType() {
169                    return _orderByType;
170            }
171    
172            public String getOrderByTypeParam() {
173                    return _orderByTypeParam;
174            }
175    
176            public RowChecker getRowChecker() {
177                    return _rowChecker;
178            }
179    
180            public SearchContainer<R> getSearchContainer() {
181                    return _searchContainer;
182            }
183    
184            public DisplayTerms getSearchTerms() {
185                    return _searchTerms;
186            }
187    
188            public String getVar() {
189                    return _var;
190            }
191    
192            public boolean isDeltaConfigurable() {
193                    return _deltaConfigurable;
194            }
195    
196            public boolean isHasResults() {
197                    return _hasResults;
198            }
199    
200            public boolean isHover() {
201                    return _hover;
202            }
203    
204            public void setCurParam(String curParam) {
205                    _curParam = curParam;
206            }
207    
208            public void setDelta(int delta) {
209                    _delta = delta;
210            }
211    
212            public void setDeltaConfigurable(boolean deltaConfigurable) {
213                    _deltaConfigurable = deltaConfigurable;
214            }
215    
216            public void setDeltaParam(String deltaParam) {
217                    _deltaParam = deltaParam;
218            }
219    
220            public void setDisplayTerms(DisplayTerms displayTerms) {
221                    _displayTerms = displayTerms;
222            }
223    
224            public void setEmptyResultsMessage(String emptyResultsMessage) {
225                    _emptyResultsMessage = emptyResultsMessage;
226            }
227    
228            public void setHasResults(boolean hasResults) {
229                    _hasResults = hasResults;
230            }
231    
232            public void setHeaderNames(String headerNames) {
233                    _headerNames = ListUtil.toList(StringUtil.split(headerNames));
234            }
235    
236            public void setHover(boolean hover) {
237                    _hover = hover;
238            }
239    
240            public void setId(String id) {
241                    _id = id;
242            }
243    
244            public void setIteratorURL(PortletURL iteratorURL) {
245                    _iteratorURL = iteratorURL;
246            }
247    
248            public void setOrderByCol(String orderByCol) {
249                    _orderByCol = orderByCol;
250            }
251    
252            public void setOrderByColParam(String orderByColParam) {
253                    _orderByColParam = orderByColParam;
254            }
255    
256            public void setOrderByComparator(OrderByComparator orderByComparator) {
257                    _orderByComparator = orderByComparator;
258            }
259    
260            public void setOrderByType(String orderByType) {
261                    _orderByType = orderByType;
262            }
263    
264            public void setOrderByTypeParam(String orderByTypeParam) {
265                    _orderByTypeParam = orderByTypeParam;
266            }
267    
268            public void setRowChecker(RowChecker rowChecker) {
269                    _rowChecker = rowChecker;
270            }
271    
272            public void setSearchContainer(SearchContainer<R> searchContainer) {
273                    _searchContainer = searchContainer;
274            }
275    
276            public void setSearchTerms(DisplayTerms searchTerms) {
277                    _searchTerms = searchTerms;
278            }
279    
280            public void setVar(String var) {
281                    _var = var;
282            }
283    
284            private String _curParam = SearchContainer.DEFAULT_CUR_PARAM;
285            private int _delta = SearchContainer.DEFAULT_DELTA;
286            private boolean _deltaConfigurable =
287                    SearchContainer.DEFAULT_DELTA_CONFIGURABLE;
288            private String _deltaParam = SearchContainer.DEFAULT_DELTA_PARAM;
289            private DisplayTerms _displayTerms;
290            private String _emptyResultsMessage;
291            private boolean _hasResults;
292            private List<String> _headerNames;
293            private boolean _hover = true;
294            private String _id;
295            private PortletURL _iteratorURL;
296            private String _orderByCol;
297            private String _orderByColParam =
298                    SearchContainer.DEFAULT_ORDER_BY_COL_PARAM;
299            private OrderByComparator _orderByComparator;
300            private String _orderByType;
301            private String _orderByTypeParam =
302                    SearchContainer.DEFAULT_ORDER_BY_TYPE_PARAM;
303            private RowChecker _rowChecker;
304            private SearchContainer<R> _searchContainer;
305            private DisplayTerms _searchTerms;
306            private String _var = DEFAULT_VAR;
307    
308    }