03 October 2013

Using POJO DataSource in BIRT 4.3



To create a report in BIRT 4.3 we can use POJO dataSource. In 4.3 this DataSource is supported. To use this we need to create a dataset class. We can check this with one example. The following example is done keeping web applications in mind. with little modifications it can be used in the standard-alone applications as well.


To retrieve a student with a specific id we need to have two POJO classes. One is Student.java and the other one is StudentDataSet.java.


Student.java:-


package com.ymd;


public class Student {

private String studentId;
private String name;
private String mobile;
private String email;
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}
}


StudentDataSet.java:-


package com.ymd;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


import javax.servlet.http.HttpServletRequest;


import org.eclipse.birt.report.engine.api.EngineConstants;



import com.ymd.Student;


public class StudentDataSet {
private Iterator<Student> iterator;
public List<Student> getStudents(String studentId){
List<Student> studs=new ArrayList<Student>();
try{
Student stud=getStudent(studentId);
studs.add(stud);
}catch(Exception e){
e.printStackTrace();
}
return studs;
}
private Student getStudent(String studentId){
Student std=new Student();
// your logic to get the details of the student
//goes here. Fetch the student details and populate
// the Student.
return std;
}


//The following method will be called by BIRT engine once when
// the report is invoked. It is also a mandatory method.
@SuppressWarnings(value = { "unchecked" })
public void open(Object appContext, Map<String,Object> map) {
Map<String,Object> mur=(Map<String,Object>) appContext;
HttpServletRequest request=(HttpServletRequest)mur.get(EngineConstants.APPCONTEXT_BIRT_VIEWER_HTTPSERVET_REQUEST);
String studentId=request.getParameter("studentId");
iterator = getStudents(studentId).iterator();
}
//this method is a mandatory method. It must be implemented. This method
// is used by the BIRT Reporting engine.
public Object next() {
   if (iterator.hasNext())
       return iterator.next();
   return null;
}
//The following method is also a mandatory method. This will be
//called by the BIRT engine once at the end of the report.
public void close() { }
}


In the dataset class the three methods, “public void open(Object obj, Map<String,Object> map)”, “public Object next()” and “public void close()” must be implemented. They will be used by the BIRT Reporting Engine using reflection to generate the report. So in the dataset class we can get the request parameters in the “open” method. so we can pass any number of parameters in the URL and can get the parameters here and use to fetch the data required for the reports. 

The final step is to pack the two classes into a jar file. The name of the jar file can be anything. This jar file will be used by the report designer just to design the report. At run time the classes will be taken from the classpath. I mean in web applications they will be taken from “WEB-INF/classes” folder.


The remaining part of tutorial on how to use POJO Datasource can be read from the following link.

Post a Comment