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

DataExlorer.gif

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

EditDataSet.gif
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.

open.gif

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;

 

fetch.gif

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.

report.gif

 

你可能感兴趣的:(java,eclipse,C++,c,C#)