4

I load main report and sub report from *.jrxml and add conditional style to main report and sub report. In main report conditional style work but in subreport it doesn't.

Code

public static JasperDesign getJasperDesign(InputStream isReport) throws JRException {
    JasperDesign jasperDesign = JRXmlLoader.load(isReport);

    // rootStyle
    JRDesignStyle jrDesignRootStyle = (JRDesignStyle) dynamicReportToJasperReportConverter.convertStyle(Template.rootStyle.getStyle());
    jrDesignRootStyle.setName("rootStyle");
    jasperDesign.addStyle(jrDesignRootStyle);

    //creatConditional Style
    JRDesignStyle jrDesignStyle = (JRDesignStyle) dynamicReportToJasperReportConverter.convertStyle(Template.columnStyle.getStyle());
    jrDesignStyle.setName("columnStyle");
    jrDesignStyle.setParentStyle(jrDesignRootStyle);
    JRDesignConditionalStyle jrDesignConditionalStyle = new JRDesignConditionalStyle();
    JRDesignExpression conditionExpression = new JRDesignExpression();
    conditionExpression.setValueClassName(String.valueOf(Boolean.class));
    conditionExpression.setText("$V{REPORT_COUNT}%2 == 1");
    jrDesignConditionalStyle.setConditionExpression(conditionExpression);
    jrDesignConditionalStyle.setBackcolor(Template.valueColor2);
    jrDesignStyle.addConditionalStyle(jrDesignConditionalStyle);
    jasperDesign.addStyle(jrDesignStyle);
    return jasperDesign;
}

In picture you see result and value of REPORT_COUNT:

Result main report and sub report

Java code where generating report.

 JasperReport jasperSubReportIC = TemplateConfigurator.buildSubReport(isSubReportIC, subQueryTO);
        return Template.getReport(reportParameters.getOutputType(),sheetNameInExcel)
                .setTemplate(Template.reportTemplate)
                .title(Template.createTitleHeader(reportName, dateText, bundle, reportContext, reportParameters))
                .lastPageFooter(Template.createFooter(dateText, bundle))
                .setParameter("subreportParameterTO", jasperSubReportTO)
                .setParameter("subreportParameterIC", jasperSubReportIC)
                .setParameter("subQueryTO", subQueryTO)
                .setParameter("subQueryIC", subQueryIC)
                .setParameter("CONTEXT", super.reportContext)
                .setLocale(locale)
                .setResourceBundle(bundle)
                .setTemplateDesign(TemplateConfigurator.getJasperDesign(is))
                .setDataSource(query, DatabaseConnection.getConnection())
                .show()
                .toJasperPrint();

JRXML code where sub report implemented.

<subreport>
            <reportElement x="0" y="0" width="802" height="49" uuid="50369a11-d831-4c54-b16f-95fbed8b5bba"/>
            <subreportParameter name="REPORT_LOCALE">
                <subreportParameterExpression><![CDATA[$P{REPORT_LOCALE}]]></subreportParameterExpression>
            </subreportParameter>
            <subreportParameter name="TO_ID">
                <subreportParameterExpression><![CDATA[$F{TO_ID}]]></subreportParameterExpression>
            </subreportParameter>
            <subreportParameter name="REPORT_RESOURCE_BUNDLE">
                <subreportParameterExpression><![CDATA[$P{REPORT_RESOURCE_BUNDLE}]]></subreportParameterExpression>
            </subreportParameter>
            <dataSourceExpression><![CDATA[$P{dataScriplet_SCRIPTLET}.createDataSourceTO($P{REPORT_CONNECTION},$P{subQueryTO},$F{TO_ID})]]></dataSourceExpression>
            <subreportExpression><![CDATA[$P{subreportParameterTO}]]></subreportExpression>
        </subreport>

1 Answers1

3

You need to run this method on both main and subreport design, styles are not automatically passed to subreport.

You can also not access the subreport design from the main report design see How to get subreport name from JasperReport object (.jasper compiled file) via API?

The easiest way probably is to include another static method in your class YourClass

public static JasperReport getSubreport(String fileName) throws JRException, FileNotFoundException{
    JasperDesign jd = getJasperDesign(new FileInputStream(fileName));
    return JasperCompileManager.compileReport(jd);
}

and in your main report call the subreport with this expression

<subreportExpression class="net.sf.jasperreports.engine.JasperReport">
    <![CDATA[my.package.YourClass.getSubreport("..pathToSubReport..")]]>
</subreportExpression>

The subreport will be loaded into JasperDesign, you apply your styles to it, then compile it to JasperReport and return it as a subreport.

Community
  • 1
  • 1
Petter Friberg
  • 19,652
  • 9
  • 51
  • 94
  • well, I've already tried that, the only difference is that i am adding subreport as parameter, not like result of method. And that does not work for my conditional styles – quento Aug 18 '16 at 13:21
  • @quento, basically you need to let the subreport design go through the same method as main report, if you add subreports as parameter, add it as JasperReport object or as "..pathToSubReport.." – Petter Friberg Aug 18 '16 at 13:25
  • I am doing everything like that, calling same methods for both subreports and main report. I am even getting same fonts, but nothing conditional in subreport appears. – quento Aug 18 '16 at 13:31
  • @quento what is jasperSubReportTO, show how it gets generated, jrxml related to style, calls in java code to add styles, table def etc.. – Petter Friberg Aug 18 '16 at 15:24
  • @quento Sorry I do not know what a "Roman Harmash" is but you could do a [mcve], hence no need to share all code, just show code related to the styles.. it's hard to tell otherwise exactly what your problem is – Petter Friberg Aug 18 '16 at 16:58
  • I mean I am not the creator of the topic – quento Aug 18 '16 at 19:18