View Javadoc

1   /*
2    * Created on Oct 3, 2004
3    *
4    */
5   package org.extremecomponents.tree.tag;
6   
7   import java.util.Iterator;
8   import java.util.List;
9   
10  import javax.servlet.jsp.JspException;
11  
12  import org.apache.commons.logging.Log;
13  import org.apache.commons.logging.LogFactory;
14  import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
15  import org.extremecomponents.table.bean.Column;
16  import org.extremecomponents.table.bean.Table;
17  import org.extremecomponents.table.model.ExportHandler;
18  import org.extremecomponents.table.model.TableModel;
19  import org.extremecomponents.table.tag.TableTag;
20  import org.extremecomponents.table.tag.TagUtils;
21  import org.extremecomponents.tree.model.TreeModel;
22  import org.extremecomponents.util.ExceptionUtils;
23  
24  /***
25   * org.extremecomponents.tree.tag.TreeTag.java - 
26   * 
27   * @author Paul Horn
28   */
29  public class TreeTag extends TableTag
30  {
31  	private static final long serialVersionUID = -6626881785728133686L;
32  	private static Log logger = LogFactory.getLog(TreeTag.class);
33  	
34  	/***
35  	 * Will iterate over the columns the number of times specified by the maxRows attribute.
36  	 */
37  	private Iterator iterator;
38  	/***
39  	 * Attribute of the bean holding a reference to it's parent.
40  	 */
41  	public String parentAttribute;
42  	/***
43  	 * Attribute of the bean holding a reference to itself.
44  	 */
45  	private String identifier;
46  	/***
47  	 * The Model represents the internal plumbing of the table
48  	 */
49  	private TreeModel model;
50  	
51  	/***
52  	 * @return Returns the parentAttribute.
53  	 */
54  	public String getParentAttribute()
55  	{
56  		return parentAttribute;
57  	}
58  	/***
59  	 * @param parentAttribute The parentAttribute to set.
60  	 */
61  	public void setParentAttribute(String parentAttribute)
62  	{
63  		this.parentAttribute = parentAttribute;
64  	}
65  	/***
66  	 * @return Returns the identifier.
67  	 */
68  	public String getIdentifier()
69  	{
70  		return identifier;
71  	}
72  	/***
73  	 * @param identifier The identifier to set.
74  	 */
75  	public void setIdentifier(String identifier)
76  	{
77  		this.identifier = identifier;
78  	}		
79  	/***
80  	 * @return Returns the model.
81  	 */
82  	public TableModel getModel()
83  	{
84  		return (TableModel) model;
85  	}
86  	/***
87  	 * @param model The model to set.
88  	 */
89  	public void setModel(TreeModel model)
90  	{
91  		this.model = model;
92  	}
93  	
94  	public boolean hasMetaData()
95  	{
96  		return model.getViewHandler().rowcount() > 0;
97  	}
98  	
99  	/***
100 	 * Add the Column meta data to the Model
101 	 */
102 	public void addColumnMetaData(Column column)
103 	{
104 		model.getColumnMetaData().addColumn(column);
105 	}
106 	
107 	/***
108 	 * Add a Column value to the Model
109 	 */
110 	public void addColumnValue(String property, Object value)
111 	{
112 		model.getViewHandler().addColumnValue(property, value);
113 	}	
114 	
115 	/***
116 	 * Find out how many rows have been added to the Model.
117 	 */
118 	public int getModelRowsSize()
119 	{
120 		return model.getViewHandler().rowcount();
121 	}	
122 	
123 	public void addProperty(String name, String value)
124 	{
125 		model.getTreeProperties().setProperty(name, value);
126 	}
127 	
128 	/***
129 	 * The start of the tag. First fire up the Model and
130 	 * then make a copy of the table tag attributes to
131 	 * send to the model.
132 	 */
133 	public int doStartTag() throws JspException
134 	{
135 		logger.debug("TreeTag.doStartTag()");
136 		
137 		try
138 		{
139 			model = new TreeModel(pageContext); //just fire up the tablemodel
140 			model.setParentAttribute(parentAttribute);
141 			model.setIdentifier(identifier);
142 			
143 			if (getAction() != null)
144 			{
145 				setAction((String) ExpressionEvaluatorManager.evaluate("action", getAction(), String.class, this, pageContext));
146 			}
147 			
148 			if (getImagePath() != null)
149 			{
150 				setImagePath((String) ExpressionEvaluatorManager.evaluate("imagePath", getImagePath(), String.class, this, pageContext));
151 			}
152 			
153 			Table table = new Table((TableModel) model); //make a copy
154 			table.setId(getId());
155 			table.setCollection(getCollection());
156 			table.setScope(getScope());
157 			table.setAction(getAction());
158 			table.setStyleClass(getStyleClass());
159 			table.setHeaderClass(getHeaderClass());
160 			table.setBorder(getBorder());
161 			table.setCellpadding(getCellpadding());
162 			table.setCellspacing(getCellspacing());
163 			table.setMaxRows("0");
164 			table.setFilter(getFilter());
165 			table.setPagination(getPagination());
166 			table.setImagePath(getImagePath());
167 			table.setSort(getSort());
168 			table.setTitle(getTitle());
169 			table.setStyle(getStyle());
170 			table.setWidth(getWidth());
171 			model.setTable(table);
172 			
173 			pageContext.setAttribute("ROWCOUNT", "0");
174 			
175 			model.getRegistry().init(pageContext);
176 		}
177 		catch (Exception e)
178 		{
179 			throw new JspException("TreeTag.doStartTag() Problem: " + ExceptionUtils.formatStackTrace(e));
180 		}
181 
182 		return EVAL_BODY_INCLUDE;
183 	}
184 	
185 	/***
186 	 * Two things need to be accomplished here.
187 	 * First, need to iterate once over the columns to load up all the attributes.
188 	 * Second, need to iterate over the columns as many times as specified by the maxRows
189 	 * attribute so the columns rows can be resolved. On each iteration over the columns
190 	 * the current bean in the Collection is passed via the PageContext.
191 	 */
192 	public int doAfterBody() throws JspException
193 	{
194 		logger.debug("TreeTag.doAfterBody()");
195 
196 		TreeModel model = (TreeModel) getModel();
197 		try
198 		{
199 			if (model.getViewHandler().rowcount() == 0)
200 			{
201 				List rows = model.init();
202 				iterator = rows.iterator();
203 			}
204 		}
205 		catch (Exception e)
206 		{
207 			e.printStackTrace();
208 			throw new JspException("TreeTag.doAfterBody() Problem: " + ExceptionUtils.formatStackTrace(e));
209 		}
210 
211 		if ((iterator != null) && iterator.hasNext())
212 		{
213 			model.getViewHandler().addRow();
214 			Object rowItem = iterator.next();
215 			logger.debug("Row: " + rowItem.toString());
216 			pageContext.setAttribute(getCollection(), rowItem);
217 			pageContext.setAttribute("ROWCOUNT", "" + getModelRowsSize());
218 
219 			return EVAL_BODY_AGAIN;
220 		}
221 		else
222 		{
223 			return SKIP_BODY;
224 		}
225 	}
226 	
227 	/***
228 	 * As a convenience use the EL expression langauge for the columns that need it.
229 	 * Also execute the Model so that it can do all its processing and build the table.
230 	 */
231 	public int doEndTag() throws JspException
232 	{
233 		logger.debug("TreeTag.doEndTag()");
234 
235 		try
236 		{
237 			if (!model.getColumnMetaData().hasMetatData())
238 			{
239 				List rows = model.init();
240 				TagUtils.autoSetColumns(this, rows);
241 			}
242 
243 			Object output = model.getViewHandler().getView().afterBody(model);
244 
245 			if (output instanceof String)
246 			{
247 				pageContext.getOut().println(output);
248 			}
249 
250 			pageContext.getRequest().setAttribute(ExportHandler.EXPORT_DATA, output);
251 		}
252 		catch (Exception e)
253 		{
254 			throw new JspException("TreeTag.doEndTag() Problem: " + ExceptionUtils.formatStackTrace(e));
255 		}
256 		
257 		model.destroy();
258 		cleanup();
259 
260 		return EVAL_PAGE;
261 	}	
262 	
263 	public void cleanup()
264 	{
265 		iterator = null;
266 		parentAttribute = null;
267 		super.cleanup();
268 	}
269 
270 }