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