jasper talk
TRANSCRIPT
Grails Reporting
Getting the most out of JasperReports and DynamicJasper in Grails
by Keith Cochran
80%
20%
Jasper Dynamic Jasper
1Wednesday, May 13, 2009
Where to get this
• www.cochran-software.com
• downloads
• Contains presentation and sample Grails app
2Wednesday, May 13, 2009
Objectives
• Today we’ll learn about
• Jasper Reports
• Creating reports with iReport
• Passing parameters to reports
• Using Dynamic Jasper
3Wednesday, May 13, 2009
What is it?
• Jasper Reports is
• Open-source reporting tool for Java
• Flexible Report Layout
• Supply data in multiple ways
• Variety of export formats
4Wednesday, May 13, 2009
System Setup
5Wednesday, May 13, 2009
For Demo
• JasperReports
• DynamicJasper
• iReport
• Example Grails App: DrinkMaster
6Wednesday, May 13, 2009
Jasper Reports
• Open Source Java Reporting Library
• grails install-plugin jasper
• Current version: 0.9.5
• Can export reports in plain text, Excel, PDF, RTF, HTML, XML, and CSV.
• Can use either .jrxml or .jasper file
7Wednesday, May 13, 2009
Dynamic Jasper
• Create reports in a config file instead of .jrxml or .jasper format
• grails install-plugin dynamic-jasper
• Current version: 0.5
8Wednesday, May 13, 2009
iReport
• Download iReport 3.5.0 for your platform
• Mac: http://mac.softpedia.com/get/Developer-Tools/iReport.shtml
• Others: http://sourceforge.net/projects/ireport/
9Wednesday, May 13, 2009
Using iReport
10Wednesday, May 13, 2009
3 Steps to Create Reports
• Set up and use a data connection
• Use report wizard to create a first cut
• Build, Run, Tweak, Retest cycle
11Wednesday, May 13, 2009
How To Setup Data Connections12Wednesday, May 13, 2009
Use Report Wizard
• File -> New -> Report Wizard
13Wednesday, May 13, 2009
Parameters
• Fill in the report name and hit “Next”
• Make sure you select the correct data source
• Select “design query” to open the Query Designer.
14Wednesday, May 13, 2009
Query Designer
• Drag a table to right
• Select fields (Order counts!)
• Right-click fields for options
15Wednesday, May 13, 2009
Where Clause• Right click on the where clause to add a condition
• Example: Enter “drink.id” in the top window
• Use “=”, enter “ingredient.drink_id” in the bottom
16Wednesday, May 13, 2009
Grouping and Ordering
• Under “Select”
• Right click a parameter
• Select either
• “add to group-by” for grouping
• “add to order-by” for ordering
17Wednesday, May 13, 2009
What the Final Query Looks Like...
SELECT
drink.`name` AS drink_name,
ingredient.`description` AS ingredient_description
FROM
`drink` drink,
`ingredient` ingredient
WHERE
drink.id = ingredient.drink_id
GROUP BY
ingredient_description
ORDER BY
drink.`name` ASC
18Wednesday, May 13, 2009
Fields
• Select the fields to display
• Use arrow keys to move them to the right.
19Wednesday, May 13, 2009
Group By
• Select the column you want to group the report by.
• This can be different than the Query.
• This allows grouping on the report.
20Wednesday, May 13, 2009
Select a layout
• Pick either a columnar or tabular layout
• Hit next
• Select “finish” on the last screen
21Wednesday, May 13, 2009
Edit and Run
• Edit the report look in the designer if desired
• Save the report (saves as .jrxml format)
• Select “Preview” to compile and view the results
• After compile, .jasper file is created and saved as well
22Wednesday, May 13, 2009
Jasper Format
• When you create a new report, it’s in jrxml format.
• After compiling it, there is a .jasper format
• The compiled .jasper format runs faster than .jrxml.
• .jrxml files need to be compiled eventually for use by Jasper.
23Wednesday, May 13, 2009
DEMO: Upload Report and Run in Grails
• Show a Demo of how to write the controller so you can upload the report.
24Wednesday, May 13, 2009
Passing Parameters to Reports
25Wednesday, May 13, 2009
Modify the Report
• Return to the report in iReport
• Right-click on “Parameters” in the report inspector and add a parameter
• Name the parameter something meaningful (i.e. “nameofingredient”) and use the appropriate data type (java.lang.String)
• I use String type for Dates as well!
26Wednesday, May 13, 2009
Modify the Query
• Open the query in the report wizard again and add the following to the “where” clause:
• where ingredient.name like $P{nameofingredient}
• The parameter is case sensitive!
27Wednesday, May 13, 2009
Passing Parameters from Grails
• In your Grails app: use hidden tags in the form when calling Jasper.
<g:jasperReport ...>
<input type=hidden name=blah value=blah/>
</g:jasperReport>
28Wednesday, May 13, 2009
Grails Domains
• For the demo, I up a ReportParameter domain in Grails to hold report parametersclass ReportParameter { Report report String name String dataType String value Date dateValue}
29Wednesday, May 13, 2009
In the GSP: Loop thru Parameters...
<g:jasperReport jasper="${report.id + '/' + report?.fileName}" format="${report?.formats()}" name=" " delimiter=" "> <g:each in="${report?.reportParameters}" status="i" var="parameter"> <g:if test="${parameter?.dataType == 'Date'}"> <input type="hidden" name="${parameter?.name}" value="<g:formatDate format="yyyy-MM-dd" date="${parameter?.dateValue}"/>"> </g:if> <g:else> <input type="hidden" name="${parameter?.name}" value="${parameter?.value}"> </g:else> </g:each></g:jasperReport>
Important! Dates should be passed as strings.
30Wednesday, May 13, 2009
DEMO
• Show code on how to include parameters in call to Jasper.
31Wednesday, May 13, 2009
Dynamic Jasper
0
25
50
75
100
2007
2008
2009
2010
Drink Price ($) Sales ($1000s)
32Wednesday, May 13, 2009
What is it?
• Create a simple report in a config file instead of using iReport
• Version 0.5 as of this demo
• Use this to create dynamic reports that don’t need the complexity of iReport
33Wednesday, May 13, 2009
Ways to use DJ
• Quickly use it in a URL
• Entity Based Configuration
• Named Reports Configuration
34Wednesday, May 13, 2009
URL Method
• Add this to your domain class you want to report on:
• def static reportable = [columns: [‘name’, etc.]]
• Append to URL: djReport/?entity=myEntity
• Report of all instances of this entity
35Wednesday, May 13, 2009
Parameters
• entity: the domain class to report on
• reportColumns: list of columns to use (overrides columns listed in domain)
• reportFormat: sets the output type
36Wednesday, May 13, 2009
Tweak Report Style
Define the following in DynamicJasperConfig.groovy
• useFullPageWidth: defines if the report will use the full page width• page: page size and orientation• intPattern: the pattern to use with integer numbers• floatPattern: the pattern to use with floating point numbers• datePattern: the pattern to use with dates• titleStyle: the style to use for the report title• subtitleStyle: the style to use for the report subtitle• headerStyle: the style to use for the columns header• detailStyle: the style to use for the data cells
37Wednesday, May 13, 2009
Entity Based Reports
• Define report parameters inside the domain class
• Use the static reportable
• You’ve already seen “columns” defined
• You can also define title, dataSource, grouping, etc.
• See Plugin page for more config options
38Wednesday, May 13, 2009
Named Reports Configuration
• In the conf directory, make a file “DynamicJasperConfig.groovy”
• Inside that config file, format is:
dynamicJasper {someReport {
<parameters go here>}
}
39Wednesday, May 13, 2009
Dynamic Jasper Config File
• This file is loaded at run-time, and changes are not picked up while running.
• Provide a report name and a closure enclosing the report parameters
40Wednesday, May 13, 2009
• Parameters
• Entity: the table to report on
• Title: title of the report
• Columns: array of columns to include
• Patterns: use for date formats, etc.
• ColumnTitles: change the display of columns
• FileName: the name of the file to create
• GroupColumn: column to group by, if needed
• GroupFooterColumn: ??
• GroupOperation: one of SUM, ???
41Wednesday, May 13, 2009
• Final Parameter...
• dataSource: This is where you can modify the query and gain complexity
• Contains session and parameter objects so you can obtain info from the session and pass in variables
• Return data set out of the closure
42Wednesday, May 13, 2009
• On the URL, these are the parameters to pass in:
• report: the name of the report closure
• reportFormat: PDF, XML, etc. PDF is the default
43Wednesday, May 13, 2009
• Named Report Building and Demo.
Demo
44Wednesday, May 13, 2009
Summary
• Today we learned about
• iReport to create reports
• Jasper Reports and passing parameters
• Dynamic Jasper for quick reports
45Wednesday, May 13, 2009
0
25
50
75
100
Less
More
Even More
Off the Chart
Questions?Question Obscurity Answer Hilarity
46Wednesday, May 13, 2009