Search in sources :

Example 1 with FailedToConnectToOpenOfficeException

use of io.jmix.reports.exception.FailedToConnectToOpenOfficeException in project jmix by jmix-framework.

the class ReportRestControllerManager method runReport.

public ReportRestResult runReport(String entityId, String bodyJson) {
    Report report = loadReportInternal(entityId);
    final ReportRunRestBody body;
    try {
        body = createGson().fromJson(bodyJson, ReportRunRestBody.class);
    } catch (JsonSyntaxException e) {
        throw new RestAPIException("Invalid JSON body", e.getMessage(), HttpStatus.BAD_REQUEST, e);
    }
    if (body.template != null) {
        ReportTemplate reportTemplate = report.getTemplates().stream().filter(t -> Objects.equals(t.getCode(), body.template)).findFirst().orElseThrow(() -> new RestAPIException("Template not found", String.format("Template with code %s not found for report %s", body.template, entityId), HttpStatus.BAD_REQUEST));
        checkReportOutputType(reportTemplate);
    } else {
        checkReportOutputType(report.getDefaultTemplate());
    }
    Map<String, Object> preparedValues = prepareValues(report, body.parameters);
    if (body.template != null) {
        try {
            ReportOutputDocument document = reportRunner.byReportEntity(report).withTemplateCode(body.template).withParams(preparedValues).run();
            return new ReportRestResult(document, body.attachment);
        } catch (FailedToConnectToOpenOfficeException e) {
            throw new RestAPIException("Run report error", "Couldn't find LibreOffice instance", HttpStatus.INTERNAL_SERVER_ERROR);
        } catch (NoOpenOfficeFreePortsException e) {
            throw new RestAPIException("Run report error", "Couldn't connect to LibreOffice instance. No free ports available.", HttpStatus.INTERNAL_SERVER_ERROR);
        } catch (ReportingException e) {
            throw new RestAPIException("Run report error", e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    } else {
        try {
            return new ReportRestResult(reportRunner.run(new ReportRunContext(report).setParams(preparedValues)), body.attachment);
        } catch (FailedToConnectToOpenOfficeException e) {
            throw new RestAPIException("Run report error", "Couldn't find LibreOffice instance", HttpStatus.INTERNAL_SERVER_ERROR);
        } catch (NoOpenOfficeFreePortsException e) {
            throw new RestAPIException("Run report error", "Couldn't connect to LibreOffice instance. No free ports available.", HttpStatus.INTERNAL_SERVER_ERROR);
        } catch (ReportingException e) {
            throw new RestAPIException("Run report error", e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}
Also used : ReportOutputDocument(com.haulmont.yarg.reporting.ReportOutputDocument) ReportingException(io.jmix.reports.exception.ReportingException) ReportRunContext(io.jmix.reports.runner.ReportRunContext) JsonSyntaxException(com.google.gson.JsonSyntaxException) NoOpenOfficeFreePortsException(io.jmix.reports.exception.NoOpenOfficeFreePortsException) FailedToConnectToOpenOfficeException(io.jmix.reports.exception.FailedToConnectToOpenOfficeException)

Example 2 with FailedToConnectToOpenOfficeException

use of io.jmix.reports.exception.FailedToConnectToOpenOfficeException in project jmix by jmix-framework.

the class UiReportRunnerImpl method runInBackground.

protected void runInBackground(UiReportRunContext context, Screen hostScreen) {
    Report targetReport = getReportForPrinting(context.getReport());
    long timeout = reportsClientProperties.getBackgroundReportProcessingTimeoutMs();
    BackgroundTask<Integer, ReportOutputDocument> task = new BackgroundTask<Integer, ReportOutputDocument>(timeout, TimeUnit.MILLISECONDS, hostScreen) {

        @SuppressWarnings("UnnecessaryLocalVariable")
        @Override
        public ReportOutputDocument run(TaskLifeCycle<Integer> taskLifeCycle) {
            context.setReport(targetReport);
            ReportOutputDocument result = reportRunner.run(context.getReportRunContext());
            return result;
        }

        @Override
        public boolean handleException(Exception ex) {
            if (ex instanceof ReportingException) {
                if (ex instanceof FailedToConnectToOpenOfficeException) {
                    String caption = messages.getMessage("io.jmix.reportsui.exception", "reportException.failedConnectToOffice");
                    return showErrorNotification(caption);
                } else if (ex instanceof NoOpenOfficeFreePortsException) {
                    String caption = messages.getMessage("io.jmix.reportsui.exception", "reportException.noOpenOfficeFreePorts");
                    return showErrorNotification(caption);
                }
            }
            return super.handleException(ex);
        }

        protected boolean showErrorNotification(String text) {
            Screen ownerScreen = this.getOwnerScreen();
            if (ownerScreen != null) {
                ScreenContext screenContext = ComponentsHelper.getScreenContext(ownerScreen.getWindow());
                Notifications notifications = screenContext.getNotifications();
                notifications.create().withCaption(text).withType(Notifications.NotificationType.ERROR).show();
                return true;
            }
            return false;
        }

        @Override
        public void done(ReportOutputDocument document) {
            showResult(document, context);
        }

        @Override
        public void canceled() {
            super.canceled();
        // todo https://github.com/Haulmont/jmix-reports/issues/22
        // reportService.cancelReportExecution(userSessionId, report.getId());
        }
    };
    showDialog(task);
}
Also used : TaskLifeCycle(io.jmix.ui.executor.TaskLifeCycle) ReportOutputDocument(com.haulmont.yarg.reporting.ReportOutputDocument) ReportingException(io.jmix.reports.exception.ReportingException) ShowPivotTableScreen(io.jmix.reportsui.screen.report.run.ShowPivotTableScreen) ShowReportTableScreen(io.jmix.reportsui.screen.report.run.ShowReportTableScreen) ShowChartScreen(io.jmix.reportsui.screen.report.run.ShowChartScreen) NoOpenOfficeFreePortsException(io.jmix.reports.exception.NoOpenOfficeFreePortsException) ReportingException(io.jmix.reports.exception.ReportingException) FailedToConnectToOpenOfficeException(io.jmix.reports.exception.FailedToConnectToOpenOfficeException) BackgroundTask(io.jmix.ui.executor.BackgroundTask) NoOpenOfficeFreePortsException(io.jmix.reports.exception.NoOpenOfficeFreePortsException) Notifications(io.jmix.ui.Notifications) FailedToConnectToOpenOfficeException(io.jmix.reports.exception.FailedToConnectToOpenOfficeException)

Aggregations

ReportOutputDocument (com.haulmont.yarg.reporting.ReportOutputDocument)2 FailedToConnectToOpenOfficeException (io.jmix.reports.exception.FailedToConnectToOpenOfficeException)2 NoOpenOfficeFreePortsException (io.jmix.reports.exception.NoOpenOfficeFreePortsException)2 ReportingException (io.jmix.reports.exception.ReportingException)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 ReportRunContext (io.jmix.reports.runner.ReportRunContext)1 ShowChartScreen (io.jmix.reportsui.screen.report.run.ShowChartScreen)1 ShowPivotTableScreen (io.jmix.reportsui.screen.report.run.ShowPivotTableScreen)1 ShowReportTableScreen (io.jmix.reportsui.screen.report.run.ShowReportTableScreen)1 Notifications (io.jmix.ui.Notifications)1 BackgroundTask (io.jmix.ui.executor.BackgroundTask)1 TaskLifeCycle (io.jmix.ui.executor.TaskLifeCycle)1