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.bean.BeanPropertiesUtil;
18  import com.liferay.portal.kernel.dao.search.ResultRow;
19  import com.liferay.portal.kernel.dao.search.SearchContainer;
20  import com.liferay.portal.kernel.log.Log;
21  import com.liferay.portal.kernel.log.LogFactoryUtil;
22  import com.liferay.portal.kernel.util.GetterUtil;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.model.BaseModel;
25  import com.liferay.taglib.util.ParamAndPropertyAncestorTagImpl;
26  
27  import java.util.ArrayList;
28  import java.util.LinkedHashMap;
29  import java.util.List;
30  import java.util.Map;
31  
32  import javax.servlet.jsp.JspException;
33  
34  /**
35   * <a href="SearchContainerRowTag.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Raymond Augé
38   */
39  public class SearchContainerRowTag<R> extends ParamAndPropertyAncestorTagImpl {
40  
41      public static final String DEFAULT_INDEX_VAR = "index";
42  
43      public static final String DEFAULT_MODEL_VAR = "model";
44  
45      public static final String DEFAULT_ROW_VAR = "row";
46  
47      public void addParam(String name, String value) {
48          if (name.equals("className")) {
49              _row.setClassName(value);
50          }
51          else if (name.equals("classHoverName")) {
52              _row.setClassHoverName(value);
53          }
54          else if (name.equals("restricted")) {
55              _row.setRestricted(GetterUtil.getBoolean(value, false));
56          }
57          else {
58              Object obj = pageContext.getAttribute(value);
59  
60              if (obj == null) {
61                  obj = value;
62              }
63  
64              _row.setParameter(name, obj);
65          }
66      }
67  
68      public int doAfterBody() {
69          if (!_headerNamesAssigned) {
70              SearchContainerTag<R> parentTag =
71                  (SearchContainerTag<R>)findAncestorWithClass(
72                      this, SearchContainerTag.class);
73  
74              SearchContainer<R> searchContainer = parentTag.getSearchContainer();
75  
76              searchContainer.setHeaderNames(_headerNames);
77              searchContainer.setOrderableHeaders(_orderableHeaders);
78  
79              _headerNamesAssigned = true;
80          }
81  
82          _resultRows.add(_row);
83  
84          _rowIndex++;
85  
86          if (_rowIndex < (_results.size())) {
87              processRow();
88  
89              return EVAL_BODY_AGAIN;
90          }
91          else {
92              return SKIP_BODY;
93          }
94      }
95  
96      public int doEndTag() {
97          _bold = false;
98          _className = null;
99          _escapedModel = false;
100         _headerNames = null;
101         _headerNamesAssigned = false;
102         _indexVar = DEFAULT_INDEX_VAR;
103         _keyProperty = null;
104         _modelVar = DEFAULT_MODEL_VAR;
105         _orderableHeaders = null;
106         _resultRows = null;
107         _rowIndex = 0;
108         _rowVar = DEFAULT_ROW_VAR;
109         _row = null;
110         _stringKey = false;
111 
112         return EVAL_PAGE;
113     }
114 
115     public int doStartTag() throws JspException {
116         SearchContainerTag<R> parentTag =
117             (SearchContainerTag<R>)findAncestorWithClass(
118                 this, SearchContainerTag.class);
119 
120         if (parentTag == null) {
121             throw new JspException("Requires liferay-ui:search-container");
122         }
123         else if (!parentTag.isHasResults()) {
124             throw new JspException(
125                 "Requires liferay-ui:search-container-results");
126         }
127 
128         _resultRows = parentTag.getSearchContainer().getResultRows();
129         _results = parentTag.getSearchContainer().getResults();
130 
131         if ((_results != null) && (!_results.isEmpty())) {
132             processRow();
133 
134             return EVAL_BODY_INCLUDE;
135         }
136         else {
137             return SKIP_BODY;
138         }
139     }
140 
141     public String getClassName() {
142         return _className;
143     }
144 
145     public List<String> getHeaderNames() {
146         if (_headerNames == null) {
147             _headerNames = new ArrayList<String>();
148         }
149 
150         return _headerNames;
151     }
152 
153     public String getIndexVar() {
154         return _indexVar;
155     }
156 
157     public String getKeyProperty() {
158         return _keyProperty;
159     }
160 
161     public String getModelVar() {
162         return _modelVar;
163     }
164 
165     public Map<String, String> getOrderableHeaders() {
166         if (_orderableHeaders == null) {
167             _orderableHeaders = new LinkedHashMap<String, String>();
168         }
169 
170         return _orderableHeaders;
171     }
172 
173     public ResultRow getRow() {
174         return _row;
175     }
176 
177     public String getRowVar() {
178         return _rowVar;
179     }
180 
181     public boolean isBold() {
182         return _bold;
183     }
184 
185     public boolean isEscapedModel() {
186         return _escapedModel;
187     }
188 
189     public boolean isHeaderNamesAssigned() {
190         return _headerNamesAssigned;
191     }
192 
193     public boolean isStringKey() {
194         return _stringKey;
195     }
196 
197     public void setBold(boolean bold) {
198         _bold = bold;
199     }
200 
201     public void setClassName(String className) {
202         _className = className;
203     }
204 
205     public void setEscapedModel(boolean escapedModel) {
206         _escapedModel = escapedModel;
207     }
208 
209     public void setHeaderNames(List<String> headerNames) {
210         _headerNames = headerNames;
211     }
212 
213     public void setHeaderNamesAssigned(boolean headerNamesAssigned) {
214         _headerNamesAssigned = headerNamesAssigned;
215     }
216 
217     public void setIndexVar(String indexVar) {
218         _indexVar = indexVar;
219     }
220 
221     public void setKeyProperty(String keyProperty) {
222         _keyProperty = keyProperty;
223     }
224 
225     public void setModelVar(String var) {
226         _modelVar = var;
227     }
228 
229     public void setOrderableHeaders(Map<String, String> orderableHeaders) {
230         _orderableHeaders = orderableHeaders;
231     }
232 
233     public void setRow(ResultRow row) {
234         _row = row;
235     }
236 
237     public void setRowVar(String rowVar) {
238         _rowVar = rowVar;
239     }
240 
241     public void setStringKey(boolean stringKey) {
242         _stringKey = stringKey;
243     }
244 
245     protected void processRow() {
246         Object model = _results.get(_rowIndex);
247 
248         if (isEscapedModel()) {
249             BaseModel<?> baseModel = (BaseModel<?>)model;
250 
251             model = baseModel.toEscapedModel();
252         }
253 
254         if (_log.isDebugEnabled()) {
255             _log.debug(BeanPropertiesUtil.getBoolean(model, "escapedModel"));
256         }
257 
258         if (Validator.isNull(_keyProperty)) {
259             String primaryKey = String.valueOf(model);
260 
261             _row = new ResultRow(model, primaryKey, _rowIndex, _bold);
262         }
263         else if (isStringKey()) {
264             String primaryKey = BeanPropertiesUtil.getString(
265                 model, _keyProperty);
266 
267             _row = new ResultRow(model, primaryKey, _rowIndex, _bold);
268         }
269         else {
270             Object primaryKey = BeanPropertiesUtil.getObject(
271                 model, _keyProperty);
272 
273             _row = new ResultRow(
274                 model, String.valueOf(primaryKey), _rowIndex, _bold);
275         }
276 
277         pageContext.setAttribute(_indexVar, _rowIndex);
278         pageContext.setAttribute(_modelVar, model);
279         pageContext.setAttribute(_rowVar, _row);
280     }
281 
282     private static Log _log = LogFactoryUtil.getLog(
283         SearchContainerRowTag.class);
284 
285     private boolean _bold;
286     private String _className;
287     private boolean _escapedModel;
288     private List<String> _headerNames;
289     private boolean _headerNamesAssigned;
290     private String _indexVar = DEFAULT_INDEX_VAR;
291     private String _keyProperty;
292     private String _modelVar = DEFAULT_MODEL_VAR;
293     private Map<String, String> _orderableHeaders;
294     private List<R> _results;
295     private List<ResultRow> _resultRows;
296     private int _rowIndex;
297     private String _rowVar = DEFAULT_ROW_VAR;
298     private ResultRow _row;
299     private boolean _stringKey = false;
300 
301 }