Your Federal Quarterly Tax Payments are due April 15th Get Help Now >>

BAM Dashboards for Open ESB and Java CAPS SUN MICROSYSTEMS BAM by bud19087

VIEWS: 77 PAGES: 42

									SUN MICROSYSTEMS
BAM Dashboards for Open ESB and Java CAPS
Version 1.0
September 29, 2008

Please send feedback to: thomas.barrett@sun.com




            - 1 of 42 -
                                                        Table of Contents
Introduction .................................................................................................................................. 3

Scenario ........................................................................................................................................ 3

Environment ................................................................................................................................. 4

Tutorial Format ............................................................................................................................. 4

Project 1: Installing the iReport NetBeans Plug-in
      Downloading iReports Plug-in for NetBeans .................................................................. 5
      Installing the iReport Plug-In .............................................................................................. 6
      Verifying Plug-in Installation .............................................................................................. 6

Project 2: Establishing a Test Database
      Creating a Java DB Database ......................................................................................... 7
      Creating a Database Table ............................................................................................... 8
      Populating a Database Table ........................................................................................... 9
      Viewing Table Data ........................................................................................................... 9

Project 3: Developing a Report with iReport
      Registering a JDBC Connection to iReport .....................................................................10
      Creating an iReport Report ................................................................................................11
      Adding a Pie Chart ............................................................................................................. 15
      Previewing a Report Design ..............................................................................................19
      Displaying an Accumulator on a Chart ..............................................................................21
      Hiding Report Sections ...................................................................................................... 22

Project 4: Developing the BAM Dashboard
      Exploring the iReports Life Cycle ...................................................................................... 28
      Filling an iReport with Data ............................................................................................... 30
      Importing the JasperSoft Library ....................................................................................... 31
      Exporting an iReport to HTML ........................................................................................... 33
      Deploying the Web Application ........................................................................................ 34
      Testing the Web Application .............................................................................................34
      Auto-Refreshing the Web Page ........................................................................................36
      Inserting Additional Test Data ...........................................................................................38

Summary ....................................................................................................................................... 39




                                                                  - 2 of 42 -
Introduction

The purpose of this tutorial is to introduce you to building a BAM (business activity monitoring)
dashboard display using the NetBeans IDE. JasperSoft has created a NetBeans plug-in
module for its iReport open source offering that allows you to create sophisticated reports for
display in either rich clients or in browsers. Having a BAM capability is essential in informing
application users of decision-impelling events. For example, Open ESB's Intelligent Event
Processor (IEP SE) may process streams of incoming financial transactions and discover
fraudulent activity. Users would likely want a dashboard where IEP output could be monitored
via business graphics.

This tutorial will get you started with the iReport NetBeans plug-in by developing a web
application that reads data generated by the Java One 2008 PennyLotto IEP demo and
constantly refreshes a pie chart summary. Here are the topics we'll cover:

   -   Installing the iReport NetBeans Plugin
   -   Establishing a Test Database
   -   Building a Report with iReport
   -   Developing the Auto-Refreshing Dashboard

Scenario

Let's assume that you are either familiar with the PennyLotto IEP demo from Java One 2008
or have read another tutorial in this series titled “Event Processing in Java CAPS 6:
Open ESB Technology.” You understand that as the PennyLotto IEPmodule determines
potential fraudulent transactions, it dumps incident records to a file. This file could populate a
database table containing the person's name and transaction amount. Since BAM is such an
important part of many web applications written today, you wonder how to reflect the incident
data in BAM business graphics. You want to work through the steps of building a web
application that summarizes the database data in a pie chart on a dashboard. When you are
done, you want to show your colleagues your NetBeans and iReport prowess at:

Building a Test Database: This is a simple one-table Java DB database. The INCIDENT table contains
the following columns: recordid, name, amount and timestamp. These table rows record fraudulent incidents
that occur at PennyLotto. You'll populate the INCIDENT table with six records initially.

Building a Report with iReport: JasperSoft's iReport tooling provides a sophisticated design
environment to define reports. An open source version is available for download. The tooling shields you from
editing the XML specification file. It also makes the compilation and data filling steps easy as you iterate
towards a report design you like. iReport is 100& and is developed atop the NetBeans framework. It is
available as a stand-alone tool. However, in this tutorial, we will use the NetBeans plug-in version. First, we'll
create a complete report with tabular data and our pie chart. Next, we'll hide the tabular data so that only our
pie chart displays for our dashboard.

Developing the Auto-Refresh Dashboard: In MVC (model-view-controller) fashion, our iReport
report specification is the view layer. We'll develop a servlet that acts as the controller to periodically refill the
report view with fresh database data (the model) and redisplays the pie chart.


                                                     - 3 of 42 -
Environment

You'll need a modern computer with at least 1GB of RAM (2GB is better) and about 600MB
of free disk space. Although the JDK and NetBeans work on a broad range of operating
systems, I'll assume that you are using Windows XP. You may still be successful with another
operating system, but the instructions were written and tested on a Windows XP (SP3) laptop.

You'll need to have with NetBeans 6 or an Open ESB build installed. NetBeans 6 can be
downloaded from http://www.netbeans.org/. Several download options are available. Choose
the option for “All” so that you'll have additional capabilities that may be useful in other
tutorials. You can find the latest Open ESB build at
https://open-esb.dev.java.net/Downloads_OpenESB_Addons_NB6.html. I favor using
recent Open ESB builds as it gives me the latest bug fixes that may not have made it into the
NetBeans download yet.


Tutorial Format

In the pages that follow, you'll see a 3-column format. In the first column, we'll include headings
to act as mileposts that to remind you of the purpose of the steps in the section. Hopefully,
these headings will also be helpful later as you use this document as a reference. Each
heading is listed in the table of contents to help you find a section quickly.

In the second column, we've included detailed, step-by-step guidance on the keystrokes and
mouse clicks required to accomplish the tasks. Along the way, you will find illustrations so you
can compare what you see on your screen to what we intend.

In the third column, you'll find narrative (in green) that provides background to the steps listed
in the second column. We have included some conceptual topics, informal advice and some
deeper technical tips here. Having them in a separate column hopefully keeps the second
column more concise while providing some deeper insight into the projects.




                                            - 4 of 42 -
