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.Validator;
31  
32  import java.util.List;
33  import java.util.Map;
34  
35  import javax.portlet.PortletURL;
36  
37  import javax.servlet.jsp.JspException;
38  import javax.servlet.jsp.JspTagException;
39  import javax.servlet.jsp.tagext.BodyContent;
40  
41  /**
42   * <a href="SearchContainerColumnTextTag.java.html"><b><i>View Source</i></b>
43   * </a>
44   *
45   * @author Raymond Augé
46   *
47   */
48  public class SearchContainerColumnTextTag extends SearchContainerColumnTag {
49  
50      public int doAfterBody() {
51          return SKIP_BODY;
52      }
53  
54      public int doEndTag() {
55          try {
56              SearchContainerRowTag parentTag =
57                  (SearchContainerRowTag)findAncestorWithClass(
58                      this, SearchContainerRowTag.class);
59  
60              ResultRow row = parentTag.getRow();
61  
62              if (Validator.isNotNull(_property)) {
63                  _value = String.valueOf(
64                      BeanPropertiesUtil.getObject(row.getObject(), _property));
65              }
66              else if (Validator.isNotNull(_buffer)) {
67                  _value = _sb.toString();
68              }
69              else if (_value == null) {
70                  BodyContent bodyContent = getBodyContent();
71  
72                  if (bodyContent != null) {
73                      _value = bodyContent.getString();
74                  }
75              }
76  
77              if (_translate) {
78                  _value = LanguageUtil.get(pageContext, _value);
79              }
80  
81              if (index <= -1) {
82                  index = row.getEntries().size();
83              }
84  
85              if (row.isRestricted()) {
86                  _href = null;
87              }
88  
89              row.addText(
90                  index,
91                  new TextSearchEntry(
92                      getAlign(), getValign(), getColspan(), getValue(),
93                      (String)getHref(), getTarget(), getTitle()));
94  
95              return EVAL_PAGE;
96          }
97          finally {
98              align = SearchEntry.DEFAULT_ALIGN;
99              _buffer = null;
100             colspan = SearchEntry.DEFAULT_COLSPAN;
101             _href = null;
102             index = -1;
103             name = null;
104             _orderable = false;
105             _orderableProperty = null;
106             _property = null;
107             _target = null;
108             _title = null;
109             _translate = false;
110             valign = SearchEntry.DEFAULT_VALIGN;
111             _value = null;
112         }
113     }
114 
115     public int doStartTag() throws JspException {
116         if (_orderable && Validator.isNull(_orderableProperty)) {
117             _orderableProperty = name;
118         }
119 
120         SearchContainerRowTag parentRowTag = (SearchContainerRowTag)
121             findAncestorWithClass(this, SearchContainerRowTag.class);
122 
123         if (parentRowTag == null) {
124             throw new JspTagException(
125                 "Requires liferay-ui:search-container-row");
126         }
127 
128         if (!parentRowTag.isHeaderNamesAssigned()) {
129             List<String> headerNames = parentRowTag.getHeaderNames();
130 
131             String name = getName();
132 
133             if (Validator.isNull(name) && Validator.isNotNull(_property)) {
134                 name = _property;
135             }
136 
137             headerNames.add(name);
138 
139             if (_orderable) {
140                 Map<String,String> orderableHeaders =
141                     parentRowTag.getOrderableHeaders();
142 
143                 if (Validator.isNotNull(_orderableProperty)) {
144                     orderableHeaders.put(name, _orderableProperty);
145                 }
146                 else if (Validator.isNotNull(_property)) {
147                     orderableHeaders.put(name, _property);
148                 }
149                 else if (Validator.isNotNull(name)) {
150                     orderableHeaders.put(name, name);
151                 }
152             }
153         }
154 
155         if (Validator.isNotNull(_property)) {
156             return SKIP_BODY;
157         }
158         else if (Validator.isNotNull(_buffer)) {
159             _sb = new StringBuilder();
160 
161             pageContext.setAttribute(_buffer, _sb);
162 
163             return EVAL_BODY_INCLUDE;
164         }
165         else if (Validator.isNull(_value)) {
166             return EVAL_BODY_BUFFERED;
167         }
168         else {
169             return SKIP_BODY;
170         }
171     }
172 
173     public String getBuffer() {
174         return _buffer;
175     }
176 
177     public Object getHref() {
178         if (Validator.isNotNull(_href) && (_href instanceof PortletURL)) {
179             _href = _href.toString();
180         }
181 
182         return _href;
183     }
184 
185     public String getOrderableProperty() {
186         return _orderableProperty;
187     }
188 
189     public String getProperty() {
190         return _property;
191     }
192 
193     public String getTarget() {
194         return _target;
195     }
196 
197     public String getTitle() {
198         return _title;
199     }
200 
201     public String getValue() {
202         return _value;
203     }
204 
205     public boolean isOrderable() {
206         return _orderable;
207     }
208 
209     public void setBuffer(String buffer) {
210         _buffer = buffer;
211     }
212 
213     public void setHref(Object href) {
214         _href = href;
215     }
216 
217     public void setOrderable(boolean orderable) {
218         _orderable = orderable;
219     }
220 
221     public void setOrderableProperty(String orderableProperty) {
222         _orderableProperty = orderableProperty;
223     }
224 
225     public void setProperty(String property) {
226         _property = property;
227     }
228 
229     public void setTarget(String target) {
230         _target = target;
231     }
232 
233     public void setTitle(String title) {
234         _title = title;
235     }
236 
237     public void setTranslate(boolean translate) {
238         _translate = translate;
239     }
240 
241     public void setValue(String value) {
242         _value = value;
243     }
244 
245     private String _buffer;
246     private Object _href;
247     private boolean _orderable;
248     private String _orderableProperty;
249     private String _property;
250     private StringBuilder _sb;
251     private String _target;
252     private String _title;
253     private boolean _translate;
254     private String _value;
255 
256 }