Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a datasource for a BIRT report programmatically?

I have a BIRT report which connects to our test database. In the productive environment I would like to supply a datasource which is provided by the container through jndi.

How would I set the datasource programmatically for the given report?

    ...
    IReportRunnable design = birtEngine.openReportDesign ( new File ( properties.getProperty ( "reportPath" ), report + ".rptdesign" ).getAbsolutePath () );
    IRunAndRenderTask task = birtEngine.createRunAndRenderTask ( design );

    PDFRenderOption options = new PDFRenderOption ();
    options.setOutputFormat ( PDFRenderOption.OUTPUT_FORMAT_PDF );
    options.setOutputStream ( out );
    task.setRenderOption ( options );
    for ( Entry<String, Object> entry : parameters.entrySet () )
    {
        task.setParameterValue ( entry.getKey (), entry.getValue () );
    }

    task.run ();
    task.close ();
    ...

I guess I would have to modify the design but on the other hand task has a method setDataSource but that looks a bit like I would have to supply some xml dom elements.

like image 792
Mauli Avatar asked Jul 14 '10 08:07

Mauli


1 Answers

This worked for me. I got the context and from context got the dataSource and passed the connection to Birt report like below

  Context initialContext = new InitialContext();
     if ( initialContext == null){
     System.out.println("JNDI problem. Cannot get InitialContext.");
        }
        DataSource datasource = (DataSource)initialContext.lookup("java:/datasources/SAMPLE");
     task.getAppContext().put("OdaJDBCDriverPassInConnection", datasource.getConnection());
like image 130
Sasi Avatar answered Oct 28 '22 10:10

Sasi