001
014
015 package com.liferay.taglib.ui;
016
017 import com.liferay.portal.kernel.bean.BeanPropertiesUtil;
018 import com.liferay.portal.kernel.dao.search.ResultRow;
019 import com.liferay.portal.kernel.dao.search.SearchContainer;
020 import com.liferay.portal.kernel.repository.model.RepositoryModel;
021 import com.liferay.portal.kernel.util.GetterUtil;
022 import com.liferay.portal.kernel.util.ServerDetector;
023 import com.liferay.portal.kernel.util.Validator;
024 import com.liferay.portal.model.BaseModel;
025 import com.liferay.taglib.util.ParamAndPropertyAncestorTagImpl;
026
027 import java.util.ArrayList;
028 import java.util.LinkedHashMap;
029 import java.util.List;
030 import java.util.Map;
031
032 import javax.servlet.ServletContext;
033 import javax.servlet.jsp.JspException;
034 import javax.servlet.jsp.tagext.BodyTag;
035
036
039 public class SearchContainerRowTag<R>
040 extends ParamAndPropertyAncestorTagImpl implements BodyTag {
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 @Override
049 public void addParam(String name, String value) {
050 if (name.equals("className")) {
051 _resultRow.setClassName(value);
052 }
053 else if (name.equals("classHoverName")) {
054 _resultRow.setClassHoverName(value);
055 }
056 else if (name.equals("restricted")) {
057 _resultRow.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 _resultRow.setParameter(name, obj);
067 }
068 }
069
070 @Override
071 public int doAfterBody() {
072 if (!_headerNamesAssigned) {
073 SearchContainerTag<R> searchContainerTag =
074 (SearchContainerTag<R>)findAncestorWithClass(
075 this, SearchContainerTag.class);
076
077 SearchContainer<R> searchContainer =
078 searchContainerTag.getSearchContainer();
079
080 searchContainer.setHeaderNames(_headerNames);
081 searchContainer.setOrderableHeaders(_orderableHeaders);
082
083 _headerNamesAssigned = true;
084 }
085
086 if (!_resultRow.isSkip()) {
087 _resultRows.add(_resultRow);
088 }
089
090 _rowIndex++;
091
092 if (_rowIndex < (_results.size())) {
093 processRow();
094
095 return EVAL_BODY_AGAIN;
096 }
097 else {
098 return SKIP_BODY;
099 }
100 }
101
102 @Override
103 public int doEndTag() {
104 _headerNames = null;
105 _headerNamesAssigned = false;
106 _resultRows = null;
107 _rowIndex = 0;
108 _resultRow = null;
109
110 if (!ServerDetector.isResin()) {
111 _bold = false;
112 _className = null;
113 _escapedModel = false;
114 _indexVar = DEFAULT_INDEX_VAR;
115 _keyProperty = null;
116 _modelVar = DEFAULT_MODEL_VAR;
117 _orderableHeaders = null;
118 _rowVar = DEFAULT_ROW_VAR;
119 _stringKey = false;
120 }
121
122 return EVAL_PAGE;
123 }
124
125 @Override
126 public int doStartTag() throws JspException {
127 SearchContainerTag<R> searchContainerTag =
128 (SearchContainerTag<R>)findAncestorWithClass(
129 this, SearchContainerTag.class);
130
131 if (searchContainerTag == null) {
132 throw new JspException("Requires liferay-ui:search-container");
133 }
134 else if (!searchContainerTag.isHasResults()) {
135 throw new JspException(
136 "Requires liferay-ui:search-container-results");
137 }
138
139 SearchContainer<R> searchContainer =
140 searchContainerTag.getSearchContainer();
141
142 _resultRows = searchContainer.getResultRows();
143 _results = searchContainer.getResults();
144
145 if ((_results != null) && (!_results.isEmpty())) {
146 processRow();
147
148 return EVAL_BODY_INCLUDE;
149 }
150 else {
151 return SKIP_BODY;
152 }
153 }
154
155 public String getClassName() {
156 return _className;
157 }
158
159 public List<String> getHeaderNames() {
160 if (_headerNames == null) {
161 _headerNames = new ArrayList<String>();
162 }
163
164 return _headerNames;
165 }
166
167 public String getIndexVar() {
168 return _indexVar;
169 }
170
171 public String getKeyProperty() {
172 return _keyProperty;
173 }
174
175 public String getModelVar() {
176 return _modelVar;
177 }
178
179 public Map<String, String> getOrderableHeaders() {
180 if (_orderableHeaders == null) {
181 _orderableHeaders = new LinkedHashMap<String, String>();
182 }
183
184 return _orderableHeaders;
185 }
186
187 public ResultRow getRow() {
188 return _resultRow;
189 }
190
191 public String getRowVar() {
192 return _rowVar;
193 }
194
195 public boolean isBold() {
196 return _bold;
197 }
198
199 public boolean isEscapedModel() {
200 return _escapedModel;
201 }
202
203 public boolean isHeaderNamesAssigned() {
204 return _headerNamesAssigned;
205 }
206
207 public boolean isStringKey() {
208 return _stringKey;
209 }
210
211 public void setBold(boolean bold) {
212 _bold = bold;
213 }
214
215 public void setClassName(String className) {
216 _className = className;
217 }
218
219 public void setEscapedModel(boolean escapedModel) {
220 _escapedModel = escapedModel;
221 }
222
223 public void setHeaderNames(List<String> headerNames) {
224 _headerNames = headerNames;
225 }
226
227 public void setHeaderNamesAssigned(boolean headerNamesAssigned) {
228 _headerNamesAssigned = headerNamesAssigned;
229 }
230
231 public void setIndexVar(String indexVar) {
232 _indexVar = indexVar;
233 }
234
235 public void setKeyProperty(String keyProperty) {
236 _keyProperty = keyProperty;
237 }
238
239 public void setModelVar(String var) {
240 _modelVar = var;
241 }
242
243 public void setOrderableHeaders(Map<String, String> orderableHeaders) {
244 _orderableHeaders = orderableHeaders;
245 }
246
247 public void setRow(ResultRow row) {
248 _resultRow = row;
249 }
250
251 public void setRowVar(String rowVar) {
252 _rowVar = rowVar;
253 }
254
255 @Override
256 public void setServletContext(ServletContext servletContext) {
257 }
258
259 public void setStringKey(boolean stringKey) {
260 _stringKey = stringKey;
261 }
262
263 protected void processRow() {
264 Object model = _results.get(_rowIndex);
265
266 if (isEscapedModel()) {
267 if (model instanceof BaseModel) {
268 BaseModel<?> baseModel = (BaseModel<?>)model;
269
270 model = baseModel.toEscapedModel();
271 }
272 else if (model instanceof RepositoryModel) {
273 RepositoryModel<?> repositoryModel = (RepositoryModel<?>)model;
274
275 model = repositoryModel.toEscapedModel();
276 }
277 }
278
279 if (Validator.isNull(_keyProperty)) {
280 String primaryKey = String.valueOf(model);
281
282 _resultRow = new ResultRow(model, primaryKey, _rowIndex, _bold);
283 }
284 else if (isStringKey()) {
285 String primaryKey = BeanPropertiesUtil.getString(
286 model, _keyProperty);
287
288 _resultRow = new ResultRow(model, primaryKey, _rowIndex, _bold);
289 }
290 else {
291 Object primaryKey = BeanPropertiesUtil.getObject(
292 model, _keyProperty);
293
294 _resultRow = new ResultRow(
295 model, String.valueOf(primaryKey), _rowIndex, _bold);
296 }
297
298 pageContext.setAttribute(_indexVar, _rowIndex);
299 pageContext.setAttribute(_modelVar, model);
300 pageContext.setAttribute(_rowVar, _resultRow);
301 }
302
303 private boolean _bold;
304 private String _className;
305 private boolean _escapedModel;
306 private List<String> _headerNames;
307 private boolean _headerNamesAssigned;
308 private String _indexVar = DEFAULT_INDEX_VAR;
309 private String _keyProperty;
310 private String _modelVar = DEFAULT_MODEL_VAR;
311 private Map<String, String> _orderableHeaders;
312 private ResultRow _resultRow;
313 private List<ResultRow> _resultRows;
314 private List<R> _results;
315 private int _rowIndex;
316 private String _rowVar = DEFAULT_ROW_VAR;
317 private boolean _stringKey;
318
319 }