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                            List<String> headerNames = _searchContainer.getHeaderNames();
077    
078                            if ((headerNames == null) || headerNames.isEmpty()) {
079                                    _searchContainer.setHeaderNames(_headerNames);
080                                    _searchContainer.setOrderableHeaders(_orderableHeaders);
081                            }
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                            _rowIdProperty = null;
119                            _rowVar = DEFAULT_ROW_VAR;
120                            _stringKey = false;
121                    }
122    
123                    return EVAL_PAGE;
124            }
125    
126            @Override
127            public int doStartTag() throws JspException {
128                    SearchContainerTag<R> searchContainerTag =
129                            (SearchContainerTag<R>)findAncestorWithClass(
130                                    this, SearchContainerTag.class);
131    
132                    if (searchContainerTag == null) {
133                            throw new JspException("Requires liferay-ui:search-container");
134                    }
135                    else if (!searchContainerTag.isHasResults()) {
136                            throw new JspException(
137                                    "Requires liferay-ui:search-container-results");
138                    }
139    
140                    _searchContainer = searchContainerTag.getSearchContainer();
141    
142                    _searchContainer.setClassName(_className);
143    
144                    _resultRows = _searchContainer.getResultRows();
145                    _results = _searchContainer.getResults();
146    
147                    if ((_results != null) && !_results.isEmpty()) {
148                            processRow();
149    
150                            return EVAL_BODY_INCLUDE;
151                    }
152                    else {
153                            return SKIP_BODY;
154                    }
155            }
156    
157            public String getClassName() {
158                    return _className;
159            }
160    
161            public List<String> getHeaderNames() {
162                    if (_headerNames == null) {
163                            _headerNames = new ArrayList<String>();
164                    }
165    
166                    return _headerNames;
167            }
168    
169            public String getIndexVar() {
170                    return _indexVar;
171            }
172    
173            public String getKeyProperty() {
174                    return _keyProperty;
175            }
176    
177            public String getModelVar() {
178                    return _modelVar;
179            }
180    
181            public Map<String, String> getOrderableHeaders() {
182                    if (_orderableHeaders == null) {
183                            _orderableHeaders = new LinkedHashMap<String, String>();
184                    }
185    
186                    return _orderableHeaders;
187            }
188    
189            public ResultRow getRow() {
190                    return _resultRow;
191            }
192    
193            public String getRowVar() {
194                    return _rowVar;
195            }
196    
197            public boolean isBold() {
198                    return _bold;
199            }
200    
201            public boolean isEscapedModel() {
202                    return _escapedModel;
203            }
204    
205            public boolean isHeaderNamesAssigned() {
206                    return _headerNamesAssigned;
207            }
208    
209            public boolean isStringKey() {
210                    return _stringKey;
211            }
212    
213            public void setBold(boolean bold) {
214                    _bold = bold;
215            }
216    
217            public void setClassName(String className) {
218                    _className = className;
219            }
220    
221            public void setEscapedModel(boolean escapedModel) {
222                    _escapedModel = escapedModel;
223            }
224    
225            public void setHeaderNames(List<String> headerNames) {
226                    _headerNames = headerNames;
227            }
228    
229            public void setHeaderNamesAssigned(boolean headerNamesAssigned) {
230                    _headerNamesAssigned = headerNamesAssigned;
231            }
232    
233            public void setIndexVar(String indexVar) {
234                    _indexVar = indexVar;
235            }
236    
237            public void setKeyProperty(String keyProperty) {
238                    _keyProperty = keyProperty;
239            }
240    
241            public void setModelVar(String var) {
242                    _modelVar = var;
243            }
244    
245            public void setOrderableHeaders(Map<String, String> orderableHeaders) {
246                    _orderableHeaders = orderableHeaders;
247            }
248    
249            public void setRow(ResultRow row) {
250                    _resultRow = row;
251            }
252    
253            public void setRowIdProperty(String rowIdProperty) {
254                    _rowIdProperty = rowIdProperty;
255            }
256    
257            public void setRowVar(String rowVar) {
258                    _rowVar = rowVar;
259            }
260    
261            public void setStringKey(boolean stringKey) {
262                    _stringKey = stringKey;
263            }
264    
265            protected void processRow() {
266                    Object model = _results.get(_rowIndex);
267    
268                    if (isEscapedModel()) {
269                            if (model instanceof BaseModel) {
270                                    BaseModel<?> baseModel = (BaseModel<?>)model;
271    
272                                    model = baseModel.toEscapedModel();
273                            }
274                            else if (model instanceof RepositoryModel) {
275                                    RepositoryModel<?> repositoryModel = (RepositoryModel<?>)model;
276    
277                                    model = repositoryModel.toEscapedModel();
278                            }
279                    }
280    
281                    String primaryKey = null;
282    
283                    if (Validator.isNull(_keyProperty)) {
284                            primaryKey = String.valueOf(model);
285                    }
286                    else if (isStringKey()) {
287                            primaryKey = BeanPropertiesUtil.getString(model, _keyProperty);
288                    }
289                    else {
290                            Object primaryKeyObj = BeanPropertiesUtil.getObject(
291                                    model, _keyProperty);
292    
293                            primaryKey = String.valueOf(primaryKeyObj);
294                    }
295    
296                    String rowId = null;
297    
298                    if (Validator.isNull(_rowIdProperty)) {
299                            rowId = String.valueOf(_rowIndex + 1);
300                    }
301                    else {
302                            Object rowIdObj = BeanPropertiesUtil.getObject(
303                                    model, _rowIdProperty);
304    
305                            if (Validator.isNull(rowIdObj)) {
306                                    rowId = String.valueOf(_rowIndex + 1);
307                            }
308                            else {
309                                    rowId = FriendlyURLNormalizerUtil.normalize(
310                                            String.valueOf(rowIdObj), _friendlyURLPattern);
311                            }
312                    }
313    
314                    _resultRow = new ResultRow(rowId, model, primaryKey, _rowIndex, _bold);
315    
316                    pageContext.setAttribute(_indexVar, _rowIndex);
317                    pageContext.setAttribute(_modelVar, model);
318                    pageContext.setAttribute(_rowVar, _resultRow);
319            }
320    
321            private static Pattern _friendlyURLPattern = Pattern.compile("[^a-z0-9_-]");
322    
323            private boolean _bold;
324            private String _className;
325            private boolean _escapedModel;
326            private List<String> _headerNames;
327            private boolean _headerNamesAssigned;
328            private String _indexVar = DEFAULT_INDEX_VAR;
329            private String _keyProperty;
330            private String _modelVar = DEFAULT_MODEL_VAR;
331            private Map<String, String> _orderableHeaders;
332            private ResultRow _resultRow;
333            private List<ResultRow> _resultRows;
334            private List<R> _results;
335            private String _rowIdProperty;
336            private int _rowIndex;
337            private String _rowVar = DEFAULT_ROW_VAR;
338            private SearchContainer<R> _searchContainer;
339            private boolean _stringKey;
340    
341    }