diff --git a/src/main/java/org/opencds/cqf/providers/JpaDataProvider.java b/src/main/java/org/opencds/cqf/providers/JpaDataProvider.java index bc18d7cc3..1bb12656b 100644 --- a/src/main/java/org/opencds/cqf/providers/JpaDataProvider.java +++ b/src/main/java/org/opencds/cqf/providers/JpaDataProvider.java @@ -67,28 +67,44 @@ public synchronized Iterable retrieve(String context, Object contextValu } } + boolean noResults = false; if (codePath != null && !codePath.equals("")) { - if (valueSet != null && terminologyProvider != null && expandValueSets) { - ValueSetInfo valueSetInfo = new ValueSetInfo().withId(valueSet); - codes = terminologyProvider.expand(valueSetInfo); - } - else if (valueSet != null) { - map.add(convertPathToSearchParam(dataType, codePath), new TokenParam(null, valueSet).setModifier(TokenParamModifier.IN)); + if (valueSet != null) { + if (expandValueSets) { + if (terminologyProvider == null) { + throw new IllegalArgumentException("Expand value sets cannot be used without a terminology provider and no terminology provider is set."); + } + ValueSetInfo valueSetInfo = new ValueSetInfo().withId(valueSet); + codes = terminologyProvider.expand(valueSetInfo); + } + else { + map.add(convertPathToSearchParam(dataType, codePath), new TokenParam(null, valueSet).setModifier(TokenParamModifier.IN)); + } } + if (codes != null) { TokenOrListParam codeParams = new TokenOrListParam(); + int codeCount = 0; for (Code code : codes) { + codeCount++; codeParams.addOr(new TokenParam(code.getSystem(), code.getCode())); } map.add(convertPathToSearchParam(dataType, codePath), codeParams); - if (codeParams.getListAsCodings().size() > 1023) - { - BooleanQuery.setMaxClauseCount(codeParams.getListAsCodings().size()); + if (codeCount == 0) { + noResults = true; + } + if (codeCount > 1023) { + BooleanQuery.setMaxClauseCount(codeCount); } } } + // If the retrieve is filtered to a value set that has no codes, there are no possible satisfying results, don't even search, just return empty + if (noResults) { + return new ArrayList(); + } + if (dateRange != null) { DateParam low = null; DateParam high = null; diff --git a/src/main/java/org/opencds/cqf/providers/JpaTerminologyProvider.java b/src/main/java/org/opencds/cqf/providers/JpaTerminologyProvider.java index 813f86b4c..224c6be6c 100644 --- a/src/main/java/org/opencds/cqf/providers/JpaTerminologyProvider.java +++ b/src/main/java/org/opencds/cqf/providers/JpaTerminologyProvider.java @@ -50,19 +50,22 @@ public synchronized boolean in(Code code, ValueSetInfo valueSet) throws Resource public synchronized Iterable expand(ValueSetInfo valueSet) throws ResourceNotFoundException { List codes = new ArrayList<>(); boolean needsExpand = false; - ValueSet vs; + ValueSet vs = null; if (valueSet.getId().startsWith("http://") || valueSet.getId().startsWith("https://")) { + if (valueSet.getVersion() != null || (valueSet.getCodeSystems() != null && valueSet.getCodeSystems().size() > 0)) { + throw new UnsupportedOperationException(String.format("Could not expand value set %s; version and code system bindings are not supported at this time.", valueSet.getId())); + } IBundleProvider bundleProvider = valueSetResourceProvider.getDao().search(new SearchParameterMap().add(ValueSet.SP_URL, new UriParam(valueSet.getId()))); List valueSets = bundleProvider.getResources(0, bundleProvider.size()); - if (!valueSets.isEmpty() && valueSets.size() == 1) { + if (valueSets.isEmpty()) { + throw new IllegalArgumentException(String.format("Could not resolve value set %s.", valueSet.getId())); + } + else if (valueSets.size() == 1) { vs = (ValueSet) valueSets.get(0); } else if (valueSets.size() > 1) { throw new IllegalArgumentException("Found more than 1 ValueSet with url: " + valueSet.getId()); } - else { - vs = null; - } } else { vs = valueSetResourceProvider.getDao().read(new IdType(valueSet.getId()));