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