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     */
040    public class SearchContainerTag<R> extends ParamAndPropertyAncestorTagImpl {
041    
042            @Override
043            public int doEndTag() {
044                    pageContext.setAttribute(
045                            _searchContainer.getTotalVar(), _searchContainer.getTotal());
046    
047                    _curParam = SearchContainer.DEFAULT_CUR_PARAM;
048                    _delta = SearchContainer.DEFAULT_DELTA;
049                    _deltaConfigurable = SearchContainer.DEFAULT_DELTA_CONFIGURABLE;
050                    _deltaParam = SearchContainer.DEFAULT_DELTA_PARAM;
051                    _displayTerms = null;
052                    _emptyResultsMessage = null;
053                    _hasResults = false;
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(_searchContainer.getTotalVar(), 0);
140                            pageContext.setAttribute(_var, _searchContainer);
141    
142                            SearchContainerReference searchContainerReference =
143                                    (SearchContainerReference)pageContext.getAttribute(
144                                            "searchContainerReference");
145    
146                            if ((searchContainerReference != null) &&
147                                    !_var.equals(SearchContainer.DEFAULT_VAR)) {
148    
149                                    searchContainerReference.register(_var, _searchContainer);
150                            }
151    
152                            return EVAL_BODY_INCLUDE;
153                    }
154                    catch (Exception e) {
155                            throw new JspException(e);
156                    }
157            }
158    
159            public String getCurParam() {
160                    return _curParam;
161            }
162    
163            public int getDelta() {
164                    return _delta;
165            }
166    
167            public String getDeltaParam() {
168                    return _deltaParam;
169            }
170    
171            public DisplayTerms getDisplayTerms() {
172                    return _displayTerms;
173            }
174    
175            public String getEmptyResultsMessage() {
176                    return _emptyResultsMessage;
177            }
178    
179            public PortletURL getIteratorURL() {
180                    return _iteratorURL;
181            }
182    
183            public String getOrderByCol() {
184                    return _orderByCol;
185            }
186    
187            public String getOrderByColParam() {
188                    return _orderByColParam;
189            }
190    
191            public OrderByComparator getOrderByComparator() {
192                    return _orderByComparator;
193            }
194    
195            public String getOrderByType() {
196                    return _orderByType;
197            }
198    
199            public String getOrderByTypeParam() {
200                    return _orderByTypeParam;
201            }
202    
203            public RowChecker getRowChecker() {
204                    return _rowChecker;
205            }
206    
207            public SearchContainer<R> getSearchContainer() {
208                    return _searchContainer;
209            }
210    
211            public DisplayTerms getSearchTerms() {
212                    return _searchTerms;
213            }
214    
215            public int getTotal() {
216                    return _total;
217            }
218    
219            public String getTotalVar() {
220                    return _totalVar;
221            }
222    
223            public String getVar() {
224                    return _var;
225            }
226    
227            public boolean isDeltaConfigurable() {
228                    return _deltaConfigurable;
229            }
230    
231            public boolean isHasResults() {
232                    return _hasResults;
233            }
234    
235            public boolean isHover() {
236                    return _hover;
237            }
238    
239            public void setCurParam(String curParam) {
240                    _curParam = curParam;
241            }
242    
243            public void setDelta(int delta) {
244                    _delta = delta;
245            }
246    
247            public void setDeltaConfigurable(boolean deltaConfigurable) {
248                    _deltaConfigurable = deltaConfigurable;
249            }
250    
251            public void setDeltaParam(String deltaParam) {
252                    _deltaParam = deltaParam;
253            }
254    
255            public void setDisplayTerms(DisplayTerms displayTerms) {
256                    _displayTerms = displayTerms;
257            }
258    
259            public void setEmptyResultsMessage(String emptyResultsMessage) {
260                    _emptyResultsMessage = emptyResultsMessage;
261            }
262    
263            public void setHasResults(boolean hasResults) {
264                    _hasResults = hasResults;
265            }
266    
267            public void setHeaderNames(String headerNames) {
268                    _headerNames = ListUtil.toList(StringUtil.split(headerNames));
269            }
270    
271            public void setHover(boolean hover) {
272                    _hover = hover;
273            }
274    
275            public void setId(String id) {
276                    _id = id;
277            }
278    
279            public void setIteratorURL(PortletURL iteratorURL) {
280                    _iteratorURL = iteratorURL;
281            }
282    
283            public void setOrderByCol(String orderByCol) {
284                    _orderByCol = orderByCol;
285            }
286    
287            public void setOrderByColParam(String orderByColParam) {
288                    _orderByColParam = orderByColParam;
289            }
290    
291            public void setOrderByComparator(OrderByComparator orderByComparator) {
292                    _orderByComparator = orderByComparator;
293            }
294    
295            public void setOrderByType(String orderByType) {
296                    _orderByType = orderByType;
297            }
298    
299            public void setOrderByTypeParam(String orderByTypeParam) {
300                    _orderByTypeParam = orderByTypeParam;
301            }
302    
303            public void setRowChecker(RowChecker rowChecker) {
304                    _rowChecker = rowChecker;
305            }
306    
307            public void setSearchContainer(SearchContainer<R> searchContainer) {
308                    _searchContainer = searchContainer;
309            }
310    
311            public void setSearchTerms(DisplayTerms searchTerms) {
312                    _searchTerms = searchTerms;
313            }
314    
315            public void setTotal(int total) {
316                    _total = total;
317            }
318    
319            public void setTotalVar(String totalVar) {
320                    _totalVar = totalVar;
321            }
322    
323            public void setVar(String var) {
324                    _var = var;
325            }
326    
327            private String _curParam = SearchContainer.DEFAULT_CUR_PARAM;
328            private int _delta = SearchContainer.DEFAULT_DELTA;
329            private boolean _deltaConfigurable =
330                    SearchContainer.DEFAULT_DELTA_CONFIGURABLE;
331            private String _deltaParam = SearchContainer.DEFAULT_DELTA_PARAM;
332            private DisplayTerms _displayTerms;
333            private String _emptyResultsMessage;
334            private boolean _hasResults;
335            private List<String> _headerNames;
336            private boolean _hover = true;
337            private String _id;
338            private PortletURL _iteratorURL;
339            private String _orderByCol;
340            private String _orderByColParam =
341                    SearchContainer.DEFAULT_ORDER_BY_COL_PARAM;
342            private OrderByComparator _orderByComparator;
343            private String _orderByType;
344            private String _orderByTypeParam =
345                    SearchContainer.DEFAULT_ORDER_BY_TYPE_PARAM;
346            private RowChecker _rowChecker;
347            private SearchContainer<R> _searchContainer;
348            private DisplayTerms _searchTerms;
349            private int _total;
350            private String _totalVar = SearchContainer.DEFAULT_TOTAL_VAR;
351            private String _var = SearchContainer.DEFAULT_VAR;
352    
353    }