reporting with eclipse birt and java objects (pojo's) - tutorial.pdf

19
27/04/13 Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial www.vogella.com/articles/EclipseBIRT/article.html 1/19 47 Free tutorial, donate to support by Lars Vogel Reporting with Eclipse BIRT and Java Objects (POJO's) Tutorial Lars Vogel Hendrik Still Version 1.2 Copyright © 2008 2011 Lars Vogel 26.06.2011 Revision History Revision 0.1 0.2 07.03.2008 Hendrik Still, Lars Vogel Created Article Revision 0.3 1.2 07.03.2009 26.06.2011 Lars Vogel bug fixes and enhancements Eclipse Birt This tutorial describes how to use Eclipse BIRT for reporting on simple Java Objects (POJO's). The tutorial explains also how to deploy the resulting BIRT report into a webcontainer (Tomcat) and how to use it in an Eclipse RCP application. Eclipse 3.7 (Indigo) is used for this tutorial. Table of Contents 1. Eclipse BIRT 1.1. Overview 1.2. Example 2. Installation 3. Create Project and REport 4. Java classes 5. Datasource and Dataset 5.1. Create Data Source 5.2. The Dataset 5.3. JavaScript 6. Display the data in a table 6.1. Overview 6.2. Create a table 7. Chart 7.1. Create a Chart 8. Deploying in Tomcat 8.1. Overview 8.2. Install BIRT in Tomcat 8.3. Install your BIRT reports in Tomcat 9. Deploying in Eclipse RCP application 9.1. BIRT deploying to an RCP Application 10. Thank you 11. Questions and Discussion 12. Links and Literature 12.1. Source Code 12.2. Eclipse BIRT resources Tra Bo

Upload: moises-icaza

Post on 03-Jan-2016

627 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial.pdf

27/04/13 Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial

www.vogella.com/articles/EclipseBIRT/article.html 1/19

47

Free tutorial, donate tosupport 

 

by Lars Vogel

Reporting with Eclipse BIRT and Java Objects(POJO's) ­ Tutorial

Lars Vogel Hendrik Still

Version 1.2

Copyright © 2008 ­ 2011 Lars Vogel

26.06.2011

Revision History

Revision 0.1 ­ 0.2 07.03.2008 Hendrik Still, Lars Vogel Created Article

Revision 0.3 ­ 1.2 07.03.2009 ­ 26.06.2011 LarsVogel

bug fixes and enhancements

Eclipse Birt

This tutorial describes how to use Eclipse BIRT for reporting on simple Java Objects (POJO's). Thetutorial explains also how to deploy the resulting BIRT report into a webcontainer (Tomcat) and how touse it in an Eclipse RCP application. Eclipse 3.7 (Indigo) is used for this tutorial.

Table of Contents

1. Eclipse BIRT

1.1. Overview1.2. Example

2. Installation3. Create Project and REport4. Java classes5. Datasource and Dataset

5.1. Create Data Source5.2. The Dataset5.3. JavaScript

6. Display the data in a table

6.1. Overview6.2. Create a table

7. Chart

7.1. Create a Chart

8. Deploying in Tomcat

8.1. Overview8.2. Install BIRT in Tomcat8.3. Install your BIRT reports in Tomcat

9. Deploying in Eclipse RCP application

9.1. BIRT deploying to an RCP Application

10. Thank you11. Questions and Discussion12. Links and Literature

12.1. Source Code12.2. Eclipse BIRT resources

Training

Books

Page 2: Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial.pdf

27/04/13 Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial

www.vogella.com/articles/EclipseBIRT/article.html 2/19

12.3. vogella Resources

1. Eclipse BIRT1.1. Overview

Eclipse BIRT allows the creation of reports based on data from different data sources. Data sourcesdefine where the data is stored.

BIRT provides for example the following data sources:

Databases (via JDBC)

Text Files (cvs, XML)

WebServices (via WSDL­Files)

Scripting Data sources

You use in BIRT "Data sets" to defines queries on data source. These data sets can be used in a report.

In a Java program it is often convenient to use Java objects as a data source for reports. This article willfocus on the usage of plain old Java objects (POJO) as data sources for BIRT reports.

1.2. Example

In this tutorial we will build a report which will show us information about the stock market. We get theinformation from a Java Object. The data will be displayed in a chart and in a table with detailedinformation. The result should look like this:

2. Installation

Use the Eclipse Update Manager to install "Business Intelligence, Reporting and Charting" ­> BIRTFramework.

3. Create Project and REport

Create a new Java Project with the name "de.vogella.birt.stocks".

Page 3: Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial.pdf

27/04/13 Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial

www.vogella.com/articles/EclipseBIRT/article.html 3/19

Create a new report "stock_report.rptdesign" via File ­> New ­> Other ­> Business Intelligence andReporting ­> Report.

Page 4: Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial.pdf

27/04/13 Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial

www.vogella.com/articles/EclipseBIRT/article.html 4/19

The new report is displayed in the "Report Design" perspective. Delete everything in the example reportexcept the report header. The result should look like the following.

4. Java classes

The report will display stock data. To demonstrate BIRT we use a Mock object for providing the data.

Create package "de.vogella.birt.stocks.model" and then the following class. This class will representthe domain model.

package de.vogella.birt.stocks.model;

import java.util.Date;

/** * Domain model for stock data * @author Lars Vogel */

Page 5: Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial.pdf

27/04/13 Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial

www.vogella.com/articles/EclipseBIRT/article.html 5/19

public class StockData { private Date date; private double open; private double high; private double low; private double close; private long volume;

public double getClose() { return close; }

public void setClose(double close) { this.close = close; }

public Date getDate() { return date; }

public void setDate(Date date) { this.date = date; }

public double getHigh() { return high; }

public void setHigh(double high) { this.high = high; }

public double getLow() { return low; }

public void setLow(double low) { this.low = low; }

public double getOpen() { return open; }

public void setOpen(double open) { this.open = open; }

public long getVolume() { return volume; }

public void setVolume(long volume) { this.volume = volume; }

}

Create the package "de.vogella.birt.stocks.daomock" and then the following class "StockDaoMock".This will only mock / fake the data and not really get it from the Internet. As we want to learn BIRT herethis should be fine.

package de.vogella.birt.stocks.daomock;

import java.util.ArrayList;import java.util.Calendar;import java.util.List;

import de.vogella.birt.stocks.model.StockData;

public class StockDaoMock {

public List<StockData> getStockValues(String company) { // Ignore the company and always return the data // A real implementation would of course use the company string List<StockData> history = new ArrayList<StockData>(); // We fake the values, we will return fake value for 01.01.2009 - // 31.01.2009 double begin = 2.5; for (int i = 1; i <= 31; i++) { Calendar day = Calendar.getInstance(); day.set(Calendar.HOUR, 0); day.set(Calendar.MINUTE, 0);

Page 6: Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial.pdf

27/04/13 Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial

www.vogella.com/articles/EclipseBIRT/article.html 6/19

day.set(Calendar.SECOND, 0); day.set(Calendar.MILLISECOND, 0); day.set(Calendar.YEAR, 2009); day.set(Calendar.MONTH, 0); day.set(Calendar.DAY_OF_MONTH, i); StockData data = new StockData(); data.setOpen(begin); double close = Math.round(begin + Math.random() * begin * 0.1); data.setClose(close); data.setLow(Math.round(Math.min(begin, begin - Math.random() * begin * 0.1))); data.setHigh(Math.round(Math.max(begin, close) + Math.random() * 2)); data.setVolume(1000 + (int) (Math.random() * 500)); begin = close; data.setDate(day.getTime()); history.add(data); } return history; }}

5. Datasource and Dataset

To use Java Objects (POJO's) as datasource in Eclipse BIRT you have to map the fields of your Javaclasses to JavaScript. This JavaScript is used in your report and will access the Java Object.

5.1. Create Data Source

The data source connects your data with your report. BIRT provides different types of data sources, weuse the "Scripted Data Source". Go back to your stocks_report, use the "Report Design" perspectiveand select the "Data Explorer" View.

You have to select your report to display the content of the datasource view.

Create a new datasource, named "srcStocks" in your report.

5.2. The Dataset

Page 7: Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial.pdf

27/04/13 Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial

www.vogella.com/articles/EclipseBIRT/article.html 7/19

The dataset defines the mapping for the datasource data and the BIRT data. Create a new datasetnamed "dataSetSocks".

Press next and define the columns for your report.

5.3. JavaScript

Now we have to write the JavaScript for our dataset. Select the dataset and choose "open" as script.The open script is called before the first access to the dataset. We use this to load our List with the stockobjects. To access a Java class you only have to use the following syntax: Packages.myJavaClasswhere myJavaClass is the full qualified Java class name.

In case you do not see the script please node that the editor for the report has several tab.One of it is labeled "source".

Page 8: Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial.pdf

27/04/13 Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial

www.vogella.com/articles/EclipseBIRT/article.html 8/19

count = 0;

// Create instance of// the GetStockHistory classgsh = new Packages.de.vogella.birt.stocks.daomock.StockDaoMock();

//Load the List

stock = gsh.getStockValues("Java");

Place the following coding in the fetch script.

if(count < stock.size()){ row["columnDate"] = stock.get(count).getDate(); row["columnOpen"] = stock.get(count).getOpen(); row["columnHigh"] = stock.get(count).getHigh(); row["columnLow"] = stock.get(count).getLow(); row["columnClose"] = stock.get(count).getClose(); row["columnVolume"] = stock.get(count).getVolume(); count++; return true;}

return false;

Check if your Script works by doubleclicking on the dataset ­> Preview Result.

6. Display the data in a table6.1. Overview

We will now display the data in a table.

6.2. Create a table

Switch from "Data Explorer" to the "Palette". Select the tab "Layout".

Page 9: Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial.pdf

27/04/13 Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial

www.vogella.com/articles/EclipseBIRT/article.html 9/19

Drag and drop the table element on the report.

Define the following settings for the table.

Change back to the "Data Explorer". And drag and drop the dataset columns into the "Details row" ofthe table.

Page 10: Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial.pdf

27/04/13 Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial

www.vogella.com/articles/EclipseBIRT/article.html 10/19

The result should look like the following.

Done. You can see a preview of the report if you click on the "Review" Tab. The result should look likethe following:

7. Chart7.1. Create a Chart

Switch back to the Palette, select a chart and drag and drop it on your report.

Choose the Line Chart with the standard settings.

Page 11: Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial.pdf

27/04/13 Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial

www.vogella.com/articles/EclipseBIRT/article.html 11/19

Press Next and select your data set.

Page 12: Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial.pdf

27/04/13 Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial

www.vogella.com/articles/EclipseBIRT/article.html 12/19

At the next step we have to assign the columns to the axis. We assign the date to the x axis and theopen value to the y axis via drag and drop.

Define 5 series in total. Assign the columns to these series by dragging the column to the Sum sign.

Page 13: Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial.pdf

27/04/13 Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial

www.vogella.com/articles/EclipseBIRT/article.html 13/19

Currently the x axis shows first the newest date. Reverse the x axis by you have to sort the dataascending. Press the highlighted button.

Go to the next tab and give titles to your columns. Hide the last one.

Page 14: Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial.pdf

27/04/13 Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial

www.vogella.com/articles/EclipseBIRT/article.html 14/19

The display of the dates use a long format, we would like to change this. Perform the following andchoose "short" as date type of the x axis

Change the display of the lines via the following.

Press finish to include your chart into your report.

8. Deploying in Tomcat8.1. Overview

Page 15: Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial.pdf

27/04/13 Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial

www.vogella.com/articles/EclipseBIRT/article.html 15/19

The following explains how to use BIRT reports in Tomcat. In general you have to:

Install the BIRT webviewer in Tomcat

Export your BIRT project into a .jar file

Move the .jar file to the birt­install­directory/WEB­INF/lib directory

Move the report design file into the root directory of birt in tomcat

Restart Tomcat

8.2. Install BIRT in Tomcat

We will use a stand­alone Tomcat 6.0 which we assume is already installed. See Apache TomcatTutorial for details.

You need the "Deployment components of BIRT" http://download.eclipse.org/birt/downloads/.

Copy the birt.war of this download into the Tomcat webappsfolder.

Currently you have to install org.eclipse.commons.logging separately into Tomcat.Download this lib from http://commons.apache.org/logging/ and put the jars into the libfolder of Tomcat.

The Birt example should be available under http://localhost:8080/birt/.If you see something like this,your Tomcat an your Web Viewer should work correct.

8.3. Install your BIRT reports in Tomcat

To run your own reports you have to copy the .rptdesign file in the root of the birt folder in Tomcat. Tomake your Java classes available export your project into a jar file.

Page 16: Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial.pdf

27/04/13 Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial

www.vogella.com/articles/EclipseBIRT/article.html 16/19

BACK TO TOP

After that the jar file has to be copied to the Tomcat webapps/birt/WEB­INF/lib/ directory. Restart theTomcat and navigate to your report.

vogella.com Tutorials Training Services Publications Connect

Page 17: Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial.pdf

27/04/13 Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial

www.vogella.com/articles/EclipseBIRT/article.html 17/19

Your report should be found under http://localhost:8080/birt/frameset?__report=stock_report.rptdesign

If you want to export your report to PDF you also need the library iText from (http://prdownloads.sourceforge.net/itext/itext­1.3.jar) . Copy the iText.jar in "/birt­viewer/WEB­INF/platform/plugins/com.lowagie.itext/lib". Now restart the Tomcat.

9. Deploying in Eclipse RCP application9.1. BIRT deploying to an RCP Application

We can use the Birtviewer also in a local RCP Application, it isn't more than an browser view whichshows a HTML Page generated by an integrated Webserver.

The following assumes that you are already familiar with Eclipse RCP development. See Eclipse RCPTutorial in case you need an introduction.

Convert "de.vogella.birt.stocks" to a plug­in project, via right­click ­> Configure ­> "Convert to plug­inproject".

Create an new plug­in project "de.vogella.birt.stocks.rcp". Select the template "RCP Application with aview".

Add the following plugins as dependendies to "de.vogella.birt.stocks.rcp".

Manifest-Version: 1.0Bundle-ManifestVersion: 2Bundle-Name: RcpBundle-SymbolicName: de.vogella.birt.stocks.rcp; singleton:=trueBundle-Version: 1.0.0.qualifierBundle-Activator: de.vogella.birt.stocks.rcp.Activator

Page 18: Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial.pdf

27/04/13 Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial

www.vogella.com/articles/EclipseBIRT/article.html 18/19

Require-Bundle: org.eclipse.ui, org.eclipse.core.runtime, org.eclipse.birt.report.viewer, org.eclipse.birt.report.engine.emitter.html, de.vogella.birt.stocks, org.eclipse.birt, org.eclipse.birt.chart.ui, org.eclipse.birt.chart.device.extension, org.eclipse.birt.chart.device.pdf, org.eclipse.birt.chart.device.svg, org.eclipse.birt.chart.device.swt, org.eclipse.birt.chart.engine.extension, org.eclipse.birt.chart.reportitem, org.eclipse.birt.chart.reportitem.ui, org.eclipse.birt.chart.ui.extension, org.eclipse.birt.core.script.function, org.eclipse.birt.report.engine.script.javascriptBundle-ActivationPolicy: lazyBundle-RequiredExecutionEnvironment: JavaSE-1.6

Copy your report to "stock_report_rcp.rptdesign" into this new project. Open this report and change the"open" JavaScript to the following.

count = 0; /* * load and init data reader * import Platform from org.eclipse.core.runtime */importPackage(Packages.org.eclipse.core.runtime);

/* load bundle with POJOs and data loading class */

myBundle = Platform.getBundle("de.vogella.birt.stocks");

/* load data reader class */readerClass = myBundle.loadClass("de.vogella.birt.stocks.daomock.StockDaoMock");

/* create new instance of DataReader */readerInstance = readerClass.newInstance();

/* read data */stock = readerInstance.getStockValues("Java");

Use this code as View.java.

package de.vogella.birt.stocks.rcp;

import java.io.IOException;import java.net.MalformedURLException;import java.net.URL;

import org.eclipse.birt.report.viewer.utilities.WebViewer;import org.eclipse.core.runtime.FileLocator;import org.eclipse.core.runtime.Path;import org.eclipse.core.runtime.Platform;import org.eclipse.swt.SWT;import org.eclipse.swt.browser.Browser;import org.eclipse.swt.widgets.Composite;import org.eclipse.ui.part.ViewPart;import org.osgi.framework.Bundle;

public class View extends ViewPart { public static final String ID = "de.vogella.birt.stocks.rcp.view";

public void createPartControl(Composite parent) { String path = ""; try { Bundle bundle = Platform.getBundle("de.vogella.birt.stocks.rcp"); URL url = FileLocator.find(bundle, new Path("stock_report_rcp.rptdesign"), null); path = FileLocator.toFileURL(url).getPath(); } catch (MalformedURLException me) { System.out.println("Fehler bei URL " + me.getStackTrace()); } catch (IOException e) { e.printStackTrace(); }

Browser browser = new Browser(parent, SWT.NONE); // Use the filename of your report WebViewer.display(path, WebViewer.HTML, browser, "frameset"); }

Page 19: Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial.pdf

27/04/13 Reporting with Eclipse BIRT and Java Objects (POJO's) - Tutorial

www.vogella.com/articles/EclipseBIRT/article.html 19/19

/** * Passing the focus request to the viewer's control. */

public void setFocus() { }}

10. Thank you

Please help me to support this article:

 

11. Questions and Discussion

Before posting questions, please see the vogella FAQ. If you have questions or find an error in thisarticle please use the www.vogella.com Google Group. I have created a short list how to creategood questions which might also help you.

12. Links and Literature12.1. Source Code

Source Code of Examples

12.2. Eclipse BIRT resources

http://wiki.eclipse.org/index.php/BIRT_Project

Eclipse BIRT Wiki

12.3. vogella Resources

vogella Training Android and Eclipse Training from the vogella team

Android Tutorial Introduction to Android Programming

GWT Tutorial Program in Java and compile to JavaScript and HTML

Eclipse RCP Tutorial Create native applications in Java

JUnit Tutorial Test your application

Git Tutorial Put everything you have under distributed version control system