4.) API Quick Start

This section contains a series of short exercises designed to illustrate some of the basic features of the Report API.  For more details about any of the features described in this section, please see the Programming Guide portion of the documentation.

4.1.) Set Up Environment

The code, given in this guide, has been created with the following in mind:

Any code given in this guide is also under the <EspressReport installation directory>/help/quickstart/src directory and any java application's class file will be under the <EspressReport installation directory>/help/quickstart/classes directory.  Any HTML files will be under the <EspressReport installation directory>/help/quickstart/html directory.  Similarly, any templates would be under the help/quickstart/templates directory.

The naming convention for the files will be as follows:

Most of the files need not be moved in order to run the exercises.  In the few instances where the files have to be moved and/or modified, instructions will be given.

In order to successfully use the Quick Start API Guide:

The templates (referenced in this guide), which uses a database as a data source, uses the HSQL java database.  For windows users, there are alternate templates available, which use Access.  These templates can be found under the <EspressReport installation directory>/help/quickstart/templates/Access directory.  The templates will again follow the naming convention specified above along with "_Acc" (without the double quotes) before the ".rpt" (without the double quotes) extension.

Depending on the version of the Tomcat server you are using, the servlet context /servlet/ may be commented out.  Please check the <Tomcat installation directory>/conf/web.xml and uncomment the following lines:

and

The block of statements is commented if there is a <!-- at the beginning and a --> at the end.  So if the above blocks are commented out, please uncomment and restart your Tomcat server.

The "classes" (without the double quotes) must also exist in your <Tomcat installation directory>/webapps/ROOT/WEB-INF directory.  If the directory does not exist, please create the directory and restart your Tomcat server.

While running the code, you may see the message "Failed to read espressmanager.cfg. Use default host and port no. 22071." (without the double quotes).  This is not an error message.  This message simply states that espressmanager.cfg was not found and that it would look for and connect to EspressManager running on the same machine, as the code, and using port number 22071.

Please note that while EspressReport API can be used without EspressManager and in other application servers, the code in this guide was designed with EspressManager and Tomcat in mind.  Please refer to the Programming Guide (Deploying w/o EspressManager and Servlets & Java Server Pages) to switch to other configurations.

The code has also been implemented with the idea that everything (i.e., EspressManager, Tomcat server and the client) is on the same machine. The code is single-threaded and set to use the same machine for the client and server for demonstration purposes.  You can generate multi-threaded code using EspressReport API, in a multi-client server environment.  To use in a multi-machine environment, you will have to edit the code (and/or accompanying HTML files).  Please refer to the Programming Guide (Deploying with EspressManager) for further details.

4.2.) Run a Report

The following sections show how to run a pre-existing template (QuickStart42.rpt located in the <EspressReport installation directory>/help/quickstart/templates directory) in an application, applet and servlet.

Each section shows the code to generate the report and any steps necessary to deploy.

4.2.1.) Application

The following code shows how to display an existing report template (in this case QuickStart42.rpt) in an application:

The class file for the above source is located in the <EspressReport installation directory>/help/quickstart/classes directory.

When the class file is run, using the command "java QuickStart421" (without the double quotes), the following report is shown:


Report generated

The main part of the code is in the doQuickStart421 component.  There, a QbReport object called report is created using the QuickStart42.rpt template.  The following constructor is used:

4.2.2.) Applet

The following html (QuickStart422.html in the <EspressReport installation directory>/help/quickstart/html directory) shows how to display an existing report template (in this case QuickStart42.rpt) in an applet:

The same code used for the application (given in the previous section) can be used to show the report in an applet as well.  To deploy the applet, copy

When the html file, using the URL "http://localhost:8080/QuickStart422.html" (without the double quotes), is run, the following report is shown:


Report generated

Please note that the images will not be shown because of a security restriction in the default configuration of the browser.

The main part of the code is in the doQuickStart421 component.  There, a QbReport object called report is created using the QuickStart42.rpt template.  The following constructor is used:

4.2.3.) Servlet

The following code shows how to display an existing report template (in this case QuickStart42.rpt) in an servlet:

To deploy the servlet:

When the servlet, using the URL "http://localhost:8080/servlet/QuickStart423" (without the double quotes), is run, the following report is shown:


Report generated

The main part of the code is in the QuickStart423.  There, a QbReport object called report is created using the QuickStart42.rpt template.  The following constructor is used:

The QbReport object, report, was then exported as a DHTML document to the servlet's response stream using the export method:

4.2.4.) JSP

Besides deploying the report using sevlets, it is also possible to deploy it using JSP.  An example of deploying reports using JSP/Java Bean technology can be found in the "<EspressReport installation directory>/help/examples/jsp/deploy" folder.  The same folder contains a README.txt file that explains how to setup this example on your servlet container.

