001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
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    
033    import javax.servlet.ServletContext;
034    import javax.servlet.jsp.JspException;
035    
036    /**
037     * @author Raymond Augé
038     */
039    public class SearchContainerRowTag extends ParamAndPropertyAncestorTagImpl {
040    
041            public static final String DEFAULT_INDEX_VAR = "index";
042    
043            public static final String DEFAULT_MODEL_VAR = "model";
044    
045            public static final String DEFAULT_ROW_VAR = "row";
046    
047            public void addParam(String name, String value) {
048                    if (name.equals("className")) {
049                            _row.setClassName(value);
050                    }
051                    else if (name.equals("classHoverName")) {
052                            _row.setClassHoverName(value);
053                    }
054                    else if (name.equals("restricted")) {
055                            _row.setRestricted(GetterUtil.getBoolean(value, false));
056                    }
057                    else {
058                            Object obj = pageContext.getAttribute(value);
059    
060                            if (obj == null) {
061                                    obj = value;
062                            }
063    
064                            _row.setParameter(name, obj);
065                    }
066            }
067    
068            public int doAfterBody() throws JspException {
069                    if (!_headerNamesAssigned) {
070                            SearchContainerTag parentTag =
071                                    (SearchContainerTag)findAncestorWithClass(
072                                            this, SearchContainerTag.class);
073    
074                            SearchContainer searchContainer = parentTag.getSearchContainer();
075    
076                            searchContainer.setHeaderNames(_headerNames);
077                            searchContainer.setOrderableHeaders(_orderableHeaders);
078    
079                            _headerNamesAssigned = true;
080                    }
081    
082                    _resultRows.add(_row);
083    
084                    _rowIndex++;
085    
086                    if (_rowIndex < (_results.size())) {
087                            processRow();
088    
089                            return EVAL_BODY_AGAIN;
090                    }
091                    else {
092                            return SKIP_BODY;
093                    }
094            }
095    
096            public int doEndTag() {
097                    _headerNames = null;
098                    _headerNamesAssigned = false;
099                    _resultRows = null;
100                    _rowIndex = 0;
101                    _row = null;
102    
103                    if (!ServerDetector.isResin()) {
104                            _bold = false;
105                            _className = null;
106                            _escapedModel = false;
107                            _indexVar = DEFAULT_INDEX_VAR;
108                            _keyProperty = null;
109                            _modelVar = DEFAULT_MODEL_VAR;
110                            _orderableHeaders = null;
111                            _rowVar = DEFAULT_ROW_VAR;
112                            _stringKey = false;
113                    }
114    
115                    return EVAL_PAGE;
116            }
117    
118            public int doStartTag() throws JspException {
119                    SearchContainerTag parentTag =
120                            (SearchContainerTag)findAncestorWithClass(
121                                    this, SearchContainerTag.class);
122    
123                    if (parentTag == null) {
124                            throw new JspException("Requires liferay-ui:search-container");
125                    }
126                    else if (!parentTag.isHasResults()) {
127                            throw new JspException(
128                                    "Requires liferay-ui:search-container-results");
129                    }
130    
131                    _resultRows = parentTag.getSearchContainer().getResultRows();
132                    _results = parentTag.getSearchContainer().getResults();
133    
134                    if ((_results != null) && (!_results.isEmpty())) {
135                            processRow();
136    
137                            return EVAL_BODY_INCLUDE;
138                    }
139                    else {
140                            return SKIP_BODY;
141                    }
142            }
143    
144            public String getClassName() {
145                    return _className;
146            }
147    
148            public List<String> getHeaderNames() {
149                    if (_headerNames == null) {
150                            _headerNames = new ArrayList<String>();
151                    }
152    
153                    return _headerNames;
154            }
155    
156            public String getIndexVar() {
157                    return _indexVar;
158            }
159    
160            public String getKeyProperty() {
161                    return _keyProperty;
162            }
163    
164            public String getModelVar() {
165                    return _modelVar;
166            }
167    
168            public Map<String, String> getOrderableHeaders() {
169                    if (_orderableHeaders == null) {
170                            _orderableHeaders = new LinkedHashMap<String, String>();
171                    }
172    
173                    return _orderableHeaders;
174            }
175    
176            public ResultRow getRow() {
177                    return _row;
178            }
179    
180            public String getRowVar() {
181                    return _rowVar;
182            }
183    
184            public boolean isBold() {
185                    return _bold;
186            }
187    
188            public boolean isEscapedModel() {
189                    return _escapedModel;
190            }
191    
192            public boolean isHeaderNamesAssigned() {
193                    return _headerNamesAssigned;
194            }
195    
196            public boolean isStringKey() {
197                    return _stringKey;
198            }
199    
200            public void setBold(boolean bold) {
201                    _bold = bold;
202            }
203    
204            public void setClassName(String className) {
205                    _className = className;
206            }
207    
208            public void setEscapedModel(boolean escapedModel) {
209                    _escapedModel = escapedModel;
210            }
211    
212            public void setHeaderNames(List<String> headerNames) {
213                    _headerNames = headerNames;
214            }
215    
216            public void setHeaderNamesAssigned(boolean headerNamesAssigned) {
217                    _headerNamesAssigned = headerNamesAssigned;
218            }
219    
220            public void setIndexVar(String indexVar) {
221                    _indexVar = indexVar;
222            }
223    
224            public void setKeyProperty(String keyProperty) {
225                    _keyProperty = keyProperty;
226            }
227    
228            public void setModelVar(String var) {
229                    _modelVar = var;
230            }
231    
232            public void setOrderableHeaders(Map<String, String> orderableHeaders) {
233                    _orderableHeaders = orderableHeaders;
234            }
235    
236            public void setRow(ResultRow row) {
237                    _row = row;
238            }
239    
240            public void setRowVar(String rowVar) {
241                    _rowVar = rowVar;
242            }
243    
244            public void setServletContext(ServletContext servletContext) {
245                    _servletContext = servletContext;
246            }
247    
248            public void setStringKey(boolean stringKey) {
249                    _stringKey = stringKey;
250            }
251    
252            protected void processRow() throws JspException {
253                    Object model = _results.get(_rowIndex);
254    
255                    if (isEscapedModel()) {
256                            BaseModel baseModel = (BaseModel)model;
257    
258                            model = baseModel.toEscapedModel();
259                    }
260    
261                    if (_log.isDebugEnabled()) {
262                            _log.debug(BeanPropertiesUtil.getBoolean(model, "escapedModel"));
263                    }
264    
265                    if (Validator.isNull(_keyProperty)) {
266                            String primaryKey = String.valueOf(model);
267    
268                            _row = new ResultRow(model, primaryKey, _rowIndex, _bold);
269                    }
270                    else if (isStringKey()) {
271                            String primaryKey = BeanPropertiesUtil.getString(
272                                    model, _keyProperty);
273    
274                            _row = new ResultRow(model, primaryKey, _rowIndex, _bold);
275                    }
276                    else {
277                            Object primaryKey = BeanPropertiesUtil.getObject(
278                                    model, _keyProperty);
279    
280                            _row = new ResultRow(
281                                    model, String.valueOf(primaryKey), _rowIndex, _bold);
282                    }
283    
284                    pageContext.setAttribute(_indexVar, _rowIndex);
285                    pageContext.setAttribute(_modelVar, model);
286                    pageContext.setAttribute(_rowVar, _row);
287            }
288    
289            private static Log _log = LogFactoryUtil.getLog(
290                    SearchContainerRowTag.class);
291    
292            private boolean _bold;
293            private String _className;
294            private boolean _escapedModel;
295            private List<String> _headerNames;
296            private boolean _headerNamesAssigned;
297            private String _indexVar = DEFAULT_INDEX_VAR;
298            private String _keyProperty;
299            private String _modelVar = DEFAULT_MODEL_VAR;
300            private Map<String, String> _orderableHeaders;
301            private List _results;
302            private List<ResultRow> _resultRows;
303            private int _rowIndex;
304            private String _rowVar = DEFAULT_ROW_VAR;
305            private ResultRow _row;
306            private ServletContext _servletContext;
307            private boolean _stringKey = false;
308    
309    }