Showing posts with label JasperReports. Show all posts
Showing posts with label JasperReports. Show all posts

Tuesday, October 30, 2012

Jasper Report: Gracefully Hide Columns On Request

For some reports you might want to offer the end users some more flexibility in terms of which columns they actually want to see in the report.
Documentation about this feature is scarce. When I was looking at a solution for this request some time ago I coincidentally stumble across Nestor's blog post (Some years ago, when I was living in Miami, Nestor and I used to work for the same company), which I found very useful.

Our requirement is that when a column is hidden it must not consume any space, meaning all columns to the right of it move gracefully to the left. This can be achieved using the table component. 
Our initial report looks something like this:


Define a parameter (i.e. SHOW_CONTINENT) and set its type to Boolean. 
Set the default value to:


new Boolean(true) 

and tick Use as a prompt.

Note: This parameter does not have to be passed to the subdata set!

Screenshot: iReport - Parameter Properties
Screenshot: JasperStudio - Parameter Properties
In the table mark the header column element for the column which you want to hide on request. It is best to select this from the Document Inspector - make sure it is the column and not the element within the cell: 
Note: It does not really matter on which level (table header, column header, detail etc) you select the column.

In the Properties set the Column Print When condition to:

new Boolean($P{PARAMETERNAME})

Screenshot: iReport - Column Properties
Screenshot: JasperStudio - Column Properties
Let's now preview the report. When asked if we want the continent column displayed, we say yes:
Now let's preview the report with the continent column disabled:
As you can see from the screenshot the continent column is not shown and the country, city and sales columns all nicely moved to the left.

Thursday, October 25, 2012

Jasper Report Bursting with Pentaho Data Integration (Kettle) using a REST service

Jasper Report Bursting with Pentaho Data Integration (Kettle) using a REST service

JasperReports Server 4.7 comes with some very useful REST services, one of them being a report creation service. The nice thing about this is that it is extremely easy to call this service.

Imagine our company has several regions and each regional managing director should receive a customized report. As the layout is the same for every region, we can just create one report that we parameterize so that we can display the data that is relevant for each region.

We have the contact details stored in a central database. As other projects already make use of Pentaho Kettle for data integration purposes, we decide to use it as well for our project. The outline of our report bursting process is as shown below:

So we first get all the contact details and then execute the report generation process for each one of these contacts.

As we want the whole process to be easily configurable we add these variables:

The transformation for retrieving the contact details is quite straightforward: Get the data from the database table, check if the email address is valid and copy rows to results:
Let's have a look now at the report generation process, where things get more interesting: 
So we set all the variables (the values are the contact details), we set the variable for the temp directory, then we create the report folder inside the temp directory if it doesn't exist already, after this we delete any files that could be in this report directory already. 

Now comes the important part: We use the HTTP job entry to call the JasperReports Server REST service.

In the General settings of this job entry we provide following URL details:

${VAR_JRS_URL}/rest_v2/reports/${VAR_JRS_DIR}/${VAR_REPORT_NAME}.${VAR_REPORT_OUTPUT_FORMAT_EXTENSION}?j_username=${VAR_JRS_USERNAME}&j_password=${VAR_JRS_PASSWORD}&CUSTOMER_ID=${VAR_CUSTOMER_ID}

As you can see this URL gets dynamically created by using some of the variables we set in the main job and also one report parameter value we retrieved from the contact details (VAR_CUSTOMER_ID). There are various output formats available for the report, in this example we use pdf, but it could also be xls, html etc.

The webserver reply should be stored in the target file (in example):
${VAR_TEMP_DIR}${VAR_REPORT_DIR}/${VAR_REPORT_NAME}-${VAR_CUSTOMER_ID}.${VAR_REPORT_OUTPUT_FORMAT_EXTENSION}


As there might be cases where for a certain parameter value there is no report data and hence no report created, we want to next check if there was actually a report created. If there is no report available, we just end this job gracefully, otherwise we execute the transformation which sends out the email:


In this transformation we get following variable values and assign them to fields:

The path to the report is dynamically created by using this Java expression:
TEMP_DIR + REPORT_DIR + "/" + REPORT_NAME +"-"+ CUSTOMER_ID + "." + REPORT_OUTPUT_FORMAT_EXTENSION

Finally the email settings are defined and the email is sent out with the report as attachment.

Most work in this process is dedicated to creating the report bursting functionality, accessing the JasperReports REST service is actually the easy part. The big advantage of this approach is also that you do not need a dedicated plugin to execute the Jasper report on the server and get the result back.