diff --git a/src/main/java/gov/cms/madie/terminology/service/FhirTerminologyService.java b/src/main/java/gov/cms/madie/terminology/service/FhirTerminologyService.java index 6251f1d..213253d 100644 --- a/src/main/java/gov/cms/madie/terminology/service/FhirTerminologyService.java +++ b/src/main/java/gov/cms/madie/terminology/service/FhirTerminologyService.java @@ -19,11 +19,8 @@ import org.springframework.web.util.UriComponentsBuilder; import java.time.Instant; +import java.util.*; import java.util.stream.Collectors; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Optional; @Service @Slf4j @@ -68,25 +65,52 @@ public List recursivelyRequestAllValueSetsExpansionsForQDM( valueSetsSearchCriteria.getProfile(), valueSetsSearchCriteria.getIncludeDraft(), valueSetsSearchCriteria.getManifestExpansion()); - ValueSet ValueSetResource = parser.parseResource(ValueSet.class, resource); - var total = ValueSetResource.getExpansion().getTotal(); // total valuesets + ValueSet valueSetResource = parser.parseResource(ValueSet.class, resource); + var total = valueSetResource.getExpansion().getTotal(); // total valuesets List concepts = - getValueSetConcepts(ValueSetResource, codeSystemEntries, "QDM"); - - allValueSets.add( - QdmValueSet.builder() - .oid(ValueSetResource.getIdPart()) - .displayName(ValueSetResource.getName()) - .version(ValueSetResource.getVersion()) - .concepts(concepts) - .build()); + getValueSetConcepts(valueSetResource, codeSystemEntries, "QDM"); + log.info( + "vs total [{}] count: [{}] offset: [{}], oid: [{}]", + total, + vsParam.getCount(), + vsParam.getOffset(), + vsParam.getOid()); + + // Check if the ValueSet with the same oid already exists in allValueSets + QdmValueSet existingValueSet = + allValueSets.stream() + .filter(vs -> vs.getOid().equals(vsParam.getOid())) + .findFirst() + .orElse(null); + if (existingValueSet != null) { + List updatedConcepts = new ArrayList<>(existingValueSet.getConcepts()); + updatedConcepts.addAll(concepts); + // Create a new QdmValueSet with the updated concepts + QdmValueSet updatedValueSet = + QdmValueSet.builder() + .oid(existingValueSet.getOid()) + .displayName(existingValueSet.getDisplayName()) + .version(existingValueSet.getVersion()) + .concepts(updatedConcepts) + .build(); + // Replace the existing QdmValueSet in the list + allValueSets.set(allValueSets.indexOf(existingValueSet), updatedValueSet); + } else { + allValueSets.add( + QdmValueSet.builder() + .oid(valueSetResource.getIdPart()) + .displayName(valueSetResource.getName()) + .version(valueSetResource.getVersion()) + .concepts(concepts) + .build()); + } // if the total results in the searchSet are still greater than our current offset + the count // of our last request, then we request again - if (vsParam.getOffset() + vsParam.getCount() < total) { + if (vsParam.getOffset() + vsParam.getCount() <= total) { vsParam.setOffset(vsParam.getOffset() + 1000); - recursivelyRequestAllValueSetsExpansionsForQDM( + return recursivelyRequestAllValueSetsExpansionsForQDM( allValueSets, apiKey, vsParam, valueSetsSearchCriteria, codeSystemEntries); } return allValueSets; @@ -95,7 +119,6 @@ public List recursivelyRequestAllValueSetsExpansionsForQDM( public List getValueSetsExpansionsForQdm( ValueSetsSearchCriteria valueSetsSearchCriteria, UmlsUser umlsUser) { List codeSystemEntries = mappingService.getCodeSystemEntries(); - List allValueSets = new ArrayList<>(); // going to build all values here. return valueSetsSearchCriteria.getValueSetParams().stream() .map( vsParam -> { @@ -106,7 +129,7 @@ public List getValueSetsExpansionsForQdm( .flatMap( vsParam -> recursivelyRequestAllValueSetsExpansionsForQDM( - allValueSets, + new ArrayList<>(), umlsUser.getApiKey(), vsParam, valueSetsSearchCriteria, diff --git a/src/main/java/gov/cms/madie/terminology/util/TerminologyServiceUtil.java b/src/main/java/gov/cms/madie/terminology/util/TerminologyServiceUtil.java index 0613705..35f17e2 100644 --- a/src/main/java/gov/cms/madie/terminology/util/TerminologyServiceUtil.java +++ b/src/main/java/gov/cms/madie/terminology/util/TerminologyServiceUtil.java @@ -119,6 +119,16 @@ public static URI buildValueSetResourceUri( ManifestExpansion manifestExpansion) { MultiValueMap params = new LinkedMultiValueMap<>(); String expandValueSetUri = "/ValueSet/" + valueSetParams.getOid() + "/$expand"; + if (valueSetParams != null) { + Integer offset = valueSetParams.getOffset(); + Integer count = valueSetParams.getCount(); + if (offset != null && offset >= 0) { + params.put("offset", List.of(String.valueOf(offset))); + } + if (count != null && count >= 0) { + params.put("count", List.of(String.valueOf(count))); + } + } if (StringUtils.isNotBlank(valueSetParams.getVersion())) { params.put("valueSetVersion", List.of(valueSetParams.getVersion())); } else if (manifestExpansion != null @@ -127,6 +137,7 @@ public static URI buildValueSetResourceUri( } else if (StringUtils.isNotBlank(includeDraft)) { params.put("includeDraft", List.of("true")); } + return UriComponentsBuilder.fromPath(expandValueSetUri).queryParams(params).build().toUri(); }