Skip to content

Commit

Permalink
Merge branch 'main' into add-security-scanning/43
Browse files Browse the repository at this point in the history
  • Loading branch information
allan-on authored Nov 3, 2023
2 parents 58db14e + aa25b59 commit 947354d
Show file tree
Hide file tree
Showing 10 changed files with 688 additions and 51 deletions.
2 changes: 1 addition & 1 deletion configuration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.smartregister</groupId>
<artifactId>hapi-fhir-opensrp-extensions</artifactId>
<version>0.0.21-SNAPSHOT</version>
<version>0.0.24-SNAPSHOT</version>
</parent>
<groupId>org.smartregister.hapi-fhir-opensrp-extensions</groupId>
<artifactId>configuration</artifactId>
Expand Down
4 changes: 2 additions & 2 deletions location/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
<parent>
<artifactId>hapi-fhir-opensrp-extensions</artifactId>
<groupId>org.smartregister</groupId>
<version>0.0.21-SNAPSHOT</version>
<version>0.0.24-SNAPSHOT</version>
</parent>

<description>The repository holds the location extensions on the HAPI server</description>
<groupId>org.smartregister.hapi-fhir-opensrp-extensions</groupId>
<name>HAPI FHIR OpenSRP Extensions (Locations)</name>
<packaging>jar</packaging>
<url>https://github.com/opensrp/hapi-fhir-opensrp-extensions</url>
<version>0.0.8-SNAPSHOT</version>
<version>0.0.10-SNAPSHOT</version>
<artifactId>location</artifactId>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,36 @@

package org.smartregister.extension.rest;

import static org.smartregister.utils.Constants.*;

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.annotation.RequiredParam;
import ca.uhn.fhir.rest.annotation.Search;
import ca.uhn.fhir.rest.api.server.IBundleProvider;
import ca.uhn.fhir.rest.param.*;
import ca.uhn.fhir.rest.param.ReferenceAndListParam;
import ca.uhn.fhir.rest.param.ReferenceOrListParam;
import ca.uhn.fhir.rest.param.ReferenceParam;
import ca.uhn.fhir.rest.param.TokenParam;
import ca.uhn.fhir.rest.server.IResourceProvider;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;
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.IBaseResource;
import org.hl7.fhir.r4.model.*;
import org.hl7.fhir.r4.model.IdType;
import org.hl7.fhir.r4.model.Location;
import org.hl7.fhir.r4.model.StringType;
import org.smartregister.model.location.LocationHierarchy;
import org.smartregister.model.location.LocationHierarchyTree;
import org.smartregister.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;

