
001package org.hl7.fhir.convertors.loaders.loaderR5; 002 003import java.io.FileInputStream; 004import java.io.IOException; 005import java.io.InputStream; 006import java.util.ArrayList; 007import java.util.HashSet; 008import java.util.List; 009import java.util.Set; 010import java.util.UUID; 011 012/* 013 Copyright (c) 2011+, HL7, Inc. 014 All rights reserved. 015 016 Redistribution and use in source and binary forms, with or without modification, 017 are permitted provided that the following conditions are met: 018 019 * Redistributions of source code must retain the above copyright notice, this 020 list of conditions and the following disclaimer. 021 * Redistributions in binary form must reproduce the above copyright notice, 022 this list of conditions and the following disclaimer in the documentation 023 and/or other materials provided with the distribution. 024 * Neither the name of HL7 nor the names of its contributors may be used to 025 endorse or promote products derived from this software without specific 026 prior written permission. 027 028 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 029 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 030 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 031 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 032 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 033 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 034 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 035 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 036 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 037 POSSIBILITY OF SUCH DAMAGE. 038 039 */ 040 041 042import org.hl7.fhir.convertors.advisors.impl.BaseAdvisor_40_50; 043import org.hl7.fhir.convertors.factory.VersionConvertorFactory_40_50; 044import org.hl7.fhir.convertors.txClient.TerminologyClientFactory; 045import org.hl7.fhir.exceptions.FHIRException; 046import org.hl7.fhir.r4.formats.JsonParser; 047import org.hl7.fhir.r4.formats.XmlParser; 048import org.hl7.fhir.r4.model.Basic; 049import org.hl7.fhir.r4.model.Resource; 050import org.hl7.fhir.r5.conformance.StructureDefinitionHacker; 051import org.hl7.fhir.r5.context.IContextResourceLoader; 052import org.hl7.fhir.r5.context.SimpleWorkerContext.PackageResourceLoader; 053import org.hl7.fhir.r5.model.Bundle; 054import org.hl7.fhir.r5.model.Bundle.BundleEntryComponent; 055import org.hl7.fhir.r5.model.Bundle.BundleType; 056import org.hl7.fhir.r5.model.CanonicalResource; 057import org.hl7.fhir.r5.model.CodeSystem; 058import org.hl7.fhir.r5.model.StructureDefinition; 059import org.hl7.fhir.r5.model.StructureDefinition.StructureDefinitionKind; 060import org.hl7.fhir.r5.terminologies.client.TerminologyClientManager.ITerminologyClientFactory; 061import org.hl7.fhir.r5.utils.R5Hacker; 062import org.hl7.fhir.utilities.Utilities; 063import org.hl7.fhir.utilities.filesystem.ManagedFileAccess; 064 065public class R4ToR5Loader extends BaseLoaderR5 implements IContextResourceLoader { 066 067 private final BaseAdvisor_40_50 advisor = new BaseAdvisor_40_50(); 068 private String version; 069 070 public R4ToR5Loader(Set<String> types, ILoaderKnowledgeProviderR5 lkp, String version) { // might be 4B 071 super(types, lkp); 072 this.version = version; 073 } 074 075 @Override 076 public Bundle loadBundle(InputStream stream, boolean isJson) throws FHIRException, IOException { 077 Resource r4 = null; 078 if (isJson) 079 r4 = new JsonParser().parse(stream); 080 else 081 r4 = new XmlParser().parse(stream); 082 org.hl7.fhir.r5.model.Resource r5 = VersionConvertorFactory_40_50.convertResource(r4, advisor); 083 084 Bundle b; 085 if (r5 instanceof Bundle) 086 b = (Bundle) r5; 087 else { 088 b = new Bundle(); 089 b.setId(UUID.randomUUID().toString().toLowerCase()); 090 b.setType(BundleType.COLLECTION); 091 b.addEntry().setResource(r5).setFullUrl(r5 instanceof CanonicalResource ? ((CanonicalResource) r5).getUrl() : null); 092 } 093 for (CodeSystem cs : advisor.getCslist()) { 094 BundleEntryComponent be = b.addEntry(); 095 be.setFullUrl(cs.getUrl()); 096 be.setResource(cs); 097 } 098 if (killPrimitives) { 099 List<BundleEntryComponent> remove = new ArrayList<BundleEntryComponent>(); 100 for (BundleEntryComponent be : b.getEntry()) { 101 if (be.hasResource() && be.getResource() instanceof StructureDefinition) { 102 StructureDefinition sd = (StructureDefinition) be.getResource(); 103 if (sd.getKind() == StructureDefinitionKind.PRIMITIVETYPE) 104 remove.add(be); 105 } 106 } 107 b.getEntry().removeAll(remove); 108 } 109 if (patchUrls) { 110 for (BundleEntryComponent be : b.getEntry()) { 111 if (be.hasResource()) { 112 doPatchUrls(be.getResource()); 113 } 114 } 115 } 116 return b; 117 } 118 119 @Override 120 public org.hl7.fhir.r5.model.Resource loadResource(InputStream stream, boolean isJson) throws FHIRException, IOException { 121 Resource r4 = null; 122 if (isJson) 123 r4 = new JsonParser().parse(stream); 124 else 125 r4 = new XmlParser().parse(stream); 126 org.hl7.fhir.r5.model.Resource r5 = VersionConvertorFactory_40_50.convertResource(r4); 127 setPath(r5); 128 129 if (!advisor.getCslist().isEmpty()) { 130 throw new FHIRException("Error: Cannot have included code systems"); 131 } 132 if (killPrimitives) { 133 throw new FHIRException("Cannot kill primitives when using deferred loading"); 134 } 135 if (r5 instanceof StructureDefinition) { 136 r5 = new StructureDefinitionHacker(version).fixSD((StructureDefinition) r5); 137 } 138 if (patchUrls) { 139 doPatchUrls(r5); 140 } 141 return r5; 142 } 143 144 145 @Override 146 public List<CodeSystem> getCodeSystems() { 147 return new ArrayList<>(); 148 } 149 150 @Override 151 protected String versionString() { 152 return "4.0"; 153 } 154 155 156 @Override 157 public ITerminologyClientFactory txFactory() { 158 return new TerminologyClientFactory(versionString()); 159 } 160 161 @Override 162 public Set<String> reviewActualTypes(Set<String> types) { 163 Set<String> set = new HashSet<String>(); 164 for (String t : types) { 165 if (Utilities.existsInList(t, "ActorDefinition", "Requirements", "SubscriptionTopic", "TestPlan")) { 166 set.add("Basic"); 167 } else { 168 set.add(t); 169 } 170 } 171 return set; 172 } 173 174 @Override 175 public PackageResourceLoader editInfo(PackageResourceLoader pri) { 176 if (pri.getType().equals("Basic")) { 177 try { 178 InputStream f = pri.getStream(); 179 try { 180 Basic b = (Basic) new JsonParser().parse(f); 181 org.hl7.fhir.r5.model.Resource r5 = VersionConvertorFactory_40_50.convertResource(b); 182 if (r5 instanceof CanonicalResource) { 183 pri.setResource((CanonicalResource) r5); 184 pri.updateInfo(); 185 setPath(r5); 186 } else { 187 return null; 188 } 189 } finally { 190 f.close(); 191 } 192 } catch (Exception e) { 193 throw new FHIRException("Error loading Resource Basic/"+pri.getId()+": "+e.getMessage(), e); 194 } 195 } 196 return pri; 197 } 198}