Project 1: Installing the iReport NetBeans Plug-in

In this project, we'll download JasperSoft's iReport plug-in module for NetBeans and install it.
With the plug-in installed, you'll be able to use drag and drop and property windows to build your
iReport definition rather than coding the details in an XML file.

Download-      Let's start out by creating a new directory for our       We'll put everything we
                                                                         download and create in this
ing iReports   tutorial efforts. We will also store software here that   tutorial into this directory.
Plug-in for    we download from JasperSoft's web site. Use
NetBeans       Windows Explorer to create the C:\MyBAMTutorial
               directory.

               Point your browser to JasperSoft's download page          JasperSoft hosts it's
               http://sourceforge.net/project/showfiles.php?group_i      community edition of its
               d=64348. This takes us to the sourceforge.net page        products as
               where the open source version of iReport is               SourceForge.net.
               available. Click on the Download link for iReport
               Plugin for NetBeans:




                                                                         Download took about one
               Right-click on the iReport-nb-3.1.1-plugin.zip link       minute for me on a
               and save the zip file in your C:\MyBAMTutorial            broadband connection.
               directory.

               After the software is downloaded, find the zip file
               and expand it into the C:\MyBAMTutorial directory:




                                            - 5 of 42 -
Installing     Start your NetBeans.
the iReport
Plug-In        Use the Tools / Plugins sequence to display the
               Plugins Window.

               Click Downloaded tab and click Add Plugins button,

               Navigate to C:\MyBAMTutorial.

               Use Shift-click to select all three nbm files:         “nbm” stands for NetBeans
                  ● heartbeat-plugin-nb-3.1.1.nbm                     module.
                  ● iReport-nb-3.1.1.nbm
                  ● jasperserver-plugin-nb-3.1.1.nbm


               Click on Install and finish the wizard sequence by
               considering the license.

               Click Finish when you see the note that installation
               has been successful.


Verifying      Check to see that the three plug-ins were installed
Plug-in        by selecting the Installed tab shown below. Sort by
Installation   category and find:
                  ● ireport-designer from Tools category
                  ● heartbeat from Uncategorized
                  ● jasperserver-plugin from Uncategorized




                                                                      Notice that when you click on
                                                                      a plug-in, you see a
                                                                      description of it displayed in
                                                                      the right panel. Here you
                                                                      learn a bit about the power of
                                                                      iReports.




               Close the Plugins window.

               Your NetBeans installation has now been
               augmented with JasperSoft's powerful iReport report
               design, compile and test facility.


                                            - 6 of 42 -
Project 2: Establishing a Test Database

The pie chart we'll include on our BAM dashboard will be driven by database content. We'll keep
things real simple here. We will have just one table in our database. We'll use the bundled Java
DB database that ships with NetBeans. We'll create our table and populate it with sample data.

Creating a     In the Services panel right-click on Java DB node
Java DB        and select the Create Database option.
Database
               For Database Name, enter FraudDatabase

               For User Name, enter admin                              The admin/admin
                                                                       combination is pretty easy to
                                                                       remember.
               For Password, enter admin




               Click OK.

               Watch as the
               jdbc:derby://localhost:1527/FraudDatabase [admin
               on ADMIN] line is added to list of connections.

               Right-click on this new database connection and
               select Connect.

               Expand the Tables node to see that no tables exist.

               Let's create one. Right-click on Tables and select      You'll appreciate the tight
               the Execute Command option. This displays an            integration of database
                                                                       access and the NetBeans
               SQL execution area where we can enter and               coding environment. It
               execute SQL statements.                                 makes it easy to quickly
                                                                       change table contents and
                                                                       see changes reflected in your
                                                                       code that performs JDBC
                                                                       operations.




                                           - 7 of 42 -
Creating a   In the SQL execution area, copy and paste the                    This syntax for the recordid
Database     following statements that will create our INCIDENT               column is a way to tell Java
                                                                              DB to automatically insert a
Table        table:                                                           sequential value whenever a
                                                                              new row is created.
             CREATE TABLE INCIDENT
             (
                   RECORDID INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
                   NAME VARCHAR(40),
                   AMOUNT NUMERIC(15),
                   TIMESTAMP VARCHAR(30)
             )




             Click Run SQL tool to execute the CREATE TABLE
             statement:




             Watch for a message like this to know that the SQL
             statement was processed:

             Executed successfully in 0.281 s, 0 rows affected. Line 1, column 1
             Execution finished after 0.281 s, 0 error(s) occurred.

             Right-click on the Tables node and select Refresh.
             Your incident table appears:




             Right-click on INCIDENT and select View Data to
             see that the table is empty as expected.




                                             - 8 of 42 -
Populating   Copy and paste the following statements into the
a Database   SQL execution area and run them. This will
Table        add a few test incidents to the INCIDENT table:
             insert into incident (recordid,name,amount,timestamp) values(default,'Joe Doe',112.00,'2008-09-17T14:06:55.53-06:00');
             insert into incident (recordid,name,amount,timestamp) values(default,'Joe Doe',122.00,'2008-09-17T14:07:55.53-06:00');
             insert into incident (recordid,name,amount,timestamp) values(default,'Joe Doe',132.00,'2008-09-17T14:08:55.53-06:00');
             insert into incident (recordid,name,amount,timestamp) values(default,'Sally Smith',122.00,'2008-09-17T14:06:55.53-06:00');
             insert into incident (recordid,name,amount,timestamp) values(default,'Sally Smith',222.00,'2008-09-17T14:07:55.53-06:00');
             insert into incident (recordid,name,amount,timestamp) values(default,'Bill Watkins',722.00,'2008-09-17T14:04:55.53-06:00');

                                                                                              Notice the “default” value
                                                                                              inserted in the recordid
                                                                                              column. As noted when we
Viewing      Right-click on INCIDENT and select View Data to                                  created the table, we defined
Table Data   verify that the six records have been added to the                               this column to include a
             table:                                                                           system-generated sequential
                                                                                              value. So, default is a
                                                                                              placeholder for that value to
                                                                                              be inserted by Java DB.




                                                                                              If you ever need to clear all
                                                                                              the rows from the table, enter
                                                                                              the following command into
                                                                                              the SQL execution area:
                                                                                              delete from ADMIN.INCIDENT




                                                     - 9 of 42 -
