Listing 1:
Creating and configuring a report engine object
// Create an EngineConfig object.
EngineConfig config = new EngineConfig( );
// Set up the path to your engine home directory.
config.setEngineHome( "C:/Program Files/birt-runtime-2_1_0/ReportEngine" );
// Explicitly set up the stand-alone application
IPlatformContext context = new PlatformFileContext( );
config.setEngineContext( context );
// Set up writing images or charts embedded in HTML output.
HTMLCompleteImageHandler imageHandler = new HTMLCompleteImageHandler( );
HTMLEmitterConfig hc = new HTMLEmitterConfig( );
hc.setImageHandler( imageHandler );
config.setEmitterConfiguration( RenderOptionBase.OUTPUT_FORMAT_HTML, hc );
// Create the engine.
ReportEngine engine = new ReportEngine( config );
Listing 2:
Accessing a report engine design
String designName = "./SimpleReport.rptdesign";
IReportRunnable runnable = null;
try {
runnable = engine.openReportDesign( designName );
}
catch ( EngineException e )
{
System.err.println
( "Design " + designName + " not found!" );
engine.shutdown( );
System.exit( -1 );
}
// Get the value of a simple property.
String author = ( String ) runnable.getProperty( IReportRunnable.AUTHOR );
Listing 3:
Accessing a report document
String dName = "./SimpleReport.rptdocument";
IReportDocument doc = null;
try { doc = engine.openReportDocument( dName );
} catch ( EngineException e ) {
System.err.println( "Document " + dName + " not found!" );
engine.shutdown( );
System.exit( -1 );
}
// Get the root of the table of contents.
TOCNode td = doc.findTOC( null );
java.util.List children = td.getChildren( );
long pNumber;
// Loop through the top level table of contents entries.
if ( children != null && children.size( ) > 0 ) {
for ( int i = 0; i < children.size( ); i++ ) {
// Find the required table of contents entry.
TOCNode child = ( TOCNode ) children.get( i );
if ( child.getDisplayString( ).equals( "103" ) ) {
// Get the number of the page that contains the data.
pNumber = doc.getPageNumber( child.getBookmark( ) );
System.out.println( "Page to print is " + pNumber );
}
}
}
Listing 4:
Setting the value of a single parameter
// Create a parameter definition task.
IGetParameterDefinitionTask task = engine.createGetParameterDefinitionTask(
runnable );
// Instantiate a scalar parameter.
IScalarParameterDefn param = (IScalarParameterDefn)
task.getParameterDefn( "customerID" );
// Get the default value of the parameter.
// The code assumes that the data type of the parameter,
// customerID, is Double.
int customerID = ((Double) task.getDefaultValue( param )).intValue( );
// Get a value for the parameter. This example does not
// provide the code for this task. This example assumes that
// this step creates a correctly typed object, inputValue.
// Set the value of the parameter.
task.setParameterValue( "customerID", inputValue );
// Get the values set by the application for all parameters.
HashMap parameterValues = task.getParameterValues( );
Listing 5:
Configuring properties on an IRunAndRenderTask object
// Create a run and render task object.
IRunAndRenderTask task = engine.createRunAndRenderTask( runnable );
// Set values for all parameters in a HashMap, parameterValues
task.setParameterValues( parameterValues );
// Validate parameter values.
boolean parametersAreGood = task.validateParameters( );
// Set the name of an output file.
HTMLRenderOption options = new HTMLRenderOption( );
String output = name.replaceFirst( ".rptdesign", ".html" );
options.setOutputFileName( output );
// Apply the rendering options to the task.
task.setRenderOption( options );
// Instantiate an HTML rendering context object and
// set the name of the directory for images.
HTMLRenderContext renderContext = new HTMLRenderContext( );
renderContext.setImageDirectory("image");
// Apply the rendering context to the task.
HashMap appContext = new HashMap( );
appContext.put( EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT, renderContext );
task.setAppContext( appContext );
Listing 6:
Setting up an external Connection object
HashMap contextMap = new HashMap( );
HTMLRenderContext renderContext = new HTMLRenderContext( );
contextMap.put
( EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT, renderContext );
// Get a connection from the pool
Connection myConnection = getConnectionFromPool( );
contextMap.put( "org.eclipse.birt.mydatapluginname",
myConnection );
task.setContext( contextMap );
Listing 7:
Generating a report from a report design or a report document
try {
task.run( );
System.out.println( "Created Report " + output + "." );
}
catch ( EngineException e1 ) {
System.err.println( "Report " + name + " run failed." );
System.err.println( e1.toString( ) );
}
engine.shutdown( );