Skip to content

Commit

Permalink
MAT-7348: Handle successive requests for valueSet Search endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmcphillips committed Jul 16, 2024
1 parent 63624f4 commit 8aa267a
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,53 +131,98 @@ public ValueSetSearchResult searchValueSets(String apiKey, Map<String, String> q
.getEntry()
.forEach(
entry -> {
Resource resource = entry.getResource();
ValueSet vs = (ValueSet) resource;
if (resource instanceof ValueSet) {
String oid = "";
for (Identifier identifier : ((ValueSet) resource).getIdentifier()) {
if (identifier.getValue() != null && !identifier.getValue().isEmpty()) {
oid = identifier.getValue();
}
}
ValueSetForSearch valueSet =
ValueSetForSearch.builder()
.title(vs.getTitle())
.author(
Optional.ofNullable(
vs.getExtensionByUrl(
"http://hl7.org/fhir/StructureDefinition/valueset-author"))
.map(extension -> String.valueOf(extension.getValue()))
.orElse(""))
.name(vs.getName())
.composedOf(
vs.getCompose().getInclude().stream()
.map(x -> x.getSystem())
.collect(Collectors.joining(",")))
.effectiveDate(
String.valueOf(
vs.getExtensionByUrl(
"http://hl7.org/fhir/StructureDefinition/valueset-effectiveDate")
.getValue()))
.lastReviewDate(
String.valueOf(
vs.getExtensionByUrl(
"http://hl7.org/fhir/StructureDefinition/resource-lastReviewDate")
.getValue()))
.lastUpdated(vs.getMeta().getLastUpdated().toString())
.url(vs.getUrl())
.version(vs.getVersion())
.status(vs.getStatus())
.publisher(vs.getPublisher())
.purpose(vs.getPurpose())
.steward(vs.getPublisher())
.oid(oid)
.build();
valueSetList.add(valueSet);
}
log.info("valueSetList {}", valueSetList);
traverseValueSet(entry, valueSetList);
});
return ValueSetSearchResult.builder().valueSets(valueSetList).resultBundle(responseString).build();
// if there's a next link we want to hit it, and append the results until we're out of results
var links = bundle.getLink();
links.forEach(
(l) -> {
if (l.getRelation().equals("next")) {
recursiveRequestValueSets(valueSetList, apiKey, l.getUrl());
}
});

return ValueSetSearchResult.builder()
.valueSets(valueSetList)
.resultBundle(responseString)
.build();
}

public void recursiveRequestValueSets(
List<ValueSetForSearch> allValueSets, String apiKey, String uriString) {
String httpsString = uriString.replaceFirst("http", "https");
log.info(
"uri we're going to hit is[{}]",
httpsString); // vsac gives us http, we want https or it fails
IParser parser = fhirContext.newJsonParser();
String responseString =
fhirTerminologyServiceWebClient.fetchResourceFromVsac(httpsString, apiKey, "bundle");
Bundle bundle = parser.parseResource(Bundle.class, responseString);
List<ValueSetForSearch> valueSetListPage = new ArrayList<>();
bundle
.getEntry()
.forEach(
entry -> {
traverseValueSet(entry, valueSetListPage);
});
allValueSets.addAll(valueSetListPage);
var links = bundle.getLink();
links.forEach(
(l) -> {
if (l.getRelation().equals("next")) {
recursiveRequestValueSets(allValueSets, apiKey, l.getUrl());
}
});
}

private void traverseValueSet(
Bundle.BundleEntryComponent entry, List<ValueSetForSearch> valueSetList) {
Resource resource = entry.getResource();
ValueSet vs = (ValueSet) resource;
if (resource instanceof ValueSet) {
String oid = "";
for (Identifier identifier : ((ValueSet) resource).getIdentifier()) {
if (identifier.getValue() != null && !identifier.getValue().isEmpty()) {
oid = identifier.getValue();
}
}
ValueSetForSearch valueSet =
ValueSetForSearch.builder()
.title(vs.getTitle())
.author(
Optional.ofNullable(
vs.getExtensionByUrl(
"http://hl7.org/fhir/StructureDefinition/valueset-author"))
.map(extension -> String.valueOf(extension.getValue()))
.orElse(""))
.name(vs.getName())
.composedOf(
vs.getCompose().getInclude().stream()
.map(x -> x.getSystem())
.collect(Collectors.joining(",")))
.effectiveDate(
Optional.ofNullable(
vs.getExtensionByUrl(
"http://hl7.org/fhir/StructureDefinition/valueset-effectiveDate"))
.map(extension -> String.valueOf(extension.getValue()))
.orElse(""))
.lastReviewDate(
Optional.ofNullable(
vs.getExtensionByUrl(
"http://hl7.org/fhir/StructureDefinition/resource-lastReviewDate"))
.map(extension -> String.valueOf(extension.getValue()))
.orElse(""))
.lastUpdated(vs.getMeta().getLastUpdated().toString())
.url(vs.getUrl())
.version(vs.getVersion())
.status(vs.getStatus())
.publisher(vs.getPublisher())
.purpose(vs.getPurpose())
.steward(vs.getPublisher())
.oid(oid)
.build();
valueSetList.add(valueSet);
}
}

public List<CodeSystem> getAllCodeSystems() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public String getCodeResource(String code, CodeSystem codeSystem, String apiKey)
return fetchResourceFromVsac(uri.toString(), apiKey, "Code");
}

private String fetchResourceFromVsac(String uri, String apiKey, String resourceType) {
public String fetchResourceFromVsac(String uri, String apiKey, String resourceType) {
return fhirTerminologyWebClient
.get()
.uri(uri)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ void testSearchValueSets() {
Principal principal = mock(Principal.class);
when(principal.getName()).thenReturn(TEST_USER);
when(vsacService.verifyUmlsAccess(anyString())).thenReturn(umlsUser);
when(fhirTerminologyService.searchValueSets(any(), any())).thenReturn(ValueSetSearchResult.builder().valueSets(mockValueSets).build());
when(fhirTerminologyService.searchValueSets(any(), any()))
.thenReturn(ValueSetSearchResult.builder().valueSets(mockValueSets).build());
Map<String, String> queryParams = new HashMap<>();
queryParams.put("param1", "value1");
queryParams.put("param2", "value2");
Expand Down

0 comments on commit 8aa267a

Please sign in to comment.