Jump to content
Changes to the Jaspersoft community edition download ×

JRExtendedBeanDataSource with subreport


lefrancky

Recommended Posts

Hi,

 

I'm actually working on a report who contains subreports.

 

The master report use a JRExtendedBeanDataSource as datasource.

And I want to give a JRExtendedBeanDataSource in my subreport's datasource expression.

 

Is it functional ?

I didn't see example(s) of reports and subreports working with JRExtendedBeanDataSource.

 

During my tests, the master is correctly printed but subreports never work.

Link to comment
Share on other sites

  • Replies 3
  • Created
  • Last Reply

Top Posters In This Topic

JRBeanCollectionDataSource and JRBeanArrayDataSource are now able to do what JRExtendedBeanDataSource does. I strongly suggest you to upgrade to these datasources.

 

A JRCollectionBeanDataSource can instanced starting from a collection, so if you have a field that is a List, you can fill the subreport with a datasource expression like:

 

new JRBeanCollectionDataSource($F{YourListField})

 

Giulio

Link to comment
Share on other sites

  • 2 years later...

Hi everyone,

I am new in iReport & JasperReports. I want to pass more than one lists in

iReport. I have searched and realised that the best practise is using

subreports.

So, I have created a datasource (datasource2) for the master report and

another one for my subreport (datasource1). I fill these datasource using

the following code.

 

"

 

public void generateListReport(TestReport2 testReport2, List MyList,

String parameter,String JasperPath,String JasperPath2) throws IOException,

JRException{

ArrayList WholeList = new ArrayList();

ArrayList DTList = new ArrayList();

ArrayList myTestReport = new ArrayList();

Map parameters = new HashMap();

 

 

myTestReport.add(testReport2);

 

facesContext = FacesContext.getCurrentInstance();

ExternalContext ctx = facesContext.getExternalContext();

HttpServletResponse response = (HttpServletResponse)

ctx.getResponse();

response.setContentType("application/pdf");

System.out.println("MyList : "+MyList);

System.out.println("testReport2 : "+testReport2);

System.out.println("myTestReport : "+myTestReport);

for (int i = 0; i < MyList.size(); i++) {

myListArray myList = new myListArray(

(String) ((List) MyList.get(i)).get(0),

(String) ((List) MyList.get(i)).get(1),

(String) ((List) MyList.get(i)).get(2));

DTList.add(myList);

}

 

WholeList.add(DTList);

WholeList.add(myTestReport);

System.out.println("datasource 1 filled with WholeList : " +

WholeList);

 

 

String reportFileName = ((ServletContext)

ctx.getContext()).getRealPath(JasperPath);

String reportFileName2 = ((ServletContext)

ctx.getContext()).getRealPath(JasperPath2);

 

JasperReport jasperReport,subReport;

try {

 

subReport = (JasperReport)

JRLoader.loadObject(reportFileName2);

 

JRDataSource dataSource1 = new

myListArrayDatasource().create(subReport, DTList);

 

parameters.put("dataSource1", dataSource1);

parameters.put("subReport", subReport);

 

JasperPrint jasperPrint =

JasperFillManager.fillReport(subReport, parameters,

dataSource1);

System.out.println("SubReport is filled!");

 

 

} catch (JRException e) {

System.out.println("SubReport is not filled!");

e.printStackTrace();

}

 

try {

jasperReport = (JasperReport)

JRLoader.loadObject(reportFileName);

 

JRDataSource dataSource2 = new

TestReportDatasource().create(jasperReport, myTestReport);

 

JasperPrint jasperPrint2 =

JasperFillManager.fillReport(jasperReport, parameters,

dataSource2);

System.out.println("Report is filled!");

 

try {

JasperExportManager.exportReportToPdfStream(jasperPrint2,response.getOutputStream());

} catch (Exception e1) {

e1.printStackTrace();

}

} catch (JRException e) {

System.out.println("Report is not filled!");

e.printStackTrace();

}

 

 

facesContext.responseComplete();

}

 

"

 

"TestReportDatasource.java":

"

package reports;

 

import java.util.ArrayList;

 

import net.sf.jasperreports.engine.JRDataSource;

import net.sf.jasperreports.engine.JRException;

import net.sf.jasperreports.engine.JRField;

import net.sf.jasperreports.engine.JRParameter;

import net.sf.jasperreports.engine.JasperReport;

import net.sf.jasperreports.engine.data.JRAbstractBeanDataSourceProvider;

import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

 

 

public class TestReportDatasource extends

JRAbstractBeanDataSourceProvider {

 

public TestReportDatasource() {

super(TestReport.class);

 

}

 

public JRField[] getFields(JasperReport report) throws JRException {

 

if (report != null) {

System.out.println(report);

JRParameter[] params = report.getParameters();

for (int i = 0; i < params.length; ++i) {

System.out.println(params.getName() + " "

+ params.getDefaultValueExpression());

}

 

String[] properties = report.getPropertyNames();

for (int i = 0; i < properties.length; ++i) {

System.out.println(properties + " = "

+ report.getProperty(properties));

}

}

return super.getFields(report);

}

 

public JRDataSource create(JasperReport report) throws JRException {

ArrayList list = new ArrayList();

return new JRBeanCollectionDataSource(list);

}

 

public JRDataSource create(JasperReport report, ArrayList list)

throws JRException {

return new JRBeanCollectionDataSource(list);

}

 

public void dispose(JRDataSource dataSource) throws JRException {

// nothing to do

}

}

"

 

"myListArrayDatasource.java":

"

package reports;

 

import java.util.ArrayList;

 

import net.sf.jasperreports.engine.JRDataSource;

import net.sf.jasperreports.engine.JRException;

import net.sf.jasperreports.engine.JRField;

import net.sf.jasperreports.engine.JRParameter;

import net.sf.jasperreports.engine.JasperReport;

import net.sf.jasperreports.engine.data.JRAbstractBeanDataSourceProvider;

import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

 

 

public class myListArrayDatasource extends

JRAbstractBeanDataSourceProvider {

 

public myListArrayDatasource() {

super(myListArray.class);

 

}

 

public JRField[] getFields(JasperReport report) throws JRException {

 

if (report != null) {

System.out.println(report);

JRParameter[] params = report.getParameters();

for (int i = 0; i < params.length; ++i) {

System.out.println(params.getName() + " "

+ params.getDefaultValueExpression());

}

 

String[] properties = report.getPropertyNames();

for (int i = 0; i < properties.length; ++i) {

System.out.println(properties + " = "

+ report.getProperty(properties));

}

}

return super.getFields(report);

}

 

public JRDataSource create(JasperReport report) throws JRException {

ArrayList list = new ArrayList();

return new JRBeanCollectionDataSource(list);

}

 

public JRDataSource create(JasperReport report, ArrayList list)

throws JRException {

return new JRBeanCollectionDataSource(list);

}

 

public void dispose(JRDataSource dataSource) throws JRException {

// nothing to do

}

}

 

"

 

 

I declare a parameter dataSource1 and subReport in iReport *.jrxml file

and, using the iReport's subreport wizard I connect the subreport(two

objects,one for testing the connection via parameter "dataSource1" and

another for testing parameter "subReport") with the master report.

 

I cannot successfully fill the subreport in the master report.

 

I would appreciate any help!

 

 

 

Thank you in advance.

 

Link to comment
Share on other sites

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