Search in sources :

Example 1 with NotSupportedExportFormatException

use of org.asqatasun.webapp.report.service.exception.NotSupportedExportFormatException in project Asqatasun by Asqatasun.

the class AuditExportResultController method exportAuditResultFromContract.

/**
     * The export view is only available for page result
     * 
     * @param webresourceId 
     * @param format 
     * @param request
     * @param response
     * @param model
     * @return
     */
@RequestMapping(value = TgolKeyStore.EXPORT_AUDIT_RESULT_CONTRACT_URL, method = RequestMethod.GET)
@Secured({ TgolKeyStore.ROLE_USER_KEY, TgolKeyStore.ROLE_ADMIN_KEY })
public String exportAuditResultFromContract(@RequestParam(value = TgolKeyStore.WEBRESOURCE_ID_KEY, required = false) String webresourceId, @RequestParam(value = TgolKeyStore.EXPORT_FORMAT_KEY, required = false) String format, HttpServletRequest request, HttpServletResponse response, Model model) {
    if (format == null || webresourceId == null) {
        throw new ForbiddenPageException();
    }
    //We first check that the current user is allowed to display the result
    //of this audit
    Long webResourceIdValue;
    try {
        webResourceIdValue = Long.valueOf(webresourceId);
    } catch (NumberFormatException nfe) {
        throw new ForbiddenPageException();
    }
    WebResource webResource = getWebResourceDataService().ligthRead(webResourceIdValue);
    // if the id of the webresource corresponds to a Site webResource
    if (isUserAllowedToDisplayResult(getAuditFromWebResource(webResource))) {
        // data are retrieved to be prepared and displayed
        try {
            prepareSuccessfullAuditDataToExport(webResource, model, getLocaleResolver().resolveLocale(request), format, request, response);
            return null;
        } catch (NotSupportedExportFormatException exc) {
            model.addAttribute(TgolKeyStore.WEBRESOURCE_ID_KEY, webresourceId);
            model.addAttribute(TgolKeyStore.EXPORT_FORMAT_KEY, format);
            LOGGER.warn(exc);
            return TgolKeyStore.EXPORT_AUDIT_FORMAT_ERROR_VIEW_REDIRECT_NAME;
        }
    }
    return TgolKeyStore.EXPORT_AUDIT_FORMAT_ERROR_VIEW_REDIRECT_NAME;
}
Also used : WebResource(org.asqatasun.entity.subject.WebResource) NotSupportedExportFormatException(org.asqatasun.webapp.report.service.exception.NotSupportedExportFormatException) ForbiddenPageException(org.asqatasun.webapp.exception.ForbiddenPageException) Secured(org.springframework.security.access.annotation.Secured) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with NotSupportedExportFormatException

use of org.asqatasun.webapp.report.service.exception.NotSupportedExportFormatException 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

NotSupportedExportFormatException (org.asqatasun.webapp.report.service.exception.NotSupportedExportFormatException)2 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 WebResource (org.asqatasun.entity.subject.WebResource)1 ForbiddenPageException (org.asqatasun.webapp.exception.ForbiddenPageException)1 ExportFormat (org.asqatasun.webapp.report.format.ExportFormat)1 Secured (org.springframework.security.access.annotation.Secured)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1