Project 3: Developing a Report with iReport

In this project, we'll use the iReport NetBeans plug-in to create an iReport definition containing
rows of data from the INCIDENT table and a summary pie chart graphic. Then, we'll tailor the
report so that only the pie chart appears since we just want business graphics to appear on our
BAM dashboard.

Registering     We need to let the iReport plug-in know that our new
a JDBC          FraudDatabase should be the default target for all
Connection      reporting. To register your JDBC connection to
to iReport      iReport, find the Report Datasource control at
                the top of the window:




                Click on Report Datasource to display the
                Connections / Datasources window.

                Click the New button to identify a new datasource.

                In the Datasource window, select
                NetBeansDatabase JDBC connection and then click
                Next.

                In the Connection drop-down list, for Name, enter:
                FraudDatabaseConnection and
                then select the following connection:
                jdbc:derby://localhost:1527/FraudDatabase [admin on ADMIN]

                Click Test to see if iReport can “find” your database.       Test is sorta like the Ping
                                                                             feature that you can use in
                Look for the Connection test successful! pop-up              GlassFish when you create a
                window as verification and click OK to dismiss the           new database connection
                window.                                                      pool and want to know if the
                                                                             pool can “see” the database
                Click Save to record your selection and close the            before you create a JDBC
                                                                             datasource to use the pooled
                NetBeans Database JDBC connection window.                    connections.




                                              - 10 of 42 -
              In the Connections / Datasources window, make             To close the window, click on
              sure that your FraudDatabaseConnection is the             red X in the upper-right
                                                                        corner of the window since
              default by seeing that the Default check box is           there is no Close or OK
              toggled one. Close the window:                            buttons. Sorta odd ...




Creating an   With a database connection in place, we can now
iReport       create our report page. Create a BAMDisplay
Report        project using the File / New Project / Web / Web
              Application sequence.

              For Project Name, enter BAMDisplay

              For Project Location, enter C:\MyBAMTutorial
              directory. Click Next

              Accept the defaults for server, Java EE version and       The wizard step we skipped
              Context Path by clicking Finish.                          here by just clicking Finish
                                                                        provided the opportunity to
                                                                        select a framework like
              An index.jsp page appears. Close it.                      Visual Web JavaServer
                                                                        Faces. We don't need to
              On the BAMDisplay node in the Projects panel, use         create a user interface with
              the File / New / Other / Report / Report sequence to      one of these frameworks.
                                                                        The iReport capabilities will
              begin creating a new iReport based upon a pre-            handle our view capabilities
              configured datasource.                                    in our MVC (model-view-
                                                                        controller) pattern. The
              For File Name, enter: PieChartByCulprit.jrxml             servlet we will create later will
              Note: It is critical to include the .jrxml extension on   be our control. The
                                                                        database, of course, is our
              the file name. Otherwise, you won't have access to        model.
              the graphical Design mode.

              For Folder, enter: web/reports and click Next to
              move to the Query wizard window.

              For Connections / Data Sources, select
              FraudDatabaseConnection from the drop-down
              list.



                                          - 11 of 42 -
Click the Design Query button:




The query design window appears as shown below.

Next to Tables drop-down list (see below), select
ADMIN from the drop-down list. The INCIDENT
table appears in the list.

Click and drag INCIDENT to the purple canvas area.
Click on the check boxes for the NAME and
AMOUNT columns:




Click the SQL tab (see the mouse pointer above) to
see the generated SQL statements:
SELECT                                               This is pretty straightforward,
     INCIDENT."NAME" AS INCIDENT_NAME,               but I think you can see by the
     INCIDENT."AMOUNT" AS INCIDENT_AMOUNT            WHERE, GROUP BY and
FROM                                                 HAVING options in the
     "ADMIN"."INCIDENT" INCIDENT                     window that the queries can
                                                     get much more sophisticated.

                          - 12 of 42 -
Click OK to return to the Query step in the wizard,   Seems that the highlighted
then, click Next.                                     steps in the left panel (Group
                                                      by here) don't match up with
                                                      the currently displayed
When prompted for Fields, use the >> control (see     panels on the right (Fields
below) to select all. Click Next:                     here). Looks like a bug?




In the Group by wizard step, select
INCIDENT_NAME from the Group 1 drop-down list,
then click Next.




In the Layout wizard window, just click Next.         iReports provides a
                                                      templating capability. Two
                                                      template styles ship with the
                                                      product: columnar and
                                                      tabular. You can add your
                                                      own standard templates to
                                                      the design environment.
                                                      We'll just take the default
                                                      here (columnar).



                           - 13 of 42 -
Click Finish and you report design appears in the         Note the Designer control
report editor window:                                     that appears in the upper-left
                                                          of the panel.




                                                          If the Designer mode doesn't
In the Report Inspector, find summary click on it:        display, see the earlier note
                                                          about needing to include the
                                                          .jrxml extension for File
                                                          Name.

                                                          If the Report Inspector
                                                          doesn't appear, use the
                                                          Window menu bar option and
                                                          find the Report designer
                                                          menu option to hide and
                                                          display panels that the
                                                          iReports plug-in has added to
                                                          your NetBeans environment.




The summary section is the most important one for
us as this is where we will create our pie chart that
summaries data delivered by the query and
presented in tabular form in the details section of the
report.

Even though you can click around on the design
canvas to find the report sections, the Report
Inspector provides the most convenient and precise
way of navigating the report structure.

                           - 14 of 42 -
            The default height for the summary section is to      The band height describes
            short. Click on summary in the Report Inspector and   the height of the summary
                                                                  section in number of pixels.
            view the Properties window. Set its band height to    A height of 400 pixels should
            400                                                   be plenty to display a big 3D
                                                                  pie chart for our dashboard.




