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