
001package org.hl7.fhir.convertors.loaders.loaderR4; 002 003import java.io.IOException; 004import java.io.InputStream; 005import java.util.ArrayList; 006import java.util.List; 007import java.util.UUID; 008 009/* 010 Copyright (c) 2011+, HL7, Inc. 011 All rights reserved. 012 013 Redistribution and use in source and binary forms, with or without modification, 014 are permitted provided that the following conditions are met: 015 016 * Redistributions of source code must retain the above copyright notice, this 017 list of conditions and the following disclaimer. 018 * Redistributions in binary form must reproduce the above copyright notice, 019 this list of conditions and the following disclaimer in the documentation 020 and/or other materials provided with the distribution. 021 * Neither the name of HL7 nor the names of its contributors may be used to 022 endorse or promote products derived from this software without specific 023 prior written permission. 024 025 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 026 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 027 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 028 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 029 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 030 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 031 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 032 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 033 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 034 POSSIBILITY OF SUCH DAMAGE. 035 036 */ 037 038 039import org.hl7.fhir.convertors.advisors.impl.BaseAdvisor_30_40; 040import org.hl7.fhir.convertors.factory.VersionConvertorFactory_30_40; 041import org.hl7.fhir.dstu3.formats.JsonParser; 042import org.hl7.fhir.dstu3.formats.XmlParser; 043import org.hl7.fhir.dstu3.model.Resource; 044import org.hl7.fhir.exceptions.FHIRException; 045import org.hl7.fhir.r4.context.SimpleWorkerContext.IContextResourceLoader; 046import org.hl7.fhir.r4.model.Bundle; 047import org.hl7.fhir.r4.model.Bundle.BundleEntryComponent; 048import org.hl7.fhir.r4.model.Bundle.BundleType; 049import org.hl7.fhir.r4.model.CanonicalType; 050import org.hl7.fhir.r4.model.CodeSystem; 051import org.hl7.fhir.r4.model.ElementDefinition; 052import org.hl7.fhir.r4.model.ElementDefinition.TypeRefComponent; 053import org.hl7.fhir.r4.model.MetadataResource; 054import org.hl7.fhir.r4.model.StructureDefinition; 055import org.hl7.fhir.r4.model.StructureDefinition.StructureDefinitionKind; 056import org.hl7.fhir.r5.utils.ToolingExtensions; 057import org.hl7.fhir.r4.model.UriType; 058 059public class R3ToR4Loader extends BaseLoaderR4 implements IContextResourceLoader { 060 061 private final BaseAdvisor_30_40 advisor = new BaseAdvisor_30_40(); 062 063 public R3ToR4Loader() { 064 super(new String[0], new NullLoaderKnowledgeProviderR4()); 065 } 066 067 @Override 068 public Bundle loadBundle(InputStream stream, boolean isJson) throws FHIRException, IOException { 069 Resource r3 = null; 070 if (isJson) 071 r3 = new JsonParser().parse(stream); 072 else 073 r3 = new XmlParser().parse(stream); 074 org.hl7.fhir.r4.model.Resource r4 = VersionConvertorFactory_30_40.convertResource(r3, advisor); 075 076 Bundle b; 077 if (r4 instanceof Bundle) 078 b = (Bundle) r4; 079 else { 080 b = new Bundle(); 081 b.setId(UUID.randomUUID().toString().toLowerCase()); 082 b.setType(BundleType.COLLECTION); 083 b.addEntry().setResource(r4).setFullUrl(r4 instanceof MetadataResource ? ((MetadataResource) r4).getUrl() : null); 084 } 085 for (CodeSystem cs : advisor.getCslist()) { 086 BundleEntryComponent be = b.addEntry(); 087 be.setFullUrl(cs.getUrl()); 088 be.setResource(cs); 089 } 090 if (killPrimitives) { 091 List<BundleEntryComponent> remove = new ArrayList<BundleEntryComponent>(); 092 for (BundleEntryComponent be : b.getEntry()) { 093 if (be.hasResource() && be.getResource() instanceof StructureDefinition) { 094 StructureDefinition sd = (StructureDefinition) be.getResource(); 095 if (sd.getKind() == StructureDefinitionKind.PRIMITIVETYPE) 096 remove.add(be); 097 } 098 } 099 b.getEntry().removeAll(remove); 100 } 101 if (patchUrls) { 102 for (BundleEntryComponent be : b.getEntry()) { 103 if (be.hasResource() && be.getResource() instanceof StructureDefinition) { 104 StructureDefinition sd = (StructureDefinition) be.getResource(); 105 sd.setUrl(sd.getUrl().replace(URL_BASE, URL_DSTU3)); 106 sd.addExtension().setUrl(ToolingExtensions.EXT_XML_NAMESPACE).setValue(new UriType(URL_BASE)); 107 for (ElementDefinition ed : sd.getSnapshot().getElement()) 108 patchUrl(ed); 109 for (ElementDefinition ed : sd.getDifferential().getElement()) 110 patchUrl(ed); 111 } 112 } 113 } 114 return b; 115 } 116 117 private void patchUrl(ElementDefinition ed) { 118 for (TypeRefComponent tr : ed.getType()) { 119 for (CanonicalType s : tr.getTargetProfile()) { 120 s.setValue(s.getValue().replace(URL_BASE, URL_DSTU3)); 121 } 122 for (CanonicalType s : tr.getProfile()) { 123 s.setValue(s.getValue().replace(URL_BASE, URL_DSTU3)); 124 } 125 } 126 } 127}