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.Set;
008import java.util.UUID;
009
010/*
011  Copyright (c) 2011+, HL7, Inc.
012  All rights reserved.
013  
014  Redistribution and use in source and binary forms, with or without modification, 
015  are permitted provided that the following conditions are met:
016    
017   * Redistributions of source code must retain the above copyright notice, this 
018     list of conditions and the following disclaimer.
019   * Redistributions in binary form must reproduce the above copyright notice, 
020     this list of conditions and the following disclaimer in the documentation 
021     and/or other materials provided with the distribution.
022   * Neither the name of HL7 nor the names of its contributors may be used to 
023     endorse or promote products derived from this software without specific 
024     prior written permission.
025  
026  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
027  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
028  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
029  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
030  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
031  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
032  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
033  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
034  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
035  POSSIBILITY OF SUCH DAMAGE.
036  
037 */
038
039
040import org.hl7.fhir.convertors.advisors.impl.BaseAdvisor_10_50;
041import org.hl7.fhir.convertors.factory.VersionConvertorFactory_10_50;
042import org.hl7.fhir.convertors.txClient.TerminologyClientFactory;
043import org.hl7.fhir.dstu2.formats.JsonParser;
044import org.hl7.fhir.dstu2.formats.XmlParser;
045import org.hl7.fhir.dstu2.model.Resource;
046import org.hl7.fhir.exceptions.FHIRException;
047import org.hl7.fhir.r5.context.IContextResourceLoader;
048import org.hl7.fhir.r5.context.SimpleWorkerContext.PackageResourceLoader;
049import org.hl7.fhir.r5.model.Bundle;
050import org.hl7.fhir.r5.model.Bundle.BundleEntryComponent;
051import org.hl7.fhir.r5.model.Bundle.BundleType;
052import org.hl7.fhir.r5.model.CanonicalResource;
053import org.hl7.fhir.r5.model.CodeSystem;
054import org.hl7.fhir.r5.model.StructureDefinition;
055import org.hl7.fhir.r5.model.StructureDefinition.StructureDefinitionKind;
056import org.hl7.fhir.r5.terminologies.client.TerminologyClientManager.ITerminologyClientFactory;
057
058public class R2ToR5Loader extends BaseLoaderR5 implements IContextResourceLoader {
059
060  private final BaseAdvisor_10_50 advisor = new BaseAdvisor_10_50();
061
062  public R2ToR5Loader(Set<String> types, ILoaderKnowledgeProviderR5 lkp) {
063    super(types, lkp);
064  }
065
066  @Override
067  public Bundle loadBundle(InputStream stream, boolean isJson) throws FHIRException, IOException {
068    Resource r2 = null;
069    if (isJson)
070      r2 = new JsonParser().parse(stream);
071    else
072      r2 = new XmlParser().parse(stream);
073    org.hl7.fhir.r5.model.Resource r5 = VersionConvertorFactory_10_50.convertResource(r2, advisor);
074    Bundle b;
075    if (r5 instanceof Bundle) {
076      b = (Bundle) r5;
077    } else {
078      b = new Bundle();
079      b.setId(UUID.randomUUID().toString().toLowerCase());
080      b.setType(BundleType.COLLECTION);
081      b.addEntry().setResource(r5).setFullUrl(r5 instanceof CanonicalResource ? ((CanonicalResource) r5).getUrl() : null);
082    }
083    // Add any code systems defined as part of processing value sets to the end of the converted Bundle
084    for (CodeSystem cs : advisor.getCslist()) {
085      BundleEntryComponent be = b.addEntry();
086      be.setFullUrl(cs.getUrl());
087      be.setResource(cs);
088    }
089
090    advisor.getCslist().clear();
091
092    if (killPrimitives) {
093      List<BundleEntryComponent> remove = new ArrayList<BundleEntryComponent>();
094      for (BundleEntryComponent be : b.getEntry()) {
095        if (be.hasResource() && be.getResource() instanceof StructureDefinition) {
096          StructureDefinition sd = (StructureDefinition) be.getResource();
097          if (sd.getKind() == StructureDefinitionKind.PRIMITIVETYPE)
098            remove.add(be);
099        }
100      }
101      b.getEntry().removeAll(remove);
102    }
103    if (patchUrls) {
104      for (BundleEntryComponent be : b.getEntry()) {
105        if (be.hasResource()) {
106          inspectResource(be.getResource());
107          doPatchUrls(be.getResource());
108        }
109      }
110    }
111    return b;
112  }
113
114
115  @Override
116  public org.hl7.fhir.r5.model.Resource loadResource(InputStream stream, boolean isJson) throws FHIRException, IOException {
117    Resource r2 = null;
118    if (isJson)
119      r2 = new JsonParser().parse(stream);
120    else
121      r2 = new XmlParser().parse(stream);
122    org.hl7.fhir.r5.model.Resource r5 = VersionConvertorFactory_10_50.convertResource(r2, advisor);
123    setPath(r5);
124    if (killPrimitives) {
125      throw new FHIRException("Cannot kill primitives when using deferred loading");
126    }
127    inspectResource(r5);
128    if (patchUrls) {
129      doPatchUrls(r5);
130    }
131    return r5;
132  }
133
134  @Override
135  public List<CodeSystem> getCodeSystems() {
136    List<CodeSystem> list = new ArrayList<>();
137    if (!advisor.getCslist().isEmpty()) {
138      list.addAll(advisor.getCslist());
139      advisor.getCslist().clear();
140    }
141    return list;
142  }
143
144  @Override
145  protected String versionString() {
146    return "1.0";
147  }
148
149  @Override
150  public ITerminologyClientFactory txFactory() {
151    return new TerminologyClientFactory(versionString());
152  }
153
154  @Override
155  public Set<String> reviewActualTypes(Set<String> types) {
156
157    // this isn't correct, but we don't care anymore for R2 IGs
158    return types;
159  }
160
161  @Override
162  public PackageResourceLoader editInfo(PackageResourceLoader pri) {
163    return pri;
164  }
165  
166}