ERP 2.50:Developers Guide/How to develop a report
Report writing is an essential part of application development. Critical company decisions are often based on enterprise reports so they must capture, filter and present the right information. In this How To you will be introduced with the JasperReports library and iReport graphical design tool. Combined they to offer a powerful and flexible approach to building enterprise reports. JasperReports is an Open Source product in it's own right provided by Jaspersoft. You save JasperReports to an intermediate format; an xml file with the suffix .jrxml. This gives you the ability to export the report to several different formats like html, pdf, excel and csv. iReport is an open source graphical report building tool that you can use to design and develop JasperReports.
Viewed from a high level developing a simple report is a 3 step process:
- Write the JasperReport Template .jrxml file using iReport.
- Embed the report .jrxml file into Openbravo ERP.
- Compile the application for the report integration to take effect.
A simple example is available as a module: Simple Report
Create a report that shows the id, name and description of all the different types of grapes recorded for making wine. Ensure there are no repeated records.
After you have downloaded and installed iReport you need to provide iReport with a JDBC driver and connection information for your database.
You need to add the JDBC driver to your iReport's CLASSPATH. This can be done by selecting: Options || Classpath
Add a JDBC driver to your CLASSPATH by selecting the appropriate JAR file according to the flavor of database you are using. Select the Save Classpath button to make you change permanent.
To set up your database connection navigate to Data || Connections / Datasources and select New.
Select the appropriate JDBC driver and fill in the relevant information about your database: JDBC URL, Server Address, Database, Username and Password. Give an alias for the Name field
Select the Test button. If the response is a pop-up window indicating success.
File || Save your database connection properties.
Write a JasperReport template
iReport contains a wizard that allows you to rapidly create a database report. The wizard allows you to use one of several predefined templates that are includes with iReport.
Select the Report Wizard menu item from the File menu File || Report Wizard. Enter the database query that will retrieve report data from the database on the resulting pop-up window. The complete query could be something like this:
SELECT DISTINCT m_product_id, name, description FROM m_product WHERE name LIKE 'WG'
Select the button labeled Next
The window shows all the query columns that could be selected as report fields
For this example you want all possible fields represented so select the >> button
Select Next and move on to the Grouping dialog box (report groups allow you to divide report data when report expressions change). In this example you will not group the report data.
Select Next and move on to the report layout dialog box. You have a couple of choices to make here. You can choose to make a Tabular or Columnar report and you have to select a template style. There are a number of predefined templates to choose from which you can adapt or make your own.
For this exercise choose classic.xml
Select Next and select Finish to generate the reports JRXML template. Save your report File || Save as m_product.jrxml. This is now your report template. Ensure you save your report into the Openbravo ERP report directory .../org/openbravo/erpCommon/ad_reports/
Compile and execute your report in one step by selecting Build || Execute report (using active connection)
You can now see your generated report.
You will want to make a few simple changes to make the report more specific. Let's start with the Title:
Right click and then select Properties || Static Text
Change the title to what you think appropriate and Save your report from the main menu.
The prompts of your report are 'what you see is what you get' WISIWIG so you can change them directly in the iReport interface. In the example they are changed to Product, Grape and Description. Now when you select Build || Execute (with active connection) your report will look more presentable.
This is a simple and perhaps crude example of a report. However, it will get you started! You can use iReport to do any modification you need to the database query and the report layout. To become a more proficient developer with iReport we recommend you read the many articles and do the tutorial available online. In the remainder of this exercise we will focus on 'How to' embed a JasperReport into an Openbravo ERP application.
Embed a JasperReport in Openbravo ERP
There are 2 ways to add a new report to the application:
- By creating a servlet that builds the filter window and generates (compiles) the output or developing only the Jasper Report template
- Or using a generic servlet to enter parameters and compile the output.
Adding your report to Openbravo
Conduct all the following work in your own development module Login into Openbravo as System Administrator
Go to Application Dictionary || Report and Process
Create a new records and enter the new Report data (for more information see the AD_Process table description):
- Search Key: RPT_MPRODUCT
- Name: Product List Report
- Mark the Jasper Report checkbox (A textbox will appear with JR Template Name)
- JR Template Name: @basedesign@/org/openbravo/erpCommon/ad_reports/m_product.jrxml
NOTE: The @basedesign@ will be replaced in runtime with: The JR Template name is the path were the .jrxml is located. I.E. your_tomcat_context/src-loc/design
If you are using the base language, or if you are using a different language, will append the folder language at the end, e.g. In Spanish will be: your_tomcat_context/src-loc/es_ES
Add the report to the Menu. Go to General Setup || Application || Menu
Create an new record and enter the following data (for more information see the AD_Menu table description):
- Name: Product List Report
- Action: Process
- Process: Product List Report
Save your work
Compiling the application
Finally, you need to recompile your application for the report call to work. If manually compiling Openbravo, use the ant compile.development -Dtab='XX'. The compile process compiles the .jrxml template to a .jasper executable and copies it to your tomcat context. The 'XX' in the command line tells the WAD not to touch code from any generated window. This will shorten your compilation time.
For more information on Openbravo build tasks, visit this page.
NOTE: Once the compilation has finished, restart Apache Tomcat server. See more on Build Tasks.
You need a Jasper Report template and this template must be registered in the Application Dictionary, after that follow this steps to test your report:
- Login as with a user that has Openbravo Admin role
- You should see a new item 'Product List Report' at the end.
Select your new report. A new pop-up will window appear. Select OK.
The report should be displayed.
NOTE: An empty report may be displayed. Occasionally Jasper Reports has errors with the layout. If you encounter this problem then open your report template (.jrxml) with iReport, and make sure that no textfields and labels are overlapping each other.
- JasperReports FAQ on proper layout design: http://jasperforge.org/uploads/publish/jasperreportswebsite/trunk/faq.html#FAQ9
ERP 2.50:Developers Guide/How to change an existing report | ERP 2.50:Developers Guide/How To Create a Manual Window