From 16cfcc87c8660dfc292ade71032eb983b2360948 Mon Sep 17 00:00:00 2001 From: James Agnew Date: Wed, 30 Oct 2019 08:57:51 -0400 Subject: [PATCH] Work on extractor --- .../extractor/BaseSearchParamExtractor.java | 102 +++++++++++------- .../extractor/ISearchParamExtractor.java | 40 +++++-- 2 files changed, 92 insertions(+), 50 deletions(-) diff --git a/hapi-fhir-jpaserver-searchparam/src/main/java/ca/uhn/fhir/jpa/searchparam/extractor/BaseSearchParamExtractor.java b/hapi-fhir-jpaserver-searchparam/src/main/java/ca/uhn/fhir/jpa/searchparam/extractor/BaseSearchParamExtractor.java index f2e8397ac5a..d69582b1ce5 100644 --- a/hapi-fhir-jpaserver-searchparam/src/main/java/ca/uhn/fhir/jpa/searchparam/extractor/BaseSearchParamExtractor.java +++ b/hapi-fhir-jpaserver-searchparam/src/main/java/ca/uhn/fhir/jpa/searchparam/extractor/BaseSearchParamExtractor.java @@ -59,6 +59,15 @@ public abstract class BaseSearchParamExtractor implements ISearchParamExtractor @Autowired private ModelConfig myModelConfig; private Set myIgnoredForSearchDatatypes; + private BaseRuntimeChildDefinition quantityValueChild; + private BaseRuntimeChildDefinition quantitySystemChild; + private BaseRuntimeChildDefinition quantityCodeChild; + private BaseRuntimeChildDefinition moneyValueChild; + private BaseRuntimeChildDefinition moneyCurrencyChild; + private BaseRuntimeElementCompositeDefinition locationPositionDefinition; + private BaseRuntimeChildDefinition codeSystemUrlValueChild; + private BaseRuntimeChildDefinition rangeLowValueChild; + private BaseRuntimeChildDefinition rangeHighValueChild; /** * Constructor @@ -77,14 +86,7 @@ public abstract class BaseSearchParamExtractor implements ISearchParamExtractor } @Override - public Set extractSearchParamTokens(IBaseResource theResource) { - BaseRuntimeElementCompositeDefinition codeSystemDefinition; - BaseRuntimeChildDefinition codeSystemUrlValueChild = null; - if (getContext().getVersion().getVersion().isEqualOrNewerThan(FhirVersionEnum.DSTU3)) { - codeSystemDefinition = getContext().getResourceDefinition("CodeSystem"); - assert codeSystemDefinition != null; - codeSystemUrlValueChild = codeSystemDefinition.getChildByName("url"); - } + public SearchParamSet extractSearchParamTokens(IBaseResource theResource) { String resourceTypeName = toRootTypeName(theResource); String useSystem; @@ -176,15 +178,20 @@ public abstract class BaseSearchParamExtractor implements ISearchParamExtractor addToken_ContactPoint(resourceTypeName, params, searchParam, value); break; default: - throw new ConfigurationException("Search param " + searchParam.getName() + " is of unexpected datatype: " + value.getClass()); + addUnexpectedDatatypeWarning(params, searchParam, value); + break; } }; return extractSearchParams(theResource, extractor, RestSearchParameterTypeEnum.TOKEN); } + public void addUnexpectedDatatypeWarning(SearchParamSet params, RuntimeSearchParam searchParam, IBase value) { + params.addWarning("Search param " + searchParam.getName() + " is of unexpected datatype: " + value.getClass()); + } + @Override - public Set extractSearchParamUri(IBaseResource theResource) { + public SearchParamSet extractSearchParamUri(IBaseResource theResource) { IExtractor extractor = (params, searchParam, value, path) -> { String nextType = toRootTypeName(value); String resourceType = toRootTypeName(theResource); @@ -197,7 +204,8 @@ public abstract class BaseSearchParamExtractor implements ISearchParamExtractor addUri_Uri(resourceType, params, searchParam, value); break; default: - throw new ConfigurationException("Search param " + searchParam.getName() + " is of unexpected datatype: " + value.getClass()); + addUnexpectedDatatypeWarning(params, searchParam, value); + break; } }; @@ -205,13 +213,12 @@ public abstract class BaseSearchParamExtractor implements ISearchParamExtractor } @Override - public Set extractSearchParamCoords(IBaseResource theResource) { - // TODO: implement - return Collections.emptySet(); + public SearchParamSet extractSearchParamCoords(IBaseResource theResource) { + return new SearchParamSet<>(); } @Override - public Set extractSearchParamDates(IBaseResource theResource) { + public SearchParamSet extractSearchParamDates(IBaseResource theResource) { IExtractor extractor = (params, searchParam, value, path) -> { String nextType = toRootTypeName(value); String resourceType = toRootTypeName(theResource); @@ -231,7 +238,8 @@ public abstract class BaseSearchParamExtractor implements ISearchParamExtractor // CarePlan.activitydate can be a string - ignored for now break; default: - throw new ConfigurationException("Search param " + searchParam.getName() + " is of unexpected datatype: " + value.getClass()); + addUnexpectedDatatypeWarning(params, searchParam, value); + break; } }; @@ -239,7 +247,7 @@ public abstract class BaseSearchParamExtractor implements ISearchParamExtractor } @Override - public Set extractSearchParamNumber(IBaseResource theResource) { + public SearchParamSet extractSearchParamNumber(IBaseResource theResource) { IExtractor extractor = (params, searchParam, value, path) -> { String nextType = toRootTypeName(value); @@ -260,7 +268,8 @@ public abstract class BaseSearchParamExtractor implements ISearchParamExtractor addNumber_Decimal(resourceType, params, searchParam, value); break; default: - throw new ConfigurationException("Search param " + searchParam.getName() + " is of unexpected datatype: " + value.getClass()); + addUnexpectedDatatypeWarning(params, searchParam, value); + break; } }; @@ -268,11 +277,7 @@ public abstract class BaseSearchParamExtractor implements ISearchParamExtractor } @Override - public Set extractSearchParamQuantity(IBaseResource theResource) { - BaseRuntimeElementCompositeDefinition locationDefinition = (BaseRuntimeElementCompositeDefinition) getContext().getResourceDefinition("Location"); - BaseRuntimeChildDefinition locationPositionValueChild = locationDefinition.getChildByName("position"); - BaseRuntimeElementCompositeDefinition locationPositionDefinition = (BaseRuntimeElementCompositeDefinition) locationPositionValueChild.getChildByName("position"); - + public SearchParamSet extractSearchParamQuantity(IBaseResource theResource) { IExtractor extractor = (params, searchParam, value, path) -> { if (value.getClass().equals(locationPositionDefinition.getImplementingClass())) { @@ -293,7 +298,8 @@ public abstract class BaseSearchParamExtractor implements ISearchParamExtractor addQuantity_Range(resourceType, params, searchParam, value); break; default: - throw new ConfigurationException("Search param " + searchParam.getName() + " is of unexpected datatype: " + value.getClass()); + addUnexpectedDatatypeWarning(params, searchParam, value); + break; } }; @@ -301,7 +307,7 @@ public abstract class BaseSearchParamExtractor implements ISearchParamExtractor } @Override - public Set extractSearchParamStrings(IBaseResource theResource) { + public SearchParamSet extractSearchParamStrings(IBaseResource theResource) { IExtractor extractor = (params, searchParam, value, path) -> { String resourceType = toRootTypeName(theResource); @@ -330,7 +336,8 @@ public abstract class BaseSearchParamExtractor implements ISearchParamExtractor addString_Range(resourceType, params, searchParam, value); break; default: - throw new ConfigurationException("Search param " + searchParam.getName() + " is of unexpected datatype: " + value.getClass()); + addUnexpectedDatatypeWarning(params, searchParam, value); + break; } }; @@ -356,7 +363,6 @@ public abstract class BaseSearchParamExtractor implements ISearchParamExtractor } /** - * [ * Override parent because we're using FHIRPath here */ private List extractValues(String thePaths, IBaseResource theResource) { @@ -419,13 +425,34 @@ public abstract class BaseSearchParamExtractor implements ISearchParamExtractor addIgnoredType(getContext(), "Ratio", myIgnoredForSearchDatatypes); addIgnoredType(getContext(), "SampledData", myIgnoredForSearchDatatypes); addIgnoredType(getContext(), "Signature", myIgnoredForSearchDatatypes); + + BaseRuntimeElementCompositeDefinition quantityDefinition = (BaseRuntimeElementCompositeDefinition) getContext().getElementDefinition("Quantity"); + quantityValueChild = quantityDefinition.getChildByName("value"); + quantitySystemChild = quantityDefinition.getChildByName("system"); + quantityCodeChild = quantityDefinition.getChildByName("code"); + + BaseRuntimeElementCompositeDefinition moneyDefinition = (BaseRuntimeElementCompositeDefinition) getContext().getElementDefinition("Money"); + moneyValueChild = moneyDefinition.getChildByName("value"); + moneyCurrencyChild = moneyDefinition.getChildByName("currency"); + + BaseRuntimeElementCompositeDefinition locationDefinition = getContext().getResourceDefinition("Location"); + BaseRuntimeChildDefinition locationPositionValueChild = locationDefinition.getChildByName("position"); + locationPositionDefinition = (BaseRuntimeElementCompositeDefinition) locationPositionValueChild.getChildByName("position"); + + BaseRuntimeElementCompositeDefinition codeSystemDefinition; + if (getContext().getVersion().getVersion().isEqualOrNewerThan(FhirVersionEnum.DSTU3)) { + codeSystemDefinition = getContext().getResourceDefinition("CodeSystem"); + assert codeSystemDefinition != null; + codeSystemUrlValueChild = codeSystemDefinition.getChildByName("url"); + } + + BaseRuntimeElementCompositeDefinition rangeDefinition = (BaseRuntimeElementCompositeDefinition) getContext().getElementDefinition("Range"); + rangeLowValueChild = rangeDefinition.getChildByName("low"); + rangeHighValueChild = rangeDefinition.getChildByName("high"); + } private void addQuantity_Quantity(String theResourceType, Set theParams, RuntimeSearchParam theSearchParam, IBase theValue) { - BaseRuntimeElementCompositeDefinition quantityDefinition = (BaseRuntimeElementCompositeDefinition) getContext().getElementDefinition("Quantity"); - BaseRuntimeChildDefinition quantityValueChild = quantityDefinition.getChildByName("value"); - BaseRuntimeChildDefinition quantitySystemChild = quantityDefinition.getChildByName("system"); - BaseRuntimeChildDefinition quantityCodeChild = quantityDefinition.getChildByName("code"); Optional> valueField = quantityValueChild.getAccessor().getFirstValueOrNull(theValue); if (valueField.isPresent() && valueField.get().getValue() != null) { @@ -440,9 +467,6 @@ public abstract class BaseSearchParamExtractor implements ISearchParamExtractor } private void addQuantity_Money(String theResourceType, Set theParams, RuntimeSearchParam theSearchParam, IBase theValue) { - BaseRuntimeElementCompositeDefinition moneyDefinition = (BaseRuntimeElementCompositeDefinition) getContext().getElementDefinition("Money"); - BaseRuntimeChildDefinition moneyValueChild = moneyDefinition.getChildByName("value"); - BaseRuntimeChildDefinition moneyCurrencyChild = moneyDefinition.getChildByName("currency"); Optional> valueField = moneyValueChild.getAccessor().getFirstValueOrNull(theValue); if (valueField.isPresent() && valueField.get().getValue() != null) { @@ -458,9 +482,6 @@ public abstract class BaseSearchParamExtractor implements ISearchParamExtractor } private void addQuantity_Range(String theResourceType, Set theParams, RuntimeSearchParam theSearchParam, IBase theValue) { - BaseRuntimeElementCompositeDefinition rangeDefinition = (BaseRuntimeElementCompositeDefinition) getContext().getElementDefinition("Range"); - BaseRuntimeChildDefinition rangeLowValueChild = rangeDefinition.getChildByName("low"); - BaseRuntimeChildDefinition rangeHighValueChild = rangeDefinition.getChildByName("high"); Optional low = rangeLowValueChild.getAccessor().getFirstValueOrNull(theValue); low.ifPresent(theIBase -> addQuantity_Quantity(theResourceType, theParams, theSearchParam, theIBase)); @@ -758,8 +779,8 @@ public abstract class BaseSearchParamExtractor implements ISearchParamExtractor } - private Set extractSearchParams(IBaseResource theResource, IExtractor theExtractor, RestSearchParameterTypeEnum theSearchParamType) { - Set retVal = new HashSet<>(); + private SearchParamSet extractSearchParams(IBaseResource theResource, IExtractor theExtractor, RestSearchParameterTypeEnum theSearchParamType) { + SearchParamSet retVal = new SearchParamSet<>(); Collection searchParams = getSearchParams(theResource); for (RuntimeSearchParam nextSpDef : searchParams) { @@ -879,14 +900,13 @@ public abstract class BaseSearchParamExtractor implements ISearchParamExtractor List get() throws FHIRException; - } @FunctionalInterface private interface IExtractor { - void extract(Set theParams, RuntimeSearchParam theSearchParam, IBase theValue, String thePath); + void extract(SearchParamSet theParams, RuntimeSearchParam theSearchParam, IBase theValue, String thePath); } diff --git a/hapi-fhir-jpaserver-searchparam/src/main/java/ca/uhn/fhir/jpa/searchparam/extractor/ISearchParamExtractor.java b/hapi-fhir-jpaserver-searchparam/src/main/java/ca/uhn/fhir/jpa/searchparam/extractor/ISearchParamExtractor.java index 16e3d9b9d3e..7d929d53ef5 100644 --- a/hapi-fhir-jpaserver-searchparam/src/main/java/ca/uhn/fhir/jpa/searchparam/extractor/ISearchParamExtractor.java +++ b/hapi-fhir-jpaserver-searchparam/src/main/java/ca/uhn/fhir/jpa/searchparam/extractor/ISearchParamExtractor.java @@ -4,8 +4,7 @@ import ca.uhn.fhir.context.RuntimeSearchParam; import ca.uhn.fhir.jpa.model.entity.*; import org.hl7.fhir.instance.model.api.IBaseResource; -import java.util.List; -import java.util.Set; +import java.util.*; /* * #%L @@ -29,21 +28,44 @@ import java.util.Set; public interface ISearchParamExtractor { - Set extractSearchParamCoords(IBaseResource theResource); + SearchParamSet extractSearchParamCoords(IBaseResource theResource); - Set extractSearchParamDates(IBaseResource theResource); + SearchParamSet extractSearchParamDates(IBaseResource theResource); - Set extractSearchParamNumber(IBaseResource theResource); + SearchParamSet extractSearchParamNumber(IBaseResource theResource); - Set extractSearchParamQuantity(IBaseResource theResource); + SearchParamSet extractSearchParamQuantity(IBaseResource theResource); - Set extractSearchParamStrings(IBaseResource theResource); + SearchParamSet extractSearchParamStrings(IBaseResource theResource); - Set extractSearchParamTokens(IBaseResource theResource); + SearchParamSet extractSearchParamTokens(IBaseResource theResource); - Set extractSearchParamUri(IBaseResource theResource); + SearchParamSet extractSearchParamUri(IBaseResource theResource); List extractResourceLinks(IBaseResource theResource, RuntimeSearchParam theNextSpDef); String[] split(String theExpression); + + + class SearchParamSet extends HashSet { + + private List myWarnings; + + public void addWarning(String theWarning) { + if (myWarnings == null) { + myWarnings = new ArrayList<>(); + } + myWarnings.add(theWarning); + } + + List getWarnings() { + if (myWarnings == null) { + return Collections.emptyList(); + } + return myWarnings; + } + + } + + }