Search in sources :

Example 6 with Report

use of io.jmix.reports.entity.Report in project jmix by jmix-framework.

the class JsonEmailTemplateDetachListener method onBeforeDetach.

@Override
public void onBeforeDetach(JsonEmailTemplate entity) {
    if (entityStates.isLoaded(entity, "reportJson") && StringUtils.isNotBlank(entity.getReportJson())) {
        Report reportFromXml = templateConverter.convertToReport(entity);
        entity.setReport(reportFromXml);
        TemplateReport templateReport = metadata.create(TemplateReport.class);
        templateReport.setReport(reportFromXml);
        templateReport.setEmailTemplate(entity);
        templateReport.setParameterValues(new ArrayList<>());
        entity.setEmailBodyReport(templateReport);
    }
}
Also used : TemplateReport(io.jmix.emailtemplates.entity.TemplateReport) Report(io.jmix.reports.entity.Report) TemplateReport(io.jmix.emailtemplates.entity.TemplateReport)

Example 7 with Report

use of io.jmix.reports.entity.Report in project jmix by jmix-framework.

the class ReportImportExportImpl method exportReport.

/**
 * Exports single report to ZIP archive with name {@code <report name>.zip}.
 * There are 2 files in archive: report.structure and a template file (odt, xls or other..)
 *
 * @param report Report object that must be exported.
 * @return ZIP archive as a byte array.
 * @throws IOException if any I/O error occurs
 */
protected byte[] exportReport(Report report) throws IOException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(byteArrayOutputStream);
    zipOutputStream.setMethod(ZipArchiveOutputStream.STORED);
    zipOutputStream.setEncoding(ENCODING);
    report = reloadReport(report);
    if (report != null) {
        String xml = report.getXml();
        byte[] xmlBytes = xml.getBytes(StandardCharsets.UTF_8);
        ArchiveEntry zipEntryReportObject = newStoredEntry("report.structure", xmlBytes);
        zipOutputStream.putArchiveEntry(zipEntryReportObject);
        zipOutputStream.write(xmlBytes);
        Report xmlReport = reportsSerialization.convertToReport(xml);
        if (report.getTemplates() != null && xmlReport.getTemplates() != null) {
            for (int i = 0; i < report.getTemplates().size(); i++) {
                ReportTemplate xmlTemplate = xmlReport.getTemplates().get(i);
                ReportTemplate template = null;
                for (ReportTemplate it : report.getTemplates()) {
                    if (xmlTemplate.equals(it)) {
                        template = it;
                        break;
                    }
                }
                if (template != null && template.getContent() != null) {
                    byte[] fileBytes = template.getContent();
                    ArchiveEntry zipEntryTemplate = newStoredEntry("templates/" + i + "/" + template.getName(), fileBytes);
                    zipOutputStream.putArchiveEntry(zipEntryTemplate);
                    zipOutputStream.write(fileBytes);
                }
            }
        }
    }
    zipOutputStream.closeArchiveEntry();
    zipOutputStream.close();
    return byteArrayOutputStream.toByteArray();
}
Also used : Report(io.jmix.reports.entity.Report) ReportTemplate(io.jmix.reports.entity.ReportTemplate) ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 8 with Report

use of io.jmix.reports.entity.Report in project jmix by jmix-framework.

the class ReportImportExportImpl method fromByteArray.

@Nullable
protected Report fromByteArray(byte[] zipBytes) throws IOException {
    Report report = null;
    try (ZipArchiveInputStream archiveReader = new ZipArchiveInputStream(new ByteArrayInputStream(zipBytes))) {
        ZipArchiveEntry archiveEntry;
        // importing report.xml to report object
        while (((archiveEntry = archiveReader.getNextZipEntry()) != null) && (report == null)) {
            if (isReportsStructureFile(archiveEntry.getName())) {
                String xml = new String(readBytesFromEntry(archiveReader), StandardCharsets.UTF_8);
                if (xml.startsWith("<")) {
                    // previous xml structure version
                    XStreamConverter xStreamConverter = new XStreamConverter();
                    report = xStreamConverter.convertToReport(xml);
                } else {
                    // current json structure
                    report = reportsSerialization.convertToReport(xml);
                }
                report.setXml(xml);
            }
        }
    }
    return report;
}
Also used : XStreamConverter(io.jmix.reports.converter.XStreamConverter) Report(io.jmix.reports.entity.Report) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) Nullable(javax.annotation.Nullable)

