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