Debian Codegraph @

source/org/jfree/report/modules/misc/tablemodel/ResultSetTableModelFactory.java

raw
1 /**
2 * ========================================
3 * JFreeReport : a free Java report library
4 * ========================================
5 *
6 * Project Info: http://reporting.pentaho.org/
7 *
8 * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
9 *
10 * This library is free software; you can redistribute it and/or modify it under the terms
11 * of the GNU Lesser General Public License as published by the Free Software Foundation;
12 * either version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 * See the GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License along with this
19 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
21 *
22 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
23 * in the United States and other countries.]
24 *
25 * ------------
26 * $Id: ResultSetTableModelFactory.java 10756 2009-12-02 15:58:24Z tmorgner $
27 * ------------
28 * (C) Copyright 2000-2005, by Object Refinery Limited.
29 * (C) Copyright 2005-2007, by Pentaho Corporation.
30 */
31 package org.jfree.report.modules.misc.tablemodel;
32
33 import java.sql.ResultSet;
34 import java.sql.ResultSetMetaData;
35 import java.sql.SQLException;
36 import java.util.ArrayList;
37 import javax.swing.table.DefaultTableModel;
38
39 import org.jfree.report.JFreeReportBoot;
40 import org.pentaho.reporting.libraries.base.util.DebugLog;
41
42 /**
43 * Creates a <code>TableModel</code> which is backed up by a <code>ResultSet</code>. If
44 * the <code>ResultSet</code> is scrollable, a {@link org.jfree.report.modules.misc.tablemodel.ScrollableResultSetTableModel}
45 * is created, otherwise all data is copied from the <code>ResultSet</code> into a
46 * <code>DefaultTableModel</code>.
47 * <p/>
48 * The creation of a <code>DefaultTableModel</code> can be forced if the system property
49 * <code>"org.jfree.report.modules.misc.tablemodel.TableFactoryMode"</code> is set to
50 * <code>"simple"</code>.
51 *
52 * @author Thomas Morgner
53 */
54 public final class ResultSetTableModelFactory
55 {
56 /**
57 * The configuration key defining how to map column names to column indices.
58 */
59 public static final String COLUMN_NAME_MAPPING_KEY =
60 "org.jfree.report.modules.misc.tablemodel.ColumnNameMapping";
61
62 /**
63 * The 'ResultSet factory mode'.
64 */
65 public static final String RESULTSET_FACTORY_MODE
66 = "org.jfree.report.modules.misc.tablemodel.TableFactoryMode";
67
68 /**
69 * Singleton instance of the factory.
70 */
71 private static ResultSetTableModelFactory defaultInstance;
72
73 /**
74 * Default constructor. This is a Singleton, use getInstance().
75 */
76 private ResultSetTableModelFactory ()
77 {
78 }
79
80 /**
81 * Creates a table model by using the given <code>ResultSet</code> as the backend. If
82 * the <code>ResultSet</code> is scrollable (the type is not
83 * <code>TYPE_FORWARD_ONLY</code>), an instance of {@link org.jfree.report.modules.misc.tablemodel.ScrollableResultSetTableModel}
84 * is returned. This model uses the extended capabilities of scrollable resultsets to
85 * directly read data from the database without caching or the need of copying the
86 * complete <code>ResultSet</code> into the programs memory.
87 * <p/>
88 * If the <code>ResultSet</code> lacks the scollable features, the data will be copied
89 * into a <code>DefaultTableModel</code> and the <code>ResultSet</code> gets closed.
90 *
91 * @param rs the result set.
92 * @return a closeable table model.
93 *
94 * @throws SQLException if there is a problem with the result set.
95 */
96 public CloseableTableModel createTableModel (final ResultSet rs)
97 throws SQLException
98 {
99 return createTableModel
100 (rs, JFreeReportBoot.getInstance().getGlobalConfig().getConfigProperty
101 (COLUMN_NAME_MAPPING_KEY, "Label").equals("Label"));
102 }
103
104 /**
105 * Creates a table model by using the given <code>ResultSet</code> as the backend. If
106 * the <code>ResultSet</code> is scrollable (the type is not
107 * <code>TYPE_FORWARD_ONLY</code>), an instance of {@link org.jfree.report.modules.misc.tablemodel.ScrollableResultSetTableModel}
108 * is returned. This model uses the extended capabilities of scrollable resultsets to
109 * directly read data from the database without caching or the need of copying the
110 * complete <code>ResultSet</code> into the programs memory.
111 * <p/>
112 * If the <code>ResultSet</code> lacks the scollable features, the data will be copied
113 * into a <code>DefaultTableModel</code> and the <code>ResultSet</code> gets closed.
114 *
115 * @param rs the result set.
116 * @param labelMapping defines, whether to use column names or column labels to compute
117 * the column index.
118 * @return a closeable table model.
119 *
120 * @throws SQLException if there is a problem with the result set.
121 */
122 public CloseableTableModel createTableModel (final ResultSet rs,
123 final boolean labelMapping)
124 throws SQLException
125 {
126 // Allow for override, some jdbc drivers are buggy :(
127 final String prop =
128 JFreeReportBoot.getInstance().getGlobalConfig().getConfigProperty
129 (RESULTSET_FACTORY_MODE, "");
130
131 if (prop.equalsIgnoreCase("simple"))
132 {
133 return generateDefaultTableModel(rs, labelMapping);
134 }
135
136 int resultSetType = ResultSet.TYPE_FORWARD_ONLY;
137 try
138 {
139 resultSetType = rs.getType();
140 }
141 catch (SQLException sqle)
142 {
143 DebugLog.log("ResultSet type could not be determined, assuming default table model.");
144 }
145 if (resultSetType == ResultSet.TYPE_FORWARD_ONLY)
146 {
147 return generateDefaultTableModel(rs, labelMapping);
148 }
149 else
150 {
151 return new ScrollableResultSetTableModel(rs, labelMapping);
152 }
153 }
154
155 /**
156 * A DefaultTableModel that implements the CloseableTableModel interface.
157 */
158 private static final class CloseableDefaultTableModel extends DefaultTableModel
159 implements CloseableTableModel
160 {
161 /**
162 * The results set.
163 */
164 private final ResultSet res;
165
166 /**
167 * Creates a new closeable table model.
168 *
169 * @param objects the table data.
170 * @param objects1 the column names.
171 * @param res the result set.
172 */
173 private CloseableDefaultTableModel (final Object[][] objects,
174 final Object[] objects1, final ResultSet res)
175 {
176 super(objects, objects1);
177 this.res = res;
178 }
179
180 /**
181 * If this model has a resultset assigned, close it, if this is a DefaultTableModel,
182 * remove all data.
183 */
184 public void close ()
185 {
186 setDataVector(new Object[0][0], new Object[0]);
187 try
188 {
189 res.close();
190 }
191 catch (Exception e)
192 {
193 //Log.debug ("Close failed for resultset table model", e);
194 }
195 }
196 }
197
198 /**
199 * Generates a <code>TableModel</code> that gets its contents filled from a
200 * <code>ResultSet</code>. The column names of the <code>ResultSet</code> will form the
201 * column names of the table model.
202 * <p/>
203 * Hint: To customize the names of the columns, use the SQL column aliasing (done with
204 * <code>SELECT nativecolumnname AS "JavaColumnName" FROM ....</code>
205 *
206 * @param rs the result set.
207 * @return a closeable table model.
208 *
209 * @throws SQLException if there is a problem with the result set.
210 */
211 public CloseableTableModel generateDefaultTableModel (final ResultSet rs)
212 throws SQLException
213 {
214 return generateDefaultTableModel(rs,
215 JFreeReportBoot.getInstance().getGlobalConfig().getConfigProperty
216 (COLUMN_NAME_MAPPING_KEY, "Label").equals("Label"));
217 }
218
219 /**
220 * Generates a <code>TableModel</code> that gets its contents filled from a
221 * <code>ResultSet</code>. The column names of the <code>ResultSet</code> will form the
222 * column names of the table model.
223 * <p/>
224 * Hint: To customize the names of the columns, use the SQL column aliasing (done with
225 * <code>SELECT nativecolumnname AS "JavaColumnName" FROM ....</code>
226 *
227 * @param rs the result set.
228 * @param labelMapping defines, whether to use column names or column labels to compute
229 * the column index.
230 * @return a closeable table model.
231 *
232 * @throws SQLException if there is a problem with the result set.
233 */
234 public CloseableTableModel generateDefaultTableModel
235 (final ResultSet rs, final boolean labelMapping)
236 throws SQLException
237 {
238 final ResultSetMetaData rsmd = rs.getMetaData();
239 final int colcount = rsmd.getColumnCount();
240 final ArrayList header = new ArrayList(colcount);
241 for (int i = 0; i < colcount; i++)
242 {
243 if (labelMapping)
244 {
245 final String name = rsmd.getColumnLabel(i + 1);
246 header.add(name);
247 }
248 else
249 {
250 final String name = rsmd.getColumnName(i + 1);
251 header.add(name);
252 }
253 }
254 final ArrayList rows = new ArrayList();
255 while (rs.next())
256 {
257 final Object[] column = new Object[colcount];
258 for (int i = 0; i < colcount; i++)
259 {
260 final Object val = rs.getObject(i + 1);
261 column[i] = val;
262 }
263 rows.add(column);
264 }
265
266 final Object[] tempRows = rows.toArray();
267 final Object[][] rowMap = new Object[tempRows.length][];
268 for (int i = 0; i < tempRows.length; i++)
269 {
270 rowMap[i] = (Object[]) tempRows[i];
271 }
272 final CloseableDefaultTableModel model =
273 new CloseableDefaultTableModel(rowMap, header.toArray(), rs);
274 for (int i = 0; i < colcount; i++)
275 {
276 }
277 return model;
278 }
279
280 /**
281 * Returns the singleton instance of the factory.
282 *
283 * @return an instance of this factory.
284 */
285 public synchronized static ResultSetTableModelFactory getInstance ()
286 {
287 if (defaultInstance == null)
288 {
289 defaultInstance = new ResultSetTableModelFactory();
290 }
291 return defaultInstance;
292 }
293
294 }