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