|
| 1 | +package org.smartregister.helpers; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.LinkedList; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Queue; |
| 7 | +import java.util.stream.Collectors; |
| 8 | + |
| 9 | +import org.hl7.fhir.instance.model.api.IBaseBundle; |
| 10 | +import org.hl7.fhir.r4.model.Bundle; |
| 11 | +import org.hl7.fhir.r4.model.Coding; |
| 12 | +import org.hl7.fhir.r4.model.Location; |
| 13 | +import org.hl7.fhir.r4.model.StringType; |
| 14 | +import org.hl7.fhir.r4.model.UriType; |
| 15 | +import org.slf4j.Logger; |
| 16 | +import org.slf4j.LoggerFactory; |
| 17 | +import org.smartregister.utils.Constants; |
| 18 | + |
| 19 | +import com.google.common.annotations.VisibleForTesting; |
| 20 | + |
| 21 | +import ca.uhn.fhir.rest.api.SearchStyleEnum; |
| 22 | +import ca.uhn.fhir.rest.client.api.IGenericClient; |
| 23 | +import ca.uhn.fhir.rest.client.impl.GenericClient; |
| 24 | +import ca.uhn.fhir.rest.gclient.IQuery; |
| 25 | +import ca.uhn.fhir.rest.gclient.ReferenceClientParam; |
| 26 | + |
| 27 | +public class LocationHelper { |
| 28 | + |
| 29 | + private static final Logger logger = LoggerFactory.getLogger(LocationHelper.class); |
| 30 | + |
| 31 | + // Custom class to pair a Location with its lineage ancestors |
| 32 | + static class LocationWithTags { |
| 33 | + Location location; |
| 34 | + List<String> ancestorIds; |
| 35 | + |
| 36 | + public LocationWithTags(Location location, List<String> ancestorIds) { |
| 37 | + this.location = location; |
| 38 | + this.ancestorIds = ancestorIds; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + public static Location updateLocationLineage(IGenericClient client, String locationId) { |
| 43 | + Queue<LocationWithTags> locationsQueue = new LinkedList<>(); |
| 44 | + Location location = client.read().resource(Location.class).withId(locationId).execute(); |
| 45 | + |
| 46 | + List<String> ancestorIds = getParentLocationLineage(client, location); |
| 47 | + locationsQueue.add(new LocationWithTags(location, ancestorIds)); |
| 48 | + |
| 49 | + while (!locationsQueue.isEmpty()) { |
| 50 | + LocationWithTags currentLocationWithTags = locationsQueue.poll(); |
| 51 | + Location currentLocation = currentLocationWithTags.location; |
| 52 | + String currentLocationId = currentLocation.getIdElement().getIdPart(); |
| 53 | + |
| 54 | + updateLocationTags(client, currentLocation, currentLocationWithTags.ancestorIds); |
| 55 | + List<Location> childLocations = fetchChildLocations(client, currentLocationId); |
| 56 | + |
| 57 | + for (Location childLocation : childLocations) { |
| 58 | + locationsQueue.add( |
| 59 | + new LocationWithTags( |
| 60 | + childLocation, |
| 61 | + new ArrayList<>(currentLocationWithTags.ancestorIds))); |
| 62 | + } |
| 63 | + } |
| 64 | + return location; |
| 65 | + } |
| 66 | + |
| 67 | + @VisibleForTesting |
| 68 | + protected static List<String> getParentLocationLineage( |
| 69 | + IGenericClient client, Location location) { |
| 70 | + if (!location.hasPartOf() || !location.getPartOf().hasReference()) { |
| 71 | + return new ArrayList<>(); |
| 72 | + } |
| 73 | + |
| 74 | + String parentLocationId = location.getPartOf().getReferenceElement().getIdPart(); |
| 75 | + Location parentLocation = |
| 76 | + client.read().resource(Location.class).withId(parentLocationId).execute(); |
| 77 | + |
| 78 | + List<String> ancestorIds = |
| 79 | + parentLocation.getMeta().getTag().stream() |
| 80 | + .filter( |
| 81 | + tag -> |
| 82 | + Constants.DEFAULT_LOCATION_LINEAGE_TAG_URL.equals( |
| 83 | + tag.getSystem())) |
| 84 | + .map(Coding::getCode) |
| 85 | + .collect(Collectors.toList()); |
| 86 | + |
| 87 | + ancestorIds.add(parentLocationId); |
| 88 | + client.update().resource(location).execute(); |
| 89 | + return ancestorIds; |
| 90 | + } |
| 91 | + |
| 92 | + @VisibleForTesting |
| 93 | + protected static void updateLocationTags( |
| 94 | + IGenericClient client, Location location, List<String> ancestorIds) { |
| 95 | + logger.info("Adding lineage tags to Location Id : {}", location.getIdElement().getIdPart()); |
| 96 | + |
| 97 | + List<Coding> newTags = |
| 98 | + location.getMeta().getTag().stream() |
| 99 | + .filter( |
| 100 | + tag -> |
| 101 | + !Constants.DEFAULT_LOCATION_LINEAGE_TAG_URL.equals( |
| 102 | + tag.getSystem())) |
| 103 | + .collect(Collectors.toList()); |
| 104 | + |
| 105 | + for (String tag : ancestorIds) { |
| 106 | + newTags.add( |
| 107 | + new Coding() |
| 108 | + .setSystem(Constants.DEFAULT_LOCATION_LINEAGE_TAG_URL) |
| 109 | + .setCode(tag)); |
| 110 | + } |
| 111 | + |
| 112 | + location.getMeta().setTag(newTags); |
| 113 | + client.update().resource(location).execute(); |
| 114 | + } |
| 115 | + |
| 116 | + private static List<Location> fetchChildLocations(IGenericClient client, String locationId) { |
| 117 | + IQuery<IBaseBundle> query = |
| 118 | + client.search() |
| 119 | + .forResource(Location.class) |
| 120 | + .where( |
| 121 | + new ReferenceClientParam(Location.SP_PARTOF) |
| 122 | + .hasAnyOfIds(locationId)); |
| 123 | + |
| 124 | + Bundle childLocationBundle = |
| 125 | + query.usingStyle(SearchStyleEnum.POST) |
| 126 | + .count(100) |
| 127 | + .returnBundle(Bundle.class) |
| 128 | + .execute(); |
| 129 | + |
| 130 | + if (childLocationBundle != null) { |
| 131 | + fetchAllBundlePagesAndInject(client, childLocationBundle); |
| 132 | + return childLocationBundle.getEntry().stream() |
| 133 | + .map(Bundle.BundleEntryComponent::getResource) |
| 134 | + .filter(resource -> resource instanceof Location) |
| 135 | + .map(resource -> (Location) resource) |
| 136 | + .collect(Collectors.toList()); |
| 137 | + } |
| 138 | + return new ArrayList<>(); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * This is a recursive function which updates the result bundle with results of all pages |
| 143 | + * whenever there's an entry for Bundle.LINK_NEXT |
| 144 | + * |
| 145 | + * @param fhirClient the Generic FHIR Client instance |
| 146 | + * @param resultBundle the result bundle from the first request |
| 147 | + */ |
| 148 | + public static void fetchAllBundlePagesAndInject( |
| 149 | + IGenericClient fhirClient, Bundle resultBundle) { |
| 150 | + |
| 151 | + if (resultBundle.getLink(Bundle.LINK_NEXT) != null) { |
| 152 | + |
| 153 | + cleanUpBundlePaginationNextLinkServerBaseUrl((GenericClient) fhirClient, resultBundle); |
| 154 | + |
| 155 | + Bundle pageResultBundle = fhirClient.loadPage().next(resultBundle).execute(); |
| 156 | + |
| 157 | + resultBundle.getEntry().addAll(pageResultBundle.getEntry()); |
| 158 | + resultBundle.setLink(pageResultBundle.getLink()); |
| 159 | + |
| 160 | + fetchAllBundlePagesAndInject(fhirClient, resultBundle); |
| 161 | + } |
| 162 | + |
| 163 | + resultBundle.setLink( |
| 164 | + resultBundle.getLink().stream() |
| 165 | + .filter( |
| 166 | + bundleLinkComponent -> |
| 167 | + !Bundle.LINK_NEXT.equals(bundleLinkComponent.getRelation())) |
| 168 | + .collect(Collectors.toList())); |
| 169 | + resultBundle.getMeta().setLastUpdated(resultBundle.getMeta().getLastUpdated()); |
| 170 | + } |
| 171 | + |
| 172 | + public static void cleanUpBundlePaginationNextLinkServerBaseUrl( |
| 173 | + GenericClient fhirClient, Bundle resultBundle) { |
| 174 | + String cleanUrl = |
| 175 | + cleanHapiPaginationLinkBaseUrl( |
| 176 | + resultBundle.getLink(Bundle.LINK_NEXT).getUrl(), fhirClient.getUrlBase()); |
| 177 | + resultBundle |
| 178 | + .getLink() |
| 179 | + .replaceAll( |
| 180 | + bundleLinkComponent -> |
| 181 | + Bundle.LINK_NEXT.equals(bundleLinkComponent.getRelation()) |
| 182 | + ? new Bundle.BundleLinkComponent( |
| 183 | + new StringType(Bundle.LINK_NEXT), |
| 184 | + new UriType(cleanUrl)) |
| 185 | + : bundleLinkComponent); |
| 186 | + } |
| 187 | + |
| 188 | + public static String cleanHapiPaginationLinkBaseUrl( |
| 189 | + String originalUrl, String fhirServerBaseUrl) { |
| 190 | + return originalUrl.indexOf('?') > -1 |
| 191 | + ? fhirServerBaseUrl + originalUrl.substring(originalUrl.indexOf('?')) |
| 192 | + : fhirServerBaseUrl; |
| 193 | + } |
| 194 | +} |
0 commit comments