From 84a9aa63fff03b90d3a45be2f12a77ba6ad25d42 Mon Sep 17 00:00:00 2001 From: Siva R Date: Thu, 19 Mar 2020 16:24:12 +0530 Subject: [PATCH] =?UTF-8?q?BAH-976=20|=20Fix=20obs=20to=20obs=20flow=20she?= =?UTF-8?q?et=20bug=20for=20form2=20ad=E2=80=A6=20(#65)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Siva, Sowmika | MOBN-887 | Fix obs to obs flow sheet bug for form2 add-more * Siva | BAH-976 | Review suggestions: add test case * Siva | BAH-976 | Review suggestions: Rename test case --- .../bahmnicore/forms2/util/FormUtil.java | 4 +- .../bahmnicore/forms2/util/FormUtilTest.java | 15 ++- ...hmniFormBuilderObsToTabularViewMapper.java | 49 +++++-- ...FormBuilderObsToTabularViewMapperTest.java | 124 ++++++++++++++++++ 4 files changed, 173 insertions(+), 19 deletions(-) diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/forms2/util/FormUtil.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/forms2/util/FormUtil.java index b5aedbb072..a88220057e 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/forms2/util/FormUtil.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/forms2/util/FormUtil.java @@ -28,9 +28,9 @@ public static List filterFormBuilderObs(List observations) { .collect(Collectors.toList()) : Collections.emptyList(); } - public static String getFormNameAlongWithVersion(String formFieldPath) { + public static String getParentFormFieldPath(String formFieldPath) { return isNotBlank(formFieldPath) && formFieldPath.contains("/") - ? formFieldPath.substring(0, formFieldPath.indexOf("/")) : ""; + ? formFieldPath.substring(0, formFieldPath.lastIndexOf("/")) : ""; } } diff --git a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/forms2/util/FormUtilTest.java b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/forms2/util/FormUtilTest.java index cd70813dd1..f45ebcaeff 100644 --- a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/forms2/util/FormUtilTest.java +++ b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/forms2/util/FormUtilTest.java @@ -100,23 +100,30 @@ public void shouldReturnObsWhichHaveFormFieldPath() { } @Test - public void shouldReturnFormNameAlongWithVersionForGivenFormFieldPath() { + public void shouldReturnParentFormFieldPathForGivenFormFieldPath() { String expectedFormNameWithVersion = "FormName.1"; - String actualFormNameWithVersion = FormUtil.getFormNameAlongWithVersion("FormName.1/1-0"); + String actualFormNameWithVersion = FormUtil.getParentFormFieldPath("FormName.1/1-0"); + assertEquals(expectedFormNameWithVersion, actualFormNameWithVersion); + } + + @Test + public void shouldReturnParentFormFieldPathForGivenThreeLevelFormFieldPath() { + String expectedFormNameWithVersion = "FormName.1/1-0/2-0"; + String actualFormNameWithVersion = FormUtil.getParentFormFieldPath("FormName.1/1-0/2-0/3-0"); assertEquals(expectedFormNameWithVersion, actualFormNameWithVersion); } @Test public void shouldReturnFormNameAlongWithVersionIfGivenFormFieldPathDoesNotHaveSlash() { String expectedFormNameWithVersion = ""; - String actualFormNameWithVersion = FormUtil.getFormNameAlongWithVersion("FormName.1"); + String actualFormNameWithVersion = FormUtil.getParentFormFieldPath("FormName.1"); assertEquals(expectedFormNameWithVersion, actualFormNameWithVersion); } @Test public void shouldReturnEmptyStringWhenFormFieldPathIsNull() { String expectedFormNameWithVersion = ""; - String actualFormNameWithVersion = FormUtil.getFormNameAlongWithVersion(null); + String actualFormNameWithVersion = FormUtil.getParentFormFieldPath(null); assertEquals(expectedFormNameWithVersion, actualFormNameWithVersion); } } diff --git a/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/mapper/BahmniFormBuilderObsToTabularViewMapper.java b/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/mapper/BahmniFormBuilderObsToTabularViewMapper.java index 1fb7b10395..404fa099d5 100644 --- a/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/mapper/BahmniFormBuilderObsToTabularViewMapper.java +++ b/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/mapper/BahmniFormBuilderObsToTabularViewMapper.java @@ -14,7 +14,7 @@ import java.util.Set; import java.util.stream.Collectors; -import static org.bahmni.module.bahmnicore.forms2.util.FormUtil.getFormNameAlongWithVersion; +import static org.bahmni.module.bahmnicore.forms2.util.FormUtil.getParentFormFieldPath; @Component public class BahmniFormBuilderObsToTabularViewMapper extends BahmniObservationsToTabularViewMapper { @@ -31,13 +31,13 @@ public PivotTable constructTable(Set concepts, return pivotTable; } - public List getNonEmptyRows(List rows, String groupByConceptName){ + public List getNonEmptyRows(List rows, String groupByConceptName) { return rows.stream().filter(row -> isNonNullRow(groupByConceptName, row)).collect(Collectors.toList()); } private List constructRows(Set concepts, Collection bahmniObservations, String groupByConceptName) { - Map> rowsMapper = getRowsMapper(bahmniObservations); + Map> rowsMapper = getRowsMapper(bahmniObservations, groupByConceptName); List rows = new ArrayList<>(); rowsMapper.forEach((rowIdentifier, rowObservations) -> { PivotRow row = new PivotRow(); @@ -49,19 +49,42 @@ private List constructRows(Set concepts, return rows; } - private Map> getRowsMapper(Collection bahmniObservations) { + private Map> getRowsMapper(Collection bahmniObservations, + String groupByConceptName) { + final Map> obsRows = prepareMapWithRowIdentifier(bahmniObservations, + groupByConceptName); + for (BahmniObservation observation : bahmniObservations) { + final String currentObsRowIdentifier = getRowIdentifier(observation); + for (String rowIdentifier : obsRows.keySet()) { + if (currentObsRowIdentifier.startsWith(rowIdentifier)) { + obsRows.get(rowIdentifier).add(observation); + break; + } + } + } + return obsRows; + } + + // Observation rows are distinguished by encounter uuid and obs parent formFieldPath + private String getRowIdentifier(BahmniObservation bahmniObservation) { + return bahmniObservation.getEncounterUuid() + getParentFormFieldPath(bahmniObservation.getFormFieldPath()); + } + + + private Map> prepareMapWithRowIdentifier(Collection bahmniObservations, + String groupByConceptName) { + List groupByConceptObservations = getGroupByConceptObservations(bahmniObservations, + groupByConceptName); Map> rowsMapper = new LinkedHashMap<>(); - bahmniObservations.forEach(bahmniObservation -> { - String rowIdentifier = getRowIdentifier(bahmniObservation); - List bahmniObs; - bahmniObs = rowsMapper.containsKey(rowIdentifier) ? rowsMapper.get(rowIdentifier) : new ArrayList<>(); - bahmniObs.add(bahmniObservation); - rowsMapper.put(rowIdentifier, bahmniObs); - }); + groupByConceptObservations.forEach(observation + -> rowsMapper.put(getRowIdentifier(observation), new ArrayList<>())); return rowsMapper; } - private String getRowIdentifier(BahmniObservation bahmniObservation) { - return bahmniObservation.getEncounterUuid() + getFormNameAlongWithVersion(bahmniObservation.getFormFieldPath()); + private List getGroupByConceptObservations(Collection bahmniObservations, + String groupByConceptName) { + return bahmniObservations.stream() + .filter(observation -> observation.getConcept().getName().equals(groupByConceptName)) + .collect(Collectors.toList()); } } diff --git a/bahmnicore-omod/src/test/java/org/bahmni/module/bahmnicore/web/v1_0/mapper/BahmniFormBuilderObsToTabularViewMapperTest.java b/bahmnicore-omod/src/test/java/org/bahmni/module/bahmnicore/web/v1_0/mapper/BahmniFormBuilderObsToTabularViewMapperTest.java index c7d0a67683..8779b22dca 100644 --- a/bahmnicore-omod/src/test/java/org/bahmni/module/bahmnicore/web/v1_0/mapper/BahmniFormBuilderObsToTabularViewMapperTest.java +++ b/bahmnicore-omod/src/test/java/org/bahmni/module/bahmnicore/web/v1_0/mapper/BahmniFormBuilderObsToTabularViewMapperTest.java @@ -250,4 +250,128 @@ public void shouldReturnPivotTableForMultiSelectObs() { assertThat(firstRowColumns.get(multiSelectConceptName), containsInAnyOrder(multiSelectFirstObs, multiSelectSecondObs)); } + + @Test + public void shouldReturnPivotTableWithTwoRowsDifferentiatedByEncounterUUIDAndParentFormFieldPathsWhenAddMoreSectionHasAllConceptsIncludingGroupByConcept() { + + String groupByConceptName = "id"; + String weightConceptName = "weight"; + + Concept groupByConcept = new Concept(); + Concept weightConcept = new Concept(); + groupByConcept.setUuid("group-concept-uuid"); + groupByConcept.setName(groupByConceptName); + weightConcept.setUuid("weight-concept-uuid"); + weightConcept.setName(weightConceptName); + + BahmniObservation idObservation = new BahmniObservation(); + BahmniObservation weightObservation = new BahmniObservation(); + idObservation.setConcept(groupByConcept); + weightObservation.setConcept(weightConcept); + weightObservation.setValue("obs value"); + idObservation.setValue("1"); + String encounterUuid = "encounter-uuid"; + idObservation.setEncounterUuid(encounterUuid); + idObservation.setFormFieldPath("MedicalForm.10/1-0/2-0"); + weightObservation.setEncounterUuid(encounterUuid); + weightObservation.setFormFieldPath("MedicalForm.10/1-0/3-0"); + + BahmniObservation anotherIdObservation = new BahmniObservation(); + BahmniObservation anotherWeightObservation = new BahmniObservation(); + anotherIdObservation.setConcept(groupByConcept); + anotherWeightObservation.setConcept(weightConcept); + anotherWeightObservation.setValue("another obs value"); + anotherIdObservation.setValue(1); + anotherIdObservation.setEncounterUuid(encounterUuid); + anotherIdObservation.setFormFieldPath("MedicalForm.10/1-1/2-0"); + anotherWeightObservation.setUuid(encounterUuid); + anotherWeightObservation.setFormFieldPath("MedicalForm.10/1-1/3-0"); + anotherIdObservation.setEncounterUuid(encounterUuid); + anotherWeightObservation.setEncounterUuid(encounterUuid); + + HashSet concepts = new HashSet<>(asList(groupByConcept, weightConcept)); + List bahmniObservations = asList(idObservation, weightObservation, anotherIdObservation, + anotherWeightObservation); + + PivotTable pivotTable = bahmniFormBuilderObsToTabularViewMapper.constructTable(concepts, bahmniObservations, + groupByConceptName); + + assertEquals(2, pivotTable.getHeaders().size()); + final List rows = pivotTable.getRows(); + assertEquals(2, rows.size()); + assertEquals(2, rows.get(0).getColumns().size()); + assertEquals(2, rows.get(1).getColumns().size()); + + final Map> firstColumn = rows.get(0).getColumns(); + final Map> secondColumn = rows.get(1).getColumns(); + + final List actualFirstRow = asList(firstColumn.get(groupByConceptName).get(0), + firstColumn.get(weightConceptName).get(0)); + + final List actualSecondRow = asList(secondColumn.get(groupByConceptName).get(0), + secondColumn.get(weightConceptName).get(0)); + + List expectedFirstRow = asList(idObservation, weightObservation); + List expectedSecondRow = asList(anotherIdObservation, anotherWeightObservation); + + + assertTrue(expectedFirstRow.containsAll(actualFirstRow) + || expectedFirstRow.containsAll(actualSecondRow)); + assertTrue(expectedSecondRow.containsAll(actualFirstRow) + || expectedSecondRow.containsAll(actualSecondRow)); + } + + @Test + public void shouldReturnPivotTableWithOneRowWhenAddMoreSectionHasAllConceptsExceptGroupByConcept() { + String groupByConceptName = "id"; + String weightConceptName = "weight"; + + Concept groupByConcept = new Concept(); + Concept weightConcept = new Concept(); + groupByConcept.setUuid("group-concept-uuid"); + groupByConcept.setName(groupByConceptName); + weightConcept.setUuid("weight-concept-uuid"); + weightConcept.setName(weightConceptName); + + BahmniObservation idObservation = new BahmniObservation(); + BahmniObservation weightObservation = new BahmniObservation(); + idObservation.setConcept(groupByConcept); + weightObservation.setConcept(weightConcept); + weightObservation.setValue("obs value"); + idObservation.setValue("1"); + String encounterUuid = "encounter-uuid"; + idObservation.setEncounterUuid(encounterUuid); + idObservation.setFormFieldPath("MedicalForm.10/1-0"); + weightObservation.setEncounterUuid(encounterUuid); + weightObservation.setFormFieldPath("MedicalForm.10/2-0/3-0"); + + BahmniObservation anotherWeightObservation = new BahmniObservation(); + anotherWeightObservation.setConcept(weightConcept); + anotherWeightObservation.setValue("another obs value"); + anotherWeightObservation.setUuid(encounterUuid); + anotherWeightObservation.setFormFieldPath("MedicalForm.10/2-1/3-0"); + anotherWeightObservation.setEncounterUuid(encounterUuid); + + HashSet concepts = new HashSet<>(asList(groupByConcept, weightConcept)); + List bahmniObservations = asList(idObservation, weightObservation, + anotherWeightObservation); + + PivotTable pivotTable = bahmniFormBuilderObsToTabularViewMapper.constructTable(concepts, bahmniObservations, + groupByConceptName); + + assertEquals(2, pivotTable.getHeaders().size()); + final List rows = pivotTable.getRows(); + assertEquals(1, rows.size()); + assertEquals(2, rows.get(0).getColumns().size()); + + final Map> columns = rows.get(0).getColumns(); + + final List actualRow = asList(columns.get(groupByConceptName).get(0), + columns.get(weightConceptName).get(0), columns.get(weightConceptName).get(1)); + + List expectedRow = asList(idObservation, weightObservation, anotherWeightObservation); + + + assertTrue(expectedRow.containsAll(actualRow)); + } }