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