001/*-
002 * #%L
003 * HAPI FHIR JPA Server
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.config;
021
022import ca.uhn.fhir.context.FhirContext;
023import ca.uhn.fhir.context.FhirVersionEnum;
024import ca.uhn.fhir.context.support.DefaultProfileValidationSupport;
025import ca.uhn.fhir.context.support.IValidationSupport;
026import ca.uhn.fhir.jpa.api.config.JpaStorageSettings;
027import ca.uhn.fhir.jpa.api.dao.DaoRegistry;
028import ca.uhn.fhir.jpa.dao.JpaPersistedResourceValidationSupport;
029import ca.uhn.fhir.jpa.validation.JpaValidationSupportChain;
030import ca.uhn.fhir.jpa.validation.ValidatorPolicyAdvisor;
031import ca.uhn.fhir.jpa.validation.ValidatorResourceFetcher;
032import ca.uhn.fhir.validation.IInstanceValidatorModule;
033import org.hl7.fhir.common.hapi.validation.support.CachingValidationSupport;
034import org.hl7.fhir.common.hapi.validation.support.InMemoryTerminologyServerValidationSupport;
035import org.hl7.fhir.common.hapi.validation.support.ValidationSupportChain;
036import org.hl7.fhir.common.hapi.validation.validator.FhirInstanceValidator;
037import org.hl7.fhir.common.hapi.validation.validator.HapiToHl7OrgDstu2ValidatingSupportWrapper;
038import org.hl7.fhir.r5.utils.validation.constants.BestPracticeWarningLevel;
039import org.springframework.context.annotation.Bean;
040import org.springframework.context.annotation.Configuration;
041import org.springframework.context.annotation.Lazy;
042
043@Configuration
044public class ValidationSupportConfig {
045        @Bean(name = "myDefaultProfileValidationSupport")
046        public DefaultProfileValidationSupport defaultProfileValidationSupport(FhirContext theFhirContext) {
047                return new DefaultProfileValidationSupport(theFhirContext);
048        }
049
050        @Bean
051        public InMemoryTerminologyServerValidationSupport inMemoryTerminologyServerValidationSupport(
052                        FhirContext theFhirContext, JpaStorageSettings theStorageSettings) {
053                InMemoryTerminologyServerValidationSupport retVal =
054                                new InMemoryTerminologyServerValidationSupport(theFhirContext);
055                retVal.setIssueSeverityForCodeDisplayMismatch(theStorageSettings.getIssueSeverityForCodeDisplayMismatch());
056                return retVal;
057        }
058
059        @Bean(name = JpaConfig.JPA_VALIDATION_SUPPORT_CHAIN)
060        public JpaValidationSupportChain jpaValidationSupportChain(FhirContext theFhirContext) {
061                return new JpaValidationSupportChain(theFhirContext);
062        }
063
064        @Bean(name = JpaConfig.JPA_VALIDATION_SUPPORT)
065        public IValidationSupport jpaValidationSupport(FhirContext theFhirContext) {
066                return new JpaPersistedResourceValidationSupport(theFhirContext);
067        }
068
069        @Bean(name = "myInstanceValidator")
070        public IInstanceValidatorModule instanceValidator(
071                        FhirContext theFhirContext,
072                        CachingValidationSupport theCachingValidationSupport,
073                        ValidationSupportChain theValidationSupportChain,
074                        IValidationSupport theValidationSupport,
075                        DaoRegistry theDaoRegistry) {
076                if (theFhirContext.getVersion().getVersion().isEqualOrNewerThan(FhirVersionEnum.DSTU3)) {
077                        FhirInstanceValidator val = new FhirInstanceValidator(theCachingValidationSupport);
078                        val.setValidatorResourceFetcher(
079                                        jpaValidatorResourceFetcher(theFhirContext, theValidationSupport, theDaoRegistry));
080                        val.setValidatorPolicyAdvisor(jpaValidatorPolicyAdvisor());
081                        val.setBestPracticeWarningLevel(BestPracticeWarningLevel.Warning);
082                        val.setValidationSupport(theCachingValidationSupport);
083                        return val;
084                } else {
085                        CachingValidationSupport cachingValidationSupport = new CachingValidationSupport(
086                                        new HapiToHl7OrgDstu2ValidatingSupportWrapper(theValidationSupportChain));
087                        FhirInstanceValidator retVal = new FhirInstanceValidator(cachingValidationSupport);
088                        retVal.setBestPracticeWarningLevel(BestPracticeWarningLevel.Warning);
089                        return retVal;
090                }
091        }
092
093        @Bean
094        @Lazy
095        public ValidatorResourceFetcher jpaValidatorResourceFetcher(
096                        FhirContext theFhirContext, IValidationSupport theValidationSupport, DaoRegistry theDaoRegistry) {
097                return new ValidatorResourceFetcher(theFhirContext, theValidationSupport, theDaoRegistry);
098        }
099
100        @Bean
101        @Lazy
102        public ValidatorPolicyAdvisor jpaValidatorPolicyAdvisor() {
103                return new ValidatorPolicyAdvisor();
104        }
105}