4.2.5.) Page Viewer

The following piece of code shows how to display the report in an application/applet using Page Viewer instead of Report Viewer.

The class file for the above source is located in the <EspressReport installation directory>/help/quickstart/classes directory.  When the class file is run, using the command "java QuickStart424" (without the double quotes), the following report is shown:


Generated Report

Notice that a QbReport object is not created.  Instead the .rpt file name is passed directly into the component:

In the back-end, the .rpt will be exported to VIEW and PAGE files that will be loaded into the viewer.  When running without EspressManager, the report will first have to be exported in VIEW format.  The resulting VIEW file can then be passed into the Page Viewer component.

The Page Viewer code can also run as an applet. The following html (QuickStart424.html in the <EspressReport installation directory>/help/quickstart/html directory) shows how to display an existing report template (in this case QuickStart42.rpt) in an Page Viewer applet:

The same code used for the application (given in the previous section) can be used to show the report in an applet as well.  To deploy the applet, copy

4.3.) Create a Report Programmatically

The following sections show how to create a report programmatically and apply a template, QuickStart43.rpt (located in the <EspressReport installation directory>/help/quickstart/templates directory) to the report in an application.

Each section shows the code to generate the report and any steps necessary to deploy.

4.3.1.) Map Summary Break ColInfo

The following code shows how to create a Summary Break report programmatically (using QuickStart43.txt as the data source):

The class file for the above source is located in the <EspressReport installation directory>/help/quickstart/classes directory.

When the class file is run, using the command "java QuickStart431" (without the double quotes), the following report is shown:


Report generated

Please note that when the above code is run, the report generated is of default look and feel without any additional formatting.

The main part of the code is in the doQuickStart431 component.  There, a QbReport object called report is created using the specified Column Mapping, the specified Report Type and the specified Data Source.  The following constructor is used:

4.3.2.) Apply Template

You can pass in a different formatting (i.e., different from the default look and feel) by specifying a template during the creation of the QbReport object.

The following code shows how to create a Summary Break report programmatically (using QuickStart43.txt as the data source and QuickStart43.rpt as the template):

The class file for the above source is located in the <EspressReport installation directory>/help/quickstart/classes directory.

When the class file is run, using the command "java QuickStart432" (without the double quotes), the following report is shown:


Report generated

The main part of the code is in the doQuickStart432 component.  There, a QbReport object called report is created using the specified Column Mapping, the specified Report Type, the specified Data Source and the specified Report Template.  The following constructor is used:

4.4.) Modify Data Source of a Report

The following code shows how to modify a report's (and its accompanying sub-report's, drill down's and independent chart's) data source, without having to create a new QbReport object.  The QbReport object (created from QuickStart44.rpt) is opened with backup data (this is so that the database is not hit unnecessarily).  The datasource is then changed to the Access Woodview database.

The class file for the above source is located in the <EspressReport installation directory>/help/quickstart/classes directory.

When the class file is run, using the command "java QuickStart44" (without the double quotes), the following report is shown:


Report generated

The main part of the code is in the doQuickStart44 component.  There, a QbReport object called report is created.  The data source for the report, subreport, drill down and chart (with independent data source) is changed using the following method in the quadbase.reportdesigner.util.IInputData interface:

4.4.1.) Modify Data Source and Query of a Report (with Sub-Report)

The following code shows how to modify a report's (and its accompanying sub-report's) data source, without having to create a new QbReport object.  The QbReport object (created from QuickStart441_Acc.rpt) is opened with backup data (this is so that the database is not hit unnecessarily).  The datasource is then changed from the Access Woodview database to the HSQLDB Woodview database.

The class file for the above source is located in the <EspressReport installation directory>/help/quickstart/classes directory.

When the class file is run, using the command "java QuickStart441" (without the double quotes), the following report is shown:


Report generated

The main part of the code is in the doQuickStart441 component.  There, a QbReport object called report is created.  The data source for the report and the sub-report is changed using the following method in the quadbase.reportdesigner.util.IInputData interface:

The sub-report is obtained by getting a handle to the cell containing the sub-report and then calling the sub-report.  The sub-report is also opened using its backup data.  This is done by:

Please note that if you wish to change the data source from the HSQLDB Woodview database to the Access Woodview database , you will need to change the following lines of code from:

to

4.4.2.) Modify Data Source and Query of a Parameterized Report (with Parameterized SubReport)

