Creation of the main report. The subreport element allow to use a report inside another report and it is a very powerful because It allows the creation of very complex layouts with different portions of a single document filled using different data sources and reports. In this tutorial we want to create an address book for each person in the company in a database with the names, the list of phone numbers and the list of email addresses, as shown in the following image:
To do this we will need a main report and two different subreports.
This time we need a datasource more complex, for this reason we need install JasperReports Server because it has some sample databases useful for our purpose.
If you are unsure on which is the correct Jaspersoft Server for your operative system. After that from Jaspersoft Studio start the creation of a new Jasper Report
using the template "Flower Gray" and as report name "ContactsList". When you must select the data adapter click the "New" button, then from the new dialog select
"Database JDBC Connection" and hit next.
Now you must choose a name for your new data adapter, use "ContactsDataset" and configure it with this data:
After this, use the button "Test" to check if the connection works, and in that case hit "Finish".
At this point we are back into the report creation wizard, in the step where we have to select the data adapter. The new data adapter should be selected automatically,
otherwise for the field "Data Adapter" select "ContactsAdapter". As main query uses "select id, name, shipping_address_city from accounts", this one returns the name of a
company and the city where it store is located. Then hit Next.
Finally we can define groups, but we don't need them, so simply hit the button ">>" to add all the fields to the report and hit "Next". Finally we can define groups, but we don't
them, so simply hit the button "Next" and then "Finish". Now the report will be created and it should be similar to the one in the following image:
Creation of the first Subreport. The second step is to create the subreport to display the email addresses. Start a n new report from File->New->Jasper Report, as template choose "Blank A4", save it in the same directory of the master report, and call it EmailReport.jrxml. When we need to choose the data adapter select the "ContactsAdapter" created before and use as query:
select contacts.email1 from contacts, accounts_contacts where accounts_contacts.account_id = 'placeholder' and accounts_contacts.contact_id=contacts.id
In the query is used a static string "placehoder" is used, as suggested by the name this is not a real value but a placeholder that will be changed with a parameter in the next steps. After doing this click "Next".
The query will discover one filed, add it to the report with the button ">>", click next and in the next step another time "Next", since we don't need groups.
Finally hit the button "Finish" and the report will be created. Now select the report root node in the outline view and in the properties tab hit the button "Edit Page Format".
A window to change the page size and margins will appear, reduce the width of the page to 270 pixels and remove the margins, since they are not useful in a subreport, then hit "Ok".
This report will not list any email address because we used a static value as placeolder, but what we want is retrieve just the one of the current person in the master.
To filter the email addresses, we will use a parameter. In the outline view select the report root node and in the property tab hit the button "Edit query, filder and options".
In the new window that will appear go into the tab "Parameters" and click the "Add" button.
A new parameter will be added with the name "Parameter_1", change its name to "ACCOUNT_ID", the type should be String by default and if you want you can add a description.
In query now change the string 'placeholer' with the value $P{ACCOUNT_ID}. Doing this the query result is influenced by a parameter that come from the main report. Then hit "Ok".
The syntax $P{ACCOUNT_ID} allows the use of a parameter inside a query, in this case to filter the result using a "where" condition.
Now drag inside the report, in the band "column header", a Static Text from the palette and change its content to "eMail address" and also drag the field "email1" from the
outline into the "detail" band. Then delete the unused bands (title,page header, column footer, page footer, summary) and resize "column header" and "detail bands" to take
an appropriate space in the main report. You need to obtain layout like the one in the image below:
Save the report and if you want you can run this report to see the result.
Creation of the second Subreport. The procedure to create the second subreport it is almost identical to the first one. Start a n new report from File->New->Jasper Report, as template choose "Blank A4", save it in the same directory as the master, and call it PhoneReport.jrxml. When we need to choose the data adapter select the "ContactsAdapter" created before and as query use:
select phone_work from contacts, accounts_contacts where accounts_contacts.account_id = 'placeholder' and accounts_contacts.contact_id=contacts.id
Add the discovered field to the report, don't define groups and complete the wizard. When the report will be created enter in the "Page Format" dialog and remove the margins and set the width to 270. At this point enter in the "edit query, filter and sort expression" dialog, switch to the tab parameters and add a new parameter "ACCOUNT_ID" and substitute the placeholder in the query with it, as in the image below:
Even this time put a StaticText element with the text "Telephone" in the "Column Header" band and the field "phone_work" in the "Detail" band, delete the unused bands
and eventually resize this two elements and their bands until you get a report as the one in the image below:
The second report is done, save it and if you want compile it to see if it is all right. Now it's time to finish the work on the main report.
Completing the Main Report. The two subreports have been created, now we need to include them in the main report, so open the report "ContactList" and resize the detail band to have enought space to include the subreports, for now use 50 pixels. Now place a subreport element from the palette into the detail band and a wizard will appear. Use the option "Select an existing report" (selected by default) and hit the "Select a report file" button. A window that show all the created reports will appear, select "EmailReport.jrxml" and hit "Ok" and then "Next".
In the following step use the option "Use the same connection used to fill the master report" (which should be selected by defaut). In this way the database
connection will be passed to the subreport to let it execute its SQL query and hit Next. In the next step we can set an expression for the parameter exposed
by the subreport. Hit "Add" to add a new parameter, double click on the name and write "ACCOUNT_ID" and hit the button with three dots on the value field (the
button is visible only if the value field is selected) and the expression editor will be opened. Here from the "Fields" select "id" ny double clicking on it and
hit "Finish". At this point you will return to the parameters dialog that should look as the one in the image below:
Now hit "Finish" and the first subreport will be added to the main report. From the palette drag another subreport and repeat the same procedure using as report
file "PhoneReport.jrxml", all the other fields use the same values. At this point you should have the two subreports into the main report. Resize them to and resize
the detail to obtain this result:
Go in the Preview tab to compile and if it is all right you should see this result (don't mind if the emails are all the same, it is right because they are the same in the used database):