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.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                            SearchContainerTag<R> searchContainerTag =
040                                    (SearchContainerTag<R>)findAncestorWithClass(
041                                            this, SearchContainerTag.class);
042    
043                            SearchContainer<R> searchContainer =
044                                    searchContainerTag.getSearchContainer();
045    
046                            int total = searchContainer.getTotal();
047    
048                            if (_total == 0) {
049                                    _total = total;
050                            }
051    
052                            if (_results == null) {
053                                    _results = (List<R>)pageContext.getAttribute(_resultsVar);
054                                    _total = (Integer)pageContext.getAttribute(_totalVar);
055                            }
056    
057                            if (_results != null) {
058                                    if (_total < _results.size()) {
059                                            _total = _results.size();
060                                    }
061                            }
062    
063                            searchContainer.setResults(_results);
064    
065                            if (total == 0) {
066                                    searchContainer.setTotal(_total);
067                            }
068    
069                            searchContainerTag.setHasResults(true);
070    
071                            pageContext.setAttribute(_resultsVar, _results);
072                            pageContext.setAttribute(_totalVar, _total);
073    
074                            return EVAL_PAGE;
075                    }
076                    catch (Exception e) {
077                            throw new JspException(e);
078                    }
079                    finally {
080                            if (!ServerDetector.isResin()) {
081                                    _results = null;
082                                    _resultsVar = DEFAULT_RESULTS_VAR;
083                                    _total = 0;
084                                    _totalVar = DEFAULT_TOTAL_VAR;
085                            }
086                    }
087            }
088    
089            @Override
090            public int doStartTag() throws JspException {
091                    SearchContainerTag<R> searchContainerTag =
092                            (SearchContainerTag<R>)findAncestorWithClass(
093                                    this, SearchContainerTag.class);
094    
095                    if (searchContainerTag == null) {
096                            throw new JspTagException("Requires liferay-ui:search-container");
097                    }
098    
099                    if (_results == null) {
100                            pageContext.setAttribute(_resultsVar, new ArrayList<R>());
101                            pageContext.setAttribute(_totalVar, 0);
102                    }
103    
104                    return EVAL_BODY_INCLUDE;
105            }
106    
107            public List<R> getResults() {
108                    return _results;
109            }
110    
111            public String getResultsVar() {
112                    return _resultsVar;
113            }
114    
115            public int getTotal() {
116                    return _total;
117            }
118    
119            public String getTotalVar() {
120                    return _totalVar;
121            }
122    
123            public void setResults(List<R> results) {
124                    _results = results;
125            }
126    
127            public void setResultsVar(String resultsVar) {
128                    _resultsVar = resultsVar;
129            }
130    
131            public void setTotal(int total) {
132                    _total = total;
133            }
134    
135            public void setTotalVar(String totalVar) {
136                    _totalVar = totalVar;
137            }
138    
139            private List<R> _results;
140            private String _resultsVar = DEFAULT_RESULTS_VAR;
141            private int _total;
142            private String _totalVar = DEFAULT_TOTAL_VAR;
143    
144    }