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