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