As we mentioned before JasperReports and other reporting tools were introduced to facilitate the reporting development cycle, but for end-developers working with declarative files –xml- is usually considered as two-sided coin. Although they are more flexible and popular, the overhead of designing report layout entirely by xml tags deserve reconsidering about using reporting tool.

Thanks to open-source community we don't need to design our report layout through declarative files by ourselves anymore, a visual design tools were introduced to the open-source reporting engines. iReport and JasperAssistant are a good examples for JasperReports visual designer they had completed the missing block in constructing robust java reporting tools. As example of using design tools we'll explore iReport and continue our tutorial application with it.

First you need to download the executable file from here, there are many versions you can download anyone –personally I use v2.0.4–. Install the executable file, start iReport and let's work in the previous article sample report:

1- Open File menu -> New Document

2- After creating the empty report we need to set our data-source connection to test the report in the design time, to set the connection go to Data menu -->Connection/Data Sources, click on 'New' button, choose your datasource type from the list –in my case its 'Database JDBC connection'-, and fill the connection parameters with your arguments.

3- Now we have an active connection to the data-source, next step is to define the report query we'll use to fetch the report results. From Data menu --> choose Report Query, and then enter the report query as shown:

You can notice the fields marked by red circle, they are generated fields from your SQL query projection that will be used as report result.

4- Now let's design our report layout, at the column header band we'll add to the report header as a Static Text element from the tools –the one marked by red circle-, then at the details band we'll add the dynamic data-source fields which are resulted from query projection, they can be added from the left panel 'Document structure' tree 'Fields' node –the one marked with blue circle-.

You may notice the three elements in the Document Structure panel: Parameters, Fields and Variables, those are the heart of the JaperReports they control every thing regarding the report behavior and results, lets clarify them in more detail:

Parameters: contain the arguments passed from the application code in run-time, we use it for building run-time search criteria and they are passed from code in the form of HashMap, written in xml by the following format: $P{REPORT_DATE}.

Fields: the dynamic result of the fields retrieved from data-source, they represent the report result, written in xml by the following format: $F{ITEM_NAME}.

Variables: they are two types, JasperReports built-in variables like (PAGE_NUMBER, PAGE_COUNT, COLUMN_COUNT… etc.), and there are the user defined variables which are used to perform more complicated operations on report results (calculations, summarizing, condition branching…etc.), they are written in xml by the following format: $V{COLUMN_NUMBER}.

5- Its time to test our report, iReport enables report designer to test his work on live data-source connection and different exporting format. From Build menu --> choose the exporting format you want, I'll choose JRViewer Preview, then choose 'Execute (with active connection)'. You should get the report result in the JRViewer as shown:

That was the first part of our lesson today; at the next part we'll modify our sample application to accept run-time search criteria, we will modify both the design file and the application code to be able to pass parameter contains the dynamic items amount limit we want search for, as shown in the following steps:

1- In the design file we'll add a new parameter and append it in the query where segment, right click on the Parameter node in the 'Document Structure' panel and choose 'add'-->'Parameter', enter the parameter name, say "itemAmount" and choose type class (Integer), then update the query statement by replacing the where condition item_amount <= 100with item_amount <= $P{itemAmount}.

2- At the code we need to update the parameter hashmap by adding an entry to it contains [key: parameter name, value: parameter value] as following:

// connection is the data source we used to fetch the data fromConnection connection = establishConnection();// jasperParameter is a Hashmap contains the parameters// passed from application to the jrxml layoutHashMap jasperParameter = new HashMap();

9 comments:

Yes, JasperSoft has released the first version of iReport for NetBeans on 18-02-2008.you can download it from:http://www.jasperforge.org/jaspersoft/opensource/business_intelligence/ireport/page.php?name=iReportNB

there is also a plug-in for Eclipse - although i know ur a netbeans fan :)-, u can get it from:http://www.jasperforge.org/index.php?option=com_mtree&task=viewlink&link_id=82&Itemid=91

Hi HossamASAK,Is it possible to create xml report template using any web application i.e I want to integrate ireport UI in my web application so that a user can set up report template online without ireport installed in his machine.I am new to JasperReport and iReport ,I might have asked wrong question.Your reply will be appreciated.Thanks

@Shafiullah,W 3alekom Asalam.JasperSoft has introduced a new product "JasperServer" to report management portal through interactive reporting server, although this won't replace iReport totally your client's still need an iReport designer in order to create their templates, which is reasonable if we consider the popularity and ease-of-use of iReport as report template creator.

But If you need that ad-hoc feature badly u'll need to implement your own template designer on top of JRXML standards just like iReport or any other designers.

For more iformation about JasperServer:http://www.jaspersoft.com/jasperserverhttp://www.jaspersoft.com/sites/default/files/downloads/JasperServer-ds.pdfhttp://jasperforge.org/espdocs/docsbrowse.php?id=74&type=docs&group_id=112&fid=304

About us

We are a group of bloggers from Faculty of Computers & Information, Helwan University. We have a passion for technology, enjoy programming and we enjoy blogging. This blog is the result of combining those skills and the desire to share our knowledge with others.