Skip to content

Commit

Permalink
Merge pull request #1 from opensrp/location-hierarchy-api
Browse files Browse the repository at this point in the history
Create a Location Hierarchy API
  • Loading branch information
rehammuzzamil authored Aug 31, 2021
2 parents e64afd4 + 4337832 commit 226a086
Show file tree
Hide file tree
Showing 15 changed files with 1,183 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Empty file modified README.md
100644 → 100755
Empty file.
89 changes: 89 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.smartregister</groupId>
<artifactId>hapi-fhir-opensrp-extensions</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>hapi-fhir-opensrp-extensions</name>
<description>This repository holds all the code extensions on top of Hapi-FHIR</description>
<url>https://github.com/opensrp/hapi-fhir-opensrp-extensions</url>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<spring.version>5.2.4.RELEASE</spring.version>
</properties>

<distributionManagement>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Nexus Snapshots Repository</name>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<uniqueVersion>false</uniqueVersion>
</snapshotRepository>
<repository>
<id>nexus-releases</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url>
</repository>
</distributionManagement>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>

<!-- This dependency includes the core HAPI-FHIR classes -->
<dependency>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir-base</artifactId>
<version>5.4.0</version>
</dependency>

<!-- This dependency includes the JPA server itself, which is packaged separately from the org.smartregister.opensrp.xyz.rest of HAPI FHIR -->
<dependency>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir-jpaserver-base</artifactId>
<version>5.4.0</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-jcl</artifactId>
</exclusion>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- Test Dependencies-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>

</dependencies>


</project>
60 changes: 60 additions & 0 deletions src/main/java/org/smartregister/extension/model/ChildTreeNode.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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 location = new Location();
Bundle bundle = new Bundle();
List<Bundle.BundleEntryComponent> theEntry = new ArrayList<>();
Bundle.BundleEntryComponent entryComponent = new Bundle.BundleEntryComponent();
entryComponent.setResource(new Bundle());
theEntry.add(entryComponent);
bundle.setEntry(theEntry);
this.copyValues(location);
return location;
}

@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;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
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<Location> 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();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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<StringType> childIdentifiers;

public StringType getIdentifier() {
return identifier;
}

public ParentChildrenMap setIdentifier(StringType identifier) {
this.identifier = identifier;
return this;
}

public List<StringType> getChildIdentifiers() {
return childIdentifiers;
}

public ParentChildrenMap setChildIdentifiers(List<StringType> 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();
}

}
Loading

0 comments on commit 226a086

Please sign in to comment.