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