From f5ee60139e9f2a125526e60d044764b18a6416f1 Mon Sep 17 00:00:00 2001 From: Reham Muzzamil Date: Mon, 23 Aug 2021 22:06:14 +0500 Subject: [PATCH 1/9] Initial Commit: Expose Location Hierarchy API --- .gitignore | 17 + README.md | 0 pom.xml | 67 ++++ .../extension/model/ChildTreeNode.java | 60 +++ .../extension/model/LocationHierarchy.java | 71 ++++ .../model/LocationHierarchyTree.java | 77 ++++ .../extension/model/ParentChildrenMap.java | 62 ++++ .../extension/model/SingleTreeNode.java | 58 +++ .../smartregister/extension/model/Tree.java | 345 ++++++++++++++++++ .../extension/model/TreeNode.java | 181 +++++++++ .../LocationHierarchyResourceProvider.java | 96 +++++ 11 files changed, 1034 insertions(+) mode change 100644 => 100755 .gitignore mode change 100644 => 100755 README.md create mode 100755 pom.xml create mode 100644 src/main/java/org/smartregister/extension/model/ChildTreeNode.java create mode 100644 src/main/java/org/smartregister/extension/model/LocationHierarchy.java create mode 100644 src/main/java/org/smartregister/extension/model/LocationHierarchyTree.java create mode 100644 src/main/java/org/smartregister/extension/model/ParentChildrenMap.java create mode 100644 src/main/java/org/smartregister/extension/model/SingleTreeNode.java create mode 100644 src/main/java/org/smartregister/extension/model/Tree.java create mode 100644 src/main/java/org/smartregister/extension/model/TreeNode.java create mode 100644 src/main/java/org/smartregister/extension/rest/LocationHierarchyResourceProvider.java diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 index a1c2a23..128621a --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,20 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* + +# Default ignored files +/shelf/ +/workspace.xml +target/ +.project +.classpath +.settings/ +.DS_Store +.idea/ +*.iml +*.ipr +*.iws +logfile +*.log* +.springBeans +.gradle/ diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/pom.xml b/pom.xml new file mode 100755 index 0000000..f925d23 --- /dev/null +++ b/pom.xml @@ -0,0 +1,67 @@ + + + 4.0.0 + + org.smartregister + hapi-fhir-opensrp-extensions + jar + 0.0.1-SNAPSHOT + hapi-fhir-opensrp-extensions + This repository holds all the code extensions on top of Hapi-FHIR + https://github.com/opensrp/hapi-fhir-opensrp-extensions + + + 8 + 8 + 5.2.4.RELEASE + + + + + org.springframework + spring-web + ${spring.version} + + + + org.springframework + spring-jdbc + ${spring.version} + + + + javax.servlet + servlet-api + 2.5 + provided + + + + + ca.uhn.hapi.fhir + hapi-fhir-base + 5.4.0 + + + + + ca.uhn.hapi.fhir + hapi-fhir-jpaserver-base + 5.4.0 + + + org.springframework + spring-jcl + + + commons-logging + commons-logging + + + + + + + \ No newline at end of file diff --git a/src/main/java/org/smartregister/extension/model/ChildTreeNode.java b/src/main/java/org/smartregister/extension/model/ChildTreeNode.java new file mode 100644 index 0000000..53e7eda --- /dev/null +++ b/src/main/java/org/smartregister/extension/model/ChildTreeNode.java @@ -0,0 +1,60 @@ +package org.smartregister.extension.model; + +import ca.uhn.fhir.model.api.annotation.Child; +import ca.uhn.fhir.model.api.annotation.DatatypeDef; +import ca.uhn.fhir.util.ElementUtil; +import org.hl7.fhir.instance.model.api.ICompositeType; +import org.hl7.fhir.r4.model.StringType; +import org.hl7.fhir.r4.model.Type; + +@DatatypeDef(name = "ChildTreeNode") +public class ChildTreeNode extends Type implements ICompositeType { + + @Child(name = "childId", type = {StringType.class}, order = 0, min = 1, max = 1, modifier = false, summary = false) + private StringType childId; + + @Child(name = "treeNode", type = {TreeNode.class}) + private TreeNode treeNode; + + public ChildTreeNode() { + treeNode = new TreeNode(); + } + + public StringType getChildId() { + return childId; + } + + public ChildTreeNode setChildId(StringType childId) { + this.childId = childId; + return this; + } + + public TreeNode getChildren() { + if (treeNode == null) { + treeNode = new TreeNode(); + } + return treeNode; + } + + public ChildTreeNode setChildren(TreeNode children) { + this.treeNode = children; + return this; + } + + @Override + public Type copy() { + ChildTreeNode childTreeNode = new ChildTreeNode(); + copyValues(childTreeNode); + return childTreeNode; + } + + @Override + public boolean isEmpty() { + return ElementUtil.isEmpty(childId); + } + + @Override + protected Type typedCopy() { + return copy(); + } +} diff --git a/src/main/java/org/smartregister/extension/model/LocationHierarchy.java b/src/main/java/org/smartregister/extension/model/LocationHierarchy.java new file mode 100644 index 0000000..b75acd0 --- /dev/null +++ b/src/main/java/org/smartregister/extension/model/LocationHierarchy.java @@ -0,0 +1,71 @@ +package org.smartregister.extension.model; + +import ca.uhn.fhir.model.api.annotation.Child; +import ca.uhn.fhir.model.api.annotation.Description; +import ca.uhn.fhir.model.api.annotation.ResourceDef; +import org.hl7.fhir.r4.model.*; + +import java.util.ArrayList; +import java.util.List; + +@ResourceDef(name = "LocationHierarchy", profile = "http://hl7.org/fhir/profiles/custom-resource") +public class LocationHierarchy extends Location { + + @Child( + name = "locationId", + type = {StringType.class}, + order = 5, + min = 0, + max = 1, + modifier = false, + summary = true + ) + @Description( + shortDefinition = "Unique id to the location", + formalDefinition = "Id of the location whose location hierarchy will be displayed." + ) + protected StringType locationId; + + @Child(name = "LocationHierarchyTree", type = {LocationHierarchyTree.class}) + @Description( + shortDefinition = "Complete Location Hierarchy Tree", + formalDefinition = "Consists of Location Hierarchy Tree and Parent Child Identifiers List" + ) + private LocationHierarchyTree locationHierarchyTree; + + @Override + public Location copy() { + Location dst = new Location(); + Bundle bundle = new Bundle(); + List theEntry = new ArrayList<>(); + Bundle.BundleEntryComponent entryComponent = new Bundle.BundleEntryComponent(); + entryComponent.setResource(new Bundle()); + theEntry.add(entryComponent); + bundle.setEntry(theEntry); + this.copyValues(dst); + return dst; + } + + @Override + public ResourceType getResourceType() { + return ResourceType.Bundle; + } + + public StringType getLocationId() { + return locationId; + } + + public void setLocationId(StringType locationId) { + this.locationId = locationId; + + } + + public LocationHierarchyTree getLocationHierarchyTree() { + return locationHierarchyTree; + } + + public void setLocationHierarchyTree(LocationHierarchyTree locationHierarchyTree) { + this.locationHierarchyTree = locationHierarchyTree; + } + +} diff --git a/src/main/java/org/smartregister/extension/model/LocationHierarchyTree.java b/src/main/java/org/smartregister/extension/model/LocationHierarchyTree.java new file mode 100644 index 0000000..91417a5 --- /dev/null +++ b/src/main/java/org/smartregister/extension/model/LocationHierarchyTree.java @@ -0,0 +1,77 @@ +package org.smartregister.extension.model; + +import ca.uhn.fhir.model.api.annotation.Child; +import ca.uhn.fhir.model.api.annotation.DatatypeDef; +import ca.uhn.fhir.util.ElementUtil; +import org.hl7.fhir.instance.model.api.ICompositeType; +import org.hl7.fhir.r4.model.Location; +import org.hl7.fhir.r4.model.StringType; +import org.hl7.fhir.r4.model.Type; +import org.thymeleaf.util.StringUtils; + +import java.util.List; + +@DatatypeDef(name = "LocationHierarchyTree") +public class LocationHierarchyTree extends Type implements ICompositeType { + + @Child(name = "locationsHierarchy") + private Tree locationsHierarchy; + + public LocationHierarchyTree() { + this.locationsHierarchy = new Tree(); + } + + public void addLocation(Location l) { + StringType idString = new StringType(); + idString.setValue(l.getId()); + if (!locationsHierarchy.hasNode(idString.getValue())) { + if (l.getPartOf() == null || StringUtils.isEmpty(l.getPartOf().getReference())) { + locationsHierarchy.addNode(idString.getValue(), l.getName(), l, null); + } else { + //get Parent Location + StringType parentId = new StringType(); + parentId.setValue(l.getPartOf().getReference()); + locationsHierarchy.addNode(idString.getValue(), l.getName(), l, parentId.getValue()); + } + } + } + + /** + * WARNING: Overrides existing locations + * + * @param locations + */ + public void buildTreeFromList(List locations) { + for (Location location : locations) { + addLocation(location); + } + } + + public Tree getLocationsHierarchy() { + return locationsHierarchy; + } + + public LocationHierarchyTree setLocationsHierarchy(Tree locationsHierarchy) { + this.locationsHierarchy = locationsHierarchy; + return this; + } + + + @Override + public Type copy() { + LocationHierarchyTree locationHierarchyTree = new LocationHierarchyTree(); + copyValues(locationHierarchyTree); + return locationHierarchyTree; + } + + @Override + public boolean isEmpty() { + return ElementUtil.isEmpty(locationsHierarchy); + } + + @Override + protected Type typedCopy() { + return copy(); + } +} + diff --git a/src/main/java/org/smartregister/extension/model/ParentChildrenMap.java b/src/main/java/org/smartregister/extension/model/ParentChildrenMap.java new file mode 100644 index 0000000..25e9238 --- /dev/null +++ b/src/main/java/org/smartregister/extension/model/ParentChildrenMap.java @@ -0,0 +1,62 @@ +package org.smartregister.extension.model; + +import ca.uhn.fhir.model.api.annotation.Child; +import ca.uhn.fhir.model.api.annotation.DatatypeDef; +import ca.uhn.fhir.util.ElementUtil; +import org.hl7.fhir.instance.model.api.ICompositeType; +import org.hl7.fhir.r4.model.StringType; +import org.hl7.fhir.r4.model.Type; + +import java.util.List; + +@DatatypeDef(name = "ParentChildrenMap") +public class ParentChildrenMap extends Type implements ICompositeType { + + @Child(name = "identifier", type = {StringType.class}, order = 0, min = 1, max = 1, modifier = false, summary = false) + private StringType identifier; + + @Child(name = "childIdentifiers", type = {StringType.class}, + order = 1, + min = 0, + max = -1, + modifier = false, + summary = false) + private List childIdentifiers; + + public StringType getIdentifier() { + return identifier; + } + + public ParentChildrenMap setIdentifier(StringType identifier) { + this.identifier = identifier; + return this; + } + + public List getChildIdentifiers() { + return childIdentifiers; + } + + public ParentChildrenMap setChildIdentifiers(List childIdentifiers) { + this.childIdentifiers = childIdentifiers; + return this; + } + + @Override + public Type copy() { + ParentChildrenMap parentChildrenMap = new ParentChildrenMap(); + copyValues(parentChildrenMap); + return parentChildrenMap; + } + + @Override + public boolean isEmpty() { + return ElementUtil.isEmpty(identifier); + } + + @Override + protected Type typedCopy() { + return copy(); + } + + +} diff --git a/src/main/java/org/smartregister/extension/model/SingleTreeNode.java b/src/main/java/org/smartregister/extension/model/SingleTreeNode.java new file mode 100644 index 0000000..8bbe4a9 --- /dev/null +++ b/src/main/java/org/smartregister/extension/model/SingleTreeNode.java @@ -0,0 +1,58 @@ +package org.smartregister.extension.model; + +import ca.uhn.fhir.model.api.annotation.Child; +import ca.uhn.fhir.model.api.annotation.DatatypeDef; +import ca.uhn.fhir.util.ElementUtil; +import org.hl7.fhir.instance.model.api.ICompositeType; +import org.hl7.fhir.r4.model.StringType; +import org.hl7.fhir.r4.model.Type; + +@DatatypeDef(name = "SingleTreeNode") +public class SingleTreeNode extends Type implements ICompositeType { + + @Child(name = "treeNodeId", type = {StringType.class}, order = 0) + private StringType treeNodeId; + + @Child(name = "treeNode", type = {TreeNode.class}, + order = 1, + min = 0, + max = -1, + modifier = false, + summary = false) + private TreeNode treeNode; + + @Override + public Type copy() { + SingleTreeNode singleTreeNode = new SingleTreeNode(); + copyValues(singleTreeNode); + return singleTreeNode; + } + + @Override + public boolean isEmpty() { + return ElementUtil.isEmpty(treeNodeId, treeNode); + } + + @Override + protected Type typedCopy() { + return copy(); + } + + public StringType getTreeNodeId() { + return treeNodeId; + } + + public SingleTreeNode setTreeNodeId(StringType treeNodeId) { + this.treeNodeId = treeNodeId; + return this; + } + + public TreeNode getTreeNode() { + return treeNode; + } + + public SingleTreeNode setTreeNode(TreeNode treeNode) { + this.treeNode = treeNode; + return this; + } +} diff --git a/src/main/java/org/smartregister/extension/model/Tree.java b/src/main/java/org/smartregister/extension/model/Tree.java new file mode 100644 index 0000000..3ae166a --- /dev/null +++ b/src/main/java/org/smartregister/extension/model/Tree.java @@ -0,0 +1,345 @@ +package org.smartregister.extension.model; + +import ca.uhn.fhir.model.api.annotation.Child; +import ca.uhn.fhir.model.api.annotation.DatatypeDef; +import ca.uhn.fhir.util.ElementUtil; +import org.hl7.fhir.instance.model.api.ICompositeType; +import org.hl7.fhir.r4.model.Location; +import org.hl7.fhir.r4.model.StringType; +import org.hl7.fhir.r4.model.Type; + +import java.util.*; + +@DatatypeDef(name = "Tree") +public class Tree extends Type implements ICompositeType { + + @Child(name = "listOfNodes", + type = {SingleTreeNode.class}) +// , +// order = 0, +// min = 0, +// max = -1, +// modifier = false, +// summary = false + private SingleTreeNode listOfNodes; +// private List listOfNodes; + + @Child(name = "parentChildren", + type = {ParentChildrenMap.class}, + order = 1, + min = 0, + max = -1, + modifier = false, + summary = false) + private List parentChildren; + + public SingleTreeNode getTree() { + return listOfNodes; + } + + public Tree() { + listOfNodes = new SingleTreeNode(); + parentChildren = new ArrayList<>(); + } + + private void addToParentChildRelation(String parent, String id) { + if (parentChildren == null) { + parentChildren = new ArrayList<>(); + } + List kids = null; + if (parentChildren != null) { + for (int i = 0; i < parentChildren.size(); i++) { + kids = parentChildren.get(i) != null && parentChildren.get(i).getIdentifier() != null && + parentChildren.get(i).getIdentifier().getValue() != null && + parentChildren.get(i).getIdentifier().getValue().equals(parent) ? parentChildren.get(i).getChildIdentifiers() : null; + } + } + + if (kids == null) { + kids = new ArrayList<>(); + } + StringType idStringType = new StringType(); + idStringType.setValue(id); + + StringType parentStringType = new StringType(); + parentStringType.setValue(parent); + kids.add(idStringType); + Boolean setParentChildMap = false; + for (int i = 0; i < parentChildren.size(); i++) { + if (parentChildren.get(i) != null && parentChildren.get(i).getIdentifier() != null && parentChildren.get(i).getIdentifier().getValue() != null && + parentChildren.get(i).getIdentifier().getValue().equals(parent)) { +// List childIdentifiers = parentChildrenMap != null ? parentChildrenMap.getChildIdentifiers() : new ArrayList<>(); +// childIdentifiers.add(kids); +// parentChildrenMap.setChildIdentifiers(kids); + parentChildren.get(i).setChildIdentifiers(kids); + setParentChildMap = true; + } + } + + if (!setParentChildMap) { + ParentChildrenMap parentChildrenMap = new ParentChildrenMap(); + parentChildrenMap.setIdentifier(parentStringType); + parentChildrenMap.setChildIdentifiers(kids); + parentChildren.add(parentChildrenMap); + } + +// parentChildren.add(parentStringType, kids); + } + + public void addNode(String id, String label, Location node, String parentId) { + if (listOfNodes == null) { + listOfNodes = new SingleTreeNode(); + } + + // if node exists we should break since user should write optimized code and also tree can not have duplicates + if (hasNode(id)) { + throw new IllegalArgumentException("Node with ID " + id + " already exists in tree"); + } + + TreeNode n = makeNode(id, label, node, parentId); + + if (parentId != null) { + addToParentChildRelation(parentId, id); + + TreeNode p = getNode(parentId); + + //if parent exists add to it otherwise add as root for now + if (p != null) { + p.addChild(n); + } else { + // if no parent exists add it as root node + String idString = (String) id; + if (idString.contains("/_")) { + idString = idString.substring(0, idString.indexOf("/_")); + } + SingleTreeNode singleTreeNode = new SingleTreeNode(); + StringType treeNodeId = new StringType(); + treeNodeId.setValue(idString); + singleTreeNode.setTreeNodeId(treeNodeId); + singleTreeNode.setTreeNode(n); +// listOfNodes.add(singleTreeNode); + } + } else { + // if no parent add it as root node + String idString = (String) id; + if (idString.contains("/_")) { + idString = idString.substring(0, idString.indexOf("/_")); + } + + SingleTreeNode singleTreeNode = new SingleTreeNode(); + StringType treeNodeId = new StringType(); + treeNodeId.setValue(idString); + singleTreeNode.setTreeNodeId(treeNodeId); + singleTreeNode.setTreeNode(n); + listOfNodes = singleTreeNode; + // + } + + List kids = null; + if (parentChildren != null) { + for (int i = 0; i < parentChildren.size(); i++) { + kids = parentChildren.get(i) != null && parentChildren.get(i).getIdentifier() != null && + parentChildren.get(i).getIdentifier().getValue() != null && parentChildren.get(i).getIdentifier().getValue().equals(id) ? parentChildren.get(i).getChildIdentifiers() : null; + } + } + +// move all its child nodes preexisting + if (kids != null) { + for (StringType kid : kids) { + //remove node from it current position and move to parent i.e. node currently being added + TreeNode kn = removeNode(kid.getValue()); + n.addChild(kn); + } + } + } + + private TreeNode makeNode(String id, String label, Location node, String parentId) { + TreeNode n = getNode(id); + if (n == null) { + n = new TreeNode(); + StringType nodeId = new StringType(); + String idString = (String) id; + if (idString.contains("/_")) { + idString = idString.substring(0, idString.indexOf("/_")); + } + nodeId.setValue((String) idString); +// nodeId.setValue((String) id); + n.setNodeId(nodeId); + StringType labelString = new StringType(); + labelString.setValue(label); + n.setLabel(labelString); + n.setNode((Location) node); + StringType parentIdString = new StringType(); + String parentIdStringVar = (String) parentId; + + + if (parentIdStringVar != null && parentIdStringVar.contains("/_")) { + parentIdStringVar = parentIdStringVar.substring(0, parentIdStringVar.indexOf("/_")); + } + parentIdString.setValue(parentIdStringVar); +// parentIdString.setValue(parentId); + n.setParent(parentIdString); + } + return n; + } + + + public TreeNode getNode(String id) { + // Check if id is any root node + String idString = (String) id; + if (idString.contains("/_")) { + idString = idString.substring(0, idString.indexOf("/_")); + } + +// for (SingleTreeNode singleTreeNode : listOfNodes) { + if (listOfNodes.getTreeNodeId() != null && listOfNodes.getTreeNodeId().getValue().equals(idString)) { +// if (singleTreeNode.getTreeNodeId() != null && singleTreeNode.getTreeNodeId().getValue().equals(idString)) { +// return map.get(idString); + return listOfNodes.getTreeNode(); +// } + + } + + // neither root itself nor parent of root + +// for (int j = 0; j < listOfNodes.size(); j++) { +// for (TreeNodeV2 root : map.get(j).g()) { + if(listOfNodes != null && listOfNodes.getTreeNode() != null && listOfNodes.getTreeNode().getChildren() != null) { + for(int i =0 ; i< listOfNodes.getTreeNode().getChildren().size() ; i++) { + ChildTreeNode childTreeNode = listOfNodes.getTreeNode().getChildren().get(i); +// TreeNode root = listOfNodes.getTreeNode().getChildren().get(i); +// = listOfNodes.getTreeNode(); +// for(TreeNode root : childTreeNode.getChildren()) { + TreeNode root = childTreeNode.getChildren(); + if (root != null) { + TreeNode n = root.findChild(id); + if (n != null) + return n; + } + } +// } + +// TreeNode root = listOfNodes.getTreeNode(); +// if (root != null) { +// TreeNode n = root.findChild(id); +// if (n != null) +// return n; +// } + } +// } +// } + + return null; + } + + + + TreeNode removeNode(String id) { + // Check if id is any root node\ + String idString = (String) id; + if (idString.contains("/_")) { + idString = idString.substring(0, idString.indexOf("/_")); + } + +// for (int i = 0; i < listOfNodes.size(); i++) { +// if (listOfNodes.get(i) != null && listOfNodes.get(i).getTreeNodeId() != null && +// listOfNodes.get(i).getTreeNodeId().equals(idString)) { +//// listOfNodes.get(i).getTreeNodeId().equals(id)) { +// listOfNodes.remove(i); +// } +// } +// if (map.containsKey(id)) { +// return map.remove(id); +// } + // neither root itself nor parent of root + +// for (SingleTreeNode singleTreeNode : listOfNodes) { + SingleTreeNode singleTreeNode = listOfNodes; + if (singleTreeNode != null && singleTreeNode.getTreeNode() != null && singleTreeNode.getTreeNode().getChildren() != null) { +// for (ChildTreeNode childTreeNode : singleTreeNode.getTreeNode().getChildren()) { + for (int j = 0; j < singleTreeNode.getTreeNode().getChildren().size(); j++) { + ChildTreeNode childTreeNode = singleTreeNode.getTreeNode().getChildren().get(j); + if (childTreeNode != null && childTreeNode.getChildId() != null && childTreeNode.getChildId().equals(idString)) { +// if (childTreeNode != null && childTreeNode.getChildId() != null && childTreeNode.getChildId().equals(id)) { + return removeChild(idString); +// return removeChild(id); + } + } + } +// } + +// for (TreeNode root : map.values()) { +// TreeNode n = root.removeChild(id); +// if (n != null) { +// return n; +// } +// } + return null; + } + + + public TreeNode removeChild(String id) { + if (listOfNodes != null) { +// for (SingleTreeNode singleTreeNode : listOfNodes) { + SingleTreeNode singleTreeNode = listOfNodes; + if (singleTreeNode != null && singleTreeNode.getTreeNode() != null) + for (int k = 0; k < singleTreeNode.getTreeNode().getChildren().size(); k++) { +// for(ChildTreeNode childTreeNode : singleTreeNode.getTreeNode().getChildren()) { + ChildTreeNode childTreeNode = singleTreeNode.getTreeNode().getChildren().get(k); + if (childTreeNode != null && childTreeNode.getChildId() != null && childTreeNode.getChildId().equals(id)) { +// return singleTreeNode.getTreeNode().getChildren().getremove(k); + } + } +// } +// for (TreeNode child : children.values()) { +// if (child.getId().equals(id)) { +// return children.remove(id); +// } else if (child.getChildren() != null) { +// TreeNode node = child.removeChild(id); +// if (node != null) +// return node; +// } +// } + } + return null; + } + + + public boolean hasNode(String id) { + return getNode(id) != null; + } + + public SingleTreeNode getListOfNodes() { + return listOfNodes; + } + + public void setListOfNodes(SingleTreeNode listOfNodes) { + this.listOfNodes = listOfNodes; + } + + public List getParentChildren() { + return parentChildren; + } + + public void setParentChildren(List parentChildren) { + this.parentChildren = parentChildren; + } + + @Override + public Type copy() { + Tree tree = new Tree(); + copyValues(tree); + return tree; + } + + @Override + public boolean isEmpty() { + return ElementUtil.isEmpty(listOfNodes); + } + + @Override + protected Type typedCopy() { + return copy(); + } + +} diff --git a/src/main/java/org/smartregister/extension/model/TreeNode.java b/src/main/java/org/smartregister/extension/model/TreeNode.java new file mode 100644 index 0000000..60deccb --- /dev/null +++ b/src/main/java/org/smartregister/extension/model/TreeNode.java @@ -0,0 +1,181 @@ +package org.smartregister.extension.model; + +import ca.uhn.fhir.model.api.annotation.Child; +import ca.uhn.fhir.model.api.annotation.DatatypeDef; +import ca.uhn.fhir.util.ElementUtil; +import org.hl7.fhir.instance.model.api.ICompositeType; +import org.hl7.fhir.r4.model.*; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; + + +@DatatypeDef(name = "TreeNode") +public class TreeNode extends Type implements ICompositeType { + + @Child(name = "name", type = {StringType.class}, order = 0, min = 1, max = 1, modifier = false, summary = false) + protected StringType name; + + @Child(name = "nodeId", type = {StringType.class}, order = 2) + private StringType nodeId; + + @Child(name = "label", type = {StringType.class}, order = 3) + private StringType label; + + @Child(name = "node", type = {Location.class}, order = 4) + private Location node; + + @Child(name = "parent", type = {StringType.class}, order = 5) + private StringType parent; + + @Child(name = "children", type = {ChildTreeNode.class}, + order = 6, + min = 0, + max = -1, + modifier = false, + summary = false) + private List children; + + public TreeNode() { + children = new ArrayList<>(); + } + + public StringType getName() { + if (name == null) { + name = new StringType(); + } + return name; + } + + public TreeNode setName(StringType name) { + this.name = name; + return this; + } + + + public StringType getLabel() { + return label; + } + + public TreeNode setLabel(StringType label) { + this.label = label; + return this; + } + + @Override + public Type copy() { + TreeNode treeNode = new TreeNode(); + copyValues(treeNode); + return treeNode; + } + + @Override + public boolean isEmpty() { + return ElementUtil.isEmpty(node); + } + + @Override + protected Type typedCopy() { + return copy(); + } + + public StringType getNodeId() { + return nodeId; + } + + public TreeNode setNodeId(StringType nodeId) { + this.nodeId = nodeId; + return this; + } + + public Location getNode() { + return node; + } + + public TreeNode setNode(Location node) { + this.node = node; + return this; + } + + public StringType getParent() { + return parent; + } + + public TreeNode setParent(StringType parent) { + this.parent = parent; + return this; + } + + public List getChildren() { + if (children == null) { + children = new ArrayList<>(); + } + return children; + } + + public TreeNode setChildren(List children) { + this.children = children; + return this; + } + + public void addChild(TreeNode node) { + if (children == null) { + children = new ArrayList<>(); + } + ChildTreeNode childTreeNode = new ChildTreeNode(); + childTreeNode.setChildId(node.getNodeId()); + List treeNodeList = new ArrayList<>(); + TreeNode treeNode = new TreeNode(); + treeNode.setNode(node.getNode()); + treeNode.setNodeId(node.getNodeId()); + treeNodeList.add(treeNode); + childTreeNode.setChildren(treeNode); +// childTreeNode.setChildNodes(treeNodeV2List); + children.add(childTreeNode); +// childTreeNodes.add(treeNodeV2); +// children.put(node.getNodeId(), node); + } + + + + public TreeNode findChild(String id) { + String idString = (String) id; + if (idString.contains("/_")) { + idString = idString.substring(0, idString.indexOf("/_")); + } + if (children != null && children.size() > 0) { + for (int i = 0; i < children.size(); i++) { + if (children.get(i) != null) { + for (ChildTreeNode child : children) { + if (child != null && child.getChildren() != null + && child.getChildren().getNodeId() != null && child.getChildren().getNodeId().getValue().equals(idString)) { + return child.getChildren(); + } else if (child != null && child != null) { + TreeNode node = child.getChildren().findChild(idString); + if (node != null) + return node; + } + } + } + } + } + return null; + } + + +// public TreeNode findChild(K id) { +// if (children != null) { +// for (TreeNode child : children.values()) { +// if (child.getId().equals(id)) { +// return child; +// } else if (child.getChildren() != null) { +// TreeNode node = child.findChild(id); +// if (node != null) +// return node; +// } +// } +// } +// return null; +// } +} diff --git a/src/main/java/org/smartregister/extension/rest/LocationHierarchyResourceProvider.java b/src/main/java/org/smartregister/extension/rest/LocationHierarchyResourceProvider.java new file mode 100644 index 0000000..edeb932 --- /dev/null +++ b/src/main/java/org/smartregister/extension/rest/LocationHierarchyResourceProvider.java @@ -0,0 +1,96 @@ +package org.smartregister.extension.rest; + +import ca.uhn.fhir.jpa.api.dao.IFhirResourceDao; +import ca.uhn.fhir.jpa.searchparam.SearchParameterMap; +import ca.uhn.fhir.rest.annotation.*; +import ca.uhn.fhir.rest.api.server.IBundleProvider; +import ca.uhn.fhir.rest.param.*; +import ca.uhn.fhir.rest.server.IResourceProvider; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.smartregister.extension.model.LocationHierarchy; +import org.smartregister.extension.model.LocationHierarchyTree; +import org.hl7.fhir.instance.model.api.IBaseResource; +import org.hl7.fhir.r4.model.*; +import org.springframework.beans.factory.annotation.Autowired; + +import javax.servlet.http.HttpServletRequest; +import java.util.*; + +public class LocationHierarchyResourceProvider implements IResourceProvider { + + @Autowired + IFhirResourceDao locationIFhirResourceDao; + + private static Logger logger = LogManager.getLogger(LocationHierarchyResourceProvider.class.toString()); + + @Override + public Class getResourceType() { + return LocationHierarchy.class; + } + + @Search + public LocationHierarchy getLocationHierarchy(HttpServletRequest request, + @OptionalParam(name = "identifier") TokenParam identifier) { + + SearchParameterMap paramMap = new SearchParameterMap(); + paramMap.add("identifier", identifier); + + IBundleProvider locationBundle = locationIFhirResourceDao.search(paramMap); + List locations = locationBundle != null ? + locationBundle.getResources(0, locationBundle.size()) : new ArrayList<>(); + Long id = null; + if (locations.size() > 0) { + id = locations.get(0).getIdElement().getIdPartAsLong(); + } + + LocationHierarchyTree locationHierarchyTree = new LocationHierarchyTree(); + LocationHierarchy locationHierarchy = new LocationHierarchy(); + if (id != null && locations.size() > 0) { + logger.info("Building Location Hierarchy of Location Id : " + id); + locationHierarchyTree.buildTreeFromList(getLocationHierarchy(id, locations.get(0))); + StringType locationIdString = new StringType().setId(id.toString()).getIdElement(); + locationHierarchy.setLocationId(locationIdString); + locationHierarchy.setId("Location Resource:" + id); + + locationHierarchy.setLocationHierarchyTree(locationHierarchyTree); + } + else { + locationHierarchy.setId("Location Resource : Not Found"); + } + return locationHierarchy; + + } + + private List getLocationHierarchy(Long id, IBaseResource parentLocation) { + return descendants(id, parentLocation); + } + + public List descendants(Long id, IBaseResource parentLocation) { + + SearchParameterMap paramMap = new SearchParameterMap(); + ReferenceAndListParam thePartOf = new ReferenceAndListParam(); + ReferenceParam partOf = new ReferenceParam(); + partOf.setValue("Location/" + id); + ReferenceOrListParam referenceOrListParam = new ReferenceOrListParam(); + referenceOrListParam.add(partOf); + thePartOf.addValue(referenceOrListParam); + paramMap.add("partof", thePartOf); + + IBundleProvider childLocationBundle = locationIFhirResourceDao.search(paramMap); + List allLocations = new ArrayList<>(); + if (parentLocation != null) { + allLocations.add((Location) parentLocation); + } + for (IBaseResource childLocation : childLocationBundle.getResources(0, childLocationBundle.size())) { + Location childLocationEntity = (Location) childLocation; + allLocations.add(childLocationEntity); + allLocations.addAll(descendants(childLocation.getIdElement().getIdPartAsLong(), null)); + } + return allLocations; + } + + public void setLocationIFhirResourceDao(IFhirResourceDao locationIFhirResourceDao) { + this.locationIFhirResourceDao = locationIFhirResourceDao; + } +} From 5d33c7931ebbecc068146dfbb323a2ec977a26a1 Mon Sep 17 00:00:00 2001 From: Reham Muzzamil Date: Tue, 24 Aug 2021 16:30:31 +0500 Subject: [PATCH 2/9] Fix building location hierarchy tree till level 3 and on. --- .../extension/model/ChildTreeNode.java | 0 .../extension/model/LocationHierarchy.java | 0 .../model/LocationHierarchyTree.java | 0 .../extension/model/ParentChildrenMap.java | 0 .../extension/model/SingleTreeNode.java | 0 .../smartregister/extension/model/Tree.java | 60 ++++++++++++++----- .../extension/model/TreeNode.java | 0 .../LocationHierarchyResourceProvider.java | 0 8 files changed, 44 insertions(+), 16 deletions(-) mode change 100644 => 100755 src/main/java/org/smartregister/extension/model/ChildTreeNode.java mode change 100644 => 100755 src/main/java/org/smartregister/extension/model/LocationHierarchy.java mode change 100644 => 100755 src/main/java/org/smartregister/extension/model/LocationHierarchyTree.java mode change 100644 => 100755 src/main/java/org/smartregister/extension/model/ParentChildrenMap.java mode change 100644 => 100755 src/main/java/org/smartregister/extension/model/SingleTreeNode.java mode change 100644 => 100755 src/main/java/org/smartregister/extension/model/Tree.java mode change 100644 => 100755 src/main/java/org/smartregister/extension/model/TreeNode.java mode change 100644 => 100755 src/main/java/org/smartregister/extension/rest/LocationHierarchyResourceProvider.java diff --git a/src/main/java/org/smartregister/extension/model/ChildTreeNode.java b/src/main/java/org/smartregister/extension/model/ChildTreeNode.java old mode 100644 new mode 100755 diff --git a/src/main/java/org/smartregister/extension/model/LocationHierarchy.java b/src/main/java/org/smartregister/extension/model/LocationHierarchy.java old mode 100644 new mode 100755 diff --git a/src/main/java/org/smartregister/extension/model/LocationHierarchyTree.java b/src/main/java/org/smartregister/extension/model/LocationHierarchyTree.java old mode 100644 new mode 100755 diff --git a/src/main/java/org/smartregister/extension/model/ParentChildrenMap.java b/src/main/java/org/smartregister/extension/model/ParentChildrenMap.java old mode 100644 new mode 100755 diff --git a/src/main/java/org/smartregister/extension/model/SingleTreeNode.java b/src/main/java/org/smartregister/extension/model/SingleTreeNode.java old mode 100644 new mode 100755 diff --git a/src/main/java/org/smartregister/extension/model/Tree.java b/src/main/java/org/smartregister/extension/model/Tree.java old mode 100644 new mode 100755 index 3ae166a..83fef4b --- a/src/main/java/org/smartregister/extension/model/Tree.java +++ b/src/main/java/org/smartregister/extension/model/Tree.java @@ -191,6 +191,8 @@ public TreeNode getNode(String id) { idString = idString.substring(0, idString.indexOf("/_")); } + + // for (SingleTreeNode singleTreeNode : listOfNodes) { if (listOfNodes.getTreeNodeId() != null && listOfNodes.getTreeNodeId().getValue().equals(idString)) { // if (singleTreeNode.getTreeNodeId() != null && singleTreeNode.getTreeNodeId().getValue().equals(idString)) { @@ -199,33 +201,44 @@ public TreeNode getNode(String id) { // } } + else { + if(listOfNodes != null && listOfNodes.getTreeNode() != null && listOfNodes.getTreeNode().getChildren() != null) { +// for() + + return recursivelyFindNode(idString, listOfNodes.getTreeNode().getChildren()); +// } +// while (listOfNodes.getTreeNode().getChildren().size() > 0); + } + } // neither root itself nor parent of root // for (int j = 0; j < listOfNodes.size(); j++) { // for (TreeNodeV2 root : map.get(j).g()) { - if(listOfNodes != null && listOfNodes.getTreeNode() != null && listOfNodes.getTreeNode().getChildren() != null) { - for(int i =0 ; i< listOfNodes.getTreeNode().getChildren().size() ; i++) { - ChildTreeNode childTreeNode = listOfNodes.getTreeNode().getChildren().get(i); -// TreeNode root = listOfNodes.getTreeNode().getChildren().get(i); -// = listOfNodes.getTreeNode(); -// for(TreeNode root : childTreeNode.getChildren()) { - TreeNode root = childTreeNode.getChildren(); - if (root != null) { - TreeNode n = root.findChild(id); - if (n != null) - return n; - } - } -// } -// TreeNode root = listOfNodes.getTreeNode(); +// temp close +// if(listOfNodes != null && listOfNodes.getTreeNode() != null && listOfNodes.getTreeNode().getChildren() != null) { +// for(int i =0 ; i< listOfNodes.getTreeNode().getChildren().size() ; i++) { +// ChildTreeNode childTreeNode = listOfNodes.getTreeNode().getChildren().get(i); +//// TreeNode root = listOfNodes.getTreeNode().getChildren().get(i); +//// = listOfNodes.getTreeNode(); +//// for(TreeNode root : childTreeNode.getChildren()) { +// TreeNode root = childTreeNode.getChildren(); // if (root != null) { // TreeNode n = root.findChild(id); // if (n != null) // return n; // } - } +// } +//// } +// +//// TreeNode root = listOfNodes.getTreeNode(); +//// if (root != null) { +//// TreeNode n = root.findChild(id); +//// if (n != null) +//// return n; +//// } +// } // } // } @@ -342,4 +355,19 @@ protected Type typedCopy() { return copy(); } + private TreeNode recursivelyFindNode(String idString, List childTreeNodeList) { + for(ChildTreeNode childTreeNode : childTreeNodeList) { + TreeNode treeNode = childTreeNode.getChildren(); + if(treeNode != null && treeNode.getNodeId() != null && treeNode.getNodeId().getValue().equals(idString)) { + return treeNode; + } + else { + if(treeNode != null && treeNode.getChildren() != null && treeNode.getChildren().size() > 0) { + return recursivelyFindNode(idString, treeNode.getChildren()); + } + } + } + return null; + } + } diff --git a/src/main/java/org/smartregister/extension/model/TreeNode.java b/src/main/java/org/smartregister/extension/model/TreeNode.java old mode 100644 new mode 100755 diff --git a/src/main/java/org/smartregister/extension/rest/LocationHierarchyResourceProvider.java b/src/main/java/org/smartregister/extension/rest/LocationHierarchyResourceProvider.java old mode 100644 new mode 100755 From 65d06fb6721a1189b18d268b909bdcc95dfd3f92 Mon Sep 17 00:00:00 2001 From: Reham Muzzamil Date: Wed, 25 Aug 2021 00:08:09 +0500 Subject: [PATCH 3/9] Fix bug in parent child hierarchy map --- .../smartregister/extension/model/Tree.java | 324 +++++++++--------- 1 file changed, 162 insertions(+), 162 deletions(-) diff --git a/src/main/java/org/smartregister/extension/model/Tree.java b/src/main/java/org/smartregister/extension/model/Tree.java index 83fef4b..a60f202 100755 --- a/src/main/java/org/smartregister/extension/model/Tree.java +++ b/src/main/java/org/smartregister/extension/model/Tree.java @@ -52,145 +52,148 @@ private void addToParentChildRelation(String parent, String id) { kids = parentChildren.get(i) != null && parentChildren.get(i).getIdentifier() != null && parentChildren.get(i).getIdentifier().getValue() != null && parentChildren.get(i).getIdentifier().getValue().equals(parent) ? parentChildren.get(i).getChildIdentifiers() : null; + System.out.println("Kids are : " + kids); + if (kids != null) { + break; + } } } - if (kids == null) { - kids = new ArrayList<>(); - } - StringType idStringType = new StringType(); - idStringType.setValue(id); - - StringType parentStringType = new StringType(); - parentStringType.setValue(parent); - kids.add(idStringType); - Boolean setParentChildMap = false; - for (int i = 0; i < parentChildren.size(); i++) { - if (parentChildren.get(i) != null && parentChildren.get(i).getIdentifier() != null && parentChildren.get(i).getIdentifier().getValue() != null && - parentChildren.get(i).getIdentifier().getValue().equals(parent)) { + if (kids == null) { + kids = new ArrayList<>(); + } + StringType idStringType = new StringType(); + idStringType.setValue(id); + + StringType parentStringType = new StringType(); + parentStringType.setValue(parent); + kids.add(idStringType); + Boolean setParentChildMap = false; + for (int i = 0; i < parentChildren.size(); i++) { + if (parentChildren.get(i) != null && parentChildren.get(i).getIdentifier() != null && parentChildren.get(i).getIdentifier().getValue() != null && + parentChildren.get(i).getIdentifier().getValue().equals(parent)) { // List childIdentifiers = parentChildrenMap != null ? parentChildrenMap.getChildIdentifiers() : new ArrayList<>(); // childIdentifiers.add(kids); // parentChildrenMap.setChildIdentifiers(kids); - parentChildren.get(i).setChildIdentifiers(kids); - setParentChildMap = true; + parentChildren.get(i).setChildIdentifiers(kids); + setParentChildMap = true; + } } - } - if (!setParentChildMap) { - ParentChildrenMap parentChildrenMap = new ParentChildrenMap(); - parentChildrenMap.setIdentifier(parentStringType); - parentChildrenMap.setChildIdentifiers(kids); - parentChildren.add(parentChildrenMap); - } + if (!setParentChildMap) { + ParentChildrenMap parentChildrenMap = new ParentChildrenMap(); + parentChildrenMap.setIdentifier(parentStringType); + parentChildrenMap.setChildIdentifiers(kids); + parentChildren.add(parentChildrenMap); + } // parentChildren.add(parentStringType, kids); - } - - public void addNode(String id, String label, Location node, String parentId) { - if (listOfNodes == null) { - listOfNodes = new SingleTreeNode(); } - // if node exists we should break since user should write optimized code and also tree can not have duplicates - if (hasNode(id)) { - throw new IllegalArgumentException("Node with ID " + id + " already exists in tree"); - } + public void addNode (String id, String label, Location node, String parentId){ + if (listOfNodes == null) { + listOfNodes = new SingleTreeNode(); + } + + // if node exists we should break since user should write optimized code and also tree can not have duplicates + if (hasNode(id)) { + throw new IllegalArgumentException("Node with ID " + id + " already exists in tree"); + } - TreeNode n = makeNode(id, label, node, parentId); + TreeNode n = makeNode(id, label, node, parentId); - if (parentId != null) { - addToParentChildRelation(parentId, id); + if (parentId != null) { + addToParentChildRelation(parentId, id); - TreeNode p = getNode(parentId); + TreeNode p = getNode(parentId); - //if parent exists add to it otherwise add as root for now - if (p != null) { - p.addChild(n); + //if parent exists add to it otherwise add as root for now + if (p != null) { + p.addChild(n); + } else { + // if no parent exists add it as root node + String idString = (String) id; + if (idString.contains("/_")) { + idString = idString.substring(0, idString.indexOf("/_")); + } + SingleTreeNode singleTreeNode = new SingleTreeNode(); + StringType treeNodeId = new StringType(); + treeNodeId.setValue(idString); + singleTreeNode.setTreeNodeId(treeNodeId); + singleTreeNode.setTreeNode(n); +// listOfNodes.add(singleTreeNode); + } } else { - // if no parent exists add it as root node + // if no parent add it as root node String idString = (String) id; if (idString.contains("/_")) { idString = idString.substring(0, idString.indexOf("/_")); } + SingleTreeNode singleTreeNode = new SingleTreeNode(); StringType treeNodeId = new StringType(); treeNodeId.setValue(idString); singleTreeNode.setTreeNodeId(treeNodeId); singleTreeNode.setTreeNode(n); -// listOfNodes.add(singleTreeNode); + listOfNodes = singleTreeNode; + // } - } else { - // if no parent add it as root node - String idString = (String) id; - if (idString.contains("/_")) { - idString = idString.substring(0, idString.indexOf("/_")); - } - - SingleTreeNode singleTreeNode = new SingleTreeNode(); - StringType treeNodeId = new StringType(); - treeNodeId.setValue(idString); - singleTreeNode.setTreeNodeId(treeNodeId); - singleTreeNode.setTreeNode(n); - listOfNodes = singleTreeNode; - // - } - List kids = null; - if (parentChildren != null) { - for (int i = 0; i < parentChildren.size(); i++) { - kids = parentChildren.get(i) != null && parentChildren.get(i).getIdentifier() != null && - parentChildren.get(i).getIdentifier().getValue() != null && parentChildren.get(i).getIdentifier().getValue().equals(id) ? parentChildren.get(i).getChildIdentifiers() : null; + List kids = null; + if (parentChildren != null) { + for (int i = 0; i < parentChildren.size(); i++) { + kids = parentChildren.get(i) != null && parentChildren.get(i).getIdentifier() != null && + parentChildren.get(i).getIdentifier().getValue() != null && parentChildren.get(i).getIdentifier().getValue().equals(id) ? parentChildren.get(i).getChildIdentifiers() : null; + } } - } // move all its child nodes preexisting - if (kids != null) { - for (StringType kid : kids) { - //remove node from it current position and move to parent i.e. node currently being added - TreeNode kn = removeNode(kid.getValue()); - n.addChild(kn); + if (kids != null) { + for (StringType kid : kids) { + //remove node from it current position and move to parent i.e. node currently being added + TreeNode kn = removeNode(kid.getValue()); + n.addChild(kn); + } } } - } - private TreeNode makeNode(String id, String label, Location node, String parentId) { - TreeNode n = getNode(id); - if (n == null) { - n = new TreeNode(); - StringType nodeId = new StringType(); - String idString = (String) id; - if (idString.contains("/_")) { - idString = idString.substring(0, idString.indexOf("/_")); - } - nodeId.setValue((String) idString); + private TreeNode makeNode (String id, String label, Location node, String parentId){ + TreeNode n = getNode(id); + if (n == null) { + n = new TreeNode(); + StringType nodeId = new StringType(); + String idString = (String) id; + if (idString.contains("/_")) { + idString = idString.substring(0, idString.indexOf("/_")); + } + nodeId.setValue((String) idString); // nodeId.setValue((String) id); - n.setNodeId(nodeId); - StringType labelString = new StringType(); - labelString.setValue(label); - n.setLabel(labelString); - n.setNode((Location) node); - StringType parentIdString = new StringType(); - String parentIdStringVar = (String) parentId; + n.setNodeId(nodeId); + StringType labelString = new StringType(); + labelString.setValue(label); + n.setLabel(labelString); + n.setNode((Location) node); + StringType parentIdString = new StringType(); + String parentIdStringVar = (String) parentId; - if (parentIdStringVar != null && parentIdStringVar.contains("/_")) { - parentIdStringVar = parentIdStringVar.substring(0, parentIdStringVar.indexOf("/_")); - } - parentIdString.setValue(parentIdStringVar); + if (parentIdStringVar != null && parentIdStringVar.contains("/_")) { + parentIdStringVar = parentIdStringVar.substring(0, parentIdStringVar.indexOf("/_")); + } + parentIdString.setValue(parentIdStringVar); // parentIdString.setValue(parentId); - n.setParent(parentIdString); + n.setParent(parentIdString); + } + return n; } - return n; - } - - public TreeNode getNode(String id) { - // Check if id is any root node - String idString = (String) id; - if (idString.contains("/_")) { - idString = idString.substring(0, idString.indexOf("/_")); - } + public TreeNode getNode (String id){ + // Check if id is any root node + String idString = (String) id; + if (idString.contains("/_")) { + idString = idString.substring(0, idString.indexOf("/_")); + } // for (SingleTreeNode singleTreeNode : listOfNodes) { @@ -200,18 +203,17 @@ public TreeNode getNode(String id) { return listOfNodes.getTreeNode(); // } - } - else { - if(listOfNodes != null && listOfNodes.getTreeNode() != null && listOfNodes.getTreeNode().getChildren() != null) { + } else { + if (listOfNodes != null && listOfNodes.getTreeNode() != null && listOfNodes.getTreeNode().getChildren() != null) { // for() - return recursivelyFindNode(idString, listOfNodes.getTreeNode().getChildren()); + return recursivelyFindNode(idString, listOfNodes.getTreeNode().getChildren()); // } // while (listOfNodes.getTreeNode().getChildren().size() > 0); } } - // neither root itself nor parent of root + // neither root itself nor parent of root // for (int j = 0; j < listOfNodes.size(); j++) { // for (TreeNodeV2 root : map.get(j).g()) { @@ -242,17 +244,16 @@ public TreeNode getNode(String id) { // } // } - return null; - } - + return null; + } - TreeNode removeNode(String id) { - // Check if id is any root node\ - String idString = (String) id; - if (idString.contains("/_")) { - idString = idString.substring(0, idString.indexOf("/_")); - } + TreeNode removeNode (String id){ + // Check if id is any root node\ + String idString = (String) id; + if (idString.contains("/_")) { + idString = idString.substring(0, idString.indexOf("/_")); + } // for (int i = 0; i < listOfNodes.size(); i++) { // if (listOfNodes.get(i) != null && listOfNodes.get(i).getTreeNodeId() != null && @@ -264,10 +265,10 @@ TreeNode removeNode(String id) { // if (map.containsKey(id)) { // return map.remove(id); // } - // neither root itself nor parent of root + // neither root itself nor parent of root // for (SingleTreeNode singleTreeNode : listOfNodes) { - SingleTreeNode singleTreeNode = listOfNodes; + SingleTreeNode singleTreeNode = listOfNodes; if (singleTreeNode != null && singleTreeNode.getTreeNode() != null && singleTreeNode.getTreeNode().getChildren() != null) { // for (ChildTreeNode childTreeNode : singleTreeNode.getTreeNode().getChildren()) { for (int j = 0; j < singleTreeNode.getTreeNode().getChildren().size(); j++) { @@ -287,14 +288,14 @@ TreeNode removeNode(String id) { // return n; // } // } - return null; - } + return null; + } - public TreeNode removeChild(String id) { - if (listOfNodes != null) { + public TreeNode removeChild (String id){ + if (listOfNodes != null) { // for (SingleTreeNode singleTreeNode : listOfNodes) { - SingleTreeNode singleTreeNode = listOfNodes; + SingleTreeNode singleTreeNode = listOfNodes; if (singleTreeNode != null && singleTreeNode.getTreeNode() != null) for (int k = 0; k < singleTreeNode.getTreeNode().getChildren().size(); k++) { // for(ChildTreeNode childTreeNode : singleTreeNode.getTreeNode().getChildren()) { @@ -313,61 +314,60 @@ public TreeNode removeChild(String id) { // return node; // } // } + } + return null; } - return null; - } - public boolean hasNode(String id) { - return getNode(id) != null; - } + public boolean hasNode (String id){ + return getNode(id) != null; + } - public SingleTreeNode getListOfNodes() { - return listOfNodes; - } + public SingleTreeNode getListOfNodes () { + return listOfNodes; + } - public void setListOfNodes(SingleTreeNode listOfNodes) { - this.listOfNodes = listOfNodes; - } + public void setListOfNodes (SingleTreeNode listOfNodes){ + this.listOfNodes = listOfNodes; + } - public List getParentChildren() { - return parentChildren; - } + public List getParentChildren () { + return parentChildren; + } - public void setParentChildren(List parentChildren) { - this.parentChildren = parentChildren; - } + public void setParentChildren (List < ParentChildrenMap > parentChildren) { + this.parentChildren = parentChildren; + } - @Override - public Type copy() { - Tree tree = new Tree(); - copyValues(tree); - return tree; - } + @Override + public Type copy () { + Tree tree = new Tree(); + copyValues(tree); + return tree; + } - @Override - public boolean isEmpty() { - return ElementUtil.isEmpty(listOfNodes); - } + @Override + public boolean isEmpty () { + return ElementUtil.isEmpty(listOfNodes); + } - @Override - protected Type typedCopy() { - return copy(); - } + @Override + protected Type typedCopy () { + return copy(); + } - private TreeNode recursivelyFindNode(String idString, List childTreeNodeList) { - for(ChildTreeNode childTreeNode : childTreeNodeList) { - TreeNode treeNode = childTreeNode.getChildren(); - if(treeNode != null && treeNode.getNodeId() != null && treeNode.getNodeId().getValue().equals(idString)) { - return treeNode; - } - else { - if(treeNode != null && treeNode.getChildren() != null && treeNode.getChildren().size() > 0) { - return recursivelyFindNode(idString, treeNode.getChildren()); + private TreeNode recursivelyFindNode (String idString, List < ChildTreeNode > childTreeNodeList){ + for (ChildTreeNode childTreeNode : childTreeNodeList) { + TreeNode treeNode = childTreeNode.getChildren(); + if (treeNode != null && treeNode.getNodeId() != null && treeNode.getNodeId().getValue().equals(idString)) { + return treeNode; + } else { + if (treeNode != null && treeNode.getChildren() != null && treeNode.getChildren().size() > 0) { + return recursivelyFindNode(idString, treeNode.getChildren()); + } } } + return null; } - return null; - } -} + } From bb053ea476e99ea699182f3c38e1a1eaa217a8d6 Mon Sep 17 00:00:00 2001 From: Reham Muzzamil Date: Wed, 25 Aug 2021 01:15:07 +0500 Subject: [PATCH 4/9] Code cleanup --- .../smartregister/extension/model/Tree.java | 420 +++++++----------- .../extension/model/TreeNode.java | 61 +-- 2 files changed, 193 insertions(+), 288 deletions(-) diff --git a/src/main/java/org/smartregister/extension/model/Tree.java b/src/main/java/org/smartregister/extension/model/Tree.java index a60f202..bcd3fe9 100755 --- a/src/main/java/org/smartregister/extension/model/Tree.java +++ b/src/main/java/org/smartregister/extension/model/Tree.java @@ -3,26 +3,21 @@ import ca.uhn.fhir.model.api.annotation.Child; import ca.uhn.fhir.model.api.annotation.DatatypeDef; import ca.uhn.fhir.util.ElementUtil; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.r4.model.Location; import org.hl7.fhir.r4.model.StringType; import org.hl7.fhir.r4.model.Type; +import org.smartregister.extension.rest.LocationHierarchyResourceProvider; import java.util.*; @DatatypeDef(name = "Tree") public class Tree extends Type implements ICompositeType { - @Child(name = "listOfNodes", - type = {SingleTreeNode.class}) -// , -// order = 0, -// min = 0, -// max = -1, -// modifier = false, -// summary = false + @Child(name = "listOfNodes", type = {SingleTreeNode.class}) private SingleTreeNode listOfNodes; -// private List listOfNodes; @Child(name = "parentChildren", type = {ParentChildrenMap.class}, @@ -33,6 +28,8 @@ public class Tree extends Type implements ICompositeType { summary = false) private List parentChildren; + private static Logger logger = LogManager.getLogger(Tree.class.toString()); + public SingleTreeNode getTree() { return listOfNodes; } @@ -52,258 +49,185 @@ private void addToParentChildRelation(String parent, String id) { kids = parentChildren.get(i) != null && parentChildren.get(i).getIdentifier() != null && parentChildren.get(i).getIdentifier().getValue() != null && parentChildren.get(i).getIdentifier().getValue().equals(parent) ? parentChildren.get(i).getChildIdentifiers() : null; - System.out.println("Kids are : " + kids); + logger.info("Kids are : " + kids); if (kids != null) { break; } } } - if (kids == null) { - kids = new ArrayList<>(); + if (kids == null) { + kids = new ArrayList<>(); + } + StringType idStringType = new StringType(); + idStringType.setValue(id); + + StringType parentStringType = new StringType(); + parentStringType.setValue(parent); + kids.add(idStringType); + Boolean setParentChildMap = false; + for (int i = 0; i < parentChildren.size(); i++) { + if (parentChildren.get(i) != null && parentChildren.get(i).getIdentifier() != null && parentChildren.get(i).getIdentifier().getValue() != null && + parentChildren.get(i).getIdentifier().getValue().equals(parent)) { + parentChildren.get(i).setChildIdentifiers(kids); + setParentChildMap = true; } - StringType idStringType = new StringType(); - idStringType.setValue(id); + } - StringType parentStringType = new StringType(); - parentStringType.setValue(parent); - kids.add(idStringType); - Boolean setParentChildMap = false; - for (int i = 0; i < parentChildren.size(); i++) { - if (parentChildren.get(i) != null && parentChildren.get(i).getIdentifier() != null && parentChildren.get(i).getIdentifier().getValue() != null && - parentChildren.get(i).getIdentifier().getValue().equals(parent)) { -// List childIdentifiers = parentChildrenMap != null ? parentChildrenMap.getChildIdentifiers() : new ArrayList<>(); -// childIdentifiers.add(kids); -// parentChildrenMap.setChildIdentifiers(kids); - parentChildren.get(i).setChildIdentifiers(kids); - setParentChildMap = true; - } - } + if (!setParentChildMap) { + ParentChildrenMap parentChildrenMap = new ParentChildrenMap(); + parentChildrenMap.setIdentifier(parentStringType); + parentChildrenMap.setChildIdentifiers(kids); + parentChildren.add(parentChildrenMap); + } - if (!setParentChildMap) { - ParentChildrenMap parentChildrenMap = new ParentChildrenMap(); - parentChildrenMap.setIdentifier(parentStringType); - parentChildrenMap.setChildIdentifiers(kids); - parentChildren.add(parentChildrenMap); - } + } -// parentChildren.add(parentStringType, kids); + public void addNode(String id, String label, Location node, String parentId) { + if (listOfNodes == null) { + listOfNodes = new SingleTreeNode(); } - public void addNode (String id, String label, Location node, String parentId){ - if (listOfNodes == null) { - listOfNodes = new SingleTreeNode(); - } - - // if node exists we should break since user should write optimized code and also tree can not have duplicates - if (hasNode(id)) { - throw new IllegalArgumentException("Node with ID " + id + " already exists in tree"); - } + // if node exists we should break since user should write optimized code and also tree can not have duplicates + if (hasNode(id)) { + throw new IllegalArgumentException("Node with ID " + id + " already exists in tree"); + } - TreeNode n = makeNode(id, label, node, parentId); + TreeNode n = makeNode(id, label, node, parentId); - if (parentId != null) { - addToParentChildRelation(parentId, id); + if (parentId != null) { + addToParentChildRelation(parentId, id); - TreeNode p = getNode(parentId); + TreeNode p = getNode(parentId); - //if parent exists add to it otherwise add as root for now - if (p != null) { - p.addChild(n); - } else { - // if no parent exists add it as root node - String idString = (String) id; - if (idString.contains("/_")) { - idString = idString.substring(0, idString.indexOf("/_")); - } - SingleTreeNode singleTreeNode = new SingleTreeNode(); - StringType treeNodeId = new StringType(); - treeNodeId.setValue(idString); - singleTreeNode.setTreeNodeId(treeNodeId); - singleTreeNode.setTreeNode(n); -// listOfNodes.add(singleTreeNode); - } + //if parent exists add to it otherwise add as root for now + if (p != null) { + p.addChild(n); } else { - // if no parent add it as root node + // if no parent exists add it as root node String idString = (String) id; if (idString.contains("/_")) { idString = idString.substring(0, idString.indexOf("/_")); } - SingleTreeNode singleTreeNode = new SingleTreeNode(); StringType treeNodeId = new StringType(); treeNodeId.setValue(idString); singleTreeNode.setTreeNodeId(treeNodeId); singleTreeNode.setTreeNode(n); - listOfNodes = singleTreeNode; - // } - - List kids = null; - if (parentChildren != null) { - for (int i = 0; i < parentChildren.size(); i++) { - kids = parentChildren.get(i) != null && parentChildren.get(i).getIdentifier() != null && - parentChildren.get(i).getIdentifier().getValue() != null && parentChildren.get(i).getIdentifier().getValue().equals(id) ? parentChildren.get(i).getChildIdentifiers() : null; - } + } else { + // if no parent add it as root node + String idString = (String) id; + if (idString.contains("/_")) { + idString = idString.substring(0, idString.indexOf("/_")); } -// move all its child nodes preexisting - if (kids != null) { - for (StringType kid : kids) { - //remove node from it current position and move to parent i.e. node currently being added - TreeNode kn = removeNode(kid.getValue()); - n.addChild(kn); - } - } + SingleTreeNode singleTreeNode = new SingleTreeNode(); + StringType treeNodeId = new StringType(); + treeNodeId.setValue(idString); + singleTreeNode.setTreeNodeId(treeNodeId); + singleTreeNode.setTreeNode(n); + listOfNodes = singleTreeNode; } - private TreeNode makeNode (String id, String label, Location node, String parentId){ - TreeNode n = getNode(id); - if (n == null) { - n = new TreeNode(); - StringType nodeId = new StringType(); - String idString = (String) id; - if (idString.contains("/_")) { - idString = idString.substring(0, idString.indexOf("/_")); - } - nodeId.setValue((String) idString); -// nodeId.setValue((String) id); - n.setNodeId(nodeId); - StringType labelString = new StringType(); - labelString.setValue(label); - n.setLabel(labelString); - n.setNode((Location) node); - StringType parentIdString = new StringType(); - String parentIdStringVar = (String) parentId; - - - if (parentIdStringVar != null && parentIdStringVar.contains("/_")) { - parentIdStringVar = parentIdStringVar.substring(0, parentIdStringVar.indexOf("/_")); - } - parentIdString.setValue(parentIdStringVar); -// parentIdString.setValue(parentId); - n.setParent(parentIdString); + List kids = null; + if (parentChildren != null) { + for (int i = 0; i < parentChildren.size(); i++) { + kids = parentChildren.get(i) != null && parentChildren.get(i).getIdentifier() != null && + parentChildren.get(i).getIdentifier().getValue() != null && parentChildren.get(i).getIdentifier().getValue().equals(id) ? parentChildren.get(i).getChildIdentifiers() : null; } - return n; } +// move all its child nodes preexisting + if (kids != null) { + for (StringType kid : kids) { + //remove node from it current position and move to parent i.e. node currently being added + TreeNode kn = removeNode(kid.getValue()); + n.addChild(kn); + } + } + } - public TreeNode getNode (String id){ - // Check if id is any root node + private TreeNode makeNode(String id, String label, Location node, String parentId) { + TreeNode n = getNode(id); + if (n == null) { + n = new TreeNode(); + StringType nodeId = new StringType(); String idString = (String) id; if (idString.contains("/_")) { idString = idString.substring(0, idString.indexOf("/_")); } - - -// for (SingleTreeNode singleTreeNode : listOfNodes) { - if (listOfNodes.getTreeNodeId() != null && listOfNodes.getTreeNodeId().getValue().equals(idString)) { -// if (singleTreeNode.getTreeNodeId() != null && singleTreeNode.getTreeNodeId().getValue().equals(idString)) { -// return map.get(idString); - return listOfNodes.getTreeNode(); -// } - - } else { - if (listOfNodes != null && listOfNodes.getTreeNode() != null && listOfNodes.getTreeNode().getChildren() != null) { -// for() - - return recursivelyFindNode(idString, listOfNodes.getTreeNode().getChildren()); -// } -// while (listOfNodes.getTreeNode().getChildren().size() > 0); - } + nodeId.setValue((String) idString); + n.setNodeId(nodeId); + StringType labelString = new StringType(); + labelString.setValue(label); + n.setLabel(labelString); + n.setNode((Location) node); + StringType parentIdString = new StringType(); + String parentIdStringVar = (String) parentId; + + + if (parentIdStringVar != null && parentIdStringVar.contains("/_")) { + parentIdStringVar = parentIdStringVar.substring(0, parentIdStringVar.indexOf("/_")); } + parentIdString.setValue(parentIdStringVar); +// parentIdString.setValue(parentId); + n.setParent(parentIdString); + } + return n; + } - // neither root itself nor parent of root - -// for (int j = 0; j < listOfNodes.size(); j++) { -// for (TreeNodeV2 root : map.get(j).g()) { - -// temp close -// if(listOfNodes != null && listOfNodes.getTreeNode() != null && listOfNodes.getTreeNode().getChildren() != null) { -// for(int i =0 ; i< listOfNodes.getTreeNode().getChildren().size() ; i++) { -// ChildTreeNode childTreeNode = listOfNodes.getTreeNode().getChildren().get(i); -//// TreeNode root = listOfNodes.getTreeNode().getChildren().get(i); -//// = listOfNodes.getTreeNode(); -//// for(TreeNode root : childTreeNode.getChildren()) { -// TreeNode root = childTreeNode.getChildren(); -// if (root != null) { -// TreeNode n = root.findChild(id); -// if (n != null) -// return n; -// } -// } -//// } -// -//// TreeNode root = listOfNodes.getTreeNode(); -//// if (root != null) { -//// TreeNode n = root.findChild(id); -//// if (n != null) -//// return n; -//// } -// } -// } -// } - return null; + public TreeNode getNode(String id) { + // Check if id is any root node + String idString = (String) id; + if (idString.contains("/_")) { + idString = idString.substring(0, idString.indexOf("/_")); } - TreeNode removeNode (String id){ - // Check if id is any root node\ - String idString = (String) id; - if (idString.contains("/_")) { - idString = idString.substring(0, idString.indexOf("/_")); + if (listOfNodes.getTreeNodeId() != null && listOfNodes.getTreeNodeId().getValue().equals(idString)) { + return listOfNodes.getTreeNode(); + + } else { + if (listOfNodes != null && listOfNodes.getTreeNode() != null && listOfNodes.getTreeNode().getChildren() != null) { + return recursivelyFindNode(idString, listOfNodes.getTreeNode().getChildren()); } + } + return null; + } -// for (int i = 0; i < listOfNodes.size(); i++) { -// if (listOfNodes.get(i) != null && listOfNodes.get(i).getTreeNodeId() != null && -// listOfNodes.get(i).getTreeNodeId().equals(idString)) { -//// listOfNodes.get(i).getTreeNodeId().equals(id)) { -// listOfNodes.remove(i); -// } -// } -// if (map.containsKey(id)) { -// return map.remove(id); -// } - // neither root itself nor parent of root -// for (SingleTreeNode singleTreeNode : listOfNodes) { - SingleTreeNode singleTreeNode = listOfNodes; - if (singleTreeNode != null && singleTreeNode.getTreeNode() != null && singleTreeNode.getTreeNode().getChildren() != null) { -// for (ChildTreeNode childTreeNode : singleTreeNode.getTreeNode().getChildren()) { - for (int j = 0; j < singleTreeNode.getTreeNode().getChildren().size(); j++) { - ChildTreeNode childTreeNode = singleTreeNode.getTreeNode().getChildren().get(j); - if (childTreeNode != null && childTreeNode.getChildId() != null && childTreeNode.getChildId().equals(idString)) { -// if (childTreeNode != null && childTreeNode.getChildId() != null && childTreeNode.getChildId().equals(id)) { - return removeChild(idString); -// return removeChild(id); - } + TreeNode removeNode(String id) { + // Check if id is any root node\ + String idString = (String) id; + if (idString.contains("/_")) { + idString = idString.substring(0, idString.indexOf("/_")); + } + + SingleTreeNode singleTreeNode = listOfNodes; + if (singleTreeNode != null && singleTreeNode.getTreeNode() != null && singleTreeNode.getTreeNode().getChildren() != null) { + for (int j = 0; j < singleTreeNode.getTreeNode().getChildren().size(); j++) { + ChildTreeNode childTreeNode = singleTreeNode.getTreeNode().getChildren().get(j); + if (childTreeNode != null && childTreeNode.getChildId() != null && childTreeNode.getChildId().equals(idString)) { + return removeChild(idString); } } -// } - -// for (TreeNode root : map.values()) { -// TreeNode n = root.removeChild(id); -// if (n != null) { -// return n; -// } -// } - return null; } + return null; + } - public TreeNode removeChild (String id){ - if (listOfNodes != null) { -// for (SingleTreeNode singleTreeNode : listOfNodes) { - SingleTreeNode singleTreeNode = listOfNodes; - if (singleTreeNode != null && singleTreeNode.getTreeNode() != null) - for (int k = 0; k < singleTreeNode.getTreeNode().getChildren().size(); k++) { -// for(ChildTreeNode childTreeNode : singleTreeNode.getTreeNode().getChildren()) { - ChildTreeNode childTreeNode = singleTreeNode.getTreeNode().getChildren().get(k); - if (childTreeNode != null && childTreeNode.getChildId() != null && childTreeNode.getChildId().equals(id)) { + public TreeNode removeChild(String id) { + if (listOfNodes != null) { + SingleTreeNode singleTreeNode = listOfNodes; + if (singleTreeNode != null && singleTreeNode.getTreeNode() != null) + for (int k = 0; k < singleTreeNode.getTreeNode().getChildren().size(); k++) { + ChildTreeNode childTreeNode = singleTreeNode.getTreeNode().getChildren().get(k); + if (childTreeNode != null && childTreeNode.getChildId() != null && childTreeNode.getChildId().equals(id)) { // return singleTreeNode.getTreeNode().getChildren().getremove(k); - } } + } // } // for (TreeNode child : children.values()) { // if (child.getId().equals(id)) { @@ -314,60 +238,60 @@ public TreeNode removeChild (String id){ // return node; // } // } - } - return null; } + return null; + } - public boolean hasNode (String id){ - return getNode(id) != null; - } + public boolean hasNode(String id) { + return getNode(id) != null; + } - public SingleTreeNode getListOfNodes () { - return listOfNodes; - } + public SingleTreeNode getListOfNodes() { + return listOfNodes; + } - public void setListOfNodes (SingleTreeNode listOfNodes){ - this.listOfNodes = listOfNodes; - } + public void setListOfNodes(SingleTreeNode listOfNodes) { + this.listOfNodes = listOfNodes; + } - public List getParentChildren () { - return parentChildren; - } + public List getParentChildren() { + return parentChildren; + } - public void setParentChildren (List < ParentChildrenMap > parentChildren) { - this.parentChildren = parentChildren; - } + public void setParentChildren(List parentChildren) { + this.parentChildren = parentChildren; + } - @Override - public Type copy () { - Tree tree = new Tree(); - copyValues(tree); - return tree; - } + @Override + public Type copy() { + Tree tree = new Tree(); + copyValues(tree); + return tree; + } - @Override - public boolean isEmpty () { - return ElementUtil.isEmpty(listOfNodes); - } + @Override + public boolean isEmpty() { + return ElementUtil.isEmpty(listOfNodes); + } - @Override - protected Type typedCopy () { - return copy(); - } + @Override + protected Type typedCopy() { + return copy(); + } - private TreeNode recursivelyFindNode (String idString, List < ChildTreeNode > childTreeNodeList){ - for (ChildTreeNode childTreeNode : childTreeNodeList) { - TreeNode treeNode = childTreeNode.getChildren(); - if (treeNode != null && treeNode.getNodeId() != null && treeNode.getNodeId().getValue().equals(idString)) { - return treeNode; - } else { - if (treeNode != null && treeNode.getChildren() != null && treeNode.getChildren().size() > 0) { - return recursivelyFindNode(idString, treeNode.getChildren()); - } + private TreeNode recursivelyFindNode(String idString, List childTreeNodeList) { + for (ChildTreeNode childTreeNode : childTreeNodeList) { + TreeNode treeNode = childTreeNode.getChildren(); + if (treeNode != null && treeNode.getNodeId() != null && treeNode.getNodeId().getValue().equals(idString)) { + return treeNode; + } else { + if (treeNode != null && treeNode.getChildren() != null && treeNode.getChildren().size() > 0) { + return recursivelyFindNode(idString, treeNode.getChildren()); } } - return null; } - + return null; } + +} diff --git a/src/main/java/org/smartregister/extension/model/TreeNode.java b/src/main/java/org/smartregister/extension/model/TreeNode.java index 60deccb..509a417 100755 --- a/src/main/java/org/smartregister/extension/model/TreeNode.java +++ b/src/main/java/org/smartregister/extension/model/TreeNode.java @@ -131,51 +131,32 @@ public void addChild(TreeNode node) { treeNode.setNodeId(node.getNodeId()); treeNodeList.add(treeNode); childTreeNode.setChildren(treeNode); -// childTreeNode.setChildNodes(treeNodeV2List); children.add(childTreeNode); -// childTreeNodes.add(treeNodeV2); -// children.put(node.getNodeId(), node); } - - - public TreeNode findChild(String id) { - String idString = (String) id; - if (idString.contains("/_")) { - idString = idString.substring(0, idString.indexOf("/_")); - } - if (children != null && children.size() > 0) { - for (int i = 0; i < children.size(); i++) { - if (children.get(i) != null) { - for (ChildTreeNode child : children) { - if (child != null && child.getChildren() != null - && child.getChildren().getNodeId() != null && child.getChildren().getNodeId().getValue().equals(idString)) { - return child.getChildren(); - } else if (child != null && child != null) { - TreeNode node = child.getChildren().findChild(idString); - if (node != null) - return node; - } - } - } - } - } - return null; - } - - -// public TreeNode findChild(K id) { -// if (children != null) { -// for (TreeNode child : children.values()) { -// if (child.getId().equals(id)) { -// return child; -// } else if (child.getChildren() != null) { -// TreeNode node = child.findChild(id); -// if (node != null) -// return node; -// } +// +// public TreeNode findChild(String id) { +// String idString = (String) id; +// if (idString.contains("/_")) { +// idString = idString.substring(0, idString.indexOf("/_")); +// } +// if (children != null && children.size() > 0) { +// for (int i = 0; i < children.size(); i++) { +// if (children.get(i) != null) { +// for (ChildTreeNode child : children) { +// if (child != null && child.getChildren() != null +// && child.getChildren().getNodeId() != null && child.getChildren().getNodeId().getValue().equals(idString)) { +// return child.getChildren(); +// } else if (child != null && child != null) { +// TreeNode node = child.getChildren().findChild(idString); +// if (node != null) +// return node; +// } +// } +// } // } // } // return null; // } + } From 376bf9f9cca8553c6baa7f404f71f2d9742b046b Mon Sep 17 00:00:00 2001 From: Reham Muzzamil Date: Thu, 26 Aug 2021 01:16:44 +0500 Subject: [PATCH 5/9] Add unit tests --- pom.xml | 9 +++ .../extension/model/TreeNode.java | 58 ++++++++++------- .../extension/model/TreeNodeTest.java | 65 +++++++++++++++++++ .../extension/model/TreeTest.java | 57 ++++++++++++++++ 4 files changed, 165 insertions(+), 24 deletions(-) create mode 100644 src/test/java/org/smartregister/extension/model/TreeNodeTest.java create mode 100644 src/test/java/org/smartregister/extension/model/TreeTest.java diff --git a/pom.xml b/pom.xml index f925d23..0b0a61c 100755 --- a/pom.xml +++ b/pom.xml @@ -61,6 +61,15 @@ + + + + junit + junit + 4.13.1 + test + + diff --git a/src/main/java/org/smartregister/extension/model/TreeNode.java b/src/main/java/org/smartregister/extension/model/TreeNode.java index 509a417..2a38906 100755 --- a/src/main/java/org/smartregister/extension/model/TreeNode.java +++ b/src/main/java/org/smartregister/extension/model/TreeNode.java @@ -41,6 +41,14 @@ public TreeNode() { children = new ArrayList<>(); } + public TreeNode(StringType name, StringType nodeId, StringType label, Location node, StringType parent) { + this.name = name; + this.nodeId = nodeId; + this.label = label; + this.node = node; + this.parent = parent; + } + public StringType getName() { if (name == null) { name = new StringType(); @@ -129,34 +137,36 @@ public void addChild(TreeNode node) { TreeNode treeNode = new TreeNode(); treeNode.setNode(node.getNode()); treeNode.setNodeId(node.getNodeId()); + treeNode.setLabel(node.getLabel()); + treeNode.setParent(node.getParent()); treeNodeList.add(treeNode); childTreeNode.setChildren(treeNode); children.add(childTreeNode); } -// -// public TreeNode findChild(String id) { -// String idString = (String) id; -// if (idString.contains("/_")) { -// idString = idString.substring(0, idString.indexOf("/_")); -// } -// if (children != null && children.size() > 0) { -// for (int i = 0; i < children.size(); i++) { -// if (children.get(i) != null) { -// for (ChildTreeNode child : children) { -// if (child != null && child.getChildren() != null -// && child.getChildren().getNodeId() != null && child.getChildren().getNodeId().getValue().equals(idString)) { -// return child.getChildren(); -// } else if (child != null && child != null) { -// TreeNode node = child.getChildren().findChild(idString); -// if (node != null) -// return node; -// } -// } -// } -// } -// } -// return null; -// } + + public TreeNode findChild(String id) { + String idString = (String) id; + if (idString.contains("/_")) { + idString = idString.substring(0, idString.indexOf("/_")); + } + if (children != null && children.size() > 0) { + for (int i = 0; i < children.size(); i++) { + if (children.get(i) != null) { + for (ChildTreeNode child : children) { + if (child != null && child.getChildren() != null + && child.getChildren().getNodeId() != null && child.getChildren().getNodeId().getValue().equals(idString)) { + return child.getChildren(); + } else if (child != null && child != null) { + TreeNode node = child.getChildren().findChild(idString); + if (node != null) + return node; + } + } + } + } + } + return null; + } } diff --git a/src/test/java/org/smartregister/extension/model/TreeNodeTest.java b/src/test/java/org/smartregister/extension/model/TreeNodeTest.java new file mode 100644 index 0000000..f4df891 --- /dev/null +++ b/src/test/java/org/smartregister/extension/model/TreeNodeTest.java @@ -0,0 +1,65 @@ +package org.smartregister.extension.model; + +import org.hl7.fhir.r4.model.Location; +import org.hl7.fhir.r4.model.StringType; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNull; + +public class TreeNodeTest { + + @Test + public void testAddingChild() { + + StringType rootNodeName = new StringType(); + rootNodeName.setValue("Root Node"); + + StringType rootNodeId = new StringType(); + rootNodeId.setValue("Location/1"); + + StringType rootNodeLabel = new StringType(); + rootNodeLabel.setValue("Root Location Node"); + + Location location = new Location(); + location.setId("Location/1"); + + StringType childNodeName = new StringType(); + childNodeName.setValue("Child Node"); + + StringType childNodeId = new StringType(); + childNodeId.setValue("Location/2"); + + StringType childNodeLabel = new StringType(); + childNodeLabel.setValue("Child Location Node"); + + TreeNode rootNode = new TreeNode(rootNodeName, rootNodeId, rootNodeLabel, location, null); + TreeNode childNode = new TreeNode(childNodeName, childNodeId, childNodeLabel, location, rootNodeId); + rootNode.addChild(childNode); + + assertEquals(childNodeId.getValue(), rootNode.findChild(childNodeId.getValue()).getNodeId().getValue()); + assertEquals(rootNodeId.getValue(), rootNode.findChild(childNodeId.getValue()).getParent().getValue()); + } + + @Test + public void findInvalidChildren() { + StringType rootNodeName = new StringType(); + rootNodeName.setValue("Root Node"); + + StringType rootNodeId = new StringType(); + rootNodeId.setValue("Location/1"); + + StringType rootNodeLabel = new StringType(); + rootNodeLabel.setValue("Root Location Node"); + + Location location = new Location(); + location.setId("Location/1"); + + TreeNode rootNode = new TreeNode(rootNodeName, rootNodeId, rootNodeLabel, location, null); + assertEquals(0, rootNode.getChildren().size()); + assertNull(rootNode.findChild("Location/2")); + } +} diff --git a/src/test/java/org/smartregister/extension/model/TreeTest.java b/src/test/java/org/smartregister/extension/model/TreeTest.java new file mode 100644 index 0000000..00cf9cb --- /dev/null +++ b/src/test/java/org/smartregister/extension/model/TreeTest.java @@ -0,0 +1,57 @@ +package org.smartregister.extension.model; + + +import org.hl7.fhir.r4.model.Location; +import org.junit.Test; + + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNull; +import static org.junit.Assert.assertNotNull; + +public class TreeTest { + + @Test + public void testAddingNodeWithOutParent() { + Tree tree = new Tree(); + Location location = new Location(); + location.setId("testId"); + tree.addNode("Location/1", "test", location, null); + + TreeNode treeNode = tree.getNode("Location/1"); + + assertEquals("Location/1", treeNode.getNodeId().getValue()); + assertEquals("test", treeNode.getLabel().getValue()); + assertEquals(location, treeNode.getNode()); + assertNull(treeNode.getParent().getValue()); + } + + @Test(expected = IllegalArgumentException.class) + public void testCannotReAddExistingNode() { + Tree tree = new Tree(); + Location location = new Location(); + location.setId("testId"); + tree.addNode("Location/1", "test", location, null); + tree.addNode("Location/1", "test", location, null); + } + + @Test + public void testAddingNodeWithValidParent() { + Tree tree = new Tree(); + Location location = new Location(); + location.setId("testId"); + tree.addNode("Location/1", "test", location, null); + tree.addNode("Location/2", "test2", location, "Location/1"); + + TreeNode childNode = tree.getNode("Location/2"); + + assertEquals("Location/2", childNode.getNodeId().getValue()); + assertEquals("test2", childNode.getLabel().getValue()); + assertEquals(location, childNode.getNode()); + assertNotNull(childNode.getParent().getValue()); + + String parentNodeId = childNode.getParent().getValue(); + + assertEquals("Location/1", parentNodeId); + } +} From 1e3c17d84ef19dde3e3bab6ad58caaeec7536b72 Mon Sep 17 00:00:00 2001 From: Reham Muzzamil Date: Thu, 26 Aug 2021 17:43:46 +0500 Subject: [PATCH 6/9] Add unit tests --- .../model/LocationHierarchyTreeTest.java | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 src/test/java/org/smartregister/extension/model/LocationHierarchyTreeTest.java diff --git a/src/test/java/org/smartregister/extension/model/LocationHierarchyTreeTest.java b/src/test/java/org/smartregister/extension/model/LocationHierarchyTreeTest.java new file mode 100644 index 0000000..fdb4bee --- /dev/null +++ b/src/test/java/org/smartregister/extension/model/LocationHierarchyTreeTest.java @@ -0,0 +1,104 @@ +package org.smartregister.extension.model; + +import ca.uhn.fhir.rest.param.ReferenceParam; +import org.hl7.fhir.r4.model.Location; +import org.hl7.fhir.r4.model.Reference; +import org.junit.Test; + + +import java.util.ArrayList; +import java.util.List; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNull; +import static org.junit.Assert.assertNotNull; + +public class LocationHierarchyTreeTest { + + @Test + public void testAddLocationWithoutChildLocations() { + Location location = new Location(); + location.setId("Location/1"); + 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()); + } + + @Test + public void testBuildTreeFromList() { + Location location1 = new Location(); + location1.setId("Location/1"); + location1.setName("Test Location"); + Reference partOfReference = new Reference(); + partOfReference.setReference(""); + location1.setPartOf(partOfReference); + + Location location2 = new Location(); + location2.setId("Location/2"); + location2.setName("Test Location 2"); + partOfReference = new Reference(); + partOfReference.setReference("Location/1"); + location2.setPartOf(partOfReference); + + Location location3 = new Location(); + location3.setId("Location/3"); + location3.setName("Test Location 3"); + partOfReference = new Reference(); + partOfReference.setReference("Location/2"); + location3.setPartOf(partOfReference); + + LocationHierarchyTree locationHierarchyTree = new LocationHierarchyTree(); + + List locationList = new ArrayList<>(); + locationList.add(location1); + locationList.add(location2); + locationList.add(location3); + + locationHierarchyTree.buildTreeFromList(locationList); + 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(1, tree.getTree().getTreeNode().getChildren().size()); + + assertEquals("Location/2", tree.getTree().getTreeNode().getChildren().get(0).getChildren().getNodeId().getValue()); + assertEquals("Test Location 2", tree.getTree().getTreeNode().getChildren().get(0).getChildren().getLabel().getValue()); + assertNotNull(tree.getTree().getTreeNode().getChildren().get(0).getChildren().getParent().getValue()); + assertEquals("Location/1", tree.getTree().getTreeNode().getChildren().get(0).getChildren().getParent().getValue()); + assertEquals(1, tree.getTree().getTreeNode().getChildren().get(0).getChildren().getChildren().size()); + + assertEquals("Location/3", tree.getTree().getTreeNode().getChildren().get(0).getChildren().getChildren().get(0).getChildren().getNodeId().getValue()); + assertEquals("Test Location 3", tree.getTree().getTreeNode().getChildren().get(0).getChildren().getChildren().get(0).getChildren().getLabel().getValue()); + assertNotNull(tree.getTree().getTreeNode().getChildren().get(0).getChildren().getChildren().get(0).getChildren().getParent().getValue()); + assertEquals("Location/2", tree.getTree().getTreeNode().getChildren().get(0).getChildren().getChildren().get(0).getChildren().getParent().getValue()); + assertEquals(0, tree.getTree().getTreeNode().getChildren().get(0).getChildren().getChildren().get(0).getChildren().getChildren().size()); + + assertNotNull(locationHierarchyTree.getLocationsHierarchy().getParentChildren()); + assertEquals(2, locationHierarchyTree.getLocationsHierarchy().getParentChildren().size()); + assertEquals("Location/1", locationHierarchyTree.getLocationsHierarchy().getParentChildren().get(0).getIdentifier().getValue()); + assertEquals(1, locationHierarchyTree.getLocationsHierarchy().getParentChildren().get(0).getChildIdentifiers().size()); + assertEquals("Location/2", locationHierarchyTree.getLocationsHierarchy().getParentChildren().get(0).getChildIdentifiers().get(0).getValue()); + + assertEquals("Location/2", locationHierarchyTree.getLocationsHierarchy().getParentChildren().get(1).getIdentifier().getValue()); + assertEquals(1, locationHierarchyTree.getLocationsHierarchy().getParentChildren().get(1).getChildIdentifiers().size()); + assertEquals("Location/3", locationHierarchyTree.getLocationsHierarchy().getParentChildren().get(1).getChildIdentifiers().get(0).getValue()); + + + } + +} From 56f9d7ae0538231f5e7c1aded108867d5460c982 Mon Sep 17 00:00:00 2001 From: Reham Muzzamil Date: Fri, 27 Aug 2021 00:35:17 +0500 Subject: [PATCH 7/9] Code cleanup, minor fixes and code formatting --- .../extension/model/ChildTreeNode.java | 94 ++-- .../extension/model/LocationHierarchy.java | 96 ++-- .../model/LocationHierarchyTree.java | 101 ++-- .../extension/model/ParentChildrenMap.java | 91 ++-- .../extension/model/SingleTreeNode.java | 90 ++-- .../smartregister/extension/model/Tree.java | 496 ++++++++---------- .../extension/model/TreeNode.java | 308 ++++++----- .../LocationHierarchyResourceProvider.java | 148 +++--- 8 files changed, 680 insertions(+), 744 deletions(-) diff --git a/src/main/java/org/smartregister/extension/model/ChildTreeNode.java b/src/main/java/org/smartregister/extension/model/ChildTreeNode.java index 53e7eda..e6d3f4f 100755 --- a/src/main/java/org/smartregister/extension/model/ChildTreeNode.java +++ b/src/main/java/org/smartregister/extension/model/ChildTreeNode.java @@ -10,51 +10,51 @@ @DatatypeDef(name = "ChildTreeNode") public class ChildTreeNode extends Type implements ICompositeType { - @Child(name = "childId", type = {StringType.class}, order = 0, min = 1, max = 1, modifier = false, summary = false) - private StringType childId; - - @Child(name = "treeNode", type = {TreeNode.class}) - private TreeNode treeNode; - - public ChildTreeNode() { - treeNode = new TreeNode(); - } - - public StringType getChildId() { - return childId; - } - - public ChildTreeNode setChildId(StringType childId) { - this.childId = childId; - return this; - } - - public TreeNode getChildren() { - if (treeNode == null) { - treeNode = new TreeNode(); - } - return treeNode; - } - - public ChildTreeNode setChildren(TreeNode children) { - this.treeNode = children; - return this; - } - - @Override - public Type copy() { - ChildTreeNode childTreeNode = new ChildTreeNode(); - copyValues(childTreeNode); - return childTreeNode; - } - - @Override - public boolean isEmpty() { - return ElementUtil.isEmpty(childId); - } - - @Override - protected Type typedCopy() { - return copy(); - } + @Child(name = "childId", type = { StringType.class }, order = 0, min = 1, max = 1, modifier = false, summary = false) + private StringType childId; + + @Child(name = "treeNode", type = { TreeNode.class }) + private TreeNode treeNode; + + public ChildTreeNode() { + treeNode = new TreeNode(); + } + + public StringType getChildId() { + return childId; + } + + public ChildTreeNode setChildId(StringType childId) { + this.childId = childId; + return this; + } + + public TreeNode getChildren() { + if (treeNode == null) { + treeNode = new TreeNode(); + } + return treeNode; + } + + public ChildTreeNode setChildren(TreeNode children) { + this.treeNode = children; + return this; + } + + @Override + public Type copy() { + ChildTreeNode childTreeNode = new ChildTreeNode(); + copyValues(childTreeNode); + return childTreeNode; + } + + @Override + public boolean isEmpty() { + return ElementUtil.isEmpty(childId); + } + + @Override + protected Type typedCopy() { + return copy(); + } } diff --git a/src/main/java/org/smartregister/extension/model/LocationHierarchy.java b/src/main/java/org/smartregister/extension/model/LocationHierarchy.java index b75acd0..84488c6 100755 --- a/src/main/java/org/smartregister/extension/model/LocationHierarchy.java +++ b/src/main/java/org/smartregister/extension/model/LocationHierarchy.java @@ -11,61 +11,61 @@ @ResourceDef(name = "LocationHierarchy", profile = "http://hl7.org/fhir/profiles/custom-resource") public class LocationHierarchy extends Location { - @Child( - name = "locationId", - type = {StringType.class}, - order = 5, - min = 0, - max = 1, - modifier = false, - summary = true - ) - @Description( - shortDefinition = "Unique id to the location", - formalDefinition = "Id of the location whose location hierarchy will be displayed." - ) - protected StringType locationId; + @Child( + name = "locationId", + type = { StringType.class }, + order = 5, + min = 0, + max = 1, + modifier = false, + summary = true + ) + @Description( + shortDefinition = "Unique id to the location", + formalDefinition = "Id of the location whose location hierarchy will be displayed." + ) + protected StringType locationId; - @Child(name = "LocationHierarchyTree", type = {LocationHierarchyTree.class}) - @Description( - shortDefinition = "Complete Location Hierarchy Tree", - formalDefinition = "Consists of Location Hierarchy Tree and Parent Child Identifiers List" - ) - private LocationHierarchyTree locationHierarchyTree; + @Child(name = "LocationHierarchyTree", type = { LocationHierarchyTree.class }) + @Description( + shortDefinition = "Complete Location Hierarchy Tree", + formalDefinition = "Consists of Location Hierarchy Tree and Parent Child Identifiers List" + ) + private LocationHierarchyTree locationHierarchyTree; - @Override - public Location copy() { - Location dst = new Location(); - Bundle bundle = new Bundle(); - List theEntry = new ArrayList<>(); - Bundle.BundleEntryComponent entryComponent = new Bundle.BundleEntryComponent(); - entryComponent.setResource(new Bundle()); - theEntry.add(entryComponent); - bundle.setEntry(theEntry); - this.copyValues(dst); - return dst; - } + @Override + public Location copy() { + Location dst = new Location(); + Bundle bundle = new Bundle(); + List theEntry = new ArrayList<>(); + Bundle.BundleEntryComponent entryComponent = new Bundle.BundleEntryComponent(); + entryComponent.setResource(new Bundle()); + theEntry.add(entryComponent); + bundle.setEntry(theEntry); + this.copyValues(dst); + return dst; + } - @Override - public ResourceType getResourceType() { - return ResourceType.Bundle; - } + @Override + public ResourceType getResourceType() { + return ResourceType.Bundle; + } - public StringType getLocationId() { - return locationId; - } + public StringType getLocationId() { + return locationId; + } - public void setLocationId(StringType locationId) { - this.locationId = locationId; + public void setLocationId(StringType locationId) { + this.locationId = locationId; - } + } - public LocationHierarchyTree getLocationHierarchyTree() { - return locationHierarchyTree; - } + public LocationHierarchyTree getLocationHierarchyTree() { + return locationHierarchyTree; + } - public void setLocationHierarchyTree(LocationHierarchyTree locationHierarchyTree) { - this.locationHierarchyTree = locationHierarchyTree; - } + public void setLocationHierarchyTree(LocationHierarchyTree locationHierarchyTree) { + this.locationHierarchyTree = locationHierarchyTree; + } } diff --git a/src/main/java/org/smartregister/extension/model/LocationHierarchyTree.java b/src/main/java/org/smartregister/extension/model/LocationHierarchyTree.java index 91417a5..5a73778 100755 --- a/src/main/java/org/smartregister/extension/model/LocationHierarchyTree.java +++ b/src/main/java/org/smartregister/extension/model/LocationHierarchyTree.java @@ -14,64 +14,63 @@ @DatatypeDef(name = "LocationHierarchyTree") public class LocationHierarchyTree extends Type implements ICompositeType { - @Child(name = "locationsHierarchy") - private Tree locationsHierarchy; + @Child(name = "locationsHierarchy") + private Tree locationsHierarchy; - public LocationHierarchyTree() { - this.locationsHierarchy = new Tree(); - } + public LocationHierarchyTree() { + this.locationsHierarchy = new Tree(); + } - public void addLocation(Location l) { - StringType idString = new StringType(); - idString.setValue(l.getId()); - if (!locationsHierarchy.hasNode(idString.getValue())) { - if (l.getPartOf() == null || StringUtils.isEmpty(l.getPartOf().getReference())) { - locationsHierarchy.addNode(idString.getValue(), l.getName(), l, null); - } else { - //get Parent Location - StringType parentId = new StringType(); - parentId.setValue(l.getPartOf().getReference()); - locationsHierarchy.addNode(idString.getValue(), l.getName(), l, parentId.getValue()); - } - } - } + public void addLocation(Location l) { + StringType idString = new StringType(); + idString.setValue(l.getId()); + if (!locationsHierarchy.hasNode(idString.getValue())) { + if (l.getPartOf() == null || StringUtils.isEmpty(l.getPartOf().getReference())) { + locationsHierarchy.addNode(idString.getValue(), l.getName(), l, null); + } else { + //get Parent Location + StringType parentId = new StringType(); + parentId.setValue(l.getPartOf().getReference()); + locationsHierarchy.addNode(idString.getValue(), l.getName(), l, parentId.getValue()); + } + } + } - /** - * WARNING: Overrides existing locations - * - * @param locations - */ - public void buildTreeFromList(List locations) { - for (Location location : locations) { - addLocation(location); - } - } + /** + * WARNING: Overrides existing locations + * + * @param locations + */ + public void buildTreeFromList(List locations) { + for (Location location : locations) { + addLocation(location); + } + } - public Tree getLocationsHierarchy() { - return locationsHierarchy; - } + public Tree getLocationsHierarchy() { + return locationsHierarchy; + } - public LocationHierarchyTree setLocationsHierarchy(Tree locationsHierarchy) { - this.locationsHierarchy = locationsHierarchy; - return this; - } + public LocationHierarchyTree setLocationsHierarchy(Tree locationsHierarchy) { + this.locationsHierarchy = locationsHierarchy; + return this; + } + @Override + public Type copy() { + LocationHierarchyTree locationHierarchyTree = new LocationHierarchyTree(); + copyValues(locationHierarchyTree); + return locationHierarchyTree; + } - @Override - public Type copy() { - LocationHierarchyTree locationHierarchyTree = new LocationHierarchyTree(); - copyValues(locationHierarchyTree); - return locationHierarchyTree; - } + @Override + public boolean isEmpty() { + return ElementUtil.isEmpty(locationsHierarchy); + } - @Override - public boolean isEmpty() { - return ElementUtil.isEmpty(locationsHierarchy); - } - - @Override - protected Type typedCopy() { - return copy(); - } + @Override + protected Type typedCopy() { + return copy(); + } } diff --git a/src/main/java/org/smartregister/extension/model/ParentChildrenMap.java b/src/main/java/org/smartregister/extension/model/ParentChildrenMap.java index 25e9238..fc40c69 100755 --- a/src/main/java/org/smartregister/extension/model/ParentChildrenMap.java +++ b/src/main/java/org/smartregister/extension/model/ParentChildrenMap.java @@ -12,51 +12,50 @@ @DatatypeDef(name = "ParentChildrenMap") public class ParentChildrenMap extends Type implements ICompositeType { - @Child(name = "identifier", type = {StringType.class}, order = 0, min = 1, max = 1, modifier = false, summary = false) - private StringType identifier; - - @Child(name = "childIdentifiers", type = {StringType.class}, - order = 1, - min = 0, - max = -1, - modifier = false, - summary = false) - private List childIdentifiers; - - public StringType getIdentifier() { - return identifier; - } - - public ParentChildrenMap setIdentifier(StringType identifier) { - this.identifier = identifier; - return this; - } - - public List getChildIdentifiers() { - return childIdentifiers; - } - - public ParentChildrenMap setChildIdentifiers(List childIdentifiers) { - this.childIdentifiers = childIdentifiers; - return this; - } - - @Override - public Type copy() { - ParentChildrenMap parentChildrenMap = new ParentChildrenMap(); - copyValues(parentChildrenMap); - return parentChildrenMap; - } - - @Override - public boolean isEmpty() { - return ElementUtil.isEmpty(identifier); - } - - @Override - protected Type typedCopy() { - return copy(); - } - + @Child(name = "identifier", type = { StringType.class }, order = 0, min = 1, max = 1, modifier = false, summary = false) + private StringType identifier; + + @Child(name = "childIdentifiers", type = { StringType.class }, + order = 1, + min = 0, + max = -1, + modifier = false, + summary = false) + private List childIdentifiers; + + public StringType getIdentifier() { + return identifier; + } + + public ParentChildrenMap setIdentifier(StringType identifier) { + this.identifier = identifier; + return this; + } + + public List getChildIdentifiers() { + return childIdentifiers; + } + + public ParentChildrenMap setChildIdentifiers(List childIdentifiers) { + this.childIdentifiers = childIdentifiers; + return this; + } + + @Override + public Type copy() { + ParentChildrenMap parentChildrenMap = new ParentChildrenMap(); + copyValues(parentChildrenMap); + return parentChildrenMap; + } + + @Override + public boolean isEmpty() { + return ElementUtil.isEmpty(identifier); + } + + @Override + protected Type typedCopy() { + return copy(); + } } diff --git a/src/main/java/org/smartregister/extension/model/SingleTreeNode.java b/src/main/java/org/smartregister/extension/model/SingleTreeNode.java index 8bbe4a9..b0f6392 100755 --- a/src/main/java/org/smartregister/extension/model/SingleTreeNode.java +++ b/src/main/java/org/smartregister/extension/model/SingleTreeNode.java @@ -10,49 +10,49 @@ @DatatypeDef(name = "SingleTreeNode") public class SingleTreeNode extends Type implements ICompositeType { - @Child(name = "treeNodeId", type = {StringType.class}, order = 0) - private StringType treeNodeId; - - @Child(name = "treeNode", type = {TreeNode.class}, - order = 1, - min = 0, - max = -1, - modifier = false, - summary = false) - private TreeNode treeNode; - - @Override - public Type copy() { - SingleTreeNode singleTreeNode = new SingleTreeNode(); - copyValues(singleTreeNode); - return singleTreeNode; - } - - @Override - public boolean isEmpty() { - return ElementUtil.isEmpty(treeNodeId, treeNode); - } - - @Override - protected Type typedCopy() { - return copy(); - } - - public StringType getTreeNodeId() { - return treeNodeId; - } - - public SingleTreeNode setTreeNodeId(StringType treeNodeId) { - this.treeNodeId = treeNodeId; - return this; - } - - public TreeNode getTreeNode() { - return treeNode; - } - - public SingleTreeNode setTreeNode(TreeNode treeNode) { - this.treeNode = treeNode; - return this; - } + @Child(name = "treeNodeId", type = { StringType.class }, order = 0) + private StringType treeNodeId; + + @Child(name = "treeNode", type = { TreeNode.class }, + order = 1, + min = 0, + max = -1, + modifier = false, + summary = false) + private TreeNode treeNode; + + @Override + public Type copy() { + SingleTreeNode singleTreeNode = new SingleTreeNode(); + copyValues(singleTreeNode); + return singleTreeNode; + } + + @Override + public boolean isEmpty() { + return ElementUtil.isEmpty(treeNodeId, treeNode); + } + + @Override + protected Type typedCopy() { + return copy(); + } + + public StringType getTreeNodeId() { + return treeNodeId; + } + + public SingleTreeNode setTreeNodeId(StringType treeNodeId) { + this.treeNodeId = treeNodeId; + return this; + } + + public TreeNode getTreeNode() { + return treeNode; + } + + public SingleTreeNode setTreeNode(TreeNode treeNode) { + this.treeNode = treeNode; + return this; + } } diff --git a/src/main/java/org/smartregister/extension/model/Tree.java b/src/main/java/org/smartregister/extension/model/Tree.java index bcd3fe9..e1e88b4 100755 --- a/src/main/java/org/smartregister/extension/model/Tree.java +++ b/src/main/java/org/smartregister/extension/model/Tree.java @@ -16,282 +16,224 @@ @DatatypeDef(name = "Tree") public class Tree extends Type implements ICompositeType { - @Child(name = "listOfNodes", type = {SingleTreeNode.class}) - private SingleTreeNode listOfNodes; - - @Child(name = "parentChildren", - type = {ParentChildrenMap.class}, - order = 1, - min = 0, - max = -1, - modifier = false, - summary = false) - private List parentChildren; - - private static Logger logger = LogManager.getLogger(Tree.class.toString()); - - public SingleTreeNode getTree() { - return listOfNodes; - } - - public Tree() { - listOfNodes = new SingleTreeNode(); - parentChildren = new ArrayList<>(); - } - - private void addToParentChildRelation(String parent, String id) { - if (parentChildren == null) { - parentChildren = new ArrayList<>(); - } - List kids = null; - if (parentChildren != null) { - for (int i = 0; i < parentChildren.size(); i++) { - kids = parentChildren.get(i) != null && parentChildren.get(i).getIdentifier() != null && - parentChildren.get(i).getIdentifier().getValue() != null && - parentChildren.get(i).getIdentifier().getValue().equals(parent) ? parentChildren.get(i).getChildIdentifiers() : null; - logger.info("Kids are : " + kids); - if (kids != null) { - break; - } - } - } - - if (kids == null) { - kids = new ArrayList<>(); - } - StringType idStringType = new StringType(); - idStringType.setValue(id); - - StringType parentStringType = new StringType(); - parentStringType.setValue(parent); - kids.add(idStringType); - Boolean setParentChildMap = false; - for (int i = 0; i < parentChildren.size(); i++) { - if (parentChildren.get(i) != null && parentChildren.get(i).getIdentifier() != null && parentChildren.get(i).getIdentifier().getValue() != null && - parentChildren.get(i).getIdentifier().getValue().equals(parent)) { - parentChildren.get(i).setChildIdentifiers(kids); - setParentChildMap = true; - } - } - - if (!setParentChildMap) { - ParentChildrenMap parentChildrenMap = new ParentChildrenMap(); - parentChildrenMap.setIdentifier(parentStringType); - parentChildrenMap.setChildIdentifiers(kids); - parentChildren.add(parentChildrenMap); - } - - } - - public void addNode(String id, String label, Location node, String parentId) { - if (listOfNodes == null) { - listOfNodes = new SingleTreeNode(); - } - - // if node exists we should break since user should write optimized code and also tree can not have duplicates - if (hasNode(id)) { - throw new IllegalArgumentException("Node with ID " + id + " already exists in tree"); - } - - TreeNode n = makeNode(id, label, node, parentId); - - if (parentId != null) { - addToParentChildRelation(parentId, id); - - TreeNode p = getNode(parentId); - - //if parent exists add to it otherwise add as root for now - if (p != null) { - p.addChild(n); - } else { - // if no parent exists add it as root node - String idString = (String) id; - if (idString.contains("/_")) { - idString = idString.substring(0, idString.indexOf("/_")); - } - SingleTreeNode singleTreeNode = new SingleTreeNode(); - StringType treeNodeId = new StringType(); - treeNodeId.setValue(idString); - singleTreeNode.setTreeNodeId(treeNodeId); - singleTreeNode.setTreeNode(n); - } - } else { - // if no parent add it as root node - String idString = (String) id; - if (idString.contains("/_")) { - idString = idString.substring(0, idString.indexOf("/_")); - } - - SingleTreeNode singleTreeNode = new SingleTreeNode(); - StringType treeNodeId = new StringType(); - treeNodeId.setValue(idString); - singleTreeNode.setTreeNodeId(treeNodeId); - singleTreeNode.setTreeNode(n); - listOfNodes = singleTreeNode; - } - - List kids = null; - if (parentChildren != null) { - for (int i = 0; i < parentChildren.size(); i++) { - kids = parentChildren.get(i) != null && parentChildren.get(i).getIdentifier() != null && - parentChildren.get(i).getIdentifier().getValue() != null && parentChildren.get(i).getIdentifier().getValue().equals(id) ? parentChildren.get(i).getChildIdentifiers() : null; - } - } - -// move all its child nodes preexisting - if (kids != null) { - for (StringType kid : kids) { - //remove node from it current position and move to parent i.e. node currently being added - TreeNode kn = removeNode(kid.getValue()); - n.addChild(kn); - } - } - } - - private TreeNode makeNode(String id, String label, Location node, String parentId) { - TreeNode n = getNode(id); - if (n == null) { - n = new TreeNode(); - StringType nodeId = new StringType(); - String idString = (String) id; - if (idString.contains("/_")) { - idString = idString.substring(0, idString.indexOf("/_")); - } - nodeId.setValue((String) idString); - n.setNodeId(nodeId); - StringType labelString = new StringType(); - labelString.setValue(label); - n.setLabel(labelString); - n.setNode((Location) node); - StringType parentIdString = new StringType(); - String parentIdStringVar = (String) parentId; - - - if (parentIdStringVar != null && parentIdStringVar.contains("/_")) { - parentIdStringVar = parentIdStringVar.substring(0, parentIdStringVar.indexOf("/_")); - } - parentIdString.setValue(parentIdStringVar); -// parentIdString.setValue(parentId); - n.setParent(parentIdString); - } - return n; - } - - - public TreeNode getNode(String id) { - // Check if id is any root node - String idString = (String) id; - if (idString.contains("/_")) { - idString = idString.substring(0, idString.indexOf("/_")); - } - - - if (listOfNodes.getTreeNodeId() != null && listOfNodes.getTreeNodeId().getValue().equals(idString)) { - return listOfNodes.getTreeNode(); - - } else { - if (listOfNodes != null && listOfNodes.getTreeNode() != null && listOfNodes.getTreeNode().getChildren() != null) { - return recursivelyFindNode(idString, listOfNodes.getTreeNode().getChildren()); - } - } - return null; - } - - - TreeNode removeNode(String id) { - // Check if id is any root node\ - String idString = (String) id; - if (idString.contains("/_")) { - idString = idString.substring(0, idString.indexOf("/_")); - } - - SingleTreeNode singleTreeNode = listOfNodes; - if (singleTreeNode != null && singleTreeNode.getTreeNode() != null && singleTreeNode.getTreeNode().getChildren() != null) { - for (int j = 0; j < singleTreeNode.getTreeNode().getChildren().size(); j++) { - ChildTreeNode childTreeNode = singleTreeNode.getTreeNode().getChildren().get(j); - if (childTreeNode != null && childTreeNode.getChildId() != null && childTreeNode.getChildId().equals(idString)) { - return removeChild(idString); - } - } - } - return null; - } - - - public TreeNode removeChild(String id) { - if (listOfNodes != null) { - SingleTreeNode singleTreeNode = listOfNodes; - if (singleTreeNode != null && singleTreeNode.getTreeNode() != null) - for (int k = 0; k < singleTreeNode.getTreeNode().getChildren().size(); k++) { - ChildTreeNode childTreeNode = singleTreeNode.getTreeNode().getChildren().get(k); - if (childTreeNode != null && childTreeNode.getChildId() != null && childTreeNode.getChildId().equals(id)) { -// return singleTreeNode.getTreeNode().getChildren().getremove(k); - } - } -// } -// for (TreeNode child : children.values()) { -// if (child.getId().equals(id)) { -// return children.remove(id); -// } else if (child.getChildren() != null) { -// TreeNode node = child.removeChild(id); -// if (node != null) -// return node; -// } -// } - } - return null; - } - - - public boolean hasNode(String id) { - return getNode(id) != null; - } - - public SingleTreeNode getListOfNodes() { - return listOfNodes; - } - - public void setListOfNodes(SingleTreeNode listOfNodes) { - this.listOfNodes = listOfNodes; - } - - public List getParentChildren() { - return parentChildren; - } - - public void setParentChildren(List parentChildren) { - this.parentChildren = parentChildren; - } - - @Override - public Type copy() { - Tree tree = new Tree(); - copyValues(tree); - return tree; - } - - @Override - public boolean isEmpty() { - return ElementUtil.isEmpty(listOfNodes); - } - - @Override - protected Type typedCopy() { - return copy(); - } - - private TreeNode recursivelyFindNode(String idString, List childTreeNodeList) { - for (ChildTreeNode childTreeNode : childTreeNodeList) { - TreeNode treeNode = childTreeNode.getChildren(); - if (treeNode != null && treeNode.getNodeId() != null && treeNode.getNodeId().getValue().equals(idString)) { - return treeNode; - } else { - if (treeNode != null && treeNode.getChildren() != null && treeNode.getChildren().size() > 0) { - return recursivelyFindNode(idString, treeNode.getChildren()); - } - } - } - return null; - } + @Child(name = "listOfNodes", type = { SingleTreeNode.class }) + private SingleTreeNode listOfNodes; + + @Child(name = "parentChildren", + type = { ParentChildrenMap.class }, + order = 1, + min = 0, + max = -1, + modifier = false, + summary = false) + private List parentChildren; + + private static Logger logger = LogManager.getLogger(Tree.class.toString()); + + public SingleTreeNode getTree() { + return listOfNodes; + } + + public Tree() { + listOfNodes = new SingleTreeNode(); + parentChildren = new ArrayList<>(); + } + + private void addToParentChildRelation(String parent, String id) { + if (parentChildren == null) { + parentChildren = new ArrayList<>(); + } + List kids = null; + if (parentChildren != null) { + for (int i = 0; i < parentChildren.size(); i++) { + kids = parentChildren.get(i) != null && parentChildren.get(i).getIdentifier() != null && + parentChildren.get(i).getIdentifier().getValue() != null && + parentChildren.get(i).getIdentifier().getValue().equals(parent) ? + parentChildren.get(i).getChildIdentifiers() : + null; + logger.info("Kids are : " + kids); + if (kids != null) { + break; + } + } + } + + if (kids == null) { + kids = new ArrayList<>(); + } + StringType idStringType = new StringType(); + String idString = id; + if (idString.contains("/_")) { + idString = idString.substring(0, idString.indexOf("/_")); + } + idStringType.setValue(idString); + + StringType parentStringType = new StringType(); + parentStringType.setValue(parent); + kids.add(idStringType); + Boolean setParentChildMap = false; + for (int i = 0; i < parentChildren.size(); i++) { + if (parentChildren.get(i) != null && parentChildren.get(i).getIdentifier() != null + && parentChildren.get(i).getIdentifier().getValue() != null && + parentChildren.get(i).getIdentifier().getValue().equals(parent)) { + parentChildren.get(i).setChildIdentifiers(kids); + setParentChildMap = true; + } + } + + if (!setParentChildMap) { + ParentChildrenMap parentChildrenMap = new ParentChildrenMap(); + parentChildrenMap.setIdentifier(parentStringType); + parentChildrenMap.setChildIdentifiers(kids); + parentChildren.add(parentChildrenMap); + } + + } + + public void addNode(String id, String label, Location node, String parentId) { + if (listOfNodes == null) { + listOfNodes = new SingleTreeNode(); + } + + // if node exists we should break since user should write optimized code and also tree can not have duplicates + if (hasNode(id)) { + throw new IllegalArgumentException("Node with ID " + id + " already exists in tree"); + } + + TreeNode n = makeNode(id, label, node, parentId); + + if (parentId != null) { + addToParentChildRelation(parentId, id); + + TreeNode p = getNode(parentId); + + //if parent exists add to it otherwise add as root for now + if (p != null) { + p.addChild(n); + } else { + // if no parent exists add it as root node + String idString = (String) id; + if (idString.contains("/_")) { + idString = idString.substring(0, idString.indexOf("/_")); + } + SingleTreeNode singleTreeNode = new SingleTreeNode(); + StringType treeNodeId = new StringType(); + treeNodeId.setValue(idString); + singleTreeNode.setTreeNodeId(treeNodeId); + singleTreeNode.setTreeNode(n); + listOfNodes = singleTreeNode; + } + } else { + // if no parent add it as root node + String idString = id; + if (idString.contains("/_")) { + idString = idString.substring(0, idString.indexOf("/_")); + } + + SingleTreeNode singleTreeNode = new SingleTreeNode(); + StringType treeNodeId = new StringType(); + treeNodeId.setValue(idString); + singleTreeNode.setTreeNodeId(treeNodeId); + singleTreeNode.setTreeNode(n); + listOfNodes = singleTreeNode; + } + } + + private TreeNode makeNode(String id, String label, Location node, String parentId) { + TreeNode n = getNode(id); + if (n == null) { + n = new TreeNode(); + StringType nodeId = new StringType(); + String idString = (String) id; + if (idString.contains("/_")) { + idString = idString.substring(0, idString.indexOf("/_")); + } + nodeId.setValue((String) idString); + n.setNodeId(nodeId); + StringType labelString = new StringType(); + labelString.setValue(label); + n.setLabel(labelString); + n.setNode(node); + StringType parentIdString = new StringType(); + String parentIdStringVar = parentId; + + if (parentIdStringVar != null && parentIdStringVar.contains("/_")) { + parentIdStringVar = parentIdStringVar.substring(0, parentIdStringVar.indexOf("/_")); + } + parentIdString.setValue(parentIdStringVar); + n.setParent(parentIdString); + } + return n; + } + + public TreeNode getNode(String id) { + // Check if id is any root node + String idString = id; + if (idString != null && idString.contains("/_")) { + idString = idString.substring(0, idString.indexOf("/_")); + } + + if (listOfNodes.getTreeNodeId() != null && listOfNodes.getTreeNodeId().getValue().equals(idString)) { + return listOfNodes.getTreeNode(); + + } else { + if (listOfNodes != null && listOfNodes.getTreeNode() != null + && listOfNodes.getTreeNode().getChildren() != null) { + return recursivelyFindNode(idString, listOfNodes.getTreeNode().getChildren()); + } + } + return null; + } + + public boolean hasNode(String id) { + return getNode(id) != null; + } + + public SingleTreeNode getListOfNodes() { + return listOfNodes; + } + + public void setListOfNodes(SingleTreeNode listOfNodes) { + this.listOfNodes = listOfNodes; + } + + public List getParentChildren() { + return parentChildren; + } + + public void setParentChildren(List parentChildren) { + this.parentChildren = parentChildren; + } + + @Override + public Type copy() { + Tree tree = new Tree(); + copyValues(tree); + return tree; + } + + @Override + public boolean isEmpty() { + return ElementUtil.isEmpty(listOfNodes); + } + + @Override + protected Type typedCopy() { + return copy(); + } + + private TreeNode recursivelyFindNode(String idString, List childTreeNodeList) { + for (ChildTreeNode childTreeNode : childTreeNodeList) { + TreeNode treeNode = childTreeNode.getChildren(); + if (treeNode != null && treeNode.getNodeId() != null && treeNode.getNodeId().getValue().equals(idString)) { + return treeNode; + } else { + if (treeNode != null && treeNode.getChildren() != null && treeNode.getChildren().size() > 0) { + return recursivelyFindNode(idString, treeNode.getChildren()); + } + } + } + return null; + } } diff --git a/src/main/java/org/smartregister/extension/model/TreeNode.java b/src/main/java/org/smartregister/extension/model/TreeNode.java index 2a38906..5f255c8 100755 --- a/src/main/java/org/smartregister/extension/model/TreeNode.java +++ b/src/main/java/org/smartregister/extension/model/TreeNode.java @@ -10,163 +10,161 @@ import java.util.LinkedHashMap; import java.util.List; - @DatatypeDef(name = "TreeNode") public class TreeNode extends Type implements ICompositeType { - @Child(name = "name", type = {StringType.class}, order = 0, min = 1, max = 1, modifier = false, summary = false) - protected StringType name; - - @Child(name = "nodeId", type = {StringType.class}, order = 2) - private StringType nodeId; - - @Child(name = "label", type = {StringType.class}, order = 3) - private StringType label; - - @Child(name = "node", type = {Location.class}, order = 4) - private Location node; - - @Child(name = "parent", type = {StringType.class}, order = 5) - private StringType parent; - - @Child(name = "children", type = {ChildTreeNode.class}, - order = 6, - min = 0, - max = -1, - modifier = false, - summary = false) - private List children; - - public TreeNode() { - children = new ArrayList<>(); - } - - public TreeNode(StringType name, StringType nodeId, StringType label, Location node, StringType parent) { - this.name = name; - this.nodeId = nodeId; - this.label = label; - this.node = node; - this.parent = parent; - } - - public StringType getName() { - if (name == null) { - name = new StringType(); - } - return name; - } - - public TreeNode setName(StringType name) { - this.name = name; - return this; - } - - - public StringType getLabel() { - return label; - } - - public TreeNode setLabel(StringType label) { - this.label = label; - return this; - } - - @Override - public Type copy() { - TreeNode treeNode = new TreeNode(); - copyValues(treeNode); - return treeNode; - } - - @Override - public boolean isEmpty() { - return ElementUtil.isEmpty(node); - } - - @Override - protected Type typedCopy() { - return copy(); - } - - public StringType getNodeId() { - return nodeId; - } - - public TreeNode setNodeId(StringType nodeId) { - this.nodeId = nodeId; - return this; - } - - public Location getNode() { - return node; - } - - public TreeNode setNode(Location node) { - this.node = node; - return this; - } - - public StringType getParent() { - return parent; - } - - public TreeNode setParent(StringType parent) { - this.parent = parent; - return this; - } - - public List getChildren() { - if (children == null) { - children = new ArrayList<>(); - } - return children; - } - - public TreeNode setChildren(List children) { - this.children = children; - return this; - } - - public void addChild(TreeNode node) { - if (children == null) { - children = new ArrayList<>(); - } - ChildTreeNode childTreeNode = new ChildTreeNode(); - childTreeNode.setChildId(node.getNodeId()); - List treeNodeList = new ArrayList<>(); - TreeNode treeNode = new TreeNode(); - treeNode.setNode(node.getNode()); - treeNode.setNodeId(node.getNodeId()); - treeNode.setLabel(node.getLabel()); - treeNode.setParent(node.getParent()); - treeNodeList.add(treeNode); - childTreeNode.setChildren(treeNode); - children.add(childTreeNode); - } - - - public TreeNode findChild(String id) { - String idString = (String) id; - if (idString.contains("/_")) { - idString = idString.substring(0, idString.indexOf("/_")); - } - if (children != null && children.size() > 0) { - for (int i = 0; i < children.size(); i++) { - if (children.get(i) != null) { - for (ChildTreeNode child : children) { - if (child != null && child.getChildren() != null - && child.getChildren().getNodeId() != null && child.getChildren().getNodeId().getValue().equals(idString)) { - return child.getChildren(); - } else if (child != null && child != null) { - TreeNode node = child.getChildren().findChild(idString); - if (node != null) - return node; - } - } - } - } - } - return null; - } + @Child(name = "name", type = { StringType.class }, order = 0, min = 1, max = 1, modifier = false, summary = false) + protected StringType name; + + @Child(name = "nodeId", type = { StringType.class }, order = 2) + private StringType nodeId; + + @Child(name = "label", type = { StringType.class }, order = 3) + private StringType label; + + @Child(name = "node", type = { Location.class }, order = 4) + private Location node; + + @Child(name = "parent", type = { StringType.class }, order = 5) + private StringType parent; + + @Child(name = "children", type = { ChildTreeNode.class }, + order = 6, + min = 0, + max = -1, + modifier = false, + summary = false) + private List children; + + public TreeNode() { + children = new ArrayList<>(); + } + + public TreeNode(StringType name, StringType nodeId, StringType label, Location node, StringType parent) { + this.name = name; + this.nodeId = nodeId; + this.label = label; + this.node = node; + this.parent = parent; + } + + public StringType getName() { + if (name == null) { + name = new StringType(); + } + return name; + } + + public TreeNode setName(StringType name) { + this.name = name; + return this; + } + + public StringType getLabel() { + return label; + } + + public TreeNode setLabel(StringType label) { + this.label = label; + return this; + } + + @Override + public Type copy() { + TreeNode treeNode = new TreeNode(); + copyValues(treeNode); + return treeNode; + } + + @Override + public boolean isEmpty() { + return ElementUtil.isEmpty(node); + } + + @Override + protected Type typedCopy() { + return copy(); + } + + public StringType getNodeId() { + return nodeId; + } + + public TreeNode setNodeId(StringType nodeId) { + this.nodeId = nodeId; + return this; + } + + public Location getNode() { + return node; + } + + public TreeNode setNode(Location node) { + this.node = node; + return this; + } + + public StringType getParent() { + return parent; + } + + public TreeNode setParent(StringType parent) { + this.parent = parent; + return this; + } + + public List getChildren() { + if (children == null) { + children = new ArrayList<>(); + } + return children; + } + + public TreeNode setChildren(List children) { + this.children = children; + return this; + } + + public void addChild(TreeNode node) { + if (children == null) { + children = new ArrayList<>(); + } + ChildTreeNode childTreeNode = new ChildTreeNode(); + childTreeNode.setChildId(node.getNodeId()); + List treeNodeList = new ArrayList<>(); + TreeNode treeNode = new TreeNode(); + treeNode.setNode(node.getNode()); + treeNode.setNodeId(node.getNodeId()); + treeNode.setLabel(node.getLabel()); + treeNode.setParent(node.getParent()); + treeNodeList.add(treeNode); + childTreeNode.setChildren(treeNode); + children.add(childTreeNode); + } + + public TreeNode findChild(String id) { + String idString = (String) id; + if (idString.contains("/_")) { + idString = idString.substring(0, idString.indexOf("/_")); + } + if (children != null && children.size() > 0) { + for (int i = 0; i < children.size(); i++) { + if (children.get(i) != null) { + for (ChildTreeNode child : children) { + if (child != null && child.getChildren() != null + && child.getChildren().getNodeId() != null && child.getChildren().getNodeId().getValue() + .equals(idString)) { + return child.getChildren(); + } else if (child != null && child != null) { + TreeNode node = child.getChildren().findChild(idString); + if (node != null) + return node; + } + } + } + } + } + return null; + } } diff --git a/src/main/java/org/smartregister/extension/rest/LocationHierarchyResourceProvider.java b/src/main/java/org/smartregister/extension/rest/LocationHierarchyResourceProvider.java index edeb932..83cf34d 100755 --- a/src/main/java/org/smartregister/extension/rest/LocationHierarchyResourceProvider.java +++ b/src/main/java/org/smartregister/extension/rest/LocationHierarchyResourceProvider.java @@ -14,83 +14,81 @@ import org.hl7.fhir.r4.model.*; import org.springframework.beans.factory.annotation.Autowired; -import javax.servlet.http.HttpServletRequest; +//import javax.servlet.http.HttpServletRequest; import java.util.*; public class LocationHierarchyResourceProvider implements IResourceProvider { - @Autowired - IFhirResourceDao locationIFhirResourceDao; - - private static Logger logger = LogManager.getLogger(LocationHierarchyResourceProvider.class.toString()); - - @Override - public Class getResourceType() { - return LocationHierarchy.class; - } - - @Search - public LocationHierarchy getLocationHierarchy(HttpServletRequest request, - @OptionalParam(name = "identifier") TokenParam identifier) { - - SearchParameterMap paramMap = new SearchParameterMap(); - paramMap.add("identifier", identifier); - - IBundleProvider locationBundle = locationIFhirResourceDao.search(paramMap); - List locations = locationBundle != null ? - locationBundle.getResources(0, locationBundle.size()) : new ArrayList<>(); - Long id = null; - if (locations.size() > 0) { - id = locations.get(0).getIdElement().getIdPartAsLong(); - } - - LocationHierarchyTree locationHierarchyTree = new LocationHierarchyTree(); - LocationHierarchy locationHierarchy = new LocationHierarchy(); - if (id != null && locations.size() > 0) { - logger.info("Building Location Hierarchy of Location Id : " + id); - locationHierarchyTree.buildTreeFromList(getLocationHierarchy(id, locations.get(0))); - StringType locationIdString = new StringType().setId(id.toString()).getIdElement(); - locationHierarchy.setLocationId(locationIdString); - locationHierarchy.setId("Location Resource:" + id); - - locationHierarchy.setLocationHierarchyTree(locationHierarchyTree); - } - else { - locationHierarchy.setId("Location Resource : Not Found"); - } - return locationHierarchy; - - } - - private List getLocationHierarchy(Long id, IBaseResource parentLocation) { - return descendants(id, parentLocation); - } - - public List descendants(Long id, IBaseResource parentLocation) { - - SearchParameterMap paramMap = new SearchParameterMap(); - ReferenceAndListParam thePartOf = new ReferenceAndListParam(); - ReferenceParam partOf = new ReferenceParam(); - partOf.setValue("Location/" + id); - ReferenceOrListParam referenceOrListParam = new ReferenceOrListParam(); - referenceOrListParam.add(partOf); - thePartOf.addValue(referenceOrListParam); - paramMap.add("partof", thePartOf); - - IBundleProvider childLocationBundle = locationIFhirResourceDao.search(paramMap); - List allLocations = new ArrayList<>(); - if (parentLocation != null) { - allLocations.add((Location) parentLocation); - } - for (IBaseResource childLocation : childLocationBundle.getResources(0, childLocationBundle.size())) { - Location childLocationEntity = (Location) childLocation; - allLocations.add(childLocationEntity); - allLocations.addAll(descendants(childLocation.getIdElement().getIdPartAsLong(), null)); - } - return allLocations; - } - - public void setLocationIFhirResourceDao(IFhirResourceDao locationIFhirResourceDao) { - this.locationIFhirResourceDao = locationIFhirResourceDao; - } + @Autowired + IFhirResourceDao locationIFhirResourceDao; + + private static Logger logger = LogManager.getLogger(LocationHierarchyResourceProvider.class.toString()); + + @Override + public Class getResourceType() { + return LocationHierarchy.class; + } + + @Search + public LocationHierarchy getLocationHierarchy(@RequiredParam(name = "identifier") TokenParam identifier) { + + SearchParameterMap paramMap = new SearchParameterMap(); + paramMap.add("identifier", identifier); + + IBundleProvider locationBundle = locationIFhirResourceDao.search(paramMap); + List locations = locationBundle != null ? + locationBundle.getResources(0, locationBundle.size()) : new ArrayList<>(); + Long id = null; + if (locations.size() > 0) { + id = locations.get(0) != null && locations.get(0).getIdElement() != null ? locations.get(0).getIdElement().getIdPartAsLong() : null; + } + + LocationHierarchyTree locationHierarchyTree = new LocationHierarchyTree(); + LocationHierarchy locationHierarchy = new LocationHierarchy(); + if (id != null && locations.size() > 0) { + logger.info("Building Location Hierarchy of Location Id : " + id); + locationHierarchyTree.buildTreeFromList(getLocationHierarchy(id, locations.get(0))); + StringType locationIdString = new StringType().setId(id.toString()).getIdElement(); + locationHierarchy.setLocationId(locationIdString); + locationHierarchy.setId("Location Resource:" + id); + + locationHierarchy.setLocationHierarchyTree(locationHierarchyTree); + } else { + locationHierarchy.setId("Location Resource : Not Found"); + } + return locationHierarchy; + + } + + private List getLocationHierarchy(Long id, IBaseResource parentLocation) { + return descendants(id, parentLocation); + } + + public List descendants(Long id, IBaseResource parentLocation) { + + SearchParameterMap paramMap = new SearchParameterMap(); + ReferenceAndListParam thePartOf = new ReferenceAndListParam(); + ReferenceParam partOf = new ReferenceParam(); + partOf.setValue("Location/" + id); + ReferenceOrListParam referenceOrListParam = new ReferenceOrListParam(); + referenceOrListParam.add(partOf); + thePartOf.addValue(referenceOrListParam); + paramMap.add("partof", thePartOf); + + IBundleProvider childLocationBundle = locationIFhirResourceDao.search(paramMap); + List allLocations = new ArrayList<>(); + if (parentLocation != null) { + allLocations.add((Location) parentLocation); + } + for (IBaseResource childLocation : childLocationBundle.getResources(0, childLocationBundle.size())) { + Location childLocationEntity = (Location) childLocation; + allLocations.add(childLocationEntity); + allLocations.addAll(descendants(childLocation.getIdElement().getIdPartAsLong(), null)); + } + return allLocations; + } + + public void setLocationIFhirResourceDao(IFhirResourceDao locationIFhirResourceDao) { + this.locationIFhirResourceDao = locationIFhirResourceDao; + } } From 31629103fcd8fd5678f82b6d5d41b94a163504c8 Mon Sep 17 00:00:00 2001 From: Reham Muzzamil Date: Fri, 27 Aug 2021 13:38:26 +0500 Subject: [PATCH 8/9] Code cleanup --- pom.xml | 15 ++++++++++- .../extension/model/LocationHierarchy.java | 6 ++--- .../smartregister/extension/model/Tree.java | 27 ++++++++++--------- .../extension/model/TreeNode.java | 6 +++-- .../LocationHierarchyResourceProvider.java | 15 ++++++----- .../extension/utils/Constants.java | 12 +++++++++ 6 files changed, 55 insertions(+), 26 deletions(-) create mode 100644 src/main/java/org/smartregister/extension/utils/Constants.java diff --git a/pom.xml b/pom.xml index 0b0a61c..d1bce25 100755 --- a/pom.xml +++ b/pom.xml @@ -18,6 +18,19 @@ 5.2.4.RELEASE + + + nexus-snapshots + Nexus Snapshots Repository + https://oss.sonatype.org/content/repositories/snapshots + false + + + nexus-releases + https://oss.sonatype.org/service/local/staging/deploy/maven2 + + + org.springframework @@ -73,4 +86,4 @@ - \ No newline at end of file + diff --git a/src/main/java/org/smartregister/extension/model/LocationHierarchy.java b/src/main/java/org/smartregister/extension/model/LocationHierarchy.java index 84488c6..e4a5c59 100755 --- a/src/main/java/org/smartregister/extension/model/LocationHierarchy.java +++ b/src/main/java/org/smartregister/extension/model/LocationHierarchy.java @@ -35,15 +35,15 @@ public class LocationHierarchy extends Location { @Override public Location copy() { - Location dst = new Location(); + Location location = new Location(); Bundle bundle = new Bundle(); List theEntry = new ArrayList<>(); Bundle.BundleEntryComponent entryComponent = new Bundle.BundleEntryComponent(); entryComponent.setResource(new Bundle()); theEntry.add(entryComponent); bundle.setEntry(theEntry); - this.copyValues(dst); - return dst; + this.copyValues(location); + return location; } @Override diff --git a/src/main/java/org/smartregister/extension/model/Tree.java b/src/main/java/org/smartregister/extension/model/Tree.java index e1e88b4..4e4f0bd 100755 --- a/src/main/java/org/smartregister/extension/model/Tree.java +++ b/src/main/java/org/smartregister/extension/model/Tree.java @@ -9,10 +9,11 @@ import org.hl7.fhir.r4.model.Location; import org.hl7.fhir.r4.model.StringType; import org.hl7.fhir.r4.model.Type; -import org.smartregister.extension.rest.LocationHierarchyResourceProvider; import java.util.*; +import static org.smartregister.extension.utils.Constants.SLASH_UNDERSCORE; + @DatatypeDef(name = "Tree") public class Tree extends Type implements ICompositeType { @@ -63,8 +64,8 @@ private void addToParentChildRelation(String parent, String id) { } StringType idStringType = new StringType(); String idString = id; - if (idString.contains("/_")) { - idString = idString.substring(0, idString.indexOf("/_")); + if (idString.contains(SLASH_UNDERSCORE)) { + idString = idString.substring(0, idString.indexOf(SLASH_UNDERSCORE)); } idStringType.setValue(idString); @@ -113,8 +114,8 @@ public void addNode(String id, String label, Location node, String parentId) { } else { // if no parent exists add it as root node String idString = (String) id; - if (idString.contains("/_")) { - idString = idString.substring(0, idString.indexOf("/_")); + if (idString.contains(SLASH_UNDERSCORE)) { + idString = idString.substring(0, idString.indexOf(SLASH_UNDERSCORE)); } SingleTreeNode singleTreeNode = new SingleTreeNode(); StringType treeNodeId = new StringType(); @@ -126,8 +127,8 @@ public void addNode(String id, String label, Location node, String parentId) { } else { // if no parent add it as root node String idString = id; - if (idString.contains("/_")) { - idString = idString.substring(0, idString.indexOf("/_")); + if (idString.contains(SLASH_UNDERSCORE)) { + idString = idString.substring(0, idString.indexOf(SLASH_UNDERSCORE)); } SingleTreeNode singleTreeNode = new SingleTreeNode(); @@ -145,8 +146,8 @@ private TreeNode makeNode(String id, String label, Location node, String parentI n = new TreeNode(); StringType nodeId = new StringType(); String idString = (String) id; - if (idString.contains("/_")) { - idString = idString.substring(0, idString.indexOf("/_")); + if (idString.contains(SLASH_UNDERSCORE)) { + idString = idString.substring(0, idString.indexOf(SLASH_UNDERSCORE)); } nodeId.setValue((String) idString); n.setNodeId(nodeId); @@ -157,8 +158,8 @@ private TreeNode makeNode(String id, String label, Location node, String parentI StringType parentIdString = new StringType(); String parentIdStringVar = parentId; - if (parentIdStringVar != null && parentIdStringVar.contains("/_")) { - parentIdStringVar = parentIdStringVar.substring(0, parentIdStringVar.indexOf("/_")); + if (parentIdStringVar != null && parentIdStringVar.contains(SLASH_UNDERSCORE)) { + parentIdStringVar = parentIdStringVar.substring(0, parentIdStringVar.indexOf(SLASH_UNDERSCORE)); } parentIdString.setValue(parentIdStringVar); n.setParent(parentIdString); @@ -169,8 +170,8 @@ private TreeNode makeNode(String id, String label, Location node, String parentI public TreeNode getNode(String id) { // Check if id is any root node String idString = id; - if (idString != null && idString.contains("/_")) { - idString = idString.substring(0, idString.indexOf("/_")); + if (idString != null && idString.contains(SLASH_UNDERSCORE)) { + idString = idString.substring(0, idString.indexOf(SLASH_UNDERSCORE)); } if (listOfNodes.getTreeNodeId() != null && listOfNodes.getTreeNodeId().getValue().equals(idString)) { diff --git a/src/main/java/org/smartregister/extension/model/TreeNode.java b/src/main/java/org/smartregister/extension/model/TreeNode.java index 5f255c8..1eb21b3 100755 --- a/src/main/java/org/smartregister/extension/model/TreeNode.java +++ b/src/main/java/org/smartregister/extension/model/TreeNode.java @@ -10,6 +10,8 @@ import java.util.LinkedHashMap; import java.util.List; +import static org.smartregister.extension.utils.Constants.SLASH_UNDERSCORE; + @DatatypeDef(name = "TreeNode") public class TreeNode extends Type implements ICompositeType { @@ -144,8 +146,8 @@ public void addChild(TreeNode node) { public TreeNode findChild(String id) { String idString = (String) id; - if (idString.contains("/_")) { - idString = idString.substring(0, idString.indexOf("/_")); + if (idString.contains(SLASH_UNDERSCORE)) { + idString = idString.substring(0, idString.indexOf(SLASH_UNDERSCORE)); } if (children != null && children.size() > 0) { for (int i = 0; i < children.size(); i++) { diff --git a/src/main/java/org/smartregister/extension/rest/LocationHierarchyResourceProvider.java b/src/main/java/org/smartregister/extension/rest/LocationHierarchyResourceProvider.java index 83cf34d..4a9ed37 100755 --- a/src/main/java/org/smartregister/extension/rest/LocationHierarchyResourceProvider.java +++ b/src/main/java/org/smartregister/extension/rest/LocationHierarchyResourceProvider.java @@ -14,9 +14,10 @@ import org.hl7.fhir.r4.model.*; import org.springframework.beans.factory.annotation.Autowired; -//import javax.servlet.http.HttpServletRequest; import java.util.*; +import static org.smartregister.extension.utils.Constants.*; + public class LocationHierarchyResourceProvider implements IResourceProvider { @Autowired @@ -30,10 +31,10 @@ public Class getResourceType() { } @Search - public LocationHierarchy getLocationHierarchy(@RequiredParam(name = "identifier") TokenParam identifier) { + public LocationHierarchy getLocationHierarchy(@RequiredParam(name = IDENTIFIER) TokenParam identifier) { SearchParameterMap paramMap = new SearchParameterMap(); - paramMap.add("identifier", identifier); + paramMap.add(IDENTIFIER, identifier); IBundleProvider locationBundle = locationIFhirResourceDao.search(paramMap); List locations = locationBundle != null ? @@ -50,11 +51,11 @@ public LocationHierarchy getLocationHierarchy(@RequiredParam(name = "identifier" locationHierarchyTree.buildTreeFromList(getLocationHierarchy(id, locations.get(0))); StringType locationIdString = new StringType().setId(id.toString()).getIdElement(); locationHierarchy.setLocationId(locationIdString); - locationHierarchy.setId("Location Resource:" + id); + locationHierarchy.setId(LOCATION_RESOURCE + id); locationHierarchy.setLocationHierarchyTree(locationHierarchyTree); } else { - locationHierarchy.setId("Location Resource : Not Found"); + locationHierarchy.setId(LOCATION_RESOURCE_NOT_FOUND); } return locationHierarchy; @@ -69,11 +70,11 @@ public List descendants(Long id, IBaseResource parentLocation) { SearchParameterMap paramMap = new SearchParameterMap(); ReferenceAndListParam thePartOf = new ReferenceAndListParam(); ReferenceParam partOf = new ReferenceParam(); - partOf.setValue("Location/" + id); + partOf.setValue(LOCATION + FORWARD_SLASH + id); ReferenceOrListParam referenceOrListParam = new ReferenceOrListParam(); referenceOrListParam.add(partOf); thePartOf.addValue(referenceOrListParam); - paramMap.add("partof", thePartOf); + paramMap.add(PART_OF, thePartOf); IBundleProvider childLocationBundle = locationIFhirResourceDao.search(paramMap); List allLocations = new ArrayList<>(); diff --git a/src/main/java/org/smartregister/extension/utils/Constants.java b/src/main/java/org/smartregister/extension/utils/Constants.java new file mode 100644 index 0000000..eaf35a6 --- /dev/null +++ b/src/main/java/org/smartregister/extension/utils/Constants.java @@ -0,0 +1,12 @@ +package org.smartregister.extension.utils; + +public interface Constants { + + String SLASH_UNDERSCORE = "/_"; + String LOCATION = "Location"; + String FORWARD_SLASH = "/"; + String IDENTIFIER = "identifier"; + String LOCATION_RESOURCE_NOT_FOUND = "Location Resource : Not Found"; + String LOCATION_RESOURCE = "Location Resource : "; + String PART_OF = "partof"; +} From 4337832006b9a83d9386ad3d7873382e2be24bb4 Mon Sep 17 00:00:00 2001 From: Reham Muzzamil Date: Fri, 27 Aug 2021 13:56:37 +0500 Subject: [PATCH 9/9] Code cleanup --- .../smartregister/extension/model/Tree.java | 39 ++++++++++--------- .../extension/model/TreeNode.java | 5 ++- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/main/java/org/smartregister/extension/model/Tree.java b/src/main/java/org/smartregister/extension/model/Tree.java index 4e4f0bd..9dde450 100755 --- a/src/main/java/org/smartregister/extension/model/Tree.java +++ b/src/main/java/org/smartregister/extension/model/Tree.java @@ -3,6 +3,7 @@ import ca.uhn.fhir.model.api.annotation.Child; import ca.uhn.fhir.model.api.annotation.DatatypeDef; import ca.uhn.fhir.util.ElementUtil; +import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.hl7.fhir.instance.model.api.ICompositeType; @@ -48,7 +49,7 @@ private void addToParentChildRelation(String parent, String id) { if (parentChildren != null) { for (int i = 0; i < parentChildren.size(); i++) { kids = parentChildren.get(i) != null && parentChildren.get(i).getIdentifier() != null && - parentChildren.get(i).getIdentifier().getValue() != null && + StringUtils.isNotBlank(parentChildren.get(i).getIdentifier().getValue()) && parentChildren.get(i).getIdentifier().getValue().equals(parent) ? parentChildren.get(i).getChildIdentifiers() : null; @@ -75,7 +76,7 @@ private void addToParentChildRelation(String parent, String id) { Boolean setParentChildMap = false; for (int i = 0; i < parentChildren.size(); i++) { if (parentChildren.get(i) != null && parentChildren.get(i).getIdentifier() != null - && parentChildren.get(i).getIdentifier().getValue() != null && + && StringUtils.isNotBlank(parentChildren.get(i).getIdentifier().getValue()) && parentChildren.get(i).getIdentifier().getValue().equals(parent)) { parentChildren.get(i).setChildIdentifiers(kids); setParentChildMap = true; @@ -101,16 +102,16 @@ public void addNode(String id, String label, Location node, String parentId) { throw new IllegalArgumentException("Node with ID " + id + " already exists in tree"); } - TreeNode n = makeNode(id, label, node, parentId); + TreeNode treeNode = makeNode(id, label, node, parentId); if (parentId != null) { addToParentChildRelation(parentId, id); - TreeNode p = getNode(parentId); + TreeNode parentNode = getNode(parentId); //if parent exists add to it otherwise add as root for now - if (p != null) { - p.addChild(n); + if (parentNode != null) { + parentNode.addChild(treeNode); } else { // if no parent exists add it as root node String idString = (String) id; @@ -121,7 +122,7 @@ public void addNode(String id, String label, Location node, String parentId) { StringType treeNodeId = new StringType(); treeNodeId.setValue(idString); singleTreeNode.setTreeNodeId(treeNodeId); - singleTreeNode.setTreeNode(n); + singleTreeNode.setTreeNode(treeNode); listOfNodes = singleTreeNode; } } else { @@ -135,26 +136,26 @@ public void addNode(String id, String label, Location node, String parentId) { StringType treeNodeId = new StringType(); treeNodeId.setValue(idString); singleTreeNode.setTreeNodeId(treeNodeId); - singleTreeNode.setTreeNode(n); + singleTreeNode.setTreeNode(treeNode); listOfNodes = singleTreeNode; } } private TreeNode makeNode(String id, String label, Location node, String parentId) { - TreeNode n = getNode(id); - if (n == null) { - n = new TreeNode(); + TreeNode treenode = getNode(id); + if (treenode == null) { + treenode = new TreeNode(); StringType nodeId = new StringType(); String idString = (String) id; if (idString.contains(SLASH_UNDERSCORE)) { idString = idString.substring(0, idString.indexOf(SLASH_UNDERSCORE)); } nodeId.setValue((String) idString); - n.setNodeId(nodeId); + treenode.setNodeId(nodeId); StringType labelString = new StringType(); labelString.setValue(label); - n.setLabel(labelString); - n.setNode(node); + treenode.setLabel(labelString); + treenode.setNode(node); StringType parentIdString = new StringType(); String parentIdStringVar = parentId; @@ -162,9 +163,9 @@ private TreeNode makeNode(String id, String label, Location node, String parentI parentIdStringVar = parentIdStringVar.substring(0, parentIdStringVar.indexOf(SLASH_UNDERSCORE)); } parentIdString.setValue(parentIdStringVar); - n.setParent(parentIdString); + treenode.setParent(parentIdString); } - return n; + return treenode; } public TreeNode getNode(String id) { @@ -174,7 +175,8 @@ public TreeNode getNode(String id) { idString = idString.substring(0, idString.indexOf(SLASH_UNDERSCORE)); } - if (listOfNodes.getTreeNodeId() != null && listOfNodes.getTreeNodeId().getValue().equals(idString)) { + if (listOfNodes.getTreeNodeId() != null && StringUtils.isNotBlank(listOfNodes.getTreeNodeId().getValue()) && + listOfNodes.getTreeNodeId().getValue().equals(idString)) { return listOfNodes.getTreeNode(); } else { @@ -226,7 +228,8 @@ protected Type typedCopy() { private TreeNode recursivelyFindNode(String idString, List childTreeNodeList) { for (ChildTreeNode childTreeNode : childTreeNodeList) { TreeNode treeNode = childTreeNode.getChildren(); - if (treeNode != null && treeNode.getNodeId() != null && treeNode.getNodeId().getValue().equals(idString)) { + if (treeNode != null && treeNode.getNodeId() != null && StringUtils.isNotBlank(treeNode.getNodeId().getValue()) && + treeNode.getNodeId().getValue().equals(idString)) { return treeNode; } else { if (treeNode != null && treeNode.getChildren() != null && treeNode.getChildren().size() > 0) { diff --git a/src/main/java/org/smartregister/extension/model/TreeNode.java b/src/main/java/org/smartregister/extension/model/TreeNode.java index 1eb21b3..95fc4c3 100755 --- a/src/main/java/org/smartregister/extension/model/TreeNode.java +++ b/src/main/java/org/smartregister/extension/model/TreeNode.java @@ -3,6 +3,7 @@ import ca.uhn.fhir.model.api.annotation.Child; import ca.uhn.fhir.model.api.annotation.DatatypeDef; import ca.uhn.fhir.util.ElementUtil; +import org.apache.commons.lang3.StringUtils; import org.hl7.fhir.instance.model.api.ICompositeType; import org.hl7.fhir.r4.model.*; @@ -154,7 +155,9 @@ public TreeNode findChild(String id) { if (children.get(i) != null) { for (ChildTreeNode child : children) { if (child != null && child.getChildren() != null - && child.getChildren().getNodeId() != null && child.getChildren().getNodeId().getValue() + && child.getChildren().getNodeId() != null && StringUtils.isNotBlank( + child.getChildren().getNodeId().getValue()) + && child.getChildren().getNodeId().getValue() .equals(idString)) { return child.getChildren(); } else if (child != null && child != null) {