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            @Override
034            public int doEndTag() throws JspException {
035                    try {
036                            SearchContainerTag<R> searchContainerTag =
037                                    (SearchContainerTag<R>)findAncestorWithClass(
038                                            this, SearchContainerTag.class);
039    
040                            SearchContainer<R> searchContainer =
041                                    searchContainerTag.getSearchContainer();
042    
043                            String totalVar = searchContainer.getTotalVar();
044    
045                            if (totalVar.equals(SearchContainer.DEFAULT_TOTAL_VAR)) {
046                                    pageContext.removeAttribute(totalVar);
047                            }
048    
049                            if (_results == null) {
050                                    _results = searchContainer.getResults();
051    
052                                    if (_results.isEmpty()) {
053                                            _results = (List<R>)pageContext.getAttribute(_resultsVar);
054                                    }
055                            }
056    
057                            searchContainer.setResults(_results);
058    
059                            pageContext.setAttribute(_resultsVar, _results);
060    
061                            return EVAL_PAGE;
062                    }
063                    catch (Exception e) {
064                            throw new JspException(e);
065                    }
066                    finally {
067                            if (!ServerDetector.isResin()) {
068                                    _results = null;
069                                    _resultsVar = SearchContainer.DEFAULT_RESULTS_VAR;
070                            }
071                    }
072            }
073    
074            @Override
075            public int doStartTag() throws JspException {
076                    SearchContainerTag<R> searchContainerTag =
077                            (SearchContainerTag<R>)findAncestorWithClass(
078                                    this, SearchContainerTag.class);
079    
080                    if (searchContainerTag == null) {
081                            throw new JspTagException("Requires liferay-ui:search-container");
082                    }
083    
084                    if (_results == null) {
085                            pageContext.setAttribute(_resultsVar, new ArrayList<R>());
086                    }
087    
088                    return EVAL_BODY_INCLUDE;
089            }
090    
091            public List<R> getResults() {
092                    return _results;
093            }
094    
095            public String getResultsVar() {
096                    return _resultsVar;
097            }
098    
099            public void setResults(List<R> results) {
100                    _results = results;
101            }
102    
103            public void setResultsVar(String resultsVar) {
104                    _resultsVar = resultsVar;
105            }
106    
107            private List<R> _results;
108            private String _resultsVar = SearchContainer.DEFAULT_RESULTS_VAR;
109    
110    }