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