Search in sources :

Example 1 with BackgroundTask

use of io.jmix.ui.executor.BackgroundTask in project jmix-docs by Haulmont.

the class ProgressBarScreen method onInit.

@Subscribe
protected void onInit(InitEvent event) {
    BackgroundTask<Integer, Void> task = new BackgroundTask<Integer, Void>(100) {

        @Override
        public Void run(TaskLifeCycle<Integer> taskLifeCycle) throws Exception {
            for (int i = 1; i <= ITERATIONS; i++) {
                // <1>
                TimeUnit.SECONDS.sleep(1);
                taskLifeCycle.publish(i);
            }
            return null;
        }

        @Override
        public void progress(List<Integer> changes) {
            double lastValue = changes.get(changes.size() - 1);
            // <2>
            progressBar.setValue(lastValue / ITERATIONS);
        }
    };
    BackgroundTaskHandler taskHandler = backgroundWorker.handle(task);
    taskHandler.execute();
}
Also used : TaskLifeCycle(io.jmix.ui.executor.TaskLifeCycle) List(java.util.List) BackgroundTaskHandler(io.jmix.ui.executor.BackgroundTaskHandler) BackgroundTask(io.jmix.ui.executor.BackgroundTask)

Example 2 with BackgroundTask

use of io.jmix.ui.executor.BackgroundTask in project jmix-docs by Haulmont.

the class DialogsScreen method onBackgroundDlgBtnClick.

// end::create-exception-dialog[]
@Subscribe("backgroundDlgBtn")
public void onBackgroundDlgBtnClick(Button.ClickEvent event) {
    BackgroundTask<Integer, Void> task = new BackgroundTask<Integer, Void>(300, this) {

        @Override
        public Void run(TaskLifeCycle<Integer> taskLifeCycle) throws Exception {
            for (int i = 1; i <= ITERATIONS; i++) {
                TimeUnit.SECONDS.sleep(2);
                taskLifeCycle.publish(i);
            }
            return null;
        }

        @Override
        public void progress(List<Integer> changes) {
            double lastValue = changes.get(changes.size() - 1);
        // progressBar.setValue((lastValue / ITERATIONS));
        }
    };
    BackgroundTaskHandler taskHandler = backgroundWorker.handle(task);
    taskHandler.execute();
    dialogs.createBackgroundWorkDialog(this, task).withCaption("Please wait").withMessage("Data loading continues").withShowProgressInPercentage(true).withCancelAllowed(true).show();
}
Also used : TaskLifeCycle(io.jmix.ui.executor.TaskLifeCycle) List(java.util.List) BackgroundTaskHandler(io.jmix.ui.executor.BackgroundTaskHandler) BackgroundTask(io.jmix.ui.executor.BackgroundTask) Subscribe(io.jmix.ui.screen.Subscribe)

Example 3 with BackgroundTask

use of io.jmix.ui.executor.BackgroundTask 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)

Example 4 with BackgroundTask

use of io.jmix.ui.executor.BackgroundTask in project jmix by jmix-framework.

the class UiReportRunnerImpl method runMultipleReports.

@Override
public void runMultipleReports(UiReportRunContext context, String multiParamAlias, Collection multiParamValue) {
    prepareContext(context);
    if (needToShowParamsDialog(context)) {
        context.addParam(multiParamAlias, multiParamValue);
        openReportParamsDialog(context, getInputParameter(context, multiParamAlias), true);
        return;
    }
    FrameOwner originFrameOwner = context.getOriginFrameOwner();
    if (originFrameOwner != null && context.getInBackground()) {
        Report targetReport = getReportForPrinting(context.getReport());
        long timeout = reportsClientProperties.getBackgroundReportProcessingTimeoutMs();
        Screen hostScreen = UiControllerUtils.getScreen(originFrameOwner);
        BackgroundTask<Integer, List<ReportOutputDocument>> task = new BackgroundTask<Integer, List<ReportOutputDocument>>(timeout, TimeUnit.MILLISECONDS, hostScreen) {

            @SuppressWarnings("UnnecessaryLocalVariable")
            @Override
            public List<ReportOutputDocument> run(TaskLifeCycle<Integer> taskLifeCycle) {
                context.setReport(targetReport);
                return multiRunSync(context, multiParamAlias, multiParamValue);
            }

            @Override
            public void done(List<ReportOutputDocument> result) {
                downloadZipArchive(result);
            }
        };
        showDialog(task);
    } else {
        List<ReportOutputDocument> outputDocuments = multiRunSync(context, multiParamAlias, multiParamValue);
        downloadZipArchive(outputDocuments);
    }
}
Also used : TaskLifeCycle(io.jmix.ui.executor.TaskLifeCycle) ReportOutputDocument(com.haulmont.yarg.reporting.ReportOutputDocument) ShowPivotTableScreen(io.jmix.reportsui.screen.report.run.ShowPivotTableScreen) ShowReportTableScreen(io.jmix.reportsui.screen.report.run.ShowReportTableScreen) ShowChartScreen(io.jmix.reportsui.screen.report.run.ShowChartScreen) BackgroundTask(io.jmix.ui.executor.BackgroundTask)

Aggregations

BackgroundTask (io.jmix.ui.executor.BackgroundTask)4 TaskLifeCycle (io.jmix.ui.executor.TaskLifeCycle)4 ReportOutputDocument (com.haulmont.yarg.reporting.ReportOutputDocument)2 ShowChartScreen (io.jmix.reportsui.screen.report.run.ShowChartScreen)2 ShowPivotTableScreen (io.jmix.reportsui.screen.report.run.ShowPivotTableScreen)2 ShowReportTableScreen (io.jmix.reportsui.screen.report.run.ShowReportTableScreen)2 BackgroundTaskHandler (io.jmix.ui.executor.BackgroundTaskHandler)2 List (java.util.List)2 FailedToConnectToOpenOfficeException (io.jmix.reports.exception.FailedToConnectToOpenOfficeException)1 NoOpenOfficeFreePortsException (io.jmix.reports.exception.NoOpenOfficeFreePortsException)1 ReportingException (io.jmix.reports.exception.ReportingException)1 Notifications (io.jmix.ui.Notifications)1 Subscribe (io.jmix.ui.screen.Subscribe)1