ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Deploying BIRT
Pages: 1, 2, 3, 4

Adding the BIRT Report Engine to an RCP Application

The Report Engine download comes with the jars necessary to use the Report Engine API. In addition, it comes with plugins and configuration directories used to launch BIRT plugins that use OSGi. When building an RCP application that needs to run and render reports, using the Report Engine download is not necessary. The Report Engine API is already in plugin format within the Framework download. To use the API in your application, add the dependencies shown in Figure 5.



Dependencies needed to include BIRT Engine in RCP Application
Figure 5. Dependencies needed to include BIRT Engine

These dependencies are enough to compile Report Engine API code. To run the application, see the attached launch configuration for the needed plugins.

The attached example is very similar to the previous example. We use an SWT Browser to display the results of the run operation

The factory.createReportEngine method retrieves the report engine handle using the currently created Eclipse Platform. The engine is then used to run and render the report. You will notice that in this example the output is rendered to an output stream. This output stream is then used in the Browser.setText method to display the report, as shown in Figure 6.

private void previewReport() 
             throws EngineException {
    EngineConfig config = new EngineConfig();
    // Create the report engine
    IReportEngineFactory factory = 
              (IReportEngineFactory) Platform
          .createFactoryObject( 
                IReportEngineFactory.
                EXTENSION_REPORT_ENGINE_FACTORY );
    IReportEngine engine = factory.
                         createReportEngine( config );
    IReportRunnable design = null;

    try {
       // Open a report design - 
       // use design to 
       // modify design, retrieve
       // embedded images etc.
       String report = reportLocation.getText();
       FileInputStream fs = new 
                       FileInputStream(report);
       design = engine.openReportDesign(fs);
       IRunAndRenderTask task = engine.
                     createRunAndRenderTask(design);

       // Set Render context to handle url 
       // and image locations
       HTMLRenderContext renderContext = 
                            new HTMLRenderContext();    

       renderContext.setImageDirectory(
                                  "c:/test/image");     
       HashMap< String, HTMLRenderContext > 
            contextMap = new 
            HashMap< String, HTMLRenderContext >();
       contextMap.put( EngineConstants.
                 APPCONTEXT_HTML_RENDER_CONTEXT, 
                 renderContext );                       
       task.setAppContext(contextMap);

       // Set rendering options - 
       // such as file or stream output,
       // output format, whether it is 
       // embeddable, etc
       // Render report to 
       // Byte Array       
       IRenderOption options;
       options = new HTMLRenderOption( );
       ByteArrayOutputStream bos = 
                   new ByteArrayOutputStream();
                           
       options.setOutputStream(bos);
       options.setOutputFormat("html");

       task.setRenderOption(options);

       // run the report and destroy the engine

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

       //set Browser text accordingly
       browser.setText(bos.toString());
       engine.destroy();
   } catch (Exception e) {
       e.printStackTrace();
   }
}

Example RCP Application with BIRT Engine Embedded
Figure 6. Example RCP Application with BIRT Engine Embedded

Conclusion

The intent of this article is to illustrate deployment models available to the BIRT developer. The BIRT project thrives on community feedback, so please feel free to post your comments or questions in the BIRT news group.

Resources

Jason Weathersby is the BIRT Evangelist at Actuate Corporation and a member of the Eclipse Business Intelligence and Reporting Tools (BIRT) Project Management Committee (PMC).


Return to ONJava.com.