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          doPatchUrls(be.getResource());
094        }
095      }
096    }
097    return b;
098  }
099
100  @Override
101  public Resource loadResource(InputStream stream, boolean isJson) throws FHIRException, IOException {
102    Resource r5 = null;
103    if (isJson)
104      r5 = new JsonParser().parse(stream);
105    else
106      r5 = new XmlParser().parse(stream);
107    setPath(r5);
108
109    if (killPrimitives) {
110      throw new FHIRException("Cannot kill primitives when using deferred loading");
111    }
112    if (patchUrls) {
113      doPatchUrls(r5);
114    }
115    if (r5 instanceof StructureDefinition) {
116      StructureDefinition sd = (StructureDefinition) r5;
117      if ("http://hl7.org/fhir/StructureDefinition/Base".equals(sd.getUrl())) {
118        sd.getSnapshot().getElementFirstRep().getConstraint().clear();
119        
120      }
121    }
122    return r5;
123  }
124  
125  @Override
126  public List<CodeSystem> getCodeSystems() {
127    return new ArrayList<>();
128  }
129
130  @Override
131  protected String versionString() {
132    return "5.0";
133  }
134
135
136  @Override
137  public ITerminologyClientFactory txFactory() {
138    return new TerminologyClientFactory(versionString());
139  }
140
141  @Override
142  public Set<String> reviewActualTypes(Set<String> types) {
143    return types;
144  }
145
146  @Override
147  public PackageResourceLoader editInfo(PackageResourceLoader pri) {
148    return pri;
149  }
150
151}