Setting default parameter values

One of the most commonly asked questions over the past few years in the newsgroups have been "How can I set a date parameter to default to the current date?"

Before BIRT 2.5, this was not an easy task to accomplish. However, new to BIRT 2.5 is the ability to set a date parameter's default value via script. Let's go about it:

  1. Create a new report called defaultParameterValueReport.rptdesign.
  2. Create the Classic Cars Sample Database Data Source.
  3. Create a dataset called dsOrderRange, with the help of following query:
    select
    *
    from
    CLASSICMODELS.ORDERS
    where
    CLASSICMODELS.ORDERS.ORDERDATE between ? and ?
    
  4. Link the two dataset parameters to report parameters called prmStartDate and prmEndDate. Make sure the two dateset parameters and two report parameters are of type Date.
    Setting default parameter values
  5. Open the editor for prmStartDate. In the drop-down box next to Default value, select Javascript Syntax and continue.
    Setting default parameter values
  6. Use the following script in the expression builder. Don't worry about what this means, just understand that it will set the start date to 20 days before the current date.
    //bring in the java.util package into the scripting scope
    importPackage( java.util );
    //create a new GregorianCalendar object
    var cal = new java.util.GregorianCalendar();
    //set the date to now
    cal.setTime(new Date());
    //substract 20 days
    cal.add(Calendar.DAY_OF_MONTH, -20);
    //return the start date
    cal.getTime();
    
  7. Do the same for the prmEndDate and set the expression to the following:
    new Date();
    
  8. Insert the dataset into the report.
  9. Create a grid of three columns and a row above the dataset table. In the first column, create a label and set the text to Orders Between:.
  10. From the Data Explorer or the Outline view, drag prmStartDate to Column 2.
  11. From the Data Explorer or the Outline view, drag prmEndDate to Column 3.
    Setting default parameter values
  12. Run the report.
Setting default parameter values

Initially, we won't see any date in the report. This is because the data in the Classic Cars Database is from 2005, so there is no date in range. But we can see from the Orders Between header that the date range has start date set to 20 days before the current date whereas the end date is the current date. We can change these at run time.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset