001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 extends TagSupport {
031    
032            public static final String DEFAULT_RESULTS_VAR = "results";
033    
034            public static final String DEFAULT_TOTAL_VAR = "total";
035    
036            public int doEndTag() throws JspException {
037                    try {
038                            if (_results == null) {
039                                    _results = (List)pageContext.getAttribute(_resultsVar);
040                                    _total = (Integer)pageContext.getAttribute(_totalVar);
041                            }
042    
043                            if (_results != null) {
044                                    if (_total < _results.size()) {
045                                            _total = _results.size();
046                                    }
047                            }
048    
049                            SearchContainerTag parentTag =
050                                    (SearchContainerTag)findAncestorWithClass(
051                                            this, SearchContainerTag.class);
052    
053                            SearchContainer searchContainer = parentTag.getSearchContainer();
054    
055                            searchContainer.setResults(_results);
056                            searchContainer.setTotal(_total);
057    
058                            parentTag.setHasResults(true);
059    
060                            pageContext.setAttribute(_resultsVar, _results);
061                            pageContext.setAttribute(_totalVar, _total);
062    
063                            return EVAL_PAGE;
064                    }
065                    catch (Exception e) {
066                            throw new JspException(e);
067                    }
068                    finally {
069                            if (!ServerDetector.isResin()) {
070                                    _results = null;
071                                    _resultsVar = DEFAULT_RESULTS_VAR;
072                                    _total = 0;
073                                    _totalVar = DEFAULT_TOTAL_VAR;
074                            }
075                    }
076            }
077    
078            public int doStartTag() throws JspException {
079                    SearchContainerTag parentTag =
080                            (SearchContainerTag)findAncestorWithClass(
081                                    this, SearchContainerTag.class);
082    
083                    if (parentTag == null) {
084                            throw new JspTagException("Requires liferay-ui:search-container");
085                    }
086    
087                    if (_results == null) {
088                            pageContext.setAttribute(_resultsVar, new ArrayList());
089                            pageContext.setAttribute(_totalVar, 0);
090                    }
091    
092                    return EVAL_BODY_INCLUDE;
093            }
094    
095            public List getResults() {
096                    return _results;
097            }
098    
099            public String getResultsVar() {
100                    return _resultsVar;
101            }
102    
103            public int getTotal() {
104                    return _total;
105            }
106    
107            public String getTotalVar() {
108                    return _totalVar;
109            }
110    
111            public void setResults(List results) {
112                    _results = results;
113            }
114    
115            public void setResultsVar(String resultsVar) {
116                    _resultsVar = resultsVar;
117            }
118    
119            public void setTotal(int total) {
120                    _total = total;
121            }
122    
123            public void setTotalVar(String totalVar) {
124                    _totalVar = totalVar;
125            }
126    
127            private List _results;
128            private String _resultsVar = DEFAULT_RESULTS_VAR;
129            private int _total;
130            private String _totalVar = DEFAULT_TOTAL_VAR;
131    
132    }