Jump to content
JasperReports Library 7.0 is now available ×

SubReports and Custom Datasources


Recommended Posts

By: John Conner - conner_john

SubReports and Custom Datasources

2002-11-13 21:42

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>

 

 

 

 

By: Teodor Danciu - teodord

RE: SubReports and Custom Datasources

2002-11-14 12:56

 

Hi,

 

I don't see you implementing the next() method of

the JRDataSource interface.

I'm sure it is there, otherwise your classes would

not compile.

 

I suspect the subreport behaves like its data source

has no records or it already reached the last record.

 

You could send me all your files to test them myself.

teodord@hotmail.com

 

I hope this helps.

Teodor

 

 

 

 

 

By: John Conner - conner_john

RE: SubReports and Custom Datasources

2002-11-14 13:45

Teodor,

 

Thanks for the reply and the offer to personally assist. When I was pulling the files together to send to you, I was compiling/filling/viewing them one last time to make sure I had the latest versions I had been working with and wouldn't you know it - I saw a mistake that once fixed made it work! If you take a look at the y coordinate for the subreport.reportElement it is set to 148 while the band height is set to 20.

 

Once I changed the band height - everything worked as expected. Guess I just needed a fresh pair of eyes looking at the error output.....

 

Also, sorry for the confusion, I do have a next() method in the data source objects. Just didn't include them for brevity....

 

Chris....

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