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);
}
}
}
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);
}
Aggregations