From version 1.2 of Inforama Java Beans/POJOs (Plain Old Java Objects) can be used as data sources. JavaBean properties are accessed using dot notation which means: if an object obj has a method getProperty() then we can refer to the value that this method returns (when invoked on obj) by using obj.property notation.
The steps below show how to set up JavaBeans Data Source and define associated Data Sets. All the examples are taken from the JavaBeansSample project which is attached to this wiki entry.
Setting up Java Beans as a data source
Each
JavaBeans data source specifies a set of
jar files containing POJOs (or rather
java classes specifying
POJOs) which are to be used in a
Inforama project. In order to set up a new data source go to
Data->Connection/Data Sources, press the
New button, choose the
Java Beans as the type of data source that you want to add and finally choose a jar files containing
POJOs which you want to use as a "data providers" in your
Inforama project. The sample attached project is using
samplejavabeans.jar file as its data source.
The file contains a definition of the classes:
Account
package org.in4ama.beans;
public class Account {
private AccountDesc desc1, desc2;
public Account(String desc1, String desc2) {
this.desc1 = new AccountDesc(desc1);
this.desc2 = new AccountDesc(desc2);
}
public AccountDesc getDesc1() {
return desc1;
}
public AccountDesc getDesc2() {
return desc2;
}
}
AccountDesc
package org.in4ama.beans;
public class AccountDesc {
private String desc;
public AccountDesc(String desc) {
this.desc = desc;
}
public String getDesc() {
return desc;
}
}
User
package org.in4ama.beans;
import java.util.LinkedList;
import java.util.List;
public class User {
private String firstName, lastName;
private final List<Account> accounts = new LinkedList<Account>();
public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFristName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public List<Account> getAccounts() {
return accounts;
}
}
UserRepository
package org.in4ama.beans;
import java.util.LinkedList;
import java.util.List;
public class UserRepository {
private final static List<User> users = buildUsers();
private static List<User> buildUsers() {
....
}
public static List<User> getUsers() {
return users;
}
}
The
samplejavabeans.jar file is located in the
resources folder of the project.
Creating Data Sets from a Java Beans Data Source
Data Sets associated with
Java Beans Data Sources are defined by any
Java expression that "returns" something, it can be either a scalar or a collection.
The
document1 in the sample project is listing all users thus we need a data set containing that list. The screenshot below shows the
Java expression that is used.
data set definition
 |
Since the
UserRepository.getUsers() method returns a list containing objects of a
User type we have access to the
firstName,
lastName and
accounts (another list) properties when using the
allUsers data set.
Sample document
The screenshot below shows a sample document listing all users from the allUsers data set which was defined above. The document (named document1) is included in the attached project.
document using data set derived from the JavaBeans data source
 |
The output document generated from this template looks like shown below.
result PDF document filled with data provided by POJOs
 |