Using JavaBeans set Datasource in Jaspersoft iReport

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.

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

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.

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

Creating Jasper Reports In (iReport) Using XML As Data Source & Publishing It to Jasper Server

This blog will teach how to create Jasper Reports using iReport, the data source to be used is XML and then publish it on Jasper servcer

  1. Run iReport; it will open showing a Welcome Window, as shown in the following screenshot:

Jasperreport XML data source

2.  DataBase (DataSource ) Connection :

a.)  Click On Datasource button

Jasperreport XML data source 2

b) Click On new button

Jasperreport XML data source 3

C) Select XML datasource

Jasperreport XML data source 4

d) A new window named XML file datasource will open, as in the following

Jasperreport XML data source 5

e) Enter AddressBook (you can give the name of your choice also) as the name for your new connection for the XML datasource in the text box beside the Name text field  & Click the Browse button to browse to the addressbook.xml file located in the E:/ in my system. Select the Use the report XPath expression when filling the report option in the XML file datasource window,

Jasperreport XML data source 5

f) Now click on “TEST” for testing the connection

3. Creating The Report  :  Open a new report and then go to “query browser”,choose the query language from drop down list as “xpath.” The text area below the Query language drop-down list will split into two parts. The left part  shows the actual XPath expression, whereas the   right part shows XML nodes

Jasperreport XML data source 7

4. In the XML nodes right click on the name child of the person node,select “add node as field”,similarly for phone child of person node also.

Jasperreport XML data source 8

5. Double-click on the Fields node in the Report Inspector window on the left of your report. The Fields node will expand to show the fields name, phone.

Jasperreport XML data source Query

Drag-and-drop the name and phone field from the Fields node into the Detail section of your report.

6. Switch to the Preview tab and you will see your report The result should be similar to the below

Jasperreport XML data source 10

7. Publishing the report to the jasper server

a) Creating data adapter :

a.)    Creating Data Adapter :  To use flat files as a datasource (like  XML  files) you need to use DataAdapters in Jasperserver instead of regular datasources. We can create them manually in the server (it’s just an XML file). Data Adapter is a way to define a source for Jasper Reports using a   configuration file,(act as the bridge between data source and jasper reports while publishing the jasper report on Jasper Server).

xmlAdapter.xml

Jasperreport XML data source 11

<filename>repo:/reports/interactive/xmlData</filename>  – > here xmlData is the name of the xml    file(addressbook.xml) which we have to add again to the Jasper Server.(I have added this file in this location “repo:/reports/interactive/”.

b) We need to add XML datasource to server also

c) Adding xmlAdapter To The Properties attribute of Report.

Property Name : net.sf.jasperreports.data.adapter

Property Value : repo:/reports/interactive/xmlAdapter

 

Note: By default xpath or xpath2 language will not come in jasper server,for this you need to do three things…

  1. Copy jasperreports-extensions-3.5.3.jar from /ireport/modules/ext to JasperReports Server/jasperserver-pro/WEB-INF/lib/jasperreports-extensions-3.5.3.jar
  2. Add the following line to /jasperserver-pro/WEB-INF/classes/jasperreports.properties

net.sf.jasperreports.query.executer.factory.xpath2=com.jaspersoft.jrx.query.JRXPathQueryExecuterFactory

  1. Restart Tomcat