Jump to content

Pass Report Parameter to Custom Data Source


fruchtiger

Recommended Posts

 Hi,

I created my own Custum Data Source by implementing JRDataSource. It's all working to my satisfaction, but now I would like to retrieve parameters from my report to make a dynamic query. Any hints how to pass parameters I defined in the report design file ? 

Thanks in advance ! 

Link to comment
Share on other sites

  • Replies 4
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Example:

- Create a parameter MYDATE

- Under the Datasource create the parameter MYDATE again.

On the SQL, you can have something like:

CALL my_store_procedure($P{MYDATE});

OR

CALL my_store_procedure('$P!{MYDATE}');

 

Then if you use the Datasource on a Table or Crosstab, this is very important, be sure to check the parameter MYDATE on the object. You need to add MYDATE as with $P{MYDATE} as a default value or else it will be null.

 

It is easy once you know how to do it, I took a long time to learn it..

 

Link to comment
Share on other sites

@robccsilva : thanks for your answer. I dont actually use a sql-query in the report-design file.  I implemented the JRDataSource Interface, with the methods  next() and getFieldValue() to fill the report. But before the filling, I need to check for paramaters within my java-code. Any ideas  ? 
 

My implementation looks  somehting like this : 

 

 

Code:
import java.util.Vector;import net.sf.jasperreports.engine.JRException;import net.sf.jasperreports.engine.JRField;import net.sf.jasperreports.engine.JasperFillManager;import net.sf.jasperreports.engine.JasperPrint;public class MyDataSource implements net.sf.jasperreports.engine.JRDataSource {		 private Object[][] data =		  {		   {"Berne", new Integer(22), "Bill Ott", "250 - 20th Ave."},		   {"Berne", new Integer(9), "James Schneider", "277 Seventh Av."},		   {"Boston", new Integer(32), "Michael Ott", "339 College Av."},		   {"Boston", new Integer(23), "Julia Heiniger", "358 College Av."},		   {"Chicago", new Integer(39), "Mary Karsen", "202 College Av."},		   {"Chicago", new Integer(35), "George Karsen", "412 College Av."},		   {"Chicago", new Integer(11), "Julia White", "412 Upland Pl."},		   {"Dallas", new Integer(47), "Janet Fuller", "445 Upland Pl."},		   {"Dallas", new Integer(43), "Susanne Smith", "2 Upland Pl."},		   {"Dallas", new Integer(40), "Susanne Miller", "440 - 20th Ave."},		   {"Dallas", new Integer(36), "John Steel", "276 Upland Pl."},		   {"Dallas", new Integer(37), "Michael Clancy", "19 Seventh Av."},		   {"Dallas", new Integer(19), "Susanne Heiniger", "86 - 20th Ave."},		   {"Dallas", new Integer(10), "Anne Fuller", "135 Upland Pl."},		   {"Dallas", new Integer(4), "Sylvia Ringer", "365 College Av."},		   {"Dallas", new Integer(0), "Laura Steel", "429 Seventh Av."},		   {"Lyon", new Integer(38), "Andrew Heiniger", "347 College Av."},		   {"Lyon", new Integer(28), "Susanne White", "74 - 20th Ave."},		   {"Lyon", new Integer(17), "Laura Ott", "443 Seventh Av."},		   {"Lyon", new Integer(2), "Anne Miller", "20 Upland Pl."},		   {"New York", new Integer(46), "Andrew May", "172 Seventh Av."},		   {"New York", new Integer(44), "Sylvia Ott", "361 College Av."},		   {"New York", new Integer(41), "Bill King", "546 College Av."},		   {"Oslo", new Integer(45), "Janet May", "396 Seventh Av."},		   {"Oslo", new Integer(42), "Robert Ott", "503 Seventh Av."},		   {"Paris", new Integer(25), "Sylvia Steel", "269 College Av."},		   {"Paris", new Integer(18), "Sylvia Fuller", "158 - 20th Ave."},		   {"Paris", new Integer(5), "Laura Miller", "294 Seventh Av."},		   {"San Francisco", new Integer(48), "Robert White", "549 Seventh Av."},		   {"San Francisco", new Integer(7), "James Peterson", "231 Upland Pl."}		  };		  		 private int index = 0;							 public boolean next() throws JRException		 {			index++;			return (index < data.length);		 }		public Object getFieldValue(JRField field) throws JRException		 {		  Object value = null;		  String fieldName = field.getName();		  if ("city".equals(fieldName))		  {				  value = data[index][0];		  }		  else if ("age".equals(fieldName))		  {			  value = data[index][1];		  }		  		  return value;		 }		 		 		 public static MyDataSource createDataSource(){			 			 return new MyDataSource();			 		 }}
Link to comment
Share on other sites