public class LocationHierarchyResourceProvider implements IResourceProvider {

@Autowired IFhirResourceDao<Location> locationIFhirResourceDao;

private static final Logger logger =
LogManager.getLogger(LocationHierarchyResourceProvider.class.toString());
Logger.getLogger(LocationHierarchyResourceProvider.class.toString());

@Override
public Class<? extends IBaseResource> getResourceType() {
Expand All @@ -48,36 +54,29 @@ public Class<? extends IBaseResource> getResourceType() {

@Search
public LocationHierarchy getLocationHierarchy(
@RequiredParam(name = IDENTIFIER) TokenParam identifier) {
@RequiredParam(name = Constants.ID) TokenParam id) {

SearchParameterMap paramMap = new SearchParameterMap();
paramMap.add(IDENTIFIER, identifier);

IBundleProvider locationBundle = locationIFhirResourceDao.search(paramMap);
List<IBaseResource> locations =
locationBundle != null
? locationBundle.getResources(0, locationBundle.size())
: new ArrayList<>();
String locationId = EMPTY_STRING;
if (locations.size() > 0
&& locations.get(0) != null
&& locations.get(0).getIdElement() != null) {
locationId = locations.get(0).getIdElement().getIdPart();
}
paramMap.add(Constants.ID, id);

Location location = locationIFhirResourceDao.read(new IdType(id.getValue()));
String locationId =
location != null ? location.getIdElement().getIdPart() : Constants.EMPTY_STRING;
LocationHierarchyTree locationHierarchyTree = new LocationHierarchyTree();
LocationHierarchy locationHierarchy = new LocationHierarchy();
if (StringUtils.isNotBlank(locationId) && locations.size() > 0) {

if (StringUtils.isNotBlank(locationId)) {

logger.info("Building Location Hierarchy of Location Id : " + locationId);
locationHierarchyTree.buildTreeFromList(
getLocationHierarchy(locationId, locations.get(0)));
locationHierarchyTree.buildTreeFromList(getLocationHierarchy(locationId, location));

StringType locationIdString = new StringType().setId(locationId).getIdElement();
locationHierarchy.setLocationId(locationIdString);
locationHierarchy.setId(LOCATION_RESOURCE + locationId);
locationHierarchy.setId(Constants.LOCATION_RESOURCE + locationId);

locationHierarchy.setLocationHierarchyTree(locationHierarchyTree);
} else {
locationHierarchy.setId(LOCATION_RESOURCE_NOT_FOUND);
locationHierarchy.setId(Constants.LOCATION_RESOURCE_NOT_FOUND);
}
return locationHierarchy;
}
Expand All @@ -86,29 +85,35 @@ private List<Location> getLocationHierarchy(String locationId, IBaseResource par
return descendants(locationId, parentLocation);
}

public List<Location> descendants(String locationId, IBaseResource parentLocation) {
public List<Location> descendants(String parentLocationId, IBaseResource parentLocation) {

SearchParameterMap paramMap = new SearchParameterMap();
ReferenceAndListParam thePartOf = new ReferenceAndListParam();
ReferenceParam partOf = new ReferenceParam();
partOf.setValue(LOCATION + FORWARD_SLASH + locationId);
partOf.setValue(Constants.LOCATION + Constants.FORWARD_SLASH + parentLocationId);
ReferenceOrListParam referenceOrListParam = new ReferenceOrListParam();
referenceOrListParam.add(partOf);
thePartOf.addValue(referenceOrListParam);
paramMap.add(PART_OF, thePartOf);
paramMap.add(Constants.PART_OF, thePartOf);

IBundleProvider childLocationBundle = locationIFhirResourceDao.search(paramMap);
List<Location> allLocations = new ArrayList<>();
List<Location> allLocations = Collections.synchronizedList(new ArrayList<>());
if (parentLocation != null) {
allLocations.add((Location) parentLocation);
}
if (childLocationBundle != null) {
for (IBaseResource childLocation :
childLocationBundle.getResources(0, childLocationBundle.size())) {
Location childLocationEntity = (Location) childLocation;
allLocations.add(childLocationEntity);
allLocations.addAll(descendants(childLocation.getIdElement().getIdPart(), null));
}
List<IBaseResource> childLocations =
childLocationBundle.getResources(0, childLocationBundle.size());

childLocations.parallelStream()
.forEach(
childLocation -> {
Location childLocationEntity = (Location) childLocation;
allLocations.add(childLocationEntity);
allLocations.addAll(
descendants(
childLocation.getIdElement().getIdPart(), null));
});
}

return allLocations;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2023 Ona Systems, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.smartregister.extension.rest;

import ca.uhn.fhir.rest.param.TokenParam;
import org.junit.Test;
import org.smartregister.extension.rest.utils.TestLocationIFhirResourceDao;

public class LocationHierarchyResourceProviderTest {

@Test
public void testGetLocationHierarchy() {
LocationHierarchyResourceProvider locationHierarchyResourceProvider =
new LocationHierarchyResourceProvider();
locationHierarchyResourceProvider.setLocationIFhirResourceDao(
new TestLocationIFhirResourceDao());

TokenParam tokenParam = new TokenParam();
tokenParam.setValue("location-id-1");
locationHierarchyResourceProvider.getLocationHierarchy(tokenParam);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright 2023 Ona Systems, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.smartregister.extension.rest.utils;

import ca.uhn.fhir.rest.api.server.IBundleProvider;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IPrimitiveType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class TestBundleProvider implements IBundleProvider {
private List<IBaseResource> resources = new ArrayList<>();

@Override
public IPrimitiveType<Date> getPublished() {
return null;
}

public void setResources(List<IBaseResource> resources) {
this.resources = resources;
}

@NotNull
@Override
public List<IBaseResource> getResources(int startIndex, int endIndex) {
return resources != null ? resources.subList(startIndex, endIndex) : new ArrayList<>();
}

@Nullable
@Override
public String getUuid() {
return null;
}

@Override
public Integer preferredPageSize() {
return null;
}

@Nullable
@Override
public Integer size() {
return resources != null ? resources.size() : 0;
}

@Override
public String getCurrentPageId() {
return IBundleProvider.super.getCurrentPageId();
}

@Override
public String getNextPageId() {
return IBundleProvider.super.getNextPageId();
}

@Override
public String getPreviousPageId() {
return IBundleProvider.super.getPreviousPageId();
}

@Override
public Integer getCurrentPageOffset() {
return IBundleProvider.super.getCurrentPageOffset();
}

@Override
public Integer getCurrentPageSize() {
return IBundleProvider.super.getCurrentPageSize();
}

@NotNull
@Override
public List<IBaseResource> getAllResources() {
return IBundleProvider.super.getAllResources();
}

@Override
public boolean isEmpty() {
return IBundleProvider.super.isEmpty();
}

@Override
public int sizeOrThrowNpe() {
return IBundleProvider.super.sizeOrThrowNpe();
}

@Override
public List<String> getAllResourceIds() {
return IBundleProvider.super.getAllResourceIds();
}
}
Loading

0 comments on commit 947354d

Please sign in to comment.