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  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              if (!ServerDetector.isResin()) {
80                  _results = null;
81                  _resultsVar = DEFAULT_RESULTS_VAR;
82                  _total = 0;
83                  _totalVar = DEFAULT_TOTAL_VAR;
84              }
85          }
86      }
87  
88      public int doStartTag() throws JspException {
89          SearchContainerTag parentTag =
90              (SearchContainerTag)findAncestorWithClass(
91                  this, SearchContainerTag.class);
92  
93          if (parentTag == null) {
94              throw new JspTagException("Requires liferay-ui:search-container");
95          }
96  
97          if (_results == null) {
98              pageContext.setAttribute(_resultsVar, new ArrayList());
99              pageContext.setAttribute(_totalVar, 0);
100         }
101 
102         return EVAL_BODY_INCLUDE;
103     }
104 
105     public List getResults() {
106         return _results;
107     }
108 
109     public String getResultsVar() {
110         return _resultsVar;
111     }
112 
113     public int getTotal() {
114         return _total;
115     }
116 
117     public String getTotalVar() {
118         return _totalVar;
119     }
120 
121     public void setResults(List results) {
122         _results = results;
123     }
124 
125     public void setResultsVar(String resultsVar) {
126         _resultsVar = resultsVar;
127     }
128 
129     public void setTotal(int total) {
130         _total = total;
131     }
132 
133     public void setTotalVar(String totalVar) {
134         _totalVar = totalVar;
135     }
136 
137     private List _results;
138     private String _resultsVar = DEFAULT_RESULTS_VAR;
139     private int _total;
140     private String _totalVar = DEFAULT_TOTAL_VAR;
141 
142 }