001package ca.uhn.fhir.jpa.config;
002
003import ca.uhn.fhir.context.FhirContext;
004import ca.uhn.fhir.context.support.DefaultProfileValidationSupport;
005import ca.uhn.fhir.context.support.IValidationSupport;
006import ca.uhn.fhir.jpa.api.dao.IFhirSystemDao;
007import ca.uhn.fhir.jpa.dao.FulltextSearchSvcImpl;
008import ca.uhn.fhir.jpa.dao.IFulltextSearchSvc;
009import ca.uhn.fhir.jpa.dao.ITransactionProcessorVersionAdapter;
010import ca.uhn.fhir.jpa.dao.JpaPersistedResourceValidationSupport;
011import ca.uhn.fhir.jpa.dao.TransactionProcessorVersionAdapterDstu2;
012import ca.uhn.fhir.jpa.term.TermReadSvcDstu2;
013import ca.uhn.fhir.jpa.term.api.ITermReadSvc;
014import ca.uhn.fhir.jpa.util.ResourceCountCache;
015import ca.uhn.fhir.model.dstu2.composite.MetaDt;
016import ca.uhn.fhir.model.dstu2.resource.Bundle;
017import ca.uhn.fhir.validation.IInstanceValidatorModule;
018import org.apache.commons.lang3.time.DateUtils;
019import org.hl7.fhir.common.hapi.validation.support.CachingValidationSupport;
020import org.hl7.fhir.common.hapi.validation.support.CommonCodeSystemsTerminologyService;
021import org.hl7.fhir.common.hapi.validation.support.InMemoryTerminologyServerValidationSupport;
022import org.hl7.fhir.common.hapi.validation.support.ValidationSupportChain;
023import org.hl7.fhir.common.hapi.validation.validator.FhirInstanceValidator;
024import org.hl7.fhir.common.hapi.validation.validator.HapiToHl7OrgDstu2ValidatingSupportWrapper;
025import org.hl7.fhir.r5.utils.IResourceValidator;
026import org.springframework.beans.factory.annotation.Autowire;
027import org.springframework.context.annotation.Bean;
028import org.springframework.context.annotation.Configuration;
029import org.springframework.context.annotation.Lazy;
030import org.springframework.context.annotation.Primary;
031import org.springframework.transaction.annotation.EnableTransactionManagement;
032
033/*
034 * #%L
035 * HAPI FHIR JPA Server
036 * %%
037 * Copyright (C) 2014 - 2021 Smile CDR, Inc.
038 * %%
039 * Licensed under the Apache License, Version 2.0 (the "License");
040 * you may not use this file except in compliance with the License.
041 * You may obtain a copy of the License at
042 *
043 * http://www.apache.org/licenses/LICENSE-2.0
044 *
045 * Unless required by applicable law or agreed to in writing, software
046 * distributed under the License is distributed on an "AS IS" BASIS,
047 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
048 * See the License for the specific language governing permissions and
049 * limitations under the License.
050 * #L%
051 */
052
053@Configuration
054@EnableTransactionManagement
055public class BaseDstu2Config extends BaseConfig {
056
057        private static FhirContext ourFhirContextDstu2;
058        private static FhirContext ourFhirContextDstu2Hl7Org;
059
060        @Bean
061        @Primary
062        public FhirContext defaultFhirContext() {
063                return fhirContextDstu2();
064        }
065
066        @Override
067        public FhirContext fhirContext() {
068                return fhirContextDstu2();
069        }
070
071        @Bean(name = "myFhirContextDstu2")
072        @Lazy
073        public FhirContext fhirContextDstu2() {
074                if (ourFhirContextDstu2 == null) {
075                        ourFhirContextDstu2 = FhirContext.forDstu2();
076                }
077                return ourFhirContextDstu2;
078        }
079
080        @Bean(name = "myFhirContextDstu2Hl7Org")
081        @Lazy
082        public FhirContext fhirContextDstu2Hl7Org() {
083                if (ourFhirContextDstu2Hl7Org == null) {
084                        ourFhirContextDstu2Hl7Org = FhirContext.forDstu2Hl7Org();
085                }
086                return ourFhirContextDstu2Hl7Org;
087        }
088
089        @Bean(name = "myInstanceValidator")
090        @Lazy
091        public IInstanceValidatorModule instanceValidator(ValidationSupportChain theValidationSupportChain) {
092                CachingValidationSupport cachingValidationSupport = new CachingValidationSupport(new HapiToHl7OrgDstu2ValidatingSupportWrapper(theValidationSupportChain));
093                FhirInstanceValidator retVal = new FhirInstanceValidator(cachingValidationSupport);
094                retVal.setBestPracticeWarningLevel(IResourceValidator.BestPracticeWarningLevel.Warning);
095                return retVal;
096        }
097
098        @Bean
099        public ITransactionProcessorVersionAdapter transactionProcessorVersionFacade() {
100                return new TransactionProcessorVersionAdapterDstu2();
101        }
102
103
104        @Bean(name = "myDefaultProfileValidationSupport")
105        public DefaultProfileValidationSupport defaultProfileValidationSupport() {
106                return new DefaultProfileValidationSupport(fhirContext());
107        }
108
109        @Bean(name = JPA_VALIDATION_SUPPORT_CHAIN)
110        public ValidationSupportChain validationSupportChain(DefaultProfileValidationSupport theDefaultProfileValidationSupport) {
111                InMemoryTerminologyServerValidationSupport inMemoryTerminologyServer = new InMemoryTerminologyServerValidationSupport(fhirContextDstu2());
112                IValidationSupport jpaValidationSupport = jpaValidationSupportDstu2();
113                CommonCodeSystemsTerminologyService commonCodeSystemsTermSvc = new CommonCodeSystemsTerminologyService(fhirContext());
114                return new ValidationSupportChain(theDefaultProfileValidationSupport, jpaValidationSupport, inMemoryTerminologyServer, commonCodeSystemsTermSvc);
115        }
116
117        @Primary
118        @Bean(name = JPA_VALIDATION_SUPPORT)
119        public IValidationSupport jpaValidationSupportDstu2() {
120                return new JpaPersistedResourceValidationSupport(fhirContextDstu2());
121        }
122
123        @Bean(name = "myResourceCountsCache")
124        public ResourceCountCache resourceCountsCache() {
125                ResourceCountCache retVal = new ResourceCountCache(() -> systemDaoDstu2().getResourceCounts());
126                retVal.setCacheMillis(4 * DateUtils.MILLIS_PER_HOUR);
127                return retVal;
128        }
129
130        @Bean(autowire = Autowire.BY_TYPE)
131        public IFulltextSearchSvc searchDao() {
132                FulltextSearchSvcImpl searchDao = new FulltextSearchSvcImpl();
133                return searchDao;
134        }
135
136        @Bean(name = "mySystemDaoDstu2", autowire = Autowire.BY_NAME)
137        public IFhirSystemDao<Bundle, MetaDt> systemDaoDstu2() {
138                ca.uhn.fhir.jpa.dao.FhirSystemDaoDstu2 retVal = new ca.uhn.fhir.jpa.dao.FhirSystemDaoDstu2();
139                return retVal;
140        }
141
142        @Bean(name = "mySystemProviderDstu2")
143        public ca.uhn.fhir.jpa.provider.JpaSystemProviderDstu2 systemProviderDstu2() {
144                ca.uhn.fhir.jpa.provider.JpaSystemProviderDstu2 retVal = new ca.uhn.fhir.jpa.provider.JpaSystemProviderDstu2();
145                retVal.setDao(systemDaoDstu2());
146                retVal.setContext(fhirContextDstu2());
147                return retVal;
148        }
149
150        @Bean(autowire = Autowire.BY_TYPE)
151        public ITermReadSvc terminologyService() {
152                return new TermReadSvcDstu2();
153        }
154
155}