BIRT and POJOs as datasources example

In this sample i'll try to demostrate how to use POJOs as DataSource.

Java Classes

Contact.java

package gr.osmosis.report.data;
public class Contact {
    String fname;
    String lname;
    String phone;
    
    public Contact(String fname, String lname, String phone){
        this.fname = fname;
        this.lname = lname;
        this.phone = phone;
    }
    
    /**
     * @return Returns the fname.
     */
    public String getFname() {
        return fname;
    }
    /**
     * @param fname The fname to set.
     */
    public void setFname(String fname) {
        this.fname = fname;
    }
    /**
     * @return Returns the lname.
     */
    public String getLname() {
        return lname;
    }
    /**
     * @param lname The lname to set.
     */
    public void setLname(String lname) {
        this.lname = lname;
    }
    /**
     * @return Returns the phone.
     */
    public String getPhone() {
        return phone;
    }
    /**
     * @param phone The phone to set.
     */
    public void setPhone(String phone) {
        this.phone = phone;
    }
}

ContactListFactory.java

package gr.osmosis.report.data;
public class ContactListFactory {
    
    public Contact[] createContactList(){
        Contact[] c = new Contact[4];
        
        c[0] = new Contact("stavros", "kounis", "2310886269");
        c[1] = new Contact("dimitris", "kounis", "2310888270");
        c[2] = new Contact("dimitris", "adamos", "2310998417");
        c[3] = new Contact("nikos", "koufotolis", "2321013770");
        
        return c;
    }
}

Report

Create a new BIRT report file (.rptdesign) and name it (for example) contactscript.rptdesign.

Datasources

Change to "Report Design" perspective. Find the "DataExplorer" View and create a new "Data Source -> Script Data Source" (named srcScript).

Now right click on "Data Sets" node and create a new dataset:


Data Set Name: setScript
DataSource: srcScript (we have allready create it above)

 

 

Set up columns
Right click in your just created data set (setScript) and select "edit"
In the "Edit Data Set" Window select the node "Output Columns".
Add here 3 entries:

Name / Type
--------------
columnFirstName / Any
columnLastName / Any
columnPhoneNumber / Any


Script code
Select now your Data Set (setScript) and set your report editor to have "Code" tab selected

1. put the below code in open method of your DataSet

count = 0;
cf = new Packages.gr.osmosis.report.data.ContactListFactory();
c = cf.createContactList();

now we have an array of Contacts stored in "c" variable.

2. put the below code in fetch method of your DataSet

if (count < c.length-1){
	count ++;
	row["columnFirstName"] = c[count].getFname();
	row["columnLastName"] = c[count].getLname();
	row["columnPhoneNumber"] = c[count].getPhone();
	return true;
}

return false;

 

Report Design

In your report editor change to "Layout" Tab

In Data Explorer View your data set must have 3 children nodes (one for each column you have created). Drag 'n drop each one from Data Explorer View to report's layout view.

Preview

Here is a litle problem when we are working in design mode. Report's script must be able to find our java classes (Contact.java, ContactListFactory.java). Report designer use BIRT's web-app viewer to render (preview) the report. But this WEB-APP does not know where to look for our java classes. This web-app exist as an eclipse plug-in.

for example:


C:\JProgramFiles\eclipse-SDK-3.0.2-win32\eclipse\plugins\org.eclipse.birt.report.viewer_1.0.0

 

We have to put our classes in a "classes" folder in this web-app. To do this create a "classes" folder at birt\web-inf.

Build Contact.java and ContactListFactory.java and put the produced .class files in this "classes" folder.
Dont forget to create inside "classes" folder the directory structure that match the package name.

for example:


if you just copy paste the code from this post then you must have a path like:
C:\JProgramFiles\eclipse-SDK-3.0.2-win32\eclipse\plugins\org.eclipse.birt.report.viewer_1.0.0\birt\WEB-INF\classes\gr\osmosis\report\data

 

Now you can preview your report selecting "Preview" tab.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值