diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/support/IValidationSupport.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/support/IValidationSupport.java index bb71db4d117..50f9e891d9c 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/support/IValidationSupport.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/support/IValidationSupport.java @@ -29,6 +29,7 @@ import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.builder.EqualsBuilder; import org.apache.commons.lang3.builder.HashCodeBuilder; import org.hl7.fhir.instance.model.api.IBase; +import org.hl7.fhir.instance.model.api.IBaseCoding; import org.hl7.fhir.instance.model.api.IBaseParameters; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.instance.model.api.IPrimitiveType; @@ -806,21 +807,43 @@ public interface IValidationSupport { class TranslateCodeRequest { - private final String mySourceSystemUrl; - private final String mySourceCode; + private List myCodings; private final String myTargetSystemUrl; - private final int myHashCode; + private final String myConceptMapUrl; + private final String myConceptMapVersion; + private final String mySourceValueSetUrl; + private final String myTargetValueSetUrl; + private final Long myResourcePid; + private final boolean myReverse; - public TranslateCodeRequest(String theSourceSystemUrl, String theSourceCode, String theTargetSystemUrl) { - mySourceSystemUrl = theSourceSystemUrl; - mySourceCode = theSourceCode; + public TranslateCodeRequest(List theCodings, String theTargetSystemUrl) { + myCodings = theCodings; myTargetSystemUrl = theTargetSystemUrl; + myConceptMapUrl = null; + myConceptMapVersion = null; + mySourceValueSetUrl = null; + myTargetValueSetUrl = null; + myResourcePid = null; + myReverse = false; + } - myHashCode = new HashCodeBuilder(17, 37) - .append(mySourceSystemUrl) - .append(mySourceCode) - .append(myTargetSystemUrl) - .toHashCode(); + public TranslateCodeRequest( + List theCodings, + String theTargetSystemUrl, + String theConceptMapUrl, + String theConceptMapVersion, + String theSourceValueSetUrl, + String theTargetValueSetUrl, + Long theResourcePid, + boolean theReverse) { + myCodings = theCodings; + myTargetSystemUrl = theTargetSystemUrl; + myConceptMapUrl = theConceptMapUrl; + myConceptMapVersion = theConceptMapVersion; + mySourceValueSetUrl = theSourceValueSetUrl; + myTargetValueSetUrl = theTargetValueSetUrl; + myResourcePid = theResourcePid; + myReverse = theReverse; } @Override @@ -836,28 +859,62 @@ public interface IValidationSupport { TranslateCodeRequest that = (TranslateCodeRequest) theO; return new EqualsBuilder() - .append(mySourceSystemUrl, that.mySourceSystemUrl) - .append(mySourceCode, that.mySourceCode) + .append(myCodings, that.myCodings) .append(myTargetSystemUrl, that.myTargetSystemUrl) + .append(myConceptMapUrl, that.myConceptMapUrl) + .append(myConceptMapVersion, that.myConceptMapVersion) + .append(mySourceValueSetUrl, that.mySourceValueSetUrl) + .append(myTargetValueSetUrl, that.myTargetValueSetUrl) + .append(myResourcePid, that.myResourcePid) + .append(myReverse, that.myReverse) .isEquals(); } @Override public int hashCode() { - return myHashCode; + return new HashCodeBuilder(17, 37) + .append(myCodings) + .append(myTargetSystemUrl) + .append(myConceptMapUrl) + .append(myConceptMapVersion) + .append(mySourceValueSetUrl) + .append(myTargetValueSetUrl) + .append(myResourcePid) + .append(myReverse) + .toHashCode(); } - public String getSourceSystemUrl() { - return mySourceSystemUrl; - } - - public String getSourceCode() { - return mySourceCode; + public List getCodings() { + return myCodings; } public String getTargetSystemUrl() { return myTargetSystemUrl; } + + public String getConceptMapUrl() { + return myConceptMapUrl; + } + + public String getConceptMapVersion() { + return myConceptMapVersion; + } + + public String getSourceValueSetUrl() { + return mySourceValueSetUrl; + } + + public String getTargetValueSetUrl() { + return myTargetValueSetUrl; + } + + public Long getResourcePid() { + return myResourcePid; + } + + public boolean isReverse() { + return myReverse; + } } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/i18n/Msg.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/i18n/Msg.java index 20be80ab082..fc93cb66b2b 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/i18n/Msg.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/i18n/Msg.java @@ -25,7 +25,7 @@ public final class Msg { /** * IMPORTANT: Please update the following comment after you add a new code - * Last code value: 2076 + * Last code value: 2078 */ private Msg() {} diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/api/Constants.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/api/Constants.java index c66c2b8296a..17d43e7a57f 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/api/Constants.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/api/Constants.java @@ -22,12 +22,7 @@ package ca.uhn.fhir.rest.api; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; +import java.util.*; import static org.apache.commons.lang3.StringUtils.defaultIfBlank; @@ -217,6 +212,7 @@ public class Constants { public static final String PARAMQUALIFIER_STRING_EXACT = ":exact"; public static final String PARAMQUALIFIER_TOKEN_TEXT = ":text"; public static final String PARAMQUALIFIER_MDM = ":mdm"; + public static final String PARAMQUALIFIER_NICKNAME = ":nickname"; public static final String PARAMQUALIFIER_TOKEN_OF_TYPE = ":of-type"; public static final String PARAMQUALIFIER_TOKEN_NOT = ":not"; public static final int STATUS_HTTP_200_OK = 200; diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/StringParam.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/StringParam.java index 3ce8b50bd46..b7b54e3f3cf 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/StringParam.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/StringParam.java @@ -21,9 +21,11 @@ package ca.uhn.fhir.rest.param; */ import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.i18n.Msg; import ca.uhn.fhir.model.api.IQueryParameterType; import ca.uhn.fhir.model.primitive.StringDt; import ca.uhn.fhir.rest.api.Constants; +import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.builder.EqualsBuilder; import org.apache.commons.lang3.builder.HashCodeBuilder; @@ -38,7 +40,8 @@ public class StringParam extends BaseParam implements IQueryParameterType { private boolean myExact; private String myValue; - private Boolean myMdmExpand; + private Boolean myNicknameExpand; + /** * Constructor @@ -77,12 +80,12 @@ public class StringParam extends BaseParam implements IQueryParameterType { return ParameterUtil.escape(myValue); } - public boolean isMdmExpand() { - return myMdmExpand != null && myMdmExpand; + public boolean isNicknameExpand() { + return myNicknameExpand != null && myNicknameExpand; } - public StringParam setMdmExpand(boolean theMdmExpand) { - myMdmExpand = theMdmExpand; + public StringParam setNicknameExpand(boolean theNicknameExpand) { + myNicknameExpand = theNicknameExpand; return this; } @@ -98,6 +101,15 @@ public class StringParam extends BaseParam implements IQueryParameterType { @Override void doSetValueAsQueryToken(FhirContext theContext, String theParamName, String theQualifier, String theValue) { + if (Constants.PARAMQUALIFIER_NICKNAME.equals(theQualifier)) { + if ("name".equals(theParamName) || "given".equals(theParamName)) { + myNicknameExpand = true; + theQualifier = ""; + } else { + throw new InvalidRequestException(Msg.code(2077) + "Modifier " + Constants.PARAMQUALIFIER_NICKNAME + " may only be used with 'name' and 'given' search parameters"); + } + } + if (Constants.PARAMQUALIFIER_STRING_EXACT.equals(theQualifier)) { setExact(true); } else { diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/SearchParameterUtil.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/SearchParameterUtil.java index 024e948ae47..64376924446 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/SearchParameterUtil.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/SearchParameterUtil.java @@ -58,8 +58,8 @@ public class SearchParameterUtil { * 1. Attempt to find one called 'patient' * 2. If that fails, find one called 'subject' * 3. If that fails, find find by Patient Compartment. - * 3.1 If that returns >1 result, throw an error - * 3.2 If that returns 1 result, return it + * 3.1 If that returns >1 result, throw an error + * 3.2 If that returns 1 result, return it */ public static Optional getOnlyPatientSearchParamForResourceType(FhirContext theFhirContext, String theResourceType) { RuntimeSearchParam myPatientSearchParam = null; @@ -115,7 +115,7 @@ public class SearchParameterUtil { */ public static boolean isResourceTypeInPatientCompartment(FhirContext theFhirContext, String theResourceType) { RuntimeResourceDefinition runtimeResourceDefinition = theFhirContext.getResourceDefinition(theResourceType); - return getAllPatientCompartmentRuntimeSearchParams(runtimeResourceDefinition).size() > 0; + return getAllPatientCompartmentRuntimeSearchParams(runtimeResourceDefinition).size() > 0; } @@ -147,4 +147,15 @@ public class SearchParameterUtil { .map(t -> t.getValueAsString()) .orElse(null); } + + public static String stripModifier(String theSearchParam) { + String retval; + int colonIndex = theSearchParam.indexOf(":"); + if (colonIndex == -1) { + retval = theSearchParam; + } else { + retval = theSearchParam.substring(0, colonIndex); + } + return retval; + } } diff --git a/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_0_0/3442-remote-terminology-translate.yaml b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_0_0/3442-remote-terminology-translate.yaml new file mode 100644 index 00000000000..67ad18519e6 --- /dev/null +++ b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_0_0/3442-remote-terminology-translate.yaml @@ -0,0 +1,3 @@ +type: add +issue: 3442 +title: "Provided a Remote Terminology Service implementation for the $translate operation." diff --git a/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_0_0/3534-add-index-on-hfj-resource-table-indexing-multiple-columns.yaml b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_0_0/3534-add-index-on-hfj-resource-table-indexing-multiple-columns.yaml new file mode 100644 index 00000000000..ad6c4662e7a --- /dev/null +++ b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_0_0/3534-add-index-on-hfj-resource-table-indexing-multiple-columns.yaml @@ -0,0 +1,3 @@ +type: add +issue: 3534 +title: "Added a new multi-column index on the HFJ_RESOURCE table indexing the columns RES_TYPE, RES_DELETED_AT, RES_UPDATED, PARTITION_ID and RES_ID and removed the existing single-column index on the RES_TYPE column. This new index will improve the performance of the $reindex operation and will be useful for some other queries a well." diff --git a/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_0_0/3534-remove-migration-timeout.yaml b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_0_0/3534-remove-migration-timeout.yaml new file mode 100644 index 00000000000..782d874f68c --- /dev/null +++ b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_0_0/3534-remove-migration-timeout.yaml @@ -0,0 +1,5 @@ +type: change +issue: 3534 +title: "Ensure that migration steps do not timeout. + Adding an index, and other migration steps can take exceed the default connection timeout. + This has been changed - migration steps now have no timeout and will run until completion." diff --git a/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_0_0/3563-nickname-matching.yaml b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_0_0/3563-nickname-matching.yaml new file mode 100644 index 00000000000..8d21b04e4b2 --- /dev/null +++ b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_0_0/3563-nickname-matching.yaml @@ -0,0 +1,5 @@ +type: add +issue: 3563 +title: "Added search parameter modifier :nickname that can be used with 'name' or 'given' search parameters. +E.g. ?Patient?given:nickname=Kenny will match a patient with the given name Kenneth. Also added MDM matching +algorithm named NICKNAME that matches based this." diff --git a/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_0_0/3569-extend-string-index.yaml b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_0_0/3569-extend-string-index.yaml new file mode 100644 index 00000000000..9d71347f023 --- /dev/null +++ b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_0_0/3569-extend-string-index.yaml @@ -0,0 +1,4 @@ +--- +type: change +issue: 3569 +title: "The normalized and exact string database indexes have been changed to provide faster string searches." diff --git a/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/docs/server_jpa/search.md b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/docs/server_jpa/search.md index 44f8fc19b8f..d06abec758d 100644 --- a/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/docs/server_jpa/search.md +++ b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/docs/server_jpa/search.md @@ -1,5 +1,7 @@ # JPA Server Search +## Limitations + The HAPI FHIR JPA Server fully implements most [FHIR search](https://www.hl7.org/fhir/search.html) operations for most versions of FHIR. However, there are some known limitations of the current implementation. Here is a partial list of search functionality that is not currently supported in HAPI FHIR: ### Chains within _has diff --git a/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/docs/server_jpa_mdm/mdm_rules.md b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/docs/server_jpa_mdm/mdm_rules.md index 5a2b7167ad4..bb328aca0ed 100644 --- a/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/docs/server_jpa_mdm/mdm_rules.md +++ b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/docs/server_jpa_mdm/mdm_rules.md @@ -406,6 +406,14 @@ The following algorithms are currently supported: Match names as strings in any order John Henry = John HENRY when exact=false, John Henry != Henry John + + + NICKNAME + matcher + + True if one name is a nickname of the other + + Ken = Kenneth, Kenny = Ken. Allen != Allan. IDENTIFIER diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/config/JpaConfig.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/config/JpaConfig.java index b1f5f45caaf..316d3109d18 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/config/JpaConfig.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/config/JpaConfig.java @@ -128,6 +128,7 @@ import ca.uhn.fhir.jpa.search.warm.CacheWarmingSvcImpl; import ca.uhn.fhir.jpa.search.warm.ICacheWarmingSvc; import ca.uhn.fhir.jpa.searchparam.config.SearchParamConfig; import ca.uhn.fhir.jpa.searchparam.extractor.IResourceLinkResolver; +import ca.uhn.fhir.jpa.searchparam.nickname.NicknameInterceptor; import ca.uhn.fhir.jpa.searchparam.registry.ISearchParamProvider; import ca.uhn.fhir.jpa.sp.ISearchParamPresenceSvc; import ca.uhn.fhir.jpa.sp.SearchParamPresenceSvcImpl; @@ -164,6 +165,7 @@ import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler; import org.springframework.scheduling.concurrent.ScheduledExecutorFactoryBean; import javax.annotation.Nullable; +import java.io.IOException; import java.util.Date; /* @@ -827,4 +829,10 @@ public class JpaConfig { public MemberMatcherR4Helper memberMatcherR4Helper(FhirContext theFhirContext) { return new MemberMatcherR4Helper(theFhirContext); } + + @Lazy + @Bean + public NicknameInterceptor nicknameInterceptor() throws IOException { + return new NicknameInterceptor(); + } } diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/FulltextSearchSvcImpl.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/FulltextSearchSvcImpl.java index a7372ef1ac5..9a5909023be 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/FulltextSearchSvcImpl.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/FulltextSearchSvcImpl.java @@ -36,6 +36,8 @@ import ca.uhn.fhir.jpa.model.search.ExtendedLuceneIndexData; import ca.uhn.fhir.jpa.search.autocomplete.ValueSetAutocompleteOptions; import ca.uhn.fhir.jpa.search.autocomplete.ValueSetAutocompleteSearch; import ca.uhn.fhir.jpa.search.builder.ISearchQueryExecutor; +import ca.uhn.fhir.jpa.search.builder.SearchQueryExecutors; +import ca.uhn.fhir.jpa.search.builder.sql.SearchQueryExecutor; import ca.uhn.fhir.jpa.searchparam.SearchParameterMap; import ca.uhn.fhir.jpa.searchparam.extractor.ISearchParamExtractor; import ca.uhn.fhir.jpa.searchparam.extractor.ResourceIndexedSearchParams; @@ -47,7 +49,9 @@ import ca.uhn.fhir.rest.server.util.ISearchParamRegistry; import ca.uhn.fhir.rest.server.util.ResourceSearchParams; import org.hibernate.search.backend.elasticsearch.ElasticsearchExtension; import org.hibernate.search.engine.search.query.SearchScroll; +import org.hibernate.search.engine.search.query.dsl.SearchQueryOptionsStep; import org.hibernate.search.mapper.orm.Search; +import org.hibernate.search.mapper.orm.search.loading.dsl.SearchLoadingOptionsStep; import org.hibernate.search.mapper.orm.session.SearchSession; import org.hibernate.search.mapper.orm.work.SearchIndexingPlan; import org.hibernate.search.util.common.SearchException; @@ -132,14 +136,34 @@ public class FulltextSearchSvcImpl implements IFulltextSearchSvc { private ISearchQueryExecutor doSearch(String theResourceType, SearchParameterMap theParams, ResourcePersistentId theReferencingPid) { // keep this in sync with supportsSomeOf(); - SearchSession session = getSearchSession(); + if (theParams.getOffset() != null && theParams.getOffset() != 0) { + // perform an offset search instead of a scroll one, which doesn't allow for offset + List queryFetchResult = getSearchQueryOptionsStep( + theResourceType, theParams, theReferencingPid).fetchHits(theParams.getOffset(), theParams.getCount()); + // indicate param was already processed, otherwise queries DB to process it + theParams.setOffset(null); + return SearchQueryExecutors.from(queryFetchResult); + } + SearchScroll esResult = getSearchScroll(theResourceType, theParams, theReferencingPid); + return new SearchScrollQueryExecutorAdaptor(esResult); + } + + + private SearchScroll getSearchScroll(String theResourceType, SearchParameterMap theParams, ResourcePersistentId theReferencingPid) { int scrollSize = 50; if (theParams.getCount()!=null) { scrollSize = theParams.getCount(); } - SearchScroll esResult = session.search(ResourceTable.class) + return getSearchQueryOptionsStep(theResourceType, theParams, theReferencingPid).scroll(scrollSize); + } + + + private SearchQueryOptionsStep getSearchQueryOptionsStep( + String theResourceType, SearchParameterMap theParams, ResourcePersistentId theReferencingPid) { + + return getSearchSession().search(ResourceTable.class) // The document id is the PK which is pid. We use this instead of _myId to avoid fetching the doc body. .select( // adapt the String docRef.id() to the Long that it really is. @@ -188,11 +212,11 @@ public class FulltextSearchSvcImpl implements IFulltextSearchSvc { //DROP EARLY HERE IF BOOL IS EMPTY? }) - ).scroll(scrollSize); - - return new SearchScrollQueryExecutorAdaptor(esResult); + ); } + + @Nonnull private SearchSession getSearchSession() { return Search.session(myEntityManager); @@ -314,4 +338,14 @@ public class FulltextSearchSvcImpl implements IFulltextSearchSvc { .map(p -> p.toResource(parser)) .collect(Collectors.toList()); } + + + + @Override + public long count(String theResourceName, SearchParameterMap theParams) { + SearchQueryOptionsStep queryOptionsStep = + getSearchQueryOptionsStep(theResourceName, theParams, null); + + return queryOptionsStep.fetchTotalHitCount(); + } } diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/IFulltextSearchSvc.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/IFulltextSearchSvc.java index 4540fed2dd0..29a27d25f79 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/IFulltextSearchSvc.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/IFulltextSearchSvc.java @@ -45,6 +45,7 @@ public interface IFulltextSearchSvc { */ List search(String theResourceName, SearchParameterMap theParams); + /** * Query the index for a scrollable iterator of results. * No max size to the result iterator. @@ -90,4 +91,8 @@ public interface IFulltextSearchSvc { */ List getResources(Collection thePids); + /** + * Returns accurate hit count + */ + long count(String theResourceName, SearchParameterMap theParams); } diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/ISearchBuilder.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/ISearchBuilder.java index 8fe603786ca..4289be90b25 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/ISearchBuilder.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/ISearchBuilder.java @@ -41,7 +41,7 @@ public interface ISearchBuilder { IResultIterator createQuery(SearchParameterMap theParams, SearchRuntimeDetails theSearchRuntime, RequestDetails theRequest, @Nonnull RequestPartitionId theRequestPartitionId); - Iterator createCountQuery(SearchParameterMap theParams, String theSearchUuid, RequestDetails theRequest, RequestPartitionId theRequestPartitionId); + Long createCountQuery(SearchParameterMap theParams, String theSearchUuid, RequestDetails theRequest, RequestPartitionId theRequestPartitionId); void setMaxResultsToFetch(Integer theMaxResultsToFetch); diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/LegacySearchBuilder.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/LegacySearchBuilder.java index ce960bd2250..203f3adb442 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/LegacySearchBuilder.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/LegacySearchBuilder.java @@ -227,14 +227,14 @@ public class LegacySearchBuilder implements ISearchBuilder { } @Override - public Iterator createCountQuery(SearchParameterMap theParams, String theSearchUuid, RequestDetails theRequest, @Nonnull RequestPartitionId theRequestPartitionId) { + public Long createCountQuery(SearchParameterMap theParams, String theSearchUuid, RequestDetails theRequest, @Nonnull RequestPartitionId theRequestPartitionId) { assert theRequestPartitionId != null; assert TransactionSynchronizationManager.isActualTransactionActive(); init(theParams, theSearchUuid, theRequestPartitionId); List> queries = createQuery(null, null, null, true, theRequest, null); - return new CountQueryIterator(queries.get(0)); + return new CountQueryIterator(queries.get(0)).next(); } /** diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoConceptMapDstu3.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoConceptMapDstu3.java index ac719110f93..a3a4a6350bb 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoConceptMapDstu3.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoConceptMapDstu3.java @@ -20,6 +20,7 @@ package ca.uhn.fhir.jpa.dao.dstu3; * #L% */ +import ca.uhn.fhir.context.support.IValidationSupport; import ca.uhn.fhir.i18n.Msg; import ca.uhn.fhir.context.support.TranslateConceptResults; import ca.uhn.fhir.jpa.api.dao.IFhirResourceDaoConceptMap; @@ -38,19 +39,19 @@ import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.instance.model.api.IBaseResource; import org.springframework.beans.factory.annotation.Autowired; +import java.util.Collections; import java.util.Date; public class FhirResourceDaoConceptMapDstu3 extends BaseHapiFhirResourceDao implements IFhirResourceDaoConceptMap { @Autowired private ITermConceptMappingSvc myTermConceptMappingSvc; + @Autowired + private IValidationSupport myValidationSupport; @Override public TranslateConceptResults translate(TranslationRequest theTranslationRequest, RequestDetails theRequestDetails) { - if (theTranslationRequest.hasReverse() && theTranslationRequest.getReverseAsBoolean()) { - return myTermConceptMappingSvc.translateWithReverse(theTranslationRequest); - } - - return myTermConceptMappingSvc.translate(theTranslationRequest); + IValidationSupport.TranslateCodeRequest translateCodeRequest = theTranslationRequest.asTranslateCodeRequest(); + return myValidationSupport.translateConcept(translateCodeRequest); } diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoConceptMapR4.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoConceptMapR4.java index 87ec435bd88..a2ea81c5061 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoConceptMapR4.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoConceptMapR4.java @@ -20,6 +20,7 @@ package ca.uhn.fhir.jpa.dao.r4; * #L% */ +import ca.uhn.fhir.context.support.IValidationSupport; import ca.uhn.fhir.context.support.TranslateConceptResults; import ca.uhn.fhir.jpa.api.dao.IFhirResourceDaoConceptMap; import ca.uhn.fhir.jpa.api.model.TranslationRequest; @@ -29,23 +30,28 @@ import ca.uhn.fhir.jpa.model.entity.ResourceTable; import ca.uhn.fhir.jpa.term.api.ITermConceptMappingSvc; import ca.uhn.fhir.rest.api.server.RequestDetails; import ca.uhn.fhir.rest.api.server.storage.TransactionDetails; +import org.hl7.fhir.instance.model.api.IBase; +import org.hl7.fhir.instance.model.api.IBaseCoding; import org.hl7.fhir.instance.model.api.IBaseResource; +import org.hl7.fhir.r4.model.CodeableConcept; +import org.hl7.fhir.r4.model.Coding; import org.hl7.fhir.r4.model.ConceptMap; import org.springframework.beans.factory.annotation.Autowired; +import java.util.Collections; import java.util.Date; +import java.util.List; public class FhirResourceDaoConceptMapR4 extends BaseHapiFhirResourceDao implements IFhirResourceDaoConceptMap { @Autowired private ITermConceptMappingSvc myTermConceptMappingSvc; + @Autowired + private IValidationSupport myValidationSupport; @Override public TranslateConceptResults translate(TranslationRequest theTranslationRequest, RequestDetails theRequestDetails) { - if (theTranslationRequest.hasReverse() && theTranslationRequest.getReverseAsBoolean()) { - return myTermConceptMappingSvc.translateWithReverse(theTranslationRequest); - } - - return myTermConceptMappingSvc.translate(theTranslationRequest); + IValidationSupport.TranslateCodeRequest translateCodeRequest = theTranslationRequest.asTranslateCodeRequest(); + return myValidationSupport.translateConcept(translateCodeRequest); } @Override diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/r5/FhirResourceDaoConceptMapR5.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/r5/FhirResourceDaoConceptMapR5.java index d0091a46e65..e96528ff8a5 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/r5/FhirResourceDaoConceptMapR5.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/r5/FhirResourceDaoConceptMapR5.java @@ -20,6 +20,7 @@ package ca.uhn.fhir.jpa.dao.r5; * #L% */ +import ca.uhn.fhir.context.support.IValidationSupport; import ca.uhn.fhir.context.support.TranslateConceptResults; import ca.uhn.fhir.jpa.api.dao.IFhirResourceDaoConceptMap; import ca.uhn.fhir.jpa.api.model.TranslationRequest; @@ -35,19 +36,19 @@ import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.r5.model.ConceptMap; import org.springframework.beans.factory.annotation.Autowired; +import java.util.Collections; import java.util.Date; public class FhirResourceDaoConceptMapR5 extends BaseHapiFhirResourceDao implements IFhirResourceDaoConceptMap { @Autowired private ITermConceptMappingSvc myTermConceptMappingSvc; + @Autowired + private IValidationSupport myValidationSupport; @Override public TranslateConceptResults translate(TranslationRequest theTranslationRequest, RequestDetails theRequestDetails) { - if (theTranslationRequest.hasReverse() && theTranslationRequest.getReverseAsBoolean()) { - return myTermConceptMappingSvc.translateWithReverse(theTranslationRequest); - } - - return myTermConceptMappingSvc.translate(theTranslationRequest); + IValidationSupport.TranslateCodeRequest translateCodeRequest = theTranslationRequest.asTranslateCodeRequest(); + return myValidationSupport.translateConcept(translateCodeRequest); } @Override diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/search/ExtendedLuceneSearchBuilder.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/search/ExtendedLuceneSearchBuilder.java index 243a5c89c18..28e31abd797 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/search/ExtendedLuceneSearchBuilder.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/search/ExtendedLuceneSearchBuilder.java @@ -102,6 +102,7 @@ public class ExtendedLuceneSearchBuilder { case EMPTY_MODIFIER: return true; case Constants.PARAMQUALIFIER_MDM: + case Constants.PARAMQUALIFIER_NICKNAME: default: return false; } diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/interceptor/MdmSearchExpandingInterceptor.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/interceptor/MdmSearchExpandingInterceptor.java index 4c1319a6a43..abb385db66e 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/interceptor/MdmSearchExpandingInterceptor.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/interceptor/MdmSearchExpandingInterceptor.java @@ -30,13 +30,11 @@ import ca.uhn.fhir.mdm.log.Logs; import ca.uhn.fhir.model.api.IQueryParameterType; import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.rest.param.ReferenceParam; -import ca.uhn.fhir.rest.param.StringParam; import ca.uhn.fhir.rest.param.TokenParam; import org.hl7.fhir.instance.model.api.IIdType; import org.slf4j.Logger; import org.springframework.beans.factory.annotation.Autowired; -import java.lang.reflect.Constructor; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -92,7 +90,7 @@ public class MdmSearchExpandingInterceptor { // If we failed, attempt to expand as a golden resource if (expandedResourceIds.isEmpty()) { - expandedResourceIds = myMdmLinkExpandSvc.expandMdmByGoldenResourceId(new IdDt(refParam.getValue())); + expandedResourceIds = myMdmLinkExpandSvc.expandMdmByGoldenResourceId(new IdDt(refParam.getValue())); } //Rebuild the search param list. @@ -104,8 +102,7 @@ public class MdmSearchExpandingInterceptor { .forEach(toAdd::add); } } - } - else if (theParamName.equalsIgnoreCase("_id")) { + } else if (theParamName.equalsIgnoreCase("_id")) { expandIdParameter(iQueryParameterType, toAdd, toRemove); } } @@ -117,6 +114,7 @@ public class MdmSearchExpandingInterceptor { /** * Expands out the provided _id parameter into all the various * ids of linked resources. + * * @param theIdParameter * @param theAddList * @param theRemoveList @@ -130,29 +128,21 @@ public class MdmSearchExpandingInterceptor { IIdType id; Creator creator; boolean mdmExpand = false; - if (theIdParameter instanceof StringParam) { - StringParam param = (StringParam) theIdParameter; - mdmExpand = param.isMdmExpand(); - id = new IdDt(param.getValue()); - creator = StringParam::new; - } - else if (theIdParameter instanceof TokenParam) { + if (theIdParameter instanceof TokenParam) { TokenParam param = (TokenParam) theIdParameter; mdmExpand = param.isMdmExpand(); id = new IdDt(param.getValue()); creator = TokenParam::new; - } - else { + } else { creator = null; id = null; } - if (id == null || creator == null) { + if (id == null) { // in case the _id paramter type is different from the above ourLog.warn("_id parameter of incorrect type. Expected StringParam or TokenParam, but got {}. No expansion will be done!", theIdParameter.getClass().getSimpleName()); - } - else if (mdmExpand) { + } else if (mdmExpand) { ourLog.debug("_id parameter must be expanded out from: {}", id.getValue()); Set expandedResourceIds = myMdmLinkExpandSvc.expandMdmBySourceResourceId(id); diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/HapiFhirJpaMigrationTasks.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/HapiFhirJpaMigrationTasks.java index a5a14f95b2d..894477f773e 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/HapiFhirJpaMigrationTasks.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/migrate/tasks/HapiFhirJpaMigrationTasks.java @@ -295,6 +295,46 @@ public class HapiFhirJpaMigrationTasks extends BaseMigrationTasks { version .onTable("HFJ_BLK_EXPORT_JOB").modifyColumn("20220423.1", "EXP_TIME").nullable().withType(ColumnTypeEnum.DATE_TIMESTAMP); + // New Index on HFJ_RESOURCE for $reindex Operation - hapi-fhir #3534 + { + version.onTable("HFJ_RESOURCE") + .addIndex("20220425.1", "IDX_RES_TYPE_DEL_UPDATED") + .unique(false) + .online(true) + .withColumns("RES_TYPE", "RES_DELETED_AT", "RES_UPDATED", "PARTITION_ID", "RES_ID"); + + // Drop existing Index on HFJ_RESOURCE.RES_TYPE since the new Index will meet the overall Index Demand + version + .onTable("HFJ_RESOURCE") + .dropIndexOnline("20220425.2", "IDX_RES_TYPE"); + } + + /** + * Update string indexing + * @see ca.uhn.fhir.jpa.search.builder.predicate.StringPredicateBuilder + * @see ResourceIndexedSearchParamString + */ + { + Builder.BuilderWithTableName tokenTable = version.onTable("HFJ_SPIDX_STRING"); + + // add res_id, and partition_id so queries are covered without row-reads. + tokenTable + .addIndex("20220428.1", "IDX_SP_STRING_HASH_NRM_V2") + .unique(false) + .online(true) + .withColumns("HASH_NORM_PREFIX", "SP_VALUE_NORMALIZED", "RES_ID", "PARTITION_ID"); + tokenTable.dropIndexOnline("20220428.2", "IDX_SP_STRING_HASH_NRM"); + + tokenTable + .addIndex("20220428.3", "IDX_SP_STRING_HASH_EXCT_V2") + .unique(false) + .online(true) + .withColumns("HASH_EXACT", "RES_ID", "PARTITION_ID"); + tokenTable.dropIndexOnline("20220428.4", "IDX_SP_STRING_HASH_EXCT"); + + // we will drop the updated column. Start with the index. + tokenTable.dropIndexOnline("20220428.5", "IDX_SP_STRING_UPDATED"); + } } /** diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/SearchCoordinatorSvcImpl.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/SearchCoordinatorSvcImpl.java index 5125640c4f7..be70b629ee5 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/SearchCoordinatorSvcImpl.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/SearchCoordinatorSvcImpl.java @@ -508,6 +508,7 @@ public class SearchCoordinatorSvcImpl implements ISearchCoordinatorSvc { return candidate.orElse(null); } + private IBundleProvider executeQuery(String theResourceType, SearchParameterMap theParams, RequestDetails theRequestDetails, String theSearchUuid, ISearchBuilder theSb, Integer theLoadSynchronousUpTo, RequestPartitionId theRequestPartitionId) { SearchRuntimeDetails searchRuntimeDetails = new SearchRuntimeDetails(theRequestDetails, theSearchUuid); searchRuntimeDetails.setLoadSynchronous(true); @@ -533,12 +534,11 @@ public class SearchCoordinatorSvcImpl implements ISearchCoordinatorSvc { List> contentAndTerms = theParams.get(Constants.PARAM_CONTENT); List> textAndTerms = theParams.get(Constants.PARAM_TEXT); - Iterator countIterator = theSb.createCountQuery(theParams, theSearchUuid, theRequestDetails, theRequestPartitionId); + count = theSb.createCountQuery(theParams, theSearchUuid, theRequestDetails, theRequestPartitionId); if (contentAndTerms != null) theParams.put(Constants.PARAM_CONTENT, contentAndTerms); if (textAndTerms != null) theParams.put(Constants.PARAM_TEXT, textAndTerms); - count = countIterator.next(); ourLog.trace("Got count {}", count); } @@ -1233,8 +1233,8 @@ public class SearchCoordinatorSvcImpl implements ISearchCoordinatorSvc { * we will have to clone those parameters here so that * the "correct" params are used in createQuery below */ - Iterator countIterator = sb.createCountQuery(myParams.clone(), mySearch.getUuid(), myRequest, myRequestPartitionId); - Long count = countIterator.hasNext() ? countIterator.next() : 0L; + Long count = sb.createCountQuery(myParams.clone(), mySearch.getUuid(), myRequest, myRequestPartitionId); + ourLog.trace("Got count {}", count); TransactionTemplate txTemplate = new TransactionTemplate(myManagedTxManager); diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/builder/SearchBuilder.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/builder/SearchBuilder.java index c9a803245e6..ab07f3b0b47 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/builder/SearchBuilder.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/builder/SearchBuilder.java @@ -75,6 +75,7 @@ import ca.uhn.fhir.model.valueset.BundleEntrySearchModeEnum; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.RestSearchParameterTypeEnum; import ca.uhn.fhir.rest.api.SearchContainedModeEnum; +import ca.uhn.fhir.rest.api.SearchTotalModeEnum; import ca.uhn.fhir.rest.api.SortOrderEnum; import ca.uhn.fhir.rest.api.SortSpec; import ca.uhn.fhir.rest.api.server.IPreResourceAccessDetails; @@ -97,6 +98,7 @@ import com.google.common.collect.Streams; import com.healthmarketscience.sqlbuilder.Condition; import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.math.NumberUtils; +import org.apache.jena.sparql.engine.QueryIterator; import org.hl7.fhir.instance.model.api.IAnyResource; import org.hl7.fhir.instance.model.api.IBaseResource; import org.slf4j.Logger; @@ -119,6 +121,7 @@ import javax.persistence.criteria.From; import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; @@ -271,18 +274,24 @@ public class SearchBuilder implements ISearchBuilder { @SuppressWarnings("ConstantConditions") @Override - public Iterator createCountQuery(SearchParameterMap theParams, String theSearchUuid, RequestDetails theRequest, @Nonnull RequestPartitionId theRequestPartitionId) { + public Long createCountQuery(SearchParameterMap theParams, String theSearchUuid, + RequestDetails theRequest, @Nonnull RequestPartitionId theRequestPartitionId) { + assert theRequestPartitionId != null; assert TransactionSynchronizationManager.isActualTransactionActive(); init(theParams, theSearchUuid, theRequestPartitionId); - List queries = createQuery(myParams, null, null, null, true, theRequest, null); - if (queries.isEmpty()) { - return Collections.emptyIterator(); + if (checkUseHibernateSearch()) { + long count = myFulltextSearchSvc.count(myResourceName, theParams.clone()); + return count; } - try (ISearchQueryExecutor queryExecutor = queries.get(0)) { - return Lists.newArrayList(queryExecutor.next()).iterator(); + + List queries = createQuery(theParams.clone(), null, null, null, true, theRequest, null); + if (queries.isEmpty()) { + return 0L; + } else { + return queries.get(0).next(); } } @@ -309,6 +318,7 @@ public class SearchBuilder implements ISearchBuilder { return new QueryIterator(theSearchRuntimeDetails, theRequest); } + private void init(SearchParameterMap theParams, String theSearchUuid, RequestPartitionId theRequestPartitionId) { myCriteriaBuilder = myEntityManager.getCriteriaBuilder(); myParams = theParams; @@ -316,7 +326,7 @@ public class SearchBuilder implements ISearchBuilder { myRequestPartitionId = theRequestPartitionId; } - private List createQuery(SearchParameterMap theParams, SortSpec sort, Integer theOffset, Integer theMaximumResults, boolean theCount, RequestDetails theRequest, + private List createQuery(SearchParameterMap theParams, SortSpec sort, Integer theOffset, Integer theMaximumResults, boolean theCountOnlyFlag, RequestDetails theRequest, SearchRuntimeDetails theSearchRuntimeDetails) { ArrayList queries = new ArrayList<>(); @@ -359,8 +369,6 @@ public class SearchBuilder implements ISearchBuilder { !fulltextExecutor.hasNext() || // Our hibernate search query doesn't respect partitions yet (!myPartitionSettings.isPartitioningEnabled() && - // we don't support _count=0 yet. - !theCount && // were there AND terms left? Then we still need the db. theParams.isEmpty() && // not every param is a param. :-( @@ -382,11 +390,11 @@ public class SearchBuilder implements ISearchBuilder { // We break the pids into chunks that fit in the 1k limit for jdbc bind params. // wipmb change chunk to take iterator new QueryChunker() - .chunk(Streams.stream(fulltextExecutor).collect(Collectors.toList()), t -> doCreateChunkedQueries(theParams, t, theOffset, sort, theCount, theRequest, queries)); + .chunk(Streams.stream(fulltextExecutor).collect(Collectors.toList()), t -> doCreateChunkedQueries(theParams, t, theOffset, sort, theCountOnlyFlag, theRequest, queries)); } } else { // do everything in the database. - Optional query = createChunkedQuery(theParams, sort, theOffset, theMaximumResults, theCount, theRequest, null); + Optional query = createChunkedQuery(theParams, sort, theOffset, theMaximumResults, theCountOnlyFlag, theRequest, null); query.ifPresent(queries::add); } @@ -408,7 +416,7 @@ public class SearchBuilder implements ISearchBuilder { } // TODO MB someday we'll want a query planner to figure out if we _should_ or _must_ use the ft index, not just if we can. - return fulltextEnabled && + return fulltextEnabled && myParams != null && myParams.getSearchContainedMode() == SearchContainedModeEnum.FALSE && myFulltextSearchSvc.supportsSomeOf(myParams); } @@ -506,9 +514,9 @@ public class SearchBuilder implements ISearchBuilder { } } - private Optional createChunkedQuery(SearchParameterMap theParams, SortSpec sort, Integer theOffset, Integer theMaximumResults, boolean theCount, RequestDetails theRequest, List thePidList) { + private Optional createChunkedQuery(SearchParameterMap theParams, SortSpec sort, Integer theOffset, Integer theMaximumResults, boolean theCountOnlyFlag, RequestDetails theRequest, List thePidList) { String sqlBuilderResourceName = myParams.getEverythingMode() == null ? myResourceName : null; - SearchQueryBuilder sqlBuilder = new SearchQueryBuilder(myContext, myDaoConfig.getModelConfig(), myPartitionSettings, myRequestPartitionId, sqlBuilderResourceName, mySqlBuilderFactory, myDialectProvider, theCount); + SearchQueryBuilder sqlBuilder = new SearchQueryBuilder(myContext, myDaoConfig.getModelConfig(), myPartitionSettings, myRequestPartitionId, sqlBuilderResourceName, mySqlBuilderFactory, myDialectProvider, theCountOnlyFlag); QueryStack queryStack3 = new QueryStack(theParams, myDaoConfig, myDaoConfig.getModelConfig(), myContext, sqlBuilder, mySearchParamRegistry, myPartitionSettings); if (theParams.keySet().size() > 1 || theParams.getSort() != null || theParams.keySet().contains(Constants.PARAM_HAS) || isPotentiallyContainedReferenceParameterExistsAtRoot(theParams)) { @@ -533,7 +541,7 @@ public class SearchBuilder implements ISearchBuilder { // is basically a reverse-include search. For type/Everything (as opposed to instance/Everything) // the one problem with this approach is that it doesn't catch Patients that have absolutely // nothing linked to them. So we do one additional query to make sure we catch those too. - SearchQueryBuilder fetchPidsSqlBuilder = new SearchQueryBuilder(myContext, myDaoConfig.getModelConfig(), myPartitionSettings, myRequestPartitionId, myResourceName, mySqlBuilderFactory, myDialectProvider, theCount); + SearchQueryBuilder fetchPidsSqlBuilder = new SearchQueryBuilder(myContext, myDaoConfig.getModelConfig(), myPartitionSettings, myRequestPartitionId, myResourceName, mySqlBuilderFactory, myDialectProvider, theCountOnlyFlag); GeneratedSql allTargetsSql = fetchPidsSqlBuilder.generate(theOffset, myMaxResultsToFetch); String sql = allTargetsSql.getSql(); Object[] args = allTargetsSql.getBindVariables().toArray(new Object[0]); @@ -613,7 +621,7 @@ public class SearchBuilder implements ISearchBuilder { * finds the appropriate resources) in an outer search which is then sorted */ if (sort != null) { - assert !theCount; + assert !theCountOnlyFlag; createSort(queryStack3, sort); } diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/term/TermConceptMappingSvcImpl.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/term/TermConceptMappingSvcImpl.java index 616fb6ca05a..f4efa4546b4 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/term/TermConceptMappingSvcImpl.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/term/TermConceptMappingSvcImpl.java @@ -47,6 +47,8 @@ import org.apache.commons.lang3.StringUtils; import org.hibernate.ScrollMode; import org.hibernate.ScrollableResults; import org.hl7.fhir.exceptions.FHIRException; +import org.hl7.fhir.instance.model.api.IBase; +import org.hl7.fhir.instance.model.api.IBaseCoding; import org.hl7.fhir.r4.model.BooleanType; import org.hl7.fhir.r4.model.CodeType; import org.hl7.fhir.r4.model.CodeableConcept; @@ -118,16 +120,10 @@ public class TermConceptMappingSvcImpl implements ITermConceptMappingSvc { @Override @Transactional public TranslateConceptResults translateConcept(TranslateCodeRequest theRequest) { - - CodeableConcept sourceCodeableConcept = new CodeableConcept(); - sourceCodeableConcept - .addCoding() - .setSystem(theRequest.getSourceSystemUrl()) - .setCode(theRequest.getSourceCode()); - - TranslationRequest request = new TranslationRequest(); - request.setCodeableConcept(sourceCodeableConcept); - request.setTargetSystem(new UriType(theRequest.getTargetSystemUrl())); + TranslationRequest request = TranslationRequest.fromTranslateCodeRequest(theRequest); + if (request.hasReverse() && request.getReverseAsBoolean()) { + return translateWithReverse(request); + } return translate(request); } diff --git a/hapi-fhir-jpaserver-mdm/src/main/java/ca/uhn/fhir/jpa/mdm/svc/MdmSearchParamSvc.java b/hapi-fhir-jpaserver-mdm/src/main/java/ca/uhn/fhir/jpa/mdm/svc/MdmSearchParamSvc.java index 40397dc5297..20d1eae1f20 100644 --- a/hapi-fhir-jpaserver-mdm/src/main/java/ca/uhn/fhir/jpa/mdm/svc/MdmSearchParamSvc.java +++ b/hapi-fhir-jpaserver-mdm/src/main/java/ca/uhn/fhir/jpa/mdm/svc/MdmSearchParamSvc.java @@ -31,6 +31,7 @@ import ca.uhn.fhir.jpa.searchparam.MatchUrlService; import ca.uhn.fhir.jpa.searchparam.SearchParameterMap; import ca.uhn.fhir.jpa.searchparam.extractor.SearchParamExtractorService; import ca.uhn.fhir.rest.server.util.ISearchParamRegistry; +import ca.uhn.fhir.util.SearchParameterUtil; import org.apache.commons.lang3.StringUtils; import org.hl7.fhir.instance.model.api.IBaseResource; import org.springframework.beans.factory.annotation.Autowired; @@ -61,7 +62,8 @@ public class MdmSearchParamSvc { public List getValueFromResourceForSearchParam(IBaseResource theResource, String theSearchParam) { String resourceType = myFhirContext.getResourceType(theResource); - RuntimeSearchParam activeSearchParam = mySearchParamRegistry.getActiveSearchParam(resourceType, theSearchParam); + String searchParam = SearchParameterUtil.stripModifier(theSearchParam); + RuntimeSearchParam activeSearchParam = mySearchParamRegistry.getActiveSearchParam(resourceType, searchParam); return mySearchParamExtractorService.extractParamValuesAsStrings(activeSearchParam, theResource); } diff --git a/hapi-fhir-jpaserver-mdm/src/test/java/ca/uhn/fhir/jpa/mdm/provider/MdmProviderMatchR4Test.java b/hapi-fhir-jpaserver-mdm/src/test/java/ca/uhn/fhir/jpa/mdm/provider/MdmProviderMatchR4Test.java index 3551facb44a..4db9cc11573 100644 --- a/hapi-fhir-jpaserver-mdm/src/test/java/ca/uhn/fhir/jpa/mdm/provider/MdmProviderMatchR4Test.java +++ b/hapi-fhir-jpaserver-mdm/src/test/java/ca/uhn/fhir/jpa/mdm/provider/MdmProviderMatchR4Test.java @@ -15,6 +15,7 @@ import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.IOException; import java.util.List; import java.util.stream.Collectors; @@ -161,68 +162,134 @@ public class MdmProviderMatchR4Test extends BaseProviderR4Test { public void testMatchWithCoarseDateGranularity() throws Exception { setMdmRuleJson("mdm/coarse-birthdate-mdm-rules.json"); - String granularPatient = "{\n" + - " \"resourceType\": \"Patient\",\n" + - " \"active\": true,\n" + - " \"name\": [\n" + - " {\n" + - " \"family\": \"PETERSON\",\n" + - " \"given\": [\n" + - " \"GARY\",\n" + - " \"D\"\n" + - " ]\n" + - " }\n" + - " ],\n" + - " \"telecom\": [\n" + - " {\n" + - " \"system\": \"phone\",\n" + - " \"value\": \"100100100\",\n" + - " \"use\": \"home\"\n" + - " }\n" + - " ],\n" + - " \"gender\": \"male\",\n" + - " \"birthDate\": \"1991-10-10\",\n" + - " \"address\": [\n" + - " {\n" + - " \"state\": \"NY\",\n" + - " \"postalCode\": \"12313\"\n" + - " }\n" + - " ]\n" + - "}"; + String granularPatient = """ + { + "resourceType": "Patient", + "active": true, + "name": [ + { + "family": "PETERSON", + "given": [ + "GARY", + "D" + ] + } + ], + "telecom": [ + { + "system": "phone", + "value": "100100100", + "use": "home" + } + ], + "gender": "male", + "birthDate": "1991-10-10", + "address": [ + { + "state": "NY", + "postalCode": "12313" + } + ] + }"""; IBaseResource iBaseResource = myFhirContext.newJsonParser().parseResource(granularPatient); createPatient((Patient) iBaseResource); - String coarsePatient = "{\n" + - " \"resourceType\": \"Patient\",\n" + - " \"active\": true,\n" + - " \"name\": [\n" + - " {\n" + - " \"family\": \"PETERSON\",\n" + - " \"given\": [\n" + - " \"GARY\",\n" + - " \"D\"\n" + - " ]\n" + - " }\n" + - " ],\n" + - " \"telecom\": [\n" + - " {\n" + - " \"system\": \"phone\",\n" + - " \"value\": \"100100100\",\n" + - " \"use\": \"home\"\n" + - " }\n" + - " ],\n" + - " \"gender\": \"male\",\n" + - " \"birthDate\": \"1991-10\",\n" + - " \"address\": [\n" + - " {\n" + - " \"state\": \"NY\",\n" + - " \"postalCode\": \"12313\"\n" + - " }\n" + - " ]\n" + - "}"; + String coarsePatient = """ + { + "resourceType": "Patient", + "active": true, + "name": [ + { + "family": "PETERSON", + "given": [ + "GARY", + "D" + ] + } + ], + "telecom": [ + { + "system": "phone", + "value": "100100100", + "use": "home" + } + ], + "gender": "male", + "birthDate": "1991-10", + "address": [ + { + "state": "NY", + "postalCode": "12313" + } + ] + }"""; IBaseResource coarseResource = myFhirContext.newJsonParser().parseResource(coarsePatient); Bundle result = (Bundle) myMdmProvider.match((Patient) coarseResource, new SystemRequestDetails()); assertEquals(1, result.getEntry().size()); } + + @Test + public void testNicknameMatch() throws IOException { + setMdmRuleJson("mdm/nickname-mdm-rules.json"); + + String formalPatientJson = """ + { + "resourceType": "Patient", + "active": true, + "name": [ + { + "family": "PETERSON", + "given": [ + "Gregory" + ] + } + ], + "gender": "male" + }"""; + Patient formalPatient = (Patient) myFhirContext.newJsonParser().parseResource(formalPatientJson); + createPatient(formalPatient); + + String noMatchPatientJson = """ + { + "resourceType": "Patient", + "active": true, + "name": [ + { + "family": "PETERSON", + "given": [ + "Bob" + ] + } + ], + "gender": "male" + }"""; + Patient noMatchPatient = (Patient) myFhirContext.newJsonParser().parseResource(noMatchPatientJson); + createPatient(noMatchPatient); + { + Bundle result = (Bundle) myMdmProvider.match(noMatchPatient, new SystemRequestDetails()); + assertEquals(0, result.getEntry().size()); + } + + String nickPatientJson = """ + { + "resourceType": "Patient", + "active": true, + "name": [ + { + "family": "PETERSON", + "given": [ + "Greg" + ] + } + ], + "gender": "male" + }"""; + + { + Patient nickPatient = (Patient) myFhirContext.newJsonParser().parseResource(nickPatientJson); + Bundle result = (Bundle) myMdmProvider.match(nickPatient, new SystemRequestDetails()); + assertEquals(1, result.getEntry().size()); + } + } } diff --git a/hapi-fhir-jpaserver-mdm/src/test/java/ca/uhn/fhir/jpa/mdm/svc/MdmCandidateSearchCriteriaBuilderSvcTest.java b/hapi-fhir-jpaserver-mdm/src/test/java/ca/uhn/fhir/jpa/mdm/svc/MdmCandidateSearchCriteriaBuilderSvcTest.java index db439e71cf9..f37ca2a653a 100644 --- a/hapi-fhir-jpaserver-mdm/src/test/java/ca/uhn/fhir/jpa/mdm/svc/MdmCandidateSearchCriteriaBuilderSvcTest.java +++ b/hapi-fhir-jpaserver-mdm/src/test/java/ca/uhn/fhir/jpa/mdm/svc/MdmCandidateSearchCriteriaBuilderSvcTest.java @@ -1,8 +1,8 @@ package ca.uhn.fhir.jpa.mdm.svc; -import ca.uhn.fhir.mdm.rules.json.MdmResourceSearchParamJson; import ca.uhn.fhir.jpa.mdm.BaseMdmR4Test; import ca.uhn.fhir.jpa.mdm.svc.candidate.MdmCandidateSearchCriteriaBuilderSvc; +import ca.uhn.fhir.mdm.rules.json.MdmResourceSearchParamJson; import org.hl7.fhir.r4.model.HumanName; import org.hl7.fhir.r4.model.Patient; import org.junit.jupiter.api.Test; diff --git a/hapi-fhir-jpaserver-mdm/src/test/java/ca/uhn/fhir/jpa/mdm/svc/MdmCandidateSearchSvcIT.java b/hapi-fhir-jpaserver-mdm/src/test/java/ca/uhn/fhir/jpa/mdm/svc/MdmCandidateSearchSvcIT.java index da1f30f0bea..f283683458e 100644 --- a/hapi-fhir-jpaserver-mdm/src/test/java/ca/uhn/fhir/jpa/mdm/svc/MdmCandidateSearchSvcIT.java +++ b/hapi-fhir-jpaserver-mdm/src/test/java/ca/uhn/fhir/jpa/mdm/svc/MdmCandidateSearchSvcIT.java @@ -4,15 +4,21 @@ import ca.uhn.fhir.interceptor.model.RequestPartitionId; import ca.uhn.fhir.jpa.mdm.BaseMdmR4Test; import ca.uhn.fhir.jpa.mdm.svc.candidate.MdmCandidateSearchSvc; import ca.uhn.fhir.jpa.mdm.svc.candidate.TooManyCandidatesException; +import ca.uhn.fhir.jpa.searchparam.MatchUrlService; +import ca.uhn.fhir.jpa.searchparam.SearchParameterMap; +import ca.uhn.fhir.jpa.searchparam.nickname.NicknameInterceptor; import ca.uhn.fhir.mdm.rules.config.MdmSettings; +import ca.uhn.fhir.rest.api.server.IBundleProvider; import org.hl7.fhir.instance.model.api.IAnyResource; import org.hl7.fhir.r4.model.Patient; import org.hl7.fhir.r4.model.Practitioner; import org.hl7.fhir.r4.model.Reference; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; +import java.io.IOException; import java.util.Collection; import java.util.Collections; import java.util.Date; @@ -28,10 +34,21 @@ public class MdmCandidateSearchSvcIT extends BaseMdmR4Test { MdmCandidateSearchSvc myMdmCandidateSearchSvc; @Autowired MdmSettings myMdmSettings; + @Autowired + MatchUrlService myMatchUrlService; + + private NicknameInterceptor myNicknameInterceptor; + + @BeforeEach + public void before() throws IOException { + myNicknameInterceptor = new NicknameInterceptor(); + myInterceptorRegistry.registerInterceptor(myNicknameInterceptor); + } @AfterEach public void resetMdmSettings() { myMdmSettings.setCandidateSearchLimit(MdmSettings.DEFAULT_CANDIDATE_SEARCH_LIMIT); + myInterceptorRegistry.unregisterInterceptor(myNicknameInterceptor); } @Test @@ -43,6 +60,42 @@ public class MdmCandidateSearchSvcIT extends BaseMdmR4Test { assertEquals(1, result.size()); } + @Test + public void testNickname() { + Practitioner formal = new Practitioner(); + formal.getNameFirstRep().addGiven("William"); + formal.getNameFirstRep().setFamily("Shatner"); + formal.setActive(true); + myPractitionerDao.create(formal); + + { + // First confirm we can search for this practitioner using a nickname search + SearchParameterMap map = myMatchUrlService.getResourceSearch("Practitioner?given:nickname=Bill&family=Shatner").getSearchParameterMap(); + map.setLoadSynchronous(true); + IBundleProvider result = myPractitionerDao.search(map); + assertEquals(1, result.size()); + Practitioner first = (Practitioner) result.getResources(0, 1).get(0); + assertEquals("William", first.getNameFirstRep().getGivenAsSingleString()); + } + + { + // Now achieve the same match via mdm + Practitioner nick = new Practitioner(); + nick.getNameFirstRep().addGiven("Bill"); + nick.getNameFirstRep().setFamily("Shatner"); + Collection result = myMdmCandidateSearchSvc.findCandidates("Practitioner", nick, RequestPartitionId.allPartitions()); + assertEquals(1, result.size()); + } + + { + // Should not match Bob + Practitioner noMatch = new Practitioner(); + noMatch.getNameFirstRep().addGiven("Bob"); + noMatch.getNameFirstRep().setFamily("Shatner"); + Collection result = myMdmCandidateSearchSvc.findCandidates("Practitioner", noMatch, RequestPartitionId.allPartitions()); + assertEquals(0, result.size()); + } + } @Test public void findCandidatesMultipleMatchesDoNotCauseDuplicates() { @@ -83,9 +136,9 @@ public class MdmCandidateSearchSvcIT extends BaseMdmR4Test { Patient newJane = buildJanePatient(); createActivePatient(); - assertEquals(1, runInTransaction(()->myMdmCandidateSearchSvc.findCandidates("Patient", newJane, RequestPartitionId.allPartitions()).size())); + assertEquals(1, runInTransaction(() -> myMdmCandidateSearchSvc.findCandidates("Patient", newJane, RequestPartitionId.allPartitions()).size())); createActivePatient(); - assertEquals(2, runInTransaction(()->myMdmCandidateSearchSvc.findCandidates("Patient", newJane, RequestPartitionId.allPartitions()).size())); + assertEquals(2, runInTransaction(() -> myMdmCandidateSearchSvc.findCandidates("Patient", newJane, RequestPartitionId.allPartitions()).size())); try { createActivePatient(); diff --git a/hapi-fhir-jpaserver-mdm/src/test/resources/mdm/mdm-rules.json b/hapi-fhir-jpaserver-mdm/src/test/resources/mdm/mdm-rules.json index 15ffb644d5e..5c0f391828c 100644 --- a/hapi-fhir-jpaserver-mdm/src/test/resources/mdm/mdm-rules.json +++ b/hapi-fhir-jpaserver-mdm/src/test/resources/mdm/mdm-rules.json @@ -25,6 +25,13 @@ "searchParams": [ "general-practitioner" ] + }, + { + "resourceType": "Practitioner", + "searchParams": [ + "given:nickname", + "family" + ] } ], "candidateFilterSearchParams": [ diff --git a/hapi-fhir-jpaserver-mdm/src/test/resources/mdm/nickname-mdm-rules.json b/hapi-fhir-jpaserver-mdm/src/test/resources/mdm/nickname-mdm-rules.json new file mode 100644 index 00000000000..5685dcde02a --- /dev/null +++ b/hapi-fhir-jpaserver-mdm/src/test/resources/mdm/nickname-mdm-rules.json @@ -0,0 +1,44 @@ +{ + "version":"1", + "mdmTypes": ["Patient", "Practitioner"], + "candidateSearchParams":[ + { + "resourceType": "*", + "searchParams": [ + "given:nickname", + "family" + ] + } + ], + "candidateFilterSearchParams":[], + "matchFields":[ + { + "name":"gender", + "resourceType":"Patient", + "resourcePath":"gender", + "matcher":{ + "algorithm":"STRING" + } + }, + { + "name":"nickname", + "resourceType":"*", + "resourcePath":"name.given", + "matcher":{ + "algorithm":"NICKNAME" + } + }, + { + "name":"lastname", + "resourceType":"*", + "resourcePath":"name.family", + "matcher":{ + "algorithm":"STRING", + "exact": true + } + } + ], + "matchResultMap":{ + "nickname,lastname": "MATCH" + } +} diff --git a/hapi-fhir-jpaserver-model/src/main/java/ca/uhn/fhir/jpa/model/entity/ResourceIndexedSearchParamString.java b/hapi-fhir-jpaserver-model/src/main/java/ca/uhn/fhir/jpa/model/entity/ResourceIndexedSearchParamString.java index 794e32d0be9..624bc5add34 100644 --- a/hapi-fhir-jpaserver-model/src/main/java/ca/uhn/fhir/jpa/model/entity/ResourceIndexedSearchParamString.java +++ b/hapi-fhir-jpaserver-model/src/main/java/ca/uhn/fhir/jpa/model/entity/ResourceIndexedSearchParamString.java @@ -59,10 +59,9 @@ import static org.apache.commons.lang3.StringUtils.defaultString; // This is used for sorting, and for :contains queries currently @Index(name = "IDX_SP_STRING_HASH_IDENT", columnList = "HASH_IDENTITY"), - @Index(name = "IDX_SP_STRING_HASH_NRM", columnList = "HASH_NORM_PREFIX,SP_VALUE_NORMALIZED"), - @Index(name = "IDX_SP_STRING_HASH_EXCT", columnList = "HASH_EXACT"), + @Index(name = "IDX_SP_STRING_HASH_NRM_V2", columnList = "HASH_NORM_PREFIX,SP_VALUE_NORMALIZED,RES_ID,PARTITION_ID"), + @Index(name = "IDX_SP_STRING_HASH_EXCT_V2", columnList = "HASH_EXACT,RES_ID,PARTITION_ID"), - @Index(name = "IDX_SP_STRING_UPDATED", columnList = "SP_UPDATED"), @Index(name = "IDX_SP_STRING_RESID", columnList = "RES_ID") }) public class ResourceIndexedSearchParamString extends BaseResourceIndexedSearchParam { diff --git a/hapi-fhir-jpaserver-model/src/main/java/ca/uhn/fhir/jpa/model/entity/ResourceTable.java b/hapi-fhir-jpaserver-model/src/main/java/ca/uhn/fhir/jpa/model/entity/ResourceTable.java index 4458f37d427..33957717d9f 100644 --- a/hapi-fhir-jpaserver-model/src/main/java/ca/uhn/fhir/jpa/model/entity/ResourceTable.java +++ b/hapi-fhir-jpaserver-model/src/main/java/ca/uhn/fhir/jpa/model/entity/ResourceTable.java @@ -73,9 +73,9 @@ import java.util.stream.Collectors; @Indexed(routingBinder= @RoutingBinderRef(type = ResourceTableRoutingBinder.class)) @Entity @Table(name = "HFJ_RESOURCE", uniqueConstraints = {}, indexes = { - // Do not reuse previously used index name: IDX_INDEXSTATUS + // Do not reuse previously used index name: IDX_INDEXSTATUS, IDX_RES_TYPE @Index(name = "IDX_RES_DATE", columnList = "RES_UPDATED"), - @Index(name = "IDX_RES_TYPE", columnList = "RES_TYPE"), + @Index(name = "IDX_RES_TYPE_DEL_UPDATED", columnList = "RES_TYPE,RES_DELETED_AT,RES_UPDATED,PARTITION_ID,RES_ID"), }) @NamedEntityGraph(name = "Resource.noJoins") public class ResourceTable extends BaseHasResource implements Serializable, IBasePersistedResource, IResourceLookup { diff --git a/hapi-fhir-jpaserver-searchparam/src/main/java/ca/uhn/fhir/jpa/searchparam/MatchUrlService.java b/hapi-fhir-jpaserver-searchparam/src/main/java/ca/uhn/fhir/jpa/searchparam/MatchUrlService.java index 9da2c4fb57c..e459e348f83 100644 --- a/hapi-fhir-jpaserver-searchparam/src/main/java/ca/uhn/fhir/jpa/searchparam/MatchUrlService.java +++ b/hapi-fhir-jpaserver-searchparam/src/main/java/ca/uhn/fhir/jpa/searchparam/MatchUrlService.java @@ -33,6 +33,7 @@ import ca.uhn.fhir.model.api.Include; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.QualifiedParamList; import ca.uhn.fhir.rest.api.RestSearchParameterTypeEnum; +import ca.uhn.fhir.rest.api.SearchTotalModeEnum; import ca.uhn.fhir.rest.param.DateRangeParam; import ca.uhn.fhir.rest.param.ParameterUtil; import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; @@ -113,6 +114,15 @@ public class MatchUrlService { throw new InvalidRequestException(Msg.code(485) + "Invalid " + Constants.PARAM_COUNT + " value: " + intString); } } + } else if (Constants.PARAM_SEARCH_TOTAL_MODE.equals(nextParamName)) { + if (paramList != null && ! paramList.isEmpty() && ! paramList.get(0).isEmpty()) { + String totalModeEnumStr = paramList.get(0).get(0); + try { + paramMap.setSearchTotalMode(SearchTotalModeEnum.valueOf(totalModeEnumStr)); + } catch (IllegalArgumentException e) { + throw new InvalidRequestException(Msg.code(2078) + "Invalid " + Constants.PARAM_SEARCH_TOTAL_MODE + " value: " + totalModeEnumStr); + } + } } else if (Constants.PARAM_OFFSET.equals(nextParamName)) { if (paramList != null && paramList.size() > 0 && paramList.get(0).size() > 0) { String intString = paramList.get(0).get(0); diff --git a/hapi-fhir-jpaserver-searchparam/src/main/java/ca/uhn/fhir/jpa/searchparam/nickname/NicknameInterceptor.java b/hapi-fhir-jpaserver-searchparam/src/main/java/ca/uhn/fhir/jpa/searchparam/nickname/NicknameInterceptor.java new file mode 100644 index 00000000000..b118286e856 --- /dev/null +++ b/hapi-fhir-jpaserver-searchparam/src/main/java/ca/uhn/fhir/jpa/searchparam/nickname/NicknameInterceptor.java @@ -0,0 +1,67 @@ +package ca.uhn.fhir.jpa.searchparam.nickname; + +import ca.uhn.fhir.interceptor.api.Hook; +import ca.uhn.fhir.interceptor.api.Pointcut; +import ca.uhn.fhir.jpa.searchparam.SearchParameterMap; +import ca.uhn.fhir.model.api.IQueryParameterType; +import ca.uhn.fhir.rest.param.StringParam; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; +import java.util.Map; + +public class NicknameInterceptor { + private static final Logger ourLog = LoggerFactory.getLogger(NicknameInterceptor.class); + + private final NicknameSvc myNicknameSvc; + + public NicknameInterceptor() throws IOException { + myNicknameSvc = new NicknameSvc(); + } + + @Hook(Pointcut.STORAGE_PRESEARCH_REGISTERED) + public void expandNicknames(SearchParameterMap theSearchParameterMap) { + for (Map.Entry>> set : theSearchParameterMap.entrySet()) { + String paramName = set.getKey(); + List> andList = set.getValue(); + for (List orList : andList) { + // here we will know if it's an _id param or not + // from theSearchParameterMap.keySet() + expandAnyNicknameParameters(paramName, orList); + } + } + } + + /** + * If a Parameter is a string parameter, and it has been set to expand Nicknames, perform the expansion. + */ + private void expandAnyNicknameParameters(String theParamName, List orList) { + List toAdd = new ArrayList<>(); + List toRemove = new ArrayList<>(); + for (IQueryParameterType iQueryParameterType : orList) { + if (iQueryParameterType instanceof StringParam) { + StringParam stringParam = (StringParam) iQueryParameterType; + if (stringParam.isNicknameExpand()) { + ourLog.debug("Found a nickname parameter to expand: {} {}", theParamName, stringParam); + toRemove.add(stringParam); + //First, attempt to expand as a formal name + String name = stringParam.getValue().toLowerCase(Locale.ROOT); + List expansions = myNicknameSvc.getEquivalentNames(name); + if (expansions == null) { + continue; + } + ourLog.debug("Parameter has been expanded to: {} {}", theParamName, String.join(", ", expansions)); + expansions.stream() + .map(StringParam::new) + .forEach(toAdd::add); + } + } + } + orList.removeAll(toRemove); + orList.addAll(toAdd); + } +} diff --git a/hapi-fhir-jpaserver-searchparam/src/main/java/ca/uhn/fhir/jpa/searchparam/nickname/NicknameMap.java b/hapi-fhir-jpaserver-searchparam/src/main/java/ca/uhn/fhir/jpa/searchparam/nickname/NicknameMap.java new file mode 100644 index 00000000000..017442ca6b1 --- /dev/null +++ b/hapi-fhir-jpaserver-searchparam/src/main/java/ca/uhn/fhir/jpa/searchparam/nickname/NicknameMap.java @@ -0,0 +1,47 @@ +package ca.uhn.fhir.jpa.searchparam.nickname; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.Reader; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +class NicknameMap { + private final Map> myFormalToNick = new HashMap<>(); + private final Map> myNicknameToFormal = new HashMap<>(); + + void load(Reader theReader) throws IOException { + try (BufferedReader reader = new BufferedReader(theReader)) { + String line; + while ((line = reader.readLine()) != null) { + String[] parts = line.split(","); + String key = parts[0]; + List values = new ArrayList<>(Arrays.asList(parts).subList(1, parts.length)); + add(key, values); + } + } + } + + private void add(String theKey, List theValues) { + myFormalToNick.put(theKey, theValues); + for (String value : theValues) { + myNicknameToFormal.putIfAbsent(value, new ArrayList<>()); + myNicknameToFormal.get(value).add(theKey); + } + } + + int size() { + return myFormalToNick.size(); + } + + List getNicknamesFromFormalNameOrNull(String theName) { + return myFormalToNick.get(theName); + } + + List getFormalNamesFromNicknameOrNull(String theNickname) { + return myNicknameToFormal.get(theNickname); + } +} diff --git a/hapi-fhir-jpaserver-searchparam/src/main/java/ca/uhn/fhir/jpa/searchparam/nickname/NicknameSvc.java b/hapi-fhir-jpaserver-searchparam/src/main/java/ca/uhn/fhir/jpa/searchparam/nickname/NicknameSvc.java new file mode 100644 index 00000000000..193b42f7fe7 --- /dev/null +++ b/hapi-fhir-jpaserver-searchparam/src/main/java/ca/uhn/fhir/jpa/searchparam/nickname/NicknameSvc.java @@ -0,0 +1,53 @@ +package ca.uhn.fhir.jpa.searchparam.nickname; + +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.util.ArrayList; +import java.util.List; + +public class NicknameSvc { + private final NicknameMap myNicknameMap = new NicknameMap(); + + public NicknameSvc() throws IOException { + Resource nicknameCsvResource = new ClassPathResource("/nickname/names.csv"); + try (InputStream inputStream = nicknameCsvResource.getInputStream()) { + try (Reader reader = new InputStreamReader(inputStream)) { + myNicknameMap.load(reader); + } + } + } + + public int size() { + return myNicknameMap.size(); + } + + public List getEquivalentNames(String theName) { + List retval = new ArrayList<>(); + retval.add(theName); + + List expansions; + expansions = getNicknamesFromFormalNameOrNull(theName); + if (expansions != null) { + retval.addAll(expansions); + } else { + expansions = getFormalNamesFromNicknameOrNull(theName); + if (expansions != null) { + retval.addAll(expansions); + } + } + return retval; + } + + List getNicknamesFromFormalNameOrNull(String theName) { + return myNicknameMap.getNicknamesFromFormalNameOrNull(theName); + } + + List getFormalNamesFromNicknameOrNull(String theNickname) { + return myNicknameMap.getFormalNamesFromNicknameOrNull(theNickname); + } +} diff --git a/hapi-fhir-jpaserver-searchparam/src/main/resources/nickname/License.txt b/hapi-fhir-jpaserver-searchparam/src/main/resources/nickname/License.txt new file mode 100644 index 00000000000..8dada3edaf5 --- /dev/null +++ b/hapi-fhir-jpaserver-searchparam/src/main/resources/nickname/License.txt @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright {yyyy} {name of copyright owner} + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/hapi-fhir-jpaserver-searchparam/src/main/resources/nickname/README.md b/hapi-fhir-jpaserver-searchparam/src/main/resources/nickname/README.md new file mode 100644 index 00000000000..863d66d776b --- /dev/null +++ b/hapi-fhir-jpaserver-searchparam/src/main/resources/nickname/README.md @@ -0,0 +1,12 @@ +# nickname-and-diminutive-names-lookup + +A simple CSV file containing US given names (first name) and their associated nicknames or diminutive names. + +This lookup file was initially created by mining this +genealogy page. Because the lookup originates from a dataset used for genealogy purposes there are old names that aren't commonly used these days, but there are recent ones as well. Examples are "gregory", "greg", or "geoffrey", "geoff". There was also a significant effort to make it machine readable, i.e. separate it with commas, remove human conventions, like "rickie(y)" would need to be made into two different names "rickie", and "ricky". + +There are Java, Perl, Python, and R parsers provided for convenience. + +This is a relatively large list with roughly 1600 names. Any help from people to clean this list up and add to it is greatly appreciated. + +This project was created by Old Dominion University - Web Science and Digital Libraries Research Group. More information about the creation of this lookup can be found here. diff --git a/hapi-fhir-jpaserver-searchparam/src/main/resources/nickname/README.txt b/hapi-fhir-jpaserver-searchparam/src/main/resources/nickname/README.txt new file mode 100644 index 00000000000..24d522ebdda --- /dev/null +++ b/hapi-fhir-jpaserver-searchparam/src/main/resources/nickname/README.txt @@ -0,0 +1,2 @@ +The files in this folder were cloned from https://github.com/carltonnorthern/nickname-and-diminutive-names-lookup + diff --git a/hapi-fhir-jpaserver-searchparam/src/main/resources/nickname/names.csv b/hapi-fhir-jpaserver-searchparam/src/main/resources/nickname/names.csv new file mode 100644 index 00000000000..76728850a12 --- /dev/null +++ b/hapi-fhir-jpaserver-searchparam/src/main/resources/nickname/names.csv @@ -0,0 +1,1084 @@ +aaron,erin,ronnie,ron +abbigail,nabby,abby,gail,abbi,abbey +abednego,bedney +abel,ebbie,ab,abe,eb +abiel,ab +abigail,nabby,abby,gail +abijah,ab,bige +abner,ab +abraham,ab,abe +abram,ab,abe +absalom,app,ab,abbie +ada,addy +adaline,delia,lena,dell,addy,ada +addison,addie,addy +adela,della +adelaide,heidi,adele,dell,addy,della +adelbert,del,albert,delbert,bert +adele,addy,dell +adeline,delia,lena,dell,addy,ada +adelphia,philly,delphia,adele,dell,addy +adolphus,dolph,ado,adolph +adrian,rian +adriane,riane +adrienne,addie,rienne,enne +agatha,aggy +agnes,inez,aggy,nessa +aileen,lena,allie +alan,al +alanson,al,lanson +alastair,al +alazama,ali +albert,bert,al +alberta,bert,allie,bertie +aldo,al +aldrich,riche,rich +aleksandr,alex,alek +aleva,levy,leve +alex,al +alexander,alex,al,sandy,alec +alexandra,alex,sandy,alla,sandra +alexandria,drina,alexander,alla,sandra +alexis,lexi +alfonse,al +alfred,freddy,al,fred +alfreda,freddy,alfy,freda,frieda +algernon,algy +alice,lisa,elsie,allie +alicia,lisa,elsie,allie +aline,adeline +alison,ali,allie +alixandra,alix +allan,al +allen,al +allisandra,allie +allison,ali,allie +allyson,ally,allie +allyssa,ally,allie +almena,mena,allie +almina,minnie +almira,myra +alonzo,lon,al,lonzo +alphinias,alphus +althea,ally +alverta,virdie,vert +alyssa,lissia,al,ally +alzada,zada +amanda,mandy,manda +ambrose,brose +amelia,amy,mel,millie,emily +amos,moses +anastasia,ana,stacy +anderson,andy +andre,drea +andrea,drea,rea,andrew +andrew,andy,drew +andriane,ada,adri,rienne +angela,angel,angie +angelica,angie,angel +angelina,angel,angie,lina +ann,annie,nan +anna,anne,ann,annie,nan +anne,annie,ann,nan +annette,anna,nettie +annie,ann,anna +anselm,ansel,selma,anse,ance +anthony,ant,tony +antoinette,tony,netta,ann +antonia,tony,netta,ann +antonio,ant,tony +appoline,appy,appie +aquilla,quil,quillie +ara,belle,arry +arabella,ara,bella,arry,belle +arabelle,ara,bella,arry,belle +araminta,armida,middie,ruminta,minty +archibald,archie +archilles,kill,killis +ariadne,arie,ari +arielle,arie +aristotle,telly +arizona,onie,ona +arlene,arly,lena +armanda,mandy +armena,mena,arry +armilda,milly +arminda,mindie +arminta,minite,minnie +arnold,arnie +aron,erin,ronnie,ron +artelepsa,epsey +artemus,art +arthur,art +arthusa,thursa +arzada,zaddi +asahel,asa +asaph,asa +asenath,sene,assene,natty +ashley,ash,leah,lee +aubrey,bree +audrey,dee +august,gus +augusta,tina,aggy,gatsy,gussie +augustina,tina,aggy,gatsy,gussie +augustine,gus,austin,august +augustus,gus,austin,august +aurelia,ree,rilly,orilla,aurilla,ora +avarilla,rilla +azariah,riah,aze +bab,barby +babs,barby,barbara,bab +barbara,barby,babs,bab,bobbie +barbery,barbara +barbie,barbara +barnabas,barney +barney,barnabas +bart,bartholomew +bartholomew,bartel,bat,meus,bart,mees +barticus,bart +bazaleel,basil +bea,beatrice +beatrice,bea,trisha,trixie,trix +becca,beck +beck,becky +bedelia,delia,bridgit +belinda,belle,linda +bella,belle,arabella,isabella +benedict,bennie,ben +benjamin,benjy,jamie,bennie,ben,benny +benjy,benjamin +bernard,barney,bernie,berney +berney,bernie +bert,bertie,bob,bobby +bertha,bert,birdie,bertie +bertram,bert +bess,bessie +beth,betsy,betty,elizabeth +bethena,beth,thaney +beverly,bev +bezaleel,zeely +biddie,biddy +bill,william,billy,robert,willie,fred +billy,william,robert,fred +blanche,bea +bob,rob,robert +bobby,rob,bob +boetius,bo +brad,bradford,ford +bradford,ford,brad +bradley,brad +brady,brody +breanna,bree,bri +breeanna,bree +brenda,brandy +brian,bryan,bryant +brianna,bri +bridget,bridie,biddy,bridgie,biddie +brittany,britt +brittney,britt +broderick,ricky,brody,brady,rick +caitlin,cait,caity +caitlyn,cait,caity +caldonia,calliedona +caleb,cal +california,callie +calista,kissy +calpurnia,cally +calvin,vin,vinny,cal +cameron,ron,cam,ronny +camille,millie,cammie +campbell,cam +candace,candy,dacey +carlotta,lottie +carlton,carl +carmellia,mellia +carmelo,melo +carmon,charm,cammie,carm +carol,lynn,carrie,carolann,cassie,caroline,carole +carolann,carol,carole +caroline,lynn,carol,carrie,cassie,carole +carolyn,lynn,carrie,cassie +carrie,cassie +carthaette,etta,etty +casey,k.c. +casper,jasper +cassandra,sandy,cassie,sandra +cassidy,cassie,cass +caswell,cass +catherine,kathy,katy,lena,kittie,kit,trina,cathy,kay,cassie +cathleen,kathy,katy,lena,kittie,kit,trina,cathy,kay,cassie +cathy,kathy,cathleen,catherine +cecilia,cissy,celia +cedric,ced,rick,ricky +celeste,lessie,celia +celinda,linda,lynn,lindy +charity,chat +charles,charlie,chuck,carl,chick +charlie,charles,chuck +charlotte,char,sherry,lottie,lotta +chauncey,chan +cheryl,cher +chesley,chet +chester,chet +chet,chester +chick,charlotte,caroline,chuck +chloe,clo +chris,christina,christopher,christine +christa,chris +christian,chris,kit +christiana,kris,kristy,ann,tina,christy,chris,crissy +christiano,chris +christina,kris,kristy,tina,christy,chris,crissy +christine,kris,kristy,chrissy,tina,chris,crissy +christoph,chris +christopher,chris,kit +christy,crissy +cicely,cilla +cinderella,arilla,rella,cindy,rilla +cindy,cinderella +claire,clair,clare,clara +clara,clarissa +clare,clara +clarence,clare,clair +clarinda,clara +clarissa,cissy,clara +claudia,claud +cleatus,cleat +clement,clem +clementine,clement,clem +cliff,clifford,cliff +clifford,ford,cliff +clifton,tony,cliff +cole,colie +columbus,clum +con,conny +conrad,conny,con +constance,connie +cordelia,cordy,delia +corey,coco,cordy,ree +corinne,cora,ora +cornelia,nelly,cornie,nelia,corny,nelle +cornelius,conny,niel,corny,con +cory,coco,cordy,ree +courtney,curt,court +crystal,chris,tal,stal,crys +curtis,curt +cynthia,cintha,cindy +cyrenius,swene,cy,serene,renius,cene +cyrus,cy +dahl,dal +dalton,dahl,dal +daniel,dan,danny,dann +danielle,ellie,dani +danny,daniel +daphne,daph,daphie +darlene,lena,darry +david,dave,day,davey +deanne,ann,dee +debbie,deb,debra,deborah,debby +debby,deb +debora,deb,debbie,debby +deborah,deb,debbie,debby +debra,deb,debbie +deidre,deedee +delbert,bert,del +delia,fidelia,cordelia,delius +delilah,lil,lila,dell,della +deliverance,delly,dilly,della +della,adela,delilah,adelaide,dell +delores,lolly,lola,della,dee,dell +delpha,philadelphia +delphine,delphi,del,delf +demaris,dea,maris,mary +demerias,dea,maris,mary +democrates,mock +dennis,denny +dennison,denny +derrick,ricky,eric,rick +deuteronomy,duty +diana,dicey,didi,di +diane,dicey,didi,di +dicey,dicie +dick,rick,richard +dickson,dick +domenic,dom +dominic,dom +dominick,dom +donald,dony,donnie,don,donny +donovan,dony,donnie,don,donny +dorcus,darkey +dorinda,dorothea,dora +doris,dora +dorothea,doda,dora +dorothy,dortha,dolly,dot,dotty +dotha,dotty +dotty,dot +douglas,doug +drusilla,silla +duncan,dunk +earnest,ernestine,ernie +ebbie,eb +ebenezer,ebbie,eben,eb +eddie,ed +eddy,ed +edgar,ed,eddie,eddy +edith,edie,edye +edmond,ed,eddie,eddy +edmund,ed,eddie,ted,eddy,ned +edna,edny +eduardo,ed,eddie,eddy +edward,teddy,ed,ned,ted,eddy,eddie +edwin,ed,eddie,win,eddy,ned +edwina,edwin +edyth,edie,edye +edythe,edie,edye +egbert,bert,burt +eighta,athy +eileen,helen +elaine,lainie,helen +elbert,albert +elbertson,elbert,bert +eleanor,lanna,nora,nelly,ellie,elaine,ellen,lenora +eleazer,lazar +elena,helen +elias,eli,lee,lias +elijah,lige,eli +eliphalel,life +eliphalet,left +elisa,lisa +elisha,lish,eli +eliza,elizabeth +elizabeth,libby,lisa,lib,lizzy,lizzie,eliza,betsy,liza,betty,bessie,bess,beth,liz +ella,ellen +ellen,nellie,nell,helen +ellender,nellie,ellen,helen +ellie,elly +ellswood,elsey +elminie,minnie +elmira,ellie,elly,mira +elnora,nora +eloise,heloise,louise +elouise,louise +elsie,elsey +elswood,elsey +elvira,elvie +elwood,woody +elysia,lisa +elze,elsey +emanuel,manuel,manny +emeline,em,emmy,emma,milly,emily +emil,emily +emily,emmy,millie,emma,mel +emma,emmy +epaphroditius,dite,ditus,eppa,dyche,dyce +ephraim,eph +erasmus,raze,rasmus +eric,rick,ricky +ernest,ernie +ernestine,teeny,ernest,tina,erna +erwin,irwin +eseneth,senie +essy,es +estella,essy,stella +estelle,essy,stella +esther,hester,essie +eudicy,dicey +eudora,dora +eudoris,dossie,dosie +eugene,gene +eunice,nicie +euphemia,effie,effy +eurydice,dicey +eustacia,stacia,stacy +eva,eve +evaline,eva,lena,eve +evangeline,ev,evan,vangie +evelyn,evelina,ev,eve +experience,exie +ezekiel,zeke,ez +ezideen,ez +ezra,ez +faith,fay +felicia,fel,felix,feli +felicity,flick,tick +feltie,felty +ferdinand,freddie,freddy,ferdie,fred +ferdinando,nando,ferdie,fred +fidelia,delia +flo,florence +flora,florence +florence,flossy,flora,flo +floyd,lloyd +fran,frannie +frances,sis,cissy,frankie,franniey,fran,francie,frannie,fanny +francie,francine +francine,franniey,fran,frannie,francie +francis,fran,frankie,frank +frank,franklin +frankie,frank,francis +franklin,fran,frank +franklind,frank +freda,frieda +frederica,frederick,freddy +frederick,freddie,freddy,fritz,fred +fredericka,freddy,ricka,freda,frieda +frieda,freddie,freddy,fred +gabriel,gabe,gabby +gabriella,ella,gabby +gabrielle,ella,gabby +garrick,garri +genevieve,jean,eve,jenny +geoffrey,geoff,jeff +george,georgie +georgiana,georgia +gerald,gerry,jerry +geraldine,gerry,gerrie,jerry,dina +gerhardt,gay +gertie,gertrude,gert +gertrude,gertie,gert,trudy +gilbert,bert,gil,wilber +giovanni,gio +gloria,glory +governor,govie +greenberry,green,berry +greggory,gregg +gregory,greg,gory +gretchen,margaret +griselda,grissel +gum,monty +gus,gussie +gustavus,gus,gussie +gwen,wendy +gwendolyn,gwen,wendy +hamilton,ham +hannah,nan,nanny,anna +harold,hal,harry +harriet,hattie +harry,harold,henry +haseltine,hassie +heather,hetty +helen,lena,ella,ellen,ellie +helena,eileen,lena,nell,nellie,eleanor,elaine,ellen,aileen +helene,lena,ella,ellen,ellie +heloise,lois,eloise,elouise +henrietta,hank,etta,etty,retta,nettie +henry,hank,hal,harry +hephsibah,hipsie +hepsibah,hipsie +herbert,bert,herb +herman,harman,dutch +hermione,hermie +hester,hessy,esther,hetty +hezekiah,hy,hez,kiah +hiram,hy +honora,honey,nora,norry,norah +hopkins,hopp,hop +horace,horry +hortense,harty,tensey +hosea,hosey,hosie +howard,hal,howie +hubert,bert,hugh,hub +ian,john +ignatius,natius,iggy,nate,nace +ignatzio,naz,iggy,nace +immanuel,manuel,emmanuel +india,indie,indy +inez,agnes +iona,onnie +irene,rena +irvin,irving +irving,irv +irwin,erwin +isaac,ike,zeke +isabel,tibbie,bell,nib,belle,bella,nibby,ib,issy +isabella,tibbie,nib,belle,bella,nibby,ib,issy +isabelle,tibbie,nib,belle,bella,nibby,ib,issy +isadora,issy,dora +isaiah,zadie,zay +isidore,izzy +iva,ivy +ivan,john +jackson,jack +jacob,jaap,jake,jay +jacobus,jacob +jacqueline,jackie,jack +jahoda,hody,hodie,hoda +jakob,jake +james,jimmy,jim,jamie,jimmie,jem +jamie,james +jane,janie,jessie,jean,jennie +janet,jan,jessie +janice,jan +jannett,nettie +jasper,jap,casper +jayme,jay +jean,jane,jeannie +jeanette,jessie,jean,janet,nettie +jeanne,jane,jeannie +jebadiah,jeb +jedediah,dyer,jed,diah +jedidiah,dyer,jed,diah +jefferey,jeff +jefferson,sonny,jeff +jeffery,jeff +jeffrey,geoff,jeff +jehiel,hiel +jehu,hugh,gee +jemima,mima +jennet,jessie,jenny,jenn +jennifer,jennie,jenn,jen,jenny +jeremiah,jereme,jerry +jeremy,jezza,jez +jerita,rita +jerry,jereme,geraldine +jessica,jessie +jessie,jane,jess,janet +jim,jimmie +jincy,jane +jinsy,jane +joan,jo,nonie +joann,jo +joanna,hannah,jody,jo,joan +joanne,jo +jody,jo +joe,joey +johanna,jo +johannah,hannah,jody,joan,nonie +johannes,jonathan,john,johnny +john,jack,johnny,jock,ian +jonathan,john,nathan,jon,jonny,jonnie +jonathon,jon,jonny,jonnie +joseph,jody,jos,joe,joey +josephine,fina,jody,jo,josey,joey,josie +josetta,jettie +josey,josophine +joshua,jos,josh +josiah,jos +joyce,joy +juanita,nita,nettie +judah,juder,jude +judith,judie,juda,judy,judi,jude +judson,sonny,jud +judy,judith +julia,julie,jill +julian,jule +julias,jule +julie,julia,jule +june,junius +junior,junie,june,jr +justin,justus,justina +kaitlin,kait,kaitie +kaitlyn,kait,kaitie +kaitlynn,kait,kaitie +kameron,kam +karonhappuck,karon,karen,carrie,happy +kasey,k.c. +katarina,catherine,tina +kate,kay +katelin,kay,kate,kaye +katelyn,kay,kate,kaye +katherine,kathy,katy,lena,kittie,kaye,kit,trina,cathy,kay,kate,cassie +kathleen,kathy,katy,lena,kittie,kit,trina,cathy,kay,cassie +kathryn,kathy,katie +katy,kathy,katie +kayla,kay +kendall,ken,kenny +kendra,kenj,kenji,kay,kenny +kendrick,ken,kenny +kendrik,ken,kenny +kenneth,ken,kenny,kendrick +kenny,ken,kenneth +kent,ken,kenny,kendrick +keziah,kizza,kizzie +kim,kimberly,kimberley +kimberley,kim +kimberly,kim +kingsley,king +kingston,king +kit,kittie +kris,chris +kristel,kris +kristen,chris +kristin,chris +kristine,kris,kristy,tina,christy,chris,crissy +kristopher,chris,kris +kristy,chris +lafayette,laffie,fate +lamont,monty +laodicia,dicy,cenia +larry,laurence,lawrence +lauren,ren,laurie +laurence,lorry,larry,lon,lonny,lorne +laurinda,laura,lawrence +lauryn,laurie +laveda,veda +laverne,vernon,verna +lavina,vina,viney,ina +lavinia,vina,viney,ina +lavonia,vina,vonnie,wyncha,viney +lavonne,von +lawrence,lorry,larry,lon,lonny,lorne +leanne,lea,annie +lecurgus,curg +lemuel,lem +lena,ellen +lenora,nora,lee +leo,leon +leonard,lineau,leo,leon,len,lenny +leonidas,lee,leon +leonora,nora,nell,nellie +leonore,nora,honor,elenor +leroy,roy,lee,l.r. +leslie,les +lester,les +letitia,tish,titia,lettice,lettie +levi,lee +levicy,vicy +levone,von +lib,libby +lidia,lyddy +lil,lilly,lily +lillah,lil,lilly,lily,lolly +lillian,lil,lilly,lolly +lilly,lily,lil +lincoln,link +linda,lindy,lynn +lindy,lynn +lionel,leon +lisa,liz +littleberry,little,berry,l.b. +lizzie,liz +lois,lou,louise +lonzo,lon +lorenzo,loren +loretta,etta,lorrie,retta +lorraine,lorrie +lotta,lottie +lou,louis,lu +louis,lewis,louise,louie,lou +louisa,eliza,lou,lois +louise,eliza,lou,lois +louvinia,vina,vonnie,wyncha,viney +lucas,luke +lucia,lucy,lucius +lucias,luke +lucille,cille,lu,lucy,lou +lucina,sinah +lucinda,lu,lucy,cindy,lou +lucretia,creasey +lucy,lucinda +luella,lula,ella,lu +luke,lucas +lunetta,nettie +lurana,lura +luther,luke +lydia,lyddy +lyndon,lindy,lynn +mabel,mehitabel,amabel +mac,mc +mack,mac,mc +mackenzie,kenzy,mac,mack +maddison,maddie,maddi +maddy,madelyn,madeline,madge +madeline,maggie,lena,magda,maddy,madge +madeline,maddie,maddi +madie,madeline,madelyn +madison,mattie,maddy +maegen,meg +magdalena,maggie,lena +magdelina,lena,magda,madge +mahala,hallie +makayla,kayla +malachi,mally +malcolm,mac,mal,malc +malinda,lindy +manda,mandy +mandie,amanda +mandy,amanda +manerva,minerva,nervie,eve,nerva +manny,manuel +manoah,noah +manola,nonnie +manuel,emanuel,manny +marcus,mark +margaret,maggie,meg,peg,midge,margy,margie,madge,peggy,maggy,marge,daisy,margery,gretta,rita +margaretta,maggie,meg,peg,midge,margie,madge,peggy,marge,daisy,margery,gretta,rita +margarita,maggie,meg,metta,midge,greta,megan,maisie,madge,marge,daisy,peggie,rita,margo +marge,margery,margaret,margaretta +margie,marjorie,margie +marguerite,peggy +mariah,mary,maria +marian,marianna,marion +marie,mae +marietta,mariah,mercy,polly,may,molly,mitzi,minnie,mollie,mae,maureen,marion,marie,mamie,mary,maria +marilyn,mary +marion,mary +marissa,rissa +marjorie,margie +marjorie,margy,margie +marsha,marcie,mary +martha,marty,mattie,mat,patsy,patty +martin,marty +martina,tina +martine,tine +marv,marvin +marvin,marv +mary,mamie,molly,mae,polly,mitzi +masayuki,masa +mat,mattie +mathew,mat,maty +mathilda,tillie,patty +matilda,tilly,maud,matty +matthew,thys,matt,thias,mattie,matty +matthias,thys,matt,thias +maud,middy +maureen,mary +maurice,morey +mavery,mave +mavine,mave +maximillian,max +maxine,max +maxwell,max +may,mae +mckenna,ken,kenna,meaka +medora,dora +megan,meg +meghan,meg +mehitabel,hetty,mitty,mabel,hitty +melanie,mellie +melchizedek,zadock,dick +melinda,linda,mel,lynn,mindy,lindy +melissa,lisa,mel,missy,milly,lissa +mellony,mellia +melody,lodi +melvin,mel +melvina,vina +mercedes,merci,sadie,mercy +merv,mervin +mervyn,merv +micajah,cage +michael,micky,mike,micah,mick,mikey +micheal,mike,miky,mikey +michelle,mickey +mick,micky +mike,micky,mick,michael +mildred,milly +millicent,missy,milly +minerva,minnie +minnie,wilhelmina +miranda,randy,mandy,mira +miriam,mimi,mitzi,mitzie +missy,melissa +mitch,mitchell +mitchell,mitch +mitzi,mary,mittie,mitty +mitzie,mittie,mitty +monet,nettie +monica,monna,monnie +monteleon,monte +montesque,monty +montgomery,monty,gum +monty,lamont +morris,morey +mortimer,mort +moses,amos,mose,moss +muriel,mur +myrtle,myrt,myrti,mert +nadine,nada,deedee +nancy,ann,nan,nanny +naomi,omi +napoleon,nap,nappy,leon +natalie,natty,nettie +natasha,tasha,nat +nathan,nate,nat +nathaniel,than,nathan,nate,nat,natty +nelle,nelly +nelson,nels +newt,newton +nicholas,nick,claes,claas,nic,nicky,nico,nickie +nicodemus,nick,nic,nicky,nico,nickie +nicole,nole,nikki,cole +nikolas,nick,claes,nic,nicky,nico,nickie +nikole,nikki +nora,nonie +norbert,bert,norby +norbusamte,norbu +nowell,noel +obadiah,dyer,obed,obie,diah +obedience,obed,beda,beedy,biddie +obie,obediah +octavia,tave,tavia +odell,odo +olive,nollie,livia,ollie +oliver,ollie +olivia,nollie,livia,ollie +ollie,oliver +onicyphorous,cyphorus,osaforus,syphorous,one,cy,osaforum +orilla,rilly,ora +orlando,roland +orphelia,phelia +ossy,ozzy +oswald,ozzy,waldo,ossy +otis,ode,ote +pamela,pam +pandora,dora +parmelia,amelia,milly,melia +parthenia,teeny,parsuny,pasoonie,phenie +patience,pat,patty +patricia,tricia,pat,patsy,patty,patti +patrick,pate,peter,pat,patsy,paddy +patsy,patty +patty,patricia +paul,polly +paula,polly,lina +paulina,polly,lina +pauline,polly +peggy,peg +pelegrine,perry +penelope,penny +percival,percy +peregrine,perry +permelia,melly,milly,mellie +pernetta,nettie +persephone,seph,sephy +peter,pete,pate +petronella,nellie +pheney,josephine +pheriba,pherbia,ferbie +philadelphia,delphia +philander,fie +philetus,leet,phil +philinda,linda,lynn,lindy +philip,phil +philipina,phoebe,penie +phillip,phil +philly,delphia +philomena,menaalmena +phoebe,fifi +pinckney,pink +pleasant,ples +pocahontas,pokey +posthuma,humey +prescott,scotty,scott,pres +priscilla,prissy,cissy,cilla +providence,provy +prudence,prue,prudy +prudy,prudence +rachel,shelly +rafaela,rafa +ramona,mona +randolph,dolph,randy +raphael,ralph +ray,raymond +raymond,ray +reba,beck,becca +rebecca,beck,becca,reba,becky +reggie,reginald,reg +regina,reggie,gina +reginald,reggie,naldo,reg,renny +relief,leafa +reuben,rube +reynold,reginald +rhoda,rodie +rhodella,della +rhyna,rhynie +ricardo,rick,ricky +rich,dick,rick +richard,dick,dickon,dickie,dicky,rick,rich,ricky,richie +rick,ricky +ricky,dick,rich +robert,hob,hobkin,dob,rob,bobby,dobbin,bob +roberta,robbie,bert,bobbie,birdie,bertie +roderick,rod,erick,rickie,roddy +rodger,roge,bobby,hodge,rod,robby,rupert,robin +rodney,rod +roger,roge,bobby,hodge,rod,robby,rupert,robin +roland,rollo,lanny,orlando,rolly +ron,ronnie,ronny +ronald,naldo,ron,ronny,ronnie +ronny,ronald +rosa,rose +rosabel,belle,roz,rosa,rose +rosabella,belle,roz,rosa,rose +rosalinda,linda,roz,rosa,rose +rosalyn,linda,roz,rosa,rose +roscoe,ross +rose,rosie +roseann,rose,ann,rosie,roz +roseanna,rose,ann,rosie,roz +roseanne,ann +rosina,sina +roxane,rox,roxie +roxanna,roxie,rose,ann +roxanne,roxie,rose,ann +rudolph,dolph,rudy,olph,rolf +rudolphus,dolph,rudy,olph,rolf +russell,russ,rusty +ryan,ry +sabrina,brina +safieel,safie +salome,loomie +salvador,sal +samantha,sammy,sam,mantha +sammy,samuel +sampson,sam +samson,sam +samuel,sammy,sam +samyra,myra +sandra,sandy,cassandra +sandy,sandra +sanford,sandy +sarah,sally,sadie +sarilla,silla +savannah,vannie,anna +scott,scotty,sceeter,squat,scottie +sebastian,sebby,seb +selma,anselm +serena,rena +serilla,rilla +seymour,see,morey +shaina,sha,shay +sharon,sha,shay +sheila,cecilia +sheldon,shelly +shelton,tony,shel,shelly +sheridan,dan,danny,sher +sheryl,sher +shirley,sherry,lee,shirl +sibbilla,sybill,sibbie,sibbell +sidney,syd,sid +sigfired,sid +sigfrid,sid +sigismund,sig +silas,si +silence,liley +silvester,vester,si,sly,vest,syl +simeon,si,sion +simon,si,sion +smith,smitty +socrates,crate +solomon,sal,salmon,sol,solly,saul,zolly +sondra,dre,sonnie +sophia,sophie +sophronia,frona,sophia,fronia +stephan,steve +stephanie,stephen,stephie,annie,steph +stephen,steve,steph +steve,stevie +steven,steve,steph,stevie +stuart,stu +sue,susie,susan +sullivan,sully,van +sully,sullivan +susan,hannah,susie,sue,sukey,suzie +susannah,hannah,susie,sue,sukey +susie,suzie +suzanne,suki,sue,susie +sybill,sibbie +sydney,sid +sylvanus,sly,syl +sylvester,sy,sly,vet,syl,vester,si,vessie +tabby,tabitha +tabitha,tabby +tamarra,tammy +tanafra,tanny +tasha,tash,tashie +ted,teddy +temperance,tempy +terence,terry +teresa,terry +terry,terence +tess,teresa,theresa +tessa,teresa,theresa +thad,thaddeus +thaddeus,thad +theo,theodore +theodora,dora +theodore,theo,ted,teddy +theodosia,theo,dosia,theodosius +theophilus,ophi +theotha,otha +theresa,tessie,thirza,tessa,terry,tracy,tess,thursa +thom,thomas,tommy,tom +thomas,thom,tommy,tom +thomasa,tamzine +tiffany,tiff,tiffy +tilford,tillie +tim,timothy,timmy +timmy,timothy +timothy,tim,timmy +tina,christina +tobias,bias,toby +tom,thomas,tommy +tommy,thomas +tony,anthony +tranquilla,trannie,quilla +trisha,patricia +trix,trixie +trudy,gertrude +tryphena,phena +unice,eunice,nicie +uriah,riah +ursula,sulie,sula +valentina,felty,vallie,val +valentine,felty +valeri,val +valerie,val +vanburen,buren +vandalia,vannie +vanessa,essa,vanna,nessa +vernisee,nicey +veronica,vonnie,ron,ronna,ronie,frony,franky,ronnie +vic,vicki,vicky +vicki,vicky,victoria +vickie,victoria +vicky,victoria +victor,vic,vickie,vicky +victoria,torie,vic,vicki,tory,vicky +vijay,vij +vincent,vic,vince,vinnie,vin,vinny +vincenzo,vic,vinnie,vin,vinny,vince +vinson,vinny,vinnie,vin +viola,ola,vi +violetta,lettie +virginia,jane,jennie,ginny,virgy,ginger +vivian,vi,viv +waldo,ozzy,ossy +wallace,wally +wally,walt +walter,wally,walt +washington,wash +webster,webb +wendy,wen +wesley,wes +westley,west,wes,farmboy +wilber,will,bert +wilbur,willy,willie +wilda,willie +wilfred,will,willie,fred,wil +wilhelm,wil,willie +wilhelmina,mina,wilma,willie,minnie +will,bill,willie,wilbur,fred +william,willy,bell,bela,bill,will,billy,willie,wil +willie,william,fred +willis,willy,bill +wilma,william,billiewilhelm +wilson,will,willy,willie +winfield,field,winny,win +winifred,freddie,winnie,winnet +winnie,winnifred +winnifred,freddie,freddy,winny,winnie,fred +winny,winnifred +winton,wint +woodrow,woody,wood,drew +yeona,onie,ona +yoshihiko,yoshi +yulan,lan,yul +yvonne,vonna +zach,zachariah +zachariah,zachy,zach,zeke,zac,zack,zak,zakk +zachary,zachy,zach,zeke,zac,zack,zak,zakk +zachery,zachy,zach,zeke,zac,zack,zak,zakk +zebedee,zeb +zedediah,dyer,zed,diah +zephaniah,zeph diff --git a/hapi-fhir-jpaserver-searchparam/src/test/java/ca/uhn/fhir/jpa/searchparam/nickname/NicknameInterceptorTest.java b/hapi-fhir-jpaserver-searchparam/src/test/java/ca/uhn/fhir/jpa/searchparam/nickname/NicknameInterceptorTest.java new file mode 100644 index 00000000000..d41f1d1569a --- /dev/null +++ b/hapi-fhir-jpaserver-searchparam/src/test/java/ca/uhn/fhir/jpa/searchparam/nickname/NicknameInterceptorTest.java @@ -0,0 +1,59 @@ +package ca.uhn.fhir.jpa.searchparam.nickname; + +import ca.uhn.fhir.jpa.searchparam.SearchParameterMap; +import ca.uhn.fhir.rest.param.StringParam; +import org.junit.jupiter.api.Test; + +import java.io.IOException; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class NicknameInterceptorTest { + @Test + public void testExpandForward() throws IOException { + // setup + String formalName = "kenneth"; + SearchParameterMap sp = new SearchParameterMap(); + sp.add("name", new StringParam(formalName).setNicknameExpand(true)); + NicknameInterceptor svc = new NicknameInterceptor(); + + // execute + svc.expandNicknames(sp); + + // verify + String newSearch = sp.toNormalizedQueryString(null); + assertEquals("?name=ken,kendrick,kenneth,kenny", newSearch); + } + + @Test + public void testExpandBackward() throws IOException { + // setup + String nickname = "ken"; + SearchParameterMap sp = new SearchParameterMap(); + sp.add("name", new StringParam(nickname).setNicknameExpand(true)); + NicknameInterceptor svc = new NicknameInterceptor(); + + // execute + svc.expandNicknames(sp); + + // verify + String newSearch = sp.toNormalizedQueryString(null); + assertEquals("?name=ken,kendall,kendrick,kendrik,kenneth,kenny,kent,mckenna", newSearch); + } + + @Test + public void testNothingToExpand() throws IOException { + // setup + String unusualName = "X Æ A-12"; + SearchParameterMap sp = new SearchParameterMap(); + sp.add("name", new StringParam(unusualName).setNicknameExpand(true)); + NicknameInterceptor svc = new NicknameInterceptor(); + + // execute + svc.expandNicknames(sp); + + // verify + String newSearch = sp.toNormalizedQueryString(null); + assertEquals("?name=x%20%C3%A6%20a-12", newSearch); + } +} diff --git a/hapi-fhir-jpaserver-searchparam/src/test/java/ca/uhn/fhir/jpa/searchparam/nickname/NicknameMapTest.java b/hapi-fhir-jpaserver-searchparam/src/test/java/ca/uhn/fhir/jpa/searchparam/nickname/NicknameMapTest.java new file mode 100644 index 00000000000..e57ea8c549a --- /dev/null +++ b/hapi-fhir-jpaserver-searchparam/src/test/java/ca/uhn/fhir/jpa/searchparam/nickname/NicknameMapTest.java @@ -0,0 +1,30 @@ +package ca.uhn.fhir.jpa.searchparam.nickname; + +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.io.StringReader; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.junit.jupiter.api.Assertions.assertEquals; + +class NicknameMapTest { + @Test + public void testLoad() throws IOException { + String testData = """ +kendall,ken,kenny +kendra,kenj,kenji,kay,kenny +kendrick,ken,kenny +kendrik,ken,kenny +kenneth,ken,kenny,kendrick +kenny,ken,kenneth +kent,ken,kenny,kendrick + """; + NicknameMap map = new NicknameMap(); + map.load(new StringReader(testData)); + assertEquals(7, map.size()); + assertThat(map.getNicknamesFromFormalNameOrNull("kenneth"), containsInAnyOrder("ken", "kenny", "kendrick")); + assertThat(map.getFormalNamesFromNicknameOrNull("ken"), containsInAnyOrder("kendall", "kendrick", "kendrik", "kenneth", "kenny", "kent")); + } +} diff --git a/hapi-fhir-jpaserver-searchparam/src/test/java/ca/uhn/fhir/jpa/searchparam/nickname/NicknameSvcTest.java b/hapi-fhir-jpaserver-searchparam/src/test/java/ca/uhn/fhir/jpa/searchparam/nickname/NicknameSvcTest.java new file mode 100644 index 00000000000..80d24b88559 --- /dev/null +++ b/hapi-fhir-jpaserver-searchparam/src/test/java/ca/uhn/fhir/jpa/searchparam/nickname/NicknameSvcTest.java @@ -0,0 +1,15 @@ +package ca.uhn.fhir.jpa.searchparam.nickname; + +import org.junit.jupiter.api.Test; + +import java.io.IOException; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class NicknameSvcTest { + @Test + public void testReadfile() throws IOException { + NicknameSvc nicknameSvc = new NicknameSvc(); + assertEquals(1082, nicknameSvc.size()); + } +} diff --git a/hapi-fhir-jpaserver-test-utilities/src/test/java/ca/uhn/fhir/jpa/dao/TestDaoSearch.java b/hapi-fhir-jpaserver-test-utilities/src/test/java/ca/uhn/fhir/jpa/dao/TestDaoSearch.java index 32601274276..3203bfe3857 100644 --- a/hapi-fhir-jpaserver-test-utilities/src/test/java/ca/uhn/fhir/jpa/dao/TestDaoSearch.java +++ b/hapi-fhir-jpaserver-test-utilities/src/test/java/ca/uhn/fhir/jpa/dao/TestDaoSearch.java @@ -19,6 +19,7 @@ import org.springframework.web.util.UriComponentsBuilder; import javax.annotation.Nonnull; import java.util.List; +import java.util.stream.Collectors; /** * Simplistic implementation of FHIR queries. @@ -51,11 +52,13 @@ public class TestDaoSearch { return result.getAllResources(); } - public List searchForIds(String theQueryUrl) { + public List searchForIds(String theQueryUrl) { // fake out the server url parsing IBundleProvider result = searchForBundleProvider(theQueryUrl); - List resourceIds = result.getAllResourceIds(); + // getAllResources is not safe as size is not always set + List resourceIds = result.getResources(0, Integer.MAX_VALUE) + .stream().map(resource -> resource.getIdElement().getIdPart()).collect(Collectors.toList()); return resourceIds; } diff --git a/hapi-fhir-jpaserver-test-utilities/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4ConceptMapTest.java b/hapi-fhir-jpaserver-test-utilities/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4ConceptMapTest.java index 55f95f103dd..ccd56ef59fd 100644 --- a/hapi-fhir-jpaserver-test-utilities/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4ConceptMapTest.java +++ b/hapi-fhir-jpaserver-test-utilities/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4ConceptMapTest.java @@ -12,6 +12,7 @@ import ca.uhn.fhir.jpa.test.BaseJpaR4Test; import org.hl7.fhir.instance.model.api.IIdType; import org.hl7.fhir.r4.model.CanonicalType; import org.hl7.fhir.r4.model.CodeableConcept; +import org.hl7.fhir.r4.model.Coding; import org.hl7.fhir.r4.model.ConceptMap; import org.hl7.fhir.r4.model.Enumerations.ConceptMapEquivalence; import org.hl7.fhir.r4.model.Enumerations.PublicationStatus; @@ -30,6 +31,7 @@ import org.springframework.transaction.support.TransactionTemplate; import javax.annotation.Nonnull; import java.io.IOException; +import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Optional; @@ -1139,7 +1141,9 @@ public class FhirResourceDaoR4ConceptMapTest extends BaseJpaR4Test { }); - List translationResults = myValidationSupport.translateConcept(new IValidationSupport.TranslateCodeRequest("http://source", "source1", "http://target")).getResults(); + CodeableConcept sourceCodeableConcept = new CodeableConcept(); + sourceCodeableConcept.addCoding(new Coding("http://source", "source1", null)); + List translationResults = myValidationSupport.translateConcept(new IValidationSupport.TranslateCodeRequest(Collections.unmodifiableList(sourceCodeableConcept.getCoding()), "http://target")).getResults(); assertThat(translationResults.toString(), translationResults, hasItem( new TranslateConceptResult() .setSystem("http://target") diff --git a/hapi-fhir-jpaserver-test-utilities/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4SearchWithElasticSearchIT.java b/hapi-fhir-jpaserver-test-utilities/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4SearchWithElasticSearchIT.java index 21951dae75d..bab3e47ebf9 100644 --- a/hapi-fhir-jpaserver-test-utilities/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4SearchWithElasticSearchIT.java +++ b/hapi-fhir-jpaserver-test-utilities/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4SearchWithElasticSearchIT.java @@ -30,6 +30,7 @@ import ca.uhn.fhir.jpa.test.config.TestR4Config; import ca.uhn.fhir.parser.DataFormatException; import ca.uhn.fhir.parser.IParser; import ca.uhn.fhir.rest.api.Constants; +import ca.uhn.fhir.rest.api.SearchTotalModeEnum; import ca.uhn.fhir.rest.api.server.IBundleProvider; import ca.uhn.fhir.rest.api.server.storage.ResourcePersistentId; import ca.uhn.fhir.rest.param.ReferenceParam; @@ -45,6 +46,7 @@ import ca.uhn.fhir.test.utilities.LogbackLevelOverrideExtension; import ca.uhn.fhir.test.utilities.docker.RequiresDocker; import ca.uhn.fhir.validation.FhirValidator; import ca.uhn.fhir.validation.ValidationResult; +import com.google.common.collect.Lists; import org.hamcrest.Matchers; import org.hl7.fhir.instance.model.api.IBaseCoding; import org.hl7.fhir.instance.model.api.IBaseResource; @@ -71,6 +73,8 @@ import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.test.annotation.DirtiesContext; @@ -84,8 +88,11 @@ import org.springframework.transaction.PlatformTransactionManager; import javax.persistence.EntityManager; import java.io.IOException; +import java.time.Month; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import java.util.function.Consumer; import java.util.stream.Collectors; import static ca.uhn.fhir.jpa.model.util.UcumServiceUtil.UCUM_CODESYSTEM_URL; @@ -99,6 +106,7 @@ import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.stringContainsInOrder; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -1472,9 +1480,9 @@ public class FhirResourceDaoR4SearchWithElasticSearchIT extends BaseJpaTest { } private IIdType withObservationWithQuantity(double theValue, String theSystem, String theCode) { - myResourceId = myTestDataBuilder.createObservation( + myResourceId = myTestDataBuilder.createObservation(asArray( myTestDataBuilder.withQuantityAtPath("valueQuantity", theValue, theSystem, theCode) - ); + )); return myResourceId; } @@ -1490,6 +1498,87 @@ public class FhirResourceDaoR4SearchWithElasticSearchIT extends BaseJpaTest { } + + @Nested + public class TotalParameter { + + @ParameterizedTest + @EnumSource(SearchTotalModeEnum.class) + public void totalParamSkipsSql(SearchTotalModeEnum theTotalModeEnum) { + myTestDataBuilder.createObservation(asArray(myTestDataBuilder.withObservationCode("http://example.com/", "theCode"))); + + myCaptureQueriesListener.clear(); + myTestDaoSearch.searchForIds("Observation?code=theCode&_total=" + theTotalModeEnum); + myCaptureQueriesListener.logSelectQueriesForCurrentThread(); + assertEquals(1, myCaptureQueriesListener.getSelectQueriesForCurrentThread().size(), "bundle was built with no sql"); + } + + + @Test + public void totalIsCorrect() { + myTestDataBuilder.createObservation(asArray(myTestDataBuilder.withObservationCode("http://example.com/", "code-1"))); + myTestDataBuilder.createObservation(asArray(myTestDataBuilder.withObservationCode("http://example.com/", "code-2"))); + myTestDataBuilder.createObservation(asArray(myTestDataBuilder.withObservationCode("http://example.com/", "code-3"))); + + IBundleProvider resultBundle = myTestDaoSearch.searchForBundleProvider("Observation?_total=" + SearchTotalModeEnum.ACCURATE); + assertEquals(3, resultBundle.size()); + } + + } + + + @Nested + public class OffsetParameter { + + @BeforeEach + public void enableResourceStorage() { + myDaoConfig.setStoreResourceInLuceneIndex(true); + } + + + @Test + public void offsetNoCount() { + myTestDataBuilder.createObservation(asArray(myTestDataBuilder.withObservationCode("http://example.com/", "code-1"))); + IIdType idCode2 = myTestDataBuilder.createObservation(asArray(myTestDataBuilder.withObservationCode("http://example.com/", "code-2"))); + IIdType idCode3 = myTestDataBuilder.createObservation(asArray(myTestDataBuilder.withObservationCode("http://example.com/", "code-3"))); + + myCaptureQueriesListener.clear(); + List resultIds = myTestDaoSearch.searchForIds("Observation?code=code-1,code-2,code-3&_offset=1"); + myCaptureQueriesListener.logSelectQueriesForCurrentThread(); + + assertThat(resultIds, containsInAnyOrder(idCode2.getIdPart(), idCode3.getIdPart())); + // make also sure no extra SQL queries were executed + assertEquals(0, myCaptureQueriesListener.getSelectQueriesForCurrentThread().size(), "bundle was built with no sql"); + } + + + @Test + public void offsetAndCount() { + myTestDataBuilder.createObservation(asArray(myTestDataBuilder.withObservationCode("http://example.com/", "code-1"))); + IIdType idCode2 = myTestDataBuilder.createObservation(asArray(myTestDataBuilder.withObservationCode("http://example.com/", "code-2"))); + myTestDataBuilder.createObservation(asArray(myTestDataBuilder.withObservationCode("http://example.com/", "code-3"))); + + myCaptureQueriesListener.clear(); + List resultIds = myTestDaoSearch.searchForIds("Observation?code=code-1,code-2,code-3&_offset=1&_count=1"); + myCaptureQueriesListener.logSelectQueriesForCurrentThread(); + + assertThat(resultIds, containsInAnyOrder(idCode2.getIdPart())); + // also validate no extra SQL queries were executed + assertEquals(0, myCaptureQueriesListener.getSelectQueriesForCurrentThread().size(), "bundle was built with no sql"); + } + } + + + private Consumer[] asArray(Consumer theIBaseResourceConsumer) { + @SuppressWarnings("unchecked") + Consumer[] array = (Consumer[]) new Consumer[]{theIBaseResourceConsumer}; + return array; + } + + + + + /** * Disallow context dirtying for nested classes */ diff --git a/hapi-fhir-jpaserver-test-utilities/src/test/java/ca/uhn/fhir/jpa/provider/r4/NicknameSearchR4Test.java b/hapi-fhir-jpaserver-test-utilities/src/test/java/ca/uhn/fhir/jpa/provider/r4/NicknameSearchR4Test.java new file mode 100644 index 00000000000..6a3054c9e27 --- /dev/null +++ b/hapi-fhir-jpaserver-test-utilities/src/test/java/ca/uhn/fhir/jpa/provider/r4/NicknameSearchR4Test.java @@ -0,0 +1,57 @@ +package ca.uhn.fhir.jpa.provider.r4; + +import ca.uhn.fhir.jpa.searchparam.nickname.NicknameInterceptor; +import ca.uhn.fhir.util.BundleUtil; +import org.hl7.fhir.r4.model.Bundle; +import org.hl7.fhir.r4.model.Patient; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.List; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasSize; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class NicknameSearchR4Test extends BaseResourceProviderR4Test { + @Autowired + NicknameInterceptor myNicknameInterceptor; + + @Override + @AfterEach + public void after() throws Exception { + super.after(); + + myInterceptorRegistry.unregisterInterceptor(myNicknameInterceptor); + } + + @BeforeEach + @Override + public void before() throws Exception { + super.before(); + myInterceptorRegistry.registerInterceptor(myNicknameInterceptor); + } + + @Test + public void testExpandNickname() { + Patient patient1 = new Patient(); + patient1.getNameFirstRep().addGiven("ken"); + myClient.create().resource(patient1).execute(); + + Patient patient2 = new Patient(); + patient2.getNameFirstRep().addGiven("bob"); + myClient.create().resource(patient2).execute(); + + Bundle result = myClient + .loadPage() + .byUrl(ourServerBase + "/Patient?name:nickname=kenneth") + .andReturnBundle(Bundle.class) + .execute(); + + List resources = BundleUtil.toListOfResourcesOfType(myFhirContext,result, Patient.class); + assertThat(resources, hasSize(1)); + assertEquals("ken", resources.get(0).getNameFirstRep().getGivenAsSingleString()); + } +} diff --git a/hapi-fhir-jpaserver-test-utilities/src/test/java/ca/uhn/fhir/jpa/provider/r4/ResourceProviderR4ElasticTest.java b/hapi-fhir-jpaserver-test-utilities/src/test/java/ca/uhn/fhir/jpa/provider/r4/ResourceProviderR4ElasticTest.java index 49ab785b3d4..83b03d59f40 100644 --- a/hapi-fhir-jpaserver-test-utilities/src/test/java/ca/uhn/fhir/jpa/provider/r4/ResourceProviderR4ElasticTest.java +++ b/hapi-fhir-jpaserver-test-utilities/src/test/java/ca/uhn/fhir/jpa/provider/r4/ResourceProviderR4ElasticTest.java @@ -36,6 +36,7 @@ import java.time.Instant; import java.util.Date; import java.util.List; import java.util.Objects; +import java.util.stream.IntStream; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.hasItem; @@ -43,6 +44,7 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.nullValue; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; @ExtendWith(SpringExtension.class) @@ -148,7 +150,7 @@ public class ResourceProviderR4ElasticTest extends BaseResourceProviderR4Test { .useHttpGet() .execute(); - assertEquals( 1, respParam.getParameter().size(), "Expected only 1 observation for blood count code"); + assertEquals(1, respParam.getParameter().size(), "Expected only 1 observation for blood count code"); Bundle bundle = (Bundle) respParam.getParameter().get(0).getResource(); Observation observation = (Observation) bundle.getEntryFirstRep().getResource(); @@ -157,4 +159,47 @@ public class ResourceProviderR4ElasticTest extends BaseResourceProviderR4Test { } + @Test + public void testCountReturnsExpectedSizeOfResources() throws IOException { + IntStream.range(0, 10).forEach(index -> { + Coding blood_count = new Coding("http://loinc.org", "789-8", "Erythrocytes in Blood by Automated count for code: " + (index + 1)); + createObservationWithCode(blood_count); + }); + HttpGet countQuery = new HttpGet(ourServerBase + "/Observation?code=789-8&_count=5"); + myCaptureQueriesListener.clear(); + try (CloseableHttpResponse response = ourHttpClient.execute(countQuery)) { + myCaptureQueriesListener.logSelectQueriesForCurrentThread(); + // then + assertEquals(Constants.STATUS_HTTP_200_OK, response.getStatusLine().getStatusCode()); + String text = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8); + Bundle bundle = myFhirContext.newXmlParser().parseResource(Bundle.class, text); + assertEquals(10, bundle.getTotal(), "Expected total 10 observations matching query"); + assertEquals(5, bundle.getEntry().size(), "Expected 5 observation entries to match page size"); + assertTrue(bundle.getLink("next").hasRelation()); + assertEquals(0, myCaptureQueriesListener.getSelectQueriesForCurrentThread().size(), "we build the bundle with no sql"); + } + } + + @Test + public void testCountZeroReturnsNoResourceEntries() throws IOException { + IntStream.range(0, 10).forEach(index -> { + Coding blood_count = new Coding("http://loinc.org", "789-8", "Erythrocytes in Blood by Automated count for code: " + (index + 1)); + createObservationWithCode(blood_count); + }); + HttpGet countQuery = new HttpGet(ourServerBase + "/Observation?code=789-8&_count=0"); + myCaptureQueriesListener.clear(); + try (CloseableHttpResponse response = ourHttpClient.execute(countQuery)) { + myCaptureQueriesListener.logSelectQueriesForCurrentThread(); + assertEquals(Constants.STATUS_HTTP_200_OK, response.getStatusLine().getStatusCode()); + String text = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8); + Bundle bundle = myFhirContext.newXmlParser().parseResource(Bundle.class, text); + assertEquals(10, bundle.getTotal(), "Expected total 10 observations matching query"); + assertEquals(0, bundle.getEntry().size(), "Expected no entries in bundle"); + assertNull(bundle.getLink("next"), "Expected no 'next' link"); + assertNull(bundle.getLink("prev"), "Expected no 'prev' link"); + assertEquals(0, myCaptureQueriesListener.getSelectQueriesForCurrentThread().size(), "we build the bundle with no sql"); + } + + } + } diff --git a/hapi-fhir-jpaserver-test-utilities/src/test/java/ca/uhn/fhir/jpa/search/SearchCoordinatorSvcImplTest.java b/hapi-fhir-jpaserver-test-utilities/src/test/java/ca/uhn/fhir/jpa/search/SearchCoordinatorSvcImplTest.java index d8d963181c7..fe213117bfc 100644 --- a/hapi-fhir-jpaserver-test-utilities/src/test/java/ca/uhn/fhir/jpa/search/SearchCoordinatorSvcImplTest.java +++ b/hapi-fhir-jpaserver-test-utilities/src/test/java/ca/uhn/fhir/jpa/search/SearchCoordinatorSvcImplTest.java @@ -561,7 +561,7 @@ public class SearchCoordinatorSvcImplTest { params.setSearchTotalMode(SearchTotalModeEnum.ACCURATE); List pids = createPidSequence(30); - when(mySearchBuilder.createCountQuery(same(params), any(String.class), nullable(RequestDetails.class), nullable(RequestPartitionId.class))).thenReturn(Lists.newArrayList(Long.valueOf(20L)).iterator()); + when(mySearchBuilder.createCountQuery(same(params), any(String.class),nullable(RequestDetails.class), nullable(RequestPartitionId.class))).thenReturn(20L); when(mySearchBuilder.createQuery(same(params), any(), nullable(RequestDetails.class), nullable(RequestPartitionId.class))).thenReturn(new ResultIterator(pids.subList(10, 20).iterator())); doAnswer(loadPids()).when(mySearchBuilder).loadResourcesByPid(any(Collection.class), any(Collection.class), any(List.class), anyBoolean(), any()); diff --git a/hapi-fhir-jpaserver-test-utilities/src/test/java/ca/uhn/fhir/jpa/term/TermConceptMappingSvcImplTest.java b/hapi-fhir-jpaserver-test-utilities/src/test/java/ca/uhn/fhir/jpa/term/TermConceptMappingSvcImplTest.java index f6d36d5cec6..8685b4d96ab 100644 --- a/hapi-fhir-jpaserver-test-utilities/src/test/java/ca/uhn/fhir/jpa/term/TermConceptMappingSvcImplTest.java +++ b/hapi-fhir-jpaserver-test-utilities/src/test/java/ca/uhn/fhir/jpa/term/TermConceptMappingSvcImplTest.java @@ -1,5 +1,6 @@ package ca.uhn.fhir.jpa.term; +import ca.uhn.fhir.context.support.IValidationSupport; import ca.uhn.fhir.context.support.TranslateConceptResult; import ca.uhn.fhir.context.support.TranslateConceptResults; import ca.uhn.fhir.i18n.Msg; @@ -8,14 +9,19 @@ import ca.uhn.fhir.jpa.entity.TermConceptMap; import ca.uhn.fhir.jpa.entity.TermConceptMapGroup; import ca.uhn.fhir.jpa.entity.TermConceptMapGroupElement; import ca.uhn.fhir.jpa.entity.TermConceptMapGroupElementTarget; +import ca.uhn.fhir.jpa.term.api.ITermConceptMappingSvc; import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; import org.hl7.fhir.instance.model.api.IIdType; import org.hl7.fhir.r4.model.CanonicalType; +import org.hl7.fhir.r4.model.CodeableConcept; +import org.hl7.fhir.r4.model.Coding; import org.hl7.fhir.r4.model.ConceptMap; import org.hl7.fhir.r4.model.Enumerations; +import org.hl7.fhir.r4.model.StringType; import org.hl7.fhir.r4.model.UriType; import org.hl7.fhir.r4.model.codesystems.HttpVerb; import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.data.domain.PageRequest; @@ -25,6 +31,7 @@ import org.springframework.transaction.support.TransactionCallbackWithoutResult; import org.springframework.transaction.support.TransactionTemplate; import javax.annotation.Nonnull; +import java.util.Collections; import java.util.List; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -32,6 +39,11 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; public class TermConceptMappingSvcImplTest extends BaseTermR4Test { private static final Logger ourLog = LoggerFactory.getLogger(TermConceptMappingSvcImplTest.class); @@ -1547,6 +1559,100 @@ public class TermConceptMappingSvcImplTest extends BaseTermR4Test { }); } + @Test + public void testTranslateCodeRequestToTranslationRequestMapping() { + CodeableConcept codeableConcept = new CodeableConcept(); + Coding coding = new Coding("theSourceSystemUrl", "theSourceCode", null); + codeableConcept.addCoding(coding); + + IValidationSupport.TranslateCodeRequest theRequest = new IValidationSupport.TranslateCodeRequest( + Collections.unmodifiableList(codeableConcept.getCoding()), + "theTargetSystemUrl", + "theConceptMapUrl", + "theConceptMapVersion", + "theSourceValueSetUrl", + "theTargetValueSetUrl", + 0L, + false + ); + + CodeableConcept sourceCodeableConcept = new CodeableConcept(); + sourceCodeableConcept + .addCoding() + .setSystem(coding.getSystem()) + .setCode(coding.getCode()); + + TranslationRequest expected = new TranslationRequest(); + expected.setCodeableConcept(sourceCodeableConcept); + expected.setConceptMapVersion(new StringType(theRequest.getConceptMapVersion())); + expected.setUrl(new UriType(theRequest.getConceptMapUrl())); + expected.setSource(new UriType(theRequest.getSourceValueSetUrl())); + expected.setTarget(new UriType(theRequest.getTargetValueSetUrl())); + expected.setTargetSystem(new UriType(theRequest.getTargetSystemUrl())); + expected.setResourceId(theRequest.getResourcePid()); + expected.setReverse(theRequest.isReverse()); + + ITermConceptMappingSvc mock = mock(TermConceptMappingSvcImpl.class); + ArgumentCaptor argument = ArgumentCaptor.forClass(TranslationRequest.class); + when(mock.translate(expected)).thenReturn(new TranslateConceptResults()); + when(mock.translateConcept(theRequest)).thenCallRealMethod(); + mock.translateConcept(theRequest); + verify(mock).translate(argument.capture()); + assertSameTranslationRequest(expected, argument.getValue()); + } + + @Test + public void testTranslateCodeRequestWithReverseToTranslationRequestMapping() { + CodeableConcept codeableConcept = new CodeableConcept(); + Coding coding = new Coding("theSourceSystemUrl", "theSourceCode", null); + codeableConcept.addCoding(coding); + + IValidationSupport.TranslateCodeRequest theRequest = new IValidationSupport.TranslateCodeRequest( + Collections.unmodifiableList(codeableConcept.getCoding()), + "theTargetSystemUrl", + "theConceptMapUrl", + "theConceptMapVersion", + "theSourceValueSetUrl", + "theTargetValueSetUrl", + 0L, + true + ); + + CodeableConcept sourceCodeableConcept = new CodeableConcept(); + sourceCodeableConcept + .addCoding() + .setSystem(coding.getSystem()) + .setCode(coding.getCode()); + + TranslationRequest expected = new TranslationRequest(); + expected.setCodeableConcept(sourceCodeableConcept); + expected.setConceptMapVersion(new StringType(theRequest.getConceptMapVersion())); + expected.setUrl(new UriType(theRequest.getConceptMapUrl())); + expected.setSource(new UriType(theRequest.getSourceValueSetUrl())); + expected.setTarget(new UriType(theRequest.getTargetValueSetUrl())); + expected.setTargetSystem(new UriType(theRequest.getTargetSystemUrl())); + expected.setResourceId(theRequest.getResourcePid()); + expected.setReverse(theRequest.isReverse()); + + ITermConceptMappingSvc mock = mock(TermConceptMappingSvcImpl.class); + ArgumentCaptor argument = ArgumentCaptor.forClass(TranslationRequest.class); + when(mock.translate(expected)).thenReturn(new TranslateConceptResults()); + when(mock.translateConcept(theRequest)).thenCallRealMethod(); + mock.translateConcept(theRequest); + verify(mock).translateWithReverse(argument.capture()); + assertSameTranslationRequest(expected, argument.getValue()); + } + + private static void assertSameTranslationRequest(TranslationRequest expected, TranslationRequest actual) { + assertTrue(expected.getCodeableConcept().equalsDeep(actual.getCodeableConcept())); + assertEquals(expected.getConceptMapVersion().asStringValue(), actual.getConceptMapVersion().asStringValue()); + assertEquals(expected.getUrl().asStringValue(), actual.getUrl().asStringValue()); + assertEquals(expected.getSource().asStringValue(), actual.getSource().asStringValue()); + assertEquals(expected.getTarget().asStringValue(), actual.getTarget().asStringValue()); + assertEquals(expected.getTargetSystem().asStringValue(), actual.getTargetSystem().asStringValue()); + assertEquals(expected.getResourceId(), actual.getResourceId()); + assertEquals(expected.getReverseAsBoolean(), actual.getReverseAsBoolean()); + } private void createAndPersistConceptMap() { ConceptMap conceptMap = createConceptMap(); diff --git a/hapi-fhir-server-mdm/src/main/java/ca/uhn/fhir/mdm/rules/config/MdmRuleValidator.java b/hapi-fhir-server-mdm/src/main/java/ca/uhn/fhir/mdm/rules/config/MdmRuleValidator.java index 75f11d366a3..0e2d2b3d31e 100644 --- a/hapi-fhir-server-mdm/src/main/java/ca/uhn/fhir/mdm/rules/config/MdmRuleValidator.java +++ b/hapi-fhir-server-mdm/src/main/java/ca/uhn/fhir/mdm/rules/config/MdmRuleValidator.java @@ -20,14 +20,14 @@ package ca.uhn.fhir.mdm.rules.config; * #L% */ -import ca.uhn.fhir.i18n.Msg; import ca.uhn.fhir.context.ConfigurationException; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.FhirVersionEnum; import ca.uhn.fhir.context.RuntimeResourceDefinition; import ca.uhn.fhir.fhirpath.IFhirPath; -import ca.uhn.fhir.mdm.api.MdmConstants; +import ca.uhn.fhir.i18n.Msg; import ca.uhn.fhir.mdm.api.IMdmRuleValidator; +import ca.uhn.fhir.mdm.api.MdmConstants; import ca.uhn.fhir.mdm.rules.json.MdmFieldMatchJson; import ca.uhn.fhir.mdm.rules.json.MdmFilterSearchParamJson; import ca.uhn.fhir.mdm.rules.json.MdmResourceSearchParamJson; @@ -36,6 +36,7 @@ import ca.uhn.fhir.mdm.rules.json.MdmSimilarityJson; import ca.uhn.fhir.parser.DataFormatException; import ca.uhn.fhir.rest.server.util.ISearchParamRegistry; import ca.uhn.fhir.util.FhirTerser; +import ca.uhn.fhir.util.SearchParameterUtil; import org.hl7.fhir.instance.model.api.IBaseResource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -150,7 +151,8 @@ public class MdmRuleValidator implements IMdmRuleValidator { } private void validateResourceSearchParam(String theFieldName, String theResourceType, String theSearchParam) { - if (mySearchParamRetriever.getActiveSearchParam(theResourceType, theSearchParam) == null) { + String searchParam = SearchParameterUtil.stripModifier(theSearchParam); + if (mySearchParamRetriever.getActiveSearchParam(theResourceType, searchParam) == null) { throw new ConfigurationException(Msg.code(1511) + "Error in " + theFieldName + ": " + theResourceType + " does not have a search parameter called '" + theSearchParam + "'"); } } diff --git a/hapi-fhir-server-mdm/src/main/java/ca/uhn/fhir/mdm/rules/matcher/MdmMatcherEnum.java b/hapi-fhir-server-mdm/src/main/java/ca/uhn/fhir/mdm/rules/matcher/MdmMatcherEnum.java index da3a06167ed..395b06d7a66 100644 --- a/hapi-fhir-server-mdm/src/main/java/ca/uhn/fhir/mdm/rules/matcher/MdmMatcherEnum.java +++ b/hapi-fhir-server-mdm/src/main/java/ca/uhn/fhir/mdm/rules/matcher/MdmMatcherEnum.java @@ -40,6 +40,7 @@ public enum MdmMatcherEnum { NYSIIS(new HapiStringMatcher(new PhoneticEncoderMatcher(PhoneticEncoderEnum.NYSIIS))), REFINED_SOUNDEX(new HapiStringMatcher(new PhoneticEncoderMatcher(PhoneticEncoderEnum.REFINED_SOUNDEX))), SOUNDEX(new HapiStringMatcher(new PhoneticEncoderMatcher(PhoneticEncoderEnum.SOUNDEX))), + NICKNAME(new HapiStringMatcher(new NicknameMatcher())), STRING(new HapiStringMatcher()), SUBSTRING(new HapiStringMatcher(new SubstringStringMatcher())), diff --git a/hapi-fhir-server-mdm/src/main/java/ca/uhn/fhir/mdm/rules/matcher/NicknameMatcher.java b/hapi-fhir-server-mdm/src/main/java/ca/uhn/fhir/mdm/rules/matcher/NicknameMatcher.java new file mode 100644 index 00000000000..f4a3fe8df57 --- /dev/null +++ b/hapi-fhir-server-mdm/src/main/java/ca/uhn/fhir/mdm/rules/matcher/NicknameMatcher.java @@ -0,0 +1,30 @@ +package ca.uhn.fhir.mdm.rules.matcher; + +import ca.uhn.fhir.context.ConfigurationException; +import ca.uhn.fhir.i18n.Msg; +import ca.uhn.fhir.jpa.searchparam.nickname.NicknameSvc; + +import java.io.IOException; +import java.util.List; +import java.util.Locale; + +public class NicknameMatcher implements IMdmStringMatcher { + private final NicknameSvc myNicknameSvc; + + public NicknameMatcher() { + try { + myNicknameSvc = new NicknameSvc(); + } catch (IOException e) { + throw new ConfigurationException(Msg.code(2078) + "Unable to load nicknames", e); + } + } + + @Override + public boolean matches(String theLeftString, String theRightString) { + String leftString = theLeftString.toLowerCase(Locale.ROOT); + String rightString = theRightString.toLowerCase(Locale.ROOT); + + List leftNames = myNicknameSvc.getEquivalentNames(leftString); + return leftNames.contains(rightString); + } +} diff --git a/hapi-fhir-server-mdm/src/test/java/ca/uhn/fhir/mdm/rules/matcher/NicknameMatcherTest.java b/hapi-fhir-server-mdm/src/test/java/ca/uhn/fhir/mdm/rules/matcher/NicknameMatcherTest.java new file mode 100644 index 00000000000..89513f9e21e --- /dev/null +++ b/hapi-fhir-server-mdm/src/test/java/ca/uhn/fhir/mdm/rules/matcher/NicknameMatcherTest.java @@ -0,0 +1,25 @@ +package ca.uhn.fhir.mdm.rules.matcher; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class NicknameMatcherTest { + IMdmStringMatcher matcher = new NicknameMatcher(); + + @Test + public void testMatches() { + assertTrue(matcher.matches("Ken", "ken")); + assertTrue(matcher.matches("ken", "Ken")); + assertTrue(matcher.matches("Ken", "Ken")); + assertTrue(matcher.matches("Kenneth", "Ken")); + assertTrue(matcher.matches("Kenneth", "Kenny")); + assertTrue(matcher.matches("Ken", "Kenneth")); + assertTrue(matcher.matches("Kenny", "Kenneth")); + + assertFalse(matcher.matches("Ken", "Bob")); + // These aren't nickname matches. If you want matches like these use a phonetic matcher + assertFalse(matcher.matches("Allen", "Allan")); + } +} diff --git a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/ResponseTerminologyTranslationInterceptor.java b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/ResponseTerminologyTranslationInterceptor.java index 78fb117bf9c..0abef15fc76 100644 --- a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/ResponseTerminologyTranslationInterceptor.java +++ b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/ResponseTerminologyTranslationInterceptor.java @@ -36,9 +36,11 @@ import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.Multimap; import org.apache.commons.lang3.Validate; import org.hl7.fhir.instance.model.api.IBase; +import org.hl7.fhir.instance.model.api.IBaseCoding; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.instance.model.api.IPrimitiveType; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -153,12 +155,17 @@ public class ResponseTerminologyTranslationInterceptor extends BaseResponseTermi if (!foundSystemsToCodes.containsKey(wantTargetSystem)) { for (String code : foundSystemsToCodes.get(nextSourceSystem)) { - TranslateConceptResults translateConceptResults = myValidationSupport.translateConcept(new IValidationSupport.TranslateCodeRequest(nextSourceSystem, code, wantTargetSystem)); + List codings = new ArrayList(); + codings.add(createCodingFromPrimitives(nextSourceSystem, code, null)); + TranslateConceptResults translateConceptResults = myValidationSupport.translateConcept(new IValidationSupport.TranslateCodeRequest(codings, wantTargetSystem)); if (translateConceptResults != null) { List mappings = translateConceptResults.getResults(); for (TranslateConceptResult nextMapping : mappings) { - IBase newCoding = createCodingFromMappingTarget(nextMapping); + IBase newCoding = createCodingFromPrimitives( + nextMapping.getSystem(), + nextMapping.getCode(), + nextMapping.getDisplay()); // Add coding to existing CodeableConcept myCodeableConceptCodingChild.getMutator().addValue(theElement, newCoding); @@ -174,14 +181,14 @@ public class ResponseTerminologyTranslationInterceptor extends BaseResponseTermi } - private IBase createCodingFromMappingTarget(TranslateConceptResult nextMapping) { - IBase newCoding = myCodingDefinitition.newInstance(); - IPrimitiveType newSystem = myUriDefinition.newInstance(nextMapping.getSystem()); + private IBaseCoding createCodingFromPrimitives(String system, String code, String display) { + IBaseCoding newCoding = (IBaseCoding) myCodingDefinitition.newInstance(); + IPrimitiveType newSystem = myUriDefinition.newInstance(system); myCodingSystemChild.getMutator().addValue(newCoding, newSystem); - IPrimitiveType newCode = myCodeDefinition.newInstance(nextMapping.getCode()); + IPrimitiveType newCode = myCodeDefinition.newInstance(code); myCodingCodeChild.getMutator().addValue(newCoding, newCode); - if (isNotBlank(nextMapping.getDisplay())) { - IPrimitiveType newDisplay = myStringDefinition.newInstance(nextMapping.getDisplay()); + if (isNotBlank(display)) { + IPrimitiveType newDisplay = myStringDefinition.newInstance(display); myCodingDisplayChild.getMutator().addValue(newCoding, newDisplay); } return newCoding; diff --git a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/auth/RuleTarget.java b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/auth/RuleTarget.java index f44f15be71e..00361b74a9b 100644 --- a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/auth/RuleTarget.java +++ b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/auth/RuleTarget.java @@ -48,11 +48,24 @@ public class RuleTarget { for (Map.Entry entry : theParameters.entrySet()) { String key = entry.getKey(); String[] value = entry.getValue(); - if (key.endsWith(Constants.PARAMQUALIFIER_MDM)) { - key = key.split(Constants.PARAMQUALIFIER_MDM)[0]; - } + key = stripMdmQualifier(key); + key = stripNicknameQualifier(key); retval.put(key, value); } return retval; } + + private String stripMdmQualifier(String theKey) { + if (theKey.endsWith(Constants.PARAMQUALIFIER_MDM)) { + theKey = theKey.split(Constants.PARAMQUALIFIER_MDM)[0]; + } + return theKey; + } + + private String stripNicknameQualifier(String theKey) { + if (theKey.endsWith(Constants.PARAMQUALIFIER_NICKNAME)) { + theKey = theKey.split(Constants.PARAMQUALIFIER_NICKNAME)[0]; + } + return theKey; + } } diff --git a/hapi-fhir-sql-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTask.java b/hapi-fhir-sql-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTask.java index 629126406cd..e654fc9b4dc 100644 --- a/hapi-fhir-sql-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTask.java +++ b/hapi-fhir-sql-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTask.java @@ -162,6 +162,8 @@ public abstract class BaseTask { private int doExecuteSql(@Language("SQL") String theSql, Object[] theArguments) { JdbcTemplate jdbcTemplate = getConnectionProperties().newJdbcTemplate(); + // 0 means no timeout -- we use this for index rebuilds that may take time. + jdbcTemplate.setQueryTimeout(0); try { int changesCount = jdbcTemplate.update(theSql, theArguments); if (!"true".equals(System.getProperty("unit_test_mode"))) { diff --git a/hapi-fhir-storage/src/main/java/ca/uhn/fhir/jpa/api/model/TranslationRequest.java b/hapi-fhir-storage/src/main/java/ca/uhn/fhir/jpa/api/model/TranslationRequest.java index 5ae233015b4..d89b882e40a 100644 --- a/hapi-fhir-storage/src/main/java/ca/uhn/fhir/jpa/api/model/TranslationRequest.java +++ b/hapi-fhir-storage/src/main/java/ca/uhn/fhir/jpa/api/model/TranslationRequest.java @@ -20,7 +20,9 @@ package ca.uhn.fhir.jpa.api.model; * #L% */ +import ca.uhn.fhir.context.support.IValidationSupport; import org.apache.commons.lang3.Validate; +import org.hl7.fhir.instance.model.api.IBaseCoding; import org.hl7.fhir.r4.model.BooleanType; import org.hl7.fhir.r4.model.CodeableConcept; import org.hl7.fhir.r4.model.Coding; @@ -28,6 +30,7 @@ import org.hl7.fhir.r4.model.StringType; import org.hl7.fhir.r4.model.UriType; import java.util.ArrayList; +import java.util.Collections; import java.util.List; public class TranslationRequest { @@ -208,4 +211,40 @@ public class TranslationRequest { public boolean hasTargetSystem() { return myTargetSystem != null && myTargetSystem.hasValue(); } + + public IValidationSupport.TranslateCodeRequest asTranslateCodeRequest() { + return new IValidationSupport.TranslateCodeRequest( + Collections.unmodifiableList(this.getCodeableConcept().getCoding()), + this.getTargetSystem() != null ? this.getTargetSystem().asStringValue() : null, + this.getUrl() != null ? this.getUrl().asStringValue() : null, + this.getConceptMapVersion() != null ? this.getConceptMapVersion().asStringValue() : null, + this.getSource() != null ? this.getSource().asStringValue() : null, + this.getTarget() != null ? this.getTarget().asStringValue() : null, + this.getResourceId(), + this.getReverseAsBoolean() + ); + } + + public static TranslationRequest fromTranslateCodeRequest(IValidationSupport.TranslateCodeRequest theRequest) { + CodeableConcept sourceCodeableConcept = new CodeableConcept(); + for (IBaseCoding aCoding : theRequest.getCodings()) { + sourceCodeableConcept + .addCoding() + .setSystem(aCoding.getSystem()) + .setCode(aCoding.getCode()) + .setVersion(((Coding) aCoding).getVersion()); + } + + TranslationRequest translationRequest = new TranslationRequest(); + translationRequest.setCodeableConcept(sourceCodeableConcept); + translationRequest.setConceptMapVersion(new StringType(theRequest.getConceptMapVersion())); + translationRequest.setUrl(new UriType(theRequest.getConceptMapUrl())); + translationRequest.setSource(new UriType(theRequest.getSourceValueSetUrl())); + translationRequest.setTarget(new UriType(theRequest.getTargetValueSetUrl())); + translationRequest.setTargetSystem(new UriType(theRequest.getTargetSystemUrl())); + translationRequest.setResourceId(theRequest.getResourcePid()); + translationRequest.setReverse(theRequest.isReverse()); + return translationRequest; + + } } diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/param/TokenParamTest.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/param/TokenParamTest.java index 1a3165bad08..4f35e91cfd1 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/param/TokenParamTest.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/param/TokenParamTest.java @@ -2,12 +2,14 @@ package ca.uhn.fhir.rest.param; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.api.Constants; +import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; public class TokenParamTest { private static final FhirContext ourCtx = FhirContext.forR4Cached(); @@ -48,4 +50,32 @@ public class TokenParamTest { assertEquals("type-value|identifier-value", param.getValue()); } + @Test + public void testNameNickname() { + StringParam param = new StringParam(); + assertFalse(param.isNicknameExpand()); + param.setValueAsQueryToken(ourCtx, "name", Constants.PARAMQUALIFIER_NICKNAME, "kenny"); + assertTrue(param.isNicknameExpand()); + } + + @Test + public void testGivenNickname() { + StringParam param = new StringParam(); + assertFalse(param.isNicknameExpand()); + param.setValueAsQueryToken(ourCtx, "given", Constants.PARAMQUALIFIER_NICKNAME, "kenny"); + assertTrue(param.isNicknameExpand()); + } + + @Test + public void testInvalidNickname() { + StringParam param = new StringParam(); + assertFalse(param.isNicknameExpand()); + try { + param.setValueAsQueryToken(ourCtx, "family", Constants.PARAMQUALIFIER_NICKNAME, "kenny"); + fail(); + } catch (InvalidRequestException e) { + assertEquals("HAPI-2077: Modifier :nickname may only be used with 'name' and 'given' search parameters", e.getMessage()); + } + } + } diff --git a/hapi-fhir-validation/src/main/java/org/hl7/fhir/common/hapi/validation/support/RemoteTerminologyServiceValidationSupport.java b/hapi-fhir-validation/src/main/java/org/hl7/fhir/common/hapi/validation/support/RemoteTerminologyServiceValidationSupport.java index 5dc693ac9ae..56c58cd675e 100644 --- a/hapi-fhir-validation/src/main/java/org/hl7/fhir/common/hapi/validation/support/RemoteTerminologyServiceValidationSupport.java +++ b/hapi-fhir-validation/src/main/java/org/hl7/fhir/common/hapi/validation/support/RemoteTerminologyServiceValidationSupport.java @@ -1,5 +1,11 @@ package org.hl7.fhir.common.hapi.validation.support; +import ca.uhn.fhir.context.BaseRuntimeChildDefinition; +import ca.uhn.fhir.context.BaseRuntimeElementCompositeDefinition; +import ca.uhn.fhir.context.BaseRuntimeElementDefinition; +import ca.uhn.fhir.context.RuntimePrimitiveDatatypeDefinition; +import ca.uhn.fhir.context.support.TranslateConceptResult; +import ca.uhn.fhir.context.support.TranslateConceptResults; import ca.uhn.fhir.i18n.Msg; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.FhirVersionEnum; @@ -11,20 +17,29 @@ import ca.uhn.fhir.rest.client.api.IGenericClient; import ca.uhn.fhir.util.BundleUtil; import ca.uhn.fhir.util.JsonUtil; import ca.uhn.fhir.util.ParametersUtil; +import ca.uhn.fhir.util.StringUtil; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.Validate; +import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseBundle; +import org.hl7.fhir.instance.model.api.IBaseCoding; import org.hl7.fhir.instance.model.api.IBaseParameters; import org.hl7.fhir.instance.model.api.IBaseResource; +import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.hl7.fhir.r4.model.CodeSystem; +import org.hl7.fhir.r4.model.Coding; +import org.hl7.fhir.r4.model.Parameters; import org.hl7.fhir.r4.model.ValueSet; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.annotation.Nonnull; import java.io.IOException; +import java.sql.Array; import java.util.ArrayList; import java.util.List; +import java.util.Objects; +import java.util.Optional; import static org.apache.commons.lang3.StringUtils.isBlank; import static org.apache.commons.lang3.StringUtils.isNotBlank; @@ -331,6 +346,23 @@ public class RemoteTerminologyServiceValidationSupport extends BaseValidationSup return fetchValueSet(theValueSetUrl) != null; } + @Override + public TranslateConceptResults translateConcept(TranslateCodeRequest theRequest) { + IGenericClient client = provideClient(); + FhirContext fhirContext = client.getFhirContext(); + + IBaseParameters params = buildTranslateInputParameters(fhirContext, theRequest); + + IBaseParameters outcome = client + .operation() + .onType("ConceptMap") + .named("$translate") + .withParameters(params) + .execute(); + + return translateOutcomeToResults(fhirContext, outcome); + } + private IGenericClient provideClient() { IGenericClient retVal = myCtx.newRestfulGenericClient(myBaseUrl); for (Object next : myClientInterceptors) { @@ -442,4 +474,103 @@ public class RemoteTerminologyServiceValidationSupport extends BaseValidationSup myClientInterceptors.add(theClientInterceptor); } + private IBaseParameters buildTranslateInputParameters(FhirContext fhirContext, TranslateCodeRequest theRequest) { + IBaseParameters params = ParametersUtil.newInstance(fhirContext); + if (!StringUtils.isEmpty(theRequest.getConceptMapUrl())) { + ParametersUtil.addParameterToParametersUri(fhirContext, params, "url", theRequest.getConceptMapUrl()); + } + if (!StringUtils.isEmpty(theRequest.getConceptMapVersion())) { + ParametersUtil.addParameterToParametersString(fhirContext, params, "conceptMapVersion", theRequest.getConceptMapVersion()); + } + if (theRequest.getCodings() != null) { + addCodingsToTranslateParameters(fhirContext, theRequest.getCodings(), params); + } + if (!StringUtils.isEmpty(theRequest.getSourceValueSetUrl())) { + ParametersUtil.addParameterToParametersUri(fhirContext, params, "source", theRequest.getSourceValueSetUrl()); + } + if (!StringUtils.isEmpty(theRequest.getTargetValueSetUrl())) { + ParametersUtil.addParameterToParametersUri(fhirContext, params, "target", theRequest.getTargetValueSetUrl()); + } + if (!StringUtils.isEmpty(theRequest.getTargetSystemUrl())) { + ParametersUtil.addParameterToParametersUri(fhirContext, params, "targetsystem", theRequest.getTargetSystemUrl()); + } + if (theRequest.isReverse()) { + ParametersUtil.addParameterToParametersBoolean(fhirContext, params, "reverse", theRequest.isReverse()); + } + + return params; + } + + private void addCodingsToTranslateParameters(FhirContext fhirContext, List theCodings, IBaseParameters theParams) { + BaseRuntimeElementCompositeDefinition codeableConceptDef = (BaseRuntimeElementCompositeDefinition) Objects.requireNonNull(fhirContext.getElementDefinition("CodeableConcept")); + BaseRuntimeChildDefinition codings = codeableConceptDef.getChildByName("coding"); + BaseRuntimeElementCompositeDefinition codingDef = (BaseRuntimeElementCompositeDefinition) Objects.requireNonNull(fhirContext.getElementDefinition("Coding")); + BaseRuntimeChildDefinition codingSystemChild = codingDef.getChildByName("system"); + BaseRuntimeChildDefinition codingCodeChild = codingDef.getChildByName("code"); + BaseRuntimeElementDefinition> systemDef = (RuntimePrimitiveDatatypeDefinition) fhirContext.getElementDefinition("uri"); + BaseRuntimeElementDefinition> codeDef = (RuntimePrimitiveDatatypeDefinition) fhirContext.getElementDefinition("code"); + + IBase codeableConcept = codeableConceptDef.newInstance(); + + for (IBaseCoding aCoding : theCodings) { + IBaseCoding newCoding = (IBaseCoding) codingDef.newInstance(); + + IPrimitiveType newSystem = systemDef.newInstance(aCoding.getSystem()); + codingSystemChild.getMutator().addValue(newCoding, newSystem); + IPrimitiveType newCode = codeDef.newInstance(aCoding.getCode()); + codingCodeChild.getMutator().addValue(newCoding, newCode); + + codings.getMutator().addValue(codeableConcept, newCoding); + } + + ParametersUtil.addParameterToParameters(fhirContext, theParams, "codeableConcept", codeableConcept); + } + + private TranslateConceptResults translateOutcomeToResults(FhirContext fhirContext, IBaseParameters outcome) { + Optional result = ParametersUtil.getNamedParameterValueAsString(fhirContext, outcome, "result"); + Optional message = ParametersUtil.getNamedParameterValueAsString(fhirContext, outcome, "message"); + List matches = ParametersUtil.getNamedParameters(fhirContext, outcome, "match"); + + TranslateConceptResults retVal = new TranslateConceptResults(); + if (result.isPresent()) { + retVal.setResult(Boolean.parseBoolean(result.get())); + } + if (message.isPresent()) { + retVal.setMessage(message.get()); + } + if (!matches.isEmpty()) { + retVal.setResults(matchesToTranslateConceptResults(fhirContext, matches)); + } + + return retVal; + } + + private List matchesToTranslateConceptResults(FhirContext fhirContext, List theMatches) { + List resultList = new ArrayList(); + for (IBase m : theMatches) { + TranslateConceptResult match = new TranslateConceptResult(); + String equivalence = ParametersUtil.getParameterPartValueAsString(fhirContext, m, "equivalence"); + Optional concept = ParametersUtil.getParameterPartValue(fhirContext, m, "concept"); + String source = ParametersUtil.getParameterPartValueAsString(fhirContext, m, "source"); + + if (StringUtils.isNotBlank(equivalence)) { + match.setEquivalence(equivalence); + } + + if (concept.isPresent()) { + IBaseCoding matchedCoding = (IBaseCoding) concept.get(); + match.setSystem(matchedCoding.getSystem()); + match.setCode(matchedCoding.getCode()); + match.setDisplay(matchedCoding.getDisplay()); + + if (StringUtils.isNotBlank(source)) { + match.setConceptMapUrl(source); + } + + resultList.add(match); + } + } + return resultList; + } + } diff --git a/hapi-fhir-validation/src/test/java/org/hl7/fhir/common/hapi/validation/support/RemoteTerminologyServiceValidationSupportTest.java b/hapi-fhir-validation/src/test/java/org/hl7/fhir/common/hapi/validation/support/RemoteTerminologyServiceValidationSupportTest.java index 3390ba15789..a40059965d6 100644 --- a/hapi-fhir-validation/src/test/java/org/hl7/fhir/common/hapi/validation/support/RemoteTerminologyServiceValidationSupportTest.java +++ b/hapi-fhir-validation/src/test/java/org/hl7/fhir/common/hapi/validation/support/RemoteTerminologyServiceValidationSupportTest.java @@ -3,6 +3,8 @@ package org.hl7.fhir.common.hapi.validation.support; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.support.ConceptValidationOptions; import ca.uhn.fhir.context.support.IValidationSupport; +import ca.uhn.fhir.context.support.TranslateConceptResult; +import ca.uhn.fhir.context.support.TranslateConceptResults; import ca.uhn.fhir.jpa.model.util.JpaConstants; import ca.uhn.fhir.parser.IJsonLikeParser; import ca.uhn.fhir.rest.annotation.IdParam; @@ -20,11 +22,14 @@ import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.test.utilities.server.RestfulServerExtension; import ca.uhn.fhir.util.ParametersUtil; import com.google.common.collect.Lists; +import org.hl7.fhir.instance.model.api.IBaseCoding; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.r4.model.BooleanType; import org.hl7.fhir.r4.model.CodeSystem; import org.hl7.fhir.r4.model.CodeType; +import org.hl7.fhir.r4.model.CodeableConcept; import org.hl7.fhir.r4.model.Coding; +import org.hl7.fhir.r4.model.ConceptMap; import org.hl7.fhir.r4.model.IdType; import org.hl7.fhir.r4.model.Parameters; import org.hl7.fhir.r4.model.StringType; @@ -58,7 +63,18 @@ public class RemoteTerminologyServiceValidationSupportTest { private static final String CODE_SYSTEM_VERSION_AS_TEXT = "v2.1.12"; private static final String CODE = "CODE"; private static final String VALUE_SET_URL = "http://value.set/url"; + private static final String TARGET_SYSTEM = "http://target.system/url"; + private static final String CONCEPT_MAP_URL = "http://concept.map/url"; + private static final String CONCEPT_MAP_VERSION = "2.1"; + private static final String SOURCE_VALUE_SET_URL = "http://source.vs.system/url"; + private static final String TARGET_VALUE_SET_URL = "http://target.vs.system/url"; + private static final String TARGET_CODE = "CODE"; + private static final String TARGET_CODE_DISPLAY = "code"; + private static final boolean REVERSE = true; + private static final String EQUIVALENCE_CODE = "equivalent"; + private static final String ERROR_MESSAGE = "This is an error message"; + private static final String SUCCESS_MESSAGE = "This is a success message"; private static FhirContext ourCtx = FhirContext.forR4Cached(); @@ -68,6 +84,7 @@ public class RemoteTerminologyServiceValidationSupportTest { private MyValueSetProvider myValueSetProvider; private RemoteTerminologyServiceValidationSupport mySvc; private MyCodeSystemProvider myCodeSystemProvider; + private MyConceptMapProvider myConceptMapProvider; @BeforeEach public void before() { @@ -77,6 +94,9 @@ public class RemoteTerminologyServiceValidationSupportTest { myCodeSystemProvider = new MyCodeSystemProvider(); myRestfulServerExtension.getRestfulServer().registerProvider(myCodeSystemProvider); + myConceptMapProvider = new MyConceptMapProvider(); + myRestfulServerExtension.getRestfulServer().registerProvider(myConceptMapProvider); + String baseUrl = "http://localhost:" + myRestfulServerExtension.getPort(); mySvc = new RemoteTerminologyServiceValidationSupport(ourCtx); @@ -277,6 +297,91 @@ public class RemoteTerminologyServiceValidationSupportTest { assertEquals(null, outcome); } + @Test + public void testTranslateCode_AllInParams_AllOutParams() { + myConceptMapProvider.myNextReturnParams = new Parameters(); + myConceptMapProvider.myNextReturnParams.addParameter("result", true); + myConceptMapProvider.myNextReturnParams.addParameter("message", ERROR_MESSAGE); + + TranslateConceptResults expectedResults = new TranslateConceptResults(); + expectedResults.setResult(true); + + // Add 2 matches + addMatchToTranslateRequest(myConceptMapProvider.myNextReturnParams); + addMatchToTranslateRequest(myConceptMapProvider.myNextReturnParams); + + List translateResults = new ArrayList<>(); + TranslateConceptResult singleResult = new TranslateConceptResult(); + singleResult + .setEquivalence(EQUIVALENCE_CODE) + .setSystem(TARGET_SYSTEM) + .setCode(TARGET_CODE) + .setConceptMapUrl(CONCEPT_MAP_URL) + .setDisplay(TARGET_CODE_DISPLAY); + translateResults.add(singleResult); + translateResults.add(singleResult); + expectedResults.setResults(translateResults); + + CodeableConcept codeableConcept = new CodeableConcept(); + codeableConcept.addCoding(new Coding(CODE_SYSTEM, CODE, null)); + + IValidationSupport.TranslateCodeRequest request = new IValidationSupport.TranslateCodeRequest( + Collections.unmodifiableList(codeableConcept.getCoding()), + TARGET_SYSTEM, + CONCEPT_MAP_URL, + CONCEPT_MAP_VERSION, + SOURCE_VALUE_SET_URL, + TARGET_VALUE_SET_URL, + null, + REVERSE); + + TranslateConceptResults results = mySvc.translateConcept(request); + + assertEquals(results.getResult(), true); + assertEquals(results.getResults().size(), 2); + for(TranslateConceptResult result : results.getResults()) { + assertEquals(singleResult, result); + } + + assertTrue(codeableConcept.equalsDeep(myConceptMapProvider.myLastCodeableConcept)); + assertEquals(TARGET_SYSTEM, myConceptMapProvider.myLastTargetCodeSystem.getValue()); + assertEquals(CONCEPT_MAP_URL, myConceptMapProvider.myLastConceptMapUrl.getValue()); + assertEquals(CONCEPT_MAP_VERSION, myConceptMapProvider.myLastConceptMapVersion.getValue()); + assertEquals(SOURCE_VALUE_SET_URL, myConceptMapProvider.myLastSourceValueSet.getValue()); + assertEquals(TARGET_VALUE_SET_URL, myConceptMapProvider.myLastTargetValueSet.getValue()); + assertEquals(REVERSE, myConceptMapProvider.myLastReverse.getValue()); + } + + @Test + public void testTranslateCode_NoInParams_NoOutParams() { + myConceptMapProvider.myNextReturnParams = new Parameters(); + + List codings = new ArrayList<>(); + codings.add(new Coding(null, null, null)); + IValidationSupport.TranslateCodeRequest request = new IValidationSupport.TranslateCodeRequest(codings, null); + + TranslateConceptResults results = mySvc.translateConcept(request); + + assertEquals(results.getResult(), false); + assertEquals(results.getResults().size(), 0); + + assertNull(myConceptMapProvider.myLastCodeableConcept); + assertNull(myConceptMapProvider.myLastTargetCodeSystem); + assertNull(myConceptMapProvider.myLastConceptMapUrl); + assertNull(myConceptMapProvider.myLastConceptMapVersion); + assertNull(myConceptMapProvider.myLastSourceValueSet); + assertNull(myConceptMapProvider.myLastTargetValueSet); + assertNull(myConceptMapProvider.myLastReverse); + } + + private void addMatchToTranslateRequest(Parameters params) { + Parameters.ParametersParameterComponent matchParam = params.addParameter().setName("match"); + matchParam.addPart().setName("equivalence").setValue(new CodeType(EQUIVALENCE_CODE)); + Coding value = new Coding(TARGET_SYSTEM, TARGET_CODE, TARGET_CODE_DISPLAY); + matchParam.addPart().setName("concept").setValue(value); + matchParam.addPart().setName("source").setValue(new UriType(CONCEPT_MAP_URL)); + } + /** * Remote terminology services can be used to validate codes when code system is present, * even when inferSystem is true @@ -636,5 +741,50 @@ public class RemoteTerminologyServiceValidationSupportTest { } + private static class MyConceptMapProvider implements IResourceProvider { + private UriType myLastConceptMapUrl; + private StringType myLastConceptMapVersion; + private CodeableConcept myLastCodeableConcept; + private UriType myLastSourceValueSet; + private UriType myLastTargetValueSet; + private UriType myLastTargetCodeSystem; + private BooleanType myLastReverse; + + private int myInvocationCount; + private Parameters myNextReturnParams; + + @Operation(name = JpaConstants.OPERATION_TRANSLATE, idempotent = true, returnParameters = { + @OperationParam(name = "result", type = BooleanType.class, min = 1, max = 1), + @OperationParam(name = "message", type = StringType.class, min = 0, max = 1), + }) + public Parameters translate( + HttpServletRequest theServletRequest, + @IdParam(optional = true) IdType theId, + @OperationParam(name = "url", min = 0, max = 1) UriType theConceptMapUrl, + @OperationParam(name = "conceptMapVersion", min = 0, max = 1) StringType theConceptMapVersion, + @OperationParam(name = "codeableConcept", min = 0, max = 1) CodeableConcept theSourceCodeableConcept, + @OperationParam(name = "source", min = 0, max = 1) UriType theSourceValueSet, + @OperationParam(name = "target", min = 0, max = 1) UriType theTargetValueSet, + @OperationParam(name = "targetsystem", min = 0, max = 1) UriType theTargetCodeSystem, + @OperationParam(name = "reverse", min = 0, max = 1) BooleanType theReverse, + RequestDetails theRequestDetails + ) { + myInvocationCount++; + myLastConceptMapUrl = theConceptMapUrl; + myLastConceptMapVersion = theConceptMapVersion; + myLastCodeableConcept = theSourceCodeableConcept; + myLastSourceValueSet = theSourceValueSet; + myLastTargetValueSet = theTargetValueSet; + myLastTargetCodeSystem = theTargetCodeSystem; + myLastReverse = theReverse; + return myNextReturnParams; + } + + @Override + public Class getResourceType() { + return ConceptMap.class; + } + + } }