1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.taglib.ui;
16  
17  import com.liferay.portal.kernel.dao.search.SearchContainer;
18  import com.liferay.portal.kernel.util.ServerDetector;
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import javax.servlet.jsp.JspException;
24  import javax.servlet.jsp.JspTagException;
25  import javax.servlet.jsp.tagext.TagSupport;
26  
27  /**
28   * <a href="SearchContainerResultsTag.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Raymond Augé
31   */
32  public class SearchContainerResultsTag<R> extends TagSupport {
33  
34      public static final String DEFAULT_RESULTS_VAR = "results";
35  
36      public static final String DEFAULT_TOTAL_VAR = "total";
37  
38      public int doEndTag() throws JspException {
39          try {
40              if (_results == null) {
41                  _results = (List<R>)pageContext.getAttribute(_resultsVar);
42                  _total = (Integer)pageContext.getAttribute(_totalVar);
43              }
44  
45              if (_results != null) {
46                  if (_total < _results.size()) {
47                      _total = _results.size();
48                  }
49              }
50  
51              SearchContainerTag<R> parentTag =
52                  (SearchContainerTag<R>)findAncestorWithClass(
53                      this, SearchContainerTag.class);
54  
55              SearchContainer<R> searchContainer = parentTag.getSearchContainer();
56  
57              searchContainer.setResults(_results);
58              searchContainer.setTotal(_total);
59  
60              parentTag.setHasResults(true);
61  
62              pageContext.setAttribute(_resultsVar, _results);
63              pageContext.setAttribute(_totalVar, _total);
64  
65              return EVAL_PAGE;
66          }
67          catch (Exception e) {
68              throw new JspException(e);
69          }
70          finally {
71              if (!ServerDetector.isResin()) {
72                  _results = null;
73                  _resultsVar = DEFAULT_RESULTS_VAR;
74                  _total = 0;
75                  _totalVar = DEFAULT_TOTAL_VAR;
76              }
77          }
78      }
79  
80      public int doStartTag() throws JspException {
81          SearchContainerTag<R> parentTag =
82              (SearchContainerTag<R>)findAncestorWithClass(
83                  this, SearchContainerTag.class);
84  
85          if (parentTag == null) {
86              throw new JspTagException("Requires liferay-ui:search-container");
87          }
88  
89          if (_results == null) {
90              pageContext.setAttribute(_resultsVar, new ArrayList<R>());
91              pageContext.setAttribute(_totalVar, 0);
92          }
93  
94          return EVAL_BODY_INCLUDE;
95      }
96  
97      public List<R> getResults() {
98          return _results;
99      }
100 
101     public String getResultsVar() {
102         return _resultsVar;
103     }
104 
105     public int getTotal() {
106         return _total;
107     }
108 
109     public String getTotalVar() {
110         return _totalVar;
111     }
112 
113     public void setResults(List<R> results) {
114         _results = results;
115     }
116 
117     public void setResultsVar(String resultsVar) {
118         _resultsVar = resultsVar;
119     }
120 
121     public void setTotal(int total) {
122         _total = total;
123     }
124 
125     public void setTotalVar(String totalVar) {
126         _totalVar = totalVar;
127     }
128 
129     private List<R> _results;
130     private String _resultsVar = DEFAULT_RESULTS_VAR;
131     private int _total;
132     private String _totalVar = DEFAULT_TOTAL_VAR;
133 
134 }