Refactor to eliminate duplication

This commit is contained in:
juan.marchionatto 2021-09-10 11:22:04 -04:00
parent d7fde3f984
commit 5b6ac3e6d6
3 changed files with 50 additions and 21 deletions

View File

@ -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.DaoRegistry;
import ca.uhn.fhir.jpa.api.dao.IFhirResourceDao; import ca.uhn.fhir.jpa.api.dao.IFhirResourceDao;
import ca.uhn.fhir.jpa.searchparam.SearchParameterMap; 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.model.primitive.IdDt;
import ca.uhn.fhir.rest.api.SortOrderEnum; import ca.uhn.fhir.rest.api.SortOrderEnum;
import ca.uhn.fhir.rest.api.SortSpec; import ca.uhn.fhir.rest.api.SortSpec;
@ -81,6 +82,10 @@ public class JpaPersistedResourceValidationSupport implements IValidationSupport
@Autowired @Autowired
private DaoRegistry myDaoRegistry; private DaoRegistry myDaoRegistry;
@Autowired
private ITermReadSvc myTermReadSvc;
private Class<? extends IBaseResource> myCodeSystemType; private Class<? extends IBaseResource> myCodeSystemType;
private Class<? extends IBaseResource> myStructureDefinitionType; private Class<? extends IBaseResource> myStructureDefinitionType;
private Class<? extends IBaseResource> myValueSetType; private Class<? extends IBaseResource> myValueSetType;
@ -107,25 +112,20 @@ public class JpaPersistedResourceValidationSupport implements IValidationSupport
@Override @Override
public IBaseResource fetchValueSet(String theSystem) { public IBaseResource fetchValueSet(String theSystem) {
boolean isNotLoincCodeSystem = ! StringUtils.containsIgnoreCase(theSystem, "loinc"); if (myTermReadSvc.isLoincNotGenericUnversionedValueSet(theSystem)) {
boolean hasVersion = theSystem.contains("|"); Optional<IBaseResource> currentVSOpt = getValueSetCurrentVersion(new UriType(theSystem));
boolean isForGenericValueSet = theSystem.equals(LOINC_GENERIC_VALUESET_URL); return currentVSOpt.orElseThrow(() -> new ResourceNotFoundException(
if (isNotLoincCodeSystem || hasVersion || isForGenericValueSet) { "Couldn't find current version ValueSet for url: " + theSystem));
return fetchResource(myValueSetType, theSystem);
} }
// if no version is present, we need to fetch the resource for the current version if one exists, return fetchResource(myValueSetType, theSystem);
// in case it is not the last loaded
Optional<IBaseResource> currentVSOpt = getValueSetCurrentVersion(new UriType(theSystem));
return currentVSOpt.orElseThrow(() -> new ResourceNotFoundException(
"Couldn't find current version ValueSet for url: " + theSystem));
} }
/** /**
* Obtains the current version of a ValueSet using the fact that the current * 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 * version is always pointed by the ForcedId for the no-versioned VS
*/ */
public Optional<IBaseResource> getValueSetCurrentVersion(UriType theUrl) { private Optional<IBaseResource> getValueSetCurrentVersion(UriType theUrl) {
if (! theUrl.getValueAsString().startsWith(LOINC_GENERIC_VALUESET_URL)) return Optional.empty(); if (! theUrl.getValueAsString().startsWith(LOINC_GENERIC_VALUESET_URL)) return Optional.empty();
if (! theUrl.getValue().startsWith(LOINC_GENERIC_VALUESET_URL_PLUS_SLASH)) { 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()); 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<? extends IBaseResource> valueSetResourceDao = myDaoRegistry.getResourceDao(myValueSetType); IFhirResourceDao<? extends IBaseResource> valueSetResourceDao = myDaoRegistry.getResourceDao(myValueSetType);
IBaseResource valueSet = valueSetResourceDao.read(new IdDt("ValueSet", forcedId)); IBaseResource valueSet = valueSetResourceDao.read(new IdDt("ValueSet", forcedId));
return Optional.ofNullable(valueSet); return Optional.ofNullable(valueSet);

View File

@ -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 @Override
public Optional<TermValueSet> findCurrentTermValueSet(String theUrl) { public Optional<TermValueSet> findCurrentTermValueSet(String theUrl) {
boolean isNotLoincCodeSystem = ! StringUtils.containsIgnoreCase(theUrl, "loinc"); if (isLoincNotGenericUnversionedValueSet(theUrl)) {
boolean hasVersion = theUrl.contains("|"); if (mustReturnEmptyValueSet(theUrl)) return Optional.empty();
boolean isForGenericValueSet = theUrl.equals(LOINC_GENERIC_VALUESET_URL);
if (isNotLoincCodeSystem || hasVersion || isForGenericValueSet) { String forcedId = theUrl.substring(LOINC_GENERIC_VALUESET_URL_PLUS_SLASH.length());
List<TermValueSet> termValueSetList = myTermValueSetDao.findTermValueSetByUrl(Pageable.ofSize(1), theUrl); if (StringUtils.isBlank(forcedId)) return Optional.empty();
if (termValueSetList.isEmpty()) return Optional.empty();
return Optional.of(termValueSetList.get(0)); return myTermValueSetDao.findTermValueSetByForcedId(forcedId);
} }
if (! theUrl.startsWith(LOINC_GENERIC_VALUESET_URL)) return Optional.empty(); List<TermValueSet> 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)) { if (! theUrl.startsWith(LOINC_GENERIC_VALUESET_URL_PLUS_SLASH)) {
throw new InternalErrorException("Don't know how to extract ForcedId from url: " + theUrl); throw new InternalErrorException("Don't know how to extract ForcedId from url: " + theUrl);
} }
String forcedId = theUrl.substring(LOINC_GENERIC_VALUESET_URL_PLUS_SLASH.length()); 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;
} }

View File

@ -120,4 +120,8 @@ public interface ITermReadSvc extends IValidationSupport {
* Version independent * Version independent
*/ */
Optional<TermValueSet> findCurrentTermValueSet(String theUrl); Optional<TermValueSet> findCurrentTermValueSet(String theUrl);
boolean mustReturnEmptyValueSet(String theUrl);
boolean isLoincNotGenericUnversionedValueSet(String theUrl);
} }