The following code shows how to modify a report's and its accompanying sub-report's parameterized data sources, without having to create a new QbReport object.  The QbReport object (created from QuickStart442_Acc.rpt) is opened with backup data (this is so that the database is not hit unnecessarily).  The datasource is then changed from the Access Woodview database to the HSQLDB Woodview database..

The class file for the above source is located in the <EspressReport installation directory>/help/quickstart/classes directory.

When the class file is run, using the command "java QuickStart442" (without the double quotes), the following report is shown:


Report generated

The main part of the code is in the doQuickStart442 component.  There, a QbReport object called report is created.  The data source for the report and the sub-report is changed using the following method in the quadbase.reportdesigner.util.IInputData interface:

However, because both reports use a parameterized report, a separate class file (QueryFileInfo) was created which used the interface quadbase.reportdesigner.util.IQueryFileInfo to pass in the database connection information and the parameter properties information as well. An object of the class QueryFileInfo (with the required information) is then passed as the argument for the setDatabaseInfo() method.

The parameter information is obtained by getting the database connection information (by called the getDatabaseInfo() method), casting it to IQueryFileInfo and using the following method in IQueryFileInfo:

The above method call returns the complete parameter properties information for all the parameters defined in the report.

The code also passes in the values for the parameters directly into the report, rather than have the user be prompted to specify the values. The values are entered by going to each parameter and specifying them before passing them to the report object. The following method, in IQueryInParam, is used to specify the value:

The sub-report is obtained by getting a handle to the cell containing the sub-report and then calling the sub-report.  The sub-report is also opened using its backup data.  This is done by:

Please note that if you wish to change the data source from the HSQLDB Woodview database to the Access Woodview database , you will need to change the following lines of code from:

to

You can also pass in a java.sql.Connection object, instead of passing in the URL, driver, userid and password of the database. For example, if you wish to pass in a Connection object, conn, you would need to change the following lines of code:

to

4.5.) Modify Report Elements

The following code shows how to modify certain elements of the report programmatically.  The QbReport object is created from QuickStart45.rpt .  When the code is run, the original report is first shown and when the "Change" (without the double quotes) button (located at the bottom) is clicked, some of the report elements will change.

The class file for the above source is located in the <EspressReport installation directory>/help/quickstart/classes directory.

When the class file is run, using the command "java QuickStart45" (without the double quotes), the following report is shown:


Report generated

When the "Change" (without double quotes") button is clicked, the following report is then shown:


Report generated after Change button is clicked

The main part of the code is in the action class.  Report properties such as Dual Color are turned on and a formula, a label and a title are added to the QbReport object.

Dual colors are set using the following code.  The dual color property is set for each table column and the alternate background color, font color and font are specified.

Adding a title to the Report Header is done using the following code.  The ReportCell object is first created, the title text then set and the ReportCell properties such as height, width, x-position and y-position are specified before adding it to the Report Header section.

A formula and a label are specified likewise.  A ReportCell object is first created, the formula or label set and the ReportCell properties specified before adding the newly created formula or label ReportCell object to the appropriate section.

4.6.) Parameterized Reports

The following sections show how to run a pre-existing template (QuickStart46.rpt located in the <EspressReport installation directory>/help/quickstart/templates directory) which uses a parameterized query as the data source.

Each section shows the code to generate the report and any steps necessary to deploy.

4.6.1.) Pass in Parameter values

The following code shows how to display an existing report template (in this case QuickStart46.rpt), which uses a parameterized query, in an application.  The parameter values are passed when creating the report.

The class file for the above source is located in the <EspressReport installation directory>/help/quickstart/classes directory.

When the class file is run, using the command "java QuickStart461" (without the double quotes), the following report is shown:


Report generated

The main part of the code is in the createReport component.  There, a QbReport object called report is created using the QuickStart46.rpt template.  The parameter values are passed using the following constructor:

Both query parameter and formula parameter values are declared in the same order as the parameters were defined.  Query parameters which take in multiple values are declared as a Vector object which then contains the different multiple values.

Please note that if you are using the Access templates, you will need to change the following lines of code from:

to

4.6.2.) Pass in Parameter values using getAllParameters

In addition to the above, you can also pass in parameter values using the getAllParameters method in QbReport. The following code shows how to display an existing report template (in this case QuickStart46.rpt), which uses a parameterized query, in an application.  The report is opened with backup data (to avoid the initial hit to the database) and the parameter values are set.  The report is then refreshed with the data.

The class file for the above source is located in the <EspressReport installation directory>/help/quickstart/classes directory.

When the class file is run, using the command "java QuickStart462" (without the double quotes), the following report is shown:


Report generated

The main part of the code is in the createReport component.  There, a QbReport object called report is created using the QuickStart46.rpt template.  The parameter values are passed using the following interface in QbReport:

