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.model;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import javax.servlet.jsp.PageContext;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.extremecomponents.table.bean.Table;
28  import org.extremecomponents.table.bean.TableSection;
29  import org.extremecomponents.table.cell.Cell;
30  import org.extremecomponents.util.ExtremeUtils;
31  
32  
33  
34  /***
35   * Build up a Model that represents the table. This is the main reference point to all objects
36   * that are needed to build the table display up.
37   *
38   * @author Jeff Johnston
39   */
40  public class TableModel
41  {
42  	private static Log logger = LogFactory.getLog(TableModel.class);
43  	private PageContext pageContext;
44  
45  	//stuff that the model should know about
46  	private Table table;
47  	private Object tableCollection;
48  	private TableSection tableSection;
49  	private TableProperties properties = new TableProperties();
50  	private ColumnMetaData columnMetaData = new ColumnMetaData(this);
51  	private SortHandler sortHandler = new SortHandler(this);
52  	private FilterHandler filterHandler = new FilterHandler(this);
53  	private PageHandler pageHandler = new PageHandler(this);
54  	private ExportHandler exportHandler = new ExportHandler(this);
55  	private FormHandler formHandler = new FormHandler(this);
56  	private ViewHandler viewHandler = new ViewHandler(this);
57  	private ParameterRegistry registry = new ParameterRegistry(this);
58  	
59  	private Map cellCache = new HashMap(); //a simple cell cache
60  
61  	public TableModel(PageContext pageContext)
62  	{
63  		this.pageContext = pageContext;
64  	}
65  
66  	public PageContext getPageContext()
67  	{
68  		return pageContext;
69  	}
70  	
71  	public void setTable(Table table)
72  	{
73  		this.table = table;
74  	}
75  
76  	public Table getTable()
77  	{
78  		return table;
79  	}
80  	
81  	public String getTableKey()
82  	{
83  		return getTable().getId() + "_";
84  	}
85  	
86  	public SortHandler getSortHandler()
87  	{
88  		return sortHandler;
89  	}
90  
91  	public FilterHandler getFilterHandler()
92  	{
93  		return filterHandler;
94  	}
95  
96  	public PageHandler getPageHandler()
97  	{
98  		return pageHandler;
99  	}
100 
101 	public FormHandler getFormHandler()
102 	{
103 		return formHandler;
104 	}
105 
106 	public ViewHandler getViewHandler()
107 	{
108 		return viewHandler;
109 	}
110 
111 	public ExportHandler getExportHandler()
112 	{
113 		return exportHandler;
114 	}
115 
116 	public TableSection getTableSection()
117 	{
118 		return tableSection;
119 	}
120 
121 	public ColumnMetaData getColumnMetaData()
122 	{
123 		return columnMetaData;
124 	}
125 
126 	public TableProperties getProperties()
127 	{
128 		return properties;
129 	}
130 	
131 	public ParameterRegistry getRegistry() 
132 	{
133 		return registry;
134 	}
135 
136 	public List getTableCollection()
137 	{
138 		return (List) tableCollection;
139 	}
140 
141 	public void setTableCollection(Object tableCollection)
142 		throws Exception
143 	{
144 		if (tableCollection instanceof List)
145 		{
146 			this.tableCollection = tableCollection;
147 		}
148 		else
149 		{
150 			throw new Exception("The eXtremeTable could not find the List of Beans (or Map) identified by the collection and scope attributes.");
151 		}
152 	}
153 	
154 	/***
155 	 * A simple implementation of a cache 
156 	 * so that do not have to keep loading up the same eXtremeTable
157 	 */
158 	public Cell getCachedCell(Class classDefinition)
159 	{
160 		Cell cell = (Cell)cellCache.get(classDefinition);
161 		
162 		if (cell == null)
163 		{
164 			try
165 			{
166 				//Class classDefinition = Class.forName(className);
167 				Object object = classDefinition.newInstance();
168 				cell = (Cell) object;
169 				cellCache.put(classDefinition, cell);
170 			}
171 			catch (Exception e)
172 			{
173 				logger.error("Model.build()", e);
174 			}
175 		}
176 		
177 		return cell;
178 	}
179 
180 	/***
181 	 * Do all the filtering and sorting and return the rows of the table
182 	 * that need to be displayed.
183 	 */
184 	public List init()
185 		throws Exception
186 	{
187 		setTableCollection(ExtremeUtils.retrieveFromScope(pageContext, getTable().getCollection(), getTable().getScope()));
188 
189 		List filteredSortedRows = new ArrayList();
190 
191 		filteredSortedRows.addAll(getTableCollection()); //Make a copy for thread safety
192 		
193 		filteredSortedRows = filterHandler.filterRows(this, filteredSortedRows);
194 
195 		sortHandler.sort(filteredSortedRows);
196 
197 		tableSection = TableModelUtils.getTableSection(this, filteredSortedRows.size());
198 		
199 		viewHandler.setView();
200 
201 		return TableModelUtils.splitIntoTableSectionRows(this, filteredSortedRows, tableSection);
202 	}
203 	
204 	public void destroy()
205 	{
206 		properties.destroy();
207 		columnMetaData.destroy();
208 		filterHandler.destroy();
209 		exportHandler.destroy();
210 		formHandler.destroy();
211 		registry.destroy();
212 		cellCache.clear();
213 		table = null;
214 		tableCollection = null;
215 		tableSection = null;
216 		properties = null;
217 		columnMetaData = null;
218 		sortHandler = null;
219 		filterHandler = null;
220 		pageHandler = null;
221 		exportHandler = null;
222 		formHandler = null;
223 		viewHandler = null;
224 		registry = null;
225 		cellCache = null;
226 	}
227 }