Skip to main content

Pentaho+ documentation has moved!

The new product documentation portal is here. Check it out now at docs.hitachivantara.com

 

Hitachi Vantara Lumada and Pentaho Documentation

Embed the reporting engine into a Java application

Parent article

This section shows in detail how to build a simple reporting application around the Pentaho Reporting engine. There are three classes for the two examples shown in this section:

  1. AbstractReportGenerator.java
  2. Sample1.java
  3. Sample2.java

You can find the full example source code, plus the .prpt report file they use, in the /source/org/pentaho/reporting/engine/classic/samples/ directory in the Pentaho Reporting SDK.

Work with the reporting engine samples

In the samples, the interaction with the Pentaho Reporting engine follows these basic steps:

Procedure

  1. Boot (initialize)

  2. Get the report definition

  3. Get the data for the report (if it is created outside of the report definition)

  4. (Optional) Get any report generation parameters

  5. Generate the report output in the requested format

Next steps

With the samples, this allows us to create an abstract base class for all the samples (AbstractReportGenerator). This class defines the abstract methods:
  • getReportDefinition(): this loads/creates/returns the report definition
  • getDataFactory(): this returns the data to be used by the reporting engine (if the report definition does not tell the engine how to retrieve the data).
  • getReportParameters(): this returns the set of parameters the reporting engine will use while generating the report

The generateReport() method tells the reporting engine to generate the report using the above method, and creates the output in one of the following methods (using the OutputType parameter): HTML, PDF, or XLS (Excel). A full list of output types is listed later in this section, but to keep these examples simple, we'll concentrate on these three.

Sample1.java

In this sample, the getReportDefinition() method loads the report definition from a PRPT file created using the Pentaho Report Designer. This report definition defines the following:
  • Data Query (retrieving a list of customers based on a set of customer names)
  • Report Title
  • Report Header – set of 4 columns (Customer Number, Customer Name, Postal Code, Country)
  • Report Data – set of 4 columns (Customer Number, Customer Name, Postal Code, Country)
The getDataFactory() method returns null to indicate that no data factory is required to be provided. In this example, the source of data is defined in the report definition.

The getReportParameters() method defines three parameters in a HashMap:

Parameter NameParameter ValueDescription
Report TitleSimple Embedded Report Example with ParametersThe value of this parameter will be placed in the Report Title that is centered on the top of each page in the report. In the report definition, the Report Title field is a Text Field whose value is “Report Title”. This indicates that the field will use the value of the parameter “Report Title” when the report is generated.
Col Headers BG ColoryellowThe value of this parameter will be used as the background color of the column header fields. In the report definition, all four of the column header fields are defined with a bg-color style of “=[Col Headers BG Color]”. This indicates that the value of the “Col Header BG Color” parameter will be used as that value.
Customer Names
"American Souvenirs Inc",
"Toys4GrownUps.com",
"giftsbymail.co.uk",
"BG&E Collectables",
"Classic Gift Ideas, Inc"
The value of this parameter defines a set of Customer Names that will be used in the data query. This allows the sample to define which customers will be used in the report at the time the report is generated.
SELECT
    "CUSTOMERS"."CUSTOMERNAME",
    "CUSTOMERS"."POSTALCODE",
    "CUSTOMERS"."COUNTRY",
    "CUSTOMERS"."CUSTOMERNUMBER"
FROM
    "CUSTOMERS"
WHERE
    "CUSTOMERS"."CUSTOMERNAME" IN (${Customer Names})
The main() method creates an output filename in which the report will be generated and then starts the report generation process.

Sample2.java

In this sample, the getReportDefinition() method creates a blank report and sets the query name to ReportQuery. It then adds a report pre-processor called RelationalAutoGeneratorPreProcessor.

Report pre-processors execute during the report generation process after the data query has been executed but before the report definition is used to determine the actual layout of the report. The benefit of this is that the RelationalAutoGeneratorPreProcessor will use the column information retrieved from the data query to add header fields in the Page Header and data fields in the Item Band of the report definition for each column of data in the result set.

The getDataFactory() method first defines the DriverConnectionProvider which contains all the information required to connect to the database. It then defines the DataFactory which will use the connection provider to connect to the database. The Data Factory then has the query set which will be used in report generation. The query name ReportQuery must match the query name defined when the report definition was created or else the report will contain no data.

The getReportParameters() method is not used in this example, so it returns null.

The main() method creates an output filename in which the report will be generated and then starts the report generation process.

Sample 0: The base case

This sample shows the basic logic for creating a report, leaving the details of input and output to the classes that extend it.

Learn more

Sample 1: Static report definition, JDBC input, PDF output

This sample is the simplest embedding scenario that produces a static report with JDBC input and a PDF output to the local file system.

Learn more

Sample 2: Static report definition, JDBC input, HTML output

This sample produces a static report with JDBC input and HTML file output.

Learn more