Skip to content

Commit

Permalink
Fix List entries request fails when REL and Gatewaymode both specified
Browse files Browse the repository at this point in the history
  • Loading branch information
ndegwamartin committed Aug 20, 2024
1 parent bc500af commit 9f59e4a
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 89 deletions.
4 changes: 2 additions & 2 deletions exec/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.smartregister</groupId>
<artifactId>opensrp-gateway-plugin</artifactId>
<version>2.0.6-rc2</version>
<version>2.0.6-rc3</version>
</parent>

<artifactId>exec</artifactId>
Expand Down Expand Up @@ -70,7 +70,7 @@
<dependency>
<groupId>org.smartregister</groupId>
<artifactId>plugins</artifactId>
<version>2.0.6-rc2</version>
<version>2.0.6-rc3</version>
</dependency>

<dependency>
Expand Down
2 changes: 1 addition & 1 deletion plugins/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.smartregister</groupId>
<artifactId>opensrp-gateway-plugin</artifactId>
<version>2.0.6-rc2</version>
<version>2.0.6-rc3</version>
</parent>

<artifactId>plugins</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,112 +220,111 @@ public String postProcess(RequestDetailsReader request, HttpResponse response)
resultContent = new BasicResponseHandler().handleResponse(response);
IBaseResource responseResource = this.fhirR4JsonParser.parseResource(resultContent);

switch (gatewayMode) {
case SyncAccessDecisionConstants.LIST_ENTRIES:
resultContentBundle = postProcessModeListEntries(responseResource, request);
break;

default:
String exceptionMessage =
"The FHIR Gateway Mode header is configured with an un-recognized value"
+ " of \'"
+ gatewayMode
+ '\'';
OperationOutcome operationOutcome = createOperationOutcome(exceptionMessage);

resultContentBundle = operationOutcome;
if (gatewayMode.equals(SyncAccessDecisionConstants.LIST_ENTRIES)) {
resultContentBundle = postProcessModeListEntries(responseResource, request);
} else {
String exceptionMessage =
"The FHIR Gateway Mode header is configured with an un-recognized value"
+ " of \'"
+ gatewayMode
+ '\'';
OperationOutcome operationOutcome = createOperationOutcome(exceptionMessage);

resultContentBundle = operationOutcome;
}

if (resultContentBundle != null)
resultContent = this.fhirR4JsonParser.encodeResourceToString(resultContentBundle);
}
resultContent = this.fhirR4JsonParser.encodeResourceToString(resultContentBundle);

if (Constants.SyncStrategy.RELATED_ENTITY_LOCATION.equals(syncStrategy)) {
} else if (Constants.SyncStrategy.RELATED_ENTITY_LOCATION.equals(syncStrategy)) {

fhirR4Client
.getFhirContext()
.getRestfulClientFactory()
.setConnectionRequestTimeout(300000);
fhirR4Client.getFhirContext().getRestfulClientFactory().setSocketTimeout(300000);
IBaseResource responseResource =
processRelatedEntityLocationSyncStrategy(request, response);

int subListSize = 100;
List<Bundle.BundleEntryComponent> allResults = new ArrayList<>();
resultContent = this.fhirR4JsonParser.encodeResourceToString(responseResource);

String requestPath =
request.getRequestPath()
+ "?"
+ getRequestParametersString(request.getParameters());
} else if (includeAttributedPractitioners(request.getRequestPath())) {
Bundle practitionerDetailsBundle =
this.practitionerDetailsEndpointHelper
.getSupervisorPractitionerDetailsByKeycloakId(keycloakUUID);
resultContent = this.fhirR4JsonParser.encodeResourceToString(practitionerDetailsBundle);
}

for (int startIndex = REL_LOCATION_CHUNKSIZE;
startIndex
< syncStrategyIdsMap
.get(Constants.SyncStrategy.RELATED_ENTITY_LOCATION)
.size();
startIndex += subListSize) {
return resultContent;
}

