Sunday, September 25, 2005

JHeadstart with Jasper Reports

I am currently developing an application using JHeadstart and JasperReports. I am able to leverage the powers of JHS to generate the parameter forms for me. Using JHS makes it conventient as drop downs, LOV, etc can all be used in the parameter forms. Following hopefully will help anyone who is trying to do the same.

Writing the Reports

We used iReport to create the reports. This tool provides a nice GUI for generating the report files. When creating parameters for your reports, you should use the same naming convention as used by Oracle ADF. As an example, if you have a database column with the name of EMPLOYEE_NAME, ADF would generate the name EmployeeName. Thus, you should name your paramers in Jasper the same way. This will make the ADF components you developed consistent and making passing the parameters to Jasper considerably easier. When you create parameters in Jasper, you need to specify the datatypes. I went ahead and declared all of my dates as String and then in the SQL query I convert them back to a date. This will ensure that you use any indexes that might exist on date fields. As an example:

AND BEGIN_DATE = TO_DATE(${BeginDate}, 'mm-dd-yyyy')

Passing Parameters to Jasper

When running a Jasper Report that requires parameters, Jasper requires the parameters to be placed into a HashMap. I wanted to create a general method for putting the required parameters into the required HashMap format. Knowing that I was going to use View Objects (VO) for the fields on parameter forms, I went ahead and created a generic VO (GeneralReportView) that all parameter form VOs would extend. I added custom code in the VO to rip through all of the attributes and put them into a HashMap thus allowing me to pass them to Jasper. I only need to add one method. Following is the method I added:


Creating View Objects

For each parameter form I am using a VO for all of the necessary parameters. When creating the VO, I am selectings "Rows Populated Programmatically..." and specifying the VO extends my base VO I describe above (GeneralReportView). When creating the attributes, make sure you use the same naming convention as you did when creating the reports. Also, make sure you are using the correct data types. Making sure your names and data type are correct will ensure that when passing them to Jasper as a HashMap they will be resolved correctly and the report will be able to run. The only option I have specified for an attirbute is "Updateable=Always". None of the other options are checked.

Application Flow

When creating an application you must have a flow. JHS will generate a flow, as long as it is data driven such as through parent-child relationships. I knew when running reports, the user would be taking three steps most of time. These steps are:

1) Select the report
2) Enter parameters
3) View Report

For the most part, when JHS generates a page and you are executing an action on that page, after the action completes you are taken back to the same page. You are not "navigated" to the next one. I had questioned if JHS was going to help but then this blog came to the rescue. What that blog showed me was that using custom templates you could control this. So, I went ahead and modified these same templates to use this wizard type interface. I now use these mofified tempates to generate a page to select the report, enter the parameters, and then finally view the generated report.

There is still some logic that has to be built into the three steps above. Such as, after step 1 the decision needs to be made "what parameter form should be displayed?". Then, after step 2 the decision needs to be made "what report should be generated?". I simply created struts actions to handle my application flow.

Gererate the Paramater Forms

For each unique parameter form, you will need to create a different ViewObject using the steps described above. If you have reports that use the same parameters, you can use the same VO, you just need to generate seperate pages. When creating the VOs, make sure to extend the base VO (GeneralReportView) and then add to your application module so that JHS can see it. In JHS Application Struture file, create a new Base Group based on your VO and using any necessary templates you will be using. Run the application generator and your parameters forms will be created.

Screenshots


Report Manager - User may view the reports they have executed previously or
select "Run Report" to run a new one.



Select Report - User selects the report they want to run.



Enter Parameters - User enters the desired parameters for the report.



View Report - User can see the report that she ran and click on the link
to open it.


I don't have the user interface 100% complete but the functionality is there. Using JHS & Jasper has made it very easy to intergrate reports into our application.

1 comment:

Prabha said...

how can i pass values from webtop to jasper engine?The webtop has to get values from the user and the jasper report has to produce the result with respect to the user input.Kindly help.