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.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<? extends IBaseResource> myCodeSystemType;
private Class<? extends IBaseResource> myStructureDefinitionType;
private Class<? extends IBaseResource> 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 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
if (myTermReadSvc.isLoincNotGenericUnversionedValueSet(theSystem)) {
Optional<IBaseResource> 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<IBaseResource> getValueSetCurrentVersion(UriType theUrl) {
private Optional<IBaseResource> 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<? extends IBaseResource> valueSetResourceDao = myDaoRegistry.getResourceDao(myValueSetType);
IBaseResource valueSet = valueSetResourceDao.read(new IdDt("ValueSet", forcedId));
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
public Optional<TermValueSet> 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) {
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);
}
List<TermValueSet> termValueSetList = myTermValueSetDao.findTermValueSetByUrl(Pageable.ofSize(1), theUrl);
if (termValueSetList.isEmpty()) return Optional.empty();
return Optional.of(termValueSetList.get(0));
}
if (! theUrl.startsWith(LOINC_GENERIC_VALUESET_URL)) return Optional.empty();
@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;
}

View File

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