Jump to content
Changes to the Jaspersoft community edition download ×

Nesting Subreports with nested data sources


2005 IR Help

Recommended Posts

By: Manfred Moser - mosabua

Nesting Subreports with nested data sources

2004-05-04 14:16

Hi!

 

We have a subreport that loops through a recordset. We now need to enhance this to have a subreport within the subreport which displays another recordsset for the current record in the main subreport.

 

So we would have something like this

 

subreport record A

sub subreport record 1 belonging to record A

sub subreport record 2 belonging to record A

sub subreport record 3 belonging to record A

sub subreport record 4 belonging to record A

subreport record B

sub subreport record 1 belonging to record B

sub subreport record 2 belonging to record B

sub subreport record 3 belonging to record B

sub subreport record 4 belonging to record B

 

subreport record C

sub subreport record 1 belonging to record C

sub subreport record 2 belonging to record C

sub subreport record 3 belonging to record C

sub subreport record 4 belonging to record C

 

And so on.

Initially we thought we could define a datasource as a field in the main subreport instead of as a parameter. But that doesnt seem to be allowed from what we can tell from our compiles.

 

Is there any other way to do this sort of nesting of recordsets?

 

Any help would be awesome

 

manfred

 

 

 

 

 

 

 

By: C-Box - c-box

RE: Nesting Subreports with nested data sources

2004-05-04 22:42

Are all your "Sub subreport records" in one datasource?

 

If yes, there should all "Sub subreport records" appear in subreport A? Isn't it? That is what I experienced... it is so because of the global counter "index" within the (for example)JRTableModelDataSource of JasperReports 0.5.2 SourceCode.

 

We solved the problem with introducing a CLONE-Method to that JRTableModelDataSource... and changed the JRBaseFiller of JasperReports 0.5.2 SourceCode a little bit.

So we always work with a copy of our datasource and all the "Sub subreport records" are shown correctly.

 

If you don't have all the "Sub subreport records" in one DataSource it should actually work. But then you need for each SubReport a own DataSource.

 

hth

C-Box

 

 

 

 

By: Manfred Moser - mosabua

RE: Nesting Subreports with nested data sourc

2004-05-05 08:35

Hm... well.

 

At the moment I was thinking of having one datasource for the main sub report -- each record would then have an id in it. e.g. parent_id

 

And the datasource for the sub sub reports would somehow use this parent_id to narrow down the results.

 

Since I dont know how many parent records there will be I can not create the data sources upfront.

 

One approach I was thinking of is to actually return all records in all the subreport but use a PrintWhenExpression to say only print records if the field parentid from the subreport is the same as the field parentid from the subsubreport.

 

In terms of all the records appearing in the first sub sub... we havent tried it. I tried to introduce the datasource as a field for the subsubreport instead of as a parameter so that the datasource would actually change all the time, but my report design didnt compile with that. Seems to be not allowed.

 

Your approach with the clone sounds reasonable. But I would much rather not tinker with the JasperReports source code. There must be a different way? I hope ..

 

Otherwise .. could we get the code?

 

thx

 

Manfred

 

 

 

 

 

 

 

 

 

 

 

By: Ashish - ashisharte

RE: Nesting Subreports with nested data sourc

2004-05-06 08:53

Well I think one way to handle multiple data sources is to actually put all these data sources on the report.

 

For instance, the main report has its own data source.

Then define a parameter which stores the datasources( eg. a Map that has a key-value pair of subreport id-datasource ) for level 1 subreport. Now, for each subreport pass this same map and its datasource.

 

That way whenever you are populating any level of subreport it always has a datasource to populate.

 

Secondly the way you can separate / select each individual subreport data source is to write a Scriptlet which accepts a subreport id, and the subreport map and return the subreport corresponding datasource.

 

Another way to view this design is to consider a bucket of datasources. Each one identifes its subreport and populates itself accordingly.

 

This can be easily done using a HashMap and a Scriptlet. I do it on many of my reports. However, it gets extremely slow and takes a long time to populate the report ( for one of our reports which prints around 87 pages, it takes 35 secs to populate it ).

 

hope this helps.

 

-Ashish

 

 

 

 

By: C-Box - c-box

RE: Nesting Subreports with nested data sources

2004-05-06 03:21

Hello Manfred,

 

it seems that we both want to do the same thing.

I didn't figured out how to build up a relationship between Master and Detail while using CustomDataSources without changing the JasperReportSourceCode.

 

