1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.taglib.ui;
16  
17  import com.liferay.portal.kernel.bean.BeanPropertiesUtil;
18  import com.liferay.portal.kernel.dao.search.ResultRow;
19  import com.liferay.portal.kernel.dao.search.SearchEntry;
20  import com.liferay.portal.kernel.dao.search.TextSearchEntry;
21  import com.liferay.portal.kernel.language.LanguageUtil;
22  import com.liferay.portal.kernel.util.ServerDetector;
23  import com.liferay.portal.kernel.util.Validator;
24  
25  import java.util.List;
26  import java.util.Map;
27  
28  import javax.portlet.PortletURL;
29  
30  import javax.servlet.jsp.JspException;
31  import javax.servlet.jsp.JspTagException;
32  import javax.servlet.jsp.tagext.BodyContent;
33  
34  /**
35   * <a href="SearchContainerColumnTextTag.java.html"><b><i>View Source</i></b>
36   * </a>
37   *
38   * @author Raymond Augé
39   */
40  public class SearchContainerColumnTextTag extends SearchContainerColumnTag {
41  
42      public int doEndTag() {
43          try {
44              SearchContainerRowTag parentTag =
45                  (SearchContainerRowTag)findAncestorWithClass(
46                      this, SearchContainerRowTag.class);
47  
48              ResultRow row = parentTag.getRow();
49  
50              if (Validator.isNotNull(_property)) {
51                  _value = String.valueOf(
52                      BeanPropertiesUtil.getObject(row.getObject(), _property));
53              }
54              else if (Validator.isNotNull(_buffer)) {
55                  _value = _sb.toString();
56              }
57              else if (_value == null) {
58                  BodyContent bodyContent = getBodyContent();
59  
60                  if (bodyContent != null) {
61                      _value = bodyContent.getString();
62                  }
63              }
64  
65              if (_translate) {
66                  _value = LanguageUtil.get(pageContext, _value);
67              }
68  
69              if (index <= -1) {
70                  index = row.getEntries().size();
71              }
72  
73              if (row.isRestricted()) {
74                  _href = null;
75              }
76  
77              row.addText(
78                  index,
79                  new TextSearchEntry(
80                      getAlign(), getValign(), getColspan(), getValue(),
81                      (String)getHref(), getTarget(), getTitle()));
82  
83              return EVAL_PAGE;
84          }
85          finally {
86              if (!ServerDetector.isResin()) {
87                  align = SearchEntry.DEFAULT_ALIGN;
88                  _buffer = null;
89                  colspan = SearchEntry.DEFAULT_COLSPAN;
90                  _href = null;
91                  index = -1;
92                  name = null;
93                  _orderable = false;
94                  _orderableProperty = null;
95                  _property = null;
96                  _target = null;
97                  _title = null;
98                  _translate = false;
99                  valign = SearchEntry.DEFAULT_VALIGN;
100                 _value = null;
101             }
102         }
103     }
104 
105     public int doStartTag() throws JspException {
106         if (_orderable && Validator.isNull(_orderableProperty)) {
107             _orderableProperty = name;
108         }
109 
110         SearchContainerRowTag parentRowTag = (SearchContainerRowTag)
111             findAncestorWithClass(this, SearchContainerRowTag.class);
112 
113         if (parentRowTag == null) {
114             throw new JspTagException(
115                 "Requires liferay-ui:search-container-row");
116         }
117 
118         if (!parentRowTag.isHeaderNamesAssigned()) {
119             List<String> headerNames = parentRowTag.getHeaderNames();
120 
121             String name = getName();
122 
123             if (Validator.isNull(name) && Validator.isNotNull(_property)) {
124                 name = _property;
125             }
126 
127             headerNames.add(name);
128 
129             if (_orderable) {
130                 Map<String,String> orderableHeaders =
131                     parentRowTag.getOrderableHeaders();
132 
133                 if (Validator.isNotNull(_orderableProperty)) {
134                     orderableHeaders.put(name, _orderableProperty);
135                 }
136                 else if (Validator.isNotNull(_property)) {
137                     orderableHeaders.put(name, _property);
138                 }
139                 else if (Validator.isNotNull(name)) {
140                     orderableHeaders.put(name, name);
141                 }
142             }
143         }
144 
145         if (Validator.isNotNull(_property)) {
146             return SKIP_BODY;
147         }
148         else if (Validator.isNotNull(_buffer)) {
149             _sb = new StringBuilder();
150 
151             pageContext.setAttribute(_buffer, _sb);
152 
153             return EVAL_BODY_INCLUDE;
154         }
155         else if (Validator.isNull(_value)) {
156             return EVAL_BODY_BUFFERED;
157         }
158         else {
159             return SKIP_BODY;
160         }
161     }
162 
163     public String getBuffer() {
164         return _buffer;
165     }
166 
167     public Object getHref() {
168         if (Validator.isNotNull(_href) && (_href instanceof PortletURL)) {
169             _href = _href.toString();
170         }
171 
172         return _href;
173     }
174 
175     public String getOrderableProperty() {
176         return _orderableProperty;
177     }
178 
179     public String getProperty() {
180         return _property;
181     }
182 
183     public String getTarget() {
184         return _target;
185     }
186 
187     public String getTitle() {
188         return _title;
189     }
190 
191     public String getValue() {
192         return _value;
193     }
194 
195     public boolean isOrderable() {
196         return _orderable;
197     }
198 
199     public void setBuffer(String buffer) {
200         _buffer = buffer;
201     }
202 
203     public void setHref(Object href) {
204         _href = href;
205     }
206 
207     public void setOrderable(boolean orderable) {
208         _orderable = orderable;
209     }
210 
211     public void setOrderableProperty(String orderableProperty) {
212         _orderableProperty = orderableProperty;
213     }
214 
215     public void setProperty(String property) {
216         _property = property;
217     }
218 
219     public void setTarget(String target) {
220         _target = target;
221     }
222 
223     public void setTitle(String title) {
224         _title = title;
225     }
226 
227     public void setTranslate(boolean translate) {
228         _translate = translate;
229     }
230 
231     public void setValue(String value) {
232         _value = value;
233     }
234 
235     private String _buffer;
236     private Object _href;
237     private boolean _orderable;
238     private String _orderableProperty;
239     private String _property;
240     private StringBuilder _sb;
241     private String _target;
242     private String _title;
243     private boolean _translate;
244     private String _value;
245 
246 }