The World's Most Widely Used Business Intelligence Software
0

Setting Up Event Driven Reporting in JasperServer Tutorial

Have Sales Contact Me
Test Drive Jaspersoft Now

Introduction

This tutorial covers options and limitations for implementing event driven reporting in JasperServer.

Example: In this tutorial, we use the following example to show how to configure a report to be viewed in JasperServer, and then how to trigger an email when a certain event happens during report execution.

  • A list of customers is filtered by the date they opened an account
  • A report is deployed to JasperServer and can be executed manually
  • A wrapper report is created which sends an email when more than 60 customers have opened an account
    • This will be simulated by changing the database manually
  • Samples files to follow this example.

Product Versions

This tutorial works with version 3.x and later.

User

Report Developer

Objectives

This guide will enable you to:

Pre-Requisites

  • JasperServer and iReport must be installed
    • In this tutorial the iReport JasperServer plug-in is used for deployment and report execution.
  • A basic understanding of report creation, deployment and scheduling is required
  • JasperServer must be configured to send emails
    • Refer the JasperServer Professional Administrators Guide: Chapter 5.6 Configuring a Mail Server, located in the <js-install>/docs folder


 

Preparing the Environment

To execute the example described above, extract the samples to create the database and set up the connections.

Step Action
1

 

Create the database in MySQL

  • Create an empty database called gems_eventdrivenreporting
  • MySQL command: create database gems_eventdrivenreporting
  • Load the sample dump mysqldump.sql into the new database
  • MySQL command: use gems_eventdrivenreporting
  • MySQL command: source db_sample.sql
2

 

Set up the database connection in iReport

  • Open iReport and set up the connection to the database

Deploy the Data Source to JasperServer using the iReport JasperServer plug-in

  • To deploy the data source to JasperServer you will need to use the JasperServer Repository plug-in
  • Navigate to the desired location within the JasperServer repository
    • Example: data sources folder
  • Right-click the data sources folder and select Add > Data Source
  • Specify the following information for the data source:
    • Name: gem_eventdrivenreporting
    • Label: gem_eventdrivenreporting
    • Select the Data Source Details tab and specify JDBC data source
    • Click on Import from iReport and select gem_eventdrivenreporting

Return to top

 

Creating the Sample

The sample report contains a simple list of customers filtered by the account opened date.

Step Action
1

 

Create a new empty report in iReport

 

2

 

Create a query selecting all fields from the customer table

  • Example:  Select * from customer)
3

 

Add the following fields: customer_id, lname, fname, city, state, country to the detail band and design the report.

 

4
  • Add a parameter named opened for the account opened date by right-clicking Parameters and indicating Add Parameter
  • Enter java.util.Date, for prompt, with the default value expression new Date(90,0,1)
5

 

Change the report query and add a where clause containing the opened parameter

  • The full query is in the in the query_1.txt file in the samples folder


WHERE
     customer.`date_accnt_opened` BETWEEN $P{opened} AND NOW() 


 

6

 

Add a title containing the parameters REPORT_COUNT and opened such as:

$V{REPORT_COUNT} + " new Customers since " + new SimpleDateFormat("dd/MM/yyyy").format($P{opened}) 


Tip: Ensure that you set the evaluation time for this field to Report.

 

7

 

Execute the report in iReport; it should look similar to the image below.

You can also view a sample of the report in the samples folder: theReport.jrxml.

 

Return to top

 

Deploying the Report to the Repository

Deploy the report to the Repository, configure, and execute it.

Step Action
1

 

Add the report as JRXML Document to the JasperServer repository

 

2

 

Add a Report Unit to the JasperServer repository using the deployed JRXML file as main JRXML as shown in the image below.

 

3

 

Add a local input control of type Date for the opened parameter

 

4

 

Log into JasperServer and run the report


 

Return to top

 

Creating the Wrapper Report

The wrapper report contains the main report as a subreport. Using report parameters and return values, we will define a condition which simulates firing an event.

Step Action
1

 

Create a new empty report in iReport

 

2

 

Add a new parameter opened which is filled with the last date of the day one month ago, type java.util.Date, not for prompting, default value 



new Date(new Date().getYear(), new Date().getMonth() -1, new Date().getDate())

 

3

 

Add a subreport to the Detail band using the wizard

  • Follow the steps, use the main report on your file system as a subreport resource
  • Pass through the parameter opened for the subreport’s parameter opened


 

4

 

Add the report query counting the rows from the original query, and checking if the returned amount of rows is larger than 60.

Note: This query is testing for the number of newly created customer accounts being greater than 60. This query serves as an example and any desired criteria can be defined.

Example: The full query is available in the samples folder: query_2.txt select count(*) as cnt from (<original query>) as tmp having cnt > 60

 

5

 

Add a new header using the parameter opened and the field cnt such as:



$F{cnt} + " new customers since " + new SimpleDateFormat("dd/MM/yyyy").format($P{opened})

 

6

 

Execute the report.

  • The report should return no pages as the resultset of the query is empty
7

 

Change the subreport expression to the repository URI of the main report’s JRXML such as:

/Event_Driven_Reporting/Common/edr_mainreport

 

8

 

Deploy the wrapper report to the repository as report unit


You can also view a sample report in the samples folder: wrapperreport.jrxml

 

Return to top

 

Scheduling the Wrapper Report

Schedule the wrapper report to run every minute and send the email in a PDF format.

Step Action
1

 

Login to JasperServer and open the repository

 

2

 

Select the wrapper report and set up a scheduler for this report:

  • Follow the wizard
  • Set a simple recurrence which runs every 1 minutes
  • Set up the output as PDF, which gets overwritten
  • Set up email notification
    • Select the option Skip empty reports  (see image below)
  • Save the settings

The report runs now every minute, if you check the snapshot folder .

 

 Return to top

 

Testing the Example

To test the example, you can create a fake event by updating the database.

Step Action
1

 

Update 57 rows of the sample database by executing the statement in the samples folder: update_01.txt.

Or, type the following statement into your MySQL frontend:



update customer set date_accnt_opened = STR_TO_DATE(CONCAT(YEAR(NOW()),'-',MONTH(NOW()),'-',DAY(date_accnt_opened)), '%Y-%m-%d') where YEAR(date_accnt_opened) = 1990


There will not be any notification in the report.

 

2

 

Reset the database to ensure that we get back into the initial state of the database, by executing this statement:



update customer set date_accnt_opened = STR_TO_DATE(CONCAT(1990,'-',MONTH(NOW()),'-',DAY(date_accnt_opened)), '%Y-%m-%d') where YEAR(date_accnt_opened) = YEAR(NOW())

 

3

 

Produce an email notification by changing 93 rows in the resultset; execute this statement, which is also available in the samples folder: update_02.txt



update customer set date_accnt_opened = STR_TO_DATE(CONCAT(YEAR(NOW()),'-',MONTH(NOW()),'-',DAY(date_accnt_opened)), '%Y-%m-%d') where YEAR(date_accnt_opened) = 1993



You should get an email notification within the next few minutes.
 

Return to top

 

What's Next?

You can now catch any kind of event using default features of JasperServer and JasperReports. Even more complex conditions for report execution can be easily integrated by using scriptlets or external jars.

Further resources:

  • JasperReport Ultimate Guide
  • JasperServer Administration Guide
  • JasperServer User Guide

 

Written By

This tutorial was written by Kerstin Klein, Professional Services Consultant, July 2009.