001package ca.uhn.fhir.cql.config;
002
003/*-
004 * #%L
005 * HAPI FHIR JPA Server - Clinical Quality Language
006 * %%
007 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.context.FhirContext;
024import ca.uhn.fhir.context.support.IValidationSupport;
025import ca.uhn.fhir.cql.common.provider.CqlProviderFactory;
026import ca.uhn.fhir.cql.common.provider.EvaluationProviderFactory;
027import ca.uhn.fhir.cql.common.provider.LibraryResolutionProvider;
028import ca.uhn.fhir.cql.r4.evaluation.ProviderFactory;
029import ca.uhn.fhir.cql.r4.helper.LibraryHelper;
030import ca.uhn.fhir.cql.r4.listener.ElmCacheResourceChangeListener;
031import ca.uhn.fhir.cql.r4.provider.JpaTerminologyProvider;
032import ca.uhn.fhir.cql.r4.provider.LibraryResolutionProviderImpl;
033import ca.uhn.fhir.cql.r4.provider.MeasureOperationsProvider;
034import ca.uhn.fhir.jpa.api.dao.DaoRegistry;
035import ca.uhn.fhir.jpa.api.dao.IFhirResourceDao;
036import ca.uhn.fhir.jpa.cache.IResourceChangeListenerRegistry;
037import ca.uhn.fhir.jpa.searchparam.SearchParameterMap;
038import ca.uhn.fhir.jpa.term.api.ITermReadSvcR4;
039import org.cqframework.cql.cql2elm.CqlTranslatorOptions;
040import org.cqframework.cql.cql2elm.model.Model;
041import org.cqframework.cql.elm.execution.Library;
042import org.hl7.elm.r1.VersionedIdentifier;
043import org.opencds.cqf.cql.engine.fhir.model.R4FhirModelResolver;
044import org.opencds.cqf.cql.engine.model.ModelResolver;
045import org.opencds.cqf.cql.engine.terminology.TerminologyProvider;
046import org.opencds.cqf.cql.evaluator.engine.model.CachingModelResolverDecorator;
047import org.springframework.context.annotation.Bean;
048import org.springframework.context.annotation.Configuration;
049import org.springframework.context.annotation.Lazy;
050
051import java.util.Map;
052
053@Configuration
054public class CqlR4Config extends BaseCqlConfig {
055
056        @Lazy
057        @Bean
058        CqlProviderFactory cqlProviderFactory() {
059                return new CqlProviderFactory();
060        }
061
062        @Lazy
063        @Bean
064        TerminologyProvider terminologyProvider(ITermReadSvcR4 theITermReadSvc, DaoRegistry theDaoRegistry,
065                        IValidationSupport theValidationSupport) {
066                return new JpaTerminologyProvider(theITermReadSvc, theDaoRegistry, theValidationSupport);
067        }
068
069        @Lazy
070        @Bean
071        EvaluationProviderFactory evaluationProviderFactory(FhirContext theFhirContext, DaoRegistry theDaoRegistry,
072                        TerminologyProvider theLocalSystemTerminologyProvider, ModelResolver modelResolver) {
073                return new ProviderFactory(theFhirContext, theDaoRegistry, theLocalSystemTerminologyProvider, modelResolver);
074        }
075
076        @Lazy
077        @Bean
078        LibraryResolutionProvider<org.hl7.fhir.r4.model.Library> libraryResolutionProvider() {
079                return new LibraryResolutionProviderImpl();
080        }
081
082        @Lazy
083        @Bean
084        public MeasureOperationsProvider measureOperationsProvider() {
085                return new MeasureOperationsProvider();
086        }
087
088        @Lazy
089        @Bean
090        public ModelResolver fhirModelResolver() {
091                return new CachingModelResolverDecorator(new R4FhirModelResolver());
092        }
093
094        @Lazy
095        @Bean
096        public LibraryHelper libraryHelper(Map<VersionedIdentifier, Model> globalModelCache,
097                        Map<org.cqframework.cql.elm.execution.VersionedIdentifier, Library> globalLibraryCache,
098                        CqlTranslatorOptions cqlTranslatorOptions) {
099                return new LibraryHelper(globalModelCache, globalLibraryCache, cqlTranslatorOptions);
100        }
101
102        @Lazy
103        @Bean
104        public CqlTranslatorOptions cqlTranslatorOptions() {
105                return CqlTranslatorOptions.defaultOptions();
106        }
107
108        @Bean
109        public ElmCacheResourceChangeListener elmCacheResourceChangeListener(IResourceChangeListenerRegistry resourceChangeListenerRegistry, IFhirResourceDao<org.hl7.fhir.r4.model.Library> libraryDao,  Map<org.cqframework.cql.elm.execution.VersionedIdentifier, Library> globalLibraryCache) {
110                ElmCacheResourceChangeListener listener = new ElmCacheResourceChangeListener(libraryDao, globalLibraryCache);
111                resourceChangeListenerRegistry.registerResourceResourceChangeListener("Library", SearchParameterMap.newSynchronous(), listener, 1000);
112                return listener;
113        }
114}