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.convertors.advisors.impl.BaseAdvisor_30_50;
034import org.hl7.fhir.convertors.factory.VersionConvertorFactory_30_50;
035import org.hl7.fhir.dstu3.formats.JsonParser;
036import org.hl7.fhir.dstu3.formats.XmlParser;
037import org.hl7.fhir.dstu3.model.Resource;
038import org.hl7.fhir.exceptions.FHIRException;
039import org.hl7.fhir.r5.context.IWorkerContext.IContextResourceLoader;
040import org.hl7.fhir.r5.model.*;
041import org.hl7.fhir.r5.model.Bundle.BundleEntryComponent;
042import org.hl7.fhir.r5.model.Bundle.BundleType;
043import org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent;
044import org.hl7.fhir.r5.model.StructureDefinition.StructureDefinitionKind;
045
046import java.io.IOException;
047import java.io.InputStream;
048import java.util.ArrayList;
049import java.util.List;
050import java.util.UUID;
051
052public class R3ToR5Loader extends BaseLoaderR5 implements IContextResourceLoader {
053
054  private final BaseAdvisor_30_50 advisor = new BaseAdvisor_30_50();
055
056  public R3ToR5Loader(String[] types, ILoaderKnowledgeProviderR5 lkp) {
057    super(types, lkp);
058  }
059
060  @Override
061  public Bundle loadBundle(InputStream stream, boolean isJson) throws FHIRException, IOException {
062    Resource r3 = null;
063    if (isJson)
064      r3 = new JsonParser().parse(stream);
065    else
066      r3 = new XmlParser().parse(stream);
067    org.hl7.fhir.r5.model.Resource r5 = VersionConvertorFactory_30_50.convertResource(r3, advisor);
068
069    Bundle b;
070    if (r5 instanceof Bundle)
071      b = (Bundle) r5;
072    else {
073      b = new Bundle();
074      b.setId(UUID.randomUUID().toString().toLowerCase());
075      b.setType(BundleType.COLLECTION);
076      b.addEntry().setResource(r5).setFullUrl(r5 instanceof CanonicalResource ? ((CanonicalResource) r5).getUrl() : null);
077    }
078    for (CodeSystem cs : advisor.getCslist()) {
079      BundleEntryComponent be = b.addEntry();
080      be.setFullUrl(cs.getUrl());
081      be.setResource(cs);
082    }
083    if (killPrimitives) {
084      List<BundleEntryComponent> remove = new ArrayList<BundleEntryComponent>();
085      for (BundleEntryComponent be : b.getEntry()) {
086        if (be.hasResource() && be.getResource() instanceof StructureDefinition) {
087          StructureDefinition sd = (StructureDefinition) be.getResource();
088          if (sd.getKind() == StructureDefinitionKind.PRIMITIVETYPE)
089            remove.add(be);
090        }
091      }
092      b.getEntry().removeAll(remove);
093    }
094    if (patchUrls) {
095      for (BundleEntryComponent be : b.getEntry()) {
096        if (be.hasResource() && be.getResource() instanceof StructureDefinition) {
097          StructureDefinition sd = (StructureDefinition) be.getResource();
098          sd.setUrl(sd.getUrl().replace(URL_BASE, URL_DSTU3));
099          sd.addExtension().setUrl(URL_ELEMENT_DEF_NAMESPACE).setValue(new UriType(URL_BASE));
100          for (ElementDefinition ed : sd.getSnapshot().getElement())
101            patchUrl(ed);
102          for (ElementDefinition ed : sd.getDifferential().getElement())
103            patchUrl(ed);
104        }
105      }
106    }
107    return b;
108  }
109
110  @Override
111  public org.hl7.fhir.r5.model.Resource loadResource(InputStream stream, boolean isJson) throws FHIRException, IOException {
112    Resource r3 = null;
113    if (isJson)
114      r3 = new JsonParser().parse(stream);
115    else
116      r3 = new XmlParser().parse(stream);
117    org.hl7.fhir.r5.model.Resource r5 = VersionConvertorFactory_30_50.convertResource(r3);
118    setPath(r5);
119
120    if (!advisor.getCslist().isEmpty()) {
121      throw new FHIRException("Error: Cannot have included code systems");
122    }
123    if (killPrimitives) {
124      throw new FHIRException("Cannot kill primitives when using deferred loading");
125    }
126    if (patchUrls) {
127      if (r5 instanceof StructureDefinition) {
128        StructureDefinition sd = (StructureDefinition) r5;
129        sd.setUrl(sd.getUrl().replace(URL_BASE, URL_R4));
130        sd.addExtension().setUrl(URL_ELEMENT_DEF_NAMESPACE).setValue(new UriType(URL_BASE));
131        for (ElementDefinition ed : sd.getSnapshot().getElement())
132          patchUrl(ed);
133        for (ElementDefinition ed : sd.getDifferential().getElement())
134          patchUrl(ed);
135      }
136    }
137    return r5;
138  }
139
140  private void patchUrl(ElementDefinition ed) {
141    for (TypeRefComponent tr : ed.getType()) {
142      for (CanonicalType s : tr.getTargetProfile()) {
143        s.setValue(s.getValue().replace(URL_BASE, URL_DSTU3));
144      }
145      for (CanonicalType s : tr.getProfile()) {
146        s.setValue(s.getValue().replace(URL_BASE, URL_DSTU3));
147      }
148    }
149  }
150}