When I debuged my project I recognized that when using SubReports and perhaps even nested SubReports with the SAME CustomDataSource the JasperReports logic doesn't know the relationship between the Master and the DetailData. Because when using direct DataBaseConnection and SQL-Query that everytime a SubReport is called a new ResultSet is given back from DataBase that is the base for the filling routine of the current SubReportCall.. if next MasterRecord comes the SubReport is called again and the SQL-Query returns just a new recordset again... and so on...

 

therefore we had to change the JasperReportsSourceCode.

 

See here for the changes we made:

http://sourceforge.net/tracker/index.php?func=detail&aid=946771&group_id=36382&atid=416706

 

With the "SYSFilterField" and the "SYSFilterValue" we introduced it's now possible to establish the relationship between Master-Detail Datasources.

With the "CloneMethod" we introduced it's then possible to "reuse" one and the same CustomDataSource within nested SubReports because of the intern copy the fillMethod works with.

Note that the changes just are for JRTableModelDataSource.

 

I hope Teodord would reply/see/recognize sometime my feature request and perhaps there is already another solution in current release 0.5.2 (but I didn't found anything)

Also with the scriptlet is no sample available and nobody replies of such questions. I don't know if that would be a solution!?!?

 

hth

C-Box

 

 

 

 

By: Manfred Moser - mosabua

RE: Nesting Subreports with nested data sourc

2004-05-10 11:48

Thanks for all the indications. We got a requirements change so this is not a priority at this stage anymore. I would however be interested to see this sort of feature supported in a more straightforward way without the need to hack the jasper source.

 

manfred

 

 

 

 

By: Mayur - mayuragrawal

RE: Nesting Subreports with nested data sources

2004-05-12 08:30

I have thought of an approach, please let me know if that can work.

 

I will take example of subreport sample that ships with Jasper.

 

Suppose I create one table data source class that contains product and address data sources as instance variables.

 

now I provide 2 getter methods in master data source class to read these instance variables.

 

now I will populate all the master data source records in constructor only and pass this to fillmanager like this.

 

fillmanager.fill("master.jasper",masterdatasourceOBject,null);

 

now i will fill product subreport

fillmanager.fill("product.jasper",masterDatasourceOBject.getProductDataSource(),null);

 

now I will implement next() method of masterDatasource in such a way that

 

class masterDatasource extends IRTable...

{

 

next(){

 

// usual code for pointing to next row of table

productDatasource.populate(currentcityId)

 

}

 

}

 

 

class productDatasource implements IRTable..

 

{

public void populate(cityId)

 

{

 

 

}

 

}

 

This way master datasource

 

 

 

 

 

 

By: Mayur - mayuragrawal

RE: Nesting Subreports with nested data sources

2004-05-12 08:32

I have thought of an approach, please let me know if that can work.

 

I will take example of subreport sample that ships with Jasper.

 

Suppose I create one table data source class that contains product and address data sources as instance variables.

 

now I provide 2 getter methods in master data source class to read these instance variables.

 

now I will populate all the master data source records in constructor only and pass this to fillmanager like this.

 

fillmanager.fill("master.jasper",masterdatasourceOBject,null);

 

now i will fill product subreport

fillmanager.fill("product.jasper",masterDatasourceOBject.getProductDataSource(),null);

 

now I will implement next() method of masterDatasource in such a way that

 

class masterDatasource extends IRTable...

{

 

next(){

 

// usual code for pointing to next row of table

productDatasource.populate(currentcityId)

 

}

 

}

 

 

class productDatasource implements IRTable..

 

{

public void populate(cityId)

 

{

// code to access database and get products for this city

 

}

 

}

 

This way master datasource iteslf will keep detail datasources in sync.

 

Please let me know If somebody has already tried these things and have't waorked.

 

 

 

 

 

 

 

 

By: Manfred Moser - mosabua

RE: Nesting Subreports with nested data sourc

2004-05-14 11:37

as far as I understand that is pretty much what cbox tried first but that didnt seem to work since you get the whole data in the first sub subreport.

 

The problem is really that the data in the sub sub report needs to be speicifc to the field record in the subreport.. and that is different for each sub sub.. so you need sort of a list of datasources... hm

 

 

 

 

By: C-Box - c-box

RE: Nesting Subreports with nested data sources

2004-05-17 01:33

@Mayur

 

It's fundamentally the same idea. We got it to work with implementing a CLONE-Method to a custom DataSourceModel (e.g. JRTableModelDataSource) then we introduced a Parameter "SYSFilterField" and "SYSFilterValue" and modified the NEXT-Method a little bit... and now it works.

 

look here for code-changes:

http://sourceforge.net/tracker/index.php?func=detail&aid=946771&group_id=36382&atid=416706

 

C-Box

 

 

 

 

 

By: Ashish - ashisharte

RE: Nesting Subreports with nested data sourc

2004-05-17 09:46

Mayur,

 

Could you please tell me how long does it take for Jasper to render these type of reports ?

We have similar reports but we are getting terrible performance.

 

-Ashish

 

 

 

 

By: C-Box - c-box

RE: Nesting Subreports with nested data sources

2004-05-18 03:41

@ Mayur,

 

could you send me the sample of your SubReportProject (I guess you changed the sample provided with JasperReport) so that I could check the performance differences to my method?

The solution you found seems to work WITHOUT modifying any jasperSourceCodeLine (what I really prefer against like I did).

 

tia

C-Box

 

 

 

 

By: Mayur - mayuragrawal

RE: Nesting Subreports with nested data sources

2004-05-17 03:15

Thanks C-BOX,

 

I have tried one more approach and that works.

 

In the master report you can actually specify datasource expression like below:

 

<dataSourceExpression>

new MySource($F{City})

<!-- DataSourceCreator.populate($V {subreportName1}$F{City},new java.util.HashMap())

-->

</dataSourceExpression>

 

This expression I used in subreport sample example by modifying MasterReport.xml and removing 'query' from productreport.xml.

 

this way you can support subreport dynamic datasource and report filling based on this dynamic data.

 

In this example we are retrieving all the products for the current city where master record is pointing to.

 

In fact, in datasoure you can provide any java statement that can return JRDatasource instance. for e.g.

 

I am using a datasourceFactory class to populate varoius datasources.

 

 

<dataSourceExpression>

<!-- MySource($F{City}) -->

DataSourceFactory.populate($V {subreportName1}$F{City},new java.util.HashMap())

-->

</dataSourceExpression>

 

 

 

 

 

 

By: Chuck Deal - cdeal

RE: Nesting Subreports with nested data sources

2004-05-19 06:29

This is a very good solution. It does almost the same thing as I was proposing in another thread.

 

I imagine that your DataSourceFactory could be incorporated into the Jasper code. We could define a standard mechanism for creating JRDataSource instances. Something like:

 

public static JRDataSource createDataSource(Class jrDatSourceClassName, Map filterParameters);

 

Of course, we could probably have a couple of variations of the method (a String instead of a Class, etc)

 

Any comments?

 

 

 

 

By: C-Box - c-box

RE: Nesting Subreports with nested data sources

2004-05-19 13:36

@Mayur

 

As far as I understood you wrote your own class that get's a parameter (in your case "$F{City}") and returns a JRDataSource containing all products to that city. But I don't understand the "$V{subreportName1} that isn't separated by commata before your so called "FilterCriteria" ? Did you forgot it to post here or what does the variable "subreportName1" + the parameter "City" together?

And what are you filling into the new created HashMap ("new java.util.HashMap())" ) as second parameter of the populate method?

 

And where does JasperReport knows your class? Do you use it as Scriptlet?

 

I encountered another problem within my method with SYSFilterField and SYSFilterValue and the "Clone" Method... :

 

1.) MasterReport (head data) contains in detailband a SubReport1 (position data)

