1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.taglib.ui;
24  
25  import com.liferay.portal.kernel.bean.BeanPropertiesUtil;
26  import com.liferay.portal.kernel.dao.search.ResultRow;
27  import com.liferay.portal.kernel.dao.search.SearchEntry;
28  import com.liferay.portal.kernel.dao.search.TextSearchEntry;
29  import com.liferay.portal.kernel.language.LanguageUtil;
30  import com.liferay.portal.kernel.util.ServerDetector;
31  import com.liferay.portal.kernel.util.Validator;
32  
33  import java.util.List;
34  import java.util.Map;
35  
36  import javax.portlet.PortletURL;
37  
38  import javax.servlet.jsp.JspException;
39  import javax.servlet.jsp.JspTagException;
40  import javax.servlet.jsp.tagext.BodyContent;
41  
42  /**
43   * <a href="SearchContainerColumnTextTag.java.html"><b><i>View Source</i></b>
44   * </a>
45   *
46   * @author Raymond Augé
47   *
48   */
49  public class SearchContainerColumnTextTag extends SearchContainerColumnTag {
50  
51      public int doAfterBody() {
52          return SKIP_BODY;
53      }
54  
55      public int doEndTag() {
56          try {
57              SearchContainerRowTag parentTag =
58                  (SearchContainerRowTag)findAncestorWithClass(
59                      this, SearchContainerRowTag.class);
60  
61              ResultRow row = parentTag.getRow();
62  
63              if (Validator.isNotNull(_property)) {
64                  _value = String.valueOf(
65                      BeanPropertiesUtil.getObject(row.getObject(), _property));
66              }
67              else if (Validator.isNotNull(_buffer)) {
68                  _value = _sb.toString();
69              }
70              else if (_value == null) {
71                  BodyContent bodyContent = getBodyContent();
72  
73                  if (bodyContent != null) {
74                      _value = bodyContent.getString();
75                  }
76              }
77  
78              if (_translate) {
79                  _value = LanguageUtil.get(pageContext, _value);
80              }
81  
82              if (index <= -1) {
83                  index = row.getEntries().size();
84              }
85  
86              if (row.isRestricted()) {
87                  _href = null;
88              }
89  
90              row.addText(
91                  index,
92                  new TextSearchEntry(
93                      getAlign(), getValign(), getColspan(), getValue(),
94                      (String)getHref(), getTarget(), getTitle()));
95  
96              return EVAL_PAGE;
97          }
98          finally {
99              if (!ServerDetector.isResin()) {
100                 align = SearchEntry.DEFAULT_ALIGN;
101                 _buffer = null;
102                 colspan = SearchEntry.DEFAULT_COLSPAN;
103                 _href = null;
104                 index = -1;
105                 name = null;
106                 _orderable = false;
107                 _orderableProperty = null;
108                 _property = null;
109                 _target = null;
110                 _title = null;
111                 _translate = false;
112                 valign = SearchEntry.DEFAULT_VALIGN;
113                 _value = null;
114             }
115         }
116     }
117 
118     public int doStartTag() throws JspException {
119         if (_orderable && Validator.isNull(_orderableProperty)) {
120             _orderableProperty = name;
121         }
122 
123         SearchContainerRowTag parentRowTag = (SearchContainerRowTag)
124             findAncestorWithClass(this, SearchContainerRowTag.class);
125 
126         if (parentRowTag == null) {
127             throw new JspTagException(
128                 "Requires liferay-ui:search-container-row");
129         }
130 
131         if (!parentRowTag.isHeaderNamesAssigned()) {
132             List<String> headerNames = parentRowTag.getHeaderNames();
133 
134             String name = getName();
135 
136             if (Validator.isNull(name) && Validator.isNotNull(_property)) {
137                 name = _property;
138             }
139 
140             headerNames.add(name);
141 
142             if (_orderable) {
143                 Map<String,String> orderableHeaders =
144                     parentRowTag.getOrderableHeaders();
145 
146                 if (Validator.isNotNull(_orderableProperty)) {
147                     orderableHeaders.put(name, _orderableProperty);
148                 }
149                 else if (Validator.isNotNull(_property)) {
150                     orderableHeaders.put(name, _property);
151                 }
152                 else if (Validator.isNotNull(name)) {
153                     orderableHeaders.put(name, name);
154                 }
155             }
156         }
157 
158         if (Validator.isNotNull(_property)) {
159             return SKIP_BODY;
160         }
161         else if (Validator.isNotNull(_buffer)) {
162             _sb = new StringBuilder();
163 
164             pageContext.setAttribute(_buffer, _sb);
165 
166             return EVAL_BODY_INCLUDE;
167         }
168         else if (Validator.isNull(_value)) {
169             return EVAL_BODY_BUFFERED;
170         }
171         else {
172             return SKIP_BODY;
173         }
174     }
175 
176     public String getBuffer() {
177         return _buffer;
178     }
179 
180     public Object getHref() {
181         if (Validator.isNotNull(_href) && (_href instanceof PortletURL)) {
182             _href = _href.toString();
183         }
184 
185         return _href;
186     }
187 
188     public String getOrderableProperty() {
189         return _orderableProperty;
190     }
191 
192     public String getProperty() {
193         return _property;
194     }
195 
196     public String getTarget() {
197         return _target;
198     }
199 
200     public String getTitle() {
201         return _title;
202     }
203 
204     public String getValue() {
205         return _value;
206     }
207 
208     public boolean isOrderable() {
209         return _orderable;
210     }
211 
212     public void setBuffer(String buffer) {
213         _buffer = buffer;
214     }
215 
216     public void setHref(Object href) {
217         _href = href;
218     }
219 
220     public void setOrderable(boolean orderable) {
221         _orderable = orderable;
222     }
223 
224     public void setOrderableProperty(String orderableProperty) {
225         _orderableProperty = orderableProperty;
226     }
227 
228     public void setProperty(String property) {
229         _property = property;
230     }
231 
232     public void setTarget(String target) {
233         _target = target;
234     }
235 
236     public void setTitle(String title) {
237         _title = title;
238     }
239 
240     public void setTranslate(boolean translate) {
241         _translate = translate;
242     }
243 
244     public void setValue(String value) {
245         _value = value;
246     }
247 
248     private String _buffer;
249     private Object _href;
250     private boolean _orderable;
251     private String _orderableProperty;
252     private String _property;
253     private StringBuilder _sb;
254     private String _target;
255     private String _title;
256     private boolean _translate;
257     private String _value;
258 
259 }