You may need to generate a summary report that collects, processes, and summarizes large amounts of data into a single document. JasperReports allows you to do that.
In this recipe, you will design a monthly billing report for an electricity supplying company. The company has many feeders to supply electricity; each feeder feeds into many transformers, and each transformer serves many electricity consumers. The report of this recipe calculates the sum of monthly bills of all consumers connected to a transformer. Then it gathers the billing data of all transformers of a feeder into a single monthly feeder billing summary.
Refer to the installPostgreSQL.txt
file included in the source code download for this chapter to install and run PostgreSQL, which should be up and running before you proceed.
The source code for this chapter also includes a file named CopyFeederDataIntoPG.txt
, which helps you to create a database named jasperdb8
and two tables named Feeder
and Transformer
. The text file also helps you to copy sample data for this recipe into the tables.
You will be using two JRXML files, MonthlyTransformerBill.jrxml
and FeederSummaryReport.jrxml
, in this recipe. You will find these files in the Task2
folder of the source code download for this chapter. The FeederSummaryReport.jrxml
file is the main report, which uses the subreport named MonthlyTransformerBill.jrxml
. The main report has to refer to its subreport using a complete path (you cannot use relative paths). This means you have to copy the main and subreport JRXML files to the C:JasperReportsCookBookSamples
folder on your PC. I have hardcoded this complete path in the master report (FeederSummaryReport.jrxml)
.
Let's discover how to work with a multi-level summary report by going through the steps of the recipe:
MonthlyTransformerBill.jrxml
file from the C:JasperReportsCookBookSamples
folder on your PC. The Designer tab of iReport shows an empty report.
SELECT SUM("Transformer"."Bill") FROM "Transformer"
The lower part of the Report query window will show a sum field. Click OK.
0
as its value. 0
as the value for their Band height properties. The report in the Designer tab will look as shown: TransformerID
as its value. The name of the parameter1 parameter will change to TransformerID. java.lang.Integer
as its value. 0
as its value. Leave the rest of the parameter properties at their default values. BillingMonth
as its value. The name of the parameter will change to BillingMonth.""
as its value.WHERE "Transformer"."TransformerID" = $P{TransformerID} AND "Transformer"."Month" = $P{BillingMonth}
The complete expression in the editor will become:
SELECT SUM("Transformer"."Bill") FROM "Transformer" WHERE "Transformer"."TransformerID" = $P{TransformerID} AND "Transformer"."Month" = $P{BillingMonth}
Click OK.
2001
as the value and click OK. Another parameter dialog will appear, which will ask you to enter the value of the billing month. Type Jan-10
as the value and click OK. The preview will appear, which will show just a figure, as shown next. The figure is actually the sum of the bills of all consumers connected to a transformer. Your MonthlyTransformerBill.jrxml
subreport is now ready. FeederSummaryReport.jrxml
file from the C:JasperReportsCookBookSamples
folder on your PC. The Designer tab of iReport shows a report containing data in the Title, Page Header, Column Header, and Detail 1 sections, as shown in the following screenshot: 1001
as the value of the FeederID parameter. It will display a list for transformers of the specified feeder: Add Parameter
option. The Parameters node will expand to show the newly added parameter named parameter1
at the end of the parameters list. Select parameter1
from the list; its properties will appear in the Properties window below the Palette. Click on the Name property of the parameter in the Properties window and type BillingMonth
as its value. The name of the parameter will change to BillingMonth
.""
as its value. MonthlyTransformerBill.jrxml
subreport file located in the C:JasperReportsCookBookSamples
folder and click the Open button. The browser dialog will disappear. Click the Next button in the Subreport wizard dialog. TransformerID
and BillingMonth
parameters of the MonthlyTransformerBill.jrxml
subreport and allow you to enter an expression to map these parameters to elements (that is, Fields or Variables) of the main FeederSummaryReport.jrxml
report. FeederSummaryReport.jrxml
report in the form of a drop-down list. Select the TransformerID field from the list. BillingMonth
parameter. A drop-down list will open; select BillingMonth
parameter from the list. Click the Next button. section.
19
as its value. The size of the subreport element will become appropriate according to the size of the other field placed in the Detail 1 section. Use your mouse to align this subreport element in the Detail 1 section, as shown in the following screenshot: 1001
and Jan-10
as value of feeder ID and billing month parameters. The report will show bill values for all transformers of the specified feeder for the particular month, as shown in the following screenshot:You have done two main things in this recipe. First you designed a subreport in steps 1 to 19. The subreport shows only one figure in its view, as shown in the screenshot for step 19. The figure represents the total bill of all customers connected to a particular transformer of the electricity supply company.
Notice from the SQL query of step 3 that it has a SUM
function. The subreport uses the SUM
function of SQL to calculate the sum of all bills.
Then in step 20, you opened a main report named FeederSummaryReport.jrxml
and later in steps 26 to 33 you inserted the MonthlySummaryReport.jrxml
subreport in the Detail 1 section of the main report. Anything that resides inside the Detail 1 section gets processed once with every record. Each record in the main report represents a transformer of the electric supply company. Therefore, the main FeederSummaryReport.jrxml
report gets a copy of the subreport for each transformer.
Recall from steps 30 and 31 of the recipe that you mapped TransformerID
and BillingMonth
parameters of the main FeederSummaryReport.jrxml
report to the same parameters of the MonthlyTransformerBill.jrxml
subreport. This means the main FeederSummaryReport.jrxml
report will pass the transformer ID and billing month to the subreport. The subreport will provide the total bill of all consumers connected to that transformer for the particular month. This way, the main FeederSummaryReport.jrxml
report passes all transformer IDs one by one to the subreport, with the same billing month every time, and gathers the bills of all transformers of the particular feeder into the monthly feeder billing summary.
You selected the Store the directory name in a parameter option in step 32. As a result, iReport stored the path to the MonthlyTransformerBill.jrxml
subreport file in a parameter named SUBREPORT_DIR
, which is useful whenever you need to relocate your subreport files (especially if there are multiple subreports in a master report).
Relocating subreports simply requires changing the value of the SUBREPORT_DIR
parameter, which you do directly from the Report Inspector window. For this purpose, you will first click on the SUBREPORT_DIR
parameter in the Parameters node from the Report Inspector window. Then you will find the Default Value Expression property from the Properties window and provide a path to the directory where your subreport files are located. For example, if your files are located in a directory named JasperReportsCookBookSamples
on your C:
drive, the value of the Default Value Expression property should be C:JasperReportsCookBookSamples
.
Especially notice that the expression is given in double-quotes ("") and it uses double backslashes (\) in the directory path notation.
3.133.154.64