- Overview of BIRT scripting
- Events overview
- Event order sequence
Events overview
When writing event handlers, understanding the event order is imperative. The order in which events fire depends on several factors. These factors include which BIRT processing phase is executing, the engine task executing the process, and what event type is processing.
Engine task processes
The scripting chapters make continuous reference to engine task processes. This section provides an overview of what these processes are and how they affect scripting.
The Report Engine that executes reports is task-oriented and provides three tasks related to the execution and rendering of reports. These tasks are RunAndRenderTask, RunTask, and RenderTask.
The RunAndRenderTask uses one process to open the report design and produce a specific output, such as PDF. The RunTask opens a report design and executes the report producing a report document file with a .rptdocument extension. This report document is an intermediate binary file that can then be used by a RenderTask to produce a report output type, such as HTML, PDF, PPT, PS, Word, or XLS. The RenderTask opens a report document (.rptdocument) and renders the appropriate output format. This task can be executed any time after a RunTask. This task can even occur on a separate system.
BIRT Report Designer uses the report engine to show a preview of a report. Selecting Preview in the Editor launches a RunAndRenderTask to produce the report. Selecting any other previewing option in BIRT Report Designer toolbar launches a RunAndRenderTask to produce the output, with the exception of the Run report in BIRT Web Viewer, which produces the report using a RunTask followed by a RenderTask.
The tasks RunTask and RenderTask can be used when the tasks need to occur at separate times or on separate systems. Using separate run and render tasks therefore requires two processes to run and render a report to a particular output format.
When RunAndRenderTask processes a report, the event firing order is different from the RunTask and RenderTask are used as two separate processes. The differences in processing are discussed throughout these chapters.
The following sections describe how the report engine processes reports in the BIRT Report Designer environment.
BIRT Web Viewer
The example BIRT Web Viewer application is a J2EE application that encapsulates the report engine to produce reports. This viewer contains three Servlet mappings used to generate reports. These are the frameset, run, and preview mappings.
In the example web viewer, two processes, RunTask and RenderTask, generate and render to the output format to create a report document. Selecting the Export report icon in the Web Viewer toolbar initiates a RenderTask to execute on the current report document.
BIRT processing phases
The BIRT services for generating and presenting report data create a report during the following processing phases:
-
Preparation
RunTask or RunAndRender Task prepare the report items for execution.
-
Generation
RunTask or RunAndRender Task create an instance of each report item, connect to the data source, execute the data sets, and process the data to produce the report.
-
Presentation
RenderTask or RunAndRender Task select the correct emitters to produce the output specified for the report.
The RunTask handles the preparation and generation phases and the Render Task handles the presentation phase. RunAndRenderTask handles all phases of report processing. The types of events and the order in which these events fire depends on the processing phase currently executing in an engine task.
BIRT event types
BIRT supports the following types of events:
- Parameter
- Report design
- Data source and data set
- Report item
Each event type has a series of events that fire during report processing.
Parameter events
BIRT currently supports two parameter level events that can be used to customize the parameter selection choices and default values before the parameter entry box is displayed. In addition, BIRT supports one parameter level event that is called after the parameters have been selected. These events are summarized in Table 7-1. These events are currently available only in JavaScript. Parameter events fire only if the report contains parameters.
Table 7-1. Parameter events
Event |
Description |
getDefaultValueList |
Sets the parameter's default value. If the parameter is set to allow multiple values, this script should return an array of values that will then be preselected in the parameter entry box. This event fires first. |
getSelectionValue List |
Returns a single value or an array of values. This event fires before the parameter entry box displays for parameters presenting a list of values. Simple text parameters do not call this event. |
validate |
This event fires after the user selects the report parameters and before the initialize event. This event fires for each parameter and returns a value of true or false. Returning false throws an exception that states which parameter failed validation. Returning true processes the report normally. |
Listing 7-1 provides an example of getDefaultValueList and getSelectionValueList event handlers for a parameter formatted as a list box. The array dSLArray contains four values that a report user can select. The array dVLArray contains two values that are the default values of the parameter. These values are selected in the list of available values when the user runs the report.
Listing 7-1. Example event handlers for a parameter formatted as a list box
//getDefaultValueList var dVLArray = []; dVLArray[0]= "10104"; dVLArray[1] = "10108"; dVLArray; //getSelectionValueList var dSLArray = []; dSLArray[0]= "10101"; dSLArray[1]= "10104"; dSLArray[2]= "10105"; dSLArray[3] = "10108"; dSLArray;
The getDefaultValueList can be useful to provide date parameters as well.
If changes to the parameter are necessary after a user enters a parameter or if extra parameter validation is required, write an event handler for the validate event. The following example shows a validate script for a string parameter:
params["MyParameter"].value = "Something Else"; true;
Report design events
Report design events fire for all reports. Table 7-2 describes these events. The events support event handlers to provide additional processing of the report.
Table 7-2. Report design events
Event |
Description |
initialize |
Fires every time a task accesses the report design (.rptdesign) or the report document (.rptdocument). This event occurs once when using RunAndRenderTask. When the processing phases run separately, RunTask triggers once for the generation phase and RenderTask triggers once for every render operation. Displaying the first page of a report in the presentation phase and using the page navigation control to access a new page also triggers this event handler. |
beforeFactory |
Fires just prior to the generation phase after the elements in the report have been prepared in the preparation phase. This event handler fires only once. Use beforeFactory( ) when modifications to the report design are required before execution. |
beforeRender |
Fires just prior to the presentation phase and is called for every render operation. In the RunAndRender task, this event occurs once just after the beforeFactory event. |
afterFactory |
Fires at the conclusion of the generation phase and fires only once. If using a RunTask or RunAndRenderTask, this event is the last one fired. If using a RenderTask, this event does not execute. |
afterRender |
Fires at the conclusion of the presentation phase for a specific render operation. If using a single RunAndRenderTask process, this event is called only once. If using separate RunTask and RenderTask processes, this event fires for every render operation. |
onPageStart |
Fires prior to placing content on a specific page and supports the use of page-level variables to modify the page. This event fires only when processing phases run separately, for example, a RunTask followed by a RenderTask. This event fires prior to the onPageBreak events for the individual elements to be placed on the page. This event is also available on the master page. |
onPageEnd |
Fires after all the onPageBreak events have fired for the content to be placed on a page and prior to evaluating the autotext items for the page. This event is also available on the master page. |
Types of data source and data set events
There are several kinds of data sources and data sets. A data source can be a flat file, a JDBC data source, a scripted data source, a web services data source, or an XML data source. All data sources have a common set of event handlers. A scripted data source has two additional event handlers.
Data set events can be called multiple times to support multipass aggregation and data set sharing. It is not advisable to write event handlers that rely on the data set event firing order. An additional issue is that data set event handlers may only be called once because of data set caching.
Table 7-3 describes the data source events.
Table 7-3. Data source events
Event |
Description |
beforeOpen |
This event is most often used to modify public properties of the data source, including database URL, username, password, driver class and JNDI URL. This event fires only once, before the connection to the data source opens event, when the connection uses multiple data sets. Fires prior to opening a connection to a data source. |
afterOpen |
Fires after the connection to the data source opens. |
beforeClose |
Fires at the conclusion of the generation phase, just prior to closing the data source connection. |
afterClose |
Fires after the data source connection closes. |
open |
Fires only for a scripted data source, providing a location for the developer to set up a connection to an external source. |
close |
Fires only for a scripted data source, providing a location for the developer to close a connection to an external source. |
Data source and data set events fire in the generation phase prior to the onCreate event of the report item that uses them. Data set events can fire several times. The event order is covered in more detail later in this chapter. Table 7-4 describes the data set events.
Table 7-4. Data set events
Event |
Description |
beforeOpen |
Fires prior to opening a data set and used most often to modify public properties of the data set. For example, when using a JDBC data set, the query text can be altered using this event. This event fires for every report item bound to the data set. If two tables use the same data set, the data set is called twice, resulting in this event firing twice. BIRT 2.6 allows report items to be bound to other report items, in which case the data set is called only once, resulting in the event triggering only once. Data set caching options continue to be added which affect how often data source and data set events fire. |
afterOpen |
Fires after the data set opens. |
onFetch |
Fires as the data set retrieves each row of data. This event fires for all rows of data before the onCreate event for the particular report item that uses the data set. |
beforeClose |
Fires before closing the data set after creating the report item that uses the data set. |
afterClose |
Fires after the data set closes. |
open |
Fires for a scripted data set, providing a location for the developer to set up a data set. |
fetch |
Fires for a scripted data set, providing a location for the developer to populate rows from the scripted data set. |
close |
Fires for a scripted data set, providing a location for the developer to close the set. |
Data source and data set events fire prior to being used on a data bound item. If the data set is never used in the report, the data source and data set are never called. A scripted data set is a data set that accesses a scripted data source. A non-scripted data set is one that accesses a standard data source. Use the scripted data source and data set to write custom code to retrieve data values.
Scripted data source events
A scripted data source contains the afterOpen, afterClose, beforeOpen, and beforeClose events common to all data sources, and open and close events. Use the event handlers for the open and close events to perform the actions of opening and closing the data source.
Scripted data set events
A scripted data set contains the afterOpen, afterClose, OnFetch, beforeOpen, beforeClose, open, close, and fetch events. Use the event handlers for the open and close events to perform the actions of opening and closing the data set. Use the fetch method to fetch a row from the data source. Using a scripted data set requires an event handler to be written for the fetch event.
ReportItem events
ReportItem events trigger for report items that are placed in the report. Most items support writing event handlers for the events listed in Table 7-5.
Table 7-5. Report item events
Event |
Description |
onPrepare |
Fires at the beginning of the preparation phase before data binding or expression evaluation occurs. This event is useful for changing the design of the item prior to generating the item instance. |
onCreate |
Fires at the time the generation phase creates the element. This event is useful when a particular instance of a report item needs alteration. |
onRender |
Fires in the presentation phase. This event is useful for operations that depend on the type or format of the output document. |
onPageBreak |
Fires for all report items currently on the page when the page break occurs. Not all report items support the onPageBreak event. |