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