Viewing: Accessing Reports and Domains from iReport > Adding an Image and Subreport to the Report Unit

Creating a Report Unit in the Repository from iReport

To add a new report unit, you need a JRXML file, which you can create in iReport beforehand. A report unit has a main JRXML file, a data source, input controls (zero or more), and resources (zero or more).

To create a report unit in the repository:

1.     In the Repository Navigator, shown in See "Server Repository as Seen from iReport", right-click a parent folder for the new report and click Add  JasperServer Report. For example, add the report unit to the Reports folder.

The ReportUnit Wizard appears.

2.     In the Naming step, enter an ID, for example, report_unit_example. No spaces allowed.

3.     In the Name field, enter a meaningful name. For example, enter Report Unit Example. This name will appear in the repository on the server, and consequently, in the Repository Navigator.

4.     In the Description field, briefly describe the report unit. The description will appear in the repository on the server and help users find the report they need.

5.     Click Next.

6.     In the Main JRXML step, locate the main JRXML file in the repository or on your hard drive. You can also specify a report that you have open in iReport.

For example, assume you have a report file named report1.jrxml on your hard drive that you already opened the report in iReport. Select Locally Defined, then click the Get source from current opened report button.

7.     Click Next.

8.     In the Locate Data Source step, select one of the following options:

    From the repository

    Locally Defined

    Don’t use any data source

For this example, select Locally Defined.

9.     Click the Edit local datasource button.

The Data Source dialog appears.

10.     In the Data Source dialog:

On the General tab, enter a unique ID. No spaces allowed. For example, enter MyFoodmartDS.

a. In Name, enter a meaningful name. This name will appear in the repository on the server. For example, enter FoodmartDS.
b. On the Data Source Details tab, select a data source type, for example JDBC Data Source, and fill in the required details:

Driver – org.postgresql.Driver

URL – jdbc:postgresql://localhost:5432/foodmart

User name (for the database) – postgres

Password (for the database)

 

If you defined the JDBC data source in the currently opened report, you can click the Import from iReport button to help provide these details. Select the data source from the drop-down that you specified during report design. Fill in the port number for the database (5432 for example), and click OK

See "Data Source Details" shows typical details for a JDBC data source.

 

Data Source Details

c. Click Save.

11.     In the ReportUnit wizard, click Finish.

 

If the report unit doesn’t appear in the Repository Navigator, right-click the server name (MyServer in See "The New Report Unit in the Repository Navigator") and select Refresh from the context menu.

12.     In the Repository Navigator, expand the Reports folder. The report unit, Report Unit Example, appears:

 

The New Report Unit in the Repository Navigator

Adding an Image and Subreport to the Report Unit

A subreport is a JasperReport that’s embedded within another report: the master report. As the Master report executes, each time the server encounters a subreport element it executes the subreport. The server embeds the subreport content into the output of the Master report. Subreports can process records from a query different from the Master report's query.

To add an image from the repository to a report unit:

1.     Continuing the previous example, expand the Images folder in the Repository Navigator, select the JR Logo image.

2.     Drag the image to the Designer. Drop the image in the page footer band of the open report, report1.jrxml.

The plug-in adds the image element to the report, setting an expression in repo: syntax, which refers to the location of the image in the repository.

See "A Repository Image Added to a Report" shows the selected image file in the Repository Navigator, the image element in the page footer band of the report, and the location expression at the top and bottom of the properties list.

 

A Repository Image Added to a Report

To add a subreport to a report unit:

1.     In iReport, assuming report1.jrxml is still open from the previous procedures, perform these steps:

From the palette, drag a Subreport element to the detail band of the report in the Designer.

The Subreport wizard appears.

d. On the Subreport page of the wizard, select one of these options:

Create a new report

Use an existing report

Just create a subreport element

For this example, assume you have a pre-existing and compiled JRXML file named subreport.jrxml. Select Use an existing report.

e. Browse to the jrxml version of the report, subreport.jrxml.
f. Select the .jrxml file. Click Open, then Next.

The Connection expression page of the Subreport wizard appears, offering these options:

Use the same connection as the master report

Fills the master report when the subreport is JDBC-based. The JDBC connection is passed to the subreport for execution.

Use another connection

Sets up a different JDBC connection.

Use a JRDatasource expression

Uses a JRDataSource object to fill the subreport. You will need to write an expression capable of returning a JRDataSource object.

Use an empty data source

Sets the data source expression to new JREmptyDataSource(). This expression creates a special data source that provides a single record with all the field values set to null. Useful for displaying static content in a subreport.

Don’t use any connection or data source

Used for displaying static content in a report.

g. Select the first option, Use the same connection used to fill the master report. Click Next.

The Parameters page of the subreport wizard appears.

 

If the report has parameters, you select the Expression drop-down next to the name of the parameter, the scroll and select the field that contains the parameter.

h. In this example, there are no parameters. On the Parameters page, click Next.

The Subreport expression page of the wizard appears.

i. On Subreport expression page of the wizard, select one of these options:

Store the directory name in a parameter

If you’re creating reports to run or preview in iReport, choose this option.

Use a static absolute path reference

If you’re creating reports to run or preview in iReport, choose this option.

See "A Subreport Expression" shows the Subreport expression page.

For this example, choose the Store the directory name in a parameter option, then click Finish.

 

A Subreport Expression

2.     Stretch the subreport element until it’s wide enough to fill the band.

3.     Select the subreport in the master report’s Report Inspector and edit the size of the subreport:

In the properties panel, edit the print margins. Set the Left and Top properties to zero because a subreport is meant to be a portion of a page, not an entire document.

j. Set the Height property to a very small value, such as 1 pixel. When the subreport element is filled with subreport data, it dynamically expands in height, so you can set its height to a very small value to leave room in the band for other elements.

4.     In iReport, click File Save to save changes to the report.

5.     Synchronize the changes you made in iReport to the Report Unit Example on the server by replacing its Main JRXMLwith the JRXML you just altered in iReport:

In the Repository Navigator, expand the Report Unit Example folder.

k. Right-click the Main JRXML, and select Replace with current document.

The JRXML validation dialog appears. See "JRXML Validation" shows the JRXML validation dialog.

 

JRXML Validation

The server detects that the report unit includes a subreport, and the subreport needs a different expression. In iReport, subreports need this expression:

$P{SUBREPORT_DIR} + reportName.jasper

In the server, subreports need this expression:

repo : reportName expression

l. If the Subreport resource is not selected, click the checkbox to select it, then click continue to upload the subreport and the proposed expression to the repository.

 

When you upload a report to the server that uses the repo: syntax to refer to resources in the repository, the server prompts you to locate the resources for JRXML validation. For more information, refer to the JasperReports Server Ultimate Guide.

A message confirms that the JRXML was updated.

m. Click OK.

In the Repository Navigator, the subreport jrxml appears in the Resources folder of the Report Unit Example.

6.     To run the report from iReport:

Right-click the Report Unit Example folder in the Repository Navigator.

 

You cannot run a subreport alone on the server; you must run the entire report unit.

n. Select Run JasperServer Report from the context menu.

7.     To run the report in the server, first you need to link a data source to the report:

Log into the server.

o. Click View Repository. Select the report unit in the repository.
p. Click Edit.
q. On the Set Up page, upload report1.jrxml.
r. On the Data Source page, link a data source to the report. For example, select the following data source from the repository:

/Analysis Components/Analysis Data Sources/FoodmartDataSourceJNDI