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_10_50; 040import org.hl7.fhir.convertors.factory.VersionConvertorFactory_10_50; 041import org.hl7.fhir.convertors.txClient.TerminologyClientFactory; 042import org.hl7.fhir.dstu2.formats.JsonParser; 043import org.hl7.fhir.dstu2.formats.XmlParser; 044import org.hl7.fhir.dstu2.model.Resource; 045import org.hl7.fhir.exceptions.FHIRException; 046import org.hl7.fhir.r5.context.IContextResourceLoader; 047import org.hl7.fhir.r5.model.Bundle; 048import org.hl7.fhir.r5.model.Bundle.BundleEntryComponent; 049import org.hl7.fhir.r5.model.Bundle.BundleType; 050import org.hl7.fhir.r5.model.CanonicalResource; 051import org.hl7.fhir.r5.model.CodeSystem; 052import org.hl7.fhir.r5.model.StructureDefinition; 053import org.hl7.fhir.r5.model.StructureDefinition.StructureDefinitionKind; 054import org.hl7.fhir.r5.terminologies.client.TerminologyClientManager.ITerminologyClientFactory; 055 056public class R2ToR5Loader extends BaseLoaderR5 implements IContextResourceLoader { 057 058 private final BaseAdvisor_10_50 advisor = new BaseAdvisor_10_50(); 059 060 public R2ToR5Loader(List<String> types, ILoaderKnowledgeProviderR5 lkp) { 061 super(types, lkp); 062 } 063 064 @Override 065 public Bundle loadBundle(InputStream stream, boolean isJson) throws FHIRException, IOException { 066 Resource r2 = null; 067 if (isJson) 068 r2 = new JsonParser().parse(stream); 069 else 070 r2 = new XmlParser().parse(stream); 071 org.hl7.fhir.r5.model.Resource r5 = VersionConvertorFactory_10_50.convertResource(r2, advisor); 072 Bundle b; 073 if (r5 instanceof Bundle) { 074 b = (Bundle) r5; 075 } else { 076 b = new Bundle(); 077 b.setId(UUID.randomUUID().toString().toLowerCase()); 078 b.setType(BundleType.COLLECTION); 079 b.addEntry().setResource(r5).setFullUrl(r5 instanceof CanonicalResource ? ((CanonicalResource) r5).getUrl() : null); 080 } 081 // Add any code systems defined as part of processing value sets to the end of the converted Bundle 082 for (CodeSystem cs : advisor.getCslist()) { 083 BundleEntryComponent be = b.addEntry(); 084 be.setFullUrl(cs.getUrl()); 085 be.setResource(cs); 086 } 087 088 advisor.getCslist().clear(); 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()) { 104 doPatchUrls(be.getResource()); 105 } 106 } 107 } 108 return b; 109 } 110 111 112 @Override 113 public org.hl7.fhir.r5.model.Resource loadResource(InputStream stream, boolean isJson) throws FHIRException, IOException { 114 Resource r2 = null; 115 if (isJson) 116 r2 = new JsonParser().parse(stream); 117 else 118 r2 = new XmlParser().parse(stream); 119 org.hl7.fhir.r5.model.Resource r5 = VersionConvertorFactory_10_50.convertResource(r2, advisor); 120 setPath(r5); 121 if (killPrimitives) { 122 throw new FHIRException("Cannot kill primitives when using deferred loading"); 123 } 124 if (patchUrls) { 125 doPatchUrls(r5); 126 } 127 return r5; 128 } 129 130 @Override 131 public List<CodeSystem> getCodeSystems() { 132 List<CodeSystem> list = new ArrayList<>(); 133 if (!advisor.getCslist().isEmpty()) { 134 list.addAll(advisor.getCslist()); 135 advisor.getCslist().clear(); 136 } 137 return list; 138 } 139 140 @Override 141 protected String versionString() { 142 return "1.0"; 143 } 144 145 @Override 146 public ITerminologyClientFactory txFactory() { 147 return new TerminologyClientFactory(versionString()); 148 } 149 150}