Both query parameter and formula parameter values are declared in the same order as the parameters were defined.  Query parameters which take in multiple values are declared as a Vector object which then contains the different multiple values.

Please note that if you are using the Access templates, you will need to change the following lines of code from:

to

4.6.3.) Use getParameterPage() and ParamReportGeneratorServlet

The following code shows how to display an existing report template (in this case QuickStart46.rpt), which uses a parameterized query, in a servlet.  The servlet creates the QbReport object by opening the template using back-up data.  A HTML page is then streamed asking for the parameter values.  These values are passed to another servlet (the ParamReportGeneratorServlet servlet) and the QbReport object is created, from the given template,  with the specified parameter values.

To deploy the servlet:

When the servlet, using the URL "http://localhost:8080/servlet/QuickStart463" (without the double quotes), is run, the following HTML page is shown:


HTML prompt page generated

Depending on what parameter values you pass in, a report similar to the following will be shown:


Report generated after passing in parameter values

The main part of the code is in the QuickStart463.  There, a QbReport object called report is created using the QuickStart46.rpt template.  The dynamic export is then called to use the ParamReportGeneratorServlet (provided with EspressReport) using the following lines:

The HTML file asking for the parameter values is then generated and passed to the OutputStream.

For more information about the parameter page writer classes go to section 3.5.7.11.3. of the Programming Guide.  For an example of generating parameter page using CssHtmlParameterPageWriter which utilize CSS, go to <EspressReport>/help/examples/servlet/CssParamReport and follow the javadoc instructions.

4.7.) Deploy/Export Drill Down

The following code shows how to display an existing report template (in this case QuickStart47.rpt), which is a drill down report, in a servlet.  The servlet creates the QbReport object by opening the template. A DHTML page, showing the contents of the first level,  is then streamed.  The next level report is obtained by clicking on the links.  These links point to the DrillDownReportServlet.  The values of the link (clicked on) are passed to the DrillDownReportServlet and the next level report (based on those values) is then generated and then streamed to the client.

To deploy the servlet:

When the servlet, using the URL "http://localhost:8080/servlet/QuickStart47" (without the double quotes), is run, the following HTML page is shown:


Report generated

Depending on what link you click, a report similar to the following will be shown:


Report generated after clicking on a link

Again, depending on what link you click, a report similar to the following will be shown:


Report generated after clicking on a link

The main part of the code is in the QuickStart47.  There, a QbReport object called report is created using the QuickStart47.rpt template.  The dynamic export is then called to use the DrillDownReportServlet (provided with EspressReport) using the following lines:

The DHTML file for the top level report is then generated and passed to the OutputStream.

4.8.) More on Servlets

The following sections show how to run a pre-existing template (QuickStart48.rpt located in the <EspressReport installation directory>/help/quickstart/templates directory) in a servlet and have the generated report saved to a file or streamed to the client browser.

Each section shows the code to generate the report and any steps necessary to deploy.

4.8.1.) Write to File (PDF)

The following code shows how to display an existing report template (in this case QuickStart48.rpt) in an servlet.  The servlet then exports the report to a PDF file.

To deploy the servlet:

When the servlet, using the URL "http://localhost:8080/servlet/QuickStart481" (without the double quotes), is run, the report is generated to your <EspressReport installation directory>/generatedReport.pdf and the following message is shown:


Message shown after report is generated

The main part of the code is in the QuickStart481.  There, a QbReport object called report is created using the QuickStart48.rpt template.  The following constructor is used.  The QbReport object, report, was then exported as a PDF using the export method:

4.8.2.) Stream Chart (DHTML)

The following code shows how to display an existing report template (in this case QuickStart48.rpt) in an servlet.  The servlet then streams the report (along with the chart) as a DTHML document to the client.

To deploy the servlet:

When the servlet, using the URL "http://localhost:8080/servlet/QuickStart482" (without the double quotes), is run, the following HTML page is shown:


Report generated

The main part of the code is in the QuickStart482.  There, a QbReport object called report is created using the QuickStart48.rpt template.  The dynamic export is then called to use the RPTImageGenerator (provided with EspressReport) using the following lines:

The DHTML file for the top level report is then generated and passed to the OutputStream.

4.9.) Launch Report Designer

The following code shows how to launch the Report Designer (in default mode) via the Report API:

The class file for the above source is located in the <EspressReport installation directory>/help/quickstart/classes directory.

To run the class file, move QuickStart49.class to the <EspressReport Installation directory> and use the command "java QuickStart49" (without the double quotes),.

The main part of the code is in the QuickStart49.  There, a QbReportDesigner object called designer is created and shown: