JReport Viewer Bean Usage
In order to make the Viewer easily embedded in user's Java
applications, the JReport Viewer is now also available
as a Java Bean. The JReport Viewer Bean can send a request from a client workstation
to run and view reports on the JReport Server.
The first step is to download the jvbean.zip file
from the web:
http://www.jinfonet.com/jvbean_download.htm
There are 5 files inside jvbean.zip:
- Sample1.java and Sample2.java
are examples.
JRViewer.zip is
used while compiling your Java program which will be called JReport Viewer Beans. It is also
used at runtime when your Java application calls the Viewer Bean.
OneStepJRViewer.jar is used instead of JRViewer.zip if you invoke the Viewer Bean without supplying report parameters (see
below).
TwoStepsJRViewer.jar is used instead of JRViewer.zip
if you invoke the Viewer Bean and supply report parameters (see below).
The two approaches of invoking the JReport Viewer
Bean are described in the following sections.
Invoking Viewer Bean without Supplying
Report Parameters
In this approach, your Java applications calls
the Java bean JRDirectViewer and provides the following properties:
- setServerName ("theServerName");(204,177,.148.110")
- setPort ("thePortNumber");
("8888")
- setReportName ("theReportName"); ("phaseorder")
- setCatName ("theCatalogName");
("Tutorial");
- setLoadReport ("Yes/No");
After the Java bean JRDirectViewer is loaded, it will ask the JReport Server to run the report
and will pop up a dialog on the screen to ask the user to input report parameters if they
are required by the report. This approach has the advantage of not requiring the user Java
application to provide the report parameters. The disadvantage is that it will display a
dialog on the screen for the user to enter parameters.
Invoking Viewer Bean with Report Parameters
The second approach of invoking the JReport Viewer
Java Bean has two passes: the first pass obtains information about the parameters required
by the report and the second pass submits the required parameters. If the user Java
application has already known the required parameters, the first pass may be
eliminated. In this approach, the Viewer Java Bean will not display any dialog on
the screen. The user Java application retains the full control to the screen. User Java application is responsible for
submitting all the required report parameters.
Pass one:
The user Java application calls the Java bean JRPrepareViewer
with the following properties:
- setServerName ("theServerName")
("204,177,.148,110")
setPort ("thePortNumber") ("8888")
setReportName ("theReportName") ("phaseorder")
setCatName ("theCatalogName")("Tutorial")
setLoadReport ("Yes/No")
JRPrepareViewer returns a vector containing
the information about the report. The method getReportParams ( ) should be called
after calling JRPrepareViewer to obtain
the vector. The size of the vector is the number of the parameters needed by the report,
which can be obtained from the method size ( ). Each element in the vector contains
the type of a parameter, which can be obtained using the method getType ( ). The possible
parameter types are listed below:
- STRING:sParams += "ok,";
- NUMBER:sParams += "123,"
- CURRENCY:
- BOOLEAN:sParams += "true,";
- DATE://sParams +=
"1998-05-01,";
sParams
+= ",";// using default value
- TIME:sParams += "09:30:00p,";
- DATETIME:
Pass two:
In the second pass, the user Java application prepares the
parameters based on the information provided by the vector.Then It calls the Java
bean JRRunViewer with the following properties:
- setServerName ("theServerName")
("204,177,.148,110")
setPort ("thePortNumber")("8888")
setReportName ("theReportName") ("phaseorder")
setCatName ("theCatalogName")("Tutorial")
setLoadReport ("Yes/No")
setParameters ("Parameters")
Note:
Parameters --
The string containing the parameter values, separated by comma (','). If there are more
than one parameters needed in the report, you can pass multiple parameters in the Parameters string. The format
is: paraName1=paraValues1,paraName2=paraValue2 where the paraName1 and paraName2 are the
parameter names and paraValue1 and paraValue2 are the values.e.g. ("204,177,.148,110")
There are also three methods with which the
JReport Viewer Bean will export result file(with the .rst extension) to different file
formats(.htm, .pdf, .txt):
- exportToHtml("HtmlFileName")("report1.html");
- exportToPdf("PdfFileName")("report1.pdf");
- exportToText("TextFileName",
true/false)("report1.txt", true);
The parameters above are explained below:
- HtmlFileName -- The name of the HTML format
result file with the full path.
- PdfFileName -- The name of the PDF format
result file with full path.
TextFileName -- The name of the TEXT
format result file with the full path, "true/false" is to
set whether this text file is a normal text format or a standard data format (the standard
data format text file is the text file that each row of it represents each record in the
report. It can be used as a text datasource to exchange data with other applications)
Also JReport Viewer Bean provides the
methods to add, delete, save and get a new WHERE clause in the SQL query(we will provide
the last three methods in the future). By specifying the name of this new WHERE
clause, users can pick it out from the report catalog easily:
- setNamedWherePortion(queryName) -- Specifies the name of a new WHERE clause
- setWherePortion(SQL) -- Sets
the WHERE clause in SQL
The parameters above are explained below:
- queryName -- The name of the WHERE clause in
SQL query.
- SQL -- the specific WHERE clause.
Return Codes, Printing Reports, and Browsing Pages
Check Return Codes
The first method getStatus ( ) should be
called each time after calling the Viewer Java Beans. The return string
"OK" means the request is accepted otherwise an error message is returned.
Printing Reports
There are three methods provided for printing the report:
- printReport ( )
- printReport (Object printJob, Object pageFormat, boolean
Interactive )
- getPrintStatus ( )
The first method printReport ( ) is called to start
printing. The second method allows you to pass the
print job, page format and interactive operation. After the printing is started, the
method getPrintStatus ( ) can be used to get the print job status. The return values
of getPrintStatus ( ) are:
- "No Print Job"
- "Busy"
- "OK"
- Other error messages if there is an error.
Browsing Report Pages
The following methods are only available to the JReport
Viewer Beans build 50 or later. To find out the build number of your JReport Viewer
Bean, please refer to the readme file in the package. If you cannot
find the readme file, it is highly recommended to
re-download the JReport Viewer Bean package
from our web .
- getCurrentPageNumber ( )
- getTotalPageNumber ( )
These two methods will return the current page number on the
screen and the total number of pages of the current report. These two methods can
be called only after you have called the method JRDirectViewer
or JRRunViewer.
- int gotoPage (int pageNumber)
This method will bring the page specified by the pageNumber on the
screen. If the pageNumber is out of the range of the current report, the first page
(if the pageNumber is less than 0) or the last page will be shown on the screen and the
return value is the current page number.
- firstPage ( )
- prevPage ( )
- nextPage ( )
- lastPage ( )
These four methods will bring the page specified to the
screen. The retune value of these method is a string. The value could be:
- "OK"
- "No more Page"
- other error messages such as "No Report Load".
Show information
The two properties showInfoLevel
and logFile are used for controlling debug, error and other
information display. The following two methods are used to set the values to these
properties:
- setShowInfoLevel (vDebug/vError/vOff)
- setLogFile ("LogFileName")
vDebug, vError and vOff are used for showing
debugging information, error information and turning off debugging and error
information. LogFileName specifies where the debugging, error and other information
will go. LogFileName contains both path and file name information. If no path
is specified, the current path is used. If no file name is specified, the file jreport.log
is used.
Exiting the Bean
There is a method called exit ( )
which should be called when you want to quit from the JReport Viewer beans. This
method will do some cleanup work such as deleting temporary files. It is important
not to forget to call this method!
Illustrative Examples
The example program Sample1.java,
Sample2.java and Sample3.java show
you how to call JReport Viewer Java beans. Please refer to it for details.