use of io.jmix.reports.runner.ReportRunContext in project jmix-docs by Haulmont.
the class RunReportScreen method onRrcBtn1Click.
@Subscribe("rrcBtn1")
protected void onRrcBtn1Click(Button.ClickEvent event) {
Report report = getReportByCode("BOOKS");
LiteratureType type = dataManager.load(LiteratureType.class).query("select c from jmxrpr_LiteratureType c where c.name = :name").parameter("name", "Art").one();
// tag::report-run-context-v1[]
ReportRunContext context = new ReportRunContext(report).addParam("type", type).setOutputNamePattern("Books");
// end::report-run-context-v1[]
ReportOutputDocument document = reportRunner.run(context);
List<ReportOutputDocument> documents = new ArrayList<>();
documents.add(document);
// tag::report-zip-utils[]
byte[] zipArchiveContent = reportZipUtils.createZipArchive(documents);
downloader.download(zipArchiveContent, "Reports.zip", DownloadFormat.ZIP);
// end::report-zip-utils[]
}
use of io.jmix.reports.runner.ReportRunContext in project jmix-docs by Haulmont.
the class RunReportScreen method onRrcBtn2Click.
// tag::rrc-btn2-start[]
@Subscribe("rrcBtn2")
protected void onRrcBtn2Click(Button.ClickEvent event) {
// end::rrc-btn2-start[]
Report report = getReportByCode("BOOKS");
LiteratureType type = dataManager.load(LiteratureType.class).query("select c from jmxrpr_LiteratureType c where c.name = :name").parameter("name", "Art").one();
Map<String, Object> paramsMap = new HashMap<>();
paramsMap.put("type", type);
ReportTemplate template = dataManager.load(ReportTemplate.class).query("select c from report_ReportTemplate c where c.code = :code and c.report = :report").parameter("code", "DEFAULT").parameter("report", report).one();
// tag::report-run-context-v2[]
ReportRunContext context = new ReportRunContext(report).setReportTemplate(template).setOutputType(ReportOutputType.PDF).setParams(paramsMap);
// end::report-run-context-v2[]
// tag::report-runner-v1[]
ReportOutputDocument document = reportRunner.run(// <1>
new ReportRunContext(report).setParams(paramsMap));
// end::report-runner-v1[]
// ReportOutputDocument document = reportRunner.run(context);
// tag::rrc-btn2-end[]
}
use of io.jmix.reports.runner.ReportRunContext in project jmix by jmix-framework.
the class EmailTemplatesImpl method createEmailAttachment.
protected EmailAttachment createEmailAttachment(String templateName, ReportWithParams reportWithParams) {
ReportOutputDocument outputDocument = reportRunner.run(new ReportRunContext(reportWithParams.getReport()).setParams(reportWithParams.getParams()));
String fileName = outputDocument.getDocumentName();
if (StringUtils.isNotBlank(templateName)) {
String extension = Files.getFileExtension(templateName);
String docExtension = Files.getFileExtension(fileName);
if (StringUtils.isNotBlank(extension)) {
fileName = templateName;
} else if (StringUtils.isNotBlank(docExtension)) {
fileName = templateName + "." + docExtension;
} else {
fileName = templateName;
}
}
return new EmailAttachment(outputDocument.getContent(), fileName);
}
use of io.jmix.reports.runner.ReportRunContext 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();
}
}
}
use of io.jmix.reports.runner.ReportRunContext 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);
}
}
}
Aggregations