2013-03-14 66 views
1

我是新來的Birt。從Java應用程序傳遞連接到Birt報告

我試圖通過從我的Java應用程序報告的連接,但我得到一個錯誤:

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)

這是創建和發佈報告我的Java代碼:

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... 
     } 
} 

這是我在劇本「beforeOpen」數據源寫的代碼:

importPackage(Packages.it.lfiammetta.birt); 

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

我設置C lasspath。

我使用的Birt版本是4.2.1。

在此先感謝您的幫助,我爲我的英語道歉。

回答

1

我做了從Java代碼(IJDBCParameters - 實際參數JDBC連接,我正在尋找的名字連接 - 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); 
       } 
      } 
     } 
    } 
2

也許你固定您的問題了,但也許有人會在未來尋找這個。首先,4.2.x版本在傳遞連接時遇到了問題。我沒有在4.4.2中觀察到同樣的錯誤。

其他的事情,我不知道爲什麼你試圖傳遞ReportRenderer在線路連接:

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

在這裏傳遞的對象應該是java.sql.Connection對象。

因此,

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

看起來只要myConnection正確的是java.sql.Connection

實現