Example 9 with Report

use of io.jmix.reports.entity.Report in project jmix by jmix-framework.

the class FluentReportRunner method buildContext.

/**
 * Creates an instance of {@link ReportRunContext} based on the parameters specified for the runner.
 *
 * @return run context
 */
public ReportRunContext buildContext() {
    Report report = getReportToUse();
    ReportTemplate reportTemplate = getReportTemplateToUse(report);
    return new ReportRunContext(report).setReportTemplate(reportTemplate).setOutputNamePattern(this.outputNamePattern).setOutputType(this.outputType).setParams(this.params);
}
Also used : Report(io.jmix.reports.entity.Report) ReportTemplate(io.jmix.reports.entity.ReportTemplate)

Example 10 with Report

use of io.jmix.reports.entity.Report in project jmix by jmix-framework.

the class ReportRunnerImpl method createReportDocumentInternal.

protected ReportOutputDocument createReportDocumentInternal(ReportRunContext context) {
    Report report = context.getReport();
    ReportTemplate template = context.getReportTemplate();
    ReportOutputType outputType = context.getOutputType();
    Map<String, Object> params = context.getParams();
    String outputNamePattern = context.getOutputNamePattern();
    StopWatch stopWatch = null;
    MDC.put("user", SecurityContextHolder.getContext().getAuthentication().getName());
    // executions.startExecution(report.getId().toString(), "Reporting");
    try {
        // TODO Slf4JStopWatch
        // stopWatch = new Slf4JStopWatch("Reporting#" + report.getName());
        Map<String, Object> resultParams = new HashMap<>(params);
        params.entrySet().stream().filter(param -> param.getValue() instanceof ParameterPrototype).forEach(param -> {
            ParameterPrototype prototype = (ParameterPrototype) param.getValue();
            List data = prototypesLoader.loadData(prototype);
            resultParams.put(param.getKey(), data);
        });
        if (template.isCustom()) {
            CustomFormatter customFormatter = applicationContext.getBean(CustomFormatter.class, report, template);
            template.setCustomReport(customFormatter);
        }
        com.haulmont.yarg.structure.ReportOutputType resultOutputType = (outputType != null) ? outputType.getOutputType() : template.getOutputType();
        return reportingAPI.runReport(new RunParams(report).template(template).params(resultParams).output(resultOutputType).outputNamePattern(outputNamePattern));
    } catch (NoFreePortsException nfe) {
        throw new NoOpenOfficeFreePortsException(nfe.getMessage());
    } catch (OpenOfficeException ooe) {
        throw new FailedToConnectToOpenOfficeException(ooe.getMessage());
    } catch (com.haulmont.yarg.exception.UnsupportedFormatException fe) {
        throw new UnsupportedFormatException(fe.getMessage());
    } catch (com.haulmont.yarg.exception.ValidationException ve) {
        throw new ValidationException(ve.getMessage());
    } catch (ReportingInterruptedException ie) {
        throw new ReportCanceledException(String.format("Report is canceled. %s", ie.getMessage()));
    } catch (com.haulmont.yarg.exception.ReportingException re) {
        // todo https://github.com/Haulmont/jmix-reports/issues/22
        // Throwable rootCause = ExceptionUtils.getRootCause(re);
        // if (rootCause instanceof ResourceCanceledException) {
        // throw new ReportCanceledException(String.format("Report is canceled. %s", rootCause.getMessage()));
        // }
        // noinspection unchecked
        List<Throwable> list = ExceptionUtils.getThrowableList(re);
        StringBuilder sb = new StringBuilder();
        for (Iterator<Throwable> it = list.iterator(); it.hasNext(); ) {
            // noinspection ThrowableResultOfMethodCallIgnored
            sb.append(it.next().getMessage());
            if (it.hasNext())
                sb.append("\n");
        }
        throw new ReportingException(sb.toString());
    } finally {
        // todo https://github.com/Haulmont/jmix-reports/issues/22
        // executions.endExecution();
        MDC.remove("user");
        MDC.remove("webContextName");
        if (stopWatch != null) {
            stopWatch.stop();
        }
    }
}
Also used : ReportingInterruptedException(com.haulmont.yarg.exception.ReportingInterruptedException) Id(io.jmix.core.Id) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) PrototypesLoader(io.jmix.reports.PrototypesLoader) ObjectProvider(org.springframework.beans.factory.ObjectProvider) FluentReportRunner(io.jmix.reports.runner.FluentReportRunner) ReportRunner(io.jmix.reports.runner.ReportRunner) ReportsUtils(io.jmix.reports.util.ReportsUtils) ReportsProperties(io.jmix.reports.ReportsProperties) ParameterPrototype(io.jmix.reports.app.ParameterPrototype) Map(java.util.Map) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) NoFreePortsException(com.haulmont.yarg.formatters.impl.doc.connector.NoFreePortsException) ReportExecutionHistoryRecorder(io.jmix.reports.ReportExecutionHistoryRecorder) DataManager(io.jmix.core.DataManager) ReportTemplate(io.jmix.reports.entity.ReportTemplate) Iterator(java.util.Iterator) ReportExecution(io.jmix.reports.entity.ReportExecution) io.jmix.reports.exception(io.jmix.reports.exception) ReportOutputType(io.jmix.reports.entity.ReportOutputType) StopWatch(org.apache.commons.lang3.time.StopWatch) ReportRunContext(io.jmix.reports.runner.ReportRunContext) ApplicationContext(org.springframework.context.ApplicationContext) Component(org.springframework.stereotype.Component) List(java.util.List) RunParams(com.haulmont.yarg.reporting.RunParams) MDC(org.slf4j.MDC) EntityStates(io.jmix.core.EntityStates) ReportOutputDocument(com.haulmont.yarg.reporting.ReportOutputDocument) ReportingAPI(com.haulmont.yarg.reporting.ReportingAPI) CustomFormatter(io.jmix.reports.libintegration.CustomFormatter) Report(io.jmix.reports.entity.Report) OpenOfficeException(com.haulmont.yarg.exception.OpenOfficeException) ExceptionUtils(org.apache.commons.lang3.exception.ExceptionUtils) io.jmix.reports.exception(io.jmix.reports.exception) ParameterPrototype(io.jmix.reports.app.ParameterPrototype) OpenOfficeException(com.haulmont.yarg.exception.OpenOfficeException) RunParams(com.haulmont.yarg.reporting.RunParams) HashMap(java.util.HashMap) ReportTemplate(io.jmix.reports.entity.ReportTemplate) ReportingInterruptedException(com.haulmont.yarg.exception.ReportingInterruptedException) ReportOutputType(io.jmix.reports.entity.ReportOutputType) Iterator(java.util.Iterator) List(java.util.List) NoFreePortsException(com.haulmont.yarg.formatters.impl.doc.connector.NoFreePortsException) Report(io.jmix.reports.entity.Report) StopWatch(org.apache.commons.lang3.time.StopWatch) CustomFormatter(io.jmix.reports.libintegration.CustomFormatter)

