Skip to content

Commit

Permalink
Add prev and next pagination links
Browse files Browse the repository at this point in the history
  • Loading branch information
lincmba committed Feb 7, 2024
1 parent 7956df7 commit edbfca5
Showing 1 changed file with 75 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,16 @@
import org.apache.http.HttpResponse;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.util.TextUtils;
import org.hl7.fhir.instance.model.api.IBaseBundle;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.r4.model.Bundle;
import org.hl7.fhir.r4.model.ListResource;
import org.hl7.fhir.r4.model.OperationOutcome;
import org.hl7.fhir.r4.model.Resource;
import org.hl7.fhir.r4.model.*;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.annotations.VisibleForTesting;
import com.google.fhir.gateway.ExceptionUtil;
import com.google.fhir.gateway.ProxyConstants;
import com.google.fhir.gateway.interfaces.AccessDecision;
import com.google.fhir.gateway.interfaces.RequestDetailsReader;
import com.google.fhir.gateway.interfaces.RequestMutation;
Expand Down Expand Up @@ -216,23 +215,10 @@ private static OperationOutcome createOperationOutcome(String exception) {

@NotNull
private static Bundle processListEntriesGatewayModeByListResource(
ListResource responseListResource, RequestDetailsReader request) {
ListResource responseListResource, int start, int count) {
Bundle requestBundle = new Bundle();
requestBundle.setType(Bundle.BundleType.BATCH);

String[] pageSize = request.getParameters().get(Constants.PAGINATION_PAGE_SIZE);
String[] pageNumber = request.getParameters().get(Constants.PAGINATION_PAGE_NUMBER);

int count =
pageSize != null && pageSize.length > 0
? Integer.parseInt(pageSize[0])
: Constants.PAGINATION_DEFAULT_PAGE_SIZE;
int page =
pageNumber != null && pageNumber.length > 0
? Integer.parseInt(pageNumber[0])
: Constants.PAGINATION_DEFAULT_PAGE_NUMBER;

int start = Math.max(0, (page - 1)) * count;
int end = start + count;

List<ListResource.ListEntryComponent> entries = responseListResource.getEntry();
Expand All @@ -249,24 +235,10 @@ private static Bundle processListEntriesGatewayModeByListResource(
}

private Bundle processListEntriesGatewayModeByBundle(
IBaseResource responseResource, RequestDetailsReader request) {
IBaseResource responseResource, int start, int count) {
Bundle requestBundle = new Bundle();
requestBundle.setType(Bundle.BundleType.BATCH);

String[] pageSize = request.getParameters().get(Constants.PAGINATION_PAGE_SIZE);
String[] pageNumber = request.getParameters().get(Constants.PAGINATION_PAGE_NUMBER);

int count =
pageSize != null && pageSize.length > 0
? Integer.parseInt(pageSize[0])
: Constants.PAGINATION_DEFAULT_PAGE_SIZE;
int page =
pageNumber != null && pageNumber.length > 0
? Integer.parseInt(pageNumber[0])
: Constants.PAGINATION_DEFAULT_PAGE_NUMBER;

int start = Math.max(0, (page - 1)) * count;

List<Bundle.BundleEntryComponent> bundleEntryComponentList =
((Bundle) responseResource)
.getEntry().stream()
Expand Down Expand Up @@ -312,21 +284,62 @@ static Bundle.BundleEntryComponent createBundleEntryComponent(
private Bundle postProcessModeListEntries(
IBaseResource responseResource, RequestDetailsReader request) {

Map<String, String[]> parameters = new HashMap<>(request.getParameters());
String[] pageSize = parameters.get(Constants.PAGINATION_PAGE_SIZE);
String[] pageNumber = parameters.get(Constants.PAGINATION_PAGE_NUMBER);

int totalEntries = 0;
int count =
pageSize != null && pageSize.length > 0
? Integer.parseInt(pageSize[0])
: Constants.PAGINATION_DEFAULT_PAGE_SIZE;
int page =
pageNumber != null && pageNumber.length > 0
? Integer.parseInt(pageNumber[0])
: Constants.PAGINATION_DEFAULT_PAGE_NUMBER;

int start = Math.max(0, (page - 1)) * count;
Bundle requestBundle = null;

if (responseResource instanceof ListResource
&& ((ListResource) responseResource).hasEntry()) {

totalEntries = ((ListResource) responseResource).getEntry().size();
requestBundle =
processListEntriesGatewayModeByListResource(
(ListResource) responseResource, request);
(ListResource) responseResource, start, count);

} else if (responseResource instanceof Bundle) {
List<Bundle.BundleEntryComponent> entries = ((Bundle) responseResource).getEntry();
for (Bundle.BundleEntryComponent entry : entries) {
if (entry.getResource() instanceof ListResource) {
totalEntries = ((ListResource) entry.getResource()).getEntry().size();
break;
}
}

requestBundle = processListEntriesGatewayModeByBundle(responseResource, request);
requestBundle = processListEntriesGatewayModeByBundle(responseResource, start, count);
}
int nextPage = page < totalEntries / count ? page + 1 : 0; // 0 indicates no next page
int prevPage = page > 1 ? page - 1 : 0; // 0 indicates no previous page
Bundle resultBundle = fhirR4Client.transaction().withBundle(requestBundle).execute();
if (nextPage > 0) {
parameters.put("_page", new String[] {String.valueOf(nextPage)});
String nextUrl = constructUpdatedUrl(request, parameters);
Bundle.BundleLinkComponent nextLink = new Bundle.BundleLinkComponent();
nextLink.setRelation(IBaseBundle.LINK_NEXT);
nextLink.setUrl(nextUrl);
resultBundle.addLink(nextLink);
}
if (prevPage > 0) {
parameters.put("_page", new String[] {String.valueOf(prevPage)});
String prevUrl = constructUpdatedUrl(request, parameters);
Bundle.BundleLinkComponent previousLink = new Bundle.BundleLinkComponent();
previousLink.setRelation(IBaseBundle.LINK_PREV);
previousLink.setUrl(prevUrl);
resultBundle.addLink(previousLink);
}

return fhirR4Client.transaction().withBundle(requestBundle).execute();
return resultBundle;
}

private String getSyncTagUrl(String syncStrategy) {
Expand Down Expand Up @@ -406,10 +419,33 @@ private boolean isSyncUrl(RequestDetailsReader requestDetailsReader) {
return false;
}

private static String constructUpdatedUrl(
RequestDetailsReader requestDetails, Map<String, String[]> parameters) {
StringBuilder updatedUrlBuilder = new StringBuilder(requestDetails.getFhirServerBase());

updatedUrlBuilder.append("/").append(requestDetails.getRequestPath());

updatedUrlBuilder.append("?");
for (Map.Entry<String, String[]> entry : parameters.entrySet()) {
String paramName = entry.getKey();
String[] paramValues = entry.getValue();

for (String paramValue : paramValues) {
updatedUrlBuilder.append(paramName).append("=").append(paramValue).append("&");
}
}

// Remove the trailing '&' if present
if (updatedUrlBuilder.charAt(updatedUrlBuilder.length() - 1) == '&') {
updatedUrlBuilder.deleteCharAt(updatedUrlBuilder.length() - 1);
}

return updatedUrlBuilder.toString();
}

private boolean isResourceTypeRequest(String requestPath) {
if (!TextUtils.isEmpty(requestPath)) {
String[] sections =
requestPath.split(com.google.fhir.gateway.ProxyConstants.HTTP_URL_SEPARATOR);
String[] sections = requestPath.split(ProxyConstants.HTTP_URL_SEPARATOR);

return sections.length == 1 || (sections.length == 2 && TextUtils.isEmpty(sections[1]));
}
Expand Down

0 comments on commit edbfca5

Please sign in to comment.