001
014
015 package com.liferay.taglib.ui;
016
017 import com.liferay.portal.kernel.bean.BeanPropertiesUtil;
018 import com.liferay.portal.kernel.bean.PortletBeanLocatorUtil;
019 import com.liferay.portal.kernel.dao.search.ResultRow;
020 import com.liferay.portal.kernel.dao.search.SearchContainer;
021 import com.liferay.portal.kernel.log.Log;
022 import com.liferay.portal.kernel.log.LogFactoryUtil;
023 import com.liferay.portal.kernel.util.GetterUtil;
024 import com.liferay.portal.kernel.util.Validator;
025 import com.liferay.taglib.util.ParamAndPropertyAncestorTagImpl;
026
027 import java.lang.reflect.Method;
028
029 import java.util.ArrayList;
030 import java.util.LinkedHashMap;
031 import java.util.List;
032 import java.util.Map;
033
034 import javax.servlet.ServletContext;
035 import javax.servlet.jsp.JspException;
036
037
040 public class SearchContainerRowTag extends ParamAndPropertyAncestorTagImpl {
041
042 public static final String DEFAULT_INDEX_VAR = "index";
043
044 public static final String DEFAULT_MODEL_VAR = "model";
045
046 public static final String DEFAULT_ROW_VAR = "row";
047
048 public void addParam(String name, String value) {
049 if (name.equals("className")) {
050 _row.setClassName(value);
051 }
052 else if (name.equals("classHoverName")) {
053 _row.setClassHoverName(value);
054 }
055 else if (name.equals("restricted")) {
056 _row.setRestricted(GetterUtil.getBoolean(value, false));
057 }
058 else {
059 Object obj = pageContext.getAttribute(value);
060
061 if (obj == null) {
062 obj = value;
063 }
064
065 _row.setParameter(name, obj);
066 }
067 }
068
069 public int doAfterBody() throws JspException {
070 if (!_headerNamesAssigned) {
071 SearchContainerTag parentTag =
072 (SearchContainerTag)findAncestorWithClass(
073 this, SearchContainerTag.class);
074
075 SearchContainer searchContainer = parentTag.getSearchContainer();
076
077 searchContainer.setHeaderNames(_headerNames);
078 searchContainer.setOrderableHeaders(_orderableHeaders);
079
080 _headerNamesAssigned = true;
081 }
082
083 _resultRows.add(_row);
084
085 _rowIndex++;
086
087 if (_rowIndex < (_results.size())) {
088 processRow();
089
090 return EVAL_BODY_AGAIN;
091 }
092 else {
093 return SKIP_BODY;
094 }
095 }
096
097 public int doEndTag() {
098 _bold = false;
099 _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 setServletContext(ServletContext servletContext) {
243 _servletContext = servletContext;
244 }
245
246 public void setStringKey(boolean stringKey) {
247 _stringKey = stringKey;
248 }
249
250 protected void processRow() throws JspException {
251 Object model = _results.get(_rowIndex);
252
253 if (isEscapedModel()) {
254 try {
255 ClassLoader classLoader = null;
256
257 if (_servletContext != null) {
258 classLoader = (ClassLoader)PortletBeanLocatorUtil.locate(
259 _servletContext.getServletContextName(),
260 "portletClassLoader");
261 }
262
263 if (classLoader == null) {
264 Thread currentThread = Thread.currentThread();
265
266 classLoader = currentThread.getContextClassLoader();
267 }
268
269 Class<?> classObj = classLoader.loadClass(_className);
270
271 Method method = classObj.getMethod(
272 "toEscapedModel", new Class[0]);
273
274 model = method.invoke(model, new Object[0]);
275 }
276 catch (Exception e) {
277 throw new JspException(e.getMessage());
278 }
279 }
280
281 if (_log.isDebugEnabled()) {
282 _log.debug(BeanPropertiesUtil.getBoolean(model, "escapedModel"));
283 }
284
285 if (Validator.isNull(_keyProperty)) {
286 String primaryKey = String.valueOf(model);
287
288 _row = new ResultRow(model, primaryKey, _rowIndex, _bold);
289 }
290 else if (isStringKey()) {
291 String primaryKey = BeanPropertiesUtil.getString(
292 model, _keyProperty);
293
294 _row = new ResultRow(model, primaryKey, _rowIndex, _bold);
295 }
296 else {
297 Object primaryKey = BeanPropertiesUtil.getObject(
298 model, _keyProperty);
299
300 _row = new ResultRow(
301 model, String.valueOf(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 }