1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.taglib.ui;
16  
17  import com.liferay.portal.kernel.dao.search.SearchContainer;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import javax.servlet.jsp.JspException;
23  import javax.servlet.jsp.JspTagException;
24  import javax.servlet.jsp.tagext.TagSupport;
25  
26  /**
27   * <a href="SearchContainerResultsTag.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Raymond Augé
30   */
31  public class SearchContainerResultsTag extends TagSupport {
32  
33      public static final String DEFAULT_RESULTS_VAR = "results";
34  
35      public static final String DEFAULT_TOTAL_VAR = "total";
36  
37      public int doEndTag() throws JspException {
38          try {
39              if (_results == null) {
40                  _results = (List)pageContext.getAttribute(_resultsVar);
41                  _total = (Integer)pageContext.getAttribute(_totalVar);
42              }
43  
44              if (_results != null) {
45                  if (_total < _results.size()) {
46                      _total = _results.size();
47                  }
48              }
49  
50              SearchContainerTag parentTag =
51                  (SearchContainerTag)findAncestorWithClass(
52                      this, SearchContainerTag.class);
53  
54              SearchContainer searchContainer = parentTag.getSearchContainer();
55  
56              searchContainer.setResults(_results);
57              searchContainer.setTotal(_total);
58  
59              parentTag.setHasResults(true);
60  
61              pageContext.setAttribute(_resultsVar, _results);
62              pageContext.setAttribute(_totalVar, _total);
63  
64              return EVAL_PAGE;
65          }
66          catch (Exception e) {
67              throw new JspException(e);
68          }
69          finally {
70              _results = null;
71              _resultsVar = DEFAULT_RESULTS_VAR;
72              _total = 0;
73              _totalVar = DEFAULT_TOTAL_VAR;
74          }
75      }
76  
77      public int doStartTag() throws JspException {
78          SearchContainerTag parentTag =
79              (SearchContainerTag)findAncestorWithClass(
80                  this, SearchContainerTag.class);
81  
82          if (parentTag == null) {
83              throw new JspTagException("Requires liferay-ui:search-container");
84          }
85  
86          if (_results == null) {
87              pageContext.setAttribute(_resultsVar, new ArrayList());
88              pageContext.setAttribute(_totalVar, 0);
89          }
90  
91          return EVAL_BODY_INCLUDE;
92      }
93  
94      public List getResults() {
95          return _results;
96      }
97  
98      public String getResultsVar() {
99          return _resultsVar;
100     }
101 
102     public int getTotal() {
103         return _total;
104     }
105 
106     public String getTotalVar() {
107         return _totalVar;
108     }
109 
110     public void setResults(List results) {
111         _results = results;
112     }
113 
114     public void setResultsVar(String resultsVar) {
115         _resultsVar = resultsVar;
116     }
117 
118     public void setTotal(int total) {
119         _total = total;
120     }
121 
122     public void setTotalVar(String totalVar) {
123         _totalVar = totalVar;
124     }
125 
126     private List _results;
127     private String _resultsVar = DEFAULT_RESULTS_VAR;
128     private int _total;
129     private String _totalVar = DEFAULT_TOTAL_VAR;
130 
131 }