From 57a29b59553f131fb6f65150a895fc720820bef3 Mon Sep 17 00:00:00 2001 From: "justin.mckelvy" Date: Wed, 10 May 2023 07:29:30 -0600 Subject: [PATCH] update search parameters for eval resources to systemRequestDetails for partitioned environment --- .../ca/uhn/fhir/cr/common/HapiFhirDal.java | 33 +++++++++++++++---- .../cr/common/HapiFhirRetrieveProvider.java | 31 ++++++++++------- .../cr/common/HapiLibrarySourceProvider.java | 9 ++++- .../config/BaseClinicalReasoningConfig.java | 13 ++++---- 4 files changed, 61 insertions(+), 25 deletions(-) diff --git a/hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/common/HapiFhirDal.java b/hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/common/HapiFhirDal.java index 2c958efc38f..15a6fdba181 100644 --- a/hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/common/HapiFhirDal.java +++ b/hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/common/HapiFhirDal.java @@ -19,30 +19,38 @@ */ package ca.uhn.fhir.cr.common; +import ca.uhn.fhir.interceptor.model.RequestPartitionId; import ca.uhn.fhir.jpa.api.dao.DaoRegistry; +import ca.uhn.fhir.jpa.partition.BaseRequestPartitionHelperSvc; import ca.uhn.fhir.jpa.searchparam.SearchParameterMap; import ca.uhn.fhir.rest.api.server.RequestDetails; +import ca.uhn.fhir.rest.api.server.SystemRequestDetails; import ca.uhn.fhir.rest.param.UriParam; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.instance.model.api.IIdType; import org.opencds.cqf.cql.evaluator.fhir.dal.FhirDal; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; @SuppressWarnings("unchecked") /** * This class leverages DaoRegistry from Hapi-fhir to implement CRUD FHIR API operations constrained to provide only the operations necessary for the cql-evaluator modules to function. **/ public class HapiFhirDal implements FhirDal { - + private static Logger logger = LoggerFactory.getLogger(HapiFhirDal.class); protected final DaoRegistry myDaoRegistry; protected final RequestDetails myRequestDetails; + protected final BaseRequestPartitionHelperSvc myBaseRequestPartitionHelperSvc; + + public HapiFhirDal(DaoRegistry theDaoRegistry, BaseRequestPartitionHelperSvc theBaseRequestPartitionHelperSvc) { + this(theDaoRegistry,null, theBaseRequestPartitionHelperSvc); - public HapiFhirDal(DaoRegistry theDaoRegistry) { - this(theDaoRegistry,null); } - public HapiFhirDal(DaoRegistry theDaoRegistry, RequestDetails theRequestDetails) { + public HapiFhirDal(DaoRegistry theDaoRegistry, RequestDetails theRequestDetails, BaseRequestPartitionHelperSvc theBaseRequestPartitionHelperSvc) { this.myDaoRegistry = theDaoRegistry; this.myRequestDetails = theRequestDetails; + this.myBaseRequestPartitionHelperSvc = theBaseRequestPartitionHelperSvc; } @Override @@ -76,9 +84,20 @@ public class HapiFhirDal implements FhirDal { @Override public Iterable searchByUrl(String theResourceType, String theUrl) { - var b = this.myDaoRegistry.getResourceDao(theResourceType) - .search(new SearchParameterMap().add("url", new UriParam(theUrl)), myRequestDetails); - return new BundleIterable(myRequestDetails, b); + + if(myBaseRequestPartitionHelperSvc.isResourcePartitionable(theResourceType)){ + var b = this.myDaoRegistry.getResourceDao(theResourceType) + .search(new SearchParameterMap().add("url", new UriParam(theUrl)), myRequestDetails); + return new BundleIterable(myRequestDetails, b); + } else { + //In Partitioned deployment certain resources are only available in default partition + SystemRequestDetails systemRequestDetails = new SystemRequestDetails(); + systemRequestDetails.setRequestPartitionId(RequestPartitionId.defaultPartition()); + var b = this.myDaoRegistry.getResourceDao(theResourceType) + .search(new SearchParameterMap().add("url", new UriParam(theUrl)), systemRequestDetails); + return new BundleIterable(systemRequestDetails, b); + + } } diff --git a/hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/common/HapiFhirRetrieveProvider.java b/hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/common/HapiFhirRetrieveProvider.java index d3200d6df73..f710eafa748 100644 --- a/hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/common/HapiFhirRetrieveProvider.java +++ b/hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/common/HapiFhirRetrieveProvider.java @@ -19,10 +19,13 @@ */ package ca.uhn.fhir.cr.common; +import ca.uhn.fhir.interceptor.model.RequestPartitionId; import ca.uhn.fhir.jpa.api.dao.DaoRegistry; +import ca.uhn.fhir.jpa.partition.BaseRequestPartitionHelperSvc; import ca.uhn.fhir.model.api.IQueryParameterType; import ca.uhn.fhir.rest.api.server.RequestDetails; import ca.uhn.fhir.rest.api.server.SystemRequestDetails; +import ca.uhn.fhir.rest.param.UriParam; import org.hl7.fhir.instance.model.api.IBaseResource; import org.opencds.cqf.cql.engine.fhir.retrieve.SearchParamFhirRetrieveProvider; import org.opencds.cqf.cql.engine.fhir.searchparam.SearchParameterMap; @@ -47,17 +50,18 @@ public class HapiFhirRetrieveProvider extends SearchParamFhirRetrieveProvider im private final DaoRegistry myDaoRegistry; private final RequestDetails myRequestDetails; + protected final BaseRequestPartitionHelperSvc myBaseRequestPartitionHelperSvc; - - public HapiFhirRetrieveProvider(DaoRegistry theDaoRegistry, SearchParameterResolver theSearchParameterResolver) { - this(theDaoRegistry, theSearchParameterResolver, new SystemRequestDetails()); + public HapiFhirRetrieveProvider(DaoRegistry theDaoRegistry, SearchParameterResolver theSearchParameterResolver, BaseRequestPartitionHelperSvc theBaseRequestPartitionHelperSvc) { + this(theDaoRegistry, theSearchParameterResolver, new SystemRequestDetails(), theBaseRequestPartitionHelperSvc); } - public HapiFhirRetrieveProvider(DaoRegistry registry, SearchParameterResolver searchParameterResolver, - RequestDetails requestDetails) { + public HapiFhirRetrieveProvider(DaoRegistry theRegistry, SearchParameterResolver searchParameterResolver, + RequestDetails theRequestDetails, BaseRequestPartitionHelperSvc theBaseRequestPartitionHelperSvc) { super(searchParameterResolver); - this.myDaoRegistry = registry; - this.myRequestDetails = requestDetails; + this.myDaoRegistry = theRegistry; + this.myRequestDetails = theRequestDetails; + this.myBaseRequestPartitionHelperSvc = theBaseRequestPartitionHelperSvc; } /** @@ -67,7 +71,6 @@ public class HapiFhirRetrieveProvider extends SearchParamFhirRetrieveProvider im private final String dataType; private final List queries; - private final BiFunction> queryFunc; public QueryIterable(String dataType, List queries, BiFunction> queryFunc) { @@ -138,11 +141,11 @@ public class HapiFhirRetrieveProvider extends SearchParamFhirRetrieveProvider im return new QueryIterable(dataType, queries, this::executeQuery); } - protected Iterable executeQuery(String dataType, SearchParameterMap map) { + protected Iterable executeQuery(String theDataType, SearchParameterMap theSearchParameterMap) { ca.uhn.fhir.jpa.searchparam.SearchParameterMap hapiMap = new ca.uhn.fhir.jpa.searchparam.SearchParameterMap(); try { - for (Map.Entry>> entry : map.entrySet()) { + for (Map.Entry>> entry : theSearchParameterMap.entrySet()) { hapiMap.put(entry.getKey(), entry.getValue()); } @@ -151,7 +154,13 @@ public class HapiFhirRetrieveProvider extends SearchParamFhirRetrieveProvider im logger.warn("Error converting search parameter map", e); } - return search(getClass(dataType), hapiMap, myRequestDetails); + SystemRequestDetails systemRequestDetails = new SystemRequestDetails(); + + if(!myBaseRequestPartitionHelperSvc.isResourcePartitionable(theDataType)){ + //if non-partitionable datatype, set to default partition + systemRequestDetails.setRequestPartitionId(RequestPartitionId.defaultPartition()); + } + return search(getClass(theDataType), hapiMap, systemRequestDetails); } @Override diff --git a/hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/common/HapiLibrarySourceProvider.java b/hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/common/HapiLibrarySourceProvider.java index 02ee27194b9..d65ad3dab33 100644 --- a/hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/common/HapiLibrarySourceProvider.java +++ b/hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/common/HapiLibrarySourceProvider.java @@ -19,8 +19,10 @@ */ package ca.uhn.fhir.cr.common; +import ca.uhn.fhir.interceptor.model.RequestPartitionId; import ca.uhn.fhir.jpa.api.dao.DaoRegistry; import ca.uhn.fhir.rest.api.server.RequestDetails; +import ca.uhn.fhir.rest.api.server.SystemRequestDetails; import org.cqframework.cql.cql2elm.LibraryContentType; import org.cqframework.cql.cql2elm.LibrarySourceProvider; import org.hl7.elm.r1.VersionedIdentifier; @@ -62,7 +64,12 @@ public class HapiLibrarySourceProvider LibraryContentType theLibraryContentType) { String name = theLibraryIdentifier.getId(); String version = theLibraryIdentifier.getVersion(); - var libraries = search(getClass("Library"), Searches.byName(name), myRequestDetails); + + // needed for partitioned environment + SystemRequestDetails systemRequestDetails = new SystemRequestDetails(); + systemRequestDetails.setRequestPartitionId(RequestPartitionId.defaultPartition()); + + var libraries = search(getClass("Library"), Searches.byName(name), systemRequestDetails); var libraryList = new ArrayList(); for(var l:libraries){ libraryList.add(l); diff --git a/hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/config/BaseClinicalReasoningConfig.java b/hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/config/BaseClinicalReasoningConfig.java index ffc3a8725ed..6306fe1bdf3 100644 --- a/hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/config/BaseClinicalReasoningConfig.java +++ b/hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/config/BaseClinicalReasoningConfig.java @@ -38,6 +38,7 @@ import ca.uhn.fhir.cr.common.ITerminologyProviderFactory; import ca.uhn.fhir.i18n.Msg; import ca.uhn.fhir.jpa.api.dao.DaoRegistry; import ca.uhn.fhir.jpa.cache.IResourceChangeListenerRegistry; +import ca.uhn.fhir.jpa.partition.BaseRequestPartitionHelperSvc; import ca.uhn.fhir.jpa.searchparam.SearchParameterMap; import ca.uhn.fhir.rest.server.provider.ResourceProviderFactory; import org.cqframework.cql.cql2elm.CqlTranslatorOptions; @@ -166,15 +167,15 @@ public abstract class BaseClinicalReasoningConfig { } @Bean - IFhirDalFactory fhirDalFactory(DaoRegistry theDaoRegistry) { - return rd -> new HapiFhirDal(theDaoRegistry, rd); + IFhirDalFactory fhirDalFactory(DaoRegistry theDaoRegistry, BaseRequestPartitionHelperSvc theBaseRequestPartitionHelperSvc) { + return rd -> new HapiFhirDal(theDaoRegistry, rd, theBaseRequestPartitionHelperSvc); } @Bean IDataProviderFactory dataProviderFactory(ModelResolver theModelResolver, DaoRegistry theDaoRegistry, - SearchParameterResolver theSearchParameterResolver) { + SearchParameterResolver theSearchParameterResolver, BaseRequestPartitionHelperSvc theBaseRequestPartitionHelperSvc) { return (rd, t) -> { - HapiFhirRetrieveProvider provider = new HapiFhirRetrieveProvider(theDaoRegistry, theSearchParameterResolver, rd); + HapiFhirRetrieveProvider provider = new HapiFhirRetrieveProvider(theDaoRegistry, theSearchParameterResolver, rd, theBaseRequestPartitionHelperSvc); if (t != null) { provider.setTerminologyProvider(t); provider.setExpandValueSets(true); @@ -205,8 +206,8 @@ public abstract class BaseClinicalReasoningConfig { @Bean public HapiFhirRetrieveProvider fhirRetrieveProvider(DaoRegistry theDaoRegistry, - SearchParameterResolver theSearchParameterResolver) { - return new HapiFhirRetrieveProvider(theDaoRegistry, theSearchParameterResolver); + SearchParameterResolver theSearchParameterResolver, BaseRequestPartitionHelperSvc theBaseRequestPartitionHelperSvc) { + return new HapiFhirRetrieveProvider(theDaoRegistry, theSearchParameterResolver, theBaseRequestPartitionHelperSvc); } @Bean