accessing parameters when writing a scripted BIRT data source in java

11,359

It is best to work with POJO-based scripted data sources via the JavaScript layer in the Report Design. This gives you access to report-context elements like parameters and leaves your Java objects to remain agnostic data providers.

Here is how I generally set things up:

1) Java Layer:

1.1) Data Model Class: A class of data elements with setters & getters. No work gets done by this class

1.2) Controller Class: Builds and maintains an array of instances of the Data model class. This is the class you will access from the Report layer. This class should implement an "action" method (like getRows(...)) that will accept your parameter and return an ArrayList of the "rows".

2) Report Layer:

2.1) Create a new Scripted Data Source.

2.1.1) Override the "open" script on the Data Source to instantiate your controller object. The code will look like this (This is how you get your parameter value into the Java layer):

// This will track your current row later on
count = 0;

// Create instance of the Controller class
controller = new Packages.com.your.package.path.DataSetController(); 

//Load the rows (Note here is where you are able to pass your parameter into the Java layer)
rows = controller.getRows(params["myParameter"]);

2.2) Create a new Data Set using the Scripted Data Source.

2.2.1) Override the fetch script to process the ArrayList built by your controller class. The code will look like this:

// Iterating over the ArrayList built by the Controller Class bound to the data source.
if(count < rows.size()){
       // Set the column values on the data set off the values store in the Data model class.
       row["product"] = rows.get(count).getProduct();
       row["date"] = rows.get(count).getDate();
       row["units"] = rows.get(count).getUnits();
       count++;
       return true;
}

return false;

That should do the trick. Good Luck!

Share:
11,359
triggerNZ
Author by

triggerNZ

Software developer. Consultant. Polyglot. Functional fundamentalist. Mad scientist. Interested in elegant code, functional programming, scalability, machine learning, tech business and everything else.

Updated on June 13, 2022

Comments

  • triggerNZ
    triggerNZ almost 2 years

    I am using BIRT reporting with a scripted POJO data source and am having trouble accessing report parameters. I am scripting everything in Java, rather than javascript. The code is below:

    public class StockDataSetHandler extends ScriptedDataSetEventAdapter {
     ...
    
     @Override
     public void open(IDataSetInstance dataSet) {
      count = 0;
      StockDaoMock mockStockDao = new StockDaoMock();
      //The code below works in javascript. How can I do the equivalent
      //in Java? Where do I get params from?
      String paramValue = params["myparameter"];
      stockData = mockStockDao.getStockValues(paramValue);
     }
    }
    

    BIRT's documenation refers to the params collection. How do I get hold of that in Java?

    Cheers Tin