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.SearchContainer;
018    import com.liferay.portal.kernel.util.ServerDetector;
019    
020    import java.util.ArrayList;
021    import java.util.List;
022    
023    import javax.servlet.jsp.JspException;
024    import javax.servlet.jsp.JspTagException;
025    import javax.servlet.jsp.tagext.TagSupport;
026    
027    /**
028     * @author Raymond Augé
029     */
030    public class SearchContainerResultsTag<R> extends TagSupport {
031    
032            public static final String DEFAULT_RESULTS_VAR = "results";
033    
034            public static final String DEFAULT_TOTAL_VAR = "total";
035    
036            @Override
037            public int doEndTag() throws JspException {
038                    try {
039                            if (_results == null) {
040                                    _results = (List<R>)pageContext.getAttribute(_resultsVar);
041                                    _total = (Integer)pageContext.getAttribute(_totalVar);
042                            }
043    
044                            if (_results != null) {
045                                    if (_total < _results.size()) {
046                                            _total = _results.size();
047                                    }
048                            }
049    
050                            SearchContainerTag<R> searchContainerTag =
051                                    (SearchContainerTag<R>)findAncestorWithClass(
052                                            this, SearchContainerTag.class);
053    
054                            SearchContainer<R> searchContainer =
055                                    searchContainerTag.getSearchContainer();
056    
057                            searchContainer.setResults(_results);
058                            searchContainer.setTotal(_total);
059    
060                            searchContainerTag.setHasResults(true);
061    
062                            pageContext.setAttribute(_resultsVar, _results);
063                            pageContext.setAttribute(_totalVar, _total);
064    
065                            return EVAL_PAGE;
066                    }
067                    catch (Exception e) {
068                            throw new JspException(e);
069                    }
070                    finally {
071                            if (!ServerDetector.isResin()) {
072                                    _results = null;
073                                    _resultsVar = DEFAULT_RESULTS_VAR;
074                                    _total = 0;
075                                    _totalVar = DEFAULT_TOTAL_VAR;
076                            }
077                    }
078            }
079    
080            @Override
081            public int doStartTag() throws JspException {
082                    SearchContainerTag<R> searchContainerTag =
083                            (SearchContainerTag<R>)findAncestorWithClass(
084                                    this, SearchContainerTag.class);
085    
086                    if (searchContainerTag == null) {
087                            throw new JspTagException("Requires liferay-ui:search-container");
088                    }
089    
090                    if (_results == null) {
091                            pageContext.setAttribute(_resultsVar, new ArrayList<R>());
092                            pageContext.setAttribute(_totalVar, 0);
093                    }
094    
095                    return EVAL_BODY_INCLUDE;
096            }
097    
098            public List<R> getResults() {
099                    return _results;
100            }
101    
102            public String getResultsVar() {
103                    return _resultsVar;
104            }
105    
106            public int getTotal() {
107                    return _total;
108            }
109    
110            public String getTotalVar() {
111                    return _totalVar;
112            }
113    
114            public void setResults(List<R> results) {
115                    _results = results;
116            }
117    
118            public void setResultsVar(String resultsVar) {
119                    _resultsVar = resultsVar;
120            }
121    
122            public void setTotal(int total) {
123                    _total = total;
124            }
125    
126            public void setTotalVar(String totalVar) {
127                    _totalVar = totalVar;
128            }
129    
130            private List<R> _results;
131            private String _resultsVar = DEFAULT_RESULTS_VAR;
132            private int _total;
133            private String _totalVar = DEFAULT_TOTAL_VAR;
134    
135    }