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                            pageContext.setAttribute(_resultsVar, _results);
103    
104                            return EVAL_PAGE;
105                    }
106                    catch (Exception e) {
107                            throw new JspException(e);
108                    }
109                    finally {
110                            if (!ServerDetector.isResin()) {
111                                    _deprecatedTotal = 0;
112                                    _deprecatedTotalVar =
113                                            SearchContainer.DEFAULT_DEPRECATED_TOTAL_VAR;
114                                    _results = null;
115                                    _resultsVar = SearchContainer.DEFAULT_RESULTS_VAR;
116                            }
117                    }
118            }
119    
120            @Override
121            public int doStartTag() throws JspException {
122                    SearchContainerTag<R> searchContainerTag =
123                            (SearchContainerTag<R>)findAncestorWithClass(
124                                    this, SearchContainerTag.class);
125    
126                    if (searchContainerTag == null) {
127                            throw new JspTagException("Requires liferay-ui:search-container");
128                    }
129    
130                    if (_results == null) {
131                            pageContext.setAttribute(_deprecatedTotalVar, 0);
132                            pageContext.setAttribute(_resultsVar, new ArrayList<R>());
133                    }
134    
135                    return EVAL_BODY_INCLUDE;
136            }
137    
138            public List<R> getResults() {
139                    return _results;
140            }
141    
142            public String getResultsVar() {
143                    return _resultsVar;
144            }
145    
146            /**
147             * @deprecated As of 6.2.0, replaced by {@link
148             *             SearchContainerTag#getTotal()}.
149             */
150            public int getTotal() {
151                    return _deprecatedTotal;
152            }
153    
154            /**
155             * @deprecated As of 6.2.0, replaced by {@link
156             *             SearchContainerTag#getTotalVar()}.
157             */
158            public String getTotalVar() {
159                    return _deprecatedTotalVar;
160            }
161    
162            public void setResults(List<R> results) {
163                    _results = results;
164            }
165    
166            public void setResultsVar(String resultsVar) {
167                    _resultsVar = resultsVar;
168            }
169    
170            /**
171             * @deprecated As of 6.2.0, replaced by {@link
172             *             SearchContainerTag#setTotal(int)}.
173             */
174            public void setTotal(int deprecatedTotal) {
175                    _deprecatedTotal = deprecatedTotal;
176            }
177    
178            /**
179             * @deprecated As of 6.2.0, replaced by {@link
180             *             SearchContainerTag#setTotalVar(String)}.
181             */
182            public void setTotalVar(String deprecatedTotalVar) {
183                    _deprecatedTotalVar = deprecatedTotalVar;
184            }
185    
186            private int _deprecatedTotal;
187            private String _deprecatedTotalVar =
188                    SearchContainer.DEFAULT_DEPRECATED_TOTAL_VAR;
189            private List<R> _results;
190            private String _resultsVar = SearchContainer.DEFAULT_RESULTS_VAR;
191    
192    }