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