final File configDir, final File taskDirectory)
throws JRException, SQLException, ExecutionException, JSONException {
// CSON: RedundantThrows
final String templateName = requestData.getString(Constants.JSON_LAYOUT_KEY);
final Template template = config.getTemplate(templateName);
if (template == null) {
final String possibleTemplates = config.getTemplates().keySet().toString();
throw new IllegalArgumentException("\nThere is no template with the name: " + templateName +
".\nAvailable templates: " + possibleTemplates);
}
final File jasperTemplateFile = new File(configDir, template.getReportTemplate());
final File jasperTemplateBuild = this.workingDirectories.getBuildFileFor(config, jasperTemplateFile,
JasperReportBuilder.JASPER_REPORT_COMPILED_FILE_EXT, LOGGER);
final Values values = new Values(requestData, template, this.parser, taskDirectory, this.httpRequestFactory,
jasperTemplateBuild.getParentFile());
double[] maxDpi = maxDpi(values);
final ForkJoinTask<Values> taskFuture = this.forkJoinPool.submit(template.getProcessorGraph().createTask(values));
try {
taskFuture.get();
} catch (InterruptedException exc) {
// if cancel() is called on the current thread, this exception will be thrown.
// in this case, also properly cancel the task future.
taskFuture.cancel(true);
Thread.currentThread().interrupt();
throw new CancellationException();
}
JasperFillManager fillManager = getJasperFillManager(config);
checkRequiredValues(config, values, template.getReportTemplate());
final JasperPrint print;
if (template.getJdbcUrl() != null) {
Connection connection;
if (template.getJdbcUser() != null) {
connection = DriverManager.getConnection(template.getJdbcUrl(), template.getJdbcUser(), template.getJdbcPassword());
} else {
connection = DriverManager.getConnection(template.getJdbcUrl());
}
print = fillManager.fill(
jasperTemplateBuild.getAbsolutePath(),
values.asMap(),
connection);
} else {
JRDataSource dataSource;
if (template.getTableDataKey() != null) {
final Object dataSourceObj = values.getObject(template.getTableDataKey(), Object.class);
if (dataSourceObj instanceof JRDataSource) {
dataSource = (JRDataSource) dataSourceObj;
} else if (dataSourceObj instanceof Iterable) {
Iterable sourceObj = (Iterable) dataSourceObj;
dataSource = toJRDataSource(sourceObj.iterator());
} else if (dataSourceObj instanceof Iterator) {
Iterator sourceObj = (Iterator) dataSourceObj;
dataSource = toJRDataSource(sourceObj);
} else if (dataSourceObj.getClass().isArray()) {
Object[] sourceObj = (Object[]) dataSourceObj;
dataSource = toJRDataSource(Arrays.asList(sourceObj).iterator());
} else {
throw new AssertionError("Objects of type: " + dataSourceObj.getClass() + " cannot be converted to a row in a " +
"JRDataSource");
}
} else {
dataSource = new JREmptyDataSource();
}
checkRequiredFields(config, dataSource, template.getReportTemplate());
print = fillManager.fill(
jasperTemplateBuild.getAbsolutePath(),
values.asMap(),
dataSource);
}