Adding a    From the Palette, drag and drop a Chart to the
Pie Chart   summary area of the canvas. The chart type
            window appears. Select the second pie chart called
            Pie 3D and click OK:

                                                                  There are so many charting
                                                                  styles to choose from. In the
                                                                  spirit of “keeping it simple,”
                                                                  we'll select a pie chart here.
                                                                  You can experiment with
                                                                  some of the others later.

                                                                  Things can be complicated
                                                                  and overwhelming at first
                                                                  since there are so many
                                                                  options. To ease the
                                                                  learning curve, download
                                                                  JasperReports and explore
                                                                  the demos. You'll find the
                                                                  free community download at:
                                                                  http://www.jaspersoft.com/re
                                                                  gistration.html?d=jr_downloa
                                                                  d.html&m=&h=&f= Sign up,
                                                                  download (jasperreports-
                                                                  3.1.0-project.zip or higher),
                                                                  unzip and explore your
                                                                  jasperreports-
                                                                  3.1.0\demo\samples\charts
                                                                  directory. Start with the
            A pie chart appears in the summary area.              readme.txt file.




                                     - 15 of 42 -
Click on the pie chart and enlarge so it fills most of
the summary area.




                                                         This signals to the designer
Right-click on the pie chart and select Chart Data       to create an accumulator that
(as shown above). The Chart details window               counts all rows in a group.
appears. Notice that there are two tabs: Dataset         We specify
and Details.                                             INCIDENT_NAME as the
                                                         group name, so the
                                                         accumulator will hold the total
On the Dataset tab, set the Increment type to Group.     number of records for each of
On the Increment group field, set it to                  the “culprits” in our
INCIDENT_NAME:                                           INCIDENT database.

                                                         Was with chart types, there is
                                                         so much power in iReports
                                                         that we can't cover in a
                                                         tutorial like this. You see that
                                                         you can condition
                                                         incrementing of group
                                                         counters. You also see that
                                                         sub data sets can be
                                                         specified. I found in-tool
                                                         documentation to be scarce
                                                         and the documentation on
                                                         the web to be inadequate.
                                                         Clearly, JasperSoft wants the
                                                         new user to buy their
                                                         “Definitive Guides” advertised
                                                         here:
                                                         http://jasperforge.org/website/ire
                                                         portwebsite/IR%20Website/iRep
                                                         ort_documentation.html?group_i
                                                         d=243&header=project&leftnav=
                                                         yes&target=ireport



                            - 16 of 42 -
Select the Details tab.

In the Key expression field click the pencil edit icon   The Key expression tells the
shown below at the right:                                chart builder what the basis
                                                         is for each pie-shaped piece
                                                         of the chart. Here we specify
                                                         that each pie is to represent
                                                         a unique culprit in our
                                                         database table.




                                                         You can click around on the
The Expression editor window appears.                    panels in the Expression
Click Fields and then double-click on                    Editor to build the expression
INCIDENT_NAME field to add                               or you can just copy and
                                                         paste the expression here
$F{INCIDENT_NAME} to the expression canvas.              into the top panel.
Click Apply:




                            - 17 of 42 -
In the Value expression field, click the pencil edit   The Value expression
icon at the right. The Expression editor window        communicates what will drive
                                                       the size of the individual pie-
appears.                                               shaped pieces. In our case,
                                                       we'll utilize the group counter
Click Variables and then double-click on the           that is available for individual
INCIDENT_NAME_COUNT variable to add                    names. This group counter
$V{INCIDENT_NAME_COUNT} to the expression              was created as a result of our
                                                       selection of
canvas as shown below.. Click Apply.                   INCIDENT_NAME as the
                                                       increment group in the
In the Label expression field, click the pencil edit   Dataset tab earlier.
icon at the right. The Expression editor window
appears.

Click Label and then double-click on the               The Label expression lets us
INCIDENT_NAME field to add                             specify how we want to
$F{INCIDENT_NAME} to the expression canvas.            assemble the label for each
Type + " - "' and then click Variables. Double-click   pie-shaped piece. Here we
                                                       are indicating that we want
on the INCIDENT_NAME_COUNT                             the label for each piece to be
variable to add $V{INCIDENT_NAME_COUNT} to             the concatenation of:
the expression canvas as shown in the diagram          - Culprit name
below. The final expression should read:               - A space, a hyphen, a space
                                                       - Count of the number of
$F{INCIDENT_NAME} + " - " + $V{INCIDENT_NAME_COUNT}      incidents reported for the
                                                         culprit

Click Apply.

Here is how the completed Details tab looks:




                            - 18 of 42 -
             Select Close to dismiss the Chart details window.   The Preview facility is pretty
                                                                 powerful. It works our report
                                                                 specification through a three-
             Save your work.                                     step life cycle. First, it
                                                                 compiles the .jrxml file into a
                                                                 .jasper file object
Previewing   Click on the Preview View control:                  representation. Second, it
a Report                                                         fills the object representation
                                                                 with data from the data
Design                                                           source. Third, it exports the
                                                                 results into HTML to display
                                                                 in a panel.


             Your report appears:




                                       - 19 of 42 -
Click back on the Designer control to return to
Designer mode.

In Report Inspector, right-click on top level node
(labeled null now) and select
Properties:




Set the Report name field to CulpritPieChart and     It is important for every report
select Close:                                        to have a report name.
                                                     Having null as a report name
                                                     would cause some
                                                     interesting debugging later as
                                                     we drive the filling and
                                                     exporting of our report in our
                                                     servlet.




                           - 20 of 42 -
Displaying   Let's add a total field to at the bottom of the chart to
an           show the total number of incidents that are included.
Accumula-    Click the Designer control if you need to in order to
tor on a     be in Designer mode.
Chart
             Click and drag a Static Text palette item below the
             pie chart:




             Double click on the new field and change Static text
             to Total Incidents:

             Change the static text field's Horizontal Alignment
             property field to Right.

             Click and drag a Text Field palette item to the right
             of the Total Incidents: static text field.

             Click on the new text field and in its properties
             window, find the Text Field Expression property.
             Click ... to edit this property value. The Text Field
             Expression editor appears. Clear the expression
             area by deleting $F{field}                                 Sometimes, point and click
                                                                        features can make something
                                                                        easy hard. Here, all we want
             Now, to add our variable, select Variables and then
                                                                        to do is enter:
             double-click on REPORT_COUNT to add                        $V{REPORT_COUNT}.toString()
             $V{REPORT_COUNT} to
             the expression canvas. Next, in the third column           It is probably easier to just
                                                                        type (copy and paste) the
             select toString() String. This makes the final
                                                                        expression into the editing
             expression read: $V{REPORT_COUNT}.toString()               area. It's nice to know that
                                                                        the point and click facility is
             Select OK to complete the expression editing.              there though. It can be
                                                                        helpful when considering
                                                                        more complicated
             Stretch out the text field on the canvas so the value
                                                                        expressions.
             appears like this under your pie chart.




                                         - 21 of 42 -
           Preview again and notice that the count for
           total incidents displays:




