
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_30_50; 040import org.hl7.fhir.convertors.factory.VersionConvertorFactory_30_50; 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.r5.context.IWorkerContext.IContextResourceLoader; 046import org.hl7.fhir.r5.model.Bundle; 047import org.hl7.fhir.r5.model.Bundle.BundleEntryComponent; 048import org.hl7.fhir.r5.model.Bundle.BundleType; 049import org.hl7.fhir.r5.model.CanonicalResource; 050import org.hl7.fhir.r5.model.CodeSystem; 051import org.hl7.fhir.r5.model.StructureDefinition; 052import org.hl7.fhir.r5.model.StructureDefinition.StructureDefinitionKind; 053 054public class R3ToR5Loader extends BaseLoaderR5 implements IContextResourceLoader { 055 056 private final BaseAdvisor_30_50 advisor = new BaseAdvisor_30_50(); 057 058 public R3ToR5Loader(List<String> types, ILoaderKnowledgeProviderR5 lkp) { 059 super(types, lkp); 060 } 061 062 @Override 063 public Bundle loadBundle(InputStream stream, boolean isJson) throws FHIRException, IOException { 064 Resource r3 = null; 065 if (isJson) 066 r3 = new JsonParser().parse(stream); 067 else 068 r3 = new XmlParser().parse(stream); 069 org.hl7.fhir.r5.model.Resource r5 = VersionConvertorFactory_30_50.convertResource(r3, advisor); 070 071 Bundle b; 072 if (r5 instanceof Bundle) 073 b = (Bundle) r5; 074 else { 075 b = new Bundle(); 076 b.setId(UUID.randomUUID().toString().toLowerCase()); 077 b.setType(BundleType.COLLECTION); 078 b.addEntry().setResource(r5).setFullUrl(r5 instanceof CanonicalResource ? ((CanonicalResource) r5).getUrl() : null); 079 } 080 for (CodeSystem cs : advisor.getCslist()) { 081 BundleEntryComponent be = b.addEntry(); 082 be.setFullUrl(cs.getUrl()); 083 be.setResource(cs); 084 } 085 if (killPrimitives) { 086 List<BundleEntryComponent> remove = new ArrayList<BundleEntryComponent>(); 087 for (BundleEntryComponent be : b.getEntry()) { 088 if (be.hasResource() && be.getResource() instanceof StructureDefinition) { 089 StructureDefinition sd = (StructureDefinition) be.getResource(); 090 if (sd.getKind() == StructureDefinitionKind.PRIMITIVETYPE) 091 remove.add(be); 092 } 093 } 094 b.getEntry().removeAll(remove); 095 } 096 if (patchUrls) { 097 for (BundleEntryComponent be : b.getEntry()) { 098 if (be.hasResource()) { 099 doPatchUrls(be.getResource()); 100 } 101 } 102 } 103 return b; 104 } 105 106 @Override 107 public org.hl7.fhir.r5.model.Resource loadResource(InputStream stream, boolean isJson) throws FHIRException, IOException { 108 Resource r3 = null; 109 if (isJson) 110 r3 = new JsonParser().parse(stream); 111 else 112 r3 = new XmlParser().parse(stream); 113 org.hl7.fhir.r5.model.Resource r5 = VersionConvertorFactory_30_50.convertResource(r3); 114 setPath(r5); 115 116 if (!advisor.getCslist().isEmpty()) { 117 throw new FHIRException("Error: Cannot have included code systems"); 118 } 119 if (killPrimitives) { 120 throw new FHIRException("Cannot kill primitives when using deferred loading"); 121 } 122 if (patchUrls) { 123 doPatchUrls(r5); 124 } 125 return r5; 126 } 127 128 @Override 129 public List<CodeSystem> getCodeSystems() { 130 return new ArrayList<>(); 131 } 132 133 @Override 134 protected String versionString() { 135 return "3.0"; 136 } 137 138}