001package org.hl7.fhir.convertors.misc;
002
003import org.hl7.fhir.exceptions.FHIRException;
004import org.hl7.fhir.r5.context.IWorkerContext;
005import org.hl7.fhir.r5.context.SimpleWorkerContext;
006import org.hl7.fhir.r5.model.ValueSet;
007import org.hl7.fhir.r5.model.ValueSet.ConceptSetComponent;
008import org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager;
009import org.hl7.fhir.utilities.npm.NpmPackage;
010import org.hl7.fhir.utilities.npm.ToolsVersion;
011
012import java.io.IOException;
013
014public class OIDBasedValueSetImporter {
015
016  protected IWorkerContext context;
017
018  protected void init() throws FHIRException, IOException {
019    FilesystemPackageCacheManager pcm = new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION);
020    NpmPackage npm = pcm.loadPackage("hl7.fhir.r5.core", "current");
021    SimpleWorkerContext context = new SimpleWorkerContext.SimpleWorkerContextBuilder().withAllowLoadingDuplicates(true).fromPackage(npm);
022    context.loadFromPackage(pcm.loadPackage("hl7.terminology"), null);
023    this.context = context;
024  }
025
026  protected String fixVersionforSystem(String url, String csver) {
027    if ("http://snomed.info/sct".equals(url)) {
028      return "http://snomed.info/sct/731000124108/version/" + csver;
029    }
030    if ("http://loinc.org".equals(url)) {
031      return csver;
032    }
033    if ("http://www.nlm.nih.gov/research/umls/rxnorm".equals(url)) {
034      if (csver.length() == 8) {
035        return csver.substring(4, 6) + csver.substring(6, 8) + csver.substring(0, 4);
036      } else {
037        return csver;
038      }
039
040    }
041    return csver;
042  }
043
044  protected ConceptSetComponent getInclude(ValueSet vs, String url, String csver) {
045    for (ConceptSetComponent t : vs.getCompose().getInclude()) {
046      if (csver == null) {
047        if (t.getSystem().equals(url) && !t.hasVersion()) {
048          return t;
049        }
050      } else {
051        if (t.getSystem().equals(url) && t.hasVersion() && t.getVersion().equals(csver)) {
052          return t;
053        }
054      }
055    }
056    ConceptSetComponent c = vs.getCompose().addInclude();
057    c.setSystem(url);
058    c.setVersion(csver);
059    return c;
060  }
061}