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_14_40; 040import org.hl7.fhir.convertors.factory.VersionConvertorFactory_14_40; 041import org.hl7.fhir.dstu2016may.formats.JsonParser; 042import org.hl7.fhir.dstu2016may.formats.XmlParser; 043import org.hl7.fhir.dstu2016may.model.Resource; 044import org.hl7.fhir.exceptions.FHIRException; 045import org.hl7.fhir.r4.conformance.ProfileUtilities; 046import org.hl7.fhir.r4.context.SimpleWorkerContext.IContextResourceLoader; 047import org.hl7.fhir.r4.model.Bundle; 048import org.hl7.fhir.r4.model.Bundle.BundleEntryComponent; 049import org.hl7.fhir.r4.model.Bundle.BundleType; 050import org.hl7.fhir.r4.model.CodeSystem; 051import org.hl7.fhir.r4.model.MetadataResource; 052import org.hl7.fhir.r4.model.StructureDefinition; 053import org.hl7.fhir.r4.model.StructureDefinition.StructureDefinitionKind; 054import org.hl7.fhir.r4.model.UriType; 055 056public class R2016MayToR4Loader extends BaseLoaderR4 implements IContextResourceLoader { 057 058 private final BaseAdvisor_14_40 advisor = new BaseAdvisor_14_40(); 059 060 public R2016MayToR4Loader() { 061 super(new String[0], null); 062 } 063 064 @Override 065 public Bundle loadBundle(InputStream stream, boolean isJson) throws FHIRException, IOException { 066 Resource r2016may = null; 067 if (isJson) 068 r2016may = new JsonParser().parse(stream); 069 else 070 r2016may = new XmlParser().parse(stream); 071 org.hl7.fhir.r4.model.Resource r4 = VersionConvertorFactory_14_40.convertResource(r2016may, advisor); 072 073 Bundle b; 074 if (r4 instanceof Bundle) 075 b = (Bundle) r4; 076 else { 077 b = new Bundle(); 078 b.setId(UUID.randomUUID().toString().toLowerCase()); 079 b.setType(BundleType.COLLECTION); 080 b.addEntry().setResource(r4).setFullUrl(r4 instanceof MetadataResource ? ((MetadataResource) r4).getUrl() : null); 081 } 082 083 for (CodeSystem cs : advisor.getCslist()) { 084 BundleEntryComponent be = b.addEntry(); 085 be.setFullUrl(cs.getUrl()); 086 be.setResource(cs); 087 } 088 if (killPrimitives) { 089 List<BundleEntryComponent> remove = new ArrayList<BundleEntryComponent>(); 090 for (BundleEntryComponent be : b.getEntry()) { 091 if (be.hasResource() && be.getResource() instanceof StructureDefinition) { 092 StructureDefinition sd = (StructureDefinition) be.getResource(); 093 if (sd.getKind() == StructureDefinitionKind.PRIMITIVETYPE) 094 remove.add(be); 095 } 096 } 097 b.getEntry().removeAll(remove); 098 } 099 for (BundleEntryComponent be : b.getEntry()) { 100 if (be.hasResource() && be.getResource() instanceof StructureDefinition) { 101 StructureDefinition sd = (StructureDefinition) be.getResource(); 102 new ProfileUtilities(null, null, null).setIds(sd, false); 103 if (patchUrls) { 104 sd.setUrl(sd.getUrl().replace(URL_BASE, "http://hl7.org/fhir/2016May/")); 105 sd.addExtension().setUrl(URL_ELEMENT_DEF_NAMESPACE).setValue(new UriType(URL_BASE)); 106 } 107 } 108 } 109 return b; 110 } 111}