1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.taglib.ui;
24  
25  import com.liferay.portal.kernel.dao.search.SearchContainer;
26  import com.liferay.portal.kernel.util.ServerDetector;
27  
28  import java.util.ArrayList;
29  import java.util.List;
30  
31  import javax.servlet.jsp.JspException;
32  import javax.servlet.jsp.JspTagException;
33  import javax.servlet.jsp.tagext.TagSupport;
34  
35  /**
36   * <a href="SearchContainerResultsTag.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Raymond Augé
39   *
40   */
41  public class SearchContainerResultsTag extends TagSupport {
42  
43      public static final String DEFAULT_RESULTS_VAR = "results";
44  
45      public static final String DEFAULT_TOTAL_VAR = "total";
46  
47      public int doEndTag() throws JspException {
48          try {
49              if (_results == null) {
50                  _results = (List)pageContext.getAttribute(_resultsVar);
51                  _total = (Integer)pageContext.getAttribute(_totalVar);
52              }
53  
54              if (_results != null) {
55                  if (_total < _results.size()) {
56                      _total = _results.size();
57                  }
58              }
59  
60              SearchContainerTag parentTag =
61                  (SearchContainerTag)findAncestorWithClass(
62                      this, SearchContainerTag.class);
63  
64              SearchContainer searchContainer = parentTag.getSearchContainer();
65  
66              searchContainer.setResults(_results);
67              searchContainer.setTotal(_total);
68  
69              parentTag.setHasResults(true);
70  
71              pageContext.setAttribute(_resultsVar, _results);
72              pageContext.setAttribute(_totalVar, _total);
73  
74              return EVAL_PAGE;
75          }
76          catch (Exception e) {
77              throw new JspException(e);
78          }
79          finally {
80              if (!ServerDetector.isResin()) {
81                  _results = null;
82                  _resultsVar = DEFAULT_RESULTS_VAR;
83                  _total = 0;
84                  _totalVar = DEFAULT_TOTAL_VAR;
85              }
86          }
87      }
88  
89      public int doStartTag() throws JspException {
90          SearchContainerTag parentTag =
91              (SearchContainerTag)findAncestorWithClass(
92                  this, SearchContainerTag.class);
93  
94          if (parentTag == null) {
95              throw new JspTagException("Requires liferay-ui:search-container");
96          }
97  
98          if (_results == null) {
99              pageContext.setAttribute(_resultsVar, new ArrayList());
100             pageContext.setAttribute(_totalVar, 0);
101         }
102 
103         return EVAL_BODY_INCLUDE;
104     }
105 
106     public List getResults() {
107         return _results;
108     }
109 
110     public String getResultsVar() {
111         return _resultsVar;
112     }
113 
114     public int getTotal() {
115         return _total;
116     }
117 
118     public String getTotalVar() {
119         return _totalVar;
120     }
121 
122     public void setResults(List results) {
123         _results = results;
124     }
125 
126     public void setResultsVar(String resultsVar) {
127         _resultsVar = resultsVar;
128     }
129 
130     public void setTotal(int total) {
131         _total = total;
132     }
133 
134     public void setTotalVar(String totalVar) {
135         _totalVar = totalVar;
136     }
137 
138     private List _results;
139     private String _resultsVar = DEFAULT_RESULTS_VAR;
140     private int _total;
141     private String _totalVar = DEFAULT_TOTAL_VAR;
142 
143 }