JasperReports JavaBean data source in JasperWave
Join the DZone community and get the full member experience.
Join For FreeThis article will explain the following information about JavaBean data source:
- how to create data source
- how to configure classpath to make the data source available for the designer
- how to define and configure JasperReports JavaBean data source in JasperWave
- how to extract report fields from JavaBean object
- how to design and preview the report using JavaBean data source
The purpose of the article is to get a good starting point for report developers who use JasperReports JavaBean (POJO) based data sources to design and run their reports with JasperWave Report Designer.
Sources
The sources for the article JavaBeansDatasourceExample.zip. This article will be most effective if you are able to reproduce each step within your working environment using sources we’ve provided as you read it. The example in the article was built and tested on:
Notice: the example will not work with minor versions of JasperWave Report Designer.
Introduction to JasperReports JavaBean data source
The JasperReports JavaBean data source implementation can wrap collections or arrays of JavaBean objects and relies on Java reflection to retrieve report field data from the wrapped objects.This data source can be used to generate reports using data already available in-memory in the form of POJOs. Each object inside the array or the collection will be seen as one record in this type of data source.
The mapping between a particular JavaBean property and the corresponding report field is made by naming conventions. The name of the report field must be the same as the name of the JavaBean property as specified by the JavaBeans specifications. For instance, to retrieve the value of a report field named address, the program will try to call through reflection a method called getAddress() on the current JavaBean object.
Nested JavaBean properties can be also accessed in a JavaBean data source. For example, if the current JavaBean object inside the data source is of type Product and contains nested supplier information accessible by calling the getSupplier() method, which returns a Supplier object. In this case, to access the address property inside the Supplier object, a report field named supplier.address is required.
Create a JasperReports JavaBean data source
The JasperReports JavaBean data source consists of two classes:
- JavaBean. A simple JavaBean class with properties and their accessors.
- Factory. A factory class is used to provide JavaBeans data either as JavaBean arrays or as JavaBean collections. In our sample, it will be Collection.
I have created two classes - ActorBean
package mybeans; import java.util.Date; public class ActorBean { private String movie; private String character; private String name; private Date birthday; public ActorBean() { } public ActorBean(String movie, String character, String name, Date birthday) { super (); this .movie = movie; this .character = character; this .name = name; this .birthday = birthday; } public String getMovie() { return movie; } public void setMovie(String movie) { this .movie = movie; } public String getCharacter() { return character; } public void setCharacter(String character) { this .character = character; } public String getName() { return name; } public void setName(String name) { this .name = name; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this .birthday = birthday; } } |
and BeanFactory to generate the Collection of ActorBean objects
package mybeans; import java.util.ArrayList; import java.util.GregorianCalendar; import java.util.List; public class BeanFactory { public BeanFactory() { } public List<ActorBean> getActorBeans() { List<ActorBean> actors = new ArrayList<ActorBean>(); ActorBean actor1 = new ActorBean("Californication", "HANK MOODY", "DAVID DUCHOVNY", new GregorianCalendar(1960, 8, 7).getTime()); actors.add(actor1); ActorBean actor2 = new ActorBean("Breaking Bad", "Walter White", "Bryan Cranston", new GregorianCalendar(1956, 3, 7).getTime()); actors.add(actor2); ActorBean actor3 = new ActorBean("Californication", "KAREN VAN DER BEEK", "NATASCHA MCELHONE", new GregorianCalendar(1969, 12, 14).getTime()); actors.add(actor3); ActorBean actor4 = new ActorBean("Breaking Bad", "Jesse Pinkman", "Aaron Paul", new GregorianCalendar(1979, 8, 27).getTime()); actors.add(actor4); ActorBean actor5 = new ActorBean("Californication", "CHARLIE RUNKLE", "EVAN HANDLER", new GregorianCalendar(1961, 1, 10).getTime()); actors.add(actor5); return actors; } } |
Classses have been compiled and packaged into JAR JavaBeanDatasource.jar after then our JavaBean data source is ready to be used.
Note:
Of course, nothing prevents us to create static method whithin ActorBean class to return the collection of data objects but it would be rather sample use case than common use of JavaBean data source.
JavaBean data source in JasperWave
Setup classpath
In order to use created JavaBean data source within JasperWave Report Designer, you should dat source JAR into classpath.To set the classpath follow steps below:
- Open classpath preferences: Window-Preferences - JasperWave - Classpath Preferences
- Press Add External JARs... button and add JavaBeanDatasource.jar
Configure the JavaBean data source
Now, let's define and configure the new data source using classes defined above - ActorBean and BeanFactory. To do that, we need to create new report since without it, the toolbar will be unavailable.
- Open Data Source dialog using toolbar button
- Press New Datasource button to start data source wizard
- Select JavaBeans Datasource
- Fulfil the data source settings
Notes:
- Your classes should be in JasperWave classpath. If you missed something, no need to close the dialog and go to Window - Preferences - JasperWave - Classpath Preferences. You can do that using Classpath button, all changes will be applied immediatelly.
- Use Get methods button to read factory class methods. Notice: only methods with return type Collection or Array will be read.
Extract report fields from JavaBean data source
As mentioned, the mapping between a particular JavaBean property and the corresponding report field is made by naming conventions.
The name of the report field must be the same as the name of the JavaBean property.
Based on a such conventions, you can create report fields manually, but JasperWave Report Designer provides you with simple and convinient way:
- Press Report Fields button
- Select report fields to be created within the report and press Ok button
- New report fields are available to use
Notes:
- Use combobox in the left to specify which property should be converted into report field.
- Using Update options section, you can manage where and how to add new report fields.
Design the report
I have created a simple report which consists of listing of all fields grouped by $F{movie}.
In order to have this grouping working, it's required to add sorting by the same field - in our case, it's $F{movie}.
Preview the report
Before running the preview, make sure that your JavaBean data source is active . If not so, either select the checkbox in Data Source dialog or select the data source from the combobox on the main toolbar.
Opinions expressed by DZone contributors are their own.
Comments