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