Chapter 11. Using a Scripted Data Source

BIRT supports accessing a data source using JavaScript code. A data source that you access using JavaScript is called a scripted data source. With a scripted data source, you can access objects other than a SQL, XML, or text file data source. Because the JavaScript code for accessing and managing a scripted data source can wrap Java objects, a scripted data source can be an EJB, an XML stream, a Hibernate object, or any other Java object that retrieves data.

A scripted data source must return data in tabular format so that BIRT can perform sorting, aggregation, and grouping.

Creating a scripted data source and scripted data set

Creating a scripted data source and creating a non-scripted data source are similar. The differences between creating a scripted data source and a nonscripted data source are:

  • The report developer must select Scripted Data Source from the list of data source types when creating a scripted data source.

  • The report developer can provide code for two event handler methods, open( ) and close( ), that are only available for a scripted data source.

Every scripted data source must have at least one scripted data set. The differences between creating a scripted data set and a non-scripted data set are:

  • The report developer must associate the scripted data set with a scripted data source.

  • The report developer must provide code for the scripted data set fetch( ) event handler method.

  • The report developer uses a different dialog for identifying the columns of a scripted data set than for a non-scripted data set.

When you use BIRT Report Designer to create a scripted data source, you must perform the following tasks:

  • Create a scripted data source.

    Right-click on Data Sources in the data explorer and select Scripted Data Source in the list of data source types.

  • Create a scripted data set.

    Right-click on Data Sets in the data explorer and select a scripted data source from the list of available data sources.

  • Define output columns.

    Define the names and types of output columns, using the scripted data set editor.

  • Supply code for the data source open( ) and close( ) methods.

    There are two scripted data source event handler methods, open( ) and close( ). It is not mandatory that you implement either method, but most applications require the use of the open( ) method.

    Use the open( ) method to initialize a data source. Typically, you create a Java object for accessing the data source in the open( ) method.

    Use the close( ) method to clean up any loose ends, including setting object references to null to ensure that the objects are deleted during garbage collection.

  • Supply code for the data set methods.

    There are three scripted data set event handler methods, open( ), fetch( ), and close( ). It is mandatory that you implement only the fetch( ) method.

    Use the open( ) method to initialize variables and to prepare the data source for fetching data.

    Use the fetch( ) method to get a row of data from the data source and to populate the columns of the row object. The fetch( ) method must return either true or false. A true value tells BIRT that there is another row to process. A false return value signifies that there are no more rows to process.

    Use the close( ) method to perform cleanup operations.

  • Place the columns on the report layout.

    Place a data set column on a report layout the same way you place a column for a non-scripted data set.

The following tutorial guides you through the procedure required to perform each task in this process.

Tutorial 2: Creating and scripting a scripted data source

This tutorial provides instructions for creating and scripting a simulated scripted data source. Although this tutorial does not use an actual data source, you learn the process for scripting a real data source.

In this tutorial, you perform the following tasks:

  • Create a new report.

  • Create a scripted data source.

  • Create a scripted data set.

  • Supply code for the open( ) and close( ) methods of the data source.

  • Supply code for the data set open( ) method.

  • Define output columns.

  • Place the columns on the report layout.

  • Supply code for the data set fetch( ) method.

Task 1: Create a new report

In this task you create a new report in BIRT Report Designer and name it ScriptedDataSrc.rptdesign.

  1. Choose File →New →Report.

  2. In File Name in New Report, type:

    ScriptedDataSrc.rptdesign
  3. In Enter or Select the Parent Folder, accept the default folder. Choose Next.

  4. In Report Templates, select My First Report. Choose Finish. The BIRT report design screen appears. If a Cheat Sheet tab appears, close it.

Task 2: Create a scripted data source

In this task you create the new data source.

  1. In Data Explorer, right-click Data Sources and choose New Data Source. Select a Data Source type appears.

  2. In New Data Source, select Scripted Data Source.

  3. In Data Source Name, type:

    ScriptedDataSource
  4. Choose Finish.

    Data Explorer and the code window for ScriptedDataSource appear, as shown in Figure 11-1.

    Data Explorer and ScriptedDataSource code window

    Figure 11-1. Data Explorer and ScriptedDataSource code window

Task 3: Create a scripted data set

In this task, you create the new data set.

  1. In Data Explorer, right-click Data Sets. Choose New Data Set. New Data Set appears, as shown in Figure 11-2.

    New data set for a scripted data source

    Figure 11-2. New data set for a scripted data source

  2. In Data Set Name, type:

    ScriptedDataSet
  3. Choose Finish.

  4. In Data Explorer, select ScriptedDataSet. The script window for the data set appears, as shown in Figure 11-3.

    Code window for ScriptedDataSet

    Figure 11-3. Code window for ScriptedDataSet

Task 4: Supply code for the open( ) and close( ) methods of the data source

In the open( ) method, you open the data source. In the close( ) method, you do cleanup tasks. In this tutorial, there is no actual data source but typically you need to place some code in these methods. The open( ) method is the default selected method upon creating a data set.

  1. If necessary, select open from the pull-down list of methods.

  2. Type the following code into the code window for the open( ) method:

    dummyObject = new Object( );

    The preceding code is a placeholder for the purpose of this simplified example. In a typical application, you use this method to initialize a Java object that provides access to the data for the report.

  3. Select close from the pull-down list of methods.

  4. Type the following code into the code window:

    dummyObject = null;

