001/*-
002 * #%L
003 * HAPI FHIR JPA Server
004 * %%
005 * Copyright (C) 2014 - 2024 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.term;
021
022import ca.uhn.fhir.jpa.api.dao.IFhirResourceDao;
023import ca.uhn.fhir.jpa.term.api.ITermVersionAdapterSvc;
024import ca.uhn.fhir.rest.api.server.RequestDetails;
025import ca.uhn.fhir.util.UrlUtil;
026import org.hl7.fhir.convertors.advisors.impl.BaseAdvisor_40_50;
027import org.hl7.fhir.convertors.factory.VersionConvertorFactory_40_50;
028import org.hl7.fhir.instance.model.api.IIdType;
029import org.hl7.fhir.r5.model.CodeSystem;
030import org.hl7.fhir.r5.model.ConceptMap;
031import org.hl7.fhir.r5.model.ValueSet;
032import org.springframework.beans.factory.annotation.Autowired;
033import org.springframework.context.ApplicationContext;
034import org.springframework.context.event.ContextRefreshedEvent;
035import org.springframework.context.event.EventListener;
036
037import static org.apache.commons.lang3.StringUtils.isBlank;
038
039public class TermVersionAdapterSvcR5 extends BaseTermVersionAdapterSvcImpl implements ITermVersionAdapterSvc {
040        private IFhirResourceDao<ConceptMap> myConceptMapResourceDao;
041        private IFhirResourceDao<CodeSystem> myCodeSystemResourceDao;
042        private IFhirResourceDao<ValueSet> myValueSetResourceDao;
043
044        @Autowired
045        private ApplicationContext myAppCtx;
046
047        /**
048         * Initialize the beans that are used by this service.
049         *
050         * Note: There is a circular dependency here where the CodeSystem DAO
051         * needs terminology services, and the term services need the CodeSystem DAO.
052         * So we look these up in a refresh event instead of just autowiring them
053         * in order to avoid weird circular reference errors.
054         */
055        @SuppressWarnings({"unchecked", "unused"})
056        @EventListener
057        public void start(ContextRefreshedEvent theEvent) {
058                myCodeSystemResourceDao = (IFhirResourceDao<CodeSystem>) myAppCtx.getBean("myCodeSystemDaoR5");
059                myValueSetResourceDao = (IFhirResourceDao<ValueSet>) myAppCtx.getBean("myValueSetDaoR5");
060                myConceptMapResourceDao = (IFhirResourceDao<ConceptMap>) myAppCtx.getBean("myConceptMapDaoR5");
061        }
062
063        @Override
064        public IIdType createOrUpdateCodeSystem(
065                        org.hl7.fhir.r4.model.CodeSystem theCodeSystemResource, RequestDetails theRequestDetails) {
066                validateCodeSystemForStorage(theCodeSystemResource);
067
068                CodeSystem codeSystemR4 = (CodeSystem)
069                                VersionConvertorFactory_40_50.convertResource(theCodeSystemResource, new BaseAdvisor_40_50(false));
070                if (isBlank(theCodeSystemResource.getIdElement().getIdPart())) {
071                        String matchUrl = "CodeSystem?url=" + UrlUtil.escapeUrlParam(theCodeSystemResource.getUrl());
072                        return myCodeSystemResourceDao
073                                        .update(codeSystemR4, matchUrl, theRequestDetails)
074                                        .getId();
075                } else {
076                        return myCodeSystemResourceDao
077                                        .update(codeSystemR4, theRequestDetails)
078                                        .getId();
079                }
080        }
081
082        @Override
083        public void createOrUpdateConceptMap(org.hl7.fhir.r4.model.ConceptMap theConceptMap) {
084
085                ConceptMap conceptMapR4 =
086                                (ConceptMap) VersionConvertorFactory_40_50.convertResource(theConceptMap, new BaseAdvisor_40_50(false));
087
088                if (isBlank(theConceptMap.getIdElement().getIdPart())) {
089                        String matchUrl = "ConceptMap?url=" + UrlUtil.escapeUrlParam(theConceptMap.getUrl());
090                        myConceptMapResourceDao.update(conceptMapR4, matchUrl);
091                } else {
092                        myConceptMapResourceDao.update(conceptMapR4);
093                }
094        }
095
096        @Override
097        public void createOrUpdateValueSet(org.hl7.fhir.r4.model.ValueSet theValueSet) {
098
099                ValueSet valueSetR4 =
100                                (ValueSet) VersionConvertorFactory_40_50.convertResource(theValueSet, new BaseAdvisor_40_50(false));
101
102                if (isBlank(theValueSet.getIdElement().getIdPart())) {
103                        String matchUrl = "ValueSet?url=" + UrlUtil.escapeUrlParam(theValueSet.getUrl());
104                        myValueSetResourceDao.update(valueSetR4, matchUrl);
105                } else {
106                        myValueSetResourceDao.update(valueSetR4);
107                }
108        }
109}