View Javadoc

1   /*
2    * Copyright 2004 Jeff Johnston
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.extremecomponents.table.tag;
17  
18  import java.util.Iterator;
19  import java.util.List;
20  
21  import javax.servlet.jsp.JspException;
22  import javax.servlet.jsp.tagext.TagSupport;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
27  import org.extremecomponents.table.TableAttributes;
28  import org.extremecomponents.table.bean.Column;
29  import org.extremecomponents.table.bean.Export;
30  import org.extremecomponents.table.bean.Form;
31  import org.extremecomponents.table.bean.Input;
32  import org.extremecomponents.table.bean.Table;
33  import org.extremecomponents.table.model.ExportHandler;
34  import org.extremecomponents.table.model.TableModel;
35  import org.extremecomponents.util.ExceptionUtils;
36  
37  
38  /***
39   * The container which holds all the main table information. Will also
40   * hold global information if needed. The table tag is copied
41   * into the Table and encapsulated in the Model.
42   *
43   * @author Jeff Johnston
44   */
45  public class TableTag extends TagSupport implements TableAttributes
46  {
47  	private static Log logger = LogFactory.getLog(TableTag.class);
48  
49  	/***
50  	 * The Model represents the internal plumbing of the table
51  	 */
52  	private TableModel model;
53  
54  	/***
55  	 * Will iterate over the columns the number of times specified by the maxRows attribute.
56  	 */
57  	private Iterator iterator;
58  
59  	private String id;
60  	private String collection;
61  	private String action;
62  	private String scope;
63  	private String styleClass;
64  	private String headerClass;
65  	private String border;
66  	private String cellpadding;
67  	private String cellspacing;
68  	private String maxRows;
69  	private String filter;
70  	private String pagination;
71  	private String imagePath;
72  	private String sort;
73  	private String title;
74  	private String style;
75  	private String width;
76  
77  	public String getId()
78  	{
79  		return id;
80  	}
81  
82  	public void setId(String id)
83  	{
84  		this.id = id;
85  	}
86  
87  	public String getCollection()
88  	{
89  		return collection;
90  	}
91  
92  	public void setCollection(String collection)
93  	{
94  		this.collection = collection;
95  	}
96  
97  	public String getAction()
98  	{
99  		return action;
100 	}
101 
102 	public void setAction(String action)
103 	{
104 		this.action = action;
105 	}
106 
107 	public String getScope()
108 	{
109 		return scope;
110 	}
111 
112 	public void setScope(String scope)
113 	{
114 		this.scope = scope;
115 	}
116 
117 	public String getStyleClass()
118 	{
119 		return styleClass;
120 	}
121 
122 	public void setStyleClass(String styleClass)
123 	{
124 		this.styleClass = styleClass;
125 	}
126 
127 	public String getHeaderClass()
128 	{
129 		return headerClass;
130 	}
131 
132 	public void setHeaderClass(String headerClass)
133 	{
134 		this.headerClass = headerClass;
135 	}
136 
137 	public String getBorder()
138 	{
139 		return border;
140 	}
141 
142 	public void setBorder(String border)
143 	{
144 		this.border = border;
145 	}
146 
147 	public String getCellpadding()
148 	{
149 		return cellpadding;
150 	}
151 
152 	public void setCellpadding(String cellpadding)
153 	{
154 		this.cellpadding = cellpadding;
155 	}
156 
157 	public String getCellspacing()
158 	{
159 		return cellspacing;
160 	}
161 
162 	public void setCellspacing(String cellspacing)
163 	{
164 		this.cellspacing = cellspacing;
165 	}
166 
167 	public String getMaxRows()
168 	{
169 		return maxRows;
170 	}
171 
172 	public void setMaxRows(String maxRows)
173 	{
174 		this.maxRows = maxRows;
175 	}
176 
177 	public String getFilter()
178 	{
179 		return filter;
180 	}
181 
182 	public void setFilter(String filter)
183 	{
184 		this.filter = filter;
185 	}
186 
187 	public String getPagination()
188 	{
189 		return pagination;
190 	}
191 
192 	public void setPagination(String pagination)
193 	{
194 		this.pagination = pagination;
195 	}
196 
197 	public String getImagePath()
198 	{
199 		return imagePath;
200 	}
201 
202 	public void setImagePath(String imagePath)
203 	{
204 		this.imagePath = imagePath;
205 	}
206 
207 	public String getSort()
208 	{
209 		return sort;
210 	}
211 
212 	public void setSort(String sort)
213 	{
214 		this.sort = sort;
215 	}
216 
217 	public String getTitle()
218 	{
219 		return title;
220 	}
221 
222 	public void setTitle(String title)
223 	{
224 		this.title = title;
225 	}
226 
227 	public String getStyle()
228 	{
229 		return style;
230 	}
231 
232 	public void setStyle(String style)
233 	{
234 		this.style = style;
235 	}
236 
237 	public String getWidth()
238 	{
239 		return width;
240 	}
241 
242 	public void setWidth(String width)
243 	{
244 		this.width = width;
245 	}
246 
247 	public void addParameter(String name, String value)
248 	{
249 		model.getRegistry().addParameter(name, value);
250 	}
251 
252 	public void addForm(Form form)
253 	{
254 		model.getFormHandler().addForm(form);
255 	}
256 
257 	public void addInput(Input input)
258 	{
259 		model.getFormHandler().addInput(input);
260 	}
261 
262 	public void addProperty(String name, String value)
263 	{
264 		model.getProperties().setProperty(name, value);
265 	}
266 
267 	public void addExport(Export export)
268 	{
269 		model.getExportHandler().addExport(export);
270 	}
271 
272 	public boolean hasMetaData()
273 	{
274 		return model.getViewHandler().rowcount() > 0;
275 	}
276 
277 	/***
278 	 * Add the Column meta data to the Model
279 	 */
280 	public void addColumnMetaData(Column column)
281 	{
282 		model.getColumnMetaData().addColumn(column);
283 	}
284 
285 	/***
286 	 * Add a Column value to the Model
287 	 */
288 	public void addColumnValue(String property, Object value)
289 	{
290 		model.getViewHandler().addColumnValue(property, value);
291 	}
292 
293 	/***
294 	 * Find out how many rows have been added to the Model.
295 	 */
296 	public int getModelRowsSize()
297 	{
298 		return model.getViewHandler().rowcount();
299 	}
300 
301 	public TableModel getModel()
302 	{
303 		return model;
304 	}
305 
306 	/***
307 	 * The start of the tag. First fire up the Model and
308 	 * then make a copy of the table tag attributes to
309 	 * send to the model.
310 	 */
311 	public int doStartTag()
312 		throws JspException
313 	{
314 		model = new TableModel(pageContext); //just fire up the tablemodel
315 
316 		if (action != null)
317 		{
318 			action = (String) ExpressionEvaluatorManager.evaluate("action", action, String.class, this, pageContext);
319 		}
320 
321 		if (imagePath != null)
322 		{
323 			imagePath = (String) ExpressionEvaluatorManager.evaluate("imagePath", imagePath, String.class, this, pageContext);
324 		}
325 
326 		Table table = new Table(model); //make a copy
327 		table.setId(id);
328 		table.setCollection(collection);
329 		table.setScope(scope);
330 		table.setAction(action);
331 		table.setStyleClass(styleClass);
332 		table.setHeaderClass(headerClass);
333 		table.setBorder(border);
334 		table.setCellpadding(cellpadding);
335 		table.setCellspacing(cellspacing);
336 		table.setMaxRows(maxRows);
337 		table.setFilter(filter);
338 		table.setPagination(pagination);
339 		table.setImagePath(imagePath);
340 		table.setSort(sort);
341 		table.setTitle(title);
342 		table.setStyle(style);
343 		table.setWidth(width);
344 		model.setTable(table);
345 
346 		pageContext.setAttribute("ROWCOUNT", "0");
347 
348 		model.getRegistry().init(pageContext);
349 
350 		return EVAL_BODY_INCLUDE;
351 	}
352 
353 	/***
354 	 * Two things need to be accomplished here.
355 	 * First, need to iterate once over the columns to load up all the attributes.
356 	 * Second, need to iterate over the columns as many times as specified by the maxRows
357 	 * attribute so the columns rows can be resolved. On each iteration over the columns
358 	 * the current bean in the Collection is passed via the PageContext.
359 	 */
360 	public int doAfterBody()
361 		throws JspException
362 	{
363 		try
364 		{
365 			if (model.getViewHandler().rowcount() == 0)
366 			{
367 				List rows = model.init();
368 				iterator = rows.iterator();
369 			}
370 		}
371 		catch (Exception e)
372 		{
373 			e.printStackTrace();
374 			throw new JspException("TableTag.doAfterBody() Problem: " + ExceptionUtils.formatStackTrace(e));
375 		}
376 
377 		if ((iterator != null) && iterator.hasNext())
378 		{
379 			model.getViewHandler().addRow();
380 			pageContext.setAttribute(collection, iterator.next());
381 			pageContext.setAttribute("ROWCOUNT", "" + getModelRowsSize());
382 
383 			return EVAL_BODY_AGAIN;
384 		}
385 		else
386 		{
387 			return SKIP_BODY;
388 		}
389 	}
390 
391 	/***
392 	 * As a convenience use the EL expression langauge for the columns that need it.
393 	 * Also execute the Model so that it can do all its processing and build the table.
394 	 */
395 	public int doEndTag()
396 		throws JspException
397 	{
398 		try
399 		{
400 			if (!model.getColumnMetaData().hasMetatData())
401 			{
402 				List rows = model.init();
403 				TagUtils.autoSetColumns(this, rows);
404 			}
405 
406 			Object output = model.getViewHandler().getView().afterBody(model);
407 
408 			if (output instanceof String)
409 			{
410 				pageContext.getOut().println(output);
411 			}
412 
413 			pageContext.getRequest().setAttribute(ExportHandler.EXPORT_DATA, output);
414 		}
415 		catch (Exception e)
416 		{
417 			throw new JspException("TableTag.doEndTag() Problem: " + ExceptionUtils.formatStackTrace(e));
418 		}
419 		
420 		model.destroy();
421 		cleanup();
422 
423 		return EVAL_PAGE;
424 	}
425 	
426 	public void cleanup()
427 	{
428 		id = null;
429 		collection = null;
430 		scope = null;
431 		action = null;
432 		styleClass = null;
433 		headerClass = null;
434 		border = null;
435 		cellpadding = null;
436 		cellspacing = null;
437 		maxRows = null;
438 		filter = null;
439 		pagination = null;
440 		imagePath = null;
441 		sort = null;
442 		title = null;
443 		style = null;
444 		width = null;
445 		model = null;
446 		iterator = null;
447 	}
448 }