Aggregations

Report (io.jmix.reports.entity.Report)45 ReportTemplate (io.jmix.reports.entity.ReportTemplate)13 Subscribe (io.jmix.ui.screen.Subscribe)8 ReportOutputDocument (com.haulmont.yarg.reporting.ReportOutputDocument)7 TemplateReport (io.jmix.emailtemplates.entity.TemplateReport)7 MetaClass (io.jmix.core.metamodel.model.MetaClass)6 FluentUiReportRunner (io.jmix.reportsui.runner.FluentUiReportRunner)5 ReportWithParams (io.jmix.emailtemplates.dto.ReportWithParams)4 ReportInputParameter (io.jmix.reports.entity.ReportInputParameter)4 UiReportRunContext (io.jmix.reportsui.runner.UiReportRunContext)4 List (java.util.List)4 Autowired (org.springframework.beans.factory.annotation.Autowired)4 DataManager (io.jmix.core.DataManager)3 ParameterPrototype (io.jmix.reports.app.ParameterPrototype)3 ReportingException (io.jmix.reports.exception.ReportingException)3 ReportRunContext (io.jmix.reports.runner.ReportRunContext)3 LiteratureType (reports.ex2.entity.LiteratureType)3 Id (io.jmix.core.Id)2 CurrentUserSubstitution (io.jmix.core.usersubstitution.CurrentUserSubstitution)2 ParameterValue (io.jmix.emailtemplates.entity.ParameterValue)2