RSS

Search Engine

Sunday, August 22, 2010

Datasource and Dataset

To use Java Objects (POJO's) as datasource in Eclipse BIRT you have to map the fields of your Java classes to JavaScript. This JavaScript is then used to access the Java Object.

5.1. The Datasource

The datasource connects your data with your report. Birt provides different types of datasources, we use the "Scripted Data Source". Go back to your stocks_report, use the "Report Design" perspective and select the "Data Explorer" View.

Tip

You have to select your report to display the content of the datasource view.

Create a new datasource, named "srcStocks" in your report.

5.2. The Dataset

The dataset defines the mapping for the datasource data and the birt data.

Create a new dataset named "dataSetSocks".

Press next and define the columns for your report.

5.3. JavaScript

Now we have to write the JavaScript for our dataset. Select the dataset and choose "open" as script. The open script is called before the first access to the dataset. We use this to load our List with the stock objects. To access a Java class you only have to use the following syntax: Packages.myJavaClass where myJavaClass is the full qualified Java class name.

Tip

In case you don't see the script please node that the editor for the report has several tab. One of it is labeled "source".

    
count = 0;

// Create instance of
// the GetStockHistory class
gsh = new Packages.sawan.modi.birt.stocks.daomock.StockDaoMock();

//Load the List

stock = gsh.getStockValues("Java");

Place the following coding in the fetch script.

    
if(count < stock.size()){
row["columnDate"] = stock.get(count).getDate();
row["columnOpen"] = stock.get(count).getOpen();
row["columnHigh"] = stock.get(count).getHigh();
row["columnLow"] = stock.get(count).getLow();
row["columnClose"] = stock.get(count).getClose();
row["columnVolume"] = stock.get(count).getVolume();
count++;
return true;
}

return false;

Check if your Script works by doubleclicking on the dataset -> Preview Result.

0 comments:

Post a Comment