|
| 1 | +/* |
| 2 | + * Copyright (C) 2023 Information Management Services, Inc. |
| 3 | + */ |
| 4 | +package lab; |
| 5 | + |
| 6 | +import java.io.File; |
| 7 | +import java.io.FileReader; |
| 8 | +import java.io.IOException; |
| 9 | + |
| 10 | +import com.opencsv.CSVReader; |
| 11 | +import com.opencsv.exceptions.CsvException; |
| 12 | + |
| 13 | +import com.imsweb.naaccrxml.NaaccrXmlDictionaryUtils; |
| 14 | +import com.imsweb.naaccrxml.entity.dictionary.NaaccrDictionary; |
| 15 | +import com.imsweb.naaccrxml.entity.dictionary.NaaccrDictionaryItem; |
| 16 | + |
| 17 | +public class DictionaryFromCsv { |
| 18 | + |
| 19 | + public static void main(String[] args) throws IOException, CsvException { |
| 20 | + |
| 21 | + // Date Item Introduced (MM/DD/YYYY),Date Item Last Updated (MM/DD/YYYY),Retired Date (MM/DD/YYYY),NAACCR ID (max. 32 characters),Item Name,"Data Item Number",Length,"Parent XML Element","Record Type",Data Type,Description,Codes,Required Status from Reporting Facilities,,, |
| 22 | + |
| 23 | + try (CSVReader reader = new CSVReader(new FileReader("path-to-csv-file"))) { |
| 24 | + |
| 25 | + NaaccrDictionary dictionary = new NaaccrDictionary(); |
| 26 | + dictionary.setDictionaryUri("<URI>"); |
| 27 | + dictionary.setDescription("<DESCRIPTION>"); |
| 28 | + |
| 29 | + for (String[] row : reader.readAll()) { |
| 30 | + String id = row[3]; |
| 31 | + String name = row[4]; |
| 32 | + String num = row[5]; |
| 33 | + String length = row[6]; |
| 34 | + String parent = row[7]; |
| 35 | + String recTypes = row[8]; |
| 36 | + if ("A, C, I, M".equals(recTypes)) |
| 37 | + recTypes = "A,M,C,I"; |
| 38 | + else |
| 39 | + throw new IllegalStateException("Unexpected record types: " + recTypes); |
| 40 | + String type = row[9]; |
| 41 | + |
| 42 | + NaaccrDictionaryItem item = new NaaccrDictionaryItem(); |
| 43 | + item.setNaaccrId(id.trim()); |
| 44 | + item.setNaaccrName(name.trim()); |
| 45 | + item.setNaaccrNum(Integer.valueOf(num)); |
| 46 | + item.setLength(Integer.valueOf(length)); |
| 47 | + item.setParentXmlElement(parent); |
| 48 | + item.setRecordTypes(recTypes); |
| 49 | + item.setDataType(type); |
| 50 | + |
| 51 | + dictionary.addItem(item); |
| 52 | + } |
| 53 | + |
| 54 | + NaaccrXmlDictionaryUtils.writeDictionary(dictionary, new File("<path-to-xml-file>")); |
| 55 | + } |
| 56 | + } |
| 57 | +} |
0 commit comments