Skip to content

Commit 90e0e7c

Browse files
committed
REPORT-916 - Support loading custom renderers via config (#265)
1 parent 3fc0d03 commit 90e0e7c

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

api/src/main/java/org/openmrs/module/reporting/common/ContentType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public enum ContentType {
1919
XML("xml", "text/xml"),
2020
CSV("csv", "text/csv"),
2121
TEXT("text", "text/plain"),
22-
ZIP("zip", "application/zip");
22+
ZIP("zip", "application/zip"),
23+
PDF("pdf", "application/pdf");
2324

2425
//***** PROPERTIES *****
2526

api/src/main/java/org/openmrs/module/reporting/config/DesignDescriptor.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
public class DesignDescriptor {
99

10+
@JsonProperty
11+
private String name;
12+
1013
@JsonProperty
1114
private String type;
1215

@@ -19,6 +22,14 @@ public class DesignDescriptor {
1922
@JsonProperty
2023
private List<ProcessorDescriptor> processors;
2124

25+
public String getName() {
26+
return name;
27+
}
28+
29+
public void setName(String name) {
30+
this.name = name;
31+
}
32+
2233
public String getType() {
2334
return type;
2435
}

api/src/main/java/org/openmrs/module/reporting/config/ReportLoader.java

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.openmrs.api.db.SerializedObject;
1717
import org.openmrs.api.db.SerializedObjectDAO;
1818
import org.openmrs.module.reporting.common.ContentType;
19+
import org.openmrs.module.reporting.common.ObjectUtil;
1920
import org.openmrs.module.reporting.config.factory.DataSetFactory;
2021
import org.openmrs.module.reporting.dataset.definition.DataSetDefinition;
2122
import org.openmrs.module.reporting.evaluation.parameter.Mapped;
@@ -30,6 +31,7 @@
3031
import org.openmrs.module.reporting.report.processor.LoggingReportProcessor;
3132
import org.openmrs.module.reporting.report.renderer.CsvReportRenderer;
3233
import org.openmrs.module.reporting.report.renderer.ReportDesignRenderer;
34+
import org.openmrs.module.reporting.report.renderer.ReportRenderer;
3335
import org.openmrs.module.reporting.report.renderer.XlsReportRenderer;
3436
import org.openmrs.module.reporting.report.service.ReportService;
3537
import org.openmrs.util.OpenmrsUtil;
@@ -250,15 +252,41 @@ public static List<ReportDesign> constructReportDesigns(ReportDefinition reportD
250252
}
251253

252254
for (DesignDescriptor designDescriptor : reportDescriptor.getDesigns()) {
253-
ReportDesign design = null;
255+
ReportDesign design;
254256
if (designDescriptor.getType().equalsIgnoreCase("csv")) {
255257
design = constructCSVReportDesign(reportDefinition);
256258
}
257259
else if (designDescriptor.getType().equalsIgnoreCase("excel") || designDescriptor.getType().equalsIgnoreCase("xls")) {
258260
design = constructXlsReportDesign(reportDefinition, reportDescriptor, designDescriptor);
259261
}
260262
else {
261-
throw new RuntimeException("Unsupported report design type: " + designDescriptor.getType() + " for report " + reportDefinition.getName());
263+
try {
264+
design = new ReportDesign();
265+
design.setName(designDescriptor.getName());
266+
design.setReportDefinition(reportDefinition);
267+
design.setRendererType((Class<? extends ReportRenderer>)Context.loadClass(designDescriptor.getType()));
268+
269+
if (StringUtils.isNotBlank(designDescriptor.getTemplate())) {
270+
String template = designDescriptor.getTemplate();
271+
ReportDesignResource resource = new ReportDesignResource();
272+
resource.setName("template");
273+
for (ContentType contentType : ContentType.values()) {
274+
if (template.toLowerCase().endsWith("." + contentType.getExtension())) {
275+
resource.setExtension(contentType.getExtension());
276+
resource.setContentType(contentType.getContentType());
277+
}
278+
}
279+
File templateFile = new File(reportDescriptor.getPath(), template);
280+
281+
byte[] templateBytes = FileUtils.readFileToByteArray(templateFile);
282+
resource.setContents(templateBytes);
283+
resource.setReportDesign(design);
284+
design.addResource(resource);
285+
}
286+
}
287+
catch (Exception e) {
288+
throw new RuntimeException("Failed to load report design: " + designDescriptor.getType() + " for report " + reportDefinition.getName(), e);
289+
}
262290
}
263291

264292
if (designDescriptor.getProperties() != null) {
@@ -325,7 +353,7 @@ public static ReportDesign constructCSVReportDesign(ReportDefinition reportDefin
325353
public static ReportDesign constructXlsReportDesign(ReportDefinition reportDefinition, ReportDescriptor reportDescriptor, DesignDescriptor designDescriptor) {
326354

327355
ReportDesign design = new ReportDesign();
328-
design.setName("reporting.excel");
356+
design.setName(ObjectUtil.nvlStr(designDescriptor.getName(), "reporting.excel"));
329357
design.setReportDefinition(reportDefinition);
330358
design.setRendererType(XlsReportRenderer.class);
331359

0 commit comments

Comments
 (0)