Search in sources :

Example 1 with ExportFormat

use of org.asqatasun.webapp.report.format.ExportFormat in project Asqatasun by Asqatasun.

the class ExportService method export.

/**
     * Processes the download for Excel format
     * @param response
     * @param resourceId
     * @param auditStatistics
     * @param dataSource
     * @param locale
     * @param format
     * @throws ColumnBuilderException
     * @throws ClassNotFoundException
     * @throws JRException
     * @throws NotSupportedExportFormatException 
     */
@SuppressWarnings("unchecked")
public void export(HttpServletResponse response, long resourceId, AuditStatistics auditStatistics, Collection<?> dataSource, Locale locale, String format) throws ColumnBuilderException, ClassNotFoundException, JRException, NotSupportedExportFormatException {
    if (!exportFormatMap.containsKey(format)) {
        throw new NotSupportedExportFormatException(format);
    }
    ExportFormat exportFormat = exportFormatMap.get(format);
    DynamicReport dr = LayoutFactory.getInstance().buildReportLayout(locale, auditStatistics, format);
    // Retrieve our data source
    JRDataSource ds = new JRBeanCollectionDataSource(dataSource);
    // params is used for passing extra parameters
    JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dr, new ClassicLayoutManager(), ds);
    // Create our output byte stream
    // This is the stream where the data will be written
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    JRExporter exporter;
    try {
        exporter = (JRExporter) Class.forName(exportFormat.getExporterClassName()).newInstance();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
        exporter.exportReport();
        response.setHeader(CONTENT_DISPOSITION, INLINE_FILENAME + getFileName(resourceId, exportFormat.getFileExtension()));
        // Make sure to set the correct content type
        // Each format has its own content type
        response.setContentType(exportFormat.getFileType());
        response.setContentLength(baos.size());
        // Write to reponse stream
        writeReportToResponseStream(response, baos);
    } catch (InstantiationException | IllegalAccessException ex) {
        LOGGER.warn(ex);
    }
}
Also used : ClassicLayoutManager(ar.com.fdvs.dj.core.layout.ClassicLayoutManager) DynamicReport(ar.com.fdvs.dj.domain.DynamicReport) JRBeanCollectionDataSource(net.sf.jasperreports.engine.data.JRBeanCollectionDataSource) NotSupportedExportFormatException(org.asqatasun.webapp.report.service.exception.NotSupportedExportFormatException) ExportFormat(org.asqatasun.webapp.report.format.ExportFormat) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

ClassicLayoutManager (ar.com.fdvs.dj.core.layout.ClassicLayoutManager)1 DynamicReport (ar.com.fdvs.dj.domain.DynamicReport)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 JRBeanCollectionDataSource (net.sf.jasperreports.engine.data.JRBeanCollectionDataSource)1 ExportFormat (org.asqatasun.webapp.report.format.ExportFormat)1 NotSupportedExportFormatException (org.asqatasun.webapp.report.service.exception.NotSupportedExportFormatException)1