package com.capgemini.reports.jasper.printers;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperPrint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

/**
 * Prints a JASPER report to .PDF.
 */
public class JasperPDFPrinter implements JasperPrinter {
    private static final Logger LOGGER = LoggerFactory.getLogger(JasperPDFPrinter.class);

    private final String outputFilePath;
    private final String outputFileName;

    /**
     * Builds a PDF report printer with the provided parameters.
     *
     * @param outputFilePath the path to the folder where the final output report should be saved
     * @param outputFileName the final output report filename
     */
    public JasperPDFPrinter(final String outputFilePath, final String outputFileName) {
        this.outputFilePath = outputFilePath;
        this.outputFileName = outputFileName;
    }

    @Override
    public File print(final JasperPrint print) throws JRException {
        long start = System.currentTimeMillis();
        String theFileName = outputFilePath + '/' + outputFileName + ".pdf";
        JasperExportManager.exportReportToPdfFile(print, theFileName);
        LOGGER.info("PDF creation time {}: {} ms", theFileName, System.currentTimeMillis() - start);
        return new File(theFileName);
    }
}
