Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to Optimize processing of the Location Hierarchy #14

Merged
merged 5 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.smartregister</groupId>
<artifactId>fhir-common-utils</artifactId>
<version>0.0.10-SNAPSHOT</version>
<version>0.0.11-SNAPSHOT</version>

<distributionManagement>
<repository>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ public void addLocation(Location l) {
* @param locations
*/
public void buildTreeFromList(List<Location> locations) {
for (Location location : locations) {
addLocation(location);
}
locations.parallelStream().forEach(location -> addLocation(location));
}

public Tree getLocationsHierarchy() {
Expand Down
63 changes: 31 additions & 32 deletions src/main/java/org/smartregister/model/location/Tree.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Logger;

import static org.smartregister.utils.Constants.SLASH_UNDERSCORE;
Expand Down Expand Up @@ -98,18 +99,20 @@ private void addToParentChildRelation(String parent, String 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
&& StringUtils.isNotBlank(parentChildren.get(i).getIdentifier().getValue())
&& parentChildren.get(i).getIdentifier().getValue().equals(parent)) {
parentChildren.get(i).setChildIdentifiers(kids);
setParentChildMap = true;
}
}
AtomicReference<Boolean> setParentChildMap = new AtomicReference<>(false);

List<StringType> finalKids = kids;
parentChildren.parallelStream().filter(parentChildrenMap -> parentChildrenMap != null
&& parentChildrenMap.getIdentifier() != null
&& StringUtils.isNotBlank(parentChildrenMap.getIdentifier().getValue())
&& parentChildrenMap.getIdentifier().getValue().equals(parent)).forEach(parentChildrenMap -> {

parentChildrenMap.setChildIdentifiers(finalKids);
setParentChildMap.set(true);

});

if (!setParentChildMap) {
if (!setParentChildMap.get()) {
ParentChildrenMap parentChildrenMap = new ParentChildrenMap();
parentChildrenMap.setIdentifier(parentStringType);
parentChildrenMap.setChildIdentifiers(kids);
Expand Down Expand Up @@ -140,43 +143,39 @@ public void addNode(String id, String label, Location node, String parentId) {
parentNode.addChild(treeNode);
} else {
// if no parent exists add it as root node
String idString = (String) id;
if (idString.contains(SLASH_UNDERSCORE)) {
idString = idString.substring(0, idString.indexOf(SLASH_UNDERSCORE));
}
SingleTreeNode singleTreeNode = new SingleTreeNode();
StringType treeNodeId = new StringType();
treeNodeId.setValue(idString);
singleTreeNode.setTreeNodeId(treeNodeId);
singleTreeNode.setTreeNode(treeNode);
SingleTreeNode singleTreeNode = getSingleTreeNode(id, treeNode);
listOfNodes = singleTreeNode;
}
} else {
// if no parent add it as root node
String idString = id;
if (idString.contains(SLASH_UNDERSCORE)) {
idString = idString.substring(0, idString.indexOf(SLASH_UNDERSCORE));
}

SingleTreeNode singleTreeNode = new SingleTreeNode();
StringType treeNodeId = new StringType();
treeNodeId.setValue(idString);
singleTreeNode.setTreeNodeId(treeNodeId);
singleTreeNode.setTreeNode(treeNode);
SingleTreeNode singleTreeNode = getSingleTreeNode(id, treeNode);
listOfNodes = singleTreeNode;
}
}

private static SingleTreeNode getSingleTreeNode(String id, TreeNode treeNode) {
String idString = id;
if (idString.contains(SLASH_UNDERSCORE)) {
idString = idString.substring(0, idString.indexOf(SLASH_UNDERSCORE));
}
SingleTreeNode singleTreeNode = new SingleTreeNode();
StringType treeNodeId = new StringType();
treeNodeId.setValue(idString);
singleTreeNode.setTreeNodeId(treeNodeId);
singleTreeNode.setTreeNode(treeNode);
return singleTreeNode;
}

private TreeNode makeNode(String id, String label, Location node, String parentId) {
TreeNode treenode = getNode(id);
if (treenode == null) {
treenode = new TreeNode();
StringType nodeId = new StringType();
String idString = (String) id;
String idString = id;
if (idString.contains(SLASH_UNDERSCORE)) {
idString = idString.substring(0, idString.indexOf(SLASH_UNDERSCORE));
}
nodeId.setValue((String) idString);
nodeId.setValue(idString);
treenode.setNodeId(nodeId);
StringType labelString = new StringType();
labelString.setValue(label);
Expand Down
Loading