Jasper sub-reports of sub-reports - programmatically - jasper-reports

There are many examples of running sub-reports in Jasper on the web, but nobody seems to have had the problem that I have, when running a sub-report that contains another sub-report.
We store our report definitions in a Relational Database, and generate and run the reports out of a java service. We only use the GUI to design and test each report, to start with.
The reports are compiled and filled at runtime, and I can cause a sub-report to run, by using the well documented feature of adding the compiled sub-report to the main report at fill time using the report expression to use a sub-report as a parameter.
That is all fine. However, how can I fill the first sub-report with the compile output of the next sub-report down the tree?
To explain a little further, in JasperStudio, I can do this in the main report:
<subreport>
<reportElement x="4" y="100" width="547" height="310" uuid="f9364882-a530-475d-97af-8d6d2d47ae57"/>
<subreportParameter name="INSP_ID">
<subreportParameterExpression><![CDATA[$F{jobid}]]></subreportParameterExpression>
</subreportParameter>
<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
<subreportExpression><![CDATA["InspectionFrogSubReport.jasper"]]></subreportExpression>
</subreport>
Then in the first sub report, to refer to the next one, I can do this:
<subreport>
<reportElement x="280" y="56" width="270" height="294" uuid="b4cbe376-1b54-471c-a6d4-0d45afeab2c8"/>
<subreportParameter name="FROG_ID">
<subreportParameterExpression><![CDATA[$F{frog_id}]]></subreportParameterExpression>
</subreportParameter>
<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
<subreportExpression><![CDATA["InspectionDamageSubReport.jasper"]]></subreportExpression>
</subreport>
So, when I preview the mian, it fills the first sub, and then the next sub, no problem.
However, to fill from Java method, I change the main sub-report expression to:
<subreport>
<reportElement x="4" y="100" width="547" height="310" uuid="f9364882-a530-475d-97af-8d6d2d47ae57"/>
<subreportParameter name="INSP_ID">
<subreportParameterExpression><![CDATA[$F{jobid}]]></subreportParameterExpression>
</subreportParameter>
<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
<subreportExpression><![CDATA["$P!{SUB1}"]]></subreportExpression>
</subreport>
Then in my code:
// Compile sub report
InputStream childStream = new ByteArrayInputStream(subReport.getChildXml().getBytes());
JasperReport compiledChild = JasperCompileManager.compileReport(childStream);
// Compile Main report
InputStream xmlStream = new ByteArrayInputStream(report.getXml().getBytes());
JasperReport compiledReport = JasperCompileManager.compileReport(xmlStream);
// Build report parameter map
Map<String, Object> params = new HashMap<>();
params.put("SUB1", compiledChild);
// Fill main report
JasperPrint jasperPrint = JasperFillManager.fillReport(compiledReport, params, conn);
so far, so good. However, how do I pass the sub-sub-report into the sub-report. I cannot use 'fillReport' as the sub report will have no master record to work on until the main report runs.

Pass subSubReport as a param to supReport.
in java code:
params.put("SUB_SUB_REPORT", compiledSubSubreport);
in main report template xml:
<subreportParameter name="SUB_SUB_REPORT">
<subreportParameterExpression><![CDATA[$P{SUB_SUB_REPORT}]]></subreportParameterExpression>
</subreportParameter>
this is what you want?

Related

Jasper Reports subreport text alignment is wrong only when in main report

When I preview my jasper subreport, text is aligned to the right (what I want).
But when it is used in the main report, text is aligned to the left
On both reports, per element styling is used, set in Jasper Studio.
The jrxml per element looks like...
<textElement textAlignment="Right" markup="html">
<paragraph leftIndent="2"/>
</textElement>
<textFieldExpression><![CDATA[$F{col_1_initials}]]> </textFieldExpression>
The element including the subreport looks like
<band height="161" splitType="Stretch">
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
<subreport isUsingCache="false" runToBottom="false">
<reportElement x="-20" y="0" width="595" height="161" uuid="4e6fb330-c43f-4d47-b7aa-6884dad63d14"/>
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("weekPairs")]]></dataSourceExpression>
<subreportExpression><![CDATA["weeksubreport.jasper"]]></subreportExpression>
</subreport>
</band>
I'm not using report Style Templates because for a reason I can't diagnose, Jaspersoft Studio crashes instantly trying to assign a style template.
What about the main report ovverides the subreport text-aligment and what would I have to change in the source to fix this?
EDIT
In the report designer->properties of a jrxml file there is a checkbox for "Derive Attributes". I've tried that both on and off, but nothing changed.

JasperServer dynamic Band Height as Subreports have different heights

I have a jrmxl report that uses different subreports with different heights, one is 421 and the other one is 600.
Is it possible for the report to dynamically set the band height on each page of the PDF that it generates?
<detail>
<band height="421">
<subreport>
<reportElement x="0" y="0" width="297" height="421" uuid="a23ff576-6d38-4582-a7ea-18e18926136c"/>
<subreportParameter name="NUMBER">
<subreportParameterExpression><![CDATA[$F{number}]]></subreportParameterExpression>
</subreportParameter>
<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
<subreportExpression><![CDATA[$F{report}.trim()]]></subreportExpression>
</subreport>
</band>
</detail>
If your subreports need to produce pages of different sizes in the same resulting PDF document, then you need to use your subreport templates as parts in a part-based JRXML, AKA book report, as opposed to the usual band-based JRXML.
You can find a book report sample in our project distro under the /demo/samples/book folder.
Make sure you use JR Lib version 6.0.0 or newer.