int endIndex =
Math.min(
startIndex + subListSize,
syncStrategyIdsMap
.get(Constants.SyncStrategy.RELATED_ENTITY_LOCATION)
.size());
private IBaseResource processRelatedEntityLocationSyncStrategy(
RequestDetailsReader request, HttpResponse response) throws IOException {
String resultContent;
fhirR4Client.getFhirContext().getRestfulClientFactory().setConnectionRequestTimeout(300000);
fhirR4Client.getFhirContext().getRestfulClientFactory().setSocketTimeout(300000);

List<String> entries =
syncStrategyIdsMap
.get(Constants.SyncStrategy.RELATED_ENTITY_LOCATION)
.subList(startIndex, endIndex);

Bundle requestBundle = new Bundle();
requestBundle.setType(Bundle.BundleType.BATCH);
for (String entry : entries) {
requestBundle.addEntry(
createBundleEntryComponent(
Bundle.HTTPVerb.GET,
requestPath
+ "&_tag="
+ Constants.DEFAULT_RELATED_ENTITY_TAG_URL
+ "%7C"
+ entry,
null));
}
int subListSize = 100;
List<Bundle.BundleEntryComponent> allResults = new ArrayList<>();

Bundle res = fhirR4Client.transaction().withBundle(requestBundle).execute();
String requestPath =
request.getRequestPath()
+ "?"
+ getRequestParametersString(request.getParameters());

List<Bundle.BundleEntryComponent> sub =
res.getEntry().parallelStream()
.map(it -> (Bundle) it.getResource())
.flatMap(it -> it.getEntry().stream())
.collect(Collectors.toList());
for (int startIndex = REL_LOCATION_CHUNKSIZE;
startIndex
< syncStrategyIdsMap
.get(Constants.SyncStrategy.RELATED_ENTITY_LOCATION)
.size();
startIndex += subListSize) {

allResults.addAll(sub);
int endIndex =
Math.min(
startIndex + subListSize,
syncStrategyIdsMap
.get(Constants.SyncStrategy.RELATED_ENTITY_LOCATION)
.size());

List<String> entries =
syncStrategyIdsMap
.get(Constants.SyncStrategy.RELATED_ENTITY_LOCATION)
.subList(startIndex, endIndex);

Bundle requestBundle = new Bundle();
requestBundle.setType(Bundle.BundleType.BATCH);
for (String entry : entries) {
requestBundle.addEntry(
createBundleEntryComponent(
Bundle.HTTPVerb.GET,
requestPath
+ "&_tag="
+ Constants.DEFAULT_RELATED_ENTITY_TAG_URL
+ "%7C"
+ entry,
null));
}

resultContent = new BasicResponseHandler().handleResponse(response);
Bundle res = fhirR4Client.transaction().withBundle(requestBundle).execute();

IBaseResource responseResource = this.fhirR4JsonParser.parseResource(resultContent);
List<Bundle.BundleEntryComponent> sub =
res.getEntry().parallelStream()
.map(it -> (Bundle) it.getResource())
.flatMap(it -> it.getEntry().stream())
.collect(Collectors.toList());

if (responseResource instanceof Bundle) {
((Bundle) responseResource).getEntry().addAll(allResults);
((Bundle) responseResource).setTotal(((Bundle) responseResource).getEntry().size());
allResults.addAll(sub);
}

Bundle.BundleLinkComponent selfLinkComponent = new Bundle.BundleLinkComponent();
selfLinkComponent.setRelation(Bundle.LINK_SELF);
selfLinkComponent.setUrl(request.getCompleteUrl());
resultContent = new BasicResponseHandler().handleResponse(response);

((Bundle) responseResource).setLink(Collections.singletonList(selfLinkComponent));
}
IBaseResource responseResource = this.fhirR4JsonParser.parseResource(resultContent);

return this.fhirR4JsonParser.encodeResourceToString(responseResource);
}
if (responseResource instanceof Bundle) {
((Bundle) responseResource).getEntry().addAll(allResults);
((Bundle) responseResource).setTotal(((Bundle) responseResource).getEntry().size());

if (includeAttributedPractitioners(request.getRequestPath())) {
Bundle practitionerDetailsBundle =
this.practitionerDetailsEndpointHelper
.getSupervisorPractitionerDetailsByKeycloakId(keycloakUUID);
resultContent = this.fhirR4JsonParser.encodeResourceToString(practitionerDetailsBundle);
}
Bundle.BundleLinkComponent selfLinkComponent = new Bundle.BundleLinkComponent();
selfLinkComponent.setRelation(Bundle.LINK_SELF);
selfLinkComponent.setUrl(request.getCompleteUrl());

return resultContent;
((Bundle) responseResource).setLink(Collections.singletonList(selfLinkComponent));
}
return responseResource;
}

private String getRequestParametersString(Map<String, String[]> parameters) {
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<groupId>org.smartregister</groupId>
<artifactId>opensrp-gateway-plugin</artifactId>
<version>2.0.6-rc2</version>
<version>2.0.6-rc3</version>
<packaging>pom</packaging>

<modules>
Expand Down

0 comments on commit 9f59e4a

Please sign in to comment.