Search in sources :

Example 1 with ReportType

use of com.qcadoo.report.api.ReportService.ReportType 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)

Aggregations

ReportException (com.qcadoo.report.api.ReportException)1 ReportType (com.qcadoo.report.api.ReportService.ReportType)1 JDOMException (org.jdom.JDOMException)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1 RedirectView (org.springframework.web.servlet.view.RedirectView)1