001package ca.uhn.fhir.jpa.dao.dstu3;
002
003/*
004 * #%L
005 * HAPI FHIR JPA Server
006 * %%
007 * Copyright (C) 2014 - 2021 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 * http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.context.support.ConceptValidationOptions;
024import ca.uhn.fhir.context.support.IValidationSupport;
025import ca.uhn.fhir.context.support.ValueSetExpansionOptions;
026import ca.uhn.fhir.jpa.api.dao.IFhirResourceDaoValueSet;
027import ca.uhn.fhir.jpa.dao.BaseHapiFhirResourceDao;
028import ca.uhn.fhir.jpa.model.cross.IBasePersistedResource;
029import ca.uhn.fhir.jpa.model.entity.ResourceTable;
030import ca.uhn.fhir.rest.api.server.RequestDetails;
031import ca.uhn.fhir.rest.api.server.storage.TransactionDetails;
032import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
033import org.hl7.fhir.convertors.advisors.impl.BaseAdvisor_30_40;
034import org.hl7.fhir.convertors.factory.VersionConvertorFactory_30_40;
035import org.hl7.fhir.dstu3.model.CodeableConcept;
036import org.hl7.fhir.dstu3.model.Coding;
037import org.hl7.fhir.dstu3.model.ValueSet;
038import org.hl7.fhir.exceptions.FHIRException;
039import org.hl7.fhir.instance.model.api.IBaseResource;
040import org.hl7.fhir.instance.model.api.IIdType;
041import org.hl7.fhir.instance.model.api.IPrimitiveType;
042
043import java.util.Date;
044
045import static ca.uhn.fhir.jpa.dao.FhirResourceDaoValueSetDstu2.toStringOrNull;
046
047public class FhirResourceDaoValueSetDstu3 extends BaseHapiFhirResourceDao<ValueSet> implements IFhirResourceDaoValueSet<ValueSet, Coding, CodeableConcept> {
048
049        @Override
050        public org.hl7.fhir.dstu3.model.ValueSet expand(IIdType theId, ValueSetExpansionOptions theOptions, RequestDetails theRequestDetails) {
051                org.hl7.fhir.dstu3.model.ValueSet source = read(theId, theRequestDetails);
052                return expand(source, theOptions);
053        }
054
055        @Override
056        public org.hl7.fhir.dstu3.model.ValueSet expand(org.hl7.fhir.dstu3.model.ValueSet theSource, ValueSetExpansionOptions theOptions) {
057                org.hl7.fhir.r4.model.ValueSet canonicalInput = (org.hl7.fhir.r4.model.ValueSet) VersionConvertorFactory_30_40.convertResource(theSource, new BaseAdvisor_30_40(false));
058                org.hl7.fhir.r4.model.ValueSet canonicalOutput = myTerminologySvc.expandValueSet(theOptions, canonicalInput);
059                return (ValueSet) VersionConvertorFactory_30_40.convertResource(canonicalOutput, new BaseAdvisor_30_40(false));
060        }
061
062        @Override
063        public org.hl7.fhir.dstu3.model.ValueSet expandByIdentifier(String theUri, ValueSetExpansionOptions theOptions) {
064                org.hl7.fhir.r4.model.ValueSet canonicalOutput = myTerminologySvc.expandValueSet(theOptions, theUri);
065                return (ValueSet) VersionConvertorFactory_30_40.convertResource(canonicalOutput, new BaseAdvisor_30_40(false));
066        }
067
068        @Override
069        public IValidationSupport.CodeValidationResult validateCode(IPrimitiveType<String> theValueSetIdentifier, IIdType theId, IPrimitiveType<String> theCode,
070                                                                                                                                                                        IPrimitiveType<String> theSystem, IPrimitiveType<String> theDisplay, Coding theCoding,
071                                                                                                                                                                        CodeableConcept theCodeableConcept, RequestDetails theRequestDetails) {
072                return myTerminologySvc.validateCode(vsValidateCodeOptions(), theId, toStringOrNull(theValueSetIdentifier), toStringOrNull(theSystem), toStringOrNull(theCode), toStringOrNull(theDisplay), theCoding, theCodeableConcept);
073        }
074
075        @Override
076        public void purgeCaches() {
077                // nothing
078        }
079
080        @Override
081        public ResourceTable updateEntity(RequestDetails theRequestDetails, IBaseResource theResource, IBasePersistedResource theEntity, Date theDeletedTimestampOrNull, boolean thePerformIndexing,
082                                                                                                 boolean theUpdateVersion, TransactionDetails theTransactionDetails, boolean theForceUpdate, boolean theCreateNewHistoryEntry) {
083                ResourceTable retVal = super.updateEntity(theRequestDetails, theResource, theEntity, theDeletedTimestampOrNull, thePerformIndexing, theUpdateVersion, theTransactionDetails, theForceUpdate, theCreateNewHistoryEntry);
084
085                if (getConfig().isPreExpandValueSets() && !retVal.isUnchangedInCurrentOperation()) {
086                        if (retVal.getDeleted() == null) {
087                                try {
088                                        ValueSet valueSet = (ValueSet) theResource;
089                                        org.hl7.fhir.r4.model.ValueSet converted = (org.hl7.fhir.r4.model.ValueSet) VersionConvertorFactory_30_40.convertResource(valueSet, new BaseAdvisor_30_40(false));
090                                        myTerminologySvc.storeTermValueSet(retVal, converted);
091                                } catch (FHIRException fe) {
092                                        throw new InternalErrorException(fe);
093                                }
094                        } else {
095                                myTerminologySvc.deleteValueSetAndChildren(retVal);
096                        }
097                }
098
099                return retVal;
100        }
101
102        public static ConceptValidationOptions vsValidateCodeOptions() {
103                return new ConceptValidationOptions().setValidateDisplay(true);
104        }
105
106}