Hiding     It's great that iReport can generate tabular reports
Report     with rows and columns full of database data in
Sections   addition to business graphics like pie charts.
           However, for our business dashboard, we may want
           only business graphics. So, how can we eliminate
           the tabular data and retain the pie chart?

           An iReport design is made up of sections. A quick
           look at the Report Inspector panel shows how data
           is grouped into sections for display:

              ●   title
              ●   pageHeader
              ●   columnHeader
              ●   INCIDENT_NAMEGroupHeader
              ●   detail
              ●   INCIDENT_NAMEGroupFooter
              ●   columnFooter
              ●   pageFooter
              ●   summary

           We would like to hide all the sections except for the
           last one, the summary section. That is where our
           pie chart is located.




                                      - 22 of 42 -
All these sections have a Print When Expression         If the Print When Expression
property that we can use to turn off printing. Click    evaluates to blank or a true
                                                        (boolean) value, the section
on title in the Report Inspector and then look in the   will print. Sure, we could just
Properties panel to find the Print When Expression      delete the unwanted
property:                                               selections, but using Print
                                                        When Expression is more
                                                        elegant. What if we wanted
                                                        to restore the tabular display
                                                        based upon input from the
                                                        user at our dashboard
                                                        display?




As the documentation at the bottom of the properties
panel indicates, we can enter an expression here.
When the expression returns blank or true, this
section will be printed.

OK, what expression would work? Let's establish a
boolean variable and stuff it with true when we
wanted all of our hidden sections to print and false
when we wanted just the pie chart to appear. Then,
let's include a Print When Expression for each
section we want to hide. That expression will
evaluate the boolean parameter and control display
of the individual sections.




                           - 23 of 42 -
First we need to create the boolean parameter and
give it a default value by clicking on the XML tool to
view the underlying XML statements for our report
design. Add the <parameter> tag shown below:




Here is the statement for easy copy and paste:
                                                                   This is easy enough to
<parameter name="isReportBodyToPrint" class="java.lang.Boolean">
                                                                   understand. We name the
     <defaultValueExpression><![CDATA[new Boolean(false)]]>
                                                                   parameter and declare it to
     </defaultValueExpression>                                     be of Java type Boolean.
</parameter>                                                       Then, we instantiate a
                                                                   Boolean with the value set to
                                                                   false.

Use Alt-Shift-F to reformat the XML once you have
added the statement.

Save your work.

Return to Designer mode by clicking on the
Designer tool.




                                    - 24 of 42 -
We now want to reference the isReportBodyToPrint
parameter in the Print When Expression for the
sections we want to hide:
   ● title
   ● pageHeader
   ● columnHeader
   ● INCIDENT_NAMEGroupHeader
   ● detail
   ● INCIDENT_NAMEGroupFooter
   ● columnFooter
   ● pageFooter


For each of the sections listed above, click on it in
the Report Inspector window and then find it's Print
When Expression property in the property window.

Click the Print When Expression property to display
the expression editor. Either use the menus (select
Parameters and then double-click on
isReportBodyToPrint) or just copy and paste the
expression below:


                                                        Again, the point and click
                                                        features of the editor are
                                                        overkill for what we are doing
                                                        here. Just copy and paste
                                                        the desired expression into
                                                        the expression creation area.




For cut and paste convenience, here is the
expression: $P{isReportDataToPrint}

Make sure you perform the same edit of the Print
When Expression for each of the sections listed
above before moving on.




                           - 25 of 42 -
Click on the Preview View control to see your report
appear. Now, you are prompted for the value of the
isReportDataToPrint parameter. Just click OK to          We are prompted for the
accept the default we set (false).                       parameter value to allow use
                                                         to test both true and false
                                                         paths. By clicking OK, we
                                                         accept the default which we
                                                         set in this part of our
                                                         parameter definition:
                                                         <defaultValueExpression>
                                                          <![CDATA[new Boolean(false)]]>
                                                         </defaultValueExpression>




Notice that only the summary section appears so all
you see is your pie chart. Section hiding mission
was accomplished!

It would be nice to have a heading displayed over
our pie chart. We turned off the title section, but
click on the pie chart and look in the Properties
panel for an alternate way to label it. Find the Title
Expression property and set is as shown below.
Also adjust the Title Font as shown:




                                                         There are lots and lots of
                                                         customization options. You
                                                         can look forward to exploring
                                                         them later on your own. Let's
                                                         move on.




                            - 26 of 42 -
Click on the Preview control. Again, accept the
default value of the isReportDataToPrint parameter
and you'll see our finished pie chart:




Before we leave report design, set the
isReportDataToPrint parameter to true when we
preview and see if our tabular data appears along
with the pie chart.

Note: If you don't make any changes between
previews, the report is not regenerated and you
aren't prompted for parameter values. So, click on
the XML control to view the XML. An easy way to
make a change is to use Alt-Shift-F to reformat.
After you do that, click the Preview control and
answer true when prompted for
isReportDataToPrint. The hidden sections are
unhidden:

                                                     Use the Zoom drop-down list
                                                     in the upper-right corner (as
                                                     illustrated here with the
                                                     mouse cursor) to zoom out
                                                     so you can see your entire
                                                     report without scrolling.




                          - 27 of 42 -
Project 4: Developing the BAM Dashboard

We have our pie chart ready for display, now we need a way to display it so it can be
automatically refreshed periodically to display the latest data. In this project, we'll explore the life
cycle for iReport generation and we'll create a servlet that periodically regenerates the iReport
and displays it in a browser page.

