Pass CSV DataSource from java program to Report's Tables

0

I have a report made of one page which is generated usign a CSV Data Adapter. In the report I have some data and 2 table where both are taking data from CSV file. In a first moment, I had problem with table showing up: I put some data in myTables but nothing were shown. I have been able to solve the problem by using and mapping REPORT_PARAMETER_MAP in ParameterMap table’s section. Now I have the same problem, but calling myReport from a java program. In this last I have created a datasource which I am sending as paramater to the report. As a result, all data are showned, unless the 2 tables. In Jaspersof Studio I have defined a parameter called DS where its class is: “new net.sf.jasperreports.engine.data.JRCsvDataSource” and I am passing it to myTables as follow:

 

In java I have:

 

JasperPrint jasperPrint = null;
 
 
    try
    {
 
 
        String[] columnNames = new String[]{"xxx3", "xxx4", "xxx2", "xxx1"]
 
 
        JRCsvDataSource ds = new JRCsvDataSource(inp);
        ds.setFieldDelimiter(';');
        ds.setUseFirstRowAsHeader(true);
        ds.setColumnNames(columnNames);
 
        _parameters.put("DS", ds);
 
        jasperPrint = JasperFillManager.fillReport(_jasperReport, _parameters, ds);
    }
 
    catch (Exception e)
    {
        putKOReport(outputStreamEnvelopeFactory,e.toString().getBytes());;
        e.printStackTrace();
        writeErrorLog("OPS: readReportConfiguration " + e.getMessage() + " " + e);
        throw new RuntimeException(e);
    }
    return jasperPrint;
}
 
public void prepareReport() throws SQLException, OutputNotFoundException, OutputEnvelopeCreationException
{
            _parameters.put("PARAM1", "/main/prj/xxx1.png");
            _parameters.put("PARAM2", "/main/prj/xxx2.jpg");
            _parameters.put("PARAM3", "/main/prj/xxx3.jpg");
            _parameters.put("PARAM4", "/main/prj/xxx4.jpg");
 
    try
    {
        _jasperReport = JasperCompileManager.compileReport(pathReportJasper);
 
        writeInfoLog("OPS: Report Configuration "+pathReportJasper+" has been prepared");
    }
    catch (Exception e)
    {
        putKOReport(outputStreamEnvelopeFactory,e.toString().getBytes());;
        e.printStackTrace();
        writeErrorLog("OPS: prepareReport " + e.getMessage() + " " + e);
        throw new RuntimeException(e);
    }
}
 
public byte[] writeOutputFile(JasperPrint jasperPrint)
{
    byte[] reportPdf = null;
    try
    {
 
        reportPdf = JasperExportManager.exportReportToPdf(jasperPrint);
 
    }
    catch(Exception e)
    {
        putKOReport(outputStreamEnvelopeFactory,e.toString().getBytes());
        e.printStackTrace();
        writeErrorLog("OPS: writeOutputFile " + e.getMessage() + " " +e);
        throw new RuntimeException(e);
    }
    return reportPdf;
}

I am doing something wrong? Does anyone could help me, please?

Thank you in adavance!

emanuele.a89's picture
Joined: Dec 12 2017 - 8:49am
Last seen: 2 weeks 8 hours ago

2 Answers:

0

Same answer as posted here: https://stackoverflow.com/questions/60167856/pass-csv-datasource-from-ja...

 

You are reusing the same data source for filling the main dataset and the tables:

JRCsvDataSource ds = new JRCsvDataSource(inp);
...
_parameters.put("DS", ds);
 
jasperPrint = JasperFillManager.fillReport(_jasperReport, _parameters, ds);

This has unpredictable results because of the multiple consumers for the same data source.

If you have control over the InputStream source like the path to the file you could pass that as a parameter and construct a new instance of JRCsvDataSource for each table that needs it.

narcism's picture
1255
Joined: Nov 22 2010 - 12:39am
Last seen: 9 hours 55 min ago
0

0

 

I have been able to solve the problem: I created a datasource for each table using a cycle from my java program and creating a JRCsvDataSource parameter for each one them. This way all my Tables are getting printed with data. In JRDataSource expression I had to cast the object $P{DS} this way:

((net.sf.jasperreports.engine.data.JRCsvDataSource)$P{DS})

otherwise tables were not printed. The object alone does not work!

emanuele.a89's picture
Joined: Dec 12 2017 - 8:49am
Last seen: 2 weeks 8 hours ago
Feedback