Task 5: Supply code for the open( ) method of the data set

When you create the data set, the open( ) method is selected by default. Use the open( ) method of the data set to do initialization, such as defining a counter and setting it to zero.

  1. Select open from the pull-down list of methods.

  2. Type the following code into the code window:

    recordCount = 0;

Task 6: Define output columns

To create the output columns for a scripted data set, you must edit the data set. The columns you create in the data set editor are the columns that the data set fetch( ) method generates.

  1. In Data Explorer, double-click ScriptedDataSet. Edit Data Set—ScriptedDataSet appears, as shown in Figure 11-4.

    Edit Data Set for a scripted data source

    Figure 11-4. Edit Data Set for a scripted data source

  2. Select Output Columns. Output Columns appears.

  3. In the Name column of the first row, type:

    col1
  4. Select the Type column of the first row. Select Integer from the drop-down list. Output Columns contains the definition of one output column, as shown in Figure 11-5.

    Column name and type in Output Columns

    Figure 11-5. Column name and type in Output Columns

  5. In the Name column of the second row, type:

    col2
  6. In the Type column of the second row, select String.

  7. In the Name column of the third row, type:

    col3

    In the Type column of the third row, select Float. Output Columns contains the definition of three output columns, as shown in Figure 11-6. Choose OK.

    Column definitions

    Figure 11-6. Column definitions

Task 7: Place the columns on the report layout

You place columns for a scripted data set in the same way as for a non-scripted data set.

  1. On ScriptedDataSrc.rptdesign, select Layout.

  2. Drag Table from Palette onto the layout.

  3. Accept the default table size of three columns and one detail row.

  4. In Data Explorer, expand ScriptedDataSet. The three columns you created appear in Data Explorer, as shown in Figure 11-7.

    New columns in ScriptedDataSet

    Figure 11-7. New columns in ScriptedDataSet

  5. Add the columns to the report detail row:

    1. Drag col1 from Data Explorer to the first column of the report detail row.

    2. Drag col2 from Data Explorer to the second column of the report detail row.

    3. Drag col3 from Data Explorer to the third column of the report detail row.

    Figure 11-8 shows the three columns of the data set in the layout editor.

    New columns in the report design

    Figure 11-8. New columns in the report design

  6. Choose Preview.

    The preview of the report appears, as shown in Figure 11-9.

    Report preview, showing the new columns

    Figure 11-9. Report preview, showing the new columns

Task 8: Supply code for the data set fetch( ) method

Use the fetch( ) method to process row data. The fetch( ) method must return either true or false. Fetch( ) returns true to indicate that there is a row to process. Fetch( ) returns false to indicate that there are no more rows to process. The fetch( ) method also calculates the values of computed fields.

The report only has column headings at this point. To include data, you must add code to the fetch( ) method.

  1. Choose the Layout tab.

  2. Right-click ScriptedDataSet in Data Explorer. Choose Edit Script.

  3. Select fetch in the drop-down list of methods.

  4. Select fetch from the pull-down list of methods in the data set code window.

  5. Type the following code into the code window. This code limits the number of rows that appear in the report to 19.

    if(recordCount < 20) {
       recordCount++;
       row.col1 = recordCount;
       row["col2"] = "Count = " + recordCount;
       row[3] = recordCount * 0.5;
       return true;
    }
    else return false;

    The three formats in the preceding script illustrate the different ways you can specify a column of a row. The code appears as shown in Figure 11-10.

    Code that generates column output

    Figure 11-10. Code that generates column output

  6. Choose Preview.

    The report now contains 20 rows and 3 columns of data, as shown in Figure 11-11.

    Report preview

    Figure 11-11. Report preview

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 from within 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 of code in the open( ) method. You typically follow the importPackage statement with code to create an instance of the Java object, as shown in the following example:

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 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( );

Then, you must return true to signal BIRT there is a valid row of data to process:

return true;

Cleaning up in the data set close( ) method

You can perform any cleanup 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 locate 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.

    Consider using this option if your Java class is in this location for other reasons.

  • The Java class is in <ECLIPSE_INSTALL>pluginsorg.eclipse.birt.report.viewerirtWEB-INFlib.

    Consider using this option if your Java class is built and 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.

    Consider using this option if you are developing your Java class simultaneously with developing your BIRT report.

Deploying your Java class

Before you deploy your BIRT report to an application server, you must place your Java class in a JAR file. You must then deploy that JAR file to the proper location on the application server so that the BIRT report viewer can find it at run time. For more information about where to deploy Java classes on an application server, see the chapter about deploying BIRT to an application server.

Using input and output parameters with a scripted data set

The scritped data set JavaScript event handler methods have two arrays you can use to access parameters, inputParams and outputParams. The inputParams array contains one string for every parameter whose direction is defined as input. The outputParams array contains one string for every parameter whose direction is defined as output.

For example, assume that you have a scripted data set with an input and an output parameter, as shown in Figure 11-12:

A scripted data set, with input and output parameters

Figure 11-12. A scripted data set, with input and output parameters

You can get and set the values of the out_msg and in_count parameters by using the inputParams and outputParams arrays as in the following example:

outputParams["out_msg"] = "Total rows: " +
   inputParams["in_count"];

You can access a parameter in the array either by the name of the parameter or by a 1-based index value. The inputParams and outputParams arrays are not accessible to Java event handlers.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset