Jump to content
JasperReports Library 7.0 is now available ×

Dynamic subreport


2006 IR Open Discussion

Recommended Posts

By: Kostas Michailidis - mkostas_gr

Dynamic subreport

2005-10-10 03:40

I have a report which shows 200 product codes with their short description. Inside the JasperViewer, the user wants to click on a product code to see product details. The problem is that I do not know in adnavce which product will be clicked. Also, using Hyperlink reference, I can reference only existing reports. Does anybody have an idea, how to do it?

 

By: toloverme - toloverme

RE: Dynamic subreport

2005-10-19 20:40

look

 

By: Kostas Michailidis - mkostas_gr

RE: Dynamic subreport

2005-10-11 06:07

This can be done! I wrote my HyperlinkListener and added it to the JRViewer list of Hyperlink listeners. The subreport layout and datasource name and some parameters must be specified in iReport's Hyperlink Reference Expression using some convetion f.e "!subreport.jasper,mySubreportDataSource,param1,param2".

Inside my HyperlinkReferenceListener class I create the new subreport dynamicly calling JasperFillManager.fillReport(). The JasperPrint object is then shown in separate JasperViewer.

User can drill-down clickink on the hyperlinks.

 

Please comment and let's see another ways.

 

 

By: anuseb - anuseb

RE: Dynamic subreport

2006-03-07 01:26

can u be more elaborate please..?

i want to implement the same situation.

What is this HyperlinkListner class...Is it a user defined class...if so where should write it..

Can u share the code with me..?

 

 

By: anuseb - anuseb

RE: Dynamic subreport

2006-03-07 01:45

any one pls help...how can i drill down by clicking on the hyperlinks and creating dynamic subreports...?

 

 

By: Kostas Michailidis - mkostas_gr

RE: Dynamic subreport

2006-03-07 02:11

Please, look into the latest version of the iReport and JasterReports for this. If there is no such feature implemented, send me your e-mail address to michailidis@amasa.gr in order to help you.

 

Kostas

 

 

By: Kostas Michailidis - mkostas_gr

RE: Dynamic subreport

2006-03-10 08:28

Hallo Anu,

 

as I informed you by e-mail, I can refer only to

the implementation with the JasperViewer. I have no idea

how to do it with PDF files.

 

The user click on parent report's hyperlink element is handled in the JRViewer class

which is part of Jasper Reports library. So, create a new class f.e Viewer99

by subclassing JRViewer and add "your" hyperlink listener, like this

Code:

public class Viewer99 extends JRViewer
{
public Viewer99(JasperPrint jrPrint)
{
super(jrPrint);
addHyperlinkListener(new HyperLinkReferenceListener());
}

public Viewer99(String fileName, boolean isXML) throws JRException
{
super(fileName, isXML);
}

public Viewer99(InputStream is, boolean isXML) throws JRException
{
super(is, isXML);
}
}

HyperLinkReferenceListener class implements JRHyperlinkListener and could be like this


public class HyperLinkReferenceListener implements JRHyperlinkListener
{
public void gotoHyperlink(JRPrintHyperlink hyperlink)
{
int idx, idx2, i;
DBASource jrData = null;

if (hyperlink.getHyperlinkType() == JRHyperlink.HYPERLINK_TYPE_REFERENCE) {
//System.out.println(hyperlink.getHyperlinkReference());
String hyperlink_reference_exp = hyperlink.getHyperlinkReference();

if (hyperlink_reference_exp.startsWith("!"«»)){

idx = hyperlink_reference_exp.indexOf(',');
if (idx == -1) return;

//get jasper file name from hyperlink_reference_exp
String jasperReportFile = hyperlink_reference_exp.substring(1,idx);

//get JRDataSource class name
idx2 = hyperlink_reference_exp.indexOf(',', idx+1);
if (idx2 == -1) return;
String dataSourceClass = hyperlink_reference_exp.substring(idx+1,idx2);
//load data source class
try {
//THIS IS FUNDAMENTAL!!! DataSource must be of DBASource type.
//Because parameters can not be passed into the class constructor,
//the DBASource implements setParams mathod.
//Because of the previous there must be a method for connecting
//the database and creating the Resultset - openResultSet method.
//Of course, the OpenResultSet method is called after calling the
//setParams method, because some parametrs can participate in the
//data retrieve SQL statement.
//HyperlinkReference Expression format:
//!report.jasper,MyDataSource,param1,param2,param3,...
//
// where MyDataSource is DBASource subclass
// report.jasper is compiled jasper report layout
// params are Strings passed into Data Source where they
// are addressed by PARAM1,PARAM2,... names
//
jrData = (DBASource) Class.forName(dataSourceClass).newInstance();
}catch (ClassNotFoundException e)
{
e.printStackTrace();
}catch (InstantiationException e1)
{
e1.printStackTrace();
}catch (IllegalAccessException e2)
{
e2.printStackTrace();
}

if (jrData == null) return;

//get parameters
i = 0;
HashMap hm = new HashMap();
while ((idx = hyperlink_reference_exp.indexOf(',', idx2+1)) != -1)
{
hm.put("PARAM" + (++i), hyperlink_reference_exp.substring(idx2+1,idx));
idx2 = idx;
}

//get the last parameter
hm.put("PARAM" + (++i), hyperlink_reference_exp.substring(idx2+1));

//hm.put("PARAM1", "980-0101-2001"«»);
//hm.put("PARAM2","0527001"«»);

jrData.setParams(hm);
jrData.openResultSet();

try{
JasperPrint jrPrint = JasperFillManager.fillReport(jasperReportFile,
null,
jrData);
jrData.closeAll();
jrData = null;

JasperViewerE cl = new JasperViewerE(jrPrint);
cl.setVisible(true);

}catch(JRException e)
{
e.printStackTrace();
}
}//if
}//if
}//public void gotoHyperlink

 

 

Now the Viewer99 has the ability to catch the user's click on a hyperlink and invoke the related subreport. Viewer99 must be called inside the JasperViewer.

But instead of modifying the original JasperViewer, craete a new class JasperViewerE base on it and replace any occurrence of JRViewer by Viewer99 and use

JasperViewerE everywhere you need this feature.

 

In this implementation, the subreport data source is always class implementing JRDataSource. You could find some other solution suitable for your needs.

 

I hope this can help you.

 

Kostas

Post edited by: tcloonan, at: 2007/12/19 11:38

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Popular Days

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