Jump to content
We've recently updated our Privacy Statement, available here ×

Subreports and using a Custom Data Source


2005 IR Help

Recommended Posts

By: Sean Barenz - forceinfinity

Subreports and using a Custom Data Source

2004-06-21 16:38

I have a situation where I'm trying to use a subreport using a custom data source. I currently have no problem creating the master reprot using the custom datasource. The problem is trying to get the proper data into the subreport and I'm at a loss as to how to do this.

 

On the left side, I'd have something like employee information, and on the right side on the same band, I'd have one or more line items for that employee. This could be something like pay history, days worked, etc.

 

The conceptual idea is to use a subreport while passing in a datasource. I know I'll need to use a Data Source expression to the subreport. I'm at a loss as to how to populate that datasource and punch it into the subreport so that it'll output those line items for that employee and only that employee, and then have this repeated correctly for all the employees within the report. Report would end up looking remotely like this

 

Name ID Pay Line 1

Addr 1 Pay Line 2

Addr 2 Pay Line 3

City State Zip ...

 

Can anyone give me an idea on how I could go about doing this?

 

TIA

 

 

 

 

By: C-Box - c-box

RE: Subreports and using a Custom Data Source

2004-06-21 22:25

Well you will have to do the following steps:

 

1.) load and compile your MasterReport and also your SubReport each into a JasperReportObject (e.g. "MasterReport" and "SRsub1")

2.) create your CustomDataSource for your MasterReport (e.g. "DSMaster") and also for your SubReport (e.g. "DSsub1") into a JRDataSource-Object (there are different forms e.g. JRTableModelDataSource)

3.) create a HashMap for your parameters (e.g. "paramMap")

4.) put your SubReport-Object (e.g. "SRsub1") into the HashMap

5.) put your CustomDataSource-Object (e.g. "DSsub1") into the HashMap

6.) fill your MasterReport with using the MasterReport-Object (e.g. "MasterReport"), your HashMap (e.g. "paramMap") and your CustomDataSource for your MasterReport (e.g. "DSMaster")

7.) in your XML-Layout for your MasterReport you will need to create a parameter with the name of the SubReport and also for it's CustomDataSource (e.g. "SRsub1" and "DSsub1")

8.) in the subreport-section of the MasterReport you have to use as datasource-expression "$P{DSsub1}" and as SubReport-expression "$P{SRsub1} (watch out the types, it should be "JRDataSource" and "JasperReport")

 

and that's it... after filling you should get a JRPrint-Object that contains your filled report.

 

hth

C-Box

 

PS: you could encounter trouble with getting the corresponding records from SubReport to the current MasterReport-Record... therefore you will need a kind of filter that the SubReport just shows the corresponding items to it's masterrecord... but first try the way above.

 

 

 

 

By: Chuck Deal - cdeal

RE: Subreports and using a Custom Data Source

2004-06-22 10:29

I have a couple of ideas, but I don't want to try and describe them all. Perhaps if you could tell us how the data will make it to the report. I know that you said a Custom DataSource, but does that mean a SQL based resource, a Bean Collection resource, etc.

 

Based upon your description of the report, (master-detail style) it seems like you will need two distinct datasources.

 

I'll try and make a suggestion once I have the information that is missing.

 

 

 

 

By: Sean Barenz - forceinfinity

RE: Subreports and using a Custom Data Source

2004-06-22 08:46

C-Box-

 

I appreciate you taking the time to answer the question, but as you correctly pointed out in your PS, that tying or the subreport record to the master report record *is* the problem. I could use the same datasource within the subreport, but the sub will attempt to consume the entire datasource, and it'll be done before it even reaches the 2nd employee record.

 

I could pass a full on 2nd data source as was suggested, but again, even with filtering, the subreport is going to consume that 2nd datasource before handing it back, so I'm stuck with the original dilema, and that is how to continuously populate that 2nd datasource during the fill process or at some other point so that the subreport has what it needs.

 

