001package org.hl7.fhir.convertors.loaders;
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 java.io.IOException;
034import java.io.InputStream;
035import java.util.ArrayList;
036import java.util.List;
037import java.util.UUID;
038
039import org.hl7.fhir.convertors.VersionConvertorAdvisor30;
040import org.hl7.fhir.convertors.VersionConvertor_10_30;
041import org.hl7.fhir.convertors.loaders.BaseLoaderR5.NullLoaderKnowledgeProvider;
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.dstu3.context.SimpleWorkerContext.IContextResourceLoader;
046import org.hl7.fhir.dstu3.model.Bundle;
047import org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent;
048import org.hl7.fhir.dstu3.model.Bundle.BundleType;
049import org.hl7.fhir.dstu3.model.CodeSystem;
050import org.hl7.fhir.dstu3.model.MetadataResource;
051import org.hl7.fhir.dstu3.model.StructureDefinition;
052import org.hl7.fhir.dstu3.model.StructureDefinition.StructureDefinitionKind;
053import org.hl7.fhir.dstu3.model.UriType;
054import org.hl7.fhir.dstu3.model.ValueSet;
055import org.hl7.fhir.exceptions.FHIRException;
056
057public class R2ToR3Loader extends BaseLoaderR3 implements VersionConvertorAdvisor30 {
058
059  private List<CodeSystem> cslist = new ArrayList<>();
060
061  public R2ToR3Loader() {
062    super(new String[0], new NullLoaderKnowledgeProvider());
063  }
064
065  @Override
066  public Bundle loadBundle(InputStream stream, boolean isJson) throws FHIRException, IOException {
067    Resource r2 = null;
068    if (isJson)
069      r2 = new JsonParser().parse(stream);
070    else
071      r2 = new XmlParser().parse(stream);
072    org.hl7.fhir.dstu3.model.Resource r3 = VersionConvertor_10_30.convertResource(r2, this);
073    Bundle b;
074    if (r3 instanceof Bundle)
075      b = (Bundle) r3;
076    else {
077      b = new Bundle();
078      b.setId(UUID.randomUUID().toString().toLowerCase());
079      b.setType(BundleType.COLLECTION);
080      b.addEntry().setResource(r3).setFullUrl(r3 instanceof MetadataResource ? ((MetadataResource) r3).getUrl() : null);
081    }
082    for (CodeSystem cs : cslist) {
083      BundleEntryComponent be = b.addEntry();
084      be.setFullUrl(cs.getUrl());
085      be.setResource(cs);
086    }
087    cslist.clear();
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    if (patchUrls) {
100      for (BundleEntryComponent be : b.getEntry()) {
101        if (be.hasResource() && be.getResource() instanceof StructureDefinition) {
102          StructureDefinition sd = (StructureDefinition) be.getResource();
103          sd.setUrl(sd.getUrl().replace(URL_BASE, URL_DSTU2));
104          sd.addExtension().setUrl(URL_ELEMENT_DEF_NAMESPACE).setValue(new UriType(URL_BASE));
105        }
106      }
107    }
108    return b;
109  }
110
111  @Override
112  public boolean ignoreEntry(BundleEntryComponent src) {
113    return false;
114  }
115
116  @Override
117  public Resource convert(org.hl7.fhir.dstu3.model.Resource resource) throws FHIRException {
118    return null;
119  }
120
121  @Override
122  public void handleCodeSystem(CodeSystem cs, ValueSet vs) {
123    cs.setId(vs.getId());
124    cs.setValueSet(vs.getUrl());
125    cslist.add(cs);
126  }
127
128  @Override
129  public CodeSystem getCodeSystem(ValueSet src) {
130    return null;
131  }
132
133}