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

Custom data source and subreports


atiquek

Recommended Posts

Hi Gurus,

I have implemented a custom datasource to fetch my data to populate a report which contains 2 subreports placed adjacent to each other. So for each row of data I want the 2 subreports to be executed. I am retrieving the data using web services in the custom datasource and need to pass this data to the subreports. I am lost here completely and need some help/guidance so that the data can be populated in the subreports. Please help.

Thanks,

Atique

Link to comment
Share on other sites

  • Replies 4
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

I fixed this on my own with some the help of some old posts on this forum. I had to create 3 custom DataSource one for main and 2 for each subreports. Then I created 2 fields fieldSubReport1 and fieldSubReport2 on the main report with their data type as java.util.List. These two fields are passed as subreport parameters to the corrresponding subreports with the DataSource expression being new my.package.ClassName($F{chartData})  and new my.package.ClassName1($F{chartDesc}). Each of these subreport DataSource implements JRDataSource and thats pretty much.

 

Link to comment
Share on other sites

  • 2 years later...
Solution to populate subreports with a customDataSource

 

I have a main report with 2 subreports inside.

 

Main Report doesnt print anything and simply pass all the info to subreports.

 

You have to create a unique custom DataSource to recover (via BD ,Webservice, etc..) all the info that you need for your 2 subreports. You can follow the customDataServiceSample that you have in samples folder in your JasperServerInstalation.

 

After recover all the data and keep it in local variables i have created two public methods to pass the data to subreports:

 

public JRDataSource getDataSourceToSubreport(){
return new JRBeanCollectionDataSource(data_report_1);}
public JRDataSource getDataSourceToSubreport2(){
return new JRBeanCollectionDataSource(data_report_2);}[/code]
 

 

And finally in the jrxml of the main report in each subreport in the DataSourceExpresion i call to this methods doing this

 

SubReport1: $P{REPORT_DATA_SOURCE}.getDataSourceToSubreport()

 

subReport2: $P{REPORT_DATA_SOURCE}.getDataSourceToSubreport2()

 

The main report not compile, in preview mode in ReportStudio because it doesn´t know about this methods, but it works in jasperServer without problems.

 

All my code is here:

 

------------CustomDSService.java-----------------

 

public class CustomDSService implements ReportDataSourceService

 {

   private JRDataSource ds;

   private RepositoryService repository;

   private Map propertyMap;

   private Map parameterValues;

   

   public Map getPropertyMap()

   {

  return this.propertyMap;

   }

   

   public void setPropertyMap(Map propertyMap)

   {

  this.propertyMap = propertyMap;

   }

   

   public CustomDSService()

   {

  this.ds = new CustomDataSource();

   }

   

   public CustomDSService(JRDataSource ds)

   {

  this.ds = ds;

   }

   

   public void closeConnection() {}

   

   @SuppressWarnings("unchecked")

   public void setReportParameterValues(Map parameterValues)

   {

 

  parameterValues.put("REPORT_DATA_SOURCE", this.ds);

 

//Pass input control data to dataService.

  CustomDataSource ds = (CustomDataSource) this.ds;

ds.setParameterValues(parameterValues);

 

   }

   

   public RepositoryService getRepository()

   {

  return this.repository;

   }

   

   public void setRepository(RepositoryService repository)

   {

  this.repository = repository;

   }

 }

 

----------------CustomDataSource.java------------------

 

public class CustomDataSource   implements JRDataSource

{

     

   

   List<E> data_report_1 = new ArrayList();

   List<E> data_report_2 = new ArrayList();

  

   

   private Map parameterValues;

   private int index = -1;

   

  

 

public CustomDataSource() {

super();

 

}

 

 

        //Public method created by myself to access to report input data before recover data

public void setParameterValues(Map parameterValues) {

this.parameterValues = parameterValues;

fillDataReport1();

fillDataReport2();

}

 

 

 

   //If your main report doesnt print anything this method is not needed to do anything

   public boolean next() throws JRException

   {

  return false;

   }

 

 

   //If your main report doesnt print anything this method is not needed to do anything

   public Object getFieldValue(JRField field)

     throws JRException

   {

 

  return null;

   }

 

   //method to pass data to subreport

   public JRDataSource getDataSourceToSubreport(){

 

return new JRBeanCollectionDataSource(data_report_1);

   }

 

   //method to pass data to subreport

   public JRDataSource getDataSourceToSubreport2(){

 

return new JRBeanCollectionDataSource(data_report_2);

   }

 

........................

........................

  //TODO Methods to recover data

 

  //fillDataReport1();

  //fillDataReport1();

}

 

 

Hope it helps:

 


 

Link to comment
Share on other sites

  • 1 year later...

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