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  
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  import javax.servlet.jsp.JspException;
31  import javax.servlet.jsp.JspTagException;
32  import javax.servlet.jsp.tagext.TagSupport;
33  
34  /**
35   * <a href="SearchContainerResultsTag.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Raymond Augé
38   */
39  public class SearchContainerResultsTag extends TagSupport {
40  
41      public static final String DEFAULT_RESULTS_VAR = "results";
42  
43      public static final String DEFAULT_TOTAL_VAR = "total";
44  
45      public int doEndTag() throws JspException {
46          try {
47              if (_results == null) {
48                  _results = (List)pageContext.getAttribute(_resultsVar);
49                  _total = (Integer)pageContext.getAttribute(_totalVar);
50              }
51  
52              if (_results != null) {
53                  if (_total < _results.size()) {
54                      _total = _results.size();
55                  }
56              }
57  
58              SearchContainerTag parentTag =
59                  (SearchContainerTag)findAncestorWithClass(
60                      this, SearchContainerTag.class);
61  
62              SearchContainer searchContainer = parentTag.getSearchContainer();
63  
64              searchContainer.setResults(_results);
65              searchContainer.setTotal(_total);
66  
67              parentTag.setHasResults(true);
68  
69              pageContext.setAttribute(_resultsVar, _results);
70              pageContext.setAttribute(_totalVar, _total);
71  
72              return EVAL_PAGE;
73          }
74          catch (Exception e) {
75              throw new JspException(e);
76          }
77          finally {
78              _results = null;
79              _resultsVar = DEFAULT_RESULTS_VAR;
80              _total = 0;
81              _totalVar = DEFAULT_TOTAL_VAR;
82          }
83      }
84  
85      public int doStartTag() throws JspException {
86          SearchContainerTag parentTag =
87              (SearchContainerTag)findAncestorWithClass(
88                  this, SearchContainerTag.class);
89  
90          if (parentTag == null) {
91              throw new JspTagException("Requires liferay-ui:search-container");
92          }
93  
94          if (_results == null) {
95              pageContext.setAttribute(_resultsVar, new ArrayList());
96              pageContext.setAttribute(_totalVar, 0);
97          }
98  
99          return EVAL_BODY_INCLUDE;
100     }
101 
102     public List getResults() {
103         return _results;
104     }
105 
106     public String getResultsVar() {
107         return _resultsVar;
108     }
109 
110     public int getTotal() {
111         return _total;
112     }
113 
114     public String getTotalVar() {
115         return _totalVar;
116     }
117 
118     public void setResults(List results) {
119         _results = results;
120     }
121 
122     public void setResultsVar(String resultsVar) {
123         _resultsVar = resultsVar;
124     }
125 
126     public void setTotal(int total) {
127         _total = total;
128     }
129 
130     public void setTotalVar(String totalVar) {
131         _totalVar = totalVar;
132     }
133 
134     private List _results;
135     private String _resultsVar = DEFAULT_RESULTS_VAR;
136     private int _total;
137     private String _totalVar = DEFAULT_TOTAL_VAR;
138 
139 }