001    /**
002     * Copyright (c) 2000-present 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            @Deprecated
038            public static final String DEFAULT_RESULTS_VAR =
039                    SearchContainer.DEFAULT_RESULTS_VAR;
040    
041            /**
042             * @deprecated As of 6.2.0, replaced by {@link
043             *             SearchContainer#DEFAULT_DEPRECATED_TOTAL_VAR}.
044             */
045            @Deprecated
046            public static final String DEFAULT_TOTAL_VAR =
047                    SearchContainer.DEFAULT_DEPRECATED_TOTAL_VAR;
048    
049            @Override
050            public int doEndTag() throws JspException {
051                    try {
052                            SearchContainerTag<R> searchContainerTag =
053                                    (SearchContainerTag<R>)findAncestorWithClass(
054                                            this, SearchContainerTag.class);
055    
056                            SearchContainer<R> searchContainer =
057                                    searchContainerTag.getSearchContainer();
058    
059                            int total = searchContainer.getTotal();
060    
061                            if (_deprecatedTotal == 0) {
062                                    _deprecatedTotal = total;
063                            }
064    
065                            String totalVar = searchContainer.getTotalVar();
066    
067                            if (!_deprecatedTotalVar.equals(
068                                            SearchContainer.DEFAULT_DEPRECATED_TOTAL_VAR) &&
069                                    totalVar.equals(SearchContainer.DEFAULT_TOTAL_VAR)) {
070    
071                                    pageContext.removeAttribute(totalVar);
072    
073                                    searchContainer.setTotalVar(_deprecatedTotalVar);
074                            }
075                            else {
076                                    pageContext.removeAttribute(_deprecatedTotalVar);
077    
078                                    _deprecatedTotalVar = totalVar;
079                            }
080    
081                            if (_results == null) {
082                                    _results = searchContainer.getResults();
083    
084                                    if (_results.isEmpty()) {
085                                            _results = (List<R>)pageContext.getAttribute(_resultsVar);
086                                    }
087    
088                                    _deprecatedTotal = (Integer)pageContext.getAttribute(
089                                            _deprecatedTotalVar);
090                            }
091    
092                            if (_results != null) {
093                                    if (_deprecatedTotal < _results.size()) {
094                                            _deprecatedTotal = _results.size();
095                                    }
096                            }
097    
098                            searchContainer.setResults(_results);
099    
100                            if (total == 0) {
101                                    searchContainer.setTotal(_deprecatedTotal);
102                            }
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            @Deprecated
153            public int getTotal() {
154                    return _deprecatedTotal;
155            }
156    
157            /**
158             * @deprecated As of 6.2.0, replaced by {@link
159             *             SearchContainerTag#getTotalVar()}.
160             */
161            @Deprecated
162            public String getTotalVar() {
163                    return _deprecatedTotalVar;
164            }
165    
166            public void setResults(List<R> results) {
167                    _results = results;
168            }
169    
170            public void setResultsVar(String resultsVar) {
171                    _resultsVar = resultsVar;
172            }
173    
174            /**
175             * @deprecated As of 6.2.0, replaced by {@link
176             *             SearchContainerTag#setTotal(int)}.
177             */
178            @Deprecated
179            public void setTotal(int deprecatedTotal) {
180                    _deprecatedTotal = deprecatedTotal;
181            }
182    
183            /**
184             * @deprecated As of 6.2.0, replaced by {@link
185             *             SearchContainerTag#setTotalVar(String)}.
186             */
187            @Deprecated
188            public void setTotalVar(String deprecatedTotalVar) {
189                    _deprecatedTotalVar = deprecatedTotalVar;
190            }
191    
192            private int _deprecatedTotal;
193            private String _deprecatedTotalVar =
194                    SearchContainer.DEFAULT_DEPRECATED_TOTAL_VAR;
195            private List<R> _results;
196            private String _resultsVar = SearchContainer.DEFAULT_RESULTS_VAR;
197    
198    }