2.) MasterReport contains in pagefooterband a SubReport2 (contains just one record with the global formfooter-text)

3.) SubReport1 contains in detailband two SubReports (SubReport11 and SubReport12) each is printed depending certain condition with the PrintWhenExpression-Tag (containing multi detail records)

4.) When any of the nested SubReports (11 or 12) causes a pagebreak then on the PageFooter of Page2 doesn't comes the record from SubReport2 because he want's the FilterCriteria (although there isn't required any) if I don't pass REPORT_PARAMETERS_MAP towards the SubReport2 then a <null> is shown, because the index counter is already set to 1 and stands so at last record.

So with passing the ParameterMap from one MasterReport to a SubReport and there to the next SubReport again there seems to be a problem with the filtering.

Note that MasterReport has it's own DataSource (DSMR -> just one record) and SubReport1, SubReport11 and SubReport12 have all together also it's own DataSource (DSSR1 -> multi records) and SubReport2 has it's own DataSource (DSSR2 -> just one record)

 

I can't merge DSMR and DSSR2 because of different contents that are reused within other reports.

 

The only way to solve that problem, is to include also a Field "ID" into the datasource for SubReport2 that is actually not need but is the FilterField on SubReport11 and also on SubReport12.

 

Perhaps you could check your method if it works without defining any filter for the pagefooter subreport.

 

And would you be so kind to send me your sample to C-Box@gmx.de

 

tia & have a nice weekend

C-Box

Link to comment
Share on other sites

  • 7 years later...
  • Replies 1
  • Created
  • Last Reply

Top Posters In This Topic

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