Example 3: Adding a Java Object Data Source UDS
The Logi JReport Designer UDS can use any Java object as a data source for you to create reports. To implement this function, Logi JReport Designer provides a class UDSForJavaBean, in which the getResultSet() method creates instances for the interface JavaBeanDataProvider, and then uses the method init() to initialize the data of the created instances.
Below is the Java code for class definition:
|
The interface JavaBeanDataProvider is defined as follows:
|
For examples of how to use the Java object data interface, Logi JReport provides you with two demos which are available in <install_root>\help\samples\APIUDS\javaUDS
.
Before running the demos, 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-core-2.7.jar;<install_root>\lib\log4j-api-2.7.jar; <install_root>\help\UDSForJavaBean\Logi 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.
- Below is a list of the sections covered in this topic:
-
Demo 1: Using a Simple Java Object UDS
Demo 2: Using a Java Object UDS with Multi-levels of Collections
In the second week of December 2021, a Log4j vulnerability was announced that may affect some customers using our products. Resolving/mitigating this issue is a high priority! We will continue to issue information to help you with this vulnerability. For more information, select this link: Statement on Log4j and Log4Net Vulnerabilities.
Demo 1: Using a Simple Java Object UDS
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\Logi JReport\uds\javabean\beans
. It is defined as follows:
|
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.
To create a report from the Person Java object:
- Make the necessary preparations.
- In Logi JReport Designer, open an existing catalog, then in the Catalog Manager, 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.
- Name: pUseFakeData (Specify whether to use generated data when running the report.)
- Right-click the data source node, and select New User Defined Data Source from the shortcut menu.
- In the New User Defined Data Source dialog, enter Person in the Name text field.
- For the Class Name field, select the Browse button, go to
<install_root>\help\UDSForJavaBean\Logi JReport\uds\javabean
, and then choose UDSForJavaBean.class, which will import the class Logi JReport.uds.javabean.beans.Person with full class name. - In the Parameter box, type in the following string:
JavaBeanDS_DataProvider=Logi JReport.uds.javabean.GenericBeanDataProvider&JavaBeanDS_RuntimeDataID=persions&GBeanProvider_BeanClsName=Logi JReport.uds.javabean.beans.Person&GBeanProvider_UseFakeData=true&GBeanProvider_NumOfFakeData=@pNumOfFakeData&GBeanProvider_RptDataInitializer=Logi 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 Logi 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 - Logi JReport.uds.javabean.GenericBeanDataProvider - which is specified by JavaBeanDS_DataProvider.
You can use this provider (Logi JReport.uds.javabean.GenericBeanDataProvider) or create your own provider by implementing the interface Logi JReport.uds.javabean.JavaBeanDataProvider.
- Select OK to add the UDS.
- Create a page report with a standard banded object in it which is based on this UDS.
- Select the View tab to run the report. In the Enter Parameter Values dialog, type 3 as the value of the parameter pNumOfFakeData, and select 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 Manager, right-click the UDS Person and select 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 Logi JReport.uds.javabean.GenericBeanDataProvider is used to specify the Java class name which implements Logi JReport.uds.javabean.RptDataInitializer interface. So if you are using the data provider Logi JReport.uds.javabean.GenericBeanDataProvider, you just need to provide a class which implements Logi JReport.uds.javabean.RptDataInitializer to return a collection, list, or array of the data objects according to different reports and parameters. Also, Logi 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
Logi JReport.uds.javabean.GenericBeanDataProvider
Logi JReport.uds.javabean.GenericBeanDataProvider implements the interface of Logi 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_RptDataInitializerThe 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 the UDS when the Logi 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.
Logi 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.
Demo 2: Using a Java Object UDS with Multi-levels of Collections
Sometimes, the attributes of a Java object are defined by other lists/collections, such as the Java object SimpleBeanTest in <install_root>\help\UDSForJavaBean\Logi JReport\uds\javabean\beans
. It is defined as follows:
|
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, Logi 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 a primary report and subreport to implement this function.
Task 1: Create the primary report
- Make the necessary preparations.
- In Logi JReport Designer, open an existing catalog, then in the Catalog Manager, 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. - Right-click the data source node and select New User Defined Data Source on the shortcut menu. The New User Defined Data Source dialog appears.
- Type SimpleTestBean in the Name text field.
- For the Class Name filed, select the Browse button, go to
<install_root>\help\UDSForJavaBean\Logi JReport\uds\javabean
, and then choose UDSForJavaBean.class. The UDS class UDSForJavaBean.class will import the class Logi JReport.uds.javabean.beans.SimpleTestBean with full class name. - In the Parameter box, type in the following string:
JavaBeanDS_DataProvider=Logi JReport.uds.javabean.GenericBeanDataProvider
&JavaBeanDS_RuntimeDataID=&GBeanProvider_BeanClsName=Logi JReport.uds.javabean.beans.SimpleBeanTest
&GBeanProvider_UseFakeData=true&GBeanProvider_NumOfFakeData=@pNumOfFakeData
&GBeanProvider_FakeDateSubCollectionInfo=persons,Logi JReport.uds.javabean.beans.Person
&GBeanProvider_RptDataInitializer=&GBeanProvider_ListOfDetailProps=persons,1,30000All 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.
- GBeanProvider_ListOfDetailProps specifies the following items:
- Select OK and the UDS SimpleTestBean is added. In the Catalog Manager, you will see that persons appears in the resource tree in the SimpleTestBean node.
- Create a page report named MainRpt.cls, with a standard banded object in it as the primary report based on the UDS SimpleTestBean.
Task 2: Create 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 data source node and select New User Defined Data Source on the shortcut menu. The New User Defined Data Source dialog appears.
- Type PersonsAsSubRpt in the Name text field.
- For the Class Name text box, select the Browse button, go to
<install_root>\help\UDSForJavaBean\Logi JReport\uds\javabean
, and then choose UDSForJavaBean.class. - In the Parameter box, type in the following string:
JavaBeanDS_DataProvider=Logi JReport.uds.javabean.GenericBeanDataProvider&JavaBeanDS_RuntimeDataID=@pRunTimeDataInfo&GBeanProvider_BeanClsName=Logi JReport.uds.javabean.beans.Person&GBeanProvider_UseFakeData=@pUseFakeData&GBeanProvider_NumOfFakeData=@pNumOfFakeData&GBeanProvider_RptDataInitializer=Logi 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 Logi 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.
- Select OK, and the UDS PersonsAsSubRpt is added.
- Create a page report named SubRpt.cls, with a table in it as the subreport based on the UDS PersonsAsSubRpt.
Task 3: Link the primary report and the subreport
- In the Catalog Manager, 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 select Insert > Subreport. When a box attached to your mouse pointer, select in the panel and the Subreport dialog is displayed.
- Select 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) - Select OK to insert the subreport. For more information about using subreports, see the document Subreports.
- 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.