简体   繁体   中英

Pass connection from Java Application to Birt report

I'm new to Birt.

I'm trying to pass the connection to the report from my java application, but I get an error:

The following items have errors:

ReportDesign (id = 1): + There are errors evaluating script "importPackage(Packages.it.lfiammetta.birt); var conn = new ReportRenderer(); reportContext.getAppContext().put("OdaJDBCDriverPassInConnection", conn);": Fail to execute script in function __bm_beforeOpen(). Source:

" + importPackage(Packages.it.lfiammetta.birt); var conn = new ReportRenderer(); reportContext.getAppContext().put("OdaJDBCDriverPassInConnection", conn); + "

A BIRT exception occurred. See next exception for more information. Error evaluating Javascript expression. Script engine error: ReferenceError: "ReportRenderer" is not defined. (/report/data-sources/oda-data-source[@id="43"]/method[@name="beforeOpen"]#2) Script source: /report/data-sources/oda-data-source[@id="43"]/method[@name="beforeOpen"], line: 0, text: __bm_beforeOpen(). (Element ID:1)

This is my java code that creates and launches report:

package it.lfiammetta.birt;

public class ReportRenderer {
        public void executeReport() {
                code...

                Map<String, Object> appContext = task.getAppContext();
                appContext.put("OdaJDBCDriverPassInConnection", myConnection);
                appContext.put("OdaJDBCDriverPassInConnectionCloseAfterUse", false);
                task.setAppContext(appContext);

                task.run();

                code...
        }
}

This is the code I wrote in the script 'beforeOpen' the datasource:

importPackage(Packages.it.lfiammetta.birt);

var conn = new ReportRenderer(); 
reportContext.getAppContext().put("OdaJDBCDriverPassInConnection", conn);

I set the classpath.

Birt version I'm using is 4.2.1.

Thanks in advance for your help and I apologize for my English.

I'm doing that from Java code ( IJDBCParameters - actually parameters for JDBC connections, I'm looking connection by name - OdaDataSourceHandle.getName() ):

@SuppressWarnings("rawtypes")
private static void substituteJDBCConnections(IReportRunnable pReportRunnable) {
    final Map<String, IJDBCParameters> jdbcConnections = reportParameters.getJdbcConnections();
    if (jdbcConnections != null ){
        for (Iterator iter = pReportRunnable.getDesignHandle().getModuleHandle().getDataSources().iterator(); iter.hasNext();){
            // http://wiki.eclipse.org/Java_-_Execute_Modified_Report_(BIRT)
            Object element = iter.next();
            if (element instanceof OdaDataSourceHandle){
                OdaDataSourceHandle dsHandle = (OdaDataSourceHandle) element;
                String key = dsHandle.getName();
                if (key == null){
                    continue;
                }
                IJDBCParameters jdbcParams = jdbcConnections.get(key);
                if (jdbcParams == null){
                    continue;
                }
                try {
                    dsHandle.setProperty( "odaDriverClass", jdbcParams.getDriverName());
                    dsHandle.setProperty( "odaURL", jdbcParams.getConnectionString());
                    dsHandle.setProperty( "odaUser", jdbcParams.getUserName());
                    dsHandle.setProperty( "odaPassword", jdbcParams.getPassword());
                } catch (SemanticException e) {
                    throw new UncheckedException(e);
                }
            }
        }
    }

Probably you fixed your issue already, but maybe someone will be looking for this in the future. First of all 4.2.x versions had problems with passed connections. I did not observed the same errors in 4.4.2.

Other thing, I do not get why you are trying to pass ReportRenderer as a connection in lines:

var conn = new ReportRenderer();
reportContext.getAppContext().put("OdaJDBCDriverPassInConnection", conn);

The passed object in here should be a java.sql.Connection object.

Therefore,

Map<String, Object> appContext = task.getAppContext();
appContext.put("OdaJDBCDriverPassInConnection", myConnection);
appContext.put("OdaJDBCDriverPassInConnectionCloseAfterUse", false);
task.setAppContext(appContext);

looks correct as long as myConnection is an implementation of java.sql.Connection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM