
Demo 2: Creating reports from Java object UDS with multi-levels of collections
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.
Sometimes, the attributes of a Java object are defined by other lists/collections, such as the Java object SimpleBeanTest in <install_root>\help\UDSForJavaBean\jreport\uds\javabean\beans
. It is defined as follows:
public class SimpleBeanTest implements Serializable {
private String test;
private long l;
private int i;
private int[] intarray;
private Person[] persons;
private Collection addresses;
private Date dMyDate;
}
|
From the code above, you can see that the Java class SimpleTestBean contains an array of Persons, a collection of addresses and an array of Int values.
For this kind of Java object, JReport Designer can create a report that gets records from the SimpleTestBean, but it cannot show the list of persons information in the same report. If you want to create such a report - each record comes from the SimpleBeanTest object, and for each record, display the list of persons information - you have to use the primary report and subreport to implement this function. For how to do it, see the demo below.
Creating the primary report
- 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.
- Create a parameter as follows, which is used to specify the number of data records to generate that will be shown when running the report.
Name: pNumOfFakeData
Value Type: Integer
Prompt Value: Any integer number. Note that the parameter must have at least one value that is larger than 0, otherwise you will get exceptions when viewing reports.
For details about how to create parameters, see Creating a parameter.
- Right-click the User Defined node and click Add User Defined Data Source on the shortcut menu.
- In the Add User Defined Data Source dialog, type SimpleTestBean in the Name text field.
- For the Class Name filed, click the Browse button, go to
<install_root>\help\UDSForJavaBean\jreport\uds\javabean
, and then choose UDSForJavaBean.class. The UDS class UDSForJavaBean.class will import the class jreport.uds.javabean.beans.SimpleTestBean with full class name.
- In the Parameter box, type in the following string:
JavaBeanDS_DataProvider=jreport.uds.javabean.GenericBeanDataProvider
&JavaBeanDS_RuntimeDataID=&GBeanProvider_BeanClsName=jreport.uds.javabean.beans.SimpleBeanTest
&GBeanProvider_UseFakeData=true&GBeanProvider_NumOfFakeData=@pNumOfFakeData
&GBeanProvider_FakeDateSubCollectionInfo=persons,jreport.uds.javabean.beans.Person
&GBeanProvider_RptDataInitializer=&GBeanProvider_ListOfDetailProps=persons,1,30000
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:
- GBeanProvider_ListOfDetailProps specifies the following items:
- The list of property names of sub-collections that will be displayed in the meta-data
- How many times that this report is going to share the sub-collection per main object
- The timeout for the shared data in the DataCenter in milliseconds (the default value is 1 minute).
In this demo, only the sub-collection property persons will be imported from the SimpleTestBean class, and the share time is 1. If you want to create another subreport using addresses, you have to specify the value for this key word as the following:
=&GBeanProvider_ListOfDetailProps=persons,1,30000$addresses,1,30000
and different properties are separated by the symbol $.
- GBeanProvider_FakeDateSubCollectionInfo
The value of this key word is used to construct the fake data for SimpleTestBean collections. Each Java class can have multiple sub-collection objects, and it is allowed to not construct the values of these sub-collection objects if you do not want to use it. However, if you do not specify them here in this parameter string as <property name>,<property class name>$<property name>,<property class name>
, the fake data for those sub-collections will not be constructed, and problems will occur when running the report on fake data.
- Click OK and the UDS SimpleTestBean is added. In the Catalog Browser, you will see that persons appears in the resource tree in the SimpleTestBean node.
- Create a page report named MainRpt.cls, and create a standard banded report in it as the primary report based on the UDS SimpleTestBean.
Creating the subreport
- Expand the data source in the catalog to which you want to add the UDS for the subreport.
- Create a parameter as follows, which is used to specify whether to use generated data when running the report.
Name: pUseFakeData
Value Type: Boolean
Prompt Value: True
- Create another parameter named pRunTimeDataInfo of String type, which will be used when setting up the link between the primary report and the subreport.
Name: pRunTimeDataInfo
Value Type: String
Prompt Value: persons
- Right-click the User Defined node and click Add User Defined Data Source on the shortcut menu.
- In the Add User Defined Data Source dialog, type PersonsAsSubRpt in the Name text field.
- For the Class Name text box, click the Browse button, go to
<install_root>\help\UDSForJavaBean\jreport\uds\javabean
, and then choose UDSForJavaBean.class.
- In the Parameter box, type in the following string:
JavaBeanDS_DataProvider=jreport.uds.javabean.GenericBeanDataProvider&JavaBeanDS_RuntimeDataID=@pRunTimeDataInfo&GBeanProvider_BeanClsName=jreport.uds.javabean.beans.Person&GBeanProvider_UseFakeData=@pUseFakeData&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_RuntimeDataID is defined to use a parameter which will be the link point from the subreport to the sub-collections in the primary report
- GBeanProvider_RptDataInitializer's value is jreport.uds.javabean.SubRptCollectionDataInitializer, which is our built-in data provider set up especially for the subreport to return the collection of data by referencing the sub-collection in the primary report. The referencing information is passed via the parameter value @pRunTimeDataInfo which will be used when we set up the link between the primary report and the subreport.
- The class name for GBeanProvider_BeanClsName is Person because the subreport will use the Person object.
- Click OK, and the UDS PersonsAsSubRpt is added.
- Create a page report named SubRpt.cls, and create a table report in it as the subreport based on the UDS PersonsAsSubRpt.
Linking the primary report and the subreport
- In the Catalog Browser, create a formula named NotUseFakeData to return false all the time, for example,
return false
. This formula will be passed into the subreport as the value of the parameter pUseFakeData in the subreport, so that when the subreport runs with the primary report, it will always use the data from the primary report instead of constructing the fake data itself.
- Open MainRpt.cls, add a new detail panel into the report.
- Selected the newly added panel and click Insert > Subreport. When a box attached to your mouse pointer, click in the panel and the Subreport dialog is displayed.
- Click the Browse button, choose SubRpt.cls as the subreport, then in the Parameters tab, specify values for the parameters as follows:
Parameter |
Value |
pNumOfFakeData |
pNumOfFakeData |
pUseFakeData |
NotUseFakeData (gives a false value to the parameter) |
pRunTimeDataInfo |
persons (specifies to use the sub-collection persons of the primary report as the data source of the subreport at runtime) |
- Click OK to insert the subreport.
- Run the primary report, and you will find that the corresponding persons information is displayed in the subreport.
- If you want to insert another subreport which shares the same sub-collection with SubRpt.cls, you should modify the value of the key word GBeanProvider_ListOfDetailProps in the primary report UDS parameter string to GBeanProvider_ListOfDetailProps=persons,2,30000. That changes the share amount of the persons property from 1 to 2.
For more information about subreport in JReport, see the section Subreports.
