|
16 | 16 | import org.openmrs.api.db.SerializedObject; |
17 | 17 | import org.openmrs.api.db.SerializedObjectDAO; |
18 | 18 | import org.openmrs.module.reporting.common.ContentType; |
| 19 | +import org.openmrs.module.reporting.common.ObjectUtil; |
19 | 20 | import org.openmrs.module.reporting.config.factory.DataSetFactory; |
20 | 21 | import org.openmrs.module.reporting.dataset.definition.DataSetDefinition; |
21 | 22 | import org.openmrs.module.reporting.evaluation.parameter.Mapped; |
|
30 | 31 | import org.openmrs.module.reporting.report.processor.LoggingReportProcessor; |
31 | 32 | import org.openmrs.module.reporting.report.renderer.CsvReportRenderer; |
32 | 33 | import org.openmrs.module.reporting.report.renderer.ReportDesignRenderer; |
| 34 | +import org.openmrs.module.reporting.report.renderer.ReportRenderer; |
33 | 35 | import org.openmrs.module.reporting.report.renderer.XlsReportRenderer; |
34 | 36 | import org.openmrs.module.reporting.report.service.ReportService; |
35 | 37 | import org.openmrs.util.OpenmrsUtil; |
@@ -250,15 +252,41 @@ public static List<ReportDesign> constructReportDesigns(ReportDefinition reportD |
250 | 252 | } |
251 | 253 |
|
252 | 254 | for (DesignDescriptor designDescriptor : reportDescriptor.getDesigns()) { |
253 | | - ReportDesign design = null; |
| 255 | + ReportDesign design; |
254 | 256 | if (designDescriptor.getType().equalsIgnoreCase("csv")) { |
255 | 257 | design = constructCSVReportDesign(reportDefinition); |
256 | 258 | } |
257 | 259 | else if (designDescriptor.getType().equalsIgnoreCase("excel") || designDescriptor.getType().equalsIgnoreCase("xls")) { |
258 | 260 | design = constructXlsReportDesign(reportDefinition, reportDescriptor, designDescriptor); |
259 | 261 | } |
260 | 262 | 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 | + } |
262 | 290 | } |
263 | 291 |
|
264 | 292 | if (designDescriptor.getProperties() != null) { |
@@ -325,7 +353,7 @@ public static ReportDesign constructCSVReportDesign(ReportDefinition reportDefin |
325 | 353 | public static ReportDesign constructXlsReportDesign(ReportDefinition reportDefinition, ReportDescriptor reportDescriptor, DesignDescriptor designDescriptor) { |
326 | 354 |
|
327 | 355 | ReportDesign design = new ReportDesign(); |
328 | | - design.setName("reporting.excel"); |
| 356 | + design.setName(ObjectUtil.nvlStr(designDescriptor.getName(), "reporting.excel")); |
329 | 357 | design.setReportDefinition(reportDefinition); |
330 | 358 | design.setRendererType(XlsReportRenderer.class); |
331 | 359 |
|
|
0 commit comments