001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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.Validator;
025    import com.liferay.portal.model.BaseModel;
026    import com.liferay.taglib.util.ParamAndPropertyAncestorTagImpl;
027    
028    import java.util.ArrayList;
029    import java.util.LinkedHashMap;
030    import java.util.List;
031    import java.util.Map;
032    import java.util.regex.Pattern;
033    
034    import javax.servlet.jsp.JspException;
035    import javax.servlet.jsp.tagext.BodyTag;
036    
037    /**
038     * @author Raymond Aug??
039     */
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 && (_headerNames != null) &&
074                            !_headerNames.isEmpty()) {
075    
076                            _searchContainer.setHeaderNames(_headerNames);
077                            _searchContainer.setOrderableHeaders(_orderableHeaders);
078    
079                            _headerNamesAssigned = true;
080                    }
081    
082                    if (!_resultRow.isSkip()) {
083                            _resultRows.add(_resultRow);
084                    }
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            @Override
099            public int doEndTag() {
100                    _headerNames = null;
101                    _headerNamesAssigned = false;
102                    _resultRows = null;
103                    _rowIndex = 0;
104                    _resultRow = null;
105    
106                    if (!ServerDetector.isResin()) {
107                            _bold = false;
108                            _className = null;
109                            _escapedModel = false;
110                            _indexVar = DEFAULT_INDEX_VAR;
111                            _keyProperty = null;
112                            _modelVar = DEFAULT_MODEL_VAR;
113                            _orderableHeaders = null;
114                            _rowIdProperty = null;
115                            _rowVar = DEFAULT_ROW_VAR;
116                            _stringKey = false;
117                    }
118    
119                    return EVAL_PAGE;
120            }
121    
122            @Override
123            public int doStartTag() throws JspException {
124                    SearchContainerTag<R> searchContainerTag =
125                            (SearchContainerTag<R>)findAncestorWithClass(
126                                    this, SearchContainerTag.class);
127    
128                    if (searchContainerTag == null) {
129                            throw new JspException("Requires liferay-ui:search-container");
130                    }
131                    else if (!searchContainerTag.isHasResults()) {
132                            throw new JspException(
133                                    "Requires liferay-ui:search-container-results");
134                    }
135    
136                    _searchContainer = searchContainerTag.getSearchContainer();
137    
138                    _searchContainer.setClassName(_className);
139    
140                    _resultRows = _searchContainer.getResultRows();
141                    _results = _searchContainer.getResults();
142    
143                    if ((_results != null) && !_results.isEmpty()) {
144                            processRow();
145    
146                            return EVAL_BODY_INCLUDE;
147                    }
148                    else {
149                            return SKIP_BODY;
150                    }
151            }
152    
153            public String getClassName() {
154                    return _className;
155            }
156    
157            public List<String> getHeaderNames() {
158                    if (_headerNames == null) {
159                            _headerNames = new ArrayList<String>();
160                    }
161    
162                    return _headerNames;
163            }
164    
165            public String getIndexVar() {
166                    return _indexVar;
167            }
168    
169            public String getKeyProperty() {
170                    return _keyProperty;
171            }
172    
173            public String getModelVar() {
174                    return _modelVar;
175            }
176    
177            public Map<String, String> getOrderableHeaders() {
178                    if (_orderableHeaders == null) {
179                            _orderableHeaders = new LinkedHashMap<String, String>();
180                    }
181    
182                    return _orderableHeaders;
183            }
184    
185            public ResultRow getRow() {
186                    return _resultRow;
187            }
188    
189            public String getRowVar() {
190                    return _rowVar;
191            }
192    
193            public boolean isBold() {
194                    return _bold;
195            }
196    
197            public boolean isEscapedModel() {
198                    return _escapedModel;
199            }
200    
201            public boolean isHeaderNamesAssigned() {
202                    return _headerNamesAssigned;
203            }
204    
205            public boolean isStringKey() {
206                    return _stringKey;
207            }
208    
209            public void setBold(boolean bold) {
210                    _bold = bold;
211            }
212    
213            public void setClassName(String className) {
214                    _className = className;
215            }
216    
217            public void setEscapedModel(boolean escapedModel) {
218                    _escapedModel = escapedModel;
219            }
220    
221            public void setHeaderNames(List<String> headerNames) {
222                    _headerNames = headerNames;
223            }
224    
225            public void setHeaderNamesAssigned(boolean headerNamesAssigned) {
226                    _headerNamesAssigned = headerNamesAssigned;
227            }
228    
229            public void setIndexVar(String indexVar) {
230                    _indexVar = indexVar;
231            }
232    
233            public void setKeyProperty(String keyProperty) {
234                    _keyProperty = keyProperty;
235            }
236    
237            public void setModelVar(String var) {
238                    _modelVar = var;
239            }
240    
241            public void setOrderableHeaders(Map<String, String> orderableHeaders) {
242                    _orderableHeaders = orderableHeaders;
243            }
244    
245            public void setRow(ResultRow row) {
246                    _resultRow = row;
247            }
248    
249            public void setRowIdProperty(String rowIdProperty) {
250                    _rowIdProperty = rowIdProperty;
251            }
252    
253            public void setRowVar(String rowVar) {
254                    _rowVar = rowVar;
255            }
256    
257            public void setStringKey(boolean stringKey) {
258                    _stringKey = stringKey;
259            }
260    
261            protected void processRow() {
262                    Object model = _results.get(_rowIndex);
263    
264                    if (isEscapedModel()) {
265                            if (model instanceof BaseModel) {
266                                    BaseModel<?> baseModel = (BaseModel<?>)model;
267    
268                                    model = baseModel.toEscapedModel();
269                            }
270                            else if (model instanceof RepositoryModel) {
271                                    RepositoryModel<?> repositoryModel = (RepositoryModel<?>)model;
272    
273                                    model = repositoryModel.toEscapedModel();
274                            }
275                    }
276    
277                    String primaryKey = null;
278    
279                    if (Validator.isNull(_keyProperty)) {
280                            primaryKey = String.valueOf(model);
281                    }
282                    else if (isStringKey()) {
283                            primaryKey = BeanPropertiesUtil.getString(model, _keyProperty);
284                    }
285                    else {
286                            Object primaryKeyObj = BeanPropertiesUtil.getObject(
287                                    model, _keyProperty);
288    
289                            primaryKey = String.valueOf(primaryKeyObj);
290                    }
291    
292                    String rowId = null;
293    
294                    if (Validator.isNull(_rowIdProperty)) {
295                            rowId = String.valueOf(_rowIndex + 1);
296                    }
297                    else {
298                            Object rowIdObj = BeanPropertiesUtil.getObject(
299                                    model, _rowIdProperty);
300    
301                            if (Validator.isNull(rowIdObj)) {
302                                    rowId = String.valueOf(_rowIndex + 1);
303                            }
304                            else {
305                                    rowId = FriendlyURLNormalizerUtil.normalize(
306                                            String.valueOf(rowIdObj), _friendlyURLPattern);
307                            }
308                    }
309    
310                    _resultRow = new ResultRow(rowId, model, primaryKey, _rowIndex, _bold);
311    
312                    pageContext.setAttribute(_indexVar, _rowIndex);
313                    pageContext.setAttribute(_modelVar, model);
314                    pageContext.setAttribute(_rowVar, _resultRow);
315            }
316    
317            private static Pattern _friendlyURLPattern = Pattern.compile("[^a-z0-9_-]");
318    
319            private boolean _bold;
320            private String _className;
321            private boolean _escapedModel;
322            private List<String> _headerNames;
323            private boolean _headerNamesAssigned;
324            private String _indexVar = DEFAULT_INDEX_VAR;
325            private String _keyProperty;
326            private String _modelVar = DEFAULT_MODEL_VAR;
327            private Map<String, String> _orderableHeaders;
328            private ResultRow _resultRow;
329            private List<ResultRow> _resultRows;
330            private List<R> _results;
331            private String _rowIdProperty;
332            private int _rowIndex;
333            private String _rowVar = DEFAULT_ROW_VAR;
334            private SearchContainer<R> _searchContainer;
335            private boolean _stringKey;
336    
337    }