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