KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > efs > openreports > engine > JXLSReportManagerImpl


1 /*
2  * Copyright (C) 2006 by Open Source Software Solutions, LLC and Contributors
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the Free
6  * Software Foundation; either version 2 of the License, or (at your option)
7  * any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16  * Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  * Original Author : Lisa Shields - brillobaby@gmail.com
19  * Contributor(s) : Erik Swenson - erik@oreports.com
20  *
21  */

22
23 package org.efs.openreports.engine;
24
25 import org.efs.openreports.providers.DataSourceProvider;
26 import org.efs.openreports.providers.ProviderException;
27 import org.apache.log4j.Logger;
28
29 import java.sql.Connection JavaDoc;
30 import java.sql.SQLException JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33
34 /**
35  * Extension of the Jxls ReportManagerImpl class,
36  * making it Open Reports DataSourceProvider aware. This means
37  * JXLS reporting templates can create multiple datasource
38  * reports in a single workbook.
39  *
40  * The standard JXLS xls template tag construct looks like this:
41  * <jx:forEach items="${rm.exec("select email, jobtitle from employees")}" var="x">
42  *
43  * The underlaying ReportManagerImpl provides a single exec function,
44  * with the following signature:
45  * rm.exec(String sql)
46  *
47  * This class extends the above standard capability, without changing it, by adding
48  * the following additional Report Manager exec functions:
49  *
50  * rm.exec(String datasourceName, String sql)
51  * rm.exec(int datasourceId, String sql)
52  *
53  * These two additional methods support template constructs as below, enabling
54  * multiple datasources (or parameter-driven datasource selection) for
55  * JXLS reports in Open Reports:
56  *
57  * <jx:forEach items="${rm.exec("OpenReports Sample Data", "select email, jobtitle from employees")}" var="x">
58  * <jx:forEach items="${rm.exec(3, "select email, jobtitle from employees")}" var="x">
59  *
60  */

61 public class JXLSReportManagerImpl extends net.sf.jxls.report.ReportManagerImpl {
62     
63     protected static Logger log = Logger.getLogger(JXLSReportManagerImpl.class);
64
65     DataSourceProvider dataSourceProvider = null;
66
67     public JXLSReportManagerImpl(Connection JavaDoc connection, Map JavaDoc beans, DataSourceProvider dataSourceProvider) throws SQLException JavaDoc {
68         super( connection, beans );
69         this.dataSourceProvider = dataSourceProvider;
70     }
71     
72     // This method allows Open Reports JXLs templates to contain ReportManager
73
// invocations of the form rm.exec(String datasourceName, String sql), where
74
// datasourceName can match any report_datasource.NAME configured in the Open Reports Application.
75
public List JavaDoc exec(String JavaDoc datasourceName, String JavaDoc sql) throws SQLException JavaDoc, ProviderException {
76         List JavaDoc results = null;
77         log.debug("JXLS exec invoked for datasourceName=[" + datasourceName +"]");
78         try {
79             results = exec(dataSourceProvider.getDataSource(datasourceName).getId(), sql);
80         } catch( Exception JavaDoc e ) {
81             String JavaDoc msg = "Unable to complete query against datasourceName=[" + datasourceName +"]." +
82                       " Please ensure the datasourceName in your xls template is valid." +
83                   " Caught exception is " + e.getClass().getName() + ", message is " + e.getMessage();
84             log.error(msg);
85             throw new ProviderException(msg);
86         }
87         return results;
88     }
89     
90    // This new method allows Open Reports JXLs templates to contain ReportManager
91
// invocations of the form rm.exec(int datasourceId, String sql), where
92
// datasourceId can match any report_datasource.DATASOURCE_ID configured in the Open Reports Application.
93
public List JavaDoc exec(Integer JavaDoc datasourceId, String JavaDoc sql) throws SQLException JavaDoc, ProviderException {
94         Connection JavaDoc defaultConnection=super.getConnection();
95         List JavaDoc list = null;
96         try {
97             super.setConnection(dataSourceProvider.getConnection(datasourceId));
98             log.debug("JXLS exec sql=[" + sql + "] against datasourceId=[" + datasourceId +"]");
99             list = super.exec(sql); // invokes net.sf.jxls.report.ReportManagerImpl.exec function
100
} catch( Exception JavaDoc e ) {
101             String JavaDoc msg = "Unable to execute sql=[" + sql + "] against datasourceId=[" + datasourceId +"]." +
102                       " Please ensure the dataSourceId and SQL in your xls template are valid." +
103                   " Caught exception is " + e.getClass().getName() + ", message is " + e.getMessage();
104             log.error(msg);
105             throw new ProviderException(msg);
106         } finally {
107             super.setConnection(defaultConnection);
108         }
109         return list;
110     }
111     
112    
113 }
114
Popular Tags