From 336cbf94f589552f6af95d69b6646bd1161175c5 Mon Sep 17 00:00:00 2001 From: lincmba Date: Tue, 21 May 2024 10:26:33 +0300 Subject: [PATCH] Handle inconsistent LocationHierarchy responses. Extract the location id in the correct format from the returned value when getId() is called fixes https://github.com/opensrp/fhir-common-utils/issues/17 --- pom.xml | 2 +- .../model/location/LocationHierarchyTree.java | 17 ++++++++++++++- .../location/LocationHierarchyTreeTest.java | 21 +++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index fdf20a7..7955069 100755 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.smartregister fhir-common-utils - 1.0.0-SNAPSHOT + 1.0.1-SNAPSHOT diff --git a/src/main/java/org/smartregister/model/location/LocationHierarchyTree.java b/src/main/java/org/smartregister/model/location/LocationHierarchyTree.java index c9d0b4c..e040fc5 100755 --- a/src/main/java/org/smartregister/model/location/LocationHierarchyTree.java +++ b/src/main/java/org/smartregister/model/location/LocationHierarchyTree.java @@ -23,8 +23,10 @@ import org.hl7.fhir.r4.model.Location; import org.hl7.fhir.r4.model.StringType; import org.hl7.fhir.r4.model.Type; +import org.smartregister.utils.Constants; import java.util.List; +import java.util.StringTokenizer; @DatatypeDef(name = "LocationHierarchyTree") public class LocationHierarchyTree extends Type implements ICompositeType { @@ -37,7 +39,8 @@ public LocationHierarchyTree() { public void addLocation(Location location) { StringType idString = new StringType(); - idString.setValue(location.getId()); + String locationId = extractLocationId(location); + idString.setValue(locationId); if (location.getPartOf() == null || StringUtils.isEmpty(location.getPartOf().getReference())) { locationsHierarchy.addNode(idString.getValue(), location.getName(), location, null); } else { @@ -49,6 +52,18 @@ public void addLocation(Location location) { } } + private static String extractLocationId(Location location) { + String locationId = location.getId(); + StringTokenizer tokenizer = new StringTokenizer(locationId, Constants.FORWARD_SLASH); + while (tokenizer.hasMoreTokens()) { + String token = tokenizer.nextToken(); + if (Constants.LOCATION.equals(token) && tokenizer.hasMoreTokens()) { + return token + Constants.FORWARD_SLASH + tokenizer.nextToken(); + } + } + return locationId; + } + /** * WARNING: Overrides existing locations * diff --git a/src/test/java/org/smartregister/model/location/LocationHierarchyTreeTest.java b/src/test/java/org/smartregister/model/location/LocationHierarchyTreeTest.java index 960edf4..63837e8 100644 --- a/src/test/java/org/smartregister/model/location/LocationHierarchyTreeTest.java +++ b/src/test/java/org/smartregister/model/location/LocationHierarchyTreeTest.java @@ -223,6 +223,27 @@ public void testBuildTreeFromList() { .getValue()); } + @Test + public void testAddLocationWithMalformedIdUsesCorrectId() { + Location location = new Location(); + location.setId("http://test-server/fhir/Location/1/_history/3"); + location.setName("Test Location"); + Reference partOfReference = new Reference(); + partOfReference.setReference(""); + location.setPartOf(partOfReference); + LocationHierarchyTree locationHierarchyTree = new LocationHierarchyTree(); + locationHierarchyTree.addLocation(location); + + Tree tree = locationHierarchyTree.getLocationsHierarchy(); + assertNotNull(tree); + assertNotNull(tree.getTree()); + assertEquals("Location/1", tree.getTree().getTreeNodeId().getValue()); + assertEquals("Location/1", tree.getTree().getTreeNode().getNodeId().getValue()); + assertEquals("Test Location", tree.getTree().getTreeNode().getLabel().getValue()); + assertNull(tree.getTree().getTreeNode().getParent().getValue()); + assertEquals(0, tree.getTree().getTreeNode().getChildren().size()); + } + private static List getLocationList() { Location location1 = new Location(); location1.setId("Location/1");