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

List as DataSource for a SubReport


2005 IR Help

Recommended Posts

By: Marcelo - marcelocda

List as DataSource for a SubReport

2004-06-02 07:45

Hi,

 

Here is the situation:

I have a list 'A' of Object 'A'. I have only ONE object in this list. The Object 'A' has a List 'B' attribute which I need to pass to a subreport.

 

I fill the master report with the List 'A', using JRBeanCollectionDataSource.

 

How can I fill the subreport with the List 'B' ?

 

Thanks in advance!

Marcelo.

 

 

 

 

By: Marcelo - marcelocda

RE: List as DataSource for a SubReport

2004-06-07 07:04

Please guys, somebody could help me?

 

 

 

 

By: Chuck Deal - cdeal

RE: List as DataSource for a SubReport

2004-06-07 07:26

why can't you set the dataSource for the subreport to:

 

new JRBeanCollectionDataSource(

  1. )

 

Have you tried this?

 

 

 

 

By: Marcelo - marcelocda

RE: List as DataSource for a SubReport

2004-06-08 04:49

But, how?

Here is my code:

 

FacadeBancario obj = new FacadeBancario();

List listaExtratoBancario = obj.geraRelatorioExtratoBancario();

ExtratoBancario extrato = (ExtratoBancario)listaExtratoBancario.get(0);

List listaMovimentacao = extrato.getListaMovimentacoes();

 

HashMap parameters = new HashMap();

 

File reportFile = new File(application.getRealPath("/WEB-INF/relatorio/ExtratoBancario.jasper"));

 

JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(listaExtratoBancario);

 

byte[] bytes = JasperRunManager.runReportToPdf(reportFile.getPath(),parameters,ds);

response.setContentType("application/pdf");

response.setContentLength(bytes.length);

response.setHeader("Expires", ""); //-IE-

response.setHeader("Cache-Control", "");//-IE-

response.setHeader("Pragma", "");//-IE-

ServletOutputStream outputStream = response.getOutputStream();

outputStream.write(bytes, 0, bytes.length);

outputStream.flush();

outputStream.close();

 

The report 'ExtratoBancario.jasper' has a subreport.

 

Chuck, I'm in doubt if I can do this...I don't know how.

Because, when I call "runReportToPdf" I have to pass a DataSource...So, I pass the master report's datasource and I can't see how I can associate the Subreport's datasource to the subreport file (I can call "runReportToPdf" only once).

 

Thanks in advance!!

Marcelo.

 

 

 

 

By: Chuck Deal - cdeal

RE: List as DataSource for a SubReport

2004-06-08 05:21

You are correct that you can only "fill" the MASTER report once, but technically your subreport is part of the filling process. You have no way to specify the datasource from the Java code you are running (and you aren't meant to). In all cases, when you are working with a subreport, you have to work "through" the master report.

 

Somewhere in your master report's XML file you have a <subreport> expression, with in that expression, you must be defining a datasource. This is where you would put the datasource that I mentioned previosuly. But how do you get a handle to the List? Everything is done "through" the master report, so the only way to indroduce the list to the report is through Parameters. So, in the master report XML define a parameter (list_B, for instance) and then from your Java code set the parameter to the second List that you wish to use. Now, your master report has access to the List in order to pass it to the constructor of the JRBeanCollectionDatasource.

 

You may have to play with it a little bit, but that is the general idea.

 

 

 

 

By: Chuck Deal - cdeal

RE: List as DataSource for a SubReport

2004-06-08 05:23

I should also mention, that if the List B list does not change depending on the current value of the master report, then you could put the ListB DataSource in as a parameter. Instead of making the report create a new instance, you could pass it in to the master report (who would in turn assign it to the subreport).

 

 

 

 

By: Marcelo - marcelocda

RE: List as DataSource for a SubReport

2004-06-08 12:01

Chuck, thanks for the help!

 

In the previous code, I changed the parameters HashMap:

HashMap parameters = new HashMap();

JRBeanCollectionDataSource subDS = new JRBeanCollectionDataSource(listaMovimentacao);

parameters.put("subDS", subDS);

Now I'm passing a JRBeanCollectionDataSource to the MASTER Report.

 

In the MASTER Report I created a parameter called 'subDS' from the class 'dori.jasper.engine.data.JRBeanCollectionDataSource' and I checked 'is for Prompting'.

 

But, I already tried to get this DataSource in the SUBREPORT, but, I couldn't do it.

 

Could you help me, step by step, how can I do it?

 

Thank you very much!

Marcelo.

 

 

 

 

 

By: Chuck Deal - cdeal

RE: List as DataSource for a SubReport

2004-06-08 12:22

There are other people much more qualified to help you with this particular task. In particular, C-box does this kind of thing and might have some good sample code for you.

 

But, I'll give it a shot anyway.

 

<subreport>

<datasourceExpression><![CDATA[$P{subDS}]]></datasourceExpression>

</subreport>

 

NOTE: isForPrompting isn't used for anything (as far as I know) within the report, it is there to help Report GUIs.

 

 

 

 

By: Marcelo - marcelocda

RE: List as DataSource for a SubReport

2004-06-08 12:26

I already did it!

 

The problem was that I forgot to set the SubReport Expression!

 

Thanks for the help !!

Marcelo.

 

 

 

 

By: Marcelo - marcelocda

RE: List as DataSource for a SubReport

2004-06-08 12:37

So, for the people with the same problem, here is all my code:

 

In the application:

HashMap parameters = new HashMap();

JRBeanCollectionDataSource subDS = new JRBeanCollectionDataSource(listaMovimentacao);

parameters.put("subDS", subDS);

 

File reportFile = new File(application.getRealPath("/WEB-INF/relatorio/ExtratoBancario.jasper"));

 

JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(listaExtratoBancario);

byte[] bytes = JasperRunManager.runReportToPdf(reportFile.getPath(),parameters,ds);

response.setContentType("application/pdf");

response.setContentLength(bytes.length);

response.setHeader("Expires", ""); //-IE-

response.setHeader("Cache-Control", "");//-IE-

response.setHeader("Pragma", "");//-IE-

ServletOutputStream outputStream = response.getOutputStream();

outputStream.write(bytes, 0, bytes.length);

outputStream.flush();

outputStream.close();

 

In The MASTER REPORT:

<parameter name="subDS" isForPrompting="true" class="dori.jasper.engine.data.JRBeanCollectionDataSource"/>

...

<dataSourceExpression><![CDATA[$P{subDS}]]></dataSourceExpression>

<subreportExpression class="java.lang.String"><![CDATA["C:\Projetos\SgnWeb\war\WEB-INF\relatorio\subEBMovimentacao.jasper"

]]></subreportExpression>

</subreport>

 

You don't need to do anything in the subreport.

 

Marcelo.

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