Eclipse's BIRT: Scripted Data Set
This article presents the usage of sripted data set in the eclipse's BIRT.
Join the DZone community and get the full member experience.
Join For FreeThis article presents the usage of sripted data set in the eclipse's BIRT. This approach I am using since the BIRT verison 2.5.1 and maybe it work even with the older version too. With the newest version of BIRT this approach works too as well.
If you want to use Java objects as data source and data set in eclipses BIRT you need to do that by using sripted data source and scripted data set. I will try to explain that here with an example by using scripting[ref] in the BIRT report designer.
What you need for the test is a Java project with one Java class with object reference to a list of objects which are defined into another Java class. The getter method of the class is used then in the rptdesign file. It is recommended all thouse classes to implement the serialization interface. The first class which contains the list reference can look like:
package name.stojanok.dzone.scripteddataset;
import java.io.Serializable;
import java.util.ArrayList;
public class PersonDataSet implements Serializable {
private static final long serialVersionUID = 1L;
private ArrayList<Person> person = new ArrayList<Person>();
public void setList(ArrayList<Person> dataStructures) {
person = dataStructures;
}
public ArrayList<Person> getList() {
person.add(createPerson("John", "Doe", 33, true));
person.add(createPerson("Johnny", "Doe", 13, true));
person.add(createPerson("Lisa", "Doe", 33, false));
person.add(createPerson("Jenny", "Doe", 13, false));
return person;
}
private Person createPerson(String firstName, String lastName, Integer age, boolean gender) {
Person person = new Person();
person.setFirstName(firstName);
person.setLastName(lastName);
OtherPersonData otherPersonData = new OtherPersonData();
otherPersonData.setAge(age);
otherPersonData.setGender(gender);
person.setOtherPersonData(otherPersonData);
return person;
}
}
The class where the references are, can be like the following classes:
package name.stojanok.dzone.scripteddataset;
public class Person {
private String firstName;
private String lastName;
private OtherPersonData otherPersonData;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public OtherPersonData getOtherPersonData() {
return otherPersonData;
}
public void setOtherPersonData(OtherPersonData otherPersonData) {
this.otherPersonData = otherPersonData;
}
}
package name.stojanok.dzone.scripteddataset;
public class OtherPersonData {
private Integer age;
private Boolean gender;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public boolean isGender() {
return gender;
}
public void setGender(boolean gender) {
this.gender = gender;
}
}
For these objects to be recogized on runtime in the report designer you need to set the class path in the report designer pereferences in eclispe to the class path of the created Java classes. To do that you can add the classes via the Preferences window in eclipse. Go to: Preferences -> Report Design -> Classpath and then choose the Java project folder (default the bin folder) where the class files are created.
Scripted Data Source
Fisrt of all creating pseudo data source is needed for this kind of approach. In the report design file we define an scripted data source with the following script content in the open and the close method of it.
Open - method
dummyObject = new Object();
Close - method
dummyObject = null;
Scripted Data Set
After defining the datasource we need to create the scripted dataset. The structure of the dataset should be almost equal like the pojo structure because the data will be „copied“ 1:1 from the Java object into the BIRT dataset. For this case the scriptd data set will have the following structure:
- FirstName - String
- LastName - String
- Age - Integer
- Gender -Boolean
- OtherPersonData - Java Object
Following content should be added ih the open, fetch and close method of the scripted data set.
Open - method
Here we need to declare the package where the pojo and the list class comes from with the JacaScript like aka Rhino syntax. After that we are creating a reference in Rhino to the Java object.
importPackage(Packages.name.stojanok.dzone.scripteddataset);
var myDataSet = new PersonDataSet();
var myData = myDataSet.getList();
myIterator = myData.iterator();
Fetch - method
In this methid the iteration through the list is done where the data are coped from the Java object into the data set.
if(myIterator.hasNext()){
var node = myIterator.next();
row["FirstName"] = node.getFirstName();
row["LastName"] = node.getLastName();
row["Age"] = node.getOtherPersonData().getAge();
row["Gender"] = node.getOtherPersonData().isGender();
row["OtherPersonData"] = node.getOtherPersonData();
return true;
} else return false;
Close - method
Here the opened references are set to null.
myDataSet = null;
myArrayList = null;
myIterator = null;
node = null;
This way the data are pass to the BIRT dataset and are ready for bounding to BIRT report items like Table and List.
Using the data set in a report text item as part of a table or list the content could look like:
<VALUE-OF>row["FirstName"]</VALUE-OF>
<VALUE-OF>row["LastName"]</VALUE-OF>
<VALUE-OF>row["Age"]</VALUE-OF>
<VALUE-OF>if (row["Gender"]) {"male"} else {"female"}</VALUE-OF>
<VALUE-OF>row["OtherPersonData"]</VALUE-OF><BR>
<VALUE-OF>row["OtherPersonData"].getAge()</VALUE-OF> <VALUE-OF>row["OtherPersonData"].isGender()</VALUE-OF>
Problems while creating this article
While I was writing this artice and creating an scripted data set example for it I came to some problems. The BIRT renderer have not found the Java class files which where set on the classpath. After some experimenting with older BIRT versions I saw that the Java classes which are compiles with Java 1.7 can not be recognized from BIRT. Thats why it is recommended the classes to be compiled with Java 1.6.
External Links
Searching in the web with the words "BIRT Java object" other articles and tutorials are found where scripte data set is used. Some of them are:
- Lars Vogel's page about BIRT and Java Objects[ref] - This is an article where BIRT chart is bound to a scripted data source.
- eclispe BIRT Tutorial as video. - explamantion[ref] - One of the links on this page leads to a video where scripted data source is described. In this tutorial the Java class files are added to one of the plugin folders.
- Yaragalla's Blog: Using POJO DataSource in BIRT 4.3[ref][ref]- Scripted sata source in BIRT 4.3.
- BIRT Java Object Data Type[ref] - Tutorial from the BIRT World Blog.
Résumé
In the newest version of BIRT you can bind Java object via the GUI in the BIRT report designer. I still have not tested that way of binding Java Objects. What I have used since BIRT 2.5.1 is the approach which is described here in this article. I hope the newest version of binding is moch more elegant, means that it doesn't need much extra scripting.
GitHub
The sources for this example can be checked out from: https://github.com/kstojanovski/birt. Use the scripted folder and create PDF report scripteddataset.rptdesign from the report folder. Before doing that set the classpath like described in this article.
Opinions expressed by DZone contributors are their own.
Comments