My knowledge with Java is very basic, see bellow an exceprt of my PHP code, which might help (see $param variable):

 


Code:
require_once("java/Java.inc.php");$System = new Java("java.lang.System");//print_r($System->getProperties());$compileManager = new JavaClass("net.sf.jasperreports.engine.JasperCompileManager");  $report = $compileManager->compileReport($report_filename);  $fillManager = new JavaClass("net.sf.jasperreports.engine.JasperFillManager");  //#############################################################################//Define parameters//#############################################################################$params = new Java("java.util.HashMap");  //$date   = new Java("java.util.Date", 70, 9, 4);//-----------------------------------------------------------------------------//Parameters are Case Sensitive//-----------------------------------------------------------------------------//$params->put("MYDATE", "2011-12-29");$params->put("MYDATE", $mydate);//$params->put("SUBREPORT_DIR", realpath(".") ."\\");//$params->put("SUBREPORT_DIR", "./");//$params->put("SUBREPORT_DIR", "C:/aweb/qualistats/www/reports/");//$params->put("SUBREPORT_DIR", ".");//$params->put("SUBREPORT_DIR", "./");$params->put("SUBREPORT_DIR", ".\\");//-----------------------------------------------------------------------------//$params->put("id_report", convertValue($id_report + 0, "java.lang.Integer"));//$params->put("text", "Java Bridge!");//$params->put("number", "3.56" + 0.01);//$params->put("date", $date);//$params->put("title", "Java Bridge!");//$params->put("when", $date);//$num    = new Java("java.util.Integer", 123);//$params->put("number", 3.00);//$params = new Java("java.util.HashMap");//$params->put("text", "This is a test string");//$params->put("number", 3.00);//$params->put("date", convertValue("2007-12-31 0:0:0", "java.sql.Timestamp"));//$params->put("id_report", "$id_report.00" );//echo "ID: ". $id_report;//#############################################################################//$fileResolver = new JavaClass("net.sf.jasperreports.engine.util.FileResolver");  //$fileResolver = new Java("net.sf.jasperreports.engine.util.SimpleFileResolver"); /*        Code:        String reportsDirPath = context.getRealPath("/reports/");        File reportsDir = new File(reportsDirPath);        if (!reportsDir.exists()) {            throw new FileNotFoundException(String.valueOf(reportsDir));        }        parameters.put(JRParameter.REPORT_FILE_RESOLVER, new SimpleFileResolver(reportsDir));*///#############################################################################//O FileResolver serve para mapear a directoria onde se encontra o report//de forma a se poder ler, p.ex., a imagem do logotipo//#############################################################################$reportsDirPath = realpath(".");  $reportsDir     = new Java("java.io.File",$reportsDirPath);  $params->put("REPORT_FILE_RESOLVER", new Java("net.sf.jasperreports.engine.util.SimpleFileResolver",$reportsDir)); //echo "###$reportsDirPath###<br />";//#############################################################################//setLocale PT - Portugal//#############################################################################$params->put("REPORT_LOCALE", new Java("java.util.Locale",convertValue("PT", "java.lang.String") ) ); //$System->setProperty( "user.language", convertValue("pt", "java.lang.String") );//$System->setProperty( "user.region", convertValue("PT", "java.lang.String") );//echo "user.language => ". $System->getProperty( "user.language" ) . "<br />";//echo "user.region => ". $System->getProperty( "user.region" ) . "<br />";//#############################################################################//#############################################################################//INIT THE REPORT'S MYSQL DATASOURCE//#############################################################################$class = new JavaClass("java.lang.Class");$class->forName("com.mysql.jdbc.Driver");$driverManager = new JavaClass("java.sql.DriverManager");//$conn_DataSource = $driverManager->getConnection("jdbc:mysql://localhost/database", "user", "pass");$conn_DataSource = $driverManager->getConnection("jdbc:mysql://{$DBCONN->host}/{$DBCONN->db}", "{$DBCONN->user}", "{$DBCONN->pass}");  //$emptyDataSource = new Java("net.sf.jasperreports.engine.JREmptyDataSource");  $jasperPrint = $fillManager->fillReport($report, $params, $conn_DataSource);  $exportManager = new JavaClass("net.sf.jasperreports.engine.JasperExportManager");  //#############################################################################//EXPORT REPORT//#############################################################################$exportManager->exportReportToPdfFile($jasperPrint, $outputPath);// We'll be outputting a PDFheader('Content-type: application/pdf');// It will be called downloaded.pdfheader('Content-Disposition: attachment; filename="report.pdf"');   // The PDF source is in original.pdfreadfile($outputPath);//#############################################################################//Delete file//#############################################################################//unlink($outputPath);
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...