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                            if (!ServerDetector.isResin()) {
084                                    align = SearchEntry.DEFAULT_ALIGN;
085                                    _buffer = null;
086                                    colspan = SearchEntry.DEFAULT_COLSPAN;
087                                    _href = null;
088                                    index = -1;
089                                    name = null;
090                                    _orderable = false;
091                                    _orderableProperty = null;
092                                    _property = null;
093                                    _target = null;
094                                    _title = null;
095                                    _translate = false;
096                                    valign = SearchEntry.DEFAULT_VALIGN;
097                                    _value = null;
098                            }
099                    }
100            }
101    
102            public int doStartTag() throws JspException {
103                    if (_orderable && Validator.isNull(_orderableProperty)) {
104                            _orderableProperty = name;
105                    }
106    
107                    SearchContainerRowTag parentRowTag = (SearchContainerRowTag)
108                            findAncestorWithClass(this, SearchContainerRowTag.class);
109    
110                    if (parentRowTag == null) {
111                            throw new JspTagException(
112                                    "Requires liferay-ui:search-container-row");
113                    }
114    
115                    if (!parentRowTag.isHeaderNamesAssigned()) {
116                            List<String> headerNames = parentRowTag.getHeaderNames();
117    
118                            String name = getName();
119    
120                            if (Validator.isNull(name) && Validator.isNotNull(_property)) {
121                                    name = _property;
122                            }
123    
124                            headerNames.add(name);
125    
126                            if (_orderable) {
127                                    Map<String,String> orderableHeaders =
128                                            parentRowTag.getOrderableHeaders();
129    
130                                    if (Validator.isNotNull(_orderableProperty)) {
131                                            orderableHeaders.put(name, _orderableProperty);
132                                    }
133                                    else if (Validator.isNotNull(_property)) {
134                                            orderableHeaders.put(name, _property);
135                                    }
136                                    else if (Validator.isNotNull(name)) {
137                                            orderableHeaders.put(name, name);
138                                    }
139                            }
140                    }
141    
142                    if (Validator.isNotNull(_property)) {
143                            return SKIP_BODY;
144                    }
145                    else if (Validator.isNotNull(_buffer)) {
146                            _sb = new StringBuilder();
147    
148                            pageContext.setAttribute(_buffer, _sb);
149    
150                            return EVAL_BODY_INCLUDE;
151                    }
152                    else if (Validator.isNull(_value)) {
153                            return EVAL_BODY_BUFFERED;
154                    }
155                    else {
156                            return SKIP_BODY;
157                    }
158            }
159    
160            public String getBuffer() {
161                    return _buffer;
162            }
163    
164            public Object getHref() {
165                    if (Validator.isNotNull(_href) && (_href instanceof PortletURL)) {
166                            _href = _href.toString();
167                    }
168    
169                    return _href;
170            }
171    
172            public String getOrderableProperty() {
173                    return _orderableProperty;
174            }
175    
176            public String getProperty() {
177                    return _property;
178            }
179    
180            public String getTarget() {
181                    return _target;
182            }
183    
184            public String getTitle() {
185                    return _title;
186            }
187    
188            public String getValue() {
189                    return _value;
190            }
191    
192            public boolean isOrderable() {
193                    return _orderable;
194            }
195    
196            public void setBuffer(String buffer) {
197                    _buffer = buffer;
198            }
199    
200            public void setHref(Object href) {
201                    _href = href;
202            }
203    
204            public void setOrderable(boolean orderable) {
205                    _orderable = orderable;
206            }
207    
208            public void setOrderableProperty(String orderableProperty) {
209                    _orderableProperty = orderableProperty;
210            }
211    
212            public void setProperty(String property) {
213                    _property = property;
214            }
215    
216            public void setTarget(String target) {
217                    _target = target;
218            }
219    
220            public void setTitle(String title) {
221                    _title = title;
222            }
223    
224            public void setTranslate(boolean translate) {
225                    _translate = translate;
226            }
227    
228            public void setValue(String value) {
229                    _value = value;
230            }
231    
232            private String _buffer;
233            private Object _href;
234            private boolean _orderable;
235            private String _orderableProperty;
236            private String _property;
237            private StringBuilder _sb;
238            private String _target;
239            private String _title;
240            private boolean _translate;
241            private String _value;
242    
243    }