Jump to content
Changes to the Jaspersoft community edition download ×

Pass paramaters using jasperserver API to a datasource invoked by a report


Recommended Posts

Hi all,

At the moment I'm not sure how to proceed  since I have created  a custom datasource that connects with a RESTful service that provides a JSON document that is parsed and displayed on the report. The service needs to send some parameters to obtain the document wanted. I find no way of extracting these parameters, getting and using their value.

Here is my datasource code:

package dataSources;import com.eqb.utilities.HttpRestClient;import java.io.IOException;import java.nio.charset.StandardCharsets;import java.nio.file.Files;import java.nio.file.Paths;import java.util.ArrayList;import java.util.HashMap;import java.util.LinkedHashMap;import java.util.List;import java.util.Map;import java.util.Set;import org.apache.commons.lang.ArrayUtils;import org.json.JSONException;import org.json.JSONObject;import com.fasterxml.jackson.core.JsonParseException;import com.fasterxml.jackson.core.type.TypeReference;import com.fasterxml.jackson.databind.JsonMappingException;import com.fasterxml.jackson.databind.ObjectMapper;import net.sf.jasperreports.engine.JRDataSource;import net.sf.jasperreports.engine.JRException;import net.sf.jasperreports.engine.JRField;import net.sf.jasperreports.engine.JasperReport;public class AnnualStatementDataSource implements JRDataSource {        private Integer counter = -1;    private String jsonText = "";        @Override    public boolean next() throws JRException {        if(counter < 0)            return true;        return false;    }        @Override    public Object getFieldValue(JRField jrField) throws JRException {        HttpRestClient http = new HttpRestClient();        String fieldValue = "";        counter++;                jsonText = http.sendPostRequest("some_url", "jasper_parameter");        fieldValue = getFieldValueFromJson(jsonText,jrField.getName());                return fieldValue;    }        public static JRDataSource StatementDataSource(){        return new StatementDataSource();    }    }[/code]

 IMPORTANT NOTE: The report instantiates this datasource using a data adapter that invokes this datasource.

If anyone could point me into the right direction, thank you and please.





Link to comment
Share on other sites

  • 2 weeks later...
  • Replies 1
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

I managed to solve this by folowing this thread using part of the code written in the comments:
"look at this for source code example"

I will resume this in basic steps:
1) Create a java class that implements JRDatasource

2) Create a java class that implements JRDatasourceProvider

3) Create a java class that extends AbstractQueryExecuterFactory

4) From the overriden createQueryExecuter method process whatever is needed to then return an instance of your JRDatasourceProvider Class, make sure to pass the report and report's parameter mapping.

5) In your provider class setup what you need to connect and pass paramenters to the JRDatasource class implemeted (I assume here is where you will access  any source and compile the necessary data to fill out the fields of the report)

6) Create a data adapter that uses "java class which implements the JRDataSourceProvider interface"

7) Add the data adapter created to the report and set to read from the query executer created (the custom query executer must be imported into jasperstudio look at here). This is done opening the query and dataset dialog.

8) Should be ready to go

NOTE: create the parameters in the report design view, and the parameter map that the method recieves in the query executer should appear.
NOTE2: If report has sub datasets that also read from custom data source, a repetition of these steps should be used (I did some bad practice therefore not publishing the actual source code, basically create a boolean parameter inside the subset and read it in the executer and instantiate whatever needed to get to the sub data set data source :] )

- If you are having problems with null pointers, enable the jss console in the settings and start printing to system.out, should help figure out whats going on.
- If empty document, check your datasource to see if it actually is returning data or that the query executer was imported correctly


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