Jump to content
We've recently updated our Privacy Statement, available here ×

How to define List<CustomClass> in subDataSet


ratna.utilman

Recommended Posts

Hi,

I am generating pdf using Jasper Report and passing objects from spring boot. And the object is very complex object. I am definig subDataSet as below:

      <subDataset name="detailDTOS" whenResourceMissingType="Key" uuid="88bba007-a9a1-4ddc-815e-ab51a921629a">
        <parameter name="consumptionAdvice" class="com.cons.ConsumptionAdviceTenantReportDTO">
            <defaultValueExpression><![CDATA[$P{consumptionAdvice}]]></defaultValueExpression>
        </parameter>
        <queryString>
            <![CDATA[]]>
        </queryString>
        <field name="electricConsumption" class="com.aus.UtilityConsumptionSummary"/>
        <field name="waterConsumption" class="com.aus.UtilityConsumptionSummary"/>
        <field name="commonElectricConsumption" class="com.aus.UtilityConsumptionSummary"/>
        <field name="commonWaterConsumption" class="com.aus.UtilityConsumptionSummary"/>
        <field name="electricMeterReadings" class="java.util.List"/>
        <field name="waterMeterReadings" class="java.util.List"/>
        <field name="unit" class="com.aus.utilman.bms.model.UnitDTO"/>
    </subDataset>

In fields, electricMeterReadings and waterMeterReadings and Listof objects. And bean definition is as below.

public class ConsumptionAdviceDetailReportDTO extends BaseDTO {    private UtilityConsumptionSummary electricConsumption;    private UtilityConsumptionSummary waterConsumption;    private UtilityConsumptionSummary commonElectricConsumption;    private UtilityConsumptionSummary commonWaterConsumption;    private List<UtilityReadingSummary> electricMeterReadings;    private List<UtilityReadingSummary> waterMeterReadings;

}

And how must I define fields, electricMeterReadings and waterMeterReadings in subDataSet in a way that report knows what type the object is..

I can't understand how to get these two fields into a subDataset that can be visualized in a List element. How do I write the dataset and the list definitition in the JRXML template

Thanks

Link to comment
Share on other sites

  • 2 weeks later...
  • Replies 1
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

You have to pass the List as the datasource to your subdataset, and the define your dataset with the properties of the objects in that list. So if you have a  List<Car>, then your sub data set might be defined with the following fields of a 'Car' - make, model, color, year, mileage, etc. You can leave the 'query' portion of that sub-dataset empty. Make sure you've packaged all of your java source code into a jar, and make the jar available to that report. I've used the Spring Framework at length, but not Spring Boot - you'll need to sort through how your jrxmls can access your jars. You may already have that figured out. Make sure to add an import statement at the top of the report (right after 'properties' at the top'), to make your java code avaiable to your report, 

<import value="com.your.java.source.*"/>

Here's an example of a list of objects being used in a list component. The key to this is the use of JRBeanCollectionDataSource.

<jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">

 <datasetRun subDataset="YourDataSet" uuid="2f451cfe-d424-4475-abcf-404ece8123a6">

<datasetParameter name="param_1">
                            <datasetParameterExpression><![CDATA[$P{your_paremeter_1}]]></datasetParameterExpression>
                        </datasetParameter>
                        <datasetParameter name="param_2">
                            <datasetParameterExpression><![CDATA[$P{your_parameter_n}]]></datasetParameterExpression>
                        </datasetParameter>
 

<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource(YourPopulatedList)</dataSourceExpression>

    </datasetRun>

<jr:listContents height="29" width="572">

 

......

 

To test your code locally, you have to make your jar file available to jasper studio - if you're not familiar, look into adding a dependency/jar to a jasper studio project/workspace. It's fairly straight forward as well. 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...