001package ca.uhn.fhir.jpa.term;
002
003import ca.uhn.fhir.context.FhirContext;
004import ca.uhn.fhir.context.support.ConceptValidationOptions;
005import ca.uhn.fhir.context.support.IValidationSupport;
006import ca.uhn.fhir.context.support.ValidationSupportContext;
007import ca.uhn.fhir.context.support.ValueSetExpansionOptions;
008import ca.uhn.fhir.jpa.model.entity.ResourceTable;
009import ca.uhn.fhir.jpa.term.api.ITermReadSvcR4;
010import ca.uhn.fhir.jpa.term.ex.ExpansionTooCostlyException;
011import org.hl7.fhir.instance.model.api.IBaseCoding;
012import org.hl7.fhir.instance.model.api.IBaseDatatype;
013import org.hl7.fhir.instance.model.api.IBaseResource;
014import org.hl7.fhir.r4.model.CodeSystem;
015import org.hl7.fhir.r4.model.CodeableConcept;
016import org.hl7.fhir.r4.model.Coding;
017import org.hl7.fhir.r4.model.ValueSet;
018import org.springframework.beans.factory.annotation.Autowired;
019import org.springframework.transaction.PlatformTransactionManager;
020
021import javax.annotation.Nonnull;
022import javax.transaction.Transactional;
023
024/*
025 * #%L
026 * HAPI FHIR JPA Server
027 * %%
028 * Copyright (C) 2014 - 2021 Smile CDR, Inc.
029 * %%
030 * Licensed under the Apache License, Version 2.0 (the "License");
031 * you may not use this file except in compliance with the License.
032 * You may obtain a copy of the License at
033 *
034 * http://www.apache.org/licenses/LICENSE-2.0
035 *
036 * Unless required by applicable law or agreed to in writing, software
037 * distributed under the License is distributed on an "AS IS" BASIS,
038 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
039 * See the License for the specific language governing permissions and
040 * limitations under the License.
041 * #L%
042 */
043
044public class TermReadSvcR4 extends BaseTermReadSvcImpl implements ITermReadSvcR4 {
045
046        @Autowired
047        private PlatformTransactionManager myTransactionManager;
048
049        @Override
050        public IBaseResource expandValueSet(ValueSetExpansionOptions theExpansionOptions, IBaseResource theInput) {
051                ValueSet valueSetToExpand = (ValueSet) theInput;
052                return super.expandValueSet(theExpansionOptions, valueSetToExpand);
053        }
054
055        @Override
056        public void expandValueSet(ValueSetExpansionOptions theExpansionOptions, IBaseResource theValueSetToExpand, IValueSetConceptAccumulator theValueSetCodeAccumulator) {
057                ValueSet valueSetToExpand = (ValueSet) theValueSetToExpand;
058                super.expandValueSet(theExpansionOptions, valueSetToExpand, theValueSetCodeAccumulator);
059        }
060
061        @Transactional(dontRollbackOn = {ExpansionTooCostlyException.class})
062        @Override
063        public IValidationSupport.ValueSetExpansionOutcome expandValueSet(ValidationSupportContext theValidationSupportContext, ValueSetExpansionOptions theExpansionOptions, @Nonnull IBaseResource theValueSetToExpand) {
064                ValueSet expanded = super.expandValueSet(theExpansionOptions, (ValueSet) theValueSetToExpand);
065                return new IValidationSupport.ValueSetExpansionOutcome(expanded);
066        }
067
068        @Override
069        protected ValueSet getValueSetFromResourceTable(ResourceTable theResourceTable) {
070                return myDaoRegistry.getResourceDao("ValueSet").toResource(ValueSet.class, theResourceTable, null, false);
071        }
072
073        @Override
074        public boolean isValueSetSupported(ValidationSupportContext theValidationSupportContext, String theValueSetUrl) {
075                return fetchValueSet(theValueSetUrl) != null;
076        }
077
078        @Override
079        public FhirContext getFhirContext() {
080                return myContext;
081        }
082
083
084        @Override
085        protected ValueSet toCanonicalValueSet(IBaseResource theValueSet) {
086                return (ValueSet) theValueSet;
087        }
088
089        @Override
090        protected CodeSystem toCanonicalCodeSystem(IBaseResource theCodeSystem) {
091                return (CodeSystem) theCodeSystem;
092        }
093
094        @Override
095        public LookupCodeResult lookupCode(ValidationSupportContext theValidationSupportContext, String theSystem, String theCode, String theDisplayLanguage) {
096                return super.lookupCode(theSystem, theCode, theDisplayLanguage);
097        }
098
099        @Override
100        public IValidationSupport.CodeValidationResult validateCodeIsInPreExpandedValueSet(ConceptValidationOptions theOptions, IBaseResource theValueSet, String theSystem, String theCode, String theDisplay, IBaseDatatype theCoding, IBaseDatatype theCodeableConcept) {
101                ValueSet valueSet = (ValueSet) theValueSet;
102                Coding coding = toCanonicalCoding(theCoding);
103                CodeableConcept codeableConcept = toCanonicalCodeableConcept(theCodeableConcept);
104                return super.validateCodeIsInPreExpandedValueSet(theOptions, valueSet, theSystem, theCode, theDisplay, coding, codeableConcept);
105        }
106
107        @Override
108        protected Coding toCanonicalCoding(IBaseDatatype theCoding) {
109                return (Coding) theCoding;
110        }
111
112        @Override
113        protected Coding toCanonicalCoding(IBaseCoding theCoding) {
114                return (Coding) theCoding;
115        }
116
117        @Override
118        protected CodeableConcept toCanonicalCodeableConcept(IBaseDatatype theCodeableConcept) {
119                return (CodeableConcept) theCodeableConcept;
120        }
121
122        @Override
123        public boolean isValueSetPreExpandedForCodeValidation(IBaseResource theValueSet) {
124                ValueSet valueSet = (ValueSet) theValueSet;
125                return super.isValueSetPreExpandedForCodeValidation(valueSet);
126        }
127}