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