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.EvaluationProviderFactory;
026import ca.uhn.fhir.cql.common.provider.LibraryResolutionProvider;
027import ca.uhn.fhir.cql.dstu3.evaluation.ProviderFactory;
028import ca.uhn.fhir.cql.dstu3.helper.LibraryHelper;
029import ca.uhn.fhir.cql.dstu3.listener.ElmCacheResourceChangeListener;
030import ca.uhn.fhir.cql.dstu3.provider.JpaTerminologyProvider;
031import ca.uhn.fhir.cql.dstu3.provider.LibraryResolutionProviderImpl;
032import ca.uhn.fhir.cql.dstu3.provider.MeasureOperationsProvider;
033import ca.uhn.fhir.jpa.api.dao.DaoRegistry;
034import ca.uhn.fhir.jpa.api.dao.IFhirResourceDao;
035import ca.uhn.fhir.jpa.cache.IResourceChangeListenerRegistry;
036import ca.uhn.fhir.jpa.searchparam.SearchParameterMap;
037import ca.uhn.fhir.jpa.term.api.ITermReadSvcDstu3;
038import org.cqframework.cql.cql2elm.CqlTranslatorOptions;
039import org.cqframework.cql.cql2elm.model.Model;
040import org.cqframework.cql.elm.execution.Library;
041import org.hl7.elm.r1.VersionedIdentifier;
042import org.opencds.cqf.cql.engine.fhir.model.Dstu3FhirModelResolver;
043import org.opencds.cqf.cql.engine.model.ModelResolver;
044import org.opencds.cqf.cql.engine.terminology.TerminologyProvider;
045import org.opencds.cqf.cql.evaluator.engine.model.CachingModelResolverDecorator;
046import org.springframework.context.annotation.Bean;
047import org.springframework.context.annotation.Configuration;
048import org.springframework.context.annotation.Lazy;
049
050import java.util.Map;
051
052@Configuration
053public class CqlDstu3Config extends BaseCqlConfig {
054        @Lazy
055        @Bean
056        TerminologyProvider terminologyProvider(ITermReadSvcDstu3 theITermReadSvc, DaoRegistry theDaoRegistry, IValidationSupport theValidationSupport) {
057                return new JpaTerminologyProvider(theITermReadSvc, theDaoRegistry, theValidationSupport);
058        }
059
060        @Lazy
061        @Bean
062        EvaluationProviderFactory evaluationProviderFactory(FhirContext theFhirContext, DaoRegistry theDaoRegistry,
063                        TerminologyProvider theLocalSystemTerminologyProvider, ModelResolver modelResolver) {
064                return new ProviderFactory(theFhirContext, theDaoRegistry, theLocalSystemTerminologyProvider, modelResolver);
065        }
066
067        @Lazy
068        @Bean
069        LibraryResolutionProvider<org.hl7.fhir.dstu3.model.Library> libraryResolutionProvider() {
070                return new LibraryResolutionProviderImpl();
071        }
072
073        @Lazy
074        @Bean
075        public MeasureOperationsProvider measureOperationsProvider() {
076                return new MeasureOperationsProvider();
077        }
078
079        @Lazy
080        @Bean
081        public ModelResolver fhirModelResolver() {
082                return new CachingModelResolverDecorator(new Dstu3FhirModelResolver());
083        }
084
085        @Lazy
086        @Bean
087        public LibraryHelper libraryHelper(Map<VersionedIdentifier, Model> globalModelCache, Map<org.cqframework.cql.elm.execution.VersionedIdentifier, Library> globalLibraryCache, CqlTranslatorOptions cqlTranslatorOptions) {
088                return new LibraryHelper(globalModelCache, globalLibraryCache, cqlTranslatorOptions);
089        }
090
091
092        @Bean
093        public CqlTranslatorOptions cqlTranslatorOptions() {
094                return CqlTranslatorOptions.defaultOptions().withCompatibilityLevel("1.3");
095        }
096
097        @Bean
098        public ElmCacheResourceChangeListener elmCacheResourceChangeListener(IResourceChangeListenerRegistry resourceChangeListenerRegistry, IFhirResourceDao<org.hl7.fhir.dstu3.model.Library> libraryDao,  Map<org.cqframework.cql.elm.execution.VersionedIdentifier, Library> globalLibraryCache) {
099                ElmCacheResourceChangeListener listener = new ElmCacheResourceChangeListener(libraryDao, globalLibraryCache);
100                resourceChangeListenerRegistry.registerResourceResourceChangeListener("Library", SearchParameterMap.newSynchronous(), listener, 1000);
101                return listener;
102        }
103}