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

0

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();
    }
 
}

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.

ramonc's picture
5
Joined: Jun 13 2017 - 8:33am
Last seen: 9 months 2 weeks ago

1 Answer:

0

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 :] )

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

 

ramonc's picture
5
Joined: Jun 13 2017 - 8:33am
Last seen: 9 months 2 weeks ago
Feedback