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.context.support.IValidationSupport;
024import ca.uhn.fhir.i18n.Msg;
025import ca.uhn.fhir.context.support.TranslateConceptResults;
026import ca.uhn.fhir.jpa.api.dao.IFhirResourceDaoConceptMap;
027import ca.uhn.fhir.jpa.api.model.TranslationRequest;
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.jpa.term.api.ITermConceptMappingSvc;
032import ca.uhn.fhir.rest.api.server.RequestDetails;
033import ca.uhn.fhir.rest.api.server.storage.TransactionDetails;
034import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
035import org.hl7.fhir.convertors.advisors.impl.BaseAdvisor_30_40;
036import org.hl7.fhir.convertors.factory.VersionConvertorFactory_30_40;
037import org.hl7.fhir.dstu3.model.ConceptMap;
038import org.hl7.fhir.exceptions.FHIRException;
039import org.hl7.fhir.instance.model.api.IBaseResource;
040import org.springframework.beans.factory.annotation.Autowired;
041
042import java.util.Collections;
043import java.util.Date;
044
045public class FhirResourceDaoConceptMapDstu3 extends BaseHapiFhirResourceDao<ConceptMap> implements IFhirResourceDaoConceptMap<ConceptMap> {
046        @Autowired
047        private ITermConceptMappingSvc myTermConceptMappingSvc;
048        @Autowired
049        private IValidationSupport myValidationSupport;
050
051        @Override
052        public TranslateConceptResults translate(TranslationRequest theTranslationRequest, RequestDetails theRequestDetails) {
053                IValidationSupport.TranslateCodeRequest translateCodeRequest = theTranslationRequest.asTranslateCodeRequest();
054                return myValidationSupport.translateConcept(translateCodeRequest);
055        }
056
057
058        @Override
059        public ResourceTable updateEntity(RequestDetails theRequestDetails, IBaseResource theResource, IBasePersistedResource theEntity, Date theDeletedTimestampOrNull, boolean thePerformIndexing,
060                                                                                                 boolean theUpdateVersion, TransactionDetails theTransactionDetails, boolean theForceUpdate, boolean theCreateNewHistoryEntry) {
061                ResourceTable retVal = super.updateEntity(theRequestDetails, theResource, theEntity, theDeletedTimestampOrNull, thePerformIndexing, theUpdateVersion, theTransactionDetails, theForceUpdate, theCreateNewHistoryEntry);
062
063                if (!retVal.isUnchangedInCurrentOperation()) {
064                        if (retVal.getDeleted() == null) {
065                                try {
066                                        ConceptMap conceptMap = (ConceptMap) theResource;
067                                        org.hl7.fhir.r4.model.ConceptMap converted = (org.hl7.fhir.r4.model.ConceptMap) VersionConvertorFactory_30_40.convertResource(conceptMap, new BaseAdvisor_30_40(false));
068                                        myTermConceptMappingSvc.storeTermConceptMapAndChildren(retVal, converted);
069                                } catch (FHIRException fe) {
070                                        throw new InternalErrorException(Msg.code(1083) + fe);
071                                }
072                        } else {
073                                myTermConceptMappingSvc.deleteConceptMapAndChildren(retVal);
074                        }
075                }
076
077                return retVal;
078        }
079}