Using dynamic titles that can change during report processing

A report title can be static or dynamic. A static title is fixed at design time, that is while designing the report. It remains unchanged and, therefore, every time you generate the report, it will have the same title.

An example of a fixed title would be "Monthly Customer Invoices" for a report that shows all invoices for a specific customer in a month.

In many cases, you may need every copy of your report to have some copy-specific words in its title. For example, if you are generating a copy of the "Monthly Customer Invoices" report, you may like to display the name of the customer in the title. This will make your title dynamic.

In this recipe, I will show you how to generate dynamic titles, which have some words that are not fixed while you are designing the report. JasperReports will decide these copy-specific words every time you generate a copy of your report.

Getting ready

In order to work with this recipe, you need to have a relational database installed on your computer. I prefer a simple and easy-to-use open source database, which makes a small footprint on your PC. That's because I am providing sample data with this recipe, so I would like to make it as easy and quick as possible for readers to import the sample data into their databases before executing the steps of this recipe.

I have used the PostgreSQL database, which fulfills these criteria. You can visit the web site (http://www.postgresql.org/) and get all the information to install and start using PostgreSQL. You may also refer to the installPostgreSQL.txt file included in the source code download for this chapter, which shows how you will install and run PostgreSQL. Make sure that PostgreSQL is up and running before you proceed.

The source code for this chapter also includes a copySampleDataIntoPGS.txt file, which will help you create a table named CustomerInvoices with five columns ( InvoiceID, CustomerName, InvoicePeriod, ProductName, and InvoiceValue) and copy sample data for all the recipes of this chapter into the table.

After inserting the required data, you will connect iReport to your database hosted on PostgreSQL using the steps described in the Creating a report from relational data recipe later in Chapter 4.

How to do it...

  1. Open the DynamicTitle.jrxml file from the Task4 folder in the source code for this chapter. The Designer tab of iReport shows an empty report with a simple main title (Monthly Customer Invoices) as shown here:
    How to do it...
  2. You are about to insert a dynamic subtitle into your report. Drag a new Text Field from the palette of components available on the right of the main iReport window and drop it into the Title section of your report, just below the main title.
  3. Right-click on the Parameters node in the Report Inspector window on the left of the Designer tab. Choose Add Parameter from the pop-up menu.
  4. The Parameters node will expand to show all the parameters including the newly added parameter named parameter1, which appears at the end of the list of parameters. Select parameter1 from the expanded view. You will see the properties of this parameter in the Properties window on the right of the Designer tab just below the palette of components.
  5. Click on the Name property of the parameter and type CustomerName as its value. This sets CustomerName as the name of this parameter. Similarly, click on the Default Value Expression property of the parameter and type"" (two double quotes) as its value. Leave the rest of the properties at their default values.
  6. Link the text field you dragged-and-dropped in step 2 earlier with the CustomerName parameter. For this purpose, right-click on the text field and select the Edit expression option from the pop-up menu. An Expression editor window will open as shown below:
    How to do it...
  7. Delete the default expression ($F{field}) from the Expression editor window.
  8. Click on Parameters in the first column of the lower half of the Expression editor window. This will list all the parameters in the adjacent second column. Scroll down the list of parameters in the second column to find the CustomerName parameter. Double-click on it. This will set $P{CustomerName} in the expression editor, as shown below:
    How to do it...
  9. Click the Apply button. This will attach the CustomerName parameter with your text field of step 2.
  10. Now open the Report query window by clicking the Report query button to the right of the Preview tab. Unfortunately, it is a bit difficult to find this button in iReport 3.6.0, as it shows no tool tip when you move your mouse over it. Its icon is similar in shape to a standard database icon and looks like A Report query window will appear, as follows:
    How to do it...
  11. Enter the following SQL query into the Report query window:
    SELECT * FROM "public"."CustomerInvoices" WHERE "public"."CustomerInvoices"."CustomerName" = $P{CustomerName}
    
  12. The Report query window will automatically fetch all the columns of the CustomerInvoices table (that is, InvoiceID, CustomerName, InvoicePeriod, ProductName, and InvoiceValue) and show them in the lower part of the Report query window, as shown next:
    How to do it...
  13. Click the OK button at the bottom of the Report query window.
  14. Your text field of step 2 is now fully dynamic. Switch to the Preview tab to see your dynamic title. As soon as you switch to the Preview tab, the Parameter prompt dialog will appear. This dialog will ask you for the name of the customer, as shown n the following screenshot:
    How to do it...
  15. Type Packt Publishing as the name of the customer and click OK. You will see a dynamic title, as shown next.
    How to do it...

How it works...

Open the XML tab of your report and you will see the following JRXML code:

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport>
<parameter name="CustomerName" class="java.lang.String">
</parameter>
<queryString>
<![CDATA[SELECT * from "public"."CustomerInvoices"
WHERE "public"."CustomerInvoices"."CustomerName" =
$P{CustomerName}]]>
</queryString>
<!-- other JasperReports XML tags -->
<title>
<band height="79" splitType="Stretch">
<staticText>
<!-- other JasperReports XML tags -->
<text><![CDATA[Monthly Customer Invoices]]></text>
</staticText>
<textField>
<reportElement x="113" y="49" width="128" height="20"/>
<textElement/>
 <textFieldExpression class="java.lang.String">
<![CDATA[$P{CustomerName}]]>
</textFieldExpression>
</textField>
<!-- other static text and text field XML tags -->
</band>
</title>
<!-- other JasperReports XML tags -->
</jasperReport>

You can see that this JRXML contains<parameter>, <queryString>, and<title> tags in expanded view, whereas all other tags that are not relevant to the present discussion of the recipe are omitted.

iReport has generated the<parameter> tag (shown highlighted in the code) in response to steps 3 and 4 of the recipe. In these steps, you configured the CustomerName parameter. The<parameter> tag wraps the name of the parameter (CustomerName) and the type of data (that is, a Java string) through its name and class attributes, respectively.

Now look at the<queryString> tag (shown highlighted in the code), which wraps the SQL query you authored in step 11 of the recipe to fetch table data from your database.

Also notice from the SQL query that it contains a string $P{CustomerName} on the right of the = sign in the Where clause. This $P is not SQL syntax. This $P will be internally processed by the JasperReports engine, which resolves it to the CustomerName parameter before executing the query.

This way, the SQL query becomes part of your JRXML code and the JasperReports engine will process and execute this query to fetch the customer name from your database. JasperReports will also copy the name of the customer into the CustomerName parameter while processing your report.

Now look at the<staticText> tag whose<textFieldExpression> child is shown highlighted in the code. This<textFieldExpression> tag specifies the parameter (CustomerName) that contains the name of the customer fetched from the database. Therefore, when JasperReports processes this<staticText> tag, it will show the name of the customer in the title of your report.

Tip

Use the overflow attribute. At runtime, if the title width exceeds the given width, it won't appear.

Also, it is better to externalize the static text into PROPERTIES files so that it will be easy to support multilingual support down the line. Also, it will be easy to fix spelling mistakes or modifications, without touching the report.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.97.75