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     * @author Roberto D??az
030     */
031    public class SearchContainerResultsTag<R> extends TagSupport {
032    
033            /**
034             * @deprecated As of 6.2.0, replaced by {@link
035             *             SearchContainer#DEFAULT_RESULTS_VAR}.
036             */
037            public static final String DEFAULT_RESULTS_VAR =
038                    SearchContainer.DEFAULT_RESULTS_VAR;
039    
040            /**
041             * @deprecated As of 6.2.0, replaced by {@link
042             *             SearchContainer#DEFAULT_DEPRECATED_TOTAL_VAR}.
043             */
044            public static final String DEFAULT_TOTAL_VAR =
045                    SearchContainer.DEFAULT_DEPRECATED_TOTAL_VAR;
046    
047            @Override
048            public int doEndTag() throws JspException {
049                    try {
050                            SearchContainerTag<R> searchContainerTag =
051                                    (SearchContainerTag<R>)findAncestorWithClass(
052                                            this, SearchContainerTag.class);
053    
054                            SearchContainer<R> searchContainer =
055                                    searchContainerTag.getSearchContainer();
056    
057                            int total = searchContainer.getTotal();
058    
059                            if (_deprecatedTotal == 0) {
060                                    _deprecatedTotal = total;
061                            }
062    
063                            String totalVar = searchContainer.getTotalVar();
064    
065                            if (!_deprecatedTotalVar.equals(
066                                            SearchContainer.DEFAULT_DEPRECATED_TOTAL_VAR) &&
067                                    totalVar.equals(SearchContainer.DEFAULT_TOTAL_VAR)) {
068    
069                                    pageContext.removeAttribute(totalVar);
070    
071                                    searchContainer.setTotalVar(_deprecatedTotalVar);
072                            }
073                            else {
074                                    pageContext.removeAttribute(_deprecatedTotalVar);
075    
076                                    _deprecatedTotalVar = totalVar;
077                            }
078    
079                            if (_results == null) {
080                                    _results = searchContainer.getResults();
081    
082                                    if (_results.isEmpty()) {
083                                            _results = (List<R>)pageContext.getAttribute(_resultsVar);
084                                    }
085    
086                                    _deprecatedTotal = (Integer)pageContext.getAttribute(
087                                            _deprecatedTotalVar);
088                            }
089    
090                            if (_results != null) {
091                                    if (_deprecatedTotal < _results.size()) {
092                                            _deprecatedTotal = _results.size();
093                                    }
094                            }
095    
096                            searchContainer.setResults(_results);
097    
098                            if (total == 0) {
099                                    searchContainer.setTotal(_deprecatedTotal);
100                            }
101    
102                            searchContainerTag.setHasResults(true);
103    
104                            pageContext.setAttribute(_resultsVar, _results);
105    
106                            return EVAL_PAGE;
107                    }
108                    catch (Exception e) {
109                            throw new JspException(e);
110                    }
111                    finally {
112                            if (!ServerDetector.isResin()) {
113                                    _deprecatedTotal = 0;
114                                    _deprecatedTotalVar =
115                                            SearchContainer.DEFAULT_DEPRECATED_TOTAL_VAR;
116                                    _results = null;
117                                    _resultsVar = SearchContainer.DEFAULT_RESULTS_VAR;
118                            }
119                    }
120            }
121    
122            @Override
123            public int doStartTag() throws JspException {
124                    SearchContainerTag<R> searchContainerTag =
125                            (SearchContainerTag<R>)findAncestorWithClass(
126                                    this, SearchContainerTag.class);
127    
128                    if (searchContainerTag == null) {
129                            throw new JspTagException("Requires liferay-ui:search-container");
130                    }
131    
132                    if (_results == null) {
133                            pageContext.setAttribute(_deprecatedTotalVar, 0);
134                            pageContext.setAttribute(_resultsVar, new ArrayList<R>());
135                    }
136    
137                    return EVAL_BODY_INCLUDE;
138            }
139    
140            public List<R> getResults() {
141                    return _results;
142            }
143    
144            public String getResultsVar() {
145                    return _resultsVar;
146            }
147    
148            /**
149             * @deprecated As of 6.2.0, replaced by {@link
150             *             SearchContainerTag#getTotal()}.
151             */
152            public int getTotal() {
153                    return _deprecatedTotal;
154            }
155    
156            /**
157             * @deprecated As of 6.2.0, replaced by {@link
158             *             SearchContainerTag#getTotalVar()}.
159             */
160            public String getTotalVar() {
161                    return _deprecatedTotalVar;
162            }
163    
164            public void setResults(List<R> results) {
165                    _results = results;
166            }
167    
168            public void setResultsVar(String resultsVar) {
169                    _resultsVar = resultsVar;
170            }
171    
172            /**
173             * @deprecated As of 6.2.0, replaced by {@link
174             *             SearchContainerTag#setTotal(int)}.
175             */
176            public void setTotal(int deprecatedTotal) {
177                    _deprecatedTotal = deprecatedTotal;
178            }
179    
180            /**
181             * @deprecated As of 6.2.0, replaced by {@link
182             *             SearchContainerTag#setTotalVar(String)}.
183             */
184            public void setTotalVar(String deprecatedTotalVar) {
185                    _deprecatedTotalVar = deprecatedTotalVar;
186            }
187    
188            private int _deprecatedTotal;
189            private String _deprecatedTotalVar =
190                    SearchContainer.DEFAULT_DEPRECATED_TOTAL_VAR;
191            private List<R> _results;
192            private String _resultsVar = SearchContainer.DEFAULT_RESULTS_VAR;
193    
194    }