001: /*
002: * Copyright (C) 2006 by Open Source Software Solutions, LLC and Contributors
003: *
004: * This program is free software; you can redistribute it and/or modify it
005: * under the terms of the GNU General Public License as published by the Free
006: * Software Foundation; either version 2 of the License, or (at your option)
007: * any later version.
008: *
009: * This program is distributed in the hope that it will be useful, but WITHOUT
010: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
012: * more details.
013: *
014: * You should have received a copy of the GNU General Public License along with
015: * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
016: * Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: * Original Author : Lisa Shields - brillobaby@gmail.com
019: * Contributor(s) : Erik Swenson - erik@oreports.com
020: *
021: */
022:
023: package org.efs.openreports.engine;
024:
025: import org.efs.openreports.providers.DataSourceProvider;
026: import org.efs.openreports.providers.ProviderException;
027: import org.apache.log4j.Logger;
028:
029: import java.sql.Connection;
030: import java.sql.SQLException;
031: import java.util.List;
032: import java.util.Map;
033:
034: /**
035: * Extension of the Jxls ReportManagerImpl class,
036: * making it Open Reports DataSourceProvider aware. This means
037: * JXLS reporting templates can create multiple datasource
038: * reports in a single workbook.
039: *
040: * The standard JXLS xls template tag construct looks like this:
041: * <jx:forEach items="${rm.exec("select email, jobtitle from employees")}" var="x">
042: *
043: * The underlaying ReportManagerImpl provides a single exec function,
044: * with the following signature:
045: * rm.exec(String sql)
046: *
047: * This class extends the above standard capability, without changing it, by adding
048: * the following additional Report Manager exec functions:
049: *
050: * rm.exec(String datasourceName, String sql)
051: * rm.exec(int datasourceId, String sql)
052: *
053: * These two additional methods support template constructs as below, enabling
054: * multiple datasources (or parameter-driven datasource selection) for
055: * JXLS reports in Open Reports:
056: *
057: * <jx:forEach items="${rm.exec("OpenReports Sample Data", "select email, jobtitle from employees")}" var="x">
058: * <jx:forEach items="${rm.exec(3, "select email, jobtitle from employees")}" var="x">
059: *
060: */
061: public class JXLSReportManagerImpl extends
062: net.sf.jxls.report.ReportManagerImpl {
063:
064: protected static Logger log = Logger
065: .getLogger(JXLSReportManagerImpl.class);
066:
067: DataSourceProvider dataSourceProvider = null;
068:
069: public JXLSReportManagerImpl(Connection connection, Map beans,
070: DataSourceProvider dataSourceProvider) throws SQLException {
071: super (connection, beans);
072: this .dataSourceProvider = dataSourceProvider;
073: }
074:
075: // This method allows Open Reports JXLs templates to contain ReportManager
076: // invocations of the form rm.exec(String datasourceName, String sql), where
077: // datasourceName can match any report_datasource.NAME configured in the Open Reports Application.
078: public List exec(String datasourceName, String sql)
079: throws SQLException, ProviderException {
080: List results = null;
081: log.debug("JXLS exec invoked for datasourceName=["
082: + datasourceName + "]");
083: try {
084: results = exec(dataSourceProvider.getDataSource(
085: datasourceName).getId(), sql);
086: } catch (Exception e) {
087: String msg = "Unable to complete query against datasourceName=["
088: + datasourceName
089: + "]."
090: + " Please ensure the datasourceName in your xls template is valid."
091: + " Caught exception is "
092: + e.getClass().getName()
093: + ", message is " + e.getMessage();
094: log.error(msg);
095: throw new ProviderException(msg);
096: }
097: return results;
098: }
099:
100: // This new method allows Open Reports JXLs templates to contain ReportManager
101: // invocations of the form rm.exec(int datasourceId, String sql), where
102: // datasourceId can match any report_datasource.DATASOURCE_ID configured in the Open Reports Application.
103: public List exec(Integer datasourceId, String sql)
104: throws SQLException, ProviderException {
105: Connection defaultConnection = super .getConnection();
106: List list = null;
107: try {
108: super .setConnection(dataSourceProvider
109: .getConnection(datasourceId));
110: log.debug("JXLS exec sql=[" + sql
111: + "] against datasourceId=[" + datasourceId + "]");
112: list = super .exec(sql); // invokes net.sf.jxls.report.ReportManagerImpl.exec function
113: } catch (Exception e) {
114: String msg = "Unable to execute sql=["
115: + sql
116: + "] against datasourceId=["
117: + datasourceId
118: + "]."
119: + " Please ensure the dataSourceId and SQL in your xls template are valid."
120: + " Caught exception is " + e.getClass().getName()
121: + ", message is " + e.getMessage();
122: log.error(msg);
123: throw new ProviderException(msg);
124: } finally {
125: super.setConnection(defaultConnection);
126: }
127: return list;
128: }
129:
130: }
|