It's a tough puzzle I know, and I do appreciate the tips you've posted here. Hope you or anyone else here might have additional insights. Thank you again

 

 

 

 

By: C-Box - c-box

RE: Subreports and using a Custom Data Source

2004-06-22 12:51

Well I had exactly the same problem you have now.

 

First I tried to change the source of JasperReports... and it works.. but than I built a own class that I named "FilterableDataSource" that is "cloneable" and has two additional attributes "FilterField" and "FilterValue" ... and in the NEXT-Method I just compare the current record of the DataSource if it does fullfill the filtercriteria.....

 

For nested SubReports (a SubReport within a SubReport) that uses the same DataSource I recognized, that that DataSource has to get cloned..... so that each call of a SubReport get's it's own "copy".

 

So all I had to do, was to put in the subreport-section in the DataSourceExpression a call like:

 

getClonedFilterableDataSource("FilterField",$F{Filterfield})

 

so I reached the goal without packing the corresponding records within a own DataSource and this DataSource into the MasterDataSource additionally (like another way would be)

 

perhaps you think about it - in my case just one field to establish was enough because in my datasources each record from MasterDataSource is linked to many records from SubDataSource just with the use of ONE field (what is standard entity relationship model like)

 

hth

C-Box

 

 

 

 

By: Sean Barenz - forceinfinity

RE: Subreports and using a Custom Data Source

2004-06-22 13:31

Thanks again for taking the time to answer:

 

Chuck, the plan was to use a bean collection datasource as my custom. This has worked quite admirably on most of the other reports I've been putting together.

 

C-Box - it sounds like indeed you an I are running into the same puzzle. In my case, the subreport would be directly related to the master via the EmployeeID. So to clear things up, the sub is related to the master.

 

Will explore the idea of using a filterable DS, and will keep my ears open for other ideas. Again I appreicate all the quick responses

 

SeanB

 

 

 

 

 

 

By: Sean Barenz - forceinfinity

Update

2004-06-24 08:29

Thanks to some kicks in the right direction by C-Box, we were able to solve the problem. We ended up modifying two Jasper objects to accomodate the filtering, and everything seems to work fine here. Thanks again C-Box et al for all the help.

 

SeanB

 

 

 

 

By: Jessica - xuh

RE: Update

2004-08-02 13:50

I am encountering the same problem. However I've read your post numerous times and tried also. Could not figure out the right way to do it? Is it possible you could explain more on this, especially which 2 objects you modified?

Thank you very much

 

 

 

 

By: C-Box - c-box

RE: Update

2004-08-03 22:37

Hi Jessica,

 

you needn't modify any existing JasperSourceFile.... just build your own class (e.g. FilterableDataSource) that implements JRRewindableDataSource.... within that class you introduce at least two new variables... one holding a fieldname (e.g. java.lang.String filterField) and one holding a value (e.g. java.lang.Object filterValue). (if you need more that one filtercriteria you could also use a Map holding all the filterFields and a map holding all the filterValues)

 

Than you modify the needed method "next" so, that you compare the current record within your CustomDataSource with the value of the attribute "filterValue" of your field "filterField". So you jump always to the next record that fullfilles your filtercriteria.

 

To set the filterField and the filterValue, you can use a constructor that returns your CustomDataSource and set both attributes at the same time. So I've done in "DataSourceExpression" of the SubReport-Section in MasterReport. (e.g. $p{DataSourceContainer}.getDataSource("SubReportNumberOne","ID",$F{ID})

 

So I pass when filling my MasterReport all needed CustomDataSources in a container (in this case for instance "DataSourceContainer") that has a method "getDataSource(String dsName, String filterField, Object filterValue)

 

So at runtime the connection between Master and SubReport is established and the CustomDataSource is of my own type (e.g. FilterableDataSource). The next method skips the records that doesn't fullfill the filtercriteria and I just get the records that are linked to the MasterRecord.

 

that's all. :-)

 

hth

C-Box

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