Jump to content
Changes to the Jaspersoft community edition download ×

Bean DataSource - Pass Parameter


arunmadhavan

Recommended Posts

Dear All,

Is there any way to pass parameters (from the JSP for running Input Controls page) to Bean data source? I would like to generate data in bean depending on parameters, which is input by user and fill report. Hope any of you guys have faced similar kind of problem and found a solution for that. Appreciate your help in advance.

Regards,

Arun

Link to comment
Share on other sites

  • Replies 8
  • Created
  • Last Reply

Top Posters In This Topic

You can only do that if the parameters you want to pass to the bean data source are also defined in the report (JRXML). In this case, you can create input controls for the parameters in the report unit, and the user values will be present in the parameters map passed to your ReportDataSourceService.setReportParameterValues(Map parameterValues) implementation.

 

HTH,

Lucian

Link to comment
Share on other sites

  • 2 weeks later...

You nead to create a bean that contains a method. This method must return an instance of ReportDataSourceService.

 

Add this bean to your application-context.xml file.

 

Use this bean as datasource by creating a bean datasource in JasperIntelligence and set the bean name to the name given in your configuration and set the method name to the name given to the method that returns the ReportDataSourceService.

Link to comment
Share on other sites

I dont use jasperIntelligence, the reporting is implemented into a swing gui.

 

On some cases my sql query is not effectual enough.

 

I created a factory class which returns a collection of rows. But the trick is the factory has to return parameter depended rows.

 

Factory code:

 

Code:
public class TPReportFactory {
public static ArrayList<TPExercisesData> collection =
new ArrayList<TPExercisesData>();
public static void populateCollection(/*String personnr*/){
/**
* do some initializations..
*/
}
public static java.util.Collection generateCollection(){
populateCollection(/*parameter*/);
return collection;
}
}

 

how can i populate the collection with a parameter?

Link to comment
Share on other sites

I dont use jasperIntelligence, the reporting is implemented into a swing gui.

 

On some cases my sql query is not effectual enough.

 

I created a factory class which returns a collection of rows. But the trick is the factory has to return parameter depended rows.

 

Factory code:

 

Code:
public class TPReportFactory {
public static ArrayList<TPExercisesData> collection =
new ArrayList<TPExercisesData>();
public static void populateCollection(/*String personnr*/){
/**
* do some initializations..
*/
}
public static java.util.Collection generateCollection(){
populateCollection(/*parameter*/);
return collection;
}
}

 

how can i populate the collection with a parameter?

Link to comment
Share on other sites

  • 7 years later...

Given a class that implements JRDataSource, such as:

import java.util.List;import net.sf.jasperreports.engine.JRDataSource;import net.sf.jasperreports.engine.JRException;import net.sf.jasperreports.engine.JRField;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.joda.time.DateTime;public class TestDataSource implements JRDataSource {    private int index = -1;    private List<TestObject> data;    private Logger logger=LogManager.getLogger();       public EBDataSource(java.sql.Date param) {        logger.debug("Single date parameter submitted: "+param.toString());        data = TestDataConstructor.createBean(param);    }       @Override    public boolean next() throws JRException {    index++;        return (index < data.size());    }       @Override    public Object getFieldValue(JRField field) throws JRException {        Object value = null;        String fieldName = field.getName();        switch (fieldName) {            case "field1":                value = data.get(index).getField1();                break;            case "field2":                value = data.get(index).getField2();                break;        }        return value;    }}[/code]

Note that TestDataConstructor.createBean(param) is a static method to get whatever information you want.  The TestDataSource class above must be called from another class that implements ReportDataSourceService, such as:

import java.util.Map;import net.sf.jasperreports.engine.JRDataSource;import net.sf.jasperreports.engine.JRParameter;import com.jaspersoft.jasperserver.api.metadata.common.service.RepositoryService;import com.jaspersoft.jasperserver.api.metadata.jasperreports.service.ReportDataSourceService;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;public class TestDataSourceService implements ReportDataSourceService {    JRDataSource jrds;    private RepositoryService repository;    private Map propertyMap;    private Logger logger=LogManager.getLogger();       public TestDataSourceService() {        //jrds = new TestDataSource();    }    public TestDataSourceService(JRDataSource ds) {        this.jrds = ds;    }       @Override    public void setReportParameterValues(Map parameterValues) {        for (Object obj : parameterValues.entrySet()) {            logger.debug("Parameter "+obj.toString());        }        jrds = new TestDataSource((java.sql.Date) parameterValues.get("PeriodEndDate"));        parameterValues.put(JRParameter.REPORT_DATA_SOURCE, jrds);    }       @Override    public void closeConnection() {        // Do nothing    }       public Map getPropertyMap() {        return propertyMap;    }    public void setPropertyMap(Map propertyMap) {        this.propertyMap = propertyMap;    }       public RepositoryService getRepository() {    return repository;    }    public void setRepository(RepositoryService repository) {    this.repository = repository;    }}[/code]

The key to passing parameters to a custom datasource is to create the data source after the parameter Map is passed in, and then use that parameter in your bean.

For the sake of completeness, you also need:

import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.ReportDataSource;import com.jaspersoft.jasperserver.api.metadata.jasperreports.service.ReportDataSourceService;import com.jaspersoft.jasperserver.api.metadata.jasperreports.service.ReportDataSourceServiceFactory;public class TestDataSourceServiceFactory implements ReportDataSourceServiceFactory {       public TestDataSourceServiceFactory() { }       public TestDataSourceService createDataSourceService() {        return new TestDataSourceService();    }       @Override    public ReportDataSourceService createService(ReportDataSource dataSource) {        return new TestDataSourceService();    }}[/code]

 

Link to comment
Share on other sites

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