jasper report « Intelligrape Groovy & Grails Blogs

Posts Tagged ‘ jasper report ’

Passing Optional Parameters in jasper reports

Posted by Sachin on September 13th, 2010

Hi Guys,

In one of my projects, While working with jasper reports, one of the requirement I faced was of passing optional parameter to the sql query in jasperĀ  report. For eg. I had to write a query to find all new customers which were added between two given dates and if an optional status value of the customer is passed the report should should be further refined to include only those customer which were added and had the same status value as passed, If the status is not provided it should be ignored in SQL query in jasper report. So, in this case the status was an optional parameter.

The solution I found was pretty elegant. On searching over the net, I found that parameters with an ! (exclamation) mark are compiled before the query is executed. That is,

$P!{status}

will be compiled before the query is executed. So, I passed the status from my code in to the jasper report. Now, to take care of the condition that it could be optional, I added a new parameter inside the jasper report, say “status_value”. such that, status_value had a default value expression (DFE) :

 
($P{status}=="" ? " and customer.status=' "+ $P{status} + " ' " )

Now in the SQL query in jasper I wrote

SELECT * FROM customer WHERE customer.start_date=$P{startDate} AND customer.end_date=$P{endDate} 
$P!{status_value}

what it does is that status_value is compiled before the execution of query. So the condition in the DFE of status_value checks whether we a receive a status from outside or not, If we do the condition will be appended in the query. Other wise it won’t be appended and we are done. :) .

With Regards

Sachin Anand
sachin@intelligrape.com

  • Share/Bookmark
Posted in Java tools

My experience of using jasper reports with grails application

Posted by Amit Jain on February 25th, 2010

In my project we needed to generate certain pdf reports. Then while exploring the options available, I encountered a PDF plugin, which renders gsp page and returns the PDF report. It looked good for the simple reports but may not be a good idea if the report are complex. Then with the help of one of my collegue, we decided to rather generate jasper reports. And as expected, there exists a plugin called ‘jasper’ to integrate jasper reports with a grails application, which made my job really easy.

Things looked good, now we were only left with an IDE to rapidly generate jasper reports and then we finally found a tool called IReport. We could make version 2.0.5 working flawlessly with the jasper plugin.

As our reports became more complex and our understanding about jasper reports grew, even things which looked big like required page breaks on certain conditions etc. became really easy to handle with the IReport tool. We could generate a complex jasper report in 3 days time which includes the time taken to get accustomed to iReport and jasper report and its integration. I will soon write more about jasper reports and few changes we made to the jasper plugin to suit our requirements like directly sending emails with jasper report as an attachment…

Cheers!
~~Amit Jain~~
amit@intelligrape.com
IntelliGrape Softwares

http://intelligrape.com

  • Share/Bookmark
Posted in Grails, Java tools