List subreportsList = DJConstants.FOOTER.equals(position)
? columnsGroup.getFooterSubreports()
: columnsGroup.getHeaderSubreports();
for (Iterator iterator = subreportsList.iterator(); iterator.hasNext();) {
Subreport sr = (Subreport) iterator.next();
JRDesignSubreport subreport = new JRDesignSubreport(new JRDesignStyle().getDefaultStyleProvider());
//The data source
int dataSourceOrigin = sr.getDatasource().getDataSourceOrigin();
if (DJConstants.DATA_SOURCE_ORIGIN_USE_REPORT_CONNECTION == dataSourceOrigin){
JRDesignExpression connectionExpression = ExpressionUtils.getReportConnectionExpression();
subreport.setConnectionExpression(connectionExpression);
} else if (DJConstants.DATA_SOURCE_TYPE_SQL_CONNECTION == sr.getDatasource().getDataSourceType()) {
JRDesignExpression connectionExpression = ExpressionUtils.getConnectionExpression(sr.getDatasource());
subreport.setConnectionExpression(connectionExpression);
} else {
JRDesignExpression dataSourceExpression = ExpressionUtils.getDataSourceExpression(sr.getDatasource());
subreport.setDataSourceExpression(dataSourceExpression);
}
// int random_ = subReportRandom.nextInt();
//the subreport design
String paramname = sr.getReport().toString(); //TODO ensure this name is unique among all possible subreports
((DynamicJasperDesign)getDesign()).getParametersWithValues().put(paramname, sr.getReport());
String expText = "("+JasperReport.class.getName()+")$P{REPORT_PARAMETERS_MAP}.get( \""+ paramname +"\" )";
JRDesignExpression srExpression = ExpressionUtils.createExpression(expText, JasperReport.class);
subreport.setExpression(srExpression );
//set the parameters
subreport.setParametersMapExpression(ExpressionUtils.getParameterExpression(sr));
for (Iterator subreportParamsIter = sr.getParameters().iterator(); subreportParamsIter.hasNext();) {
SubreportParameter srparam = (SubreportParameter) subreportParamsIter.next();
JRDesignSubreportParameter subreportParameter = new JRDesignSubreportParameter();
subreportParameter.setName(srparam.getName());
JRExpression expression2 = ExpressionUtils.createExpression(srparam);
subreportParameter.setExpression(expression2);
try {
subreport.addParameter(subreportParameter );
} catch (JRException e) {
log.error("Error registering parameter for subreport, there must be another parameter with the same name");
throw new CoreException(e.getMessage(),e);
}
}
//some other options (cosmetic)
//subreport.setStretchType(JRDesignElement.STRETCH_TYPE_NO_STRETCH);
int offset = LayoutUtils.findVerticalOffset(band);
subreport.setY(offset);
subreport.setX(-getReport().getOptions().getLeftMargin().intValue());
subreport.setWidth(getReport().getOptions().getPage().getWidth());
subreport.setHeight(SUBREPORT_DEFAULT_HEIGHT);
subreport.setPositionType(JRElement.POSITION_TYPE_FLOAT);
subreport.setStretchType(JRElement.STRETCH_TYPE_NO_STRETCH);
subreport.setRemoveLineWhenBlank(true); //No subreport, no reserved space
band.setHeight(offset + subreport.getHeight());
if (sr.getStyle() != null)
applyStyleToElement(sr.getStyle(), subreport);
//adding to the band
if (sr.isStartInNewPage()) {
JRDesignGroup jrgroup = getJRGroupFromDJGroup(columnsGroup);
JRDesignBand targetBand = null;
int idx = getDesign().getGroupsList().indexOf(jrgroup);
if (DJConstants.HEADER.equals(position)) {
// if (idx == 0){
// if (getDesign().getColumnHeader() != null)
// targetBand = (JRDesignBand) getDesign().getColumnHeader();
// else if (getDesign().getPageHeader() != null)
// targetBand = (JRDesignBand) getDesign().getPageHeader();
// else
// targetBand = band;
// }
// else
// targetBand = (JRDesignBand) ((JRDesignGroup) getDesign().getGroupsList().get(idx-1)).getGroupHeader();
}
else { //footer subreport (and concatenated report)
if (idx+1 < getDesign().getGroupsList().size())
idx++;
targetBand = (JRDesignBand) ((JRDesignGroup) getDesign().getGroupsList().get(idx)).getGroupFooter();
}
/**
* There is no meaning in adding a page-break in header sub reports since
* they will be placed right after the group header
*/
if (DJConstants.FOOTER.equals(position)){
JRDesignBreak pageBreak = new JRDesignBreak(new JRDesignStyle().getDefaultStyleProvider());
pageBreak.setKey(PAGE_BREAK_FOR_ + jrgroup.toString()); //set up a name to recognize the item later
pageBreak.setY(0);
pageBreak.setPositionType(JRDesignElement.POSITION_TYPE_FLOAT);
targetBand.addElement(pageBreak);
}
}
band.addElement(subreport);
sendPageBreakToBottom(band);
/**
* A subreport is placed in a group header or footer. This option configures the group's
* header/footer band to allow it contents to be split. I'm not sure splitting logic works
* inside complex object such as sub-reports since it has it's own bands inside
*/
band.setSplitAllowed(sr.isSplitAllowed());
}
}