From 9f59e4a4bc1da691f9dcaa1b1151b573c0d16e91 Mon Sep 17 00:00:00 2001 From: Martin Ndegwa Date: Tue, 20 Aug 2024 15:43:43 +0300 Subject: [PATCH] Fix List entries request fails when REL and Gatewaymode both specified --- exec/pom.xml | 4 +- plugins/pom.xml | 2 +- .../gateway/plugins/SyncAccessDecision.java | 169 +++++++++--------- pom.xml | 2 +- 4 files changed, 88 insertions(+), 89 deletions(-) diff --git a/exec/pom.xml b/exec/pom.xml index 211adee..f079526 100755 --- a/exec/pom.xml +++ b/exec/pom.xml @@ -4,7 +4,7 @@ org.smartregister opensrp-gateway-plugin - 2.0.6-rc2 + 2.0.6-rc3 exec @@ -70,7 +70,7 @@ org.smartregister plugins - 2.0.6-rc2 + 2.0.6-rc3 diff --git a/plugins/pom.xml b/plugins/pom.xml index c83ab70..829258f 100644 --- a/plugins/pom.xml +++ b/plugins/pom.xml @@ -4,7 +4,7 @@ org.smartregister opensrp-gateway-plugin - 2.0.6-rc2 + 2.0.6-rc3 plugins diff --git a/plugins/src/main/java/org/smartregister/fhir/gateway/plugins/SyncAccessDecision.java b/plugins/src/main/java/org/smartregister/fhir/gateway/plugins/SyncAccessDecision.java index 017b2e2..83c116d 100755 --- a/plugins/src/main/java/org/smartregister/fhir/gateway/plugins/SyncAccessDecision.java +++ b/plugins/src/main/java/org/smartregister/fhir/gateway/plugins/SyncAccessDecision.java @@ -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 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 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 allResults = new ArrayList<>(); - Bundle res = fhirR4Client.transaction().withBundle(requestBundle).execute(); + String requestPath = + request.getRequestPath() + + "?" + + getRequestParametersString(request.getParameters()); - List 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 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 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 parameters) { diff --git a/pom.xml b/pom.xml index b3fea7a..57c6df2 100755 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ org.smartregister opensrp-gateway-plugin - 2.0.6-rc2 + 2.0.6-rc3 pom