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 ctxt = SimpleWorkerContext.fromPackage(npm);
022    ctxt.setAllowLoadingDuplicates(true);
023    ctxt.loadFromPackage(pcm.loadPackage("hl7.terminology"), null);
024    context = ctxt;
025  }
026
027  protected String fixVersionforSystem(String url, String csver) {
028    if ("http://snomed.info/sct".equals(url)) {
029      return "http://snomed.info/sct/731000124108/version/" + csver;
030    }
031    if ("http://loinc.org".equals(url)) {
032      return csver;
033    }
034    if ("http://www.nlm.nih.gov/research/umls/rxnorm".equals(url)) {
035      if (csver.length() == 8) {
036        return csver.substring(4, 6) + csver.substring(6, 8) + csver.substring(0, 4);
037      } else {
038        return csver;
039      }
040
041    }
042    return csver;
043  }
044
045  protected ConceptSetComponent getInclude(ValueSet vs, String url, String csver) {
046    for (ConceptSetComponent t : vs.getCompose().getInclude()) {
047      if (csver == null) {
048        if (t.getSystem().equals(url) && !t.hasVersion()) {
049          return t;
050        }
051      } else {
052        if (t.getSystem().equals(url) && t.hasVersion() && t.getVersion().equals(csver)) {
053          return t;
054        }
055      }
056    }
057    ConceptSetComponent c = vs.getCompose().addInclude();
058    c.setSystem(url);
059    c.setVersion(csver);
060    return c;
061  }
062}