Using a Java object to access a data source
A common use of a scripted data set is to access a Java object that accesses or generates the data for a report. This section shows how to access a Java class in the JavaScript code for a scripted data set.
Performing initialization in the data set open( ) method
Use the data set open( ) method to perform initialization tasks. A typical initialization task is to get an instance of the Java object that provides the data for the report.
When referring to a Java object, first import its package into the JavaScript environment. For example, the following code imports the package com.yourCompany.yourApplication:
importPackage( Packages.com.yourCompany.yourApplication );
This statement is like the import statement in Java and allows you to omit the package name when referencing a class. This statement is normally the first line in the open( ) method. You typically follow the importPackage statement with code to create the Java object instance, as shown in the following code:
var myList = MyListFactory.getList( );
A typical way of getting rows of data from a Java object is to use an iterator object. The open( ) method is the proper place to create an iterator object. For example, the following statement gets an iterator from myList:
var iterator = myList.getIterator( );
Getting a new row of data in the data set fetch( ) method
Once you have a way to get rows of data from your Java object, use the fetch( ) method to call the Java method that returns the rows. The fetch( ) method determines if there are any more rows of data and returns false if there are none, as shown in the following code:
if( iterator.hasNext( ) == false ){
return false;
}
At this point, the fetch( ) method can populate a row with the data that it gets from the iterator, as shown in the following code:
var node = iterator.next( );
row[1] = node.getFirstCol( );
row[2] = node.getSecondCol( );
row[3] = node.getThirdCol( );
You must return true to signal BIRT that there is a valid row of data to process, as shown in the following code:
return true;
Cleaning up in the data set close( ) method
You can perform any clean‑up in the close( ) method. This method is a good place to set to null any objects that you created. For example, the following code sets three object references to null:
myList = null;
iterator = null;
node = null;
Deciding where to place your Java class
If a scripted data source uses a custom Java class, that class must reside in a location where BIRT can find it. BIRT can find the Java class if its location meets any of the following requirements:
*The Java class is in the classpath of the Java Runtime Environment (JRE) under which Eclipse runs.
Use this option if your Java class is in this location for other reasons.
*The Java class is in <ECLIPSE_INSTALL>\plugins\org.eclipse.birt.report.viewer\birt\WEB-INF\lib.
Use this option if your Java class is built, tested, and ready to deploy.
*The Java class is a part of an Eclipse Java project that is in the same workspace as the BIRT report project.
Use this option if you are developing your Java class simultaneously with developing your BIRT report.
Deploying your Java class
Before deploying your BIRT report to an application server, you must place the Java class in a JAR file. Then, deploy the JAR file to the proper location on the application server, so that the BIRT report viewer can find it at run time.