Home > JASPER REPORTS, Uncategorized > IReport / Jasper Reports – Working with subreports and collections in Jasper Reports

IReport / Jasper Reports – Working with subreports and collections in Jasper Reports


Hi, and sorry for not updating for a while. I am currently under heavy load, and can scarcely find time to write, although i have several new topics prepared. I am also working on a presentation on Java Performance Testing and Monitoring which i will probably hold here in Hamburg, on the 18th of July. More on that for those interested in a follow-up post.

Now let’s dive into the subject: Working with subreports and collections in Jasper Reports

It seems that several users have been facing this problem, so i thought i wrote an explanatory post on this topic.

1. Building the main report

Let’s start with the main report. This looks like this:

Image

The sub-report is the grey box with a yellowish highlighting. The main report is passing the four following parameters to the sub-report, of which one is a collection:

  • http_request
  • filterstop
  • ic_testconfig
  • filterstart

Please notice how the name matches EXACTLY the expression (the passed parameter has to be named exactly the same as the local variable used in the subreport)

Also notice the properties of the sub-report, highlighted in the screenshot below:

  • Subreport Expression has to be: “repo:statistics”, where statistics is the name of the IReport file containing the designed subreport
  • Expression Class: java.lang.String
  • Connection type: Use a connection expression

Image

2. Creating the sub-report

Let’s create a sub-report in the repository, with the name we just configured in the main report: statistics

My recommendation is to create a single folder in your repository, called “subreports”, and add all sub-reports there. In my case, i have three sub-reports (we will only focus on the statistics subreport):

  • statistics
  • detailed_statistics
  • hudson_statistics

The structure in the repository looks like this:

Image

Let’s take a look at my sub-report. This will receive the collection as input parameters. The collection is actually a series of test id’s that i use to build a report over several test runs (for example, if i run a test twice, once with id 123, and once with id 124, and i want to see a single report of all transactions for both test-runs, i will give both parameters as input:

Image

The query is the one that takes the collection input parameter and processes it. Let’s see how that looks like:

select
count(t) as totaltransactions,
avg(t) as responseaverage,
……
from
testresults tr
where $X{IN,tr.testrun_id,ic_testconfig} and DATE_FORMAT(DATE_ADD(‘1970-01-01 00:00:00′ ,INTERVAL ts*1000 MICROSECOND),’%H:%i:%s’) between $P{filterstart} and $P{filterstop}
group by tr.lb

We will now add this sub-report into the Jasper Server Repository, by adding a new resource from the JRXML File we just created for our subreport. We will have to assign two identifiers:

  • Label: statistics
  • Name: rootavg

 

Jasper Server Repository - Adding a JRXML Resource

Jasper Server Repository – Adding a JRXML Resource

 

Now we add the two identifiers mentioned above

Jasper Server Repository - Labeling the JRXML Resource

Jasper Server Repository – Labeling the JRXML Resource

We can now refresh the repository in the IReport local instance, and see the sub-report added in the location we chose, with the identifiers we just assigned (name is statistics, id is rootavg):

Subreport in IReport Repository

Subreport in IReport Repository

3. Adding the sub-report as a resource to the main report

We have now added both the main report and the sub-report. Well, it is not enough to define the sub-report in the main report. The main report has to know where the called sub-report resides, therefore we need to add it as a resource of the main report. Remember that these resources have to be defined and available in the JasperServer Repository (Server Side).

We start by editing the main report on the server side, and adding the resources:

Add subreport as resource in jasper server repository

Add subreport as resource in jasper server repository

We still have to add the parameters on the server side. Remember we want to use a collection. In order to do that, we use a “Multi Select Query Type” Input Control:

 

Jasper Server Add Input control

Jasper Server Add Input control

We configure it as a “Multi Select Query Type” with the same name we are going to use in our report, that being “ic_testconfig”:

Jasper Server Collection Input Control

Jasper Server Collection Input Control

After refreshing the Repository in IReport, our Report looks like this. Notice the input controls, and how the collection item is now available

Configured repository with subreports

Configured repository with subreports

4. Final

Let’s review the steps once again:

  1. Create the main report, and decide on the parameters you want to pass to the sub-report. Upload the main report to the Jasper Server, and add the parameters on the server side too. The resources always have to bee synchronized
  2. Create the sub-report, and the query that will receive the collection. Upload the sub-report to Jasper Server
  3. Add the sub-report as a resource of the main report in Jasper Server
  4. Watch out the query syntax when using collections:
    1. where $X{IN,tr.testrun_id,ic_testconfig}

I think that’s it. Tried to put it as explanatory as possible, in the short amount of time that i have at disposal these days. I am really sorry for the delay in replying to comments, and posting new content. Hope to get things off my head in the near future

Cheers,

Alex

 

About these ads
  1. December 22, 2012 at 1:20 am

    Do you have even more articles like this specific 1 termed, IReport / Jasper Reports – Working with subreports
    and collections in Jasper Reports | Qants – Tips in the field of Java Performance
    & Software Testing? I wish to read even much
    more about it. Thanks.

  2. emma
    December 16, 2013 at 8:27 pm

    I would like to have a frank answer about Jasper reporting tool, how much is it accessible for user without Java programing knowledge ?
    Notice I’m BI reporting analyst.

  3. December 17, 2013 at 7:57 am

    Hi Emma,
    Pentaho is a great tool for BI people, programming skills may be required for more complex scenarios, but i am sure that by googling and researching you could easily do a cast operation by yourself.
    You need to know things like data types, sql queries, the rest is pretty much done by classes implemented in the form of let’s call them “patterns”…for importing data, for exporting it, for manipulating it, for sending email and so on.
    So my frank answer is : HIGHLY accessible. You just need a little bit of time for try and err, otherwise you will do just great with this.
    Cheers
    Alex

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 29 other followers

%d bloggers like this: