Simplify code

This commit is contained in:
juan.marchionatto 2021-09-24 15:05:06 -04:00
parent bc250e141d
commit eb9f29b3f3
4 changed files with 30 additions and 35 deletions

View File

@ -38,7 +38,6 @@ import ca.uhn.fhir.rest.param.UriParam;
import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.hl7.fhir.instance.model.api.IAnyResource;
import org.hl7.fhir.instance.model.api.IBaseResource;
@ -63,7 +62,6 @@ import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.hl7.fhir.common.hapi.validation.support.ValidationConstants.LOINC_GENERIC_VALUESET_URL_PLUS_SLASH;
import static org.hl7.fhir.common.hapi.validation.support.ValidationConstants.LOINC_LOW;
/**
@ -144,13 +142,11 @@ public class JpaPersistedResourceValidationSupport implements IValidationSupport
* version is always pointed by the ForcedId for the no-versioned VS
*/
private Optional<IBaseResource> getValueSetCurrentVersion(UriType theUrl) {
if (TermReadSvcUtil.mustReturnEmptyValueSet(theUrl.getValueAsString())) return Optional.empty();
String forcedId = theUrl.getValue().substring(LOINC_GENERIC_VALUESET_URL_PLUS_SLASH.length());
if (StringUtils.isBlank(forcedId)) return Optional.empty();
Optional<String> vsIdOpt = TermReadSvcUtil.getValueSetId(theUrl.getValueAsString());
if (! vsIdOpt.isPresent()) return Optional.empty();
IFhirResourceDao<? extends IBaseResource> valueSetResourceDao = myDaoRegistry.getResourceDao(myValueSetType);
IBaseResource valueSet = valueSetResourceDao.read(new IdDt("ValueSet", forcedId));
IBaseResource valueSet = valueSetResourceDao.read(new IdDt("ValueSet", vsIdOpt.get()));
return Optional.ofNullable(valueSet);
}

View File

@ -181,7 +181,6 @@ import static org.apache.commons.lang3.StringUtils.isNoneBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.apache.commons.lang3.StringUtils.lowerCase;
import static org.apache.commons.lang3.StringUtils.startsWithIgnoreCase;
import static org.hl7.fhir.common.hapi.validation.support.ValidationConstants.LOINC_GENERIC_VALUESET_URL_PLUS_SLASH;
import static org.hl7.fhir.common.hapi.validation.support.ValidationConstants.LOINC_LOW;
public abstract class BaseTermReadSvcImpl implements ITermReadSvc {
@ -2349,12 +2348,10 @@ public abstract class BaseTermReadSvcImpl implements ITermReadSvc {
@Override
public Optional<TermValueSet> findCurrentTermValueSet(String theUrl) {
if (TermReadSvcUtil.isLoincNotGenericUnversionedValueSet(theUrl)) {
if (TermReadSvcUtil.mustReturnEmptyValueSet(theUrl)) return Optional.empty();
Optional<String> vsIdOpt = TermReadSvcUtil.getValueSetId(theUrl);
if (! vsIdOpt.isPresent()) 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);
return myTermValueSetDao.findTermValueSetByForcedId(vsIdOpt.get());
}
List<TermValueSet> termValueSetList = myTermValueSetDao.findTermValueSetByUrl(Pageable.ofSize(1), theUrl);

View File

@ -20,24 +20,30 @@ package ca.uhn.fhir.jpa.term;
* #L%
*/
import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Optional;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.hl7.fhir.common.hapi.validation.support.ValidationConstants.LOINC_GENERIC_VALUESET_URL;
import static org.hl7.fhir.common.hapi.validation.support.ValidationConstants.LOINC_GENERIC_VALUESET_URL_PLUS_SLASH;
import static org.hl7.fhir.common.hapi.validation.support.ValidationConstants.LOINC_LOW;
public class TermReadSvcUtil {
private static final Logger ourLog = LoggerFactory.getLogger(TermReadSvcUtil.class);
public static boolean mustReturnEmptyValueSet(String theUrl) {
if (! theUrl.startsWith(LOINC_GENERIC_VALUESET_URL)) return true;
public static Optional<String> getValueSetId(String theUrl) {
if (! theUrl.startsWith(LOINC_GENERIC_VALUESET_URL)) return Optional.empty();
if (! theUrl.startsWith(LOINC_GENERIC_VALUESET_URL_PLUS_SLASH)) {
throw new InternalErrorException("Don't know how to extract ValueSet's ForcedId from url: " + theUrl);
ourLog.error("Don't know how to extract ValueSet's ForcedId from url: " + theUrl);
return Optional.empty();
}
String forcedId = theUrl.substring(LOINC_GENERIC_VALUESET_URL_PLUS_SLASH.length());
return StringUtils.isBlank(forcedId);
return isBlank(forcedId) ? Optional.empty() : Optional.of(forcedId);
}

View File

@ -26,7 +26,6 @@ import ca.uhn.fhir.jpa.dao.data.ITermValueSetDao;
import ca.uhn.fhir.jpa.model.entity.ResourceTable;
import ca.uhn.fhir.jpa.term.TermReadSvcR4;
import ca.uhn.fhir.jpa.term.TermReadSvcUtil;
import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
import com.google.common.collect.Lists;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.r4.model.CodeSystem;
@ -96,33 +95,30 @@ class ITermReadSvcTest {
@Nested
public class MustReturnEmptyValueSet {
public class GetValueSetId {
@Test
void doesntStartWithGenericVSReturnsTrue() {
boolean ret = TermReadSvcUtil.mustReturnEmptyValueSet("http://boing.org");
assertTrue(ret);
void doesntStartWithGenericVSReturnsEmpty() {
Optional<String> vsIdOpt = TermReadSvcUtil.getValueSetId("http://boing.org");
assertFalse(vsIdOpt.isPresent());
}
@Test
void doesntStartWithGenericVSPlusSlashThrows() {
InternalErrorException thrown = assertThrows(
InternalErrorException.class,
() -> TermReadSvcUtil.mustReturnEmptyValueSet("http://loinc.org/vs-no-slash-after-vs"));
assertTrue(thrown.getMessage().contains("Don't know how to extract ValueSet's ForcedId from url:"));
void doesntStartWithGenericVSPlusSlashReturnsEmpty() {
Optional<String> vsIdOpt = TermReadSvcUtil.getValueSetId("http://loinc.org/vs-no-slash-after-vs");
assertFalse(vsIdOpt.isPresent());
}
@Test
void blankVsIdReturnsTrue() {
boolean ret = TermReadSvcUtil.mustReturnEmptyValueSet("http://loinc.org/vs/");
assertTrue(ret);
void blankVsIdReturnsEmpty() {
Optional<String> vsIdOpt = TermReadSvcUtil.getValueSetId("http://loinc.org");
assertFalse(vsIdOpt.isPresent());
}
@Test
void startsWithGenericPlusSlashPlusIdReturnsFalse() {
boolean ret = TermReadSvcUtil.mustReturnEmptyValueSet("http://loinc.org/vs/some-vs-id");
assertFalse(ret);
void startsWithGenericPlusSlashPlusIdReturnsValid() {
Optional<String> vsIdOpt = TermReadSvcUtil.getValueSetId("http://loinc.org/vs/some-vs-id");
assertTrue(vsIdOpt.isPresent());
}
}