Source code: com/dghda/kent/reports/BaseJDBCReport.java
1 /* Copyright (C) 2001 Duane Griffin <duanegriffin@users.sourceforge.net>
2 This file is part of Kent.
3
4 Kent is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 Kent is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
13
14 You should have received a copy of the GNU General Public
15 License along with Kent; see the file COPYING. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA.
18 */
19
20 package com.dghda.kent.reports;
21
22 import java.io.*;
23
24 import com.dghda.kent.*;
25 import com.dghda.module.*;
26
27 /**
28 A base class to simplify writing reports which access a database through JDBC.
29 */
30 public abstract class BaseJDBCReport extends FixedTemplateReport implements Report {
31
32 /**
33 Creates a new report object loaded by the given report engine.
34 @param id The unique ID of the module.
35 @param shortName The name of the module.
36 @param description A description of the module.
37 @param version The module's version information.
38 @param engine The report engine the module runs under.
39 @param input The stream to load the report template from.
40 */
41 public BaseJDBCReport (String id, String shortName, String description,
42 Module.ModuleVersion version, ReportEngine engine,
43 InputStream input) throws IOException {
44 super (id, shortName, description, version, engine, input);
45 }
46
47 /**
48 The base JDBC report implementation has no configuration options.
49 If configuration options are required this method should be overriden.
50 */
51 public String getConfigurationOptions (java.util.Properties config) {
52 return ReportEngine.EMPTY_CONFIG;
53 }
54
55 /** Adds the given query to the report. */
56 public void addQuery (SQLQueryMapping query) {
57 m_Queries.add (query);
58 }
59
60 /**
61 Creates a report data document using the SQL query.
62 @see com.dghda.kent.reports.BaseJDBCReport createReportData
63 */
64 public String getReportData (java.util.Properties config) throws ReportingException {
65 try {
66 ReportData data = createReportData (config);
67 return data.getData();
68 } catch (java.sql.SQLException exc) {
69 throw new ReportingException ("An error occurred while attempting to access the database", exc);
70 }
71 }
72
73 /**
74 Creates a report data document containing data sets generated by the report's queries.
75 To implement more complex behaviour, override this method and call it from the sub-class.
76 */
77 protected ReportData createReportData (java.util.Properties config) throws java.sql.SQLException {
78 ReportData result = new ReportData();
79 java.util.Iterator i = m_Queries.iterator();
80 while (i.hasNext()) {
81 SQLQueryMapping mapping = (SQLQueryMapping) i.next();
82 result.addDataSet (mapping.execute());
83 }
84 return result;
85 }
86
87 private java.util.LinkedList m_Queries = new java.util.LinkedList();
88 }