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.UnknownCodeSystemWarningValidationSupport;
034import org.hl7.fhir.common.hapi.validation.support.ValidationSupportChain;
035import org.hl7.fhir.common.hapi.validation.validator.WorkerContextValidationSupportAdapter;
036import org.springframework.beans.factory.annotation.Autowired;
037import org.springframework.beans.factory.annotation.Qualifier;
038
039public class JpaValidationSupportChain extends ValidationSupportChain {
040
041        private final FhirContext myFhirContext;
042        private final WorkerContextValidationSupportAdapter myWorkerContextValidationSupportAdapter;
043
044        @Autowired
045        @Qualifier(JpaConfig.JPA_VALIDATION_SUPPORT)
046        public IValidationSupport myJpaValidationSupport;
047
048        @Qualifier("myDefaultProfileValidationSupport")
049        @Autowired
050        private IValidationSupport myDefaultProfileValidationSupport;
051
052        @Autowired
053        private ITermReadSvc myTerminologyService;
054
055        @Autowired
056        private NpmJpaValidationSupport myNpmJpaValidationSupport;
057
058        @Autowired
059        private ITermConceptMappingSvc myConceptMappingSvc;
060
061        @Autowired
062        private UnknownCodeSystemWarningValidationSupport myUnknownCodeSystemWarningValidationSupport;
063
064        @Autowired
065        private InMemoryTerminologyServerValidationSupport myInMemoryTerminologyServerValidationSupport;
066
067        /**
068         * Constructor
069         */
070        public JpaValidationSupportChain(
071                        FhirContext theFhirContext,
072                        CacheConfiguration theCacheConfiguration,
073                        WorkerContextValidationSupportAdapter theWorkerContextValidationSupportAdapter) {
074                super(theCacheConfiguration);
075
076                assert theFhirContext != null;
077                assert theCacheConfiguration != null;
078
079                myFhirContext = theFhirContext;
080                myWorkerContextValidationSupportAdapter = theWorkerContextValidationSupportAdapter;
081        }
082
083        @Override
084        public FhirContext getFhirContext() {
085                return myFhirContext;
086        }
087
088        @PreDestroy
089        public void flush() {
090                invalidateCaches();
091        }
092
093        @PostConstruct
094        public void postConstruct() {
095                myWorkerContextValidationSupportAdapter.setValidationSupport(this);
096
097                addValidationSupport(myDefaultProfileValidationSupport);
098                addValidationSupport(myJpaValidationSupport);
099                addValidationSupport(myTerminologyService);
100                addValidationSupport(
101                                new SnapshotGeneratingValidationSupport(myFhirContext, myWorkerContextValidationSupportAdapter));
102                addValidationSupport(myInMemoryTerminologyServerValidationSupport);
103                addValidationSupport(myNpmJpaValidationSupport);
104                addValidationSupport(new CommonCodeSystemsTerminologyService(myFhirContext));
105                addValidationSupport(myConceptMappingSvc);
106
107                // This needs to be last in the chain, it was designed for that
108                addValidationSupport(myUnknownCodeSystemWarningValidationSupport);
109        }
110}