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.ServerDetector;
025 import com.liferay.portal.kernel.util.Validator;
026 import com.liferay.taglib.util.ParamAndPropertyAncestorTagImpl;
027
028 import java.lang.reflect.Method;
029
030 import java.util.ArrayList;
031 import java.util.LinkedHashMap;
032 import java.util.List;
033 import java.util.Map;
034
035 import javax.servlet.ServletContext;
036 import javax.servlet.jsp.JspException;
037
038
041 public class SearchContainerRowTag extends ParamAndPropertyAncestorTagImpl {
042
043 public static final String DEFAULT_INDEX_VAR = "index";
044
045 public static final String DEFAULT_MODEL_VAR = "model";
046
047 public static final String DEFAULT_ROW_VAR = "row";
048
049 public void addParam(String name, String value) {
050 if (name.equals("className")) {
051 _row.setClassName(value);
052 }
053 else if (name.equals("classHoverName")) {
054 _row.setClassHoverName(value);
055 }
056 else if (name.equals("restricted")) {
057 _row.setRestricted(GetterUtil.getBoolean(value, false));
058 }
059 else {
060 Object obj = pageContext.getAttribute(value);
061
062 if (obj == null) {
063 obj = value;
064 }
065
066 _row.setParameter(name, obj);
067 }
068 }
069
070 public int doAfterBody() throws JspException {
071 if (!_headerNamesAssigned) {
072 SearchContainerTag parentTag =
073 (SearchContainerTag)findAncestorWithClass(
074 this, SearchContainerTag.class);
075
076 SearchContainer searchContainer = parentTag.getSearchContainer();
077
078 searchContainer.setHeaderNames(_headerNames);
079 searchContainer.setOrderableHeaders(_orderableHeaders);
080
081 _headerNamesAssigned = true;
082 }
083
084 _resultRows.add(_row);
085
086 _rowIndex++;
087
088 if (_rowIndex < (_results.size())) {
089 processRow();
090
091 return EVAL_BODY_AGAIN;
092 }
093 else {
094 return SKIP_BODY;
095 }
096 }
097
098 public int doEndTag() {
099 _headerNames = null;
100 _headerNamesAssigned = false;
101 _resultRows = null;
102 _rowIndex = 0;
103 _row = null;
104
105 if (!ServerDetector.isResin()) {
106 _bold = false;
107 _className = null;
108 _escapedModel = false;
109 _indexVar = DEFAULT_INDEX_VAR;
110 _keyProperty = null;
111 _modelVar = DEFAULT_MODEL_VAR;
112 _orderableHeaders = null;
113 _rowVar = DEFAULT_ROW_VAR;
114 _stringKey = false;
115 }
116
117 return EVAL_PAGE;
118 }
119
120 public int doStartTag() throws JspException {
121 SearchContainerTag parentTag =
122 (SearchContainerTag)findAncestorWithClass(
123 this, SearchContainerTag.class);
124
125 if (parentTag == null) {
126 throw new JspException("Requires liferay-ui:search-container");
127 }
128 else if (!parentTag.isHasResults()) {
129 throw new JspException(
130 "Requires liferay-ui:search-container-results");
131 }
132
133 _resultRows = parentTag.getSearchContainer().getResultRows();
134 _results = parentTag.getSearchContainer().getResults();
135
136 if ((_results != null) && (!_results.isEmpty())) {
137 processRow();
138
139 return EVAL_BODY_INCLUDE;
140 }
141 else {
142 return SKIP_BODY;
143 }
144 }
145
146 public String getClassName() {
147 return _className;
148 }
149
150 public List<String> getHeaderNames() {
151 if (_headerNames == null) {
152 _headerNames = new ArrayList<String>();
153 }
154
155 return _headerNames;
156 }
157
158 public String getIndexVar() {
159 return _indexVar;
160 }
161
162 public String getKeyProperty() {
163 return _keyProperty;
164 }
165
166 public String getModelVar() {
167 return _modelVar;
168 }
169
170 public Map<String, String> getOrderableHeaders() {
171 if (_orderableHeaders == null) {
172 _orderableHeaders = new LinkedHashMap<String, String>();
173 }
174
175 return _orderableHeaders;
176 }
177
178 public ResultRow getRow() {
179 return _row;
180 }
181
182 public String getRowVar() {
183 return _rowVar;
184 }
185
186 public boolean isBold() {
187 return _bold;
188 }
189
190 public boolean isEscapedModel() {
191 return _escapedModel;
192 }
193
194 public boolean isHeaderNamesAssigned() {
195 return _headerNamesAssigned;
196 }
197
198 public boolean isStringKey() {
199 return _stringKey;
200 }
201
202 public void setBold(boolean bold) {
203 _bold = bold;
204 }
205
206 public void setClassName(String className) {
207 _className = className;
208 }
209
210 public void setEscapedModel(boolean escapedModel) {
211 _escapedModel = escapedModel;
212 }
213
214 public void setHeaderNames(List<String> headerNames) {
215 _headerNames = headerNames;
216 }
217
218 public void setHeaderNamesAssigned(boolean headerNamesAssigned) {
219 _headerNamesAssigned = headerNamesAssigned;
220 }
221
222 public void setIndexVar(String indexVar) {
223 _indexVar = indexVar;
224 }
225
226 public void setKeyProperty(String keyProperty) {
227 _keyProperty = keyProperty;
228 }
229
230 public void setModelVar(String var) {
231 _modelVar = var;
232 }
233
234 public void setOrderableHeaders(Map<String, String> orderableHeaders) {
235 _orderableHeaders = orderableHeaders;
236 }
237
238 public void setRow(ResultRow row) {
239 _row = row;
240 }
241
242 public void setRowVar(String rowVar) {
243 _rowVar = rowVar;
244 }
245
246 public void setServletContext(ServletContext servletContext) {
247 _servletContext = servletContext;
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 ClassLoader classLoader = null;
260
261 if (_servletContext != null) {
262 classLoader = (ClassLoader)PortletBeanLocatorUtil.locate(
263 _servletContext.getServletContextName(),
264 "portletClassLoader");
265 }
266
267 if (classLoader == null) {
268 Thread currentThread = Thread.currentThread();
269
270 classLoader = currentThread.getContextClassLoader();
271 }
272
273 Class<?> classObj = classLoader.loadClass(_className);
274
275 Method method = classObj.getMethod(
276 "toEscapedModel", new Class[0]);
277
278 model = method.invoke(model, new Object[0]);
279 }
280 catch (Exception e) {
281 throw new JspException(e.getMessage());
282 }
283 }
284
285 if (_log.isDebugEnabled()) {
286 _log.debug(BeanPropertiesUtil.getBoolean(model, "escapedModel"));
287 }
288
289 if (Validator.isNull(_keyProperty)) {
290 String primaryKey = String.valueOf(model);
291
292 _row = new ResultRow(model, primaryKey, _rowIndex, _bold);
293 }
294 else if (isStringKey()) {
295 String primaryKey = BeanPropertiesUtil.getString(
296 model, _keyProperty);
297
298 _row = new ResultRow(model, primaryKey, _rowIndex, _bold);
299 }
300 else {
301 Object primaryKey = BeanPropertiesUtil.getObject(
302 model, _keyProperty);
303
304 _row = new ResultRow(
305 model, String.valueOf(primaryKey), _rowIndex, _bold);
306 }
307
308 pageContext.setAttribute(_indexVar, _rowIndex);
309 pageContext.setAttribute(_modelVar, model);
310 pageContext.setAttribute(_rowVar, _row);
311 }
312
313 private static Log _log = LogFactoryUtil.getLog(
314 SearchContainerRowTag.class);
315
316 private boolean _bold;
317 private String _className;
318 private boolean _escapedModel;
319 private List<String> _headerNames;
320 private boolean _headerNamesAssigned;
321 private String _indexVar = DEFAULT_INDEX_VAR;
322 private String _keyProperty;
323 private String _modelVar = DEFAULT_MODEL_VAR;
324 private Map<String, String> _orderableHeaders;
325 private List _results;
326 private List<ResultRow> _resultRows;
327 private int _rowIndex;
328 private String _rowVar = DEFAULT_ROW_VAR;
329 private ResultRow _row;
330 private ServletContext _servletContext;
331 private boolean _stringKey = false;
332
333 }