Search in sources :

Example 1 with ReportException

use of com.qcadoo.report.api.ReportException in project qcadoo by qcadoo.

the class ReportServiceImpl method generateReport.

@Transactional(readOnly = true)
@Override
public byte[] generateReport(final String templateContent, final ReportType type, final Map<String, Object> parameters, final Locale locale) throws ReportException {
    InputStream in = null;
    try {
        in = new ByteArrayInputStream(templateContent.getBytes("UTF-8"));
        JasperReport template = JasperCompileManager.compileReport(in);
        return generateReport(template, type, parameters, locale);
    } catch (JRException e) {
        throw new ReportException(ReportException.Type.NO_TEMPLATE_FOUND, e);
    } catch (UnsupportedEncodingException e) {
        throw new ReportException(ReportException.Type.NO_TEMPLATE_FOUND, e);
    } finally {
        IOUtils.closeQuietly(in);
    }
}
Also used : JRException(net.sf.jasperreports.engine.JRException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ReportException(com.qcadoo.report.api.ReportException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JasperReport(net.sf.jasperreports.engine.JasperReport) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with ReportException

use of com.qcadoo.report.api.ReportException in project qcadoo by qcadoo.

the class ReportController method convertJsonStringToMap.

private Map<String, String> convertJsonStringToMap(final String jsonText) throws ReportException {
    Map<String, String> result = new HashMap<String, String>();
    try {
        JSONObject userArgsObject = new JSONObject(jsonText);
        @SuppressWarnings("unchecked") Iterator<String> it = userArgsObject.keys();
        while (it.hasNext()) {
            String key = it.next();
            result.put(key, userArgsObject.getString(key));
        }
    } catch (JSONException e) {
        throw new ReportException(ReportException.Type.JSON_EXCEPTION, e);
    }
    return result;
}
Also used : JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) JSONException(org.json.JSONException) ReportException(com.qcadoo.report.api.ReportException)

Example 3 with ReportException

use of com.qcadoo.report.api.ReportException in project qcadoo by qcadoo.

the class ReportDevelopmentController method generateReport.

@RequestMapping(value = "developReport/generate", method = RequestMethod.POST)
public ModelAndView generateReport(@RequestParam(value = L_TEMPLATE) final String template, @RequestParam(value = "type") final String type, @RequestParam(value = L_LOCALE) final String locale, final HttpServletRequest request, final HttpServletResponse response) {
    if (!showReportDevelopment) {
        return new ModelAndView(new RedirectView("/"));
    }
    List<ReportParameter> params = null;
    try {
        params = getReportParameters(template);
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        return showException(L_QCADOO_REPORT_REPORT, e).addObject(L_TEMPLATE, template).addObject("isParameter", true).addObject(L_LOCALE, locale);
    }
    try {
        ReportType reportType = ReportType.valueOf(type.toUpperCase(Locale.ENGLISH));
        Locale reportLocale = new Locale(locale);
        Map<String, Object> parameters = new HashMap<String, Object>();
        for (ReportParameter param : params) {
            param.setValue(request.getParameter("params[" + param.getName() + "]"));
            parameters.put(param.getName(), param.getRawValue());
        }
        byte[] report = reportService.generateReport(template, reportType, parameters, reportLocale);
        response.setContentLength(report.length);
        response.setContentType(reportType.getMimeType());
        response.setHeader("Content-disposition", "attachment; filename=report." + type);
        response.addHeader("Expires", "Tue, 03 Jul 2001 06:00:00 GMT");
        response.addHeader("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.addHeader("Pragma", "no-cache");
        OutputStream out = response.getOutputStream();
        try {
            IOUtils.copy(new ByteArrayInputStream(report), out);
        } catch (IOException e) {
            LOG.error(e.getMessage(), e);
            throw new ReportException(ReportException.Type.ERROR_WHILE_COPYING_REPORT_TO_RESPONSE, e);
        }
        out.flush();
        return null;
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        return showException(L_QCADOO_REPORT_REPORT, e).addObject(L_TEMPLATE, template).addObject("isParameter", true).addObject(L_PARAMS, params).addObject(L_LOCALE, locale);
    }
}
Also used : ModelAndView(org.springframework.web.servlet.ModelAndView) JDOMException(org.jdom.JDOMException) ReportException(com.qcadoo.report.api.ReportException) RedirectView(org.springframework.web.servlet.view.RedirectView) ReportException(com.qcadoo.report.api.ReportException) ReportType(com.qcadoo.report.api.ReportService.ReportType) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with ReportException

use of com.qcadoo.report.api.ReportException in project qcadoo by qcadoo.

the class ReportServiceImpl method generateReport.

private byte[] generateReport(final JasperReport template, final ReportType type, final Map<String, Object> parameters, final Locale locale) throws ReportException {
    Session session = null;
    try {
        session = sessionFactory.openSession();
        parameters.put(JRParameter.REPORT_LOCALE, locale);
        parameters.put("Author", pdfHelper.getDocumentAuthor());
        parameters.put(JRHibernateQueryExecuterFactory.PARAMETER_HIBERNATE_SESSION, session);
        ResourceBundle resourceBundle = new MessageSourceResourceBundle(messageSource, locale);
        parameters.put(JRParameter.REPORT_RESOURCE_BUNDLE, resourceBundle);
        parameters.put(JRParameter.REPORT_FORMAT_FACTORY, new ReportFormatFactory());
        JasperPrint jasperPrint = JasperFillManager.fillReport(template, parameters);
        JRExporter exporter = getExporter(type);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, stream);
        exporter.exportReport();
        return stream.toByteArray();
    } catch (JRException e) {
        throw new ReportException(ReportException.Type.GENERATE_REPORT_EXCEPTION, e);
    } finally {
        if (session != null) {
            session.close();
        }
    }
}
Also used : MessageSourceResourceBundle(org.springframework.context.support.MessageSourceResourceBundle) JRException(net.sf.jasperreports.engine.JRException) JasperPrint(net.sf.jasperreports.engine.JasperPrint) ReportFormatFactory(com.qcadoo.report.internal.util.ReportFormatFactory) ReportException(com.qcadoo.report.api.ReportException) MessageSourceResourceBundle(org.springframework.context.support.MessageSourceResourceBundle) ResourceBundle(java.util.ResourceBundle) JRExporter(net.sf.jasperreports.engine.JRExporter) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) Session(org.hibernate.Session)

