2012-05-21 68 views
2

我正在尋找編碼BIRT API w/a數據源的方向。我不確定如何配置我的應用程序以訪問創建的數據源。如果我能得到一些幫助,這將是很好的。這是我的位置。我已經通過BIRT RCP創建了一個報告。現在我正在尋找使用常規Java應用程序和Web應用程序生成報告。兩者都將通過我將要創建的GUI傳遞日期參數。兩者都需要有一個數據源。我已經看到了一些使用報表設計器的例子,但我沒有使用它。我也沒有使用BIRT報告創建器(RCP)GUI來生成這個。通過API設置BIRT數據源

感謝

import java.util.logging.Level; 
import org.eclipse.birt.core.framework.Platform; 
import org.eclipse.birt.report.engine.api.EngineConfig; 
import org.eclipse.birt.report.engine.api.EngineException; 
import org.eclipse.birt.report.engine.api.HTMLRenderOption; 
import org.eclipse.birt.report.engine.api.IReportEngine; 
import org.eclipse.birt.report.engine.api.IReportEngineFactory; 
import org.eclipse.birt.report.engine.api.IReportRunnable; 
import org.eclipse.birt.report.engine.api.IRunAndRenderTask; 

public class ReportGenerator { 
    public static void main(String args[]) throws EngineException { 
     ReportGenerator reportGenerator = new ReportGenerator(); 
     reportGenerator.executeReport(); 
    } 

    public void executeReport() throws EngineException { 

     IReportEngine engine=null; 
     EngineConfig config = null; 

     try{ 
      config = new EngineConfig();   
      config.setBIRTHome("C:\\birt-rcp-report-designer-3_7_2\\ReportEngine"); 
      config.setLogConfig("c:/temp/test", Level.FINEST); 
      Platform.startup(config); 
      IReportEngineFactory factory = (IReportEngineFactory) Platform.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY); 
      engine = factory.createReportEngine(config);  

      IReportRunnable design = null; 
      //Open the report design 
      design = engine.openReportDesign("ReportTemplates/testNoData.rptdesign"); 
      IRunAndRenderTask task = engine.createRunAndRenderTask(design); 
      task.setParameterValue("AuthorName", "Dale DeMott"); 
      HTMLRenderOption options = new HTMLRenderOption();  
      options.setOutputFileName("output/resample/Parmdisp.html"); 
      options.setOutputFormat("html"); 

      task.setRenderOption(options); 

      //Looking to create and insert a datasource here. 
      //task.setDataSource(some parameters here that represent the ds); 

      task.run(); 
      task.close(); 
      engine.destroy(); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } finally { 
      Platform.shutdown(); 
     } 
    } 
} 

回答

7

研究這個更深我找到了解決我自己的問題,我想我會分享答案。爲了澄清我正在尋找一種方法將我的代碼連接到數據源,以便我的BIRT報告查詢能夠運行。我發現我可以通過IGetParameterDefinitionTask對象傳遞連接,方法是獲取應用程序上下文,然後通過鍵值對設置在此對象中設置連接。

在下面的代碼中看到下面這行...... 'task.getAppContext()。put(「OdaJDBCDriverPassInConnection」,conn);'

public class ReportGenerator { 
public static void main(String args[]) throws EngineException { 
    ReportGenerator reportGenerator = new ReportGenerator(); 
    reportGenerator.executeReport(); 
} 

public void executeReport() throws EngineException { 

    IReportEngine engine=null; 
    EngineConfig config = null; 

    try{ 
     config = new EngineConfig();   
     config.setBIRTHome("C:\\birt-rcp-report-designer-3_7_2\\ReportEngine"); 
     config.setLogConfig("c:/temp/test", Level.FINEST); 
     Platform.startup(config); 
     IReportEngineFactory factory = (IReportEngineFactory) Platform.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY); 
     engine = factory.createReportEngine(config);  

     IReportRunnable design = null; 
     //Open the report design 
     design = engine.openReportDesign("ReportTemplates/testNoData.rptdesign"); 
     IRunAndRenderTask task = engine.createRunAndRenderTask(design); 
     task.setParameterValue("AuthorName", "Dale DeMott"); 
     HTMLRenderOption options = new HTMLRenderOption();  
     options.setOutputFileName("output/resample/Parmdisp.html"); 
     options.setOutputFormat("html"); 

     task.setRenderOption(options); 

     //Connection helper is a utility class used to create a connection to your 
     //database and return it. 
     ConnectionHelper connectionHelper = new ConnectionHelper(); 
     Connection conn = connectionHelper.getConnection(); 
     task.getAppContext().put("OdaJDBCDriverPassInConnection", conn); 

     task.run(); 
     task.close(); 
     engine.destroy(); 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } finally { 
     Platform.shutdown(); 
    } 
} 

}