Exploring    The report design (PieChartByCulprit.jrxml) provides
the iReports the specification for a web page to be generated that
Life Cycle   displays our report. iReports has a four-step life
             cycle as illustrated below:




                During Design, you use the standalone iReport tool
                or the iReport NetBeans Plug-in to create a remote
                specification as a .jrxml file. The Compile step takes
                the .jrxml file and produces an object representation
                stored in a .jasper file. Design and Compile typically
                form an iterative cycle during design time with
                frequent previews taken to assess progress. The
                preview generation actually performs the Fill and
                Export steps so the completed report can be
                previewed in the design environment.

                Once the specification is complete, the Fill and
                Export steps are invoked programmatically to read
                the latest database data, file the reports object
                structures and export the finished report to it its
                destination (rich client, web browser, PDF, ...)

                 We can drive the entire four-step process program-
                matically. However, we don't have to do the
                compilation phase at runtime as this is done at
                design time when we use the Preview control to
                populate and display our report. The .jasper file
                created during compilation will be deployed as part
                of our web application.

                                              - 28 of 42 -
We'll create a servlet to do the filling and exporting
steps at run time. Let's get started.

Right-click on the BAMDisplay node in the Projects
panel and use the New / Servlet sequence to start a
new servlet.

For Class Name, enter DisplayPieChart
                                                         Note that we are skipping the
For Package, enter piechart and click Next.              step in the wizard where we
                                                         can pick a visual component
Accept the defaults for Servlet Name and URL             framework like Visual Web
                                                         JavaServer Faces or Struts.
Pattern(s) by clicking Finish.                           That's because we will let
                                                         iReport generate our user
Your new servlet appears in the Java editor.             interface based upon our
                                                         .jrxml file. In MVC (model-
                                                         view-controller) speak, our
                                                         servlet will be just the
                                                         controller while our iReport
Let's address the filling step first by adding code to   specification drives the
the processRequest method. Clear out the skeleton        generation of the view layer
code that was initially created for the method:          at runtime.




                            - 29 of 42 -