Example 5 with ReportException

use of com.qcadoo.report.api.ReportException in project qcadoo by qcadoo.

the class ReportController method getReportType.

private ReportService.ReportType getReportType(final HttpServletRequest request) throws ReportException {
    String uri = request.getRequestURI();
    String type = uri.substring(uri.lastIndexOf('.') + 1).toUpperCase(Locale.getDefault());
    try {
        return ReportService.ReportType.valueOf(type);
    } catch (IllegalArgumentException e) {
        throw new ReportException(ReportException.Type.WRONG_REPORT_TYPE, e, type);
    }
}
Also used : ReportException(com.qcadoo.report.api.ReportException)

Aggregations

ReportException (com.qcadoo.report.api.ReportException)6 ByteArrayInputStream (java.io.ByteArrayInputStream)2 JRException (net.sf.jasperreports.engine.JRException)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ReportService (com.qcadoo.report.api.ReportService)1 ReportType (com.qcadoo.report.api.ReportService.ReportType)1 ReportFormatFactory (com.qcadoo.report.internal.util.ReportFormatFactory)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HashMap (java.util.HashMap)1 ResourceBundle (java.util.ResourceBundle)1 JRExporter (net.sf.jasperreports.engine.JRExporter)1 JasperPrint (net.sf.jasperreports.engine.JasperPrint)1 JasperReport (net.sf.jasperreports.engine.JasperReport)1 ByteArrayOutputStream (org.apache.commons.io.output.ByteArrayOutputStream)1 Session (org.hibernate.Session)1 JDOMException (org.jdom.JDOMException)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1