Using JavaBeans set Datasource in Jaspersoft iReport

Posted on by By Shraddha Tambe, in Business Intelligence, Jaspersoft, Open Source Business Intelligence | 0

JasperReports provides several types of pre-defined/ready-to-use datasources which can be used to provide the data that is filled in the report. We are going to look at one such datasource, the ‘JavaBeans Set Datasource’, which allows us to use JavaBeans as data to fill reports.

Make data easy with Helical Insight.
Helical Insight is the world’s best open source business intelligence tool.

The basic idea is to provide 2 important things –

1. A JavaBean Class which defines the different fields of the datasource
2. A bean factory class which essentially returns a set of objects of the JavaBean class in form of an Array or Collection. Each object inside the array or the collection will be seen as one record in this type of data source.

We will now discuss each of these classes and how are they used in the datasource with an example.

1. Creating the Java Bean

The JavaBean Class is a Java class which exposes its attributes using “getter” methods. We define a “getter” method for every attribute which looks like public getXXX() , where XXX is the attribute in the class.
The example below, shows a WeatherBean Class which holds weather information for various cities. You can see a “getter” (accessor) method defined for each of the attributes as per the specified format.

/**
* Bean Class which defines that data structure and provides the getter methods 
*/
public class WeatherBean {
	private String cityName = ""; 
	private String weatherType = "";
	private int temperature = 0;

	public WeatherBean(String cityName, String weatherType, int temperature) {
	this.cityName = cityName;
	this.weatherType = weatherType;
	this.temperature = temperature;
	}

	public String getcityName() {
	return cityName;
	}

	public String getweatherType() {
	return weatherType;
	}

	public int gettemperature() {
	return temperature;
	}
}

2. Creating the bean factory class

The bean factory class should have a static method which will return a collection or an array of objects of the bean class. In the below example the method is getWeatherBeans().

import java.util.Collection;
import java.util.Vector;

/**
* Bean Factory Class which returns a collection of beans
*/
public class WeatherBeanFactory {

public static Collection getWeatherBeans(){
Vector allbeans = new Vector();
try {
allbeans.add(new WeatherBean("Delhi","Sunny",41));
allbeans.add(new WeatherBean("Mumbai","Cloudy",36));
allbeans.add(new WeatherBean("Bangalore","Rainy",28));
allbeans.add(new WeatherBean("Hyderabad","Sunny",39));
}
catch(Exception e){
e.printStackTrace();
}
return allbeans;
}
}

Export both the classes into a JAR file.

3. Add the JAR file to the iReport Classpath

jar-add-ireport

4. Create the JavaBean Set Datasource

Make data easy with Helical Insight.
Helical Insight is the world’s best open source business intelligence tool.

Select the datasource type as “JavaBeans set datasource”

jar-add

Set the Factory class name and Static method call to retrieve the collection:
In our example, it is “WeatherBeanFactory” and “getWeatherBeans”

ds-1

5. Set the Report Query
Go to the “JavaBean” datasource tab. In class name, give the name of the bean class. In our example, its “WeatherBean”. Click “Read attributes” and select the fields as per your requirement.

rq-1

The mapping between a particular JavaBean property/attribute and the corresponding report field is made by naming conventions. The name of the report field must be the same as the name of the JavaBean property as specified by the JavaBeans specifications. For instance, to retrieve the value of a report field named cityName, the program will try to call through reflection a method called getcityName() on the current JavaBean object. This is how, it “getter” methods are used.
Below report is designed using the fields from the “WeatherBean” class.

rq-2

The output is shown below.

Make data easy with Helical Insight.
Helical Insight is the world’s best open source business intelligence tool.

rq-3

Though we have used a simple JavaBean class, with attributes as basic types, Nested JavaBean properties can be also accessed in a JavaBean data source.

– Shraddha
Helical IT Solutions

logo

Best Open Source Business Intelligence Software Helical Insight is Here

logo

A Business Intelligence Framework

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments