1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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<R> extends SearchContainerColumnTag {
41  
42      public int doEndTag() {
43          try {
44              SearchContainerRowTag<R> parentTag =
45                  (SearchContainerRowTag<R>)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<R> parentRowTag =
111             (SearchContainerRowTag<R>)findAncestorWithClass(
112                 this, SearchContainerRowTag.class);
113 
114         if (parentRowTag == null) {
115             throw new JspTagException(
116                 "Requires liferay-ui:search-container-row");
117         }
118 
119         if (!parentRowTag.isHeaderNamesAssigned()) {
120             List<String> headerNames = parentRowTag.getHeaderNames();
121 
122             String name = getName();
123 
124             if (Validator.isNull(name) && Validator.isNotNull(_property)) {
125                 name = _property;
126             }
127 
128             headerNames.add(name);
129 
130             if (_orderable) {
131                 Map<String,String> orderableHeaders =
132                     parentRowTag.getOrderableHeaders();
133 
134                 if (Validator.isNotNull(_orderableProperty)) {
135                     orderableHeaders.put(name, _orderableProperty);
136                 }
137                 else if (Validator.isNotNull(_property)) {
138                     orderableHeaders.put(name, _property);
139                 }
140                 else if (Validator.isNotNull(name)) {
141                     orderableHeaders.put(name, name);
142                 }
143             }
144         }
145 
146         if (Validator.isNotNull(_property)) {
147             return SKIP_BODY;
148         }
149         else if (Validator.isNotNull(_buffer)) {
150             _sb = new StringBuilder();
151 
152             pageContext.setAttribute(_buffer, _sb);
153 
154             return EVAL_BODY_INCLUDE;
155         }
156         else if (Validator.isNull(_value)) {
157             return EVAL_BODY_BUFFERED;
158         }
159         else {
160             return SKIP_BODY;
161         }
162     }
163 
164     public String getBuffer() {
165         return _buffer;
166     }
167 
168     public Object getHref() {
169         if (Validator.isNotNull(_href) && (_href instanceof PortletURL)) {
170             _href = _href.toString();
171         }
172 
173         return _href;
174     }
175 
176     public String getOrderableProperty() {
177         return _orderableProperty;
178     }
179 
180     public String getProperty() {
181         return _property;
182     }
183 
184     public String getTarget() {
185         return _target;
186     }
187 
188     public String getTitle() {
189         return _title;
190     }
191 
192     public String getValue() {
193         return _value;
194     }
195 
196     public boolean isOrderable() {
197         return _orderable;
198     }
199 
200     public void setBuffer(String buffer) {
201         _buffer = buffer;
202     }
203 
204     public void setHref(Object href) {
205         _href = href;
206     }
207 
208     public void setOrderable(boolean orderable) {
209         _orderable = orderable;
210     }
211 
212     public void setOrderableProperty(String orderableProperty) {
213         _orderableProperty = orderableProperty;
214     }
215 
216     public void setProperty(String property) {
217         _property = property;
218     }
219 
220     public void setTarget(String target) {
221         _target = target;
222     }
223 
224     public void setTitle(String title) {
225         _title = title;
226     }
227 
228     public void setTranslate(boolean translate) {
229         _translate = translate;
230     }
231 
232     public void setValue(String value) {
233         _value = value;
234     }
235 
236     private String _buffer;
237     private Object _href;
238     private boolean _orderable;
239     private String _orderableProperty;
240     private String _property;
241     private StringBuilder _sb;
242     private String _target;
243     private String _title;
244     private boolean _translate;
245     private String _value;
246 
247 }