diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/config/BaseConfig.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/config/BaseConfig.java index d8e50d3e009..5cf4bd44de3 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/config/BaseConfig.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/config/BaseConfig.java @@ -9,9 +9,9 @@ package ca.uhn.fhir.jpa.config; * 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. @@ -22,6 +22,7 @@ package ca.uhn.fhir.jpa.config; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.i18n.HapiLocalizer; +import ca.uhn.fhir.jpa.dao.DaoRegistry; import ca.uhn.fhir.jpa.provider.SubscriptionRetriggeringProvider; import ca.uhn.fhir.jpa.search.*; import ca.uhn.fhir.jpa.sp.ISearchParamPresenceSvc; @@ -63,6 +64,11 @@ public abstract class BaseConfig implements SchedulingConfigurer { @Autowired protected Environment myEnv; + @Bean + public DaoRegistry daoRegistry() { + return new DaoRegistry(); + } + @Override public void configureTasks(@Nonnull ScheduledTaskRegistrar theTaskRegistrar) { theTaskRegistrar.setTaskScheduler(taskScheduler()); diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/DaoRegistry.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/DaoRegistry.java new file mode 100644 index 00000000000..abee9e15ded --- /dev/null +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/DaoRegistry.java @@ -0,0 +1,43 @@ +package ca.uhn.fhir.jpa.dao; + +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.context.RuntimeResourceDefinition; +import org.apache.commons.lang3.Validate; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; + +import javax.annotation.PostConstruct; +import java.util.HashMap; +import java.util.Map; + +public class DaoRegistry implements ApplicationContextAware { + private ApplicationContext myAppCtx; + + @Autowired + private FhirContext myCtx; + private Map> myResourceNameToResourceDao = new HashMap<>(); + + @Override + public void setApplicationContext(ApplicationContext theApplicationContext) throws BeansException { + myAppCtx = theApplicationContext; + } + + @PostConstruct + public void start() { + Map resourceDaos = myAppCtx.getBeansOfType(IFhirResourceDao.class); + for (IFhirResourceDao nextResourceDao : resourceDaos.values()) { + RuntimeResourceDefinition nextResourceDef = myCtx.getResourceDefinition(nextResourceDao.getResourceType()); + myResourceNameToResourceDao.put(nextResourceDef.getName(), nextResourceDao); + } + } + + public IFhirResourceDao getResourceDao(String theResourceName) { + IFhirResourceDao retVal = myResourceNameToResourceDao.get(theResourceName); + Validate.notNull(retVal, "No DAO exists for resource type %s", theResourceName); + return retVal; + + } + +} 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 c85b65c1a11..59e362241e9 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 @@ -89,7 +89,7 @@ public class SearchCoordinatorSvcImpl implements ISearchCoordinatorSvc { @Autowired private PlatformTransactionManager myManagedTxManager; @Autowired - private IFhirSystemDao mySystemDao; + private DaoRegistry myDaoRegistry; @Autowired private IPagingProvider myPagingProvider; @@ -170,9 +170,8 @@ public class SearchCoordinatorSvcImpl implements ISearchCoordinatorSvc { if (newSearch.isPresent()) { search = newSearch.get(); String resourceType = search.getResourceType(); - Class type = myContext.getResourceDefinition(resourceType).getImplementingClass(); SearchParameterMap params = search.getSearchParameterMap(); - SearchContinuationTask task = new SearchContinuationTask(search, mySystemDao.getDao(type), params, resourceType); + SearchContinuationTask task = new SearchContinuationTask(search, myDaoRegistry.getResourceDao(resourceType), params, resourceType); myIdToSearchTask.put(search.getUuid(), task); myExecutor.submit(task); }