Jump to content

sub reports data source isn't seen


Recommended Posts

By: slowfinger - slowfinger

sub reports data source isn't seen

2006-01-20 03:50

Hi,

 

I have 2 simple reports which will show it's data in single mode but if I put one of them as an subreport in the other one the subreport will not show it's data. I work with jasper reports 1.1.1 from java and the data come from beans collections. Reports and code are following:

 

1. master report:

 

<parameter name="dataSource" isForPrompting="false" class="net.sf.jasperreports.engine.data.JRMapCollectionDataSource"/>

 

<field name="autoId" class="java.lang.Integer"/>

<field name="name" class="java.lang.String"/>

<field name="baujahr" class="java.util.Date"/>

<field name="farbe" class="java.lang.String"/>

 

<subreport isUsingCache="true">

<parametersMapExpression><![CDATA[new HashMap($P{REPORT_PARAMETERS_MAP})]]></parametersMapExpression>

<subreportParameter name="autoId">

<subreportParameterExpression><![CDATA[$F{autoId}]]></subreportParameterExpression>

</subreportParameter>

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

</subreport>

 

2. sub report

 

<field name="autoId" class="java.lang.Integer"/>

<field name="name" class="java.lang.String"/>

 

3. the java code:

 

JasperReport reportCar = ((JasperReport) JRLoader.loadObject(new File(carReportFile + ".jasper")));

JasperReport reportCarParts = ((JasperReport) JRLoader.loadObject(new File(carReportPartsFile + ".jasper")));

 

Collection dataCar = new ArrayList();

dataCar.add(new AutoReportBean(new Integer(1), "audi", "red", new Date()));

dataCar.add(new AutoReportBean(new Integer(2), "golf", "blue",new Date()));

 

Collection dataCarParts = new ArrayList();

dataCarParts.add(new AutoTeileReportBean(new Integer(1), "Lenkrad"));

dataCarParts.add(new AutoTeileReportBean(new Integer(1), "Batterie"));

dataCarParts.add(new AutoTeileReportBean(new Integer(1), "Auspuff"));

dataCarParts.add(new AutoTeileReportBean(new Integer(1), "Sanikasten"));

dataCarParts.add(new AutoTeileReportBean(new Integer(2), "Kurbelwelle"));

dataCarParts.add(new AutoTeileReportBean(new Integer(2), "Ganghebel"));

Map mapCarTeile = new Hashtable();

mapCarTeile.put("dataSource", new JRMapCollectionDataSource(dataCarParts));

 

JasperPrint printAuto = JasperFillManager.fillReport(reportCar,mapCarTeile, new JRBeanCollectionDataSource(dataCar));

 

JasperViewer.viewReport(printAuto);

 

The data of the master report will be shown but not the subreports data. What's wrong here. What I want in the end is to display a one to many relation on the report using the sub report mechanism and this easy trial doesn't work.

 

Any helps ;-)

Bye Rico

 

 

By: jasperkan - jasperkannan

RE: sub reports data source isn't seen

2006-01-20 06:58

I have encountered the same problem .these are the solns for this .

 

1: make both reports as subreports and then add them to a main report .

 

2: put the subreport into the summary area and then set the option show summary data in a new page .

 

 

 

 

By: Lucian Chirita - lucianc

RE: sub reports data source isn't seen

2006-01-20 09:20

Hi

 

First an observation: you don't have an subreportExpression for your subreport. This will obviously cause the subreport not to show. Also you load the subreport in the Java code but you don't use it.

 

Now regarding the report you're trying to produce. Usually in JR when you have a subreport that appears multiple times in the master report, the subreport would have a different data source (I mean the same data source type but instantiated with different parameter values).

 

In your case this means that the usual way to do it is to have separate part lists for each car and when you call the subreport to pass the list for the current car (the list could be a member of the AutoReportBean bean). I'm don't know whether you can fetch the data in this manner, but it's the canonic way to use master-detail subreports.

 

Your dataCarParts data source would have to be used by several subreport instances, based on some key matching logic. This not (yet) supported in JR, see this enhancement request:

https://sourceforge.net/tracker/index.php?func=detail&aid=1045435&group_id=36382&atid=416706

 

HTH,

Lucian

 

 

By: slowfinger - slowfinger

RE: sub reports data source isn't seen

2006-01-20 12:45

Hi Lucian,

 

thanks for your answer. A subreportParameterExpression I have it is the 'autoId'. So that all car parts should be listed to each car. Your answer sounds clear but how I can solve that. The demo sample of subreport which shows an example of using the subreport mechanism makes exactly what I want in the first step, but it does that unfortunatly with a database connection. So the question keeps how I can do that procedure with bean collections.

 

Thanks in forward

Rico

 

 

By: Lucian Chirita - lucianc

RE: sub reports data source isn't seen

2006-01-24 01:25

Let me first ask you again: do you have a subreportExpression for the subreport? Does the subreport show any data at all?

 

Regarding the autoId parameter, how is it used in the subreport?

 

As I was saying in previous post, the easiest way to construct your master-detail report is, if possible, by changing the way you retrieve the data. Currently you have a dataCarParts collection containing parts for all the cars. You need to have a parts collection per car.

 

If you can't fetch your data in this manner, you can process your consolidated parts collection and break it into per car collections. You can do this easily using a parts collection map indexed by the car id or, if both the dataCar and dataCarParts collections are sorted by id, use a faster merge-like algorithm to break down the parts collection.

 

After you have the parts collection broken down per car, you'll have to use for each car the collection of its parts as a data source for the subreport.

 

Regards

Lucian

 

 

By: slowfinger - slowfinger

RE: sub reports data source isn't seen

2006-01-24 05:11

No, I have no subreport expression and the subreport doesn't show the data, only the master report shows it.

 

The autoId parameter is only defined in the report definition I never used it outside. The problem in my understanding is I need some more information over the normal way which use database connection. So I adapt the database sample for subreport. But it doesn't really help.

 

I understand your explanations but what me really help would be a short example how to do that all in the right manner.

 

Thanks

Rico

 

 

By: Lucian Chirita - lucianc

RE: sub reports data source isn't seen

2006-01-25 11:30

Ok, you can try this:

1. Break down car parts per car (I'm assuming you're not able to fetch the data like this directly):

Map carParts = new HashMap();

for (Iterator it = dataCar.iterator; it.hasNext(); )

{

AutoReportBean car = (AutoReportBean) it.next();

carParts.add(car.getAutoId(), new ArrayList());

}

for (Iterator it = dataCarParts.iterator; it.hasNext(); )

{

AutoTeileReportBean carPart = (AutoTeileReportBean) it.next();

List parts = (List) carParts.get(carPart.getAutoId());

parts.add(carPart);

}

2. Send both carParts and reportCarParts to the master report as parameters.

3. Define your subreport like this:

<subreport>

<reportElement .../>

<dataSourceExpression>new JRBeanCollectionDataSource((List) $P{carParts}.get($F{autoId}))</dataSourceExpression>

<subreportExpression class="net.sf.jasperreports.engine.JasperReport">$P{reportCarParts}</subreportExpression>

</subreport>

 

HTH,

Lucian

 

 

By: slowfinger - slowfinger

RE: sub reports data source isn't seen

2006-01-26 08:50

Thanks Lucian,

 

it works great. This is an starting point to more.

 

Bye

Rico

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