}
@Override
public void execute(final JobExecutionContext context) throws JobExecutionException {
Report report = reportDAO.find(reportId);
if (report == null) {
throw new JobExecutionException("Report " + reportId + " not found");
}
// 1. create execution
ReportExec execution = new ReportExec();
execution.setStatus(ReportExecStatus.STARTED);
execution.setStartDate(new Date());
execution.setReport(report);
execution = reportExecDAO.save(execution);
report.addExec(execution);
report = reportDAO.save(report);
// 2. define a SAX handler for generating result as XML
TransformerHandler handler;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos);
zos.setLevel(Deflater.BEST_COMPRESSION);
try {
SAXTransformerFactory transformerFactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
handler = transformerFactory.newTransformerHandler();
Transformer serializer = handler.getTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
// a single ZipEntry in the ZipOutputStream
zos.putNextEntry(new ZipEntry(report.getName()));
// streaming SAX handler in a compressed byte array stream
handler.setResult(new StreamResult(zos));
} catch (Exception e) {
throw new JobExecutionException("While configuring for SAX generation", e, true);
}
execution.setStatus(ReportExecStatus.RUNNING);
execution = reportExecDAO.save(execution);
ConfigurableListableBeanFactory beanFactory =
ApplicationContextProvider.getApplicationContext().getBeanFactory();
// 3. actual report execution
StringBuilder reportExecutionMessage = new StringBuilder();
StringWriter exceptionWriter = new StringWriter();
try {
// report header
handler.startDocument();
AttributesImpl atts = new AttributesImpl();
atts.addAttribute("", "", ATTR_NAME, XSD_STRING, report.getName());
handler.startElement("", "", ELEMENT_REPORT, atts);
// iterate over reportlet instances defined for this report
for (ReportletConf reportletConf : report.getReportletConfs()) {
Class<Reportlet> reportletClass =
dataBinder.findReportletClassHavingConfClass(reportletConf.getClass());
if (reportletClass != null) {
Reportlet autowired = (Reportlet) beanFactory.createBean(reportletClass,
AbstractBeanDefinition.AUTOWIRE_BY_TYPE, false);