Jump to content
Changes to the Jaspersoft community edition download ×

Filling a Custom DataSource for a SubReport

2004 IR Help

Recommended Posts

By: Dom H - speedsix

Filling a Custom DataSource for a SubReport

2005-02-14 08:51

I'm just changing my code over from using an Sql connection to a custom data source.


My custom data source needs to 'fill' itself everytime the report is run, I have extended the JRDataSource to include a fillDataSource method which queries my database and fills in the data.

If I call this fill method on the sub report's custom data source and pass it to the main report via the DataSourceExpression, the sub report prints but the sub reports datasource only fills the once and is empty on subsequent repetitions.


How do I get the sub report's data source to fill itself everytime it's needed?


Many thanks





By: C-Box - c-box

RE: Filling a Custom DataSource for a SubRepo

2005-02-15 00:07

Well I guess you should make a CLONE for your SubDataSource because the internal counter is incremented each time the NEXT-METHOD of your CustomDataSource is called.... so I can imagine, that your SubDataSource is only called once because the internal counter has reached it's end... you also could try to call the MOVEFIRST-METHOD before you call the fillDataSource Method (or call it within that method)








By: Dom H - speedsix

RE: Filling a Custom DataSource for a SubRepo

2005-02-15 02:09



Perhaps I should explain a little better, I'm confusing myself here!


Ok, I have a main report which prints all 'Suppliers' for each supplier there is a sub-report printing all the 'Parts' for each supplier. So it might say 'Speedy Parts' and then iterate through all the parts underneath.


The thing is I want my DataSource to be generated dynamically not statically like the examples show. What I did was create a sub interface of JRDataSource like so..


public interface DB4ODataSource extends JRDataSource



public void fillDataSource(db4oDAO DAO);




I then create an concrete instance of this for each report, each with a different implementation of the fillDataSource() method.


The problem is I don't know how to get the 'Parts' sub report to fill itself everytime it's called (i.e once for each supplier) I can Fill both the Main/Sub reports DataSource's once before I print but each subsequent part sub-report is empty so I get something like;


Speed Parts






Cheap Parts




Parts R Us






Not sure if I'm going about this the correct way, I can't find any docs on dynamic DataSources.









By: C-Box - c-box

RE: Filling a Custom DataSource for a SubRepo

2005-02-15 03:25



I've also done something similiar...


I do print a lists of offers... each offer has some positions...so what I've done is to create an own (I called it JRFilterableDTODataSource) customDataSource for all offers and an own (also again of type JRFilterableDTODataSource) customDataSource for all the positions.... then I put the "Pos-DataSource" into a container and pass that container as parameter and used it as DataSourceExpression like this.




so my DataContainer has a method "getFilterableDTODataSource(Object filterValue, String filterField)" that returns a JRFilterableDTODatasource that contains ALL positions that matches the current value of the field OfferID.... in the next-method of the JRFilterableDTODataSource I do compare the current value of the position-record with the current value of the so called FilterField (that is $F{OfferID} in master and parenOfferID in pos-datasource...and the next-method skips all records that do not match. At the beginning of each call I call the MoveFirst-method of course.


So I get all positons to the current masterrecord.


perhaps it helps you






By: Dom H - speedsix

RE: Filling a Custom DataSource for a SubRepo

2005-02-15 06:09

Hi thanks, I do have a wrapper class for my reports which I have included a getDataSource() method which fills the data source and returns it. I then set the DataSourceExpression ofthesub report to:




but it doesn't seem to call this method atall and my sub reports are still blank?









By: C-Box - c-box

RE: Filling a Custom DataSource for a SubRepo

2005-02-15 06:45

put a System.out("in getDataSource"); into your method.... and look at the console if it's called at all.


here is one of my subreports-element:


<subreport isUsingCache="false">

<reportElement key="element-11" positionType="Float" x="1" y="0" width="497" height="17" isRemoveLineWhenBlank="true"/>



<subreportExpression class="net.sf.jasperreports.engine.JasperReport"><![CDATA[$P{DataContainer}.getSRPositions()]]></subreportExpression>




make a search in jasper-reports forum (open discussion when I remember right) there I posted my FilterableDataSource Class.









By: Dom H - speedsix

RE: Filling a Custom DataSource for a SubRepo

2005-02-15 07:01

Hi, I have added a System.out.println("Filling & Retrieving DataSource"); to my method and I see nothing?


If I pass a plain DataSource to it it works for the first iteration of the sub report but won't repeat itself.


Thanks again





By: C-Box - c-box

RE: Filling a Custom DataSource for a SubRepo

2005-02-15 07:33

and the report compiles okay with that datasource-expression????







By: Dom H - speedsix

RE: Filling a Custom DataSource for a SubRepo

2005-02-15 11:10

Yeah it compiles fine, I am using i-Report to compile.





By: Raymond L. Luangco - rlluangco

RE: Filling a Custom DataSource for a SubReport

2005-02-17 00:08



I have this application that is similar to your problem.

Let say a bean, TeacherBean have the ff attributes:

- teacherId

- name

- students (List)


where, students is a List of StudentBean handled by a teacher, having the ff attributes

- studentId

- name


In my report, I will have List, which is a collection of TeacherBean. By design, I have a master report for Teacher and a subreport for Student.


1. In my program, I created 2 JasperReport objects, 1 for master and 1 for subreport:


JasperReport masterReport = (JasperReport)JRLoader.loadObject("<MASTER_JASPER_FILE>");

JasperReport subReport = (JasperReport)JRLoader.loadObject("<SUBREPORT_JASPER_FILE>");


2. Put the subreport in the parameters. This parameter shall be the value of your "Subreport Expression" (Subreport (Other) tab in iReport).


3. That's it! You now have the datasource of your report. Now, let us proceed to iReport to define the datasource of the subreport.


4. Define a parameter for your subreport with class type net.sf.jasperreports.engine.JasperReport. The name of this parameter shall be the same in #2.


5. Create a "students" field with a class type "java.lang.Object". This is the List of StudentBean.


6. Put a subreport in the detail section with the ff properties:

Connection/Datasource Expression: Use datasource expression. Enter the value "new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource((java.util.List)$F{students})" in the textarea field.


Subreport Expression: $P{<SUBREPORT_PARAMETER_IN_#2>}


7. You now have a datasource for your subreport. Define the fields of StudentBean and display it in your report.












By: Dom H - speedsix

RE: Filling a Custom DataSource for a SubRepo

2005-02-17 07:31



I seem to have got it working, not sure why though!


I removed my fillDataSourceMethod and put all the filling code into the DataSource's constructor and changed the DataSourceExpression to 'new org.mypackage.MyCustomerDataSource()'


Thanks for the help chaps

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