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