001/*-
002 * #%L
003 * HAPI FHIR JPA - Search Parameters
004 * %%
005 * Copyright (C) 2014 - 2024 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.jpa.searchparam.config;
021
022import ca.uhn.fhir.context.FhirContext;
023import ca.uhn.fhir.i18n.Msg;
024import ca.uhn.fhir.jpa.cache.IResourceChangeListener;
025import ca.uhn.fhir.jpa.cache.IResourceChangeListenerCacheRefresher;
026import ca.uhn.fhir.jpa.cache.IResourceChangeListenerRegistry;
027import ca.uhn.fhir.jpa.cache.ResourceChangeListenerCache;
028import ca.uhn.fhir.jpa.cache.ResourceChangeListenerCacheFactory;
029import ca.uhn.fhir.jpa.cache.ResourceChangeListenerCacheRefresherImpl;
030import ca.uhn.fhir.jpa.cache.ResourceChangeListenerRegistryImpl;
031import ca.uhn.fhir.jpa.searchparam.MatchUrlService;
032import ca.uhn.fhir.jpa.searchparam.SearchParameterMap;
033import ca.uhn.fhir.jpa.searchparam.extractor.ISearchParamExtractor;
034import ca.uhn.fhir.jpa.searchparam.extractor.SearchParamExtractorDstu2;
035import ca.uhn.fhir.jpa.searchparam.extractor.SearchParamExtractorDstu3;
036import ca.uhn.fhir.jpa.searchparam.extractor.SearchParamExtractorR4;
037import ca.uhn.fhir.jpa.searchparam.extractor.SearchParamExtractorR4B;
038import ca.uhn.fhir.jpa.searchparam.extractor.SearchParamExtractorR5;
039import ca.uhn.fhir.jpa.searchparam.extractor.SearchParamExtractorService;
040import ca.uhn.fhir.jpa.searchparam.matcher.InMemoryResourceMatcher;
041import ca.uhn.fhir.jpa.searchparam.matcher.IndexedSearchParamExtractor;
042import ca.uhn.fhir.jpa.searchparam.matcher.SearchParamMatcher;
043import ca.uhn.fhir.jpa.searchparam.registry.SearchParamRegistryImpl;
044import ca.uhn.fhir.jpa.searchparam.registry.SearchParameterCanonicalizer;
045import ca.uhn.fhir.jpa.searchparam.util.SearchParameterHelper;
046import ca.uhn.fhir.rest.server.util.ISearchParamRegistry;
047import org.springframework.beans.factory.annotation.Autowired;
048import org.springframework.context.annotation.Bean;
049import org.springframework.context.annotation.Configuration;
050import org.springframework.context.annotation.Lazy;
051import org.springframework.context.annotation.Scope;
052
053@Configuration
054public class SearchParamConfig {
055
056        @Autowired
057        private FhirContext myFhirContext;
058
059        @Bean
060        public ISearchParamExtractor searchParamExtractor() {
061                switch (myFhirContext.getVersion().getVersion()) {
062                        case DSTU2:
063                                return new SearchParamExtractorDstu2();
064                        case DSTU3:
065                                return new SearchParamExtractorDstu3();
066                        case R4:
067                                return new SearchParamExtractorR4();
068                        case R4B:
069                                return new SearchParamExtractorR4B();
070                        case R5:
071                                return new SearchParamExtractorR5();
072                        case DSTU2_HL7ORG:
073                        case DSTU2_1:
074                        default:
075                                throw new IllegalStateException(Msg.code(501) + "Can not handle version: "
076                                                + myFhirContext.getVersion().getVersion());
077                }
078        }
079
080        @Bean
081        public ISearchParamRegistry searchParamRegistry() {
082                return new SearchParamRegistryImpl();
083        }
084
085        @Bean
086        public MatchUrlService matchUrlService() {
087                return new MatchUrlService();
088        }
089
090        @Bean
091        @Lazy
092        public SearchParamExtractorService searchParamExtractorService() {
093                return new SearchParamExtractorService();
094        }
095
096        @Bean
097        @Lazy
098        public SearchParameterCanonicalizer searchParameterCanonicalizer(FhirContext theFhirContext) {
099                return new SearchParameterCanonicalizer(theFhirContext);
100        }
101
102        @Bean
103        public IndexedSearchParamExtractor indexedSearchParamExtractor() {
104                return new IndexedSearchParamExtractor();
105        }
106
107        @Bean
108        public InMemoryResourceMatcher inMemoryResourceMatcher() {
109                return new InMemoryResourceMatcher();
110        }
111
112        @Bean
113        public SearchParamMatcher searchParamMatcher() {
114                return new SearchParamMatcher();
115        }
116
117        @Bean
118        IResourceChangeListenerRegistry resourceChangeListenerRegistry(
119                        FhirContext theFhirContext,
120                        ResourceChangeListenerCacheFactory theResourceChangeListenerCacheFactory,
121                        InMemoryResourceMatcher theInMemoryResourceMatcher) {
122                return new ResourceChangeListenerRegistryImpl(
123                                theFhirContext, theResourceChangeListenerCacheFactory, theInMemoryResourceMatcher);
124        }
125
126        @Bean
127        IResourceChangeListenerCacheRefresher resourceChangeListenerCacheRefresher() {
128                return new ResourceChangeListenerCacheRefresherImpl();
129        }
130
131        @Bean
132        ResourceChangeListenerCacheFactory registeredResourceListenerFactory() {
133                return new ResourceChangeListenerCacheFactory();
134        }
135
136        @Bean
137        @Scope("prototype")
138        ResourceChangeListenerCache registeredResourceChangeListener(
139                        String theResourceName,
140                        IResourceChangeListener theResourceChangeListener,
141                        SearchParameterMap theSearchParameterMap,
142                        long theRemoteRefreshIntervalMs) {
143                return new ResourceChangeListenerCache(
144                                theResourceName, theResourceChangeListener, theSearchParameterMap, theRemoteRefreshIntervalMs);
145        }
146
147        @Bean
148        @Lazy
149        SearchParameterHelper searchParameterHelper(FhirContext theFhirContext) {
150                return new SearchParameterHelper(searchParameterCanonicalizer(theFhirContext));
151        }
152}