001package org.hl7.fhir.convertors.loaders.loaderR3;
002
003/*
004  Copyright (c) 2011+, HL7, Inc.
005  All rights reserved.
006  
007  Redistribution and use in source and binary forms, with or without modification, 
008  are permitted provided that the following conditions are met:
009    
010   * Redistributions of source code must retain the above copyright notice, this 
011     list of conditions and the following disclaimer.
012   * Redistributions in binary form must reproduce the above copyright notice, 
013     this list of conditions and the following disclaimer in the documentation 
014     and/or other materials provided with the distribution.
015   * Neither the name of HL7 nor the names of its contributors may be used to 
016     endorse or promote products derived from this software without specific 
017     prior written permission.
018  
019  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
020  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
021  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
022  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
023  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
024  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
025  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
026  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
027  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
028  POSSIBILITY OF SUCH DAMAGE.
029  
030 */
031
032
033import org.hl7.fhir.convertors.advisors.impl.BaseAdvisor_10_30;
034import org.hl7.fhir.convertors.factory.VersionConvertorFactory_10_30;
035import org.hl7.fhir.dstu2.formats.JsonParser;
036import org.hl7.fhir.dstu2.formats.XmlParser;
037import org.hl7.fhir.dstu2.model.Resource;
038import org.hl7.fhir.dstu3.model.Bundle;
039import org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent;
040import org.hl7.fhir.dstu3.model.Bundle.BundleType;
041import org.hl7.fhir.dstu3.model.MetadataResource;
042import org.hl7.fhir.dstu3.model.StructureDefinition;
043import org.hl7.fhir.dstu3.model.StructureDefinition.StructureDefinitionKind;
044import org.hl7.fhir.dstu3.model.UriType;
045import org.hl7.fhir.exceptions.FHIRException;
046
047import java.io.IOException;
048import java.io.InputStream;
049import java.util.ArrayList;
050import java.util.List;
051import java.util.UUID;
052
053public class R2ToR3Loader extends BaseLoaderR3 {
054
055  private final BaseAdvisor_10_30 advisor_10_30 = new BaseAdvisor_10_30();
056
057  public R2ToR3Loader() {
058    super(new String[0], new NullLoaderKnowledgeProviderR3());
059  }
060
061  @Override
062  public Bundle loadBundle(InputStream stream, boolean isJson) throws FHIRException, IOException {
063    Resource r2;
064    if (isJson)
065      r2 = new JsonParser().parse(stream);
066    else
067      r2 = new XmlParser().parse(stream);
068    org.hl7.fhir.dstu3.model.Resource r3 = VersionConvertorFactory_10_30.convertResource(r2, advisor_10_30);
069    Bundle b;
070    if (r3 instanceof Bundle) {
071      b = (Bundle) r3;
072    } else {
073      b = new Bundle();
074      b.setId(UUID.randomUUID().toString().toLowerCase());
075      b.setType(BundleType.COLLECTION);
076      b.addEntry().setResource(r3).setFullUrl(r3 instanceof MetadataResource ? ((MetadataResource) r3).getUrl() : null);
077    }
078    advisor_10_30.getCslist().forEach(cs -> {
079      BundleEntryComponent be = b.addEntry();
080      be.setFullUrl(cs.getUrl());
081      be.setResource(cs);
082    });
083    advisor_10_30.getCslist().clear();
084    if (killPrimitives) {
085      List<BundleEntryComponent> remove = new ArrayList<BundleEntryComponent>();
086      for (BundleEntryComponent be : b.getEntry()) {
087        if (be.hasResource() && be.getResource() instanceof StructureDefinition) {
088          StructureDefinition sd = (StructureDefinition) be.getResource();
089          if (sd.getKind() == StructureDefinitionKind.PRIMITIVETYPE)
090            remove.add(be);
091        }
092      }
093      b.getEntry().removeAll(remove);
094    }
095    if (patchUrls) {
096      b.getEntry().stream()
097        .filter(be -> be.hasResource() && be.getResource() instanceof StructureDefinition)
098        .map(be -> (StructureDefinition) be.getResource())
099        .forEach(sd -> {
100          sd.setUrl(sd.getUrl().replace(URL_BASE, URL_DSTU2));
101          sd.addExtension().setUrl(URL_ELEMENT_DEF_NAMESPACE).setValue(new UriType(URL_BASE));
102        });
103    }
104    return b;
105  }
106}