Demo 1: Creating reports from simple Java object UDS

Before running the demo, you need to do the following:

  1. Copy all the content in <install_root>\help\samples\APIUDS\javaUDS to <install_root>\help\UDSForJavaBean. You need to create the UDSForJavaBean directory.
  2. 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

  3. Copy data.txt in <install_root>\help\UDSForJavaBean to <install_root>\bin.
  4. 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:

  1. 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.
  2. 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:

    For details about how to create parameters, see Creating a parameter.

  3. Right-click the data source node, and select Add User Defined Data Source from the shortcut menu.
  4. In the Add User Defined Data Source dialog, enter Person in the Name text field.
  5. 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.
  6. 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:

    You can use this provider (jreport.uds.javabean.GenericBeanDataProvider) or create your own provider by implementing the interface jreport.uds.javabean.JavaBeanDataProvider.

  7. Click OK to add the UDS.
  8. Create a standard banded report with this UDS using the report wizard.
  9. 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.
  10. 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.

  11. 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;

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();