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

Get Value from Parameter in DatasourceProvider


carsten07

Recommended Posts

Hi, i'm evaluating ireport and i have a problem with my own Datasourceprovider. How can i access a report parameter in the datasourceprovider?

Here is the code:

Code:

public class MyDatasourceProvider extends JRAbstractBeanDataSourceProvider
{
private static Logger log =
Logger.getLogger(MyDatasourceProvider.class);

public MyDatasourceProvider()
{
super(Test.class);
}

/*
* @see net.sf.jasperreports.engine.JRDataSourceProvider#create(net.sf.jasperreports.engine.JasperReport)
*/
public JRDataSource create(JasperReport report) throws JRException
{
Collection collection = null;
JRParameter[] param = report.getParameters();
log.debug("Param: " + param.length);
for(int i=0;i<param.length;i++)
{
log.debug("Param[" + i + "]: " + param.getName());
log.debug("Paramclass[" + i + "]: " + param.getClass());
log.debug("Paramvclass[" + i + "]: " + param.getValueClass());
log.debug("Param[" + i + "]: " + param.getDescription());
log.debug("Param[" + i + "]: " + param.getDefaultValueExpression());
}
return new JRBeanCollectionDataSource(collection);
}

/*
* @see net.sf.jasperreports.engine.JRDataSourceProvider#dispose(net.sf.jasperreports.engine.JRDataSource)
*/
public void dispose(JRDataSource arg0) throws JRException
{
}

 

I see the params but i've no idea how to access them?

 

Thanks in advance

 

Carsten

Link to comment
Share on other sites

  • Replies 5
  • Created
  • Last Reply

Top Posters In This Topic

This should be a plain and simple Java solution

 

Add

Code:
private JRParameter[] params = null;

to the class.

 

In your create method dont say

Code:
[code]JRParameter[] param = report.getParameters();

but say

Code:
[code]this.params = report.getParameters();

 

Then create a getParams() method that returns the param array or create a method getParam(String name) that returns a param with a certain name.

 

Or would you like to find out how to access the parameters from your report?

 

Post edited by: martynhiemstra, at: 2007/03/01 07:19

Post edited by: martynhiemstra, at: 2007/03/01 07:41

Link to comment
Share on other sites

  • 2 weeks later...

I am trying to do the same thing. I have tryed many things but none have worked. Here is my latest attempt

 

Code:
public class MyDataSourceProvider extends JRAbstractBeanDataSourceProvider{

// some code

public JRDataSource create(JasperReport report) throws JRException
{
Object clubName = getParameterValue(report, "clubName"«»);
// some code
JRBeanArrayDataSource beanDataSource = new JRBeanArrayDataSource(this.items.toArray());
return beanDataSource;
}

public static Object getParameterValue(JasperReport report, String paramName)
{
JRParameter[] param = report.getParameters();
if(param != null)
{
for(int i = 0; i < param.length; ++i)
{
if(paramName.equals(param.getName()))
{
if(param instanceof JRValueParameter)
{
return ((JRValueParameter)param).getValue();
}
}
}

return "no value";
}
else
{
return "getParameters is Null";
}
}
}

 

This last attempt seemed to be the most promising, but it seams that none of the parameters are of type JRValueParameter. If I find a solution I’ll post it.

Link to comment
Share on other sites

  • 4 years 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...