Search code examples
javajasper-reports

JasperReport setParameter() Deprecated?


I have recently upgraded the Jasper Reports library for my project, from 3.7.6 to 6.0.0. I can finally Maven build and the reports are working just great. However, the setParameter() function appears to have been deprecated between releases, and I am unsure how to refactor my code to accommodate for this.

Example of Deprecated Code:

private static void exportMultipleToCSV(Collection<JasperPrint> jasperPrints, OutputStream baos) throws JRException {
    JRCsvExporter csvExporter = new JRCsvExporter();

    csvExporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrints);
    csvExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
    csvExporter.setParameter(JRTextExporterParameter.PAGE_HEIGHT, Integer.valueOf(1500000));
    csvExporter.setParameter(JRTextExporterParameter.PAGE_WIDTH, Integer.valueOf(40000000));
    csvExporter.setParameter(JRTextExporterParameter.CHARACTER_WIDTH, Integer.valueOf(4));
    csvExporter.setParameter(JRTextExporterParameter.CHARACTER_HEIGHT, Integer.valueOf(15));

    csvExporter.exportReport();
}

I have looked through the SourceForge page, and can see that it has been replaced by ExporterInput, ExporterConfiguration and ExporterOutput but I am unsure how to utilise them all together to achieve the desired output.


Solution

  • The equivalent code would look something like this:

    JRCsvExporter csvExporter = new JRCsvExporter();
    //jasperPrints is Collection, but we need a List
    csvExporter.setExporterInput(SimpleExporterInput.getInstance(new ArrayList(jasperPrints)));
    csvExporter.setExporterOutput(new SimpleWriterExporterOutput(baos));
    SimpleCsvExporterConfiguration exporterConfiguration = new SimpleCsvExporterConfiguration();
    //nothing to set here, but you could do things like exporterConfiguration.setFieldDelimiter
    csvExporter.setConfiguration(exporterConfiguration);
    csvExporter.exportReport();
    

    Note that the old API allowed you do things like csvExporter.setParameter(JRTextExporterParameter.PAGE_HEIGHT). The problem with that was that the CSV exporter did not actually use that parameter, only the text exporter was looking at JRTextExporterParameter.PAGE_HEIGHT. With the new API, it's clear what settings/configuration you can do on each exporter.