Jump to content

Using subreports with parameters


raphy

Recommended Posts

Hello!

I am trying to using custom data sources and subreports together.

 

Situation:

I have two classes named CustomDataSourceParents and CustomDataSourceChildren and work perfectly as individual reports.

 

Problem:

I would like to print a listing of all parents with their own children.

 

Question:

How can I passe the parent id as parameter to my CustomDataSourceChildren class to retrieve all their children.

 

Thank you in advanced for your kindly reply.

Best Regards

Raphy

Link to comment
Share on other sites

  • Replies 9
  • Created
  • Last Reply

Top Posters In This Topic

In the subreport properties window (element properties window actually) there is a table to set subrpeort parameters.

 

Please note that a datasource is a consumable object, you can use only once. You should find a way to reset your custom datasource each time you attempt to fill your subreport or provide a datasource instance for each your records in the main report.

 

Giulio

Link to comment
Share on other sites

giulio wrote:

In the subreport properties window (element properties window actually) there is a table to set subrpeort parameters.

Please note that a datasource is a consumable object, you can use only once. You should find a way to reset your custom datasource each time you attempt to fill your subreport or provide a datasource instance for each your records in the main report.

Giulio

 

Hello,

Thanks for your reply.

How can I reset or provide the subreport's datasource instance for each records in the main report?

Raphy

Link to comment
Share on other sites

Well this is something I think I can help you.

 

I have made a so called "FilterableDTODataSource".

 

As I do also show Master Child Relations within my reports I do put all my SubReports (as JasperReport Objects) and all its CustomDataSources (as FilterableDTODataSource Objects) in a container object that holds these SubReports and DataSources.

 

In the design I made following SubReport-Expression:

 

Code:
$P{DataContainer}.getSubReportByName("MyNeededSubReportNumber1"«»)

 

this method at my DataContainer returns a JasperReport Object by the name that I've given it before.

 

the DataSourceExpression looks like this:

 

Code:
[code]$P{DataContainer}.getDataSourceByName("MyNeededDataSourceNumber1","NameOf TheMatchingFieldInChildDataSourceLikeParentId",$F{Id})

 

This method returns my FilterableDTODataSource that does following things before returning:

gets the datasource from the list in the container

make a clone of this (because of reuse in nested subreports) if needed

call the method "moveFirst" to the cloned datasource

set the attribute "filterFieldName" to the given Name (in my sample "NameOfTheMatchingFieldInChildDataSourceLikeParentId"

set the attribute "filterValue" to the value that is passed into (in my sample the Value of the field "Id" from parent master datasource)

return this FilterableDTODataSource[/ol]

 

the "next" Method in this FilterableDTODataSource filters out the records that fullfill the filter criteria (filterFieldName + filterValue)

 

so I do work up to 13 SubReports with its related SubDataSources... and I do even have nested SubReports that reuse the same DataSource that the upper SubReports already loops through...

 

works great since 2 years with our Server Client Enterprise Application.

 

finally: every a thing of your creativity - JasperReports is therefor the best choice!!! ;)

 

hth + good Luck

C-Box

Link to comment
Share on other sites

Hmmm posting my class is not a good idea because it's included in a complex set of custom classes, so a working sample is impossible. :(

 

But here another simple way to link two custum datasources with subReport using.

 

1.) The parent datasource:

 

Code:
public class ParentData{
private String recordNumber;
// ... all the needed fields
private ArrayList posRecordsList = null;

public void addChildData(ChildData childData) {
if (posRecordsList == null){
posRecordsList = new ArrayList();
}
if (childData!= null && !posRecordsList .contains(childData)){
posRecordsList .add(childData);
}
}

public JRDataSource getPosRecordsList(){
if (posRecordsList == null){
posRecordsList = new ArrayList();
}
return new JRBeanCollectionDataSource( (ArrayList) posRecordsList , false);
}

// the getter and setter for your parentData fields

}

 

2.) the childDataSource (just a simple class)

 

Code:
[code]
public class ChildData{
// ... all the needed fields

// Constructor

// Getter and Setter

}

 

 

3.) where you build up your datasources you must add for each parent all the childs with the "addChildData" method

 

4.) in your SubReport-DataSource Expression you just use following syntax:

 

Code:
[code]$F{getPosRecordsList}

 

5.) the SubReport itself you must pass as Parameter to the fill-method. (you did already passed via parameter as I've seen.)

 

so you can easily link two custom-datasources when you prepare your data and needn't care of getting the right childs to the current parent in the JRXML Design. ;)

 

hth

C-Box

Link to comment
Share on other sites

  • 4 weeks later...

Hello,

Writing affter 4 weeks, please help me to resolve my problem.

I have implemented all classes as you describe in the reply.

Actually I don't know where to put this line:

Code:
$F{getPosRecordsList}

 

Best regards

Raphy

Link to comment
Share on other sites

Hi again,

 

just open your MasterReport in iReport.

select the SubReport-Element.

open the Properties-Dialog for this SubReport-Element.

go to the "Datasource-Expression" and put this line into:

Code:
$F{getPosRecordsList}

compile and voilá that's it.

 

[/ol]

 

hth

C-Box

Post edited by: CBox, at: 2006/10/16 19:45

Link to comment
Share on other sites

Thanks for your reply. All classes functionning correctly. Thanks a lot. But the problem is I couldn't acces directly to the methode:

public JRDataSource getPosRecordsList ()
to retreive the data source array list.

The solution is to call the methode:

public Object getFieldValue (JRField field) throws JRException
and retreive the data source as an objet.

Is it correct?

Best Regards

Raphy

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