Demo 1: Creating reports from simple Java object UDS
Before running the demo, you need to do the following:
- Copy all the content in
<install_root>\help\samples\APIUDS\javaUDS
to <install_root>\help\UDSForJavaBean
. You need to create the UDSForJavaBean directory.
- Compile all the java files.
javac -classpath <install_root>\help\UDSForJavaBean;<install_root>\lib\JREngine.jar;<install_root>\lib\log4j-1.2.8.jar; <install_root>\help\UDSForJavaBean\jreport\uds\javabean\*.java
- Copy data.txt in
<install_root>\help\UDSForJavaBean
to <install_root>\bin
.
- Add the path
<install_root>\help\UDSForJavaBean
in the ADDCLASSPATH variable in setenv.bat in the <install_root>\bin
directory.
In this demo, a Java object named Person will be used as the data source, you can find the person.java file in <install_root>\help\UDSForJavaBean\jreport\uds\javabean\beans
. It is defined as follows:
public class Person implements Serializable
{
private String gender;
private String ssn;
private String emailAddress;
private Date birthDate;
private Long newbornWeight;
private YesNo isDataVerified;
private Name name;
private Address currentMailingAddress;
private Phone currentWorkPhone;n;
/**
* @return the birthDate.
*/
public Date getBirthDate()
{
return birthDate;
}
}
|
Similar to the code above, other attributes of the Person object are defined by other Java objects, such as currentMailingAddress which is defined by the Address class.
Creating a report from the Person Java object
Take the following steps:
- In JReport Designer, open an existing catalog, then in the Data tab of the Catalog Browser, expand the data source to which the UDS is to be added.
- This demo uses generated data, in order to get the real collection of the data objects, create two parameters before importing the Person Java object as follows:
- Name: pUseFakeData (Specify whether to use generated data when running the report.)
Value Type: Boolean
Prompt Value: True
- Name: pNumOfFakeData (Specify the number of data records to generate that will be shown when running the report.)
Value Type: Integer
Prompt Value: Any integer number
For details about how to create parameters, see Creating a parameter.
- Right-click the User Defined node, and select Add User Defined Data Source from the shortcut menu.
- In the Add User Defined Data Source dialog, enter Person in the Name text field.
- For the Class Name field, click the Browse button, go to
<install_root>\help\UDSForJavaBean\jreport\uds\javabean
, and then choose UDSForJavaBean.class, which will import the class jreport.uds.javabean.beans.Person with full class name.
- In the Parameter box, type in the following string:
JavaBeanDS_DataProvider=jreport.uds.javabean.GenericBeanDataProvider&JavaBeanDS_RuntimeDataID=persions&GBeanProvider_BeanClsName=jreport.uds.javabean.beans.Person&GBeanProvider_UseFakeData=true&GBeanProvider_NumOfFakeData=@pNumOfFakeData&GBeanProvider_RptDataInitializer=jreport.uds.javabean.SubRptCollectionDataInitializer
All the highlighted names in the parameter string above are the key words for the information required by this UDS and related Java class. See the detailed explanation below:
- JavaBeanDS_DataProvider is used to specify the Java class which implements jreport.uds.javabean.JavaBeanDataProvider interface, and will return the list of required data objects at runtime – in this demo, it is a list of Person objects.
- JavaBeanDS_RuntimeDataID is a reserved value used as a key to get data objects from the DataCenter.
- GBeanProvider_* are required values for the special data provider - jreport.uds.javabean.GenericBeanDataProvider - which is specified by JavaBeanDS_DataProvider.
You can use this provider (jreport.uds.javabean.GenericBeanDataProvider) or create your own provider by implementing the interface jreport.uds.javabean.JavaBeanDataProvider.
- Click OK to add the UDS.
- Create a standard banded report with this UDS using the report wizard.
- Click the View tab to run the report. In the Enter Parameter Values dialog, type 3 as the value of the parameter pNumOfFakeData, and click OK. Three records will be returned. However, the data you get now is the generated data, because in the parameter string of the UDS, you have specified the value of the key word GBeanProvider_UseFakeData to true.
- In order to get the real collection of data objects, the parameter pUseFakeData will be used to control the value of the key GBeanProvider_UseFakeData value dynamically as follows.
In the Catalog Browser, right-click the UDS Person and click Edit User Defined Data Source on the shortcut menu. In the Edit User Defined Data Source dialog, modify the value of GBeanProvider_UseFakeData to @pUseFakeData in the Parameter box.
- Run the report again and specify the value of pUseFakeData as false to get the real collection of data at runtime.
Note: The key word GBeanProvider_RptDataInitializer in the data provider jreport.uds.javabean.GenericBeanDataProvider is used to specify the Java class name which implements jreport.uds.javabean.RptDataInitializer interface. So if you are using the data provider jreport.uds.javabean.GenericBeanDataProvider, you just need to provide a class which implements jreport.uds.javabean.RptDataInitializer to return a collection, list, or array of the data objects according to different reports and parameters. Also, jreport.uds.javabean.GenericBeanDataProvider can recognize vector, collection and array of objects and retrieve the objects inside of the collection one by one.
Methods in the demo
jreport.uds.javabean.GenericBeanDataProvider
jreport.uds.javabean.GenericBeanDataProvider implements the interface of jreport.uds.javabean.JavaBeanDataProvider by using the following methods:
public void init(String dataID, Properties initprops) throws JavaBeanDataProviderException;
public Class getMetadataJavaBean() throws ClassNotFoundException;
public Object next() throws JavaBeanDataProviderException;
public boolean requireDetails(String collectionPropName);
public int getMaxShareTimes(String collectionPropName);
public void exit()throws JavaBeanDataProviderException;
- init()
This method is called by UDSForJavaBean to ask the data provider to prepare the data collection/list by the given initProperties. Basically, the initProperties are the name and value pairs parsed from the UDS parameter string.
For example, the GenericBeanDataProvider will get the list of properties with the following keys:
JavaBeanDS_DataProvider
JavaBeanDS_RuntimeDataID
GBeanProvider_BeanClsName
GBeanProvider_UseFakeData
GBeanProvider_NumOfFakeData
GBeanProvider_RptDataInitializer
The values for those keys will be used by GenericBeanDataProvider to prepare the data collection. For example, if the value of GBeanProvider_UseFakeData is true, the GeneridBeanDataProvider will use the fake data, otherwise it will create an instance by the class name provided by GBeanProvider_RptDataInitializer and ask the data initializer object to return the collection of data objects.
- getMetadataJavaBean()
This method is called back from the UDS to get the bean class in order to construct the meta data for UDS. For GenericBeanDataProvider, the Java bean class in the collection is passed in by the key GBeanProvider_BeanClsName when you define this data source by UDS.
- next()
This method is called back from the UDS to fetch the next data object as a record for the report at runtime. For GenericBeanDataProvider, the init() method constructs the data object collection, and the next() method is going to check if the collection is a Vector, Collection, or array of objects to determine automatically how to get the next object from the constructed collection.
- exit()
This method is called back from theUDS when the JReport engine closes the result set returned by UDS. For GenericBeanDataProvider, it will call the data initializer to close if the data is constructed by data initializer.
- requireDetails()
This method is called back by the UDS to check if a certain collection attribute from Java bean needs to be displayed in the metadata.
- getMaxShareTimes()
The sub-collection attribute from the Java data object could be shared among subreports. This is the call-back method from the UDS to determine the maximum number of times that a certain sub-collection object is going to be shared by the current report. If your report is trying to share the same sub-collection of a Java data object more than this specified value, you will get an error at runtime. However, if your report actually needs to be shared less than the number specified, the data will stay in the buffer without being cleaned.
jreport.uds.javabean.RptDataInitializer
The implementation of this interface will be used by GenericBeanDataProvider to provide the collection of Java objects for different reports and parameters.
The following methods need to be implemented for this interface:
public Object getDataCollection(Properties props)throws RptDataInitializerException;
public void close();
- getDataCollection()
This method is called by GenericBeanDataProvider to return the data collection object. The input props are passed down from GenericBeanDataProvider and includes all the values passed down into the init() method of the GenericBeanDataProvider.
- close()
This method is called back by GenericBeanDataProvider when exit() function is called there.