Filling an   Add (copy and paste) the following Java statements
iReport with to perform report filling. This takes care of
Data         populating the compiled report from your
             INCIDENTS table based upon the query you built
             during report design:
             // Fill Report
             ServletContext context = this.getServletConfig().getServletContext();
             String reportName = "PieChartByCulprit";
             String reportDirectory = "/reports/";
             String compiledFileName = context.getRealPath(reportDirectory + reportName +
               ".jasper");
             File reportFile = new File(compiledFileName);
             System.out.println("*** Filling report");
             Map parameters = new HashMap();
             parameters.put("ReportTitle", "Culprits Report");
             parameters.put("BaseDir", reportFile.getParentFile());
             JasperPrint jasperPrint = null;
             String dbURL =
             "jdbc:derby://localhost:1527/FraudDatabase;user=admin;password=admin";
             Connection conn = null;
             try {
                 Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
                 conn = DriverManager.getConnection(dbURL);
                 jasperPrint = JasperFillManager.fillReport(compiledFileName, parameters, conn);
                 System.out.println("*** Report succesfully filled from file " +
                 compiledFileName + " from database " + conn);
             } catch (Exception ex) {
             System.out.println("*** Could not create compiled jasper file: " +
                                 ex.getMessage());
             } finally {
                 try {
                     conn.close();
                 } catch (SQLException ex) {
                 System.out.println("*** Could not close connection after filling report");
             }

              You see lots of errors as designated by all the yellow
              and red icons along the left margin. The errors are
              caused by missing imports. Insert the following
              import statements at the top of your code right below
              the package piechart statement:

              import   javax.servlet.ServletContext;
              import   java.io.File;
              import   java.sql.Connection;
              import   java.sql.DriverManager;
              import   java.sql.SQLException;




                                         - 30 of 42 -
             That takes care of some of the problems, but the
             editor can't still find JasperPrint and the
             JasperFileManager classes. Let's focus on this now.

             When you installed the iReport NetBeans plug-in, a
             JasperSoft library was added to your NetBeans
             environment. Let's import that library into our project
             to address our two “class not found” problems.


Importing    Right-click on the BAMDisplay node in the Projects        The NetBeans Library feature
the          panel and select Properties.                              is a convenient one. A library
JasperSoft                                                             is a collection of one or more
Library      In the Properties window, select the Libraries node       jar files containing class
             and use the Add Library button to display a list of       definitions needed by a
                                                                       programming API. iReport
             libraries installed in your NetBeans environment.         (the design time tool) and
                                                                       JasperReports (the runtime
             Scroll down the list of Available Libraries and select    environment) require a bunch
             JasperReports 2.0.5-snapshot. Click Add Library           of jar files to be able to do
             and then OK.                                              compilation, filling and
                                                                       export.
                                                                                    If you use the
                                                                                    Tools / Libraries
                                                                                    menu option and
                                                                                    look inside the
                                                                                    JasperReports
                                                                                    library, you will
                                                                                    see that it
                                                                                    includes about 30
                                                                                    jar files. It sure is
                                                                                    convenient to have
                                                                                    them in a single
                                                                                    library that you
                                                                                    can add to your
                                                                                    project rather than
                                                                                    having to add each
                                                                                    one individually.
                                                                                    Thanks to
                                                                                    NetBeans for
                                                                                    providing the
                                                                                    Libraries facility
                                                                                    and thanks to
                                                                                    JasperSoft for
                                                                       automatically installing their
                                                                       library complete with all the
                                                                       jar files we need.




                                        - 31 of 42 -
Return to the Java editor window and click on each
of the yellow light bulb icons and elect to have
NetBeans import the class definition from the library
we just added:




This adds the following import statements to the top
of your servlet code:
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;


Use F9 (Build / Compile) to compile the servlet and
check for errors. You should see ”BUILD
SUCCESSFUL” in the Output panel indicating that
our code is syntactically correct.

Let's move on to the last step in the iReport report
lifecycle – exporting the filled report objects. We'll
export to HTML so we can display our report in the
browser.




                            - 32 of 42 -
Exporting    Below the code you just added for filling the iReport
an iReport   objects with database data, copy and paste the
to HTML      following statements. These export an HTML
             representation of the filled, compiled report:
             // Export Filled Report to HTML
             response.setContentType("text/html;charset=UTF-8");
             PrintWriter out = response.getWriter();
             System.out.println("*** Exporting report to HTML");
             String HTMLOutFileName = "../docroot/FraudIncidentReport.html";
             try {
                 JasperExportManager.exportReportToHtmlFile(jasperPrint, HTMLOutFileName);
                 System.out.println("*** Successfully exported to HTML file: " +
                   HTMLOutFileName);
             } catch (JRException ex) {
                 System.out.println("*** Could not export report " + compiledFileName + "
                   to HTML");
             }
             You will see one error. The editor can'tfind the
             JasperExportManager class. To solve this, add the
             following import statement below your package
             statement:
             import net.sf.jasperreports.engine.JasperExportManager;


             Once again, use F9 (Build / Compile) to compile the
             servlet and check for errors. You should see
             ”BUILD SUCCESSFUL” in the Output panel
             indicating that our code is syntactically correct.

             Let's test our servlet by first building and deploying
             our web application.




                                         - 33 of 42 -
Deploying     Deploy your BAMDisplay web application to
the Web       GlassFish so we can test the servlet.
Application
              Right-click on BAMDisplay in the Projects panel and
              select Build. Watch the Output panel to see the     If your GlassFish instance
              reassuring message: “BUILD SUCCESSFUL.”             isn't up and running when
                                                                     you deploy, it will be
                                                                     automatically started. This
              Right-click on BAMDisplay in the Projects panel and    can make the first deploy of a
              select Undeploy and Deploy. Again watch the            session a lot slower than
              Output panel for progress messages.                    subsequent ones. My first
                                                                     deployment took about 90
                                                                     seconds.
Testing the   Right-click on your DisplayPieChart.java node in the
Web           Projects panel and select Run. The following pop-
Application   up window appears:
                                                                     This will result in the
                                                                     following URL that we could
                                                                     use from the browser to
                                                                     invoke our servlet:
                                                                     http://localhost:8080/BAMDisplay/DisplayPieChart




              Accept the default URI by clicking on OK.

              Drum roll please ... the browser displays ... and
              nothing ... just a blank page! Humm ...

              Let's check the GlassFish V2 Output panel for
              errors:




              That's what we hoped for based upon the                If you see a
                                                                     ConnectException
              System.out.println statements we included in our       referencing port 1527, it
              servlet code.                                          means that you need to go to
                                                                     the Services panel and
                                                                     connection your
                                                                     FraudDataBase connection.




                                         - 34 of 42 -
So, what's up? Well, maybe it really did work?
Revisit the following lines of code in our servlet:
String HTMLOutFileName = "../docroot/FraudIncidentReport.html";
JasperExportManager.exportReportToHtmlFile(jasperPrint, HTMLOutFileName);

Note that the JasperExportManager is instructed to
put the generated HTML into
FraudIncidentReport.html in the docroot directory.
The docroot directory is the top-level document
directory for GlassFish's built-in web server. Use
Windows Explorer to find your file in docroot:




Display the HTML file and your pie chart should         You can see that my
                                                        GlassFish is installed under
appear as shown above.                                  the C:\openesb directory.
                                                        Yours will likely be in another
OK, it did work as we specified, but that won't work    place.
for our end user. It is unreasonable to ask him/her
to search for the generated file to display it in the
browser.




                            - 35 of 42 -
Auto-        If this pie chart page is going to become part of a
Refreshing   dashboard, we really have two problems to solve.
the Web      First, it needs to display when we hit the servlet's
Page         URL instead of just landing invisibly in the docroot
             directory. Second, it needs to refresh periodically to Dashboard users wouldn't
             reflect the latest database contents.                  like having to constantly hit
                                                                        the Refresh control on their
                                                                        browser to refresh the pie
             Let's return to the servlet's Java code and add the        chart.
             following at the bottom to solve the two problems:
             // Insert <meta> to control refresh and <base-ref> to locate graphics
                     System.out.println("*** Inserting statements inside <head> tag");
                     BufferedReader inputStream =
                       new BufferedReader(new FileReader(HTMLOutFileName));
                     String htmlLine = "";
                     while ((htmlLine = inputStream.readLine()) != null) {
                         out.println(htmlLine);
                         if (htmlLine.equals("<head>")) {
                             out.println("<meta http-equiv=\"refresh\"content
             =\"15;url=http://localhost:8080/BAMDisplay/DisplayPieChart\">");
                             out.println("<base href=\"http://localhost:8080\" />");
                         }
                     }
                     out.close();
                     System.out.println("*** Successfully inserted <meta> and <base-ref> in
             <head> section");
                 }

             You'll need to add the following import lines to the
             top of your servlet to resolve “class not found”:

             import java.io.BufferedReader;
             import java.io.FileReader;

             Use Alt-Shift-F to reformat your code and F9 to
             recompile it to check for errors.

             Right-click on BAMDisplay in the Projects panel and
             select Build. Watch the Output panel to see the
             reassuring message: “BUILD SUCCESSFUL.”

             Right-click on BAMDisplay in the Projects panel and
             select Undeploy and Deploy. Again watch the
             Output panel for progress messages.

             Right-click on your DisplayPieChart.java node in the You can also use the
             Projects panel and select Run.                       following URL to invoke the
                                                                        servlet:
                                                                        http://localhost:8080/BAMDisplay/DisplayPieChart




                                          - 36 of 42 -
As the pie chart displays in the browser window,           For Mozilla Firefox, the
watch for the page to refresh every 15 seconds.            refresh indicator is the circle
                                                           of dots in the upper-right
Since the data in the pie chart isn't changing, you'll     corner below the red X:
have to watch the browsers refresh indicator.

Look in the Output panel for further verification of the
refresh. Every 15 seconds, you should see the
following block of output indicating that the servlet is
refilling the iReport objects from the database with
each refresh cycle:




Great. You saw the browser refresh and you saw
the System.out.println statements generate play-by-
play to stdout with each refresh cycle, but it would
be nice to see the chart change, right? To do that,
we'll have to change the content of the database.

In the Services panel, find your INCIDENT table
again:




Right-click on INCIDENT and select View Data.
That brings up a SQL command area populated with
select * from ADMIN.INCIDENT. At the bottom, you
see the rows that are currently in the table.




                            - 37 of 42 -
Inserting    With your pie chart page still displaying in the
Additional   browser, copy and paste the following insert
Test Data    statements into the SQL command area and then
             click the Run SQL tool to execute them. This will
             give our pie chart more data to display:

             insert into incident (recordid,name,amount,timestamp) values(default,'Joe Doe',412.00,'2008-09-17T14:10:55.53-06:00');
             insert into incident (recordid,name,amount,timestamp) values(default,'Tom Tilman',100.00,'2008-09-17T14:07:55.53-06:00');
             insert into incident (recordid,name,amount,timestamp) values(default,'Tom Tilman',732.00,'2008-09-17T17:08:55.53-06:00');
             insert into incident (recordid,name,amount,timestamp) values(default,'Bill Watkins',122.00,'2008-09-17T14:06:55.53-06:00');
             insert into incident (recordid,name,amount,timestamp) values(default,'Bill Watkins',222.00,'2008-09-17T14:07:55.53-06:00');
             insert into incident (recordid,name,amount,timestamp) values(default,'Bill Watkins',722.00,'2008-09-17T14:04:55.53-06:00');

             Watch the pie chart page refresh and see the pie
             chart change. Click the Run SQL tool again to
             execute this batch of insert statements a second
             time to see additional changes on the next refresh
             cycle. With 18 records in the database, here is how
             the chart displays:




             Success! Our dashboard displays automatically to
             show the current contents of the underlying
             database.




                                                    - 38 of 42 -
                                                                             ●
Summary

We accomplished quite a bit in the four projects. Let's review by revisiting the highlights of each:

Project 1: Installing the iReport NetBeans Plug-in
                   ●   We started out by installing the iReport plug-
                       in for NetBeans. iReport is a Java application
                       built atop the NetBeans framework. We could
                       use iReport as a stand-alone tool. However,
                       we chose to download the version that plugs
                       into an existing NetBeans installation and
                       runs alongside other plug-ins that you have in
                       your NetBeans environment.

                   ●   iReport, created by JasperSoft, provides
                       design, compilation and preview capabilities
                       for report generation. Under the covers it
                       uses JasperSoft's JasperReports to fill reports
                       with database data and export the finished
                       product to formats like HTML and PDF.

                   ●   JasperSoft makes both iReport and
                       JasperReports available in community edition
                       form free for download and use from the
                       Web.

Project 2: Establishing a Test Database
                   ●   We built a test database to contain fraud
                       incidents that might be generated by the Java
                       One 2008 PennyLotto IEP demo. PennyLotto
                       features the Intelligent Event Processor (as
                       provided by the IEP Service Engine) and
                       watches streams of financial data. It
                       generates alerts when it detects fraudulent
                       behavior.




                                            - 39 of 42 -
                ●   For convenience, we used the built-in Java
                    DB (Derby) database in NetBeans and we
                    built a new FraudDatabase. When the new
                    Java DB database was built, a JDBC
                    connection was automatically created for us
                    in the Services panel that we could use to
                    access its contents.

                ●   We used the SQL execution area and the
                    JDBC connection to create a simple
                    INCIDENT table containing columns for
                    recordid, name, amount and timestamp. We
                    populated the table with some sample data.

Project 3: Developing a Report with iReport
                ●   Our first step in building our report was to
                    register our JDBC connection to the iReport
                    plug-in. We tested that connection to make
                    sure that iReport could "see" FraudDatabase
                    in Java DB.

                ●   We created our BAMDisplay web application
                    project and started building the
                    PieChartByCulprit.jrxml iReport specification.

                ●   Initially, we had to specify the query we would
                    base our report upon. We kept it simple by
                    selecting only the name and amount columns
                    since this is all the data we anticipated
                    needing in our BAM dashboard business
                    graphics. Actually, we ended up using only
                    the name data.

                ●   We specified that the query data was to be
                    grouped by name since we were thinking
                    ahead that our business graphic would be a
                    pie chart and each pie piece would represent
                    a unique culprit.




                                        - 40 of 42 -
●   We used a default report template but learned
    that iReport allows us to create our own
    standard templates and include them in the
    tool. The default template showed our data in
    tabular form.

●   We did a lot of detail work getting the pie
    chart configured. The key decisions we were
    to:
        ● Accumulate totals based upon group
           change and defined group to mean the
           name of our culprit
        ● Have each pie piece be based upon a
           unique culprit name
        ● Base the size of the pie pieces on the
           value of the group (name) accumulator
        ● Label each piece by concatenating the
           culprit's name with the accumulator
           count (number of times his/her name
           appeared in the query result set)

●   We added a label at the bottom of the pie
    chart to show how many total incidents were
    represented in our report.

●   We began pondering how to hide the tabular
    report data that we didn't want to show on our
    BAM dashboard. We used a boolean
    parameter and print expressions for each
    unwanted section to condition printing based
    upon the parameter's default value we set in
    the XML view of the .jrxml report specification.

●   We tested our report with iReport's preview
    feature and were happy with the report
    specification. Next step was to "drive" the
    filling and exporting of the report via a servlet.




                         - 41 of 42 -
Project 4: Developing the Dashboard

                ●   We reviewed the four-step life cycle of a
                    report in iReport and JasperReports. We saw
                    that the Design and Compile steps had been
                    the primary focus of our design time work so
                    far. However, some Fill and Export work had
                    been done also during report preview.

                ●   We turned our attention to driving Fill and
                    Export programmatically from a servlet so the
                    user could see the report as a HTML page
                    displayed in a browser.

                ●   Servlet programming become the focus as we
                    created the servlet and utilized JasperSoft's
                    API to fill the report from our INCIDENT table
                    in Java DB. Then, we used the API to export
                    the filled report objects into HTML form. An
                    HTML file was generated successfully in
                    GlassFish's docroot directory.

                ●   Having the end user have to manually find the
                    generated report and display it in the browser
                    wasn't meeting our expectations. The servlet
                    should automatically display the report in the
                    browser and automatically refresh the
                    contents of the report periodically to show
                    fresh database data.

                ●   So, we added a bit of code in our servlet to
                    augment the HTML that was generated by the
                    export API. Specifically, we added a <meta>
                    tag to set a refresh interval. We also added a
                    <base> tag so that the servlet could find
                    generated graphics files produced during
                    export.

                ●   We tested our servlet and watched it work on
                    updated data from the INCIDENT table.


             I hope that you enjoyed this tutorial and would enjoy
             hearing from you. Please provide feedback to:
             thomas.barrett@sun.com.



                                        - 42 of 42 -

								
To top