Previous Page Next Page
Programming with the Catalog Bean
Preparations
Using the Catalog Bean
Test tool program
JReport Catalog Bean provides a programming API for achieving functions such as creating an empty catalog, adding objects to a catalog, removing objects from a catalog, and getting object description in a catalog.
Preparations
Before programming with the Catalog Bean, you need to import the Catalog Bean classes by adding the following import statements to the beginning of your program. The first two import statements are added to import the Catalog Bean, and exception classes. The third is added to import the connection description class.
import jet.bean.JRCatalog;
import jet.bean.JRCatalogException;
import jet.universe.ConnectionDesc;
Then you need to create an instance of the Catalog Bean. The constructor of the Catalog Bean has no parameter. After the object has been created, you should then set the report home to the object.
JRCatalog jrCatalog = new JRCatalog();
jrCatalog.setReportHome("c:\\jreport") ;
With the Catalog Bean object, you can manipulate and access many catalogs. You do not need to create a bean for each catalog.
Using the Catalog Bean
The Catalog Bean provides many useful methods. The following shows how to use the Catalog Bean to achieve some goals.
Note that after finishing working on a catalog, the closeCatalog method should be called to close the catalog. This will release resource and clear up the temporary file. Also, the program requires using the closeCatalog method when operations on it have been completed.
Creating a catalog
The method newCatalog creates an empty catalog according to the catalog name that is set in catName, and saves it to disk.
When creating a new catalog, the directory of the catalog name should exist and contain no catalog files. You should make sure of this before you start creating the new catalog.
Try
{
jrCatalog.setCatName("c:\\a.cat");
newCatalog();
}
catch(JRCatalogException e)
{
System.out.println(e);
}
|
Loading an existing catalog
The method loadCatalog is used to load a catalog according to the catalog name that is set in catName.
try
{
jrCatalog.setCatName("c:\\a.cat");
loadCatalog();
}
catch(JRCatalogException e)
{
System.out.println(e);
}
|
Manipulating a catalog
A catalog can be manipulated by the actions of adding, deleting and editing of objects within it. A catalog contains many kinds of objects. These objects include connections, tables, views, synonyms, file queries, stored procedures, queries, WHERE portions, formulas, summaries and parameters.
For example, you can modify the connection, the name, the URL, the user, the password and the driver.
try
{
ConnectionDesc conDesc=new ConnectionDesc();
conDesc.strName="Demo";
conDesc.strURL="jdbc:oracle:thin:@myhost:1521:orcl";
conDesc.strUser="system";
conDesc.strPassword="manager";
conDesc.strDriver="oracle.jdbc.driver.OracleDriver";
jrCatalog.modifyConnection(conDesc);
}
catch(JRCatalogException e)
{
System.out.println(e);
}
|
Getting information
You can get information about objects in a catalog via some methods.
For example, you can get the connection information via the method getConnectionDesc().
Try
{
ConnectionDesc conDesc = bean.getConnectionDesc();
}
catch(JRCatalogException e)
{
System.out.println(e);
}
|
Saving a catalog
Changes to a catalog are saved to a disk only when the method saveCatalog is used.
Try
{
jrCatalog.saveCatalog();
}
catch(JRCatalogException e)
{
System.out.println(e);
}
|
Changing the catalog connection
In the following program, the Catalog Bean will be used to change the connection of a catalog:
try
{
// create catalog bean
JRCatalog jrCatalog = new JRCatalog();
// set report home
jrCatalog.setReportHome( "c:\\jrcbean");
// load catalog
jrCatalog.setCatName("c:\\a.cat">);
loadCatalog();
// modify connection
ConnectionDesc conDesc = new ConnectionDesc();
conDesc.strName = "Demo";
conDesc.strURL = "jdbc:oracle:thin:@myhost:1521:orcl";
conDesc.strUser = "System";
conDesc.strPassword = "Manager";
conDesc.strDriver = "oracle.jdbc.driver.OracleDriver";
jrCatalog.modifyConnection(conDesc);
// save catalog
jrCatalog.saveCatalog();
// close catalog
jrCatalog.closeCatalog();
}
catch(JRCatalogException e)
{
System.out.println(e);
}
|
Handling exceptions
When an error occurs, the JRCatalogException is thrown out. Your program will catch the exception and handle the error.
The exception describes the error code and detailed message as follows:
- Code: 0
Message: "System initialize failed."
Description: The system initialization process failed. The Catalog Bean needs to be initialized to resolve the environment. This error is caused by reasons such as wrong report home, and bad installation.
Detail: The reason why the initialization failed.
- Code: 1
Message: "The catalog name is null."
Description: The catalog name is null. The catalog name should be set before the loadReport() or newReport() methods are used.
Detail: None.
- Code: 2
Message: "Creating the catalog failed, because the specified directory does not exist or there is an existing catalog in that directory."
Description: Cannot create a catalog in the specified directory, because the directory does not exist or there is a catalog in the directory.
Detail: None
- Code: 3
Message: "Cannot open the catalog file."
Description: Cannot open the specified catalog file. The reason is file not found, file corrupt, format error or version mismatch.
Detail: The reason why the opening of a catalog file failed.
- Code: 4
Message: "Save catalog failed."
Description: Cannot save the catalog file successfully. The cause is a file writing error, or a formula file save has failed.
Detail: The reason why the saving of a catalog failed.
- Code: 5
Message: "No catalog loaded."
Description: The loadCatalog() or newCatalog() methods should be used before other methods, and closeCatalog() should be used in the end.
Detail: None
- Code: 6
Message: "The name of the WHERE portion is invalid or already exists."
Description: The name specified is invalid or has already been used by another WHERE portion.
Detail: None
- Code: 7
Message: "The specified WHERE portion does not exist."
Description: The specified WHERE portion does not exist.
Detail: None.
- Code: 8
Message: "A catalog can contain only one connection."
Description: A catalog can contain only one connection, and this error occurs when adding a second connection.
Detail: None.
- Code: 9
Message: "The name of the connection is invalid or already exists."
Description: The name specified is invalid or has already been used.
Detail: None
- Code: 10
Message: "Connection failure."
Description: The JDBC connection failed when trying to create a connection to the database.
Detail: The message of SQLException or ClassNotFoundException.
- Code: 11
Message: "The name of the file query (customer SQL) is invalid or already exists."
Description: The name of file query is either invalid or already exists.
Detail: None.
- Code: 12
Message: "Cannot create the file query."
Description: Cannot create file query object.
Detail: The reason why the file query cannot be created.
- Code: 13
Message: "There is no connection in the catalog."
Description: There is no connection in the catalog. If you are deleting or modifying a connection, the problem is that no such connection exists. If you are adding a file query, you cannot create it without a connection.
Detail: None
- Code: 14
Message: "The specified file query doesn't exist."
Description: The specified file query does not exist.
Detail: None.
- Code: 15
Message: "Unknown Error."
Description: The unexpected error.
Detail: None
- Code: 16
Message: "Invalid user ID or password"
Description: The user ID or password is invalid.
Detail: None
- Code: 17
Message: "Only Tables or Views can be added into catalog."
Description: The table type users specified is not supported.
Detail: None
- Code: 18
Message: "Close connection failure."
Description: When closing connection, an error occurred.
Detail: None
Test tool program
TestCatalogBean.java located in <install_root>\help\samples\APICatalog
, is a test tool for JReport Catalog Bean. It is primarily designed and built for you to test all the methods of JReport Catalog Bean.
The test tool program has a user interface, and all the functions of the Catalog Bean can be invoked using the menu items. Depending on success or failure of each test, you will get either a return result or an exception message respectively.
To run the JReport Catalog Bean test tool program, you should:
- Compile the source code of TestCatalogBean.java. For example, JReport Designer is installed in
C:\JReport\Designer
, you can compile the it using the following command (make sure that the path of the file JREngine.jar is before that of the file report.jar):
C:\JReport\Designer\help\samples\APICatalog>javac -classpath "C:\JReport\Designer\lib\JREngine.jar;C:\JReport\Designer\lib\sac-1.3.jar;C:\JReport\Designer\lib\report.jar;" TestCatalogBean.java
- Run the test tool:
C:\JReport\Designer\help\samples\APICatalog>java -Dreporthome="C:\JReport\Designer" -classpath "C:\JReport\Designer\lib\JREngine.jar;C:\JReport\Designer\lib\sac-1.3.jar;C:\JReport\Designer\lib\report.jar;C:\JReport\Designer\lib\log4j-core-2.7.jar;C:\JReport\Designer\lib\log4j-api-2.7.jar;C:\JReport\Designer\lib\hsqldb-2.3.4.jar" TestCatalogBean
After activating the program, a window will be displayed.
- Before starting other commands, you should first set the home path and the catalog name.
Select File > Set Home Directory to set the home path and File > Set Catalog Name to set the catalog name.
- Then, you can create a new catalog with the specified catalog name, or load an existing catalog in the same way.
- The test tool also provides the source code of each method, and you can copy and paste it into your program from the text window.
Previous Page Next Page