001/*-
002 * #%L
003 * HAPI FHIR JPA Server
004 * %%
005 * Copyright (C) 2014 - 2023 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.dao.DaoRegistry;
027import ca.uhn.fhir.jpa.dao.JpaPersistedResourceValidationSupport;
028import ca.uhn.fhir.jpa.validation.JpaValidationSupportChain;
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.CachingValidationSupport;
033import org.hl7.fhir.common.hapi.validation.support.ValidationSupportChain;
034import org.hl7.fhir.common.hapi.validation.validator.FhirInstanceValidator;
035import org.hl7.fhir.common.hapi.validation.validator.HapiToHl7OrgDstu2ValidatingSupportWrapper;
036import org.hl7.fhir.r5.utils.validation.constants.BestPracticeWarningLevel;
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        @Bean(name = "myDefaultProfileValidationSupport")
044        public DefaultProfileValidationSupport defaultProfileValidationSupport(FhirContext theFhirContext) {
045                return new DefaultProfileValidationSupport(theFhirContext);
046        }
047
048        @Bean(name = JpaConfig.JPA_VALIDATION_SUPPORT_CHAIN)
049        public JpaValidationSupportChain jpaValidationSupportChain(FhirContext theFhirContext) {
050                return new JpaValidationSupportChain(theFhirContext);
051        }
052
053        @Bean(name = JpaConfig.JPA_VALIDATION_SUPPORT)
054        public IValidationSupport jpaValidationSupport(FhirContext theFhirContext) {
055                return new JpaPersistedResourceValidationSupport(theFhirContext);
056        }
057
058        @Bean(name = "myInstanceValidator")
059        public IInstanceValidatorModule instanceValidator(FhirContext theFhirContext, CachingValidationSupport theCachingValidationSupport, ValidationSupportChain theValidationSupportChain, IValidationSupport theValidationSupport, DaoRegistry theDaoRegistry) {
060                if (theFhirContext.getVersion().getVersion().isEqualOrNewerThan(FhirVersionEnum.DSTU3)) {
061                        FhirInstanceValidator val = new FhirInstanceValidator(theCachingValidationSupport);
062                        val.setValidatorResourceFetcher(jpaValidatorResourceFetcher(theFhirContext, theValidationSupport, theDaoRegistry));
063                        val.setValidatorPolicyAdvisor(jpaValidatorPolicyAdvisor());
064                        val.setBestPracticeWarningLevel(BestPracticeWarningLevel.Warning);
065                        val.setValidationSupport(theCachingValidationSupport);
066                        return val;
067                } else {
068                        CachingValidationSupport cachingValidationSupport = new CachingValidationSupport(new HapiToHl7OrgDstu2ValidatingSupportWrapper(theValidationSupportChain));
069                        FhirInstanceValidator retVal = new FhirInstanceValidator(cachingValidationSupport);
070                        retVal.setBestPracticeWarningLevel(BestPracticeWarningLevel.Warning);
071                        return retVal;
072                }
073        }
074
075        @Bean
076        @Lazy
077        public ValidatorResourceFetcher jpaValidatorResourceFetcher(FhirContext theFhirContext, IValidationSupport theValidationSupport, DaoRegistry theDaoRegistry) {
078                return new ValidatorResourceFetcher(theFhirContext, theValidationSupport, theDaoRegistry);
079        }
080
081        @Bean
082        @Lazy
083        public ValidatorPolicyAdvisor jpaValidatorPolicyAdvisor() {
084                return new ValidatorPolicyAdvisor();
085        }
086
087}