001package ca.uhn.fhir.jpa.dao.dstu3;
002
003/*
004 * #%L
005 * HAPI FHIR JPA Server
006 * %%
007 * Copyright (C) 2014 - 2022 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.i18n.Msg;
024import ca.uhn.fhir.context.support.ConceptValidationOptions;
025import ca.uhn.fhir.context.support.IValidationSupport;
026import ca.uhn.fhir.context.support.ValueSetExpansionOptions;
027import ca.uhn.fhir.jpa.api.dao.IFhirResourceDaoValueSet;
028import ca.uhn.fhir.jpa.dao.BaseHapiFhirResourceDao;
029import ca.uhn.fhir.jpa.model.cross.IBasePersistedResource;
030import ca.uhn.fhir.jpa.model.entity.ResourceTable;
031import ca.uhn.fhir.rest.api.server.RequestDetails;
032import ca.uhn.fhir.rest.api.server.storage.TransactionDetails;
033import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
034import org.hl7.fhir.convertors.advisors.impl.BaseAdvisor_30_40;
035import org.hl7.fhir.convertors.factory.VersionConvertorFactory_30_40;
036import org.hl7.fhir.dstu3.model.CodeableConcept;
037import org.hl7.fhir.dstu3.model.Coding;
038import org.hl7.fhir.dstu3.model.ValueSet;
039import org.hl7.fhir.exceptions.FHIRException;
040import org.hl7.fhir.instance.model.api.IBaseResource;
041import org.hl7.fhir.instance.model.api.IIdType;
042import org.hl7.fhir.instance.model.api.IPrimitiveType;
043
044import java.util.Date;
045
046import static ca.uhn.fhir.jpa.dao.FhirResourceDaoValueSetDstu2.toStringOrNull;
047
048public class FhirResourceDaoValueSetDstu3 extends BaseHapiFhirResourceDao<ValueSet> implements IFhirResourceDaoValueSet<ValueSet, Coding, CodeableConcept> {
049
050        @Override
051        public org.hl7.fhir.dstu3.model.ValueSet expand(IIdType theId, ValueSetExpansionOptions theOptions, RequestDetails theRequestDetails) {
052                org.hl7.fhir.dstu3.model.ValueSet source = read(theId, theRequestDetails);
053                return expand(source, theOptions);
054        }
055
056        @Override
057        public org.hl7.fhir.dstu3.model.ValueSet expand(org.hl7.fhir.dstu3.model.ValueSet theSource, ValueSetExpansionOptions theOptions) {
058                org.hl7.fhir.r4.model.ValueSet canonicalInput = (org.hl7.fhir.r4.model.ValueSet) VersionConvertorFactory_30_40.convertResource(theSource, new BaseAdvisor_30_40(false));
059                org.hl7.fhir.r4.model.ValueSet canonicalOutput = myTerminologySvc.expandValueSet(theOptions, canonicalInput);
060                return (ValueSet) VersionConvertorFactory_30_40.convertResource(canonicalOutput, new BaseAdvisor_30_40(false));
061        }
062
063        @Override
064        public org.hl7.fhir.dstu3.model.ValueSet expandByIdentifier(String theUri, ValueSetExpansionOptions theOptions) {
065                org.hl7.fhir.r4.model.ValueSet canonicalOutput = myTerminologySvc.expandValueSet(theOptions, theUri);
066                return (ValueSet) VersionConvertorFactory_30_40.convertResource(canonicalOutput, new BaseAdvisor_30_40(false));
067        }
068
069        @Override
070        public IValidationSupport.CodeValidationResult validateCode(IPrimitiveType<String> theValueSetIdentifier, IIdType theId, IPrimitiveType<String> theCode,
071                                                                                                                                                                        IPrimitiveType<String> theSystem, IPrimitiveType<String> theDisplay, Coding theCoding,
072                                                                                                                                                                        CodeableConcept theCodeableConcept, RequestDetails theRequestDetails) {
073                return myTerminologySvc.validateCode(vsValidateCodeOptions(), theId, toStringOrNull(theValueSetIdentifier), toStringOrNull(theSystem), toStringOrNull(theCode), toStringOrNull(theDisplay), theCoding, theCodeableConcept);
074        }
075
076        @Override
077        public void purgeCaches() {
078                // nothing
079        }
080
081        @Override
082        public ResourceTable updateEntity(RequestDetails theRequestDetails, IBaseResource theResource, IBasePersistedResource theEntity, Date theDeletedTimestampOrNull, boolean thePerformIndexing,
083                                                                                                 boolean theUpdateVersion, TransactionDetails theTransactionDetails, boolean theForceUpdate, boolean theCreateNewHistoryEntry) {
084                ResourceTable retVal = super.updateEntity(theRequestDetails, theResource, theEntity, theDeletedTimestampOrNull, thePerformIndexing, theUpdateVersion, theTransactionDetails, theForceUpdate, theCreateNewHistoryEntry);
085
086                if (getConfig().isPreExpandValueSets() && !retVal.isUnchangedInCurrentOperation()) {
087                        if (retVal.getDeleted() == null) {
088                                try {
089                                        ValueSet valueSet = (ValueSet) theResource;
090                                        org.hl7.fhir.r4.model.ValueSet converted = (org.hl7.fhir.r4.model.ValueSet) VersionConvertorFactory_30_40.convertResource(valueSet, new BaseAdvisor_30_40(false));
091                                        myTerminologySvc.storeTermValueSet(retVal, converted);
092                                } catch (FHIRException fe) {
093                                        throw new InternalErrorException(Msg.code(1080) + fe);
094                                }
095                        } else {
096                                myTerminologySvc.deleteValueSetAndChildren(retVal);
097                        }
098                }
099
100                return retVal;
101        }
102
103        public static ConceptValidationOptions vsValidateCodeOptions() {
104                return new ConceptValidationOptions().setValidateDisplay(true);
105        }
106
107}