diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/primitive/IdDt.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/primitive/IdDt.java index fd626a3fb3e..1e58340c09b 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/primitive/IdDt.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/primitive/IdDt.java @@ -645,16 +645,8 @@ public class IdDt extends UriDt implements /*IPrimitiveDatatype, */IIdTy return new IdDt(value + '/' + Constants.PARAM_HISTORY + '/' + theVersion); } - private static boolean isValidLong(String id) { - if (StringUtils.isBlank(id)) { - return false; - } - for (int i = 0; i < id.length(); i++) { - if (Character.isDigit(id.charAt(i)) == false) { - return false; - } - } - return true; + public static boolean isValidLong(String id) { + return StringUtils.isNumeric(id); } /** diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/ReferenceParam.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/ReferenceParam.java index fca87b14841..b934f99dd77 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/ReferenceParam.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/ReferenceParam.java @@ -19,6 +19,7 @@ package ca.uhn.fhir.rest.param; * limitations under the License. * #L% */ +import static ca.uhn.fhir.model.primitive.IdDt.isValidLong; import static org.apache.commons.lang3.StringUtils.isBlank; import static org.apache.commons.lang3.StringUtils.isNotBlank; @@ -280,4 +281,8 @@ public class ReferenceParam extends BaseParam /*implements IQueryParameterType*/ retVal.setValueAsQueryToken(theContext, null, null, getValueAsQueryToken(theContext)); return retVal; } + + public boolean isIdPartValidLong() { + return isValidLong(getIdPart()); + } } diff --git a/hapi-fhir-jpaserver-searchparam/src/main/java/ca/uhn/fhir/jpa/searchparam/extractor/ResourceIndexedSearchParams.java b/hapi-fhir-jpaserver-searchparam/src/main/java/ca/uhn/fhir/jpa/searchparam/extractor/ResourceIndexedSearchParams.java index ad1febdcc77..70950c52bc8 100644 --- a/hapi-fhir-jpaserver-searchparam/src/main/java/ca/uhn/fhir/jpa/searchparam/extractor/ResourceIndexedSearchParams.java +++ b/hapi-fhir-jpaserver-searchparam/src/main/java/ca/uhn/fhir/jpa/searchparam/extractor/ResourceIndexedSearchParams.java @@ -260,6 +260,7 @@ public final class ResourceIndexedSearchParams { return resourceParams.stream().anyMatch(namedParamPredicate); } + // KHS This needs to be public as libraries outside of hapi call it directly public boolean matchResourceLinks(String theResourceName, String theParamName, IQueryParameterType theParam, String theParamPath) { ReferenceParam reference = (ReferenceParam)theParam; @@ -274,7 +275,11 @@ public final class ResourceIndexedSearchParams { ResourceTable target = theResourceLink.getTargetResource(); IdDt idDt = target.getIdDt(); if (idDt.isIdPartValidLong()) { - return theReference.getIdPartAsLong().equals(idDt.getIdPartAsLong()); + if (theReference.isIdPartValidLong()) { + return theReference.getIdPartAsLong().equals(idDt.getIdPartAsLong()); + } else { + return false; + } } else { ForcedId forcedId = target.getForcedId(); if (forcedId != null) { diff --git a/hapi-fhir-jpaserver-searchparam/src/test/java/ca/uhn/fhir/jpa/searchparam/extractor/ResourceIndexedSearchParamsTest.java b/hapi-fhir-jpaserver-searchparam/src/test/java/ca/uhn/fhir/jpa/searchparam/extractor/ResourceIndexedSearchParamsTest.java new file mode 100644 index 00000000000..aecf2227d02 --- /dev/null +++ b/hapi-fhir-jpaserver-searchparam/src/test/java/ca/uhn/fhir/jpa/searchparam/extractor/ResourceIndexedSearchParamsTest.java @@ -0,0 +1,82 @@ +package ca.uhn.fhir.jpa.searchparam.extractor; + +import ca.uhn.fhir.jpa.model.entity.ForcedId; +import ca.uhn.fhir.jpa.model.entity.ResourceLink; +import ca.uhn.fhir.jpa.model.entity.ResourceTable; +import ca.uhn.fhir.model.primitive.IdDt; +import ca.uhn.fhir.rest.param.ReferenceParam; +import org.hl7.fhir.instance.model.api.IIdType; +import org.junit.Before; +import org.junit.Test; + +import java.util.Date; + +import static org.junit.Assert.*; + +public class ResourceIndexedSearchParamsTest { + + public static final String STRING_ID = "StringId"; + public static final String LONG_ID = "123"; + private ResourceIndexedSearchParams myParams; + private ResourceTable myTarget; + + @Before + public void before() { + ResourceTable source = new ResourceTable(); + source.setResourceType("Patient"); + + myTarget = new ResourceTable(); + myTarget.setResourceType("Organization"); + + myParams = new ResourceIndexedSearchParams(source); + ResourceLink link = new ResourceLink("organization", source, myTarget, new Date()); + myParams.getResourceLinks().add(link); + } + + @Test + public void matchResourceLinksStringCompareToLong() { + ReferenceParam referenceParam = getReferenceParam(STRING_ID); + myTarget.setId(123L); + + boolean result = myParams.matchResourceLinks("Patient", "organization", referenceParam, "organization"); + assertFalse(result); + } + + @Test + public void matchResourceLinksStringCompareToString() { + ReferenceParam referenceParam = getReferenceParam(STRING_ID); + ForcedId forcedid = new ForcedId(); + forcedid.setForcedId(STRING_ID); + myTarget.setForcedId(forcedid); + + boolean result = myParams.matchResourceLinks("Patient", "organization", referenceParam, "organization"); + assertTrue(result); + } + + @Test + public void matchResourceLinksLongCompareToString() { + ReferenceParam referenceParam = getReferenceParam(LONG_ID); + ForcedId forcedid = new ForcedId(); + forcedid.setForcedId(STRING_ID); + myTarget.setForcedId(forcedid); + + boolean result = myParams.matchResourceLinks("Patient", "organization", referenceParam, "organization"); + assertFalse(result); + } + + @Test + public void matchResourceLinksLongCompareToLong() { + ReferenceParam referenceParam = getReferenceParam(LONG_ID); + myTarget.setId(123L); + + boolean result = myParams.matchResourceLinks("Patient", "organization", referenceParam, "organization"); + assertTrue(result); + } + + private ReferenceParam getReferenceParam(String theId) { + ReferenceParam retval = new ReferenceParam(); + retval.setValue(theId); + return retval; + } + +} diff --git a/hapi-fhir-jpaserver-searchparam/src/test/java/ca/uhn/fhir/jpa/searchparam/SearchParamExtractorDstu3Test.java b/hapi-fhir-jpaserver-searchparam/src/test/java/ca/uhn/fhir/jpa/searchparam/extractor/SearchParamExtractorDstu3Test.java similarity index 97% rename from hapi-fhir-jpaserver-searchparam/src/test/java/ca/uhn/fhir/jpa/searchparam/SearchParamExtractorDstu3Test.java rename to hapi-fhir-jpaserver-searchparam/src/test/java/ca/uhn/fhir/jpa/searchparam/extractor/SearchParamExtractorDstu3Test.java index 5e74873d84a..9cf121417cc 100644 --- a/hapi-fhir-jpaserver-searchparam/src/test/java/ca/uhn/fhir/jpa/searchparam/SearchParamExtractorDstu3Test.java +++ b/hapi-fhir-jpaserver-searchparam/src/test/java/ca/uhn/fhir/jpa/searchparam/extractor/SearchParamExtractorDstu3Test.java @@ -1,4 +1,4 @@ -package ca.uhn.fhir.jpa.searchparam; +package ca.uhn.fhir.jpa.searchparam.extractor; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.RuntimeResourceDefinition; @@ -7,6 +7,7 @@ import ca.uhn.fhir.jpa.model.entity.BaseResourceIndexedSearchParam; import ca.uhn.fhir.jpa.model.entity.ModelConfig; import ca.uhn.fhir.jpa.model.entity.ResourceIndexedSearchParamToken; import ca.uhn.fhir.jpa.model.entity.ResourceTable; +import ca.uhn.fhir.jpa.searchparam.JpaRuntimeSearchParam; import ca.uhn.fhir.jpa.searchparam.extractor.SearchParamExtractorDstu3; import ca.uhn.fhir.jpa.searchparam.registry.ISearchParamRegistry; import ca.uhn.fhir.util.TestUtil;