Jump to content
Changes to the Jaspersoft community edition download ×

SubReports and Custom Datasources


Recommended Posts

By: John Conner - conner_john

SubReports and Custom Datasources

2002-11-13 21:53

Is it possible to pass a custom datasource to sub-report by obtaining the sub-report data source from the parent data source? The example I am trying to get to work is included below (just the relevant portions):

 

I've tried several variations and dug into the code a bit to see if there was something I forgot to initialize. When I run this Parent Report, I can see that the CDS2 is being accessed (via the print statements) however, nothing from the sub-report is showing up in the PDF file when I open it. The parenet report prints two lines just like expect but nothing from the sub-report.

 

As a side note, when I generate just the SubReport compile/fill/pdf and pass it a CDS2 via the JasperFillManager.fillReportToFile, the report prints as expected. Am I trying to do something that Jasper Reports can't support right now or is my code at fault.

 

public class CDS1 implements JRDataSource{

private Object[][] data = {

{java.sql.Date.valueOf("2002-01-01"), "Test 1"},

{java.sql.Date.valueOf("2002-01-02"), "Test 2"}

};

 

public Object getFieldValue(JRField field) throws JRException {

Object value = null;

 

String fieldName = field.getName();

 

if ("dateField".equals(fieldName)) {

value = data[index][0];

}

else if ("description".equals(fieldName)) {

value = data[index][1];

}

else if ("subReportDataSource".equals(fieldName)){

value = new CDS2();

}

 

System.err.println("CD: field["+field+"] value["+value+"]");

 

return value;

}

}

 

public class CDS2 implements JRDataSource{

 

private Object[][] data = {

{"Item 1","Nothing"},

{"Item 2","Nothing"},

{"Item 3","Nothing"},

{"Item 4","Nothing"},

{"Item 5","Nothing"},

{"Item 6","Nothing"},

{"Item 7","Nothing"}

};

 

public Object getFieldValue(JRField field) throws JRException {

Object value = null;

 

String fieldName = field.getName();

 

if ("desc".equals(fieldName)) {

value = data[index][0];

}

 

System.err.println("I am getting["+field+"] value is["+value+"]");

 

return value;

}

}

 

With the following reports:

Section of the Parent Report:

<field name="dateField" class="java.util.Date"/>

<field name="description" class="java.lang.String"/>

<field name="subReportDataSource" class="java.lang.Object"/>

 

<detail>

<band height="20">

<staticText>

<reportElement x="0" y="5" width="50" height="15"/>

<textElement textAlignment="Left"><font size="12"/></textElement>

<text>Date:</text>

</staticText>

<textField isBlankWhenNull="true">

<reportElement x="51" y="5" width="240" height="15"/>

<textElement textAlignment="Left"/>

<textFieldExpression class="java.util.Date">

$F{dateField}

</textFieldExpression>

</textField>

 

<subreport isUsingCache="true">

<reportElement x="0" y="148" width="515" height="20"

forecolor="#FFFFFF" positionType="Float"/>

<dataSourceExpression>

$F{subReportDataSource}

</dataSourceExpression>

<subreportExpression class="java.lang.String">

"SubReport.jasper"

</subreportExpression>

</subreport>

</band>

</detail>

 

Section from SubReport:

<field name="desc" class="java.lang.String"/>

 

<detail>

<band height="20">

<staticText>

<reportElement x="0" y="5" width="50" height="15"/>

<textElement textAlignment="Left"><font size="12"/></textElement>

<text>Description: </text>

</staticText>

<textField isBlankWhenNull="true">

<reportElement x="51" y="5" width="240" height="15"/>

<textElement textAlignment="Left"/>

<textFieldExpression>

$F{desc}

</textFieldExpression>

</textField>

</band>

</detail>

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

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...