001package org.hl7.fhir.convertors.loaders.loaderR5;
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.exceptions.FHIRException;
034import org.hl7.fhir.r5.formats.JsonParser;
035import org.hl7.fhir.r5.formats.XmlParser;
036import org.hl7.fhir.r5.model.*;
037import org.hl7.fhir.r5.model.Bundle.BundleEntryComponent;
038import org.hl7.fhir.r5.model.Bundle.BundleType;
039import org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent;
040import org.hl7.fhir.r5.model.StructureDefinition.StructureDefinitionKind;
041
042import java.io.IOException;
043import java.io.InputStream;
044import java.util.ArrayList;
045import java.util.List;
046import java.util.UUID;
047
048public class R5ToR5Loader extends BaseLoaderR5 {
049
050  // TODO Grahame, will this ever be populated? No conversion is being done?
051  private final List<CodeSystem> cslist = new ArrayList<>();
052
053  public R5ToR5Loader(String[] types, ILoaderKnowledgeProviderR5 lkp) {
054    super(types, lkp);
055  }
056
057  @Override
058  public Bundle loadBundle(InputStream stream, boolean isJson) throws FHIRException, IOException {
059    Resource r5 = null;
060    if (isJson)
061      r5 = new JsonParser().parse(stream);
062    else
063      r5 = new XmlParser().parse(stream);
064
065    Bundle b;
066    if (r5 instanceof Bundle)
067      b = (Bundle) r5;
068    else {
069      b = new Bundle();
070      b.setId(UUID.randomUUID().toString().toLowerCase());
071      b.setType(BundleType.COLLECTION);
072      b.addEntry().setResource(r5).setFullUrl(r5 instanceof CanonicalResource ? ((CanonicalResource) r5).getUrl() : null);
073    }
074    for (CodeSystem cs : cslist) {
075      BundleEntryComponent be = b.addEntry();
076      be.setFullUrl(cs.getUrl());
077      be.setResource(cs);
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() && be.getResource() instanceof StructureDefinition) {
093          StructureDefinition sd = (StructureDefinition) be.getResource();
094          sd.setUrl(sd.getUrl().replace(URL_BASE, URL_R4));
095          sd.addExtension().setUrl(URL_ELEMENT_DEF_NAMESPACE).setValue(new UriType(URL_BASE));
096          for (ElementDefinition ed : sd.getSnapshot().getElement())
097            patchUrl(ed);
098          for (ElementDefinition ed : sd.getDifferential().getElement())
099            patchUrl(ed);
100        }
101      }
102    }
103    return b;
104  }
105
106  @Override
107  public Resource loadResource(InputStream stream, boolean isJson) throws FHIRException, IOException {
108    Resource r5 = null;
109    if (isJson)
110      r5 = new JsonParser().parse(stream);
111    else
112      r5 = new XmlParser().parse(stream);
113    setPath(r5);
114
115    if (!cslist.isEmpty()) {
116      throw new FHIRException("Error: Cannot have included code systems");
117    }
118    if (killPrimitives) {
119      throw new FHIRException("Cannot kill primitives when using deferred loading");
120    }
121    if (patchUrls) {
122      if (r5 instanceof StructureDefinition) {
123        StructureDefinition sd = (StructureDefinition) r5;
124        sd.setUrl(sd.getUrl().replace(URL_BASE, URL_R4));
125        sd.addExtension().setUrl(URL_ELEMENT_DEF_NAMESPACE).setValue(new UriType(URL_BASE));
126        for (ElementDefinition ed : sd.getSnapshot().getElement())
127          patchUrl(ed);
128        for (ElementDefinition ed : sd.getDifferential().getElement())
129          patchUrl(ed);
130      }
131    }
132    return r5;
133  }
134
135  private void patchUrl(ElementDefinition ed) {
136    for (TypeRefComponent tr : ed.getType()) {
137      for (CanonicalType s : tr.getTargetProfile()) {
138        s.setValue(s.getValue().replace(URL_BASE, URL_R4));
139      }
140      for (CanonicalType s : tr.getProfile()) {
141        s.setValue(s.getValue().replace(URL_BASE, URL_R4));
142      }
143    }
144  }
145}