HOW-TO Add Subreports (fast way)

Useful examples:

Test Description Download PDF
ar.com.fdvs.dj.test.subreport.SubReportTest Creates a single Subreport download
ar.com.fdvs.dj.test.subreport.SubReportBuilderTest Creates a single Subreport download
ar.com.fdvs.dj.test.subreport.ConcatenatedReportTest Creates a single PDF that concatenates 3 reports download
ar.com.fdvs.dj.test.subreport.SubReportRecursiveTest Creates a report that contains a subreport, and the subrerport contains a subreport too download

An easy way to add subreport is through the DynamicReportBuilder class (which internally uses SubReportBuilder, but makes it even easier) The SubReportBuilder class has much more options to setup a subreport, the quick methods added to the DynamicReportBuilder covers what we consider the most common scenarios, but for specific configurations SubReportBuilder should be used.

Look at the example bellow

DynamicReport drHeaderSubreport = createHeaderSubreport();

drb.addSubreportInGroupHeader(2, drHeaderSubreport, new ClassicLayoutManager(),
                "statistics"
                , DJConstants.SUBREPORT_DATA_SOURCE_ORIGIN_FIELD
                , DJConstants.DATA_SOURCE_TYPE_COLLECTION);

drb.addField("statistics", List.class.getName());

The first call to the builder is:

 .addSubreportInGroupHeader( <group number>, <dynamicReport>, <LayOutManager>
                          ,  <path to the data source>, <origin of the data source>, <type of the data source>)

group number: Subreports are placed in the header or footers of a group. This parameter defines which group

dynamicReport: A DynamicReport object to use as a subreport. In this example we create it in the call to createHeaderSubreport()

LayOutManager: Since the subreport is going to be a DynamicReport, a LayOutManager is needed

path to the data source: A string that points to the data source.

origin of the data source: A constant from DJConstants class which can be: SUBREPORT_DATA_SOURCE_ORIGIN_FIELD or SUBREPORT_DATA_SOURCE_ORIGIN_PARAMETER, it just tells the subreport where to look for the data source (the parameters map, or if its a property of the current row)

For example, if path to the data source is "subreportDatasource" and origin of the data source is SUBREPORT_DATA_SOURCE_ORIGIN_PARAMETER, then it will look in the parameters map for an object under the key of "subreportDatasource", on the other hand if we pass SUBREPORT_DATA_SOURCE_ORIGIN_FIELD, in the current row, there must be a property with the name "subreportDatasource" that returns an object of the type DATA_SOURCE_TYPE_<XXX>

type of the data source: A constant from DJConstants class, all types constants has a name like DATA_SOURCE_TYPE_<XXX>, where <XXX> represents the type. Most known types are java.sql.ResultSet, Collection, Array, Xml, etc.

An overloaded way of this method also lets you pass the following:

SubreportParameter[] params An array of parameters (in parent report) to make available in child report.

boolean startInNewPage tells the DJ to make the sub report to start in a new page.

The 2nd call is:

 drb.addField("statistics", List.class.getName());

Because in this example, the origin is DJConstants.SUBREPORT_DATA_SOURCE_ORIGIN_FIELD, there must be such a field in the reports datasource. That is why, registering the field is necessary.

This is a example of a report with 2 different subreports

Chart Report

The SubReportBuilder class

The SubReportBuilder class covers all possible configurations left behind by the DynamicReportBuilder, since the first one is the specific one to do this job.

Builder methods

  • setSplitAllowed(boolean splitAllowed): Default is true. When false, if the subreport does not fit's in the space given up to the end of the page, it will be pushed to start in the next page.
  • setStartInNewPage(boolean startInNewPage): Default is false. When true, a page break is inserted before the subreport, forcing it to start in a new page.
  • setFitToParentPrintableArea(boolean fitToParentPrintableArea): Default is false. When the sub-report is dynamic (and no template), we can tell the report to fit its parent report width.
  • setUseParentReportParameters(boolean useParentReportParameters): Default is false. If true, parent and child report shares the parameters Map. If it is set to false, then setParameterMapPath(...) should be set.
  • setParameterMapPath(String path) and setParameterMapPath(String path, int origin): First assumes origin is relative to parent map, on the second version we can tell the origin is from the parents MAP or from a FIELD (DJConstants.SUBREPORT_PARAMETER_MAP_ORIGIN_PARAMETER or DJConstants.SUBREPORT_PARAMETER_MAP_ORIGIN_FIELD)