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  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 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)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 parentTag =
52                  (SearchContainerTag)findAncestorWithClass(
53                      this, SearchContainerTag.class);
54  
55              SearchContainer 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 parentTag =
82              (SearchContainerTag)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());
91              pageContext.setAttribute(_totalVar, 0);
92          }
93  
94          return EVAL_BODY_INCLUDE;
95      }
96  
97      public List 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 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 _results;
130     private String _resultsVar = DEFAULT_RESULTS_VAR;
131     private int _total;
132     private String _totalVar = DEFAULT_TOTAL_VAR;
133 
134 }