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.validation;
021
022import ca.uhn.fhir.context.FhirContext;
023import ca.uhn.fhir.context.support.IValidationSupport;
024import ca.uhn.fhir.jpa.config.JpaConfig;
025import ca.uhn.fhir.jpa.packages.NpmJpaValidationSupport;
026import ca.uhn.fhir.jpa.term.api.ITermConceptMappingSvc;
027import ca.uhn.fhir.jpa.term.api.ITermReadSvc;
028import jakarta.annotation.PostConstruct;
029import jakarta.annotation.PreDestroy;
030import org.hl7.fhir.common.hapi.validation.support.CommonCodeSystemsTerminologyService;
031import org.hl7.fhir.common.hapi.validation.support.InMemoryTerminologyServerValidationSupport;
032import org.hl7.fhir.common.hapi.validation.support.SnapshotGeneratingValidationSupport;
033import org.hl7.fhir.common.hapi.validation.support.ValidationSupportChain;
034import org.hl7.fhir.common.hapi.validation.validator.WorkerContextValidationSupportAdapter;
035import org.springframework.beans.factory.annotation.Autowired;
036import org.springframework.beans.factory.annotation.Qualifier;
037
038public class JpaValidationSupportChain extends ValidationSupportChain {
039
040        private final FhirContext myFhirContext;
041        private final WorkerContextValidationSupportAdapter myWorkerContextValidationSupportAdapter;
042
043        @Autowired
044        @Qualifier(JpaConfig.JPA_VALIDATION_SUPPORT)
045        public IValidationSupport myJpaValidationSupport;
046
047        @Qualifier("myDefaultProfileValidationSupport")
048        @Autowired
049        private IValidationSupport myDefaultProfileValidationSupport;
050
051        @Autowired
052        private ITermReadSvc myTerminologyService;
053
054        @Autowired
055        private NpmJpaValidationSupport myNpmJpaValidationSupport;
056
057        @Autowired
058        private ITermConceptMappingSvc myConceptMappingSvc;
059
060        @Autowired
061        private InMemoryTerminologyServerValidationSupport myInMemoryTerminologyServerValidationSupport;
062
063        /**
064         * Constructor
065         */
066        public JpaValidationSupportChain(
067                        FhirContext theFhirContext,
068                        CacheConfiguration theCacheConfiguration,
069                        WorkerContextValidationSupportAdapter theWorkerContextValidationSupportAdapter) {
070                super(theCacheConfiguration);
071
072                assert theFhirContext != null;
073                assert theCacheConfiguration != null;
074
075                myFhirContext = theFhirContext;
076                myWorkerContextValidationSupportAdapter = theWorkerContextValidationSupportAdapter;
077        }
078
079        @Override
080        public FhirContext getFhirContext() {
081                return myFhirContext;
082        }
083
084        @PreDestroy
085        public void flush() {
086                invalidateCaches();
087        }
088
089        @PostConstruct
090        public void postConstruct() {
091                myWorkerContextValidationSupportAdapter.setValidationSupport(this);
092
093                addValidationSupport(myDefaultProfileValidationSupport);
094                addValidationSupport(myJpaValidationSupport);
095                addValidationSupport(myTerminologyService);
096                addValidationSupport(
097                                new SnapshotGeneratingValidationSupport(myFhirContext, myWorkerContextValidationSupportAdapter));
098                addValidationSupport(myInMemoryTerminologyServerValidationSupport);
099                addValidationSupport(myNpmJpaValidationSupport);
100                addValidationSupport(new CommonCodeSystemsTerminologyService(myFhirContext));
101                addValidationSupport(myConceptMappingSvc);
102        }
103}