Monday, May 18, 2009

BIRT dynamic parameters

BIRT is simply amazing. There is no denying that. The best thing about it is that it is open source and free to use. This makes it simply an amazing solution to work with. It is really great as long as they keep the normal report rendering api free and open source :)

This post is about a particular scenario in reporting. It relates to time reporting. Most commonly we need trend analysis of the past week to better analyze the data. But you dont want to put in the date / time of the last week manaually. Using oracle BI Publisher I am used to taking care of these using sysdate inside of SQL Queries. But BIRT provides a better way.

Heres a step by step howto.

Create two report parameters. One for start and one for the stop time:

Remeber to uncheck the Is Required property:

This is because we are going to generate the value dynamically in case the parameters are not set :)

Needless to say you need to link these to your query (data set) parameters:


Now click the main surface of your report in the layout tab:


Then click the script tab and add the following code to the initialze event handler as shown:


function getdateBehindByDays(days)
{

var cal = java.util.Calendar.getInstance();
cal.add(java.util.Calendar.DAY_OF_MONTH, -days);
return (cal.get(java.util.Calendar.MONTH) + 1 ) + "/" + cal.get(java.util.Calendar.DAY_OF_MONTH) + "/" + cal.get(java.util.Calendar.YEAR);

}

if (params["StartTime"].value == null)
{
params["StartTime"].value = getdateBehindByDays(7);
}

if (params["StopTime"].value==null)
{
params["StopTime"].value = getdateBehindByDays(0);
}


What this code is doing is that it is sending the start date back by one week and the stop time to today :). Just change the values '7' and '0' to what you require :)

Now just run the report and set the parameters you want to be made dynamic to null:

Viola! welcome to the wonderful world of automated reporting :)

No comments:

Post a Comment