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