001package org.hl7.fhir.convertors.loaders.loaderR5;
002
003import com.google.gson.JsonSyntaxException;
004import lombok.Getter;
005import lombok.Setter;
006import lombok.experimental.Accessors;
007import org.hl7.fhir.exceptions.FHIRException;
008import org.hl7.fhir.r5.context.IWorkerContext.IContextResourceLoader;
009import org.hl7.fhir.r5.model.Resource;
010import org.hl7.fhir.utilities.VersionUtilities;
011import org.hl7.fhir.utilities.npm.NpmPackage;
012
013import java.io.IOException;
014
015@Accessors(chain = true)
016public abstract class BaseLoaderR5 implements IContextResourceLoader {
017
018  protected final String URL_BASE = "http://hl7.org/fhir/";
019  protected final String URL_DSTU2 = "http://hl7.org/fhir/1.0/";
020  protected final String URL_DSTU2016MAY = "http://hl7.org/fhir/1.4/";
021  protected final String URL_DSTU3 = "http://hl7.org/fhir/3.0/";
022  protected final String URL_R4 = "http://hl7.org/fhir/4.0/";
023  protected final String URL_ELEMENT_DEF_NAMESPACE = "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace";
024  @Getter @Setter protected boolean patchUrls;
025  @Getter @Setter protected boolean killPrimitives;
026  @Getter protected String[] types;
027  protected ILoaderKnowledgeProviderR5 lkp;
028
029  public BaseLoaderR5(String[] types, ILoaderKnowledgeProviderR5 lkp) {
030    super();
031    this.types = types;
032    this.lkp = lkp;
033  }
034
035  public String getResourcePath(Resource resource) {
036    return lkp.getResourcePath(resource);
037  }
038
039  public void setPath(Resource r) {
040    String path = lkp.getResourcePath(r);
041    if (path != null) {
042      r.setUserData("path", path);
043    }
044  }
045
046  public IContextResourceLoader getNewLoader(NpmPackage npm) throws JsonSyntaxException, IOException {
047    BaseLoaderR5 ret = loaderFactory(npm);
048    ret.patchUrls = patchUrls;
049    ret.killPrimitives = killPrimitives;
050    return ret;
051  }
052
053  protected BaseLoaderR5 loaderFactory(NpmPackage npm) throws JsonSyntaxException, IOException {
054    if (VersionUtilities.isR5Ver(npm.fhirVersion())) {
055      return new R5ToR5Loader(types, lkp.forNewPackage(npm));
056    } else if (VersionUtilities.isR4Ver(npm.fhirVersion())) {
057      return new R4ToR5Loader(types, lkp.forNewPackage(npm), npm.version());
058    } else if (VersionUtilities.isR3Ver(npm.fhirVersion())) {
059      return new R3ToR5Loader(types, lkp.forNewPackage(npm));
060    } else if (VersionUtilities.isR2Ver(npm.fhirVersion())) {
061      return new R2ToR5Loader(types, lkp.forNewPackage(npm));
062    } else if (VersionUtilities.isR2BVer(npm.fhirVersion())) {
063      return new R2016MayToR5Loader(types, lkp.forNewPackage(npm));
064    } else {
065      throw new FHIRException("Unsupported FHIR Version " + npm.fhirVersion());
066    }
067  }
068
069}