Skip to content

Commit

Permalink
Handle inconsistent LocationHierarchy responses.
Browse files Browse the repository at this point in the history
 Extract the location id in the correct format from the returned value when getId() is called
 fixes #17
  • Loading branch information
lincmba committed May 21, 2024
1 parent dc803ec commit 336cbf9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.smartregister</groupId>
<artifactId>fhir-common-utils</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>1.0.1-SNAPSHOT</version>

<distributionManagement>
<repository>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Location> getLocationList() {
Location location1 = new Location();
location1.setId("Location/1");
Expand Down

0 comments on commit 336cbf9

Please sign in to comment.