Skip to content

Commit

Permalink
KH-362: Fixed HC Report CSV format
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruhanga committed Nov 6, 2023
1 parent 63e8eac commit 1879e3a
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* The contents of this file are subject to the OpenMRS Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://license.openmrs.org
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
*/
package org.openmrs.module.commonreports.renderer;

import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.openmrs.Cohort;
import org.openmrs.annotation.Handler;
import org.openmrs.module.reporting.common.Localized;
import org.openmrs.module.reporting.common.ObjectUtil;
import org.openmrs.module.reporting.dataset.DataSet;
import org.openmrs.module.reporting.dataset.DataSetColumn;
import org.openmrs.module.reporting.dataset.DataSetRow;
import org.openmrs.module.reporting.dataset.SimpleDataSet;
import org.openmrs.module.reporting.dataset.SimpleDataSetMetaData;
import org.openmrs.module.reporting.dataset.definition.CohortCrossTabDataSetDefinition;
import org.openmrs.module.reporting.evaluation.EvaluationContext;
import org.openmrs.module.reporting.report.ReportData;
import org.openmrs.module.reporting.report.renderer.CsvReportRenderer;
import org.openmrs.module.reporting.report.renderer.RenderingException;

/**
* ReportRenderer that renders to a csv form for CohortCrossTabDataSet reports
*/
@Handler
@Localized("reporting.CohortCrossTabDataSetCsvReportRenderer")
public class CohortCrossTabDataSetCsvReportRenderer extends CsvReportRenderer {

@Override
public void render(ReportData results, String argument, OutputStream out) throws IOException, RenderingException {
results.getDefinition();
EvaluationContext context = ObjectUtil.nvl(results.getContext(), new EvaluationContext());
ReportData resultsToRender = new ReportData();
resultsToRender.setContext(context);
resultsToRender.setDefinition(results.getDefinition());

Map<String, DataSet> dataSetsToRender = new HashMap<String, DataSet>();

results.getDataSets().forEach((name, ds) -> {
if (ds.getDefinition() instanceof CohortCrossTabDataSetDefinition) {
CohortCrossTabDataSetDefinition dsd = (CohortCrossTabDataSetDefinition) ds.getDefinition();
DataSetRow cohortCrossTabDataSetRow = ds.iterator().next();

SimpleDataSet data = new SimpleDataSet(dsd, context);

SimpleDataSetMetaData metaData = new SimpleDataSetMetaData();

// Add first empty column since it's a place holder for the row indicators.
metaData.addColumn(new DataSetColumn("", "", Object.class));

Set<String> columns = dsd.getColumns().keySet();
Set<String> rows = dsd.getRows().keySet();

// Add rest of the columns
for (String column : columns) {
metaData.addColumn(new DataSetColumn(column, column, Object.class));
}
data.setMetaData(metaData);

// Add column values
for (String row : rows) {
DataSetRow dsr = new DataSetRow();
for (DataSetColumn column : metaData.getColumns()) {
if (column.getName().equals("")) {
dsr.addColumnValue(column, row);
} else {
dsr.addColumnValue(column, ((Cohort) cohortCrossTabDataSetRow.getColumnValue(row + "." + column))
.size());
}
}
data.addRow(dsr);
}
dataSetsToRender.put(name, data);
} else {
dataSetsToRender.put(name, ds);
}
});
resultsToRender.setDataSets(dataSetsToRender);

super.render(resultsToRender, argument, out);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.openmrs.Location;
import org.openmrs.module.commonreports.ActivatedReportManager;
import org.openmrs.module.commonreports.CommonReportsConstants;
import org.openmrs.module.commonreports.renderer.CohortCrossTabDataSetCsvReportRenderer;
import org.openmrs.module.commonreports.renderer.PatientHistoryXmlReportRenderer;
import org.openmrs.module.initializer.api.InitializerService;
import org.openmrs.module.reporting.cohort.definition.AgeCohortDefinition;
import org.openmrs.module.reporting.cohort.definition.CodedObsCohortDefinition;
Expand Down Expand Up @@ -325,7 +327,11 @@ private CompositionCohortDefinition createCohortComposition(Object... elements)

@Override
public List<ReportDesign> constructReportDesigns(ReportDefinition reportDefinition) {
return Arrays
.asList(ReportManagerUtil.createCsvReportDesign("42b32ac1-fcd0-473d-8fdb-71fd6fc2e26d", reportDefinition));
ReportDesign reportDesign = new ReportDesign();
reportDesign.setName("CSV");
reportDesign.setUuid("42b32ac1-fcd0-473d-8fdb-71fd6fc2e26d");
reportDesign.setReportDefinition(reportDefinition);
reportDesign.setRendererType(CohortCrossTabDataSetCsvReportRenderer.class);
return Arrays.asList(reportDesign);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.Iterator;
import java.util.List;
Expand All @@ -18,6 +19,7 @@
import org.openmrs.api.ConceptService;
import org.openmrs.module.commonreports.ActivatedReportManager;
import org.openmrs.module.commonreports.CommonReportsConstants;
import org.openmrs.module.commonreports.renderer.CohortCrossTabDataSetCsvReportRenderer;
import org.openmrs.module.commonreports.reports.OutpatientConsultationReportManager;
import org.openmrs.module.initializer.Domain;
import org.openmrs.module.initializer.api.InitializerService;
Expand Down Expand Up @@ -78,6 +80,8 @@ public void setupReport_shouldSetupOPDRecBook() {
// verif
Assert.assertNotNull(rs.getReportDesignByUuid("42b32ac1-fcd0-473d-8fdb-71fd6fc2e26d"));

assertEquals(rs.getReportDesignByUuid("42b32ac1-fcd0-473d-8fdb-71fd6fc2e26d").getRendererType(), CohortCrossTabDataSetCsvReportRenderer.class);

}

@Test
Expand Down Expand Up @@ -153,4 +157,30 @@ public void testReport() throws Exception {
}
}

@Test
public void testReportRenderer() throws Exception {

// Setup
EvaluationContext context = new EvaluationContext();
context.addParameterValue("startDate", DateUtil.parseDate("2008-08-01", "yyyy-MM-dd"));
context.addParameterValue("endDate", DateUtil.parseDate("2009-09-30", "yyyy-MM-dd"));

ReportDefinition rd = manager.constructReportDefinition();
ReportData data = rds.evaluate(rd, context);

CohortCrossTabDataSetCsvReportRenderer renderer = new CohortCrossTabDataSetCsvReportRenderer();
ByteArrayOutputStream out = new ByteArrayOutputStream();

// Replay
renderer.render(data, "", out);

// Verify
String expectedFormat = "\"\",\"0-28 days - Males\",\"0-28 days - Females\",\"1-12 months - Males\",\"1-12 months - Females\",\"1-4 years - Males\",\"1-4 years - Females\",\"5-14 years - Males\",\"5-14 years - Females\",\"15-24 years - Males\",\"15-24 years - Females\",\"25-49 years - Males\",\"25-49 years - Females\",\"50-64 years - Males\",\"50-64 years - Females\",\"_> 65 years - Males\",\"_> 65 years - Females\",\"Total - Males\",\"Total - Females\",\"Total\",\"Referred To - Males\",\"Referred To - Females\"\r\n" +
"\"MALARIA\",\"0\",\"0\",\"0\",\"0\",\"1\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"1\",\"0\",\"0\",\"0\",\"0\",\"1\",\"1\",\"2\",\"0\",\"1\"\r\n" +
"\"FEVER\",\"0\",\"0\",\"0\",\"0\",\"1\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"1\",\"0\",\"1\",\"0\",\"0\"\r\n" +
"\"DIABETES\",\"0\",\"0\",\"0\",\"0\",\"1\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"1\",\"0\",\"1\",\"0\",\"0\"\r\n";

assertThat(out.toString(), is(expectedFormat));
}

}

0 comments on commit 1879e3a

Please sign in to comment.