001package org.hl7.fhir.convertors.loaders.loaderR5; 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_40_50; 040import org.hl7.fhir.convertors.factory.VersionConvertorFactory_40_50; 041import org.hl7.fhir.convertors.txClient.TerminologyClientFactory; 042import org.hl7.fhir.exceptions.FHIRException; 043import org.hl7.fhir.r4.formats.JsonParser; 044import org.hl7.fhir.r4.formats.XmlParser; 045import org.hl7.fhir.r4.model.Resource; 046import org.hl7.fhir.r5.conformance.StructureDefinitionHacker; 047import org.hl7.fhir.r5.context.IContextResourceLoader; 048import org.hl7.fhir.r5.model.Bundle; 049import org.hl7.fhir.r5.model.Bundle.BundleEntryComponent; 050import org.hl7.fhir.r5.model.Bundle.BundleType; 051import org.hl7.fhir.r5.model.CanonicalResource; 052import org.hl7.fhir.r5.model.CodeSystem; 053import org.hl7.fhir.r5.model.StructureDefinition; 054import org.hl7.fhir.r5.model.StructureDefinition.StructureDefinitionKind; 055import org.hl7.fhir.r5.terminologies.client.TerminologyClientManager.ITerminologyClientFactory; 056import org.hl7.fhir.utilities.VersionUtilities; 057 058public class R4ToR5Loader extends BaseLoaderR5 implements IContextResourceLoader { 059 060 private final BaseAdvisor_40_50 advisor = new BaseAdvisor_40_50(); 061 private String version; 062 063 public R4ToR5Loader(List<String> types, ILoaderKnowledgeProviderR5 lkp, String version) { // might be 4B 064 super(types, lkp); 065 this.version = version; 066 } 067 068 @Override 069 public Bundle loadBundle(InputStream stream, boolean isJson) throws FHIRException, IOException { 070 Resource r4 = null; 071 if (isJson) 072 r4 = new JsonParser().parse(stream); 073 else 074 r4 = new XmlParser().parse(stream); 075 org.hl7.fhir.r5.model.Resource r5 = VersionConvertorFactory_40_50.convertResource(r4, advisor); 076 077 Bundle b; 078 if (r5 instanceof Bundle) 079 b = (Bundle) r5; 080 else { 081 b = new Bundle(); 082 b.setId(UUID.randomUUID().toString().toLowerCase()); 083 b.setType(BundleType.COLLECTION); 084 b.addEntry().setResource(r5).setFullUrl(r5 instanceof CanonicalResource ? ((CanonicalResource) r5).getUrl() : null); 085 } 086 for (CodeSystem cs : advisor.getCslist()) { 087 BundleEntryComponent be = b.addEntry(); 088 be.setFullUrl(cs.getUrl()); 089 be.setResource(cs); 090 } 091 if (killPrimitives) { 092 List<BundleEntryComponent> remove = new ArrayList<BundleEntryComponent>(); 093 for (BundleEntryComponent be : b.getEntry()) { 094 if (be.hasResource() && be.getResource() instanceof StructureDefinition) { 095 StructureDefinition sd = (StructureDefinition) be.getResource(); 096 if (sd.getKind() == StructureDefinitionKind.PRIMITIVETYPE) 097 remove.add(be); 098 } 099 } 100 b.getEntry().removeAll(remove); 101 } 102 if (patchUrls) { 103 for (BundleEntryComponent be : b.getEntry()) { 104 if (be.hasResource()) { 105 doPatchUrls(be.getResource()); 106 } 107 } 108 } 109 return b; 110 } 111 112 @Override 113 public org.hl7.fhir.r5.model.Resource loadResource(InputStream stream, boolean isJson) throws FHIRException, IOException { 114 Resource r4 = null; 115 if (isJson) 116 r4 = new JsonParser().parse(stream); 117 else 118 r4 = new XmlParser().parse(stream); 119 org.hl7.fhir.r5.model.Resource r5 = VersionConvertorFactory_40_50.convertResource(r4); 120 setPath(r5); 121 122 if (!advisor.getCslist().isEmpty()) { 123 throw new FHIRException("Error: Cannot have included code systems"); 124 } 125 if (killPrimitives) { 126 throw new FHIRException("Cannot kill primitives when using deferred loading"); 127 } 128 if (r5 instanceof StructureDefinition) { 129 r5 = new StructureDefinitionHacker(version).fixSD((StructureDefinition) r5); 130 } 131 if (patchUrls) { 132 doPatchUrls(r5); 133 } 134 return r5; 135 } 136 137 138 @Override 139 public List<CodeSystem> getCodeSystems() { 140 return new ArrayList<>(); 141 } 142 143 @Override 144 protected String versionString() { 145 return "4.0"; 146 } 147 148 149 @Override 150 public ITerminologyClientFactory txFactory() { 151 return new TerminologyClientFactory(versionString()); 152 } 153 154}