Friday, November 11, 2005

Using Embedded Java Objects within BIRT

Embedded Java Object Reporting

Most often reporting tools pull data into the design tool or engine and then format, group and aggregate the data.  The report developer fires up the design tool, creates the data sources and retrieves the result sets.  The developer then proceeds to do the work of reporting.  After completing the design, the report is usually deployed to some type of server.  This model is usually sufficient for most applications.

In embedded applications this model does not always perform to expectations.  for instance, assume that I have built an RCP application that handles benefits.  The user of this application works through several data collection screens.  At the completion of this task the user needs to print a summary of all the picks that have been made.  As the choices were selected they are persisted to a database, so the natural inclination is to start up the report designer and report the choices from the database.  This is not optimal for many reasons.  If we already have all the choices the user made within a Java Object, I why would rather use it as the reports data source.  Doing this would save at least one more trip back to the database.  Some tools do this by allowing an Object type to be passed in as a parameter.  In BIRT, this can be done in several different ways.  

One of the more interesting and powerful ways is to use the addScriptableJavaObject method of the RunAndRenderTask class.  This method allows the BIRT scripting engine to access the Java Object and use it as if it were native to the engine.  This gives the application designer a lot of capabilities, such as passing in usernames, beans, and HTTPSession objects.
The method takes two parameters.  The first is the name the script engine will reference the object by.  The second is the actual Object instance.

In the case of our example, I add the BenefitObjectInstance as below.

//excerpt from the RunReport.java Example

IRunAndRenderTask task = engine.createRunAndRenderTask( report );
.
.
.
task.setParameterValues( params );
task.addScriptableJavaObject("BenefitObject", BenefitObjectInstance);
task.run( );


To use this object within the designer, I will reference the BenefitObject in script. I add a Data control and then place a reference to my Object in the Expression builder.


//Value for the Data Control
benefit =BenefitObject;
benefit.getSocialSecurityNumber();






In BIRT 2.0 an additional feature is being added that allows the application to pass in a full context object to the data layer.  This will not only allow reporting off of existing Objects, but will allow things like credentials, parameters and connection handles to be passed in from the application.  With BIRT’s M2 release, this feature is available now.  I will write up a short example of how to use it in a later post.  

2 comments:

Anonymous said...

Thanks. The ability to report agains objects will make BIRT useful to me. I'm anxious to see the 2.0 follow up.

krishnamurthy said...

Hey
Kindly post in a sample application. Hope that would help us a lot.
Thanks