Programming using the BIRT Reporting APIs : Generating reports from an application : Preparing to generate the report : Providing an external object to a report design
 
Providing an external object to a report design
BIRT supports an application passing previously instantiated objects into the report engine. Using this technique, the engine does not have to provide the code to create the object using information external to BIRT. The calling application can manipulate the object in memory immediately before calling the report engine. A typical use of external objects is in the BIRT scripting environment. After the application passes the object to the report engine, script expressions can reference the object by name at the appropriate stage in the report generation process. To supply an object to the report engine, use the application context, accessed from either the EngineConfig object or the task object, as shown in the code in Listing 12‑9.
Listing 12‑9 Setting up an external Connection object
myConnection mc = DriverManager.getConnection( myURL );
config = new EngineConfig( );
// Get the application context from the config or the task
HashMap hm = configOrTask.getAppContext( );
hm.put( "MyConnectionObject", mc );
configOrTask.setAppContext( hm );
 
// To refer to this object in a BIRT script
// or expression, use MyConnectionObject.myMethod()
Generating a binary report document
A binary report document contains all the information required to create a formatted report. The document contains appearance, data, and pagination information. Generate a binary report document if the same information must be rendered to multiple formats, if the output format is unknown at the time the report runs, or to render a subset of the pages in the report. Call an IRunTask.run( ) method to create a binary report document.
How to generate a binary report document
The code sample in Listing 12‑10 shows the use of an IRunTask object to set the output file name. The variable, engine, is a ReportEngine object. The variable, runnable, is an object of type IReportRunnable. The variable, name, is the name of the report design. The variable, str_DateStamp, is a String representing today’s date.
Listing 12‑10 Using an IRunTask object to create a binary report document
// Create a run task object.
IRunTask task = engine.createRunTask( runnable );
String output = name.replaceFirst( ".rptdesign", str_DateStamp + ".rptdocument" );
 
try {
task.run( output );
task.close( );
System.out.println( "Created document " + output + "." );
}
catch ( EngineException e1 ) {
System.err.println( "Report " + output + " creation failed." );
System.err.println( e1.toString( ) );
}