001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 row = searchContainerRowTag.getRow();
049    
050                            if (Validator.isNotNull(_property)) {
051                                    _value = String.valueOf(
052                                            BeanPropertiesUtil.getObject(row.getObject(), _property));
053                            }
054                            else if (Validator.isNotNull(_buffer)) {
055                                    _value = _sb.toString();
056                            }
057                            else if (_value == null) {
058                                    BodyContent bodyContent = getBodyContent();
059    
060                                    if (bodyContent != null) {
061                                            _value = bodyContent.getString();
062                                    }
063                                    else {
064                                            Object object = BeanPropertiesUtil.getObject(
065                                                    row.getObject(), getName());
066    
067                                            _value = String.valueOf(object);
068                                    }
069                            }
070    
071                            if (_translate) {
072                                    _value = LanguageUtil.get(pageContext, _value);
073                            }
074    
075                            if (index <= -1) {
076                                    index = row.getEntries().size();
077                            }
078    
079                            if (row.isRestricted()) {
080                                    _href = null;
081                            }
082    
083                            TextSearchEntry textSearchEntry = new TextSearchEntry();
084    
085                            textSearchEntry.setAlign(getAlign());
086                            textSearchEntry.setColspan(getColspan());
087                            textSearchEntry.setCssClass(getCssClass());
088                            textSearchEntry.setHref((String)getHref());
089                            textSearchEntry.setName(getValue());
090                            textSearchEntry.setTarget(getTarget());
091                            textSearchEntry.setTitle(getTitle());
092                            textSearchEntry.setValign(getValign());
093    
094                            row.addSearchEntry(index, textSearchEntry);
095    
096                            return EVAL_PAGE;
097                    }
098                    finally {
099                            index = -1;
100                            _value = null;
101    
102                            if (!ServerDetector.isResin()) {
103                                    align = SearchEntry.DEFAULT_ALIGN;
104                                    _buffer = null;
105                                    colspan = SearchEntry.DEFAULT_COLSPAN;
106                                    cssClass = SearchEntry.DEFAULT_CSS_CLASS;
107                                    _href = null;
108                                    name = null;
109                                    _orderable = false;
110                                    _orderableProperty = null;
111                                    _property = null;
112                                    _sb = null;
113                                    _target = null;
114                                    _title = null;
115                                    _translate = false;
116                                    valign = SearchEntry.DEFAULT_VALIGN;
117                            }
118                    }
119            }
120    
121            @Override
122            public int doStartTag() throws JspException {
123                    if (_orderable && Validator.isNull(_orderableProperty)) {
124                            _orderableProperty = name;
125                    }
126    
127                    SearchContainerRowTag<R> searchContainerRowTag =
128                            (SearchContainerRowTag<R>)findAncestorWithClass(
129                                    this, SearchContainerRowTag.class);
130    
131                    if (searchContainerRowTag == null) {
132                            throw new JspTagException(
133                                    "Requires liferay-ui:search-container-row");
134                    }
135    
136                    if (!searchContainerRowTag.isHeaderNamesAssigned()) {
137                            List<String> headerNames = searchContainerRowTag.getHeaderNames();
138    
139                            String name = getName();
140    
141                            if (Validator.isNull(name) && Validator.isNotNull(_property)) {
142                                    name = _property;
143                            }
144    
145                            headerNames.add(name);
146    
147                            if (_orderable) {
148                                    Map<String,String> orderableHeaders =
149                                            searchContainerRowTag.getOrderableHeaders();
150    
151                                    if (Validator.isNotNull(_orderableProperty)) {
152                                            orderableHeaders.put(name, _orderableProperty);
153                                    }
154                                    else if (Validator.isNotNull(_property)) {
155                                            orderableHeaders.put(name, _property);
156                                    }
157                                    else if (Validator.isNotNull(name)) {
158                                            orderableHeaders.put(name, name);
159                                    }
160                            }
161                    }
162    
163                    if (Validator.isNotNull(_property)) {
164                            return SKIP_BODY;
165                    }
166                    else if (Validator.isNotNull(_buffer)) {
167                            _sb = new StringBuilder();
168    
169                            pageContext.setAttribute(_buffer, _sb);
170    
171                            return EVAL_BODY_INCLUDE;
172                    }
173                    else if (Validator.isNull(_value)) {
174                            return EVAL_BODY_BUFFERED;
175                    }
176                    else {
177                            return SKIP_BODY;
178                    }
179            }
180    
181            public String getBuffer() {
182                    return _buffer;
183            }
184    
185            public Object getHref() {
186                    if (Validator.isNotNull(_href) && (_href instanceof PortletURL)) {
187                            _href = _href.toString();
188                    }
189    
190                    return _href;
191            }
192    
193            public String getOrderableProperty() {
194                    return _orderableProperty;
195            }
196    
197            public String getProperty() {
198                    return _property;
199            }
200    
201            public String getTarget() {
202                    return _target;
203            }
204    
205            public String getTitle() {
206                    return _title;
207            }
208    
209            public String getValue() {
210                    return _value;
211            }
212    
213            public boolean isOrderable() {
214                    return _orderable;
215            }
216    
217            public void setBuffer(String buffer) {
218                    _buffer = buffer;
219            }
220    
221            public void setHref(Object href) {
222                    _href = href;
223            }
224    
225            public void setOrderable(boolean orderable) {
226                    _orderable = orderable;
227            }
228    
229            public void setOrderableProperty(String orderableProperty) {
230                    _orderableProperty = orderableProperty;
231            }
232    
233            public void setProperty(String property) {
234                    _property = property;
235            }
236    
237            public void setTarget(String target) {
238                    _target = target;
239            }
240    
241            public void setTitle(String title) {
242                    _title = title;
243            }
244    
245            public void setTranslate(boolean translate) {
246                    _translate = translate;
247            }
248    
249            public void setValue(String value) {
250                    _value = value;
251            }
252    
253            private String _buffer;
254            private Object _href;
255            private boolean _orderable;
256            private String _orderableProperty;
257            private String _property;
258            private StringBuilder _sb;
259            private String _target;
260            private String _title;
261            private boolean _translate;
262            private String _value;
263    
264    }