Grouping by parameter value in jasper

Well, I don't know, maybe I'm missing something, but I've been trying to group data in a jasper report, but so far the grouping appears only if the group expression is a field. Is it possible to group data by parameter value instead of field value? i.e., something like
<groupExpression><[!CDATA[$P{some_param}]]></groupExpression>
instead of
<groupExpression><[!CDATA[$F{some_field}]]></groupExpression>
in the .jrxml file?
Is it possible to group data by parameter value instead of field value?
Yes, it is, just like you mentioned. You would have no syntax errors and the report would be generated.
The problem is that it may not bring what you're expecting. The group bands are usually printed every time the "groupExpression" changes. So basically this parameter needs to be associated with something that will change during the report generation (for example, filling a parameter of a subreport with a field in way that this subreport uses this paramater as a group expression). And of course, it needs to be associated with something that makes sense and bring you the desirable behavior.
You can have something like this in your subreport:
...
<parameter name="START" class="java.util.Date"/>
<parameter name="END" class="java.util.Date"/>
...
And something like this in a detail band of your "super" report:
<subreport>
<reportElement x="0" y="10" width="555" height="200" uuid="ac2c99da-f595-4498-a518-2bfb1f31b73c"/>
<subreportParameter name="START">
<subreportParameterExpression><![CDATA[$F{start}]]></subreportParameterExpression>
</subreportParameter>
<subreportParameter name="END">
<subreportParameterExpression><![CDATA[$F{end}]]></subreportParameterExpression>
</subreportParameter>
...
</subreport>
Just notice I'm assuming the fields $F{end} and $F{start} are also "java.util.Date" objects.

Make lines with iReport?

How can I do this with iReport (using iReport v4.0)?
Here is jrxml file
it depend on the size of Sub-report, check the sub-repot preview, if you want to increase the size of sub-rport in main report , for this you have to increase the size of sub-reportas where it is separately created.
You could try using background band for those lines.
Maybe inside your parent report
When you create a sub-report you can delete; Page Header, Column Header and Page Footer bands on your main report. After then, you can drag and drop sub-report icon on Summary part of your main report. You will probably use ColumnHeader and Detail bands on your Sub-Report. (It depends on your design. If you use subreports recursively, you can use again summary band.) Later,you will select Remove Report Margins on your subreport. To get expected result; your subreport icon Width on main report should equal your subreport page width.
Adding borders worked great for me. Expand your text element to cell size, right click -> Padding and Borders and add them all around. It's easier than having to handle draw lines. Less elements and faster editing.
You can add lines via the iReport Designer gui interface using the Palette (go to Window > Palette, then click Line, then drag it over to the Designer).
You can also add lines by modifying the jrxml code directly. I haven't found guidance in the documentation on this. But this answer to another question shows how to create a vertical line:
<line>
<reportElement x="0" y="0" width="1" height="30"/>
</line>
And it also shows how to create a horizontal line:
<line>
<reportElement x="0" y="0" width="30" height="1"/>
</line>
Also, the JRXML Sources and Jasper Files section of the documentation has some more involved code such as the following, which uses a direction parameter with value BottomUp (there are also references to TopDown in the community site or here):
<line direction="BottomUp">
<reportElement key="line" x="1" y="4" width="554" height="1"/>
</line>
And this, which uses positionType parameter of FixRelativeToBottom:
<line>
<reportElement positionType="FixRelativeToBottom" x="0" y="51" width="555"
height="1"/>
</line>

How to create sub-reports using XML DataSources in JasperReports?

I'm using iReport and I need to create a sub-report using a XML DataSource.
I will have only one XML for the hole report. Something like this:
<question>
<text>What do you think about SO?</text>
<options>
<option>Like it</option>
<option>Really like it</option>
<option>Love it</option>
</options>
</question>
The main report will have it's detail linked to the questions, each question will have many options. Each sub-report must be linked to the options for the question... Well master-detail.
All that I could find with some googling was using SQL, I want to use XPath.
Take a look at the JRXmlDataSource JavaDoc. There are example that show how it's done.
Create your subreport as a report first using the xpath. Then create a subreport element in the parent report and link the subreport to the parent report.
below is a sample subreport element:
<subreport>
<reportElement x="0" y="20" width="555" height="100"/>
<subreportParameter name="XML_DATE_PATTERN">
<subreportParameterExpression><![CDATA[$P{XML_DATE_PATTERN}]]></subreportParameterExpression>
</subreportParameter>
<subreportParameter name="XML_DATA_DOCUMENT">
<subreportParameterExpression><![CDATA[$P{XML_DATA_DOCUMENT}]]></subreportParameterExpression>
</subreportParameter>
<subreportParameter name="XML_LOCALE">
<subreportParameterExpression><![CDATA[$P{XML_LOCALE}]]></subreportParameterExpression>
</subreportParameter>
<subreportParameter name="XML_NUMBER_PATTERN">
<subreportParameterExpression><![CDATA[$P{XML_NUMBER_PATTERN}]]></subreportParameterExpression>
</subreportParameter>
<subreportParameter name="XML_TIME_ZONE">
<expressionistic><![CDATA[$P{XML_TIME_ZONE}]]></subreportParameterExpression>
</subreportParameter>
<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
<subreportExpression class="java.lang.String"><![CDATA[$P{SUBREPORT_DIRECTORY} + "PS_Product_Match.jasper"]]></subreportExpression>
</subreport>