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(
060                        FhirContext theFhirContext,
061                        CachingValidationSupport theCachingValidationSupport,
062                        ValidationSupportChain theValidationSupportChain,
063                        IValidationSupport theValidationSupport,
064                        DaoRegistry theDaoRegistry) {
065                if (theFhirContext.getVersion().getVersion().isEqualOrNewerThan(FhirVersionEnum.DSTU3)) {
066                        FhirInstanceValidator val = new FhirInstanceValidator(theCachingValidationSupport);
067                        val.setValidatorResourceFetcher(
068                                        jpaValidatorResourceFetcher(theFhirContext, theValidationSupport, theDaoRegistry));
069                        val.setValidatorPolicyAdvisor(jpaValidatorPolicyAdvisor());
070                        val.setBestPracticeWarningLevel(BestPracticeWarningLevel.Warning);
071                        val.setValidationSupport(theCachingValidationSupport);
072                        return val;
073                } else {
074                        CachingValidationSupport cachingValidationSupport = new CachingValidationSupport(
075                                        new HapiToHl7OrgDstu2ValidatingSupportWrapper(theValidationSupportChain));
076                        FhirInstanceValidator retVal = new FhirInstanceValidator(cachingValidationSupport);
077                        retVal.setBestPracticeWarningLevel(BestPracticeWarningLevel.Warning);
078                        return retVal;
079                }
080        }
081
082        @Bean
083        @Lazy
084        public ValidatorResourceFetcher jpaValidatorResourceFetcher(
085                        FhirContext theFhirContext, IValidationSupport theValidationSupport, DaoRegistry theDaoRegistry) {
086                return new ValidatorResourceFetcher(theFhirContext, theValidationSupport, theDaoRegistry);
087        }
088
089        @Bean
090        @Lazy
091        public ValidatorPolicyAdvisor jpaValidatorPolicyAdvisor() {
092                return new ValidatorPolicyAdvisor();
093        }
094}