use of org.apache.syncope.core.persistence.api.entity.ReportExec in project syncope by apache.
the class ReportTest method deleteReportExecution.
@Test
public void deleteReportExecution() {
ReportExec execution = reportExecDAO.find("c13f39c5-0d35-4bff-ba79-3cd5de940369");
int executionNumber = execution.getReport().getExecs().size();
reportExecDAO.delete("c13f39c5-0d35-4bff-ba79-3cd5de940369");
reportExecDAO.flush();
assertNull(reportExecDAO.find("0062ea9c-924d-4ecf-9961-4492a8cc6d1b"));
Report report = reportDAO.find("0062ea9c-924d-4ecf-9961-4492a8cc6d1b");
assertEquals(report.getExecs().size(), executionNumber - 1);
}
use of org.apache.syncope.core.persistence.api.entity.ReportExec in project syncope by apache.
the class DefaultReportJobDelegate method execute.
@Transactional
@Override
public void execute(final String reportKey) throws JobExecutionException {
Report report = reportDAO.find(reportKey);
if (report == null) {
throw new JobExecutionException("Report " + reportKey + " not found");
}
if (!report.isActive()) {
LOG.info("Report {} not active, aborting...", reportKey);
return;
}
// 1. create execution
ReportExec execution = entityFactory.newEntity(ReportExec.class);
execution.setStatus(ReportExecStatus.STARTED);
execution.setStart(new Date());
execution.setReport(report);
execution = reportExecDAO.save(execution);
report.add(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 {
handler = TRANSFORMER_FACTORY.newTransformerHandler();
Transformer serializer = handler.getTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, StandardCharsets.UTF_8.name());
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);
status.set("Starting");
// 3. actual report execution
StringBuilder reportExecutionMessage = new StringBuilder();
try {
// report header
handler.startDocument();
AttributesImpl atts = new AttributesImpl();
atts.addAttribute("", "", ReportXMLConst.ATTR_NAME, ReportXMLConst.XSD_STRING, report.getName());
handler.startElement("", "", ReportXMLConst.ELEMENT_REPORT, atts);
status.set("Generating report header");
// iterate over reportlet instances defined for this report
for (int i = 0; i < report.getReportlets().size() && !interrupt; i++) {
Optional<Reportlet> reportlet = ImplementationManager.buildReportlet(report.getReportlets().get(i));
if (reportlet.isPresent()) {
try {
status.set("Invoking reportlet " + report.getReportlets().get(i).getKey());
reportlet.get().extract(handler, status);
} catch (Throwable t) {
LOG.error("While executing reportlet {} for report {}", reportlet, reportKey, t);
execution.setStatus(ReportExecStatus.FAILURE);
Throwable effective = t instanceof ReportException ? t.getCause() : t;
reportExecutionMessage.append(ExceptionUtils2.getFullStackTrace(effective)).append("\n==================\n");
}
}
}
if (interrupt) {
LOG.debug("Report job {} interrupted", reportKey);
interrupted = true;
}
// report footer
status.set("Generating report footer");
handler.endElement("", "", ReportXMLConst.ELEMENT_REPORT);
handler.endDocument();
if (!ReportExecStatus.FAILURE.name().equals(execution.getStatus())) {
execution.setStatus(ReportExecStatus.SUCCESS);
}
} catch (Exception e) {
execution.setStatus(ReportExecStatus.FAILURE);
reportExecutionMessage.append(ExceptionUtils2.getFullStackTrace(e));
throw new JobExecutionException(e, true);
} finally {
status.set("Completed");
try {
zos.closeEntry();
zos.close();
baos.close();
} catch (IOException e) {
LOG.error("While closing StreamResult's backend", e);
}
execution.setExecResult(baos.toByteArray());
execution.setMessage(reportExecutionMessage.toString());
execution.setEnd(new Date());
reportExecDAO.save(execution);
}
}
use of org.apache.syncope.core.persistence.api.entity.ReportExec in project syncope by apache.
the class ReportServiceImpl method exportExecutionResult.
@Override
public Response exportExecutionResult(final String executionKey, final ReportExecExportFormat fmt) {
ReportExecExportFormat format = (fmt == null) ? ReportExecExportFormat.XML : fmt;
ReportExec reportExec = logic.getReportExec(executionKey);
StreamingOutput sout = (os) -> logic.exportExecutionResult(os, reportExec, format);
return Response.ok(sout).header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + reportExec.getReport().getName() + "." + format.name().toLowerCase()).build();
}
use of org.apache.syncope.core.persistence.api.entity.ReportExec in project syncope by apache.
the class ReportLogic method getReportExec.
@PreAuthorize("hasRole('" + StandardEntitlement.REPORT_READ + "')")
public ReportExec getReportExec(final String executionKey) {
ReportExec reportExec = reportExecDAO.find(executionKey);
if (reportExec == null) {
throw new NotFoundException("Report execution " + executionKey);
}
if (!ReportExecStatus.SUCCESS.name().equals(reportExec.getStatus()) || reportExec.getExecResult() == null) {
SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidReportExec);
sce.getElements().add(reportExec.getExecResult() == null ? "No report data produced" : "Report did not run successfully");
throw sce;
}
return reportExec;
}
Aggregations