diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/JpaPersistedResourceValidationSupport.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/JpaPersistedResourceValidationSupport.java index 77467bd3431..b88cff4231d 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/JpaPersistedResourceValidationSupport.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/JpaPersistedResourceValidationSupport.java @@ -26,6 +26,7 @@ import ca.uhn.fhir.context.support.IValidationSupport; import ca.uhn.fhir.jpa.api.dao.DaoRegistry; import ca.uhn.fhir.jpa.api.dao.IFhirResourceDao; import ca.uhn.fhir.jpa.searchparam.SearchParameterMap; +import ca.uhn.fhir.jpa.term.api.ITermReadSvc; import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.rest.api.SortOrderEnum; import ca.uhn.fhir.rest.api.SortSpec; @@ -81,6 +82,10 @@ public class JpaPersistedResourceValidationSupport implements IValidationSupport @Autowired private DaoRegistry myDaoRegistry; + + @Autowired + private ITermReadSvc myTermReadSvc; + private Class myCodeSystemType; private Class myStructureDefinitionType; private Class myValueSetType; @@ -107,25 +112,20 @@ public class JpaPersistedResourceValidationSupport implements IValidationSupport @Override public IBaseResource fetchValueSet(String theSystem) { - boolean isNotLoincCodeSystem = ! StringUtils.containsIgnoreCase(theSystem, "loinc"); - boolean hasVersion = theSystem.contains("|"); - boolean isForGenericValueSet = theSystem.equals(LOINC_GENERIC_VALUESET_URL); - if (isNotLoincCodeSystem || hasVersion || isForGenericValueSet) { - return fetchResource(myValueSetType, theSystem); + if (myTermReadSvc.isLoincNotGenericUnversionedValueSet(theSystem)) { + Optional currentVSOpt = getValueSetCurrentVersion(new UriType(theSystem)); + return currentVSOpt.orElseThrow(() -> new ResourceNotFoundException( + "Couldn't find current version ValueSet for url: " + theSystem)); } - // if no version is present, we need to fetch the resource for the current version if one exists, - // in case it is not the last loaded - Optional currentVSOpt = getValueSetCurrentVersion(new UriType(theSystem)); - return currentVSOpt.orElseThrow(() -> new ResourceNotFoundException( - "Couldn't find current version ValueSet for url: " + theSystem)); + return fetchResource(myValueSetType, theSystem); } /** * Obtains the current version of a ValueSet using the fact that the current * version is always pointed by the ForcedId for the no-versioned VS */ - public Optional getValueSetCurrentVersion(UriType theUrl) { + private Optional getValueSetCurrentVersion(UriType theUrl) { if (! theUrl.getValueAsString().startsWith(LOINC_GENERIC_VALUESET_URL)) return Optional.empty(); if (! theUrl.getValue().startsWith(LOINC_GENERIC_VALUESET_URL_PLUS_SLASH)) { @@ -133,6 +133,10 @@ public class JpaPersistedResourceValidationSupport implements IValidationSupport } String forcedId = theUrl.getValue().substring(LOINC_GENERIC_VALUESET_URL_PLUS_SLASH.length()); + if (StringUtils.isBlank(forcedId)) return Optional.empty(); + + if (myTermReadSvc.mustReturnEmptyValueSet(theUrl.getValueAsString())) return Optional.empty(); + IFhirResourceDao valueSetResourceDao = myDaoRegistry.getResourceDao(myValueSetType); IBaseResource valueSet = valueSetResourceDao.read(new IdDt("ValueSet", forcedId)); return Optional.ofNullable(valueSet); diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/term/BaseTermReadSvcImpl.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/term/BaseTermReadSvcImpl.java index b55dab9e7a4..cd96da10f42 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/term/BaseTermReadSvcImpl.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/term/BaseTermReadSvcImpl.java @@ -2337,27 +2337,48 @@ public abstract class BaseTermReadSvcImpl implements ITermReadSvc { } + /** + * When the search is for no-version loinc system it uses the forcedId to obtain the current + * version, as it is not necessarily the last one anymore. + * For other cases it keeps on considering the last uploaded as the current + */ @Override public Optional findCurrentTermValueSet(String theUrl) { - boolean isNotLoincCodeSystem = ! StringUtils.containsIgnoreCase(theUrl, "loinc"); - boolean hasVersion = theUrl.contains("|"); - boolean isForGenericValueSet = theUrl.equals(LOINC_GENERIC_VALUESET_URL); - if (isNotLoincCodeSystem || hasVersion || isForGenericValueSet) { - List termValueSetList = myTermValueSetDao.findTermValueSetByUrl(Pageable.ofSize(1), theUrl); - if (termValueSetList.isEmpty()) return Optional.empty(); - return Optional.of(termValueSetList.get(0)); + if (isLoincNotGenericUnversionedValueSet(theUrl)) { + if (mustReturnEmptyValueSet(theUrl)) return Optional.empty(); + + String forcedId = theUrl.substring(LOINC_GENERIC_VALUESET_URL_PLUS_SLASH.length()); + if (StringUtils.isBlank(forcedId)) return Optional.empty(); + + return myTermValueSetDao.findTermValueSetByForcedId(forcedId); } - if (! theUrl.startsWith(LOINC_GENERIC_VALUESET_URL)) return Optional.empty(); + List termValueSetList = myTermValueSetDao.findTermValueSetByUrl(Pageable.ofSize(1), theUrl); + if (termValueSetList.isEmpty()) return Optional.empty(); + return Optional.of(termValueSetList.get(0)); + } + + + @Override + public boolean mustReturnEmptyValueSet(String theUrl) { + if (! theUrl.startsWith(LOINC_GENERIC_VALUESET_URL)) return true; if (! theUrl.startsWith(LOINC_GENERIC_VALUESET_URL_PLUS_SLASH)) { throw new InternalErrorException("Don't know how to extract ForcedId from url: " + theUrl); } String forcedId = theUrl.substring(LOINC_GENERIC_VALUESET_URL_PLUS_SLASH.length()); - if (StringUtils.isBlank(forcedId)) return Optional.empty(); + return StringUtils.isBlank(forcedId); + } - return myTermValueSetDao.findTermValueSetByForcedId(forcedId); + + @Override + public boolean isLoincNotGenericUnversionedValueSet(String theUrl) { + boolean isLoincCodeSystem = StringUtils.containsIgnoreCase(theUrl, "loinc"); + boolean isNoVersion = ! theUrl.contains("|"); + boolean isNotLoincGenericValueSet = ! theUrl.equals(LOINC_GENERIC_VALUESET_URL); + + return isLoincCodeSystem && isNoVersion && isNotLoincGenericValueSet; } diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/term/api/ITermReadSvc.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/term/api/ITermReadSvc.java index b4d0d94e2e7..31569dedfd9 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/term/api/ITermReadSvc.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/term/api/ITermReadSvc.java @@ -120,4 +120,8 @@ public interface ITermReadSvc extends IValidationSupport { * Version independent */ Optional findCurrentTermValueSet(String theUrl); + + boolean mustReturnEmptyValueSet(String theUrl); + + boolean isLoincNotGenericUnversionedValueSet(String theUrl); }