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