Search in sources :

Example 6 with ReportExec

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);
}
Also used : Report(org.apache.syncope.core.persistence.api.entity.Report) ReportExec(org.apache.syncope.core.persistence.api.entity.ReportExec) Test(org.junit.jupiter.api.Test) AbstractTest(org.apache.syncope.core.persistence.jpa.AbstractTest)

Example 7 with ReportExec

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);
    }
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Report(org.apache.syncope.core.persistence.api.entity.Report) Reportlet(org.apache.syncope.core.persistence.api.dao.Reportlet) ZipEntry(java.util.zip.ZipEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ReportExec(org.apache.syncope.core.persistence.api.entity.ReportExec) Date(java.util.Date) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) JobExecutionException(org.quartz.JobExecutionException) JobExecutionException(org.quartz.JobExecutionException) AttributesImpl(org.xml.sax.helpers.AttributesImpl) ZipOutputStream(java.util.zip.ZipOutputStream) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with ReportExec

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();
}
Also used : ReportExecExportFormat(org.apache.syncope.common.lib.types.ReportExecExportFormat) ReportLogic(org.apache.syncope.core.logic.ReportLogic) Autowired(org.springframework.beans.factory.annotation.Autowired) StreamingOutput(javax.ws.rs.core.StreamingOutput) ReportService(org.apache.syncope.common.rest.api.service.ReportService) List(java.util.List) HttpHeaders(javax.ws.rs.core.HttpHeaders) Response(javax.ws.rs.core.Response) Service(org.springframework.stereotype.Service) ReportExec(org.apache.syncope.core.persistence.api.entity.ReportExec) URI(java.net.URI) ReportTO(org.apache.syncope.common.lib.to.ReportTO) RESTHeaders(org.apache.syncope.common.rest.api.RESTHeaders) AbstractExecutableLogic(org.apache.syncope.core.logic.AbstractExecutableLogic) ReportExecExportFormat(org.apache.syncope.common.lib.types.ReportExecExportFormat) StreamingOutput(javax.ws.rs.core.StreamingOutput) ReportExec(org.apache.syncope.core.persistence.api.entity.ReportExec)

Example 9 with ReportExec

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;
}
Also used : SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) ReportExec(org.apache.syncope.core.persistence.api.entity.ReportExec) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

ReportExec (org.apache.syncope.core.persistence.api.entity.ReportExec)9 Report (org.apache.syncope.core.persistence.api.entity.Report)4 Date (java.util.Date)3 ReportTO (org.apache.syncope.common.lib.to.ReportTO)3 NotFoundException (org.apache.syncope.core.persistence.api.dao.NotFoundException)3 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)3 List (java.util.List)2 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)2 ExecTO (org.apache.syncope.common.lib.to.ExecTO)2 ReportExecExportFormat (org.apache.syncope.common.lib.types.ReportExecExportFormat)2 SchedulerException (org.quartz.SchedulerException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 Method (java.lang.reflect.Method)1 URI (java.net.URI)1 StandardCharsets (java.nio.charset.StandardCharsets)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1