Jump to content
Changes to the Jaspersoft community edition download ×

Connecting a report to its subreport


Recommended Posts

By: Eddie Dimond - dimondeddie

Connecting a report to its subreport

2003-11-29 11:51

I have seen the question I am asking here in other postings in both this forum and the Help forum for JasperReports but, none of those posts have been answered so I am going to ask the question again.

 

I have a report that contains a subreport. The main report lists information by date for sub-companies within our corporation. I use the <Group element to create groups for each company and I placed the subreport in the group footer. The <Group tag has the isStartNewPage="true" set so I get new page after the group is printed. Before the page changes, I want the subreport to print the information for the current company at the bottom of the date information of the main report.

 

If I implement the subreport using a <QueryString element, pass it a jdbc connection and a parameter that is used in the where clause to select the subreport data, the subreport works as it should and two lines of extra information for the company are printed.

 

The problem begins when I attempt to use a JRDataSource instead of a <queryString> tag in the subreport. The JRDataSource is a custom class very similar to the CustomDataSource class in the datasource sample. It holds the information in an array of objects which it iterates over and returns values upon request from the getFieldValue method. When the report is built, filled and viewed the subreport now prints out all of its information (for all companies) after the first page of data is printed. None of the subsequent pages have any subreport data. I have been through the entire help and open discussion forums looking for clues to this and have found none. The subreport example does not address it either. (A second example with JRDataSources would be extremely helpful and would probably answer my questions).

 

What I (and others) have failed to understand from the samples and documentation is how do I link the subreport to the main report when using a JRDataSource in the subreport?

 

To put it another way:

If the subreport example were re-written using JRDataSources instead of <queryString> tags with $P{City} parameters modifying the Where clause of the SQL, what would the solution look like? What would the JRDataSource classes look like? Would the two subreports have to be changed?

 

Please respond as soon as you can. I have report files and java code if that will help clarify my question.

 

Regards.

 

 

 

 

 

By: Kristian Eric R. Maglalang - kazier

RE: Connecting a report to its subreport

2003-11-30 21:38

I agree.

 

I also can't make subreports work using datasources instead of db connections.

 

A subreport (using datasource) example would really help.

 

 

 

 

By: Teodor Danciu - teodord

RE: Connecting a report to its subreport

2003-12-01 00:32

 

Hi,

 

I suspect that in your <dataSourceExpression> for

the subreport you are passing "the same" data source

instance for all companies.

The first time it works, but begining with the second

subreport, the pointer insinde your custom data

source has already reached the last record and for

the subsequent subreports it would appear like there

are no records in the data source, so by default they

do not print anything.

 

If you want to see your subreports generating some

of their sections even if there are no records in their

data source, use whenNoDataType="AllSectionsNoDetail"

in your subreport template.

 

But as I said, I guess the problem resides in the way

you use your custom data source. If it were for me,

I would wrap my array data in a new instance of my

custom data source class every time I pass it to the

subreport, or at least I would improvise a way to

reposition the cursor on the first record.

 

I hope this helps.

Teodor

 

 

 

 

 

By: Eddie Dimond - dimondeddie

RE: Connecting a report to its subreport

2003-12-01 10:23

Teodor,

 

Thank you for your timely reply.

 

Your analysis of what is happening is correct but I'm not certain that you understand my goal with this report (probably because I didn't explain the logic of my report clearly so I will try in this reply).

 

The main report contains data (from a JRDataSource object) that pertains to companies within our corporation. The companies all have a numeric CompanyCode that I use to create a <group> which I set the isStartNewPage="true" attribute so that I get a new page for each company. The subreport contains forecast data from another table in our database that is organized by the same CompanyCode. I have placed the subreport definition in the <groupFooter> section of the <group> definition where I would like it to print the forecast data for the current company only. I am passing the subreport a CompanyCode paramater. When I use a <queryString> in the subreport and add a "where table.companyCode=$P{CompanyCode}, the subreport prints only the current company's forecast data after it has printed the main reports data.

 

Because of other restrictions and requirements, I need to duplicate this same behavior with a subreport driven by a JRDataSource object. I cannot see how to pass that CompanyCode parameter to the subreport so that I can access it in my JRDataSource class and select only the current company's data. I have not seen an explanation or example of anything like this.

 

This is a very common type of business report, similar to the Invoice Header and Invoice Line Item examples so common in SQL texts. It is also very similar to the subreport example you have on your website. The City parameter that you pass to your subreports is used in exactly the way I use my CompanyCode. Have you ever thought of how you would write that report using JRDataSource objects instead of <queryStrings> in you subreports? That example solution would show me and others how to use subreports with JRDataSource objects.

 

Thanks again for you reply.

 

Regards,

 

Eddie

 

 

 

 

By: Carlos Costa e Silva - carloscs

RE: Connecting a report to its subreport

2003-12-03 21:28

I imagine something like this:

 

 

class MasterSource implements JRDataSource {

 

// whatever: getFieldValue and next

 

public Object getCurrentFilter();

}

 

 

 

class SubReportSource implements JRDataSource {

 

MasterSource master;

 

Iterator iterator;

 

public SubReportSource(MasterSource masterSource) {

master = masterSource;

}

 

public boolean next() {

if (iterator == null) {

refreshIterator();

}

 

boolean result = iterator.next();

if (!result) {

iterator = null;

}

return result;

}

 

private void refreshIterator() {

iterator = ... ; // create iterator with current master info

}

}

 

This implementation depends on next being always called until false is returned.

 

More ways,

 

Subdatasources cache the master filter object(s) and if they change refresh the iterator.

 

Using listeners:

 

class MasterSource implements JRDataSource {

 

 

public void addListener(Listener listener) {

//

}

 

public rowChanged() {

// notify listeners

}

}

 

 

But IMO, this would be a lot cleaner:

 

interface JRSubDataSource extends JRDataSource {

 

public void initialize();

 

public void clean();

}

 

The subreport would call the initialize and clean methods, and there's no hacking in the subdatasources.

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