Skip to content

Commit

Permalink
REPORT-859: Add a JSON Renderer to TextTemplate feature (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
solemabrothers authored and ssmusoke committed Dec 11, 2019
1 parent 2057019 commit de48eac
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void setup() {
p.setBirthdate(DateUtil.getDateTime(2007, 5, 27));
patientService.savePatient(p);
}

@Test
public void shouldRenderVariableReplacementTemplate() throws Exception {
shouldRenderTemplate("VariableReplacementTemplate.txt", null);
Expand All @@ -75,15 +75,15 @@ private void shouldRenderTemplate(String templateName, String templateType) thro
allPatients.addPatientProperty("gender");
allPatients.addPatientProperty("birthdate");
reportDefinition.addDataSetDefinition("allPatients", allPatients, null);

GenderCohortDefinition males = new GenderCohortDefinition();
males.setName("Males");
males.setMaleIncluded(true);

GenderCohortDefinition females = new GenderCohortDefinition();
females.setName("Females");
females.setFemaleIncluded(true);

CohortCrossTabDataSetDefinition genderDsd = new CohortCrossTabDataSetDefinition();
genderDsd.addColumn("males", males, null);
genderDsd.addColumn("females", females, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.openmrs.module.reporting.report.definition.ReportDefinition;
import org.openmrs.module.reporting.report.definition.service.ReportDefinitionService;
import org.openmrs.module.reporting.report.renderer.CsvReportRenderer;
import org.openmrs.module.reporting.report.renderer.TextTemplateRenderer;
import org.openmrs.module.reporting.report.renderer.XlsReportRenderer;
import org.openmrs.module.reporting.report.service.ReportService;
import org.openmrs.module.reporting.report.util.ReportUtil;
Expand Down Expand Up @@ -159,6 +160,23 @@ public static ReportDesign createExcelTemplateDesign(String reportDesignUuid, Re
return design;
}

/**
* @return a new ReportDesign for a JSON template, using a file on the classpath as the template
*/
public static ReportDesign createJSONTemplateDesign(String reportDesignUuid, ReportDefinition reportDefinition, String resourcePath) {
ReportDesign design = createJSONReportDesign(reportDesignUuid,reportDefinition);
design.setReportDefinition(reportDefinition);
design.setRendererType(TextTemplateRenderer.class);
ReportDesignResource resource = new ReportDesignResource();
resource.setName("template");
resource.setExtension("json");
resource.setContentType("application/json");
resource.setContents(ReportUtil.readByteArrayFromResource(resourcePath));
resource.setReportDesign(design);
design.addResource(resource);
return design;
}

/**
* @return a new ReportDesign for a standard Excel output
*/
Expand All @@ -183,4 +201,16 @@ public static ReportDesign createCsvReportDesign(String reportDesignUuid, Report
design.setRendererType(CsvReportRenderer.class);
return design;
}

/**
* @return a new ReportDesign for a standard CSV output
*/
public static ReportDesign createJSONReportDesign(String reportDesignUuid, ReportDefinition reportDefinition) {
ReportDesign design = new ReportDesign();
design.setUuid(reportDesignUuid);
design.setName("JSON");
design.setReportDefinition(reportDefinition);
design.setRendererType(TextTemplateRenderer.class);
return design;
}
}

0 comments on commit de48eac

Please sign in to comment.