001package org.hl7.fhir.convertors.misc;
002
003import org.hl7.fhir.exceptions.FHIRException;
004import org.hl7.fhir.r5.formats.JsonParser;
005import org.hl7.fhir.r5.model.Enumerations.PublicationStatus;
006import org.hl7.fhir.r5.model.ValueSet;
007import org.hl7.fhir.r5.model.ValueSet.ConceptSetComponent;
008import org.hl7.fhir.utilities.CSVReader;
009import org.hl7.fhir.utilities.TextFile;
010import org.hl7.fhir.utilities.Utilities;
011
012import java.io.ByteArrayInputStream;
013import java.io.File;
014import java.io.FileOutputStream;
015import java.io.IOException;
016import java.text.ParseException;
017import java.text.SimpleDateFormat;
018import java.util.List;
019
020public class PhinVadsImporter extends OIDBasedValueSetImporter {
021
022  public PhinVadsImporter() throws FHIRException, IOException {
023    super();
024    init();
025  }
026
027  public static void main(String[] args) throws FHIRException, IOException, ParseException {
028//    new PhinVadsImporter().importValueSet(TextFile.fileToBytes("C:\\work\\org.hl7.fhir\\packages\\us.cdc.phinvads-source\\source\\PHVS_BirthDefectsLateralityatDiagnosis_HL7_V1.txt"));
029    PhinVadsImporter self = new PhinVadsImporter();
030    self.process(args[0], args[1]);
031  }
032
033  private void process(String source, String dest) {
034    for (File f : new File(source).listFiles()) {
035      try {
036        System.out.println("Process " + f.getName());
037        ValueSet vs = importValueSet(TextFile.fileToBytes(f));
038        if (vs.getId() != null) {
039          new JsonParser().compose(new FileOutputStream(Utilities.path(dest, "ValueSet-" + vs.getId() + ".json")), vs);
040        }
041      } catch (Exception e) {
042        e.printStackTrace();
043      }
044    }
045  }
046
047  private ValueSet importValueSet(byte[] source) throws FHIRException, IOException, ParseException {
048    // first thing do is split into 2 
049    List<byte[]> parts = Utilities.splitBytes(source, "\r\n\r\n".getBytes());
050    if (parts.size() < 2) {
051      TextFile.bytesToFile(source, "c:\\temp\\phinvads.txt");
052      throw new FHIRException("Unable to parse phinvads value set: " + parts.size() + " parts found");
053    }
054    CSVReader rdr = new CSVReader(new ByteArrayInputStream(parts.get(0)));
055    rdr.setDelimiter('\t');
056    rdr.setMultiline(true);
057    rdr.readHeaders();
058    rdr.line();
059
060    ValueSet vs = new ValueSet();
061    vs.setId(rdr.cell("Value Set OID"));
062    vs.setUrl("http://phinvads.cdc.gov/fhir/ValueSet/" + vs.getId());
063    vs.getMeta().setSource("https://phinvads.cdc.gov/vads/ViewValueSet.action?oid=" + vs.getId());
064    vs.setVersion(rdr.cell("Value Set Version"));
065    vs.setTitle(rdr.cell("Value Set Name"));
066    vs.setName(rdr.cell("Value Set Code"));
067    vs.setDescription(rdr.cell("Value Set Definition"));
068    if ("Published".equals(rdr.cell("Value Set Status"))) {
069      vs.setStatus(PublicationStatus.ACTIVE);
070    } else {
071      vs.setStatus(PublicationStatus.DRAFT);
072    }
073    if (rdr.has("VS Last Updated Date")) {
074      vs.setDate(new SimpleDateFormat("mm/dd/yyyy").parse(rdr.cell("VS Last Updated Date")));
075    }
076
077    rdr = new CSVReader(new ByteArrayInputStream(parts.get(parts.size() - 1)));
078    rdr.setMultiline(true);
079    rdr.setDelimiter('\t');
080    rdr.readHeaders();
081    while (rdr.line()) {
082      String code = rdr.cell("Concept Code");
083      String display = rdr.cell("Preferred Concept Name");
084      String csoid = rdr.cell("Code System OID");
085      String csver = rdr.cell("Code System Version");
086      String url = context.oid2Uri(csoid);
087      if (url == null) {
088        url = "urn:oid:" + csoid;
089      }
090      csver = fixVersionforSystem(url, csver);
091      ConceptSetComponent inc = getInclude(vs, url, csver);
092      inc.addConcept().setCode(code).setDisplay(display);
093    }
094    return vs;
095  }
096
097
098}