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.SearchEntry;
020    import com.liferay.portal.kernel.dao.search.TextSearchEntry;
021    import com.liferay.portal.kernel.language.LanguageUtil;
022    import com.liferay.portal.kernel.util.ServerDetector;
023    import com.liferay.portal.kernel.util.Validator;
024    
025    import java.util.List;
026    import java.util.Map;
027    
028    import javax.portlet.PortletURL;
029    
030    import javax.servlet.jsp.JspException;
031    import javax.servlet.jsp.JspTagException;
032    import javax.servlet.jsp.tagext.BodyContent;
033    import javax.servlet.jsp.tagext.BodyTag;
034    
035    /**
036     * @author Raymond Aug??
037     */
038    public class SearchContainerColumnTextTag<R>
039            extends SearchContainerColumnTag implements BodyTag {
040    
041            @Override
042            public int doEndTag() {
043                    try {
044                            SearchContainerRowTag<R> searchContainerRowTag =
045                                    (SearchContainerRowTag<R>)findAncestorWithClass(
046                                            this, SearchContainerRowTag.class);
047    
048                            ResultRow resultRow = searchContainerRowTag.getRow();
049    
050                            if (Validator.isNotNull(_property)) {
051                                    _value = String.valueOf(
052                                            BeanPropertiesUtil.getObject(
053                                                    resultRow.getObject(), _property));
054                            }
055                            else if (Validator.isNotNull(_buffer)) {
056                                    _value = _sb.toString();
057                            }
058                            else if (_value == null) {
059                                    BodyContent bodyContent = getBodyContent();
060    
061                                    if (bodyContent != null) {
062                                            _value = bodyContent.getString();
063                                    }
064                                    else {
065                                            Object object = BeanPropertiesUtil.getObject(
066                                                    resultRow.getObject(), getName());
067    
068                                            _value = String.valueOf(object);
069                                    }
070                            }
071    
072                            if (_translate) {
073                                    _value = LanguageUtil.get(pageContext, _value);
074                            }
075    
076                            if (index <= -1) {
077                                    List<SearchEntry> searchEntries = resultRow.getEntries();
078    
079                                    index = searchEntries.size();
080                            }
081    
082                            if (resultRow.isRestricted()) {
083                                    _href = null;
084                            }
085    
086                            TextSearchEntry textSearchEntry = new TextSearchEntry();
087    
088                            textSearchEntry.setAlign(getAlign());
089                            textSearchEntry.setColspan(getColspan());
090                            textSearchEntry.setCssClass(getCssClass());
091                            textSearchEntry.setHref((String)getHref());
092                            textSearchEntry.setName(getValue());
093                            textSearchEntry.setTarget(getTarget());
094                            textSearchEntry.setTitle(getTitle());
095                            textSearchEntry.setValign(getValign());
096    
097                            resultRow.addSearchEntry(index, textSearchEntry);
098    
099                            return EVAL_PAGE;
100                    }
101                    finally {
102                            index = -1;
103                            _value = null;
104    
105                            if (!ServerDetector.isResin()) {
106                                    align = SearchEntry.DEFAULT_ALIGN;
107                                    _buffer = null;
108                                    colspan = SearchEntry.DEFAULT_COLSPAN;
109                                    cssClass = SearchEntry.DEFAULT_CSS_CLASS;
110                                    _href = null;
111                                    name = null;
112                                    _orderable = false;
113                                    _orderableProperty = null;
114                                    _property = null;
115                                    _sb = null;
116                                    _target = null;
117                                    _title = null;
118                                    _translate = false;
119                                    valign = SearchEntry.DEFAULT_VALIGN;
120                            }
121                    }
122            }
123    
124            @Override
125            public int doStartTag() throws JspException {
126                    if (_orderable && Validator.isNull(_orderableProperty)) {
127                            _orderableProperty = name;
128                    }
129    
130                    SearchContainerRowTag<R> searchContainerRowTag =
131                            (SearchContainerRowTag<R>)findAncestorWithClass(
132                                    this, SearchContainerRowTag.class);
133    
134                    if (searchContainerRowTag == null) {
135                            throw new JspTagException(
136                                    "Requires liferay-ui:search-container-row");
137                    }
138    
139                    if (!searchContainerRowTag.isHeaderNamesAssigned()) {
140                            List<String> headerNames = searchContainerRowTag.getHeaderNames();
141    
142                            String name = getName();
143    
144                            if (Validator.isNull(name) && Validator.isNotNull(_property)) {
145                                    name = _property;
146                            }
147    
148                            headerNames.add(name);
149    
150                            if (_orderable) {
151                                    Map<String, String> orderableHeaders =
152                                            searchContainerRowTag.getOrderableHeaders();
153    
154                                    if (Validator.isNotNull(_orderableProperty)) {
155                                            orderableHeaders.put(name, _orderableProperty);
156                                    }
157                                    else if (Validator.isNotNull(_property)) {
158                                            orderableHeaders.put(name, _property);
159                                    }
160                                    else if (Validator.isNotNull(name)) {
161                                            orderableHeaders.put(name, name);
162                                    }
163                            }
164                    }
165    
166                    if (Validator.isNotNull(_property)) {
167                            return SKIP_BODY;
168                    }
169                    else if (Validator.isNotNull(_buffer)) {
170                            _sb = new StringBuilder();
171    
172                            pageContext.setAttribute(_buffer, _sb);
173    
174                            return EVAL_BODY_INCLUDE;
175                    }
176                    else if (Validator.isNull(_value)) {
177                            return EVAL_BODY_BUFFERED;
178                    }
179                    else {
180                            return SKIP_BODY;
181                    }
182            }
183    
184            public String getBuffer() {
185                    return _buffer;
186            }
187    
188            public Object getHref() {
189                    if (Validator.isNotNull(_href) && (_href instanceof PortletURL)) {
190                            _href = _href.toString();
191                    }
192    
193                    return _href;
194            }
195    
196            public String getOrderableProperty() {
197                    return _orderableProperty;
198            }
199    
200            public String getProperty() {
201                    return _property;
202            }
203    
204            public String getTarget() {
205                    return _target;
206            }
207    
208            public String getTitle() {
209                    return _title;
210            }
211    
212            public String getValue() {
213                    return _value;
214            }
215    
216            public boolean isOrderable() {
217                    return _orderable;
218            }
219    
220            public void setBuffer(String buffer) {
221                    _buffer = buffer;
222            }
223    
224            public void setHref(Object href) {
225                    _href = href;
226            }
227    
228            public void setOrderable(boolean orderable) {
229                    _orderable = orderable;
230            }
231    
232            public void setOrderableProperty(String orderableProperty) {
233                    _orderableProperty = orderableProperty;
234            }
235    
236            public void setProperty(String property) {
237                    _property = property;
238            }
239    
240            public void setTarget(String target) {
241                    _target = target;
242            }
243    
244            public void setTitle(String title) {
245                    _title = title;
246            }
247    
248            public void setTranslate(boolean translate) {
249                    _translate = translate;
250            }
251    
252            public void setValue(String value) {
253                    _value = value;
254            }
255    
256            private String _buffer;
257            private Object _href;
258            private boolean _orderable;
259            private String _orderableProperty;
260            private String _property;
261            private StringBuilder _sb;
262            private String _target;
263            private String _title;
264            private boolean _translate;
265            private String _value;
266    
267    }