ReportTemplate template = new ReportDAO().loadByName(reportName);
if (template == null)
throw new ReportException("Report template does not exist!");
// COMPILE REPORT
JasperReport jasperReport = null;
ProcessToolContext ctx = ProcessToolContext.Util.getThreadProcessToolContext();
try {
ByteArrayInputStream contentInputStream = getContentInputStream(template.getContent());
Thread t = Thread.currentThread();
ClassLoader previousLoader = t.getContextClassLoader();
try {
ClassLoader newClassLoader = ctx.getRegistry().getModelAwareClassLoader(getClass().getClassLoader());
t.setContextClassLoader(newClassLoader);
jasperReport = JasperCompileManager.compileReport(contentInputStream);
} finally {
t.setContextClassLoader(previousLoader);
}
} catch (Exception e) {
throw new ReportException("Report compilation failed!", e);
}
// PREPARE INPUT
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put(JRXPathQueryExecuterFactory.XML_DATE_PATTERN, DATETIME_PATTERN);
parameters.put(JRParameter.REPORT_LOCALE, locale);
parameters.put(PROCESS_CONTEXT_KEY, ctx);
// FILL REPORT
JasperPrint jasperPrint = null;
try {
Thread t = Thread.currentThread();
ClassLoader previousLoader = t.getContextClassLoader();
try {
ClassLoader newClassLoader = ctx
.getRegistry().getModelAwareClassLoader(getClass().getClassLoader());
t.setContextClassLoader(newClassLoader);
parameters.put(DICTIONARY_HELPER, new DictionaryHelperImpl(processInstance));
if (jasperReport.getQuery() != null && jasperReport.getQuery().getLanguage().equals("xPath")) {
XStream xs = new XStream();
xs.registerConverter(new MyPersistentSetConverter(xs.getMapper()), XStream.PRIORITY_VERY_HIGH);
xs.omitField(ProcessInstance.class, "definition");
xs.omitField(ProcessInstance.class, "processLogs");
String s = xs.toXML(processInstance);
jasperPrint = JasperFillManager.fillReport(jasperReport,
parameters,
new JRXmlDataSource(
new ByteArrayInputStream(s.getBytes())
));
} else if (jasperReport.getQuery() != null && hasText(jasperReport.getQuery().getText())) {
parameters.put(PROCESS_INSTANCE_ID_KEY, String.valueOf(processInstance.getId()));
jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, ctx.getHibernateSession().connection());
} else {
parameters.put(PROCESS_INSTANCE_KEY, processInstance);
jasperPrint = JasperFillManager.fillReport(jasperReport,
parameters,
new JREmptyDataSource());
}
} finally {
t.setContextClassLoader(previousLoader);
}
} catch (Exception e) {
throw new ReportException("Report filling failed!", e);
}
// EXPORT REPORT
byte[] report = null;
try {
Thread t = Thread.currentThread();
ClassLoader previousLoader = t.getContextClassLoader();
try {
ClassLoader newClassLoader = ctx
.getRegistry().getModelAwareClassLoader(getClass().getClassLoader());
t.setContextClassLoader(newClassLoader);
report = exportReport(jasperPrint, format, encoding);
} finally {
t.setContextClassLoader(previousLoader);