jasper talk

46
Grails Reporting Getting the most out of JasperReports and DynamicJasper in Grails by Keith Cochran 80% 20% Jasper Dynamic Jasper 1 Wednesday, May 13, 2009

Upload: simbarashe-musharu

Post on 18-Apr-2015

63 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Jasper Talk

Grails Reporting

Getting the most out of JasperReports and DynamicJasper in Grails

by Keith Cochran

80%

20%

Jasper Dynamic Jasper

1Wednesday, May 13, 2009

Page 2: Jasper Talk

Where to get this

• www.cochran-software.com

• downloads

• Contains presentation and sample Grails app

2Wednesday, May 13, 2009

Page 3: Jasper Talk

Objectives

• Today we’ll learn about

• Jasper Reports

• Creating reports with iReport

• Passing parameters to reports

• Using Dynamic Jasper

3Wednesday, May 13, 2009

Page 4: Jasper Talk

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

Page 5: Jasper Talk

System Setup

5Wednesday, May 13, 2009

Page 6: Jasper Talk

For Demo

• JasperReports

• DynamicJasper

• iReport

• Example Grails App: DrinkMaster

6Wednesday, May 13, 2009

Page 7: Jasper Talk

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

Page 8: Jasper Talk

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

Page 10: Jasper Talk

Using iReport

10Wednesday, May 13, 2009

Page 11: Jasper Talk

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

Page 12: Jasper Talk

How To Setup Data Connections12Wednesday, May 13, 2009

Page 13: Jasper Talk

Use Report Wizard

• File -> New -> Report Wizard

13Wednesday, May 13, 2009

Page 14: Jasper Talk

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

Page 15: Jasper Talk

Query Designer

• Drag a table to right

• Select fields (Order counts!)

• Right-click fields for options

15Wednesday, May 13, 2009

Page 16: Jasper Talk

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

Page 17: Jasper Talk

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

Page 18: Jasper Talk

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

Page 19: Jasper Talk

Fields

• Select the fields to display

• Use arrow keys to move them to the right.

19Wednesday, May 13, 2009

Page 20: Jasper Talk

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

Page 21: Jasper Talk

Select a layout

• Pick either a columnar or tabular layout

• Hit next

• Select “finish” on the last screen

21Wednesday, May 13, 2009

Page 22: Jasper Talk

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

Page 23: Jasper Talk

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

Page 24: Jasper Talk

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

Page 25: Jasper Talk

Passing Parameters to Reports

25Wednesday, May 13, 2009

Page 26: Jasper Talk

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

Page 27: Jasper Talk

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

Page 28: Jasper Talk

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

Page 29: Jasper Talk

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

Page 30: Jasper Talk

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

Page 31: Jasper Talk

DEMO

• Show code on how to include parameters in call to Jasper.

31Wednesday, May 13, 2009

Page 32: Jasper Talk

Dynamic Jasper

0

25

50

75

100

2007

2008

2009

2010

Drink Price ($) Sales ($1000s)

32Wednesday, May 13, 2009

Page 33: Jasper Talk

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

Page 34: Jasper Talk

Ways to use DJ

• Quickly use it in a URL

• Entity Based Configuration

• Named Reports Configuration

34Wednesday, May 13, 2009

Page 35: Jasper Talk

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

Page 36: Jasper Talk

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

Page 37: Jasper Talk

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

Page 38: Jasper Talk

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

Page 39: Jasper Talk

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

Page 40: Jasper Talk

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

Page 41: Jasper Talk

• 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

Page 42: Jasper Talk

• 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

Page 43: Jasper Talk

• 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

Page 44: Jasper Talk

• Named Report Building and Demo.

Demo

44Wednesday, May 13, 2009

Page 45: Jasper Talk

Summary

• Today we learned about

• iReport to create reports

• Jasper Reports and passing parameters

• Dynamic Jasper for quick reports

45Wednesday, May 13, 2009

Page 46: Jasper Talk

0

25

50

75

100

Less

More

Even More

Off the Chart

Questions?Question Obscurity Answer Hilarity

46Wednesday, May 13, 2009