diff --git a/src/org/ohdsi/utilities/files/WriteCSVFileWithHeader.java b/src/org/ohdsi/utilities/files/WriteCSVFileWithHeader.java index c5ef85b7..dd1ea499 100644 --- a/src/org/ohdsi/utilities/files/WriteCSVFileWithHeader.java +++ b/src/org/ohdsi/utilities/files/WriteCSVFileWithHeader.java @@ -15,8 +15,12 @@ public class WriteCSVFileWithHeader { private boolean headerWritten = false; public WriteCSVFileWithHeader(String fileName) { + this(fileName, CSVFormat.RFC4180); + } + + public WriteCSVFileWithHeader(String fileName, CSVFormat format) { try { - printer = new CSVPrinter(new FileWriter(fileName), CSVFormat.RFC4180); + printer = new CSVPrinter(new FileWriter(fileName), format); } catch (IOException e) { throw new RuntimeException(e.getMessage()); } diff --git a/src/org/ohdsi/whiteRabbit/DbSettings.java b/src/org/ohdsi/whiteRabbit/DbSettings.java index 0b8a9f00..31784dec 100644 --- a/src/org/ohdsi/whiteRabbit/DbSettings.java +++ b/src/org/ohdsi/whiteRabbit/DbSettings.java @@ -20,6 +20,7 @@ import java.util.ArrayList; import java.util.List; +import org.apache.commons.csv.CSVFormat; import org.ohdsi.databases.DbType; public class DbSettings { @@ -39,4 +40,5 @@ public class DbSettings { // CSV file settings public char delimiter = ','; + public CSVFormat csvFormat = CSVFormat.RFC4180; } diff --git a/src/org/ohdsi/whiteRabbit/WhiteRabbitMain.java b/src/org/ohdsi/whiteRabbit/WhiteRabbitMain.java index 7b347d24..18dbafb8 100644 --- a/src/org/ohdsi/whiteRabbit/WhiteRabbitMain.java +++ b/src/org/ohdsi/whiteRabbit/WhiteRabbitMain.java @@ -70,6 +70,7 @@ import javax.swing.event.ChangeListener; import javax.swing.filechooser.FileNameExtensionFilter; +import org.apache.commons.csv.CSVFormat; import org.ohdsi.databases.DbType; import org.ohdsi.databases.RichConnection; import org.ohdsi.utilities.DirectoryUtilities; @@ -103,6 +104,7 @@ public class WhiteRabbitMain implements ActionListener { private JTextField targetDatabaseField; private JTextField sourceDelimiterField; private JTextField targetDelimiterField; + private JComboBox targetCSVFormat; private JTextField sourceServerField; private JTextField sourceUserField; private JTextField sourcePasswordField; @@ -492,7 +494,7 @@ public void itemStateChanged(ItemEvent arg0) { targetUserField.setEnabled(!targetIsFiles); targetPasswordField.setEnabled(!targetIsFiles); targetDatabaseField.setEnabled(!targetIsFiles); - targetDelimiterField.setEnabled(targetIsFiles); + targetCSVFormat.setEnabled(targetIsFiles); if (!targetIsFiles && arg0.getItem().toString().equals("Oracle")) { targetServerField @@ -537,11 +539,12 @@ public void itemStateChanged(ItemEvent arg0) { targetDatabaseField.setEnabled(false); targetPanel.add(targetDatabaseField); - targetPanel.add(new JLabel("Delimiter")); - targetDelimiterField = new JTextField(","); - targetDelimiterField.setToolTipText("The delimiter that separates values. Enter 'tab' for tab."); - targetDelimiterField.setEnabled(true); - targetPanel.add(targetDelimiterField); + targetPanel.add(new JLabel("CSV Format")); + targetCSVFormat = new JComboBox<>( + new String[] { "Default (comma, CRLF)", "TDF (tab, CRLF)", "MySQL (tab, LF)", "RFC4180", "Excel CSV" }); + targetCSVFormat.setToolTipText("The format of the output"); + targetCSVFormat.setEnabled(true); + targetPanel.add(targetCSVFormat); c.gridx = 0; c.gridy = 1; @@ -799,14 +802,27 @@ private DbSettings getTargetDbSettings() { DbSettings dbSettings = new DbSettings(); if (targetType.getSelectedItem().equals("Delimited text files")) { dbSettings.dataType = DbSettings.CSVFILES; - if (targetDelimiterField.getText().length() == 0) { - JOptionPane.showMessageDialog(frame, "Delimiter field cannot be empty for target files", "Error", JOptionPane.ERROR_MESSAGE); - return null; + + switch((String) targetCSVFormat.getSelectedItem()) { + case "Default (comma, CRLF)": + dbSettings.csvFormat = CSVFormat.DEFAULT; + break; + case "RFC4180": + dbSettings.csvFormat = CSVFormat.RFC4180; + break; + case "Excel CSV": + dbSettings.csvFormat = CSVFormat.EXCEL; + break; + case "TDF (tab, CRLF)": + dbSettings.csvFormat = CSVFormat.TDF; + break; + case "MySQL (tab, LF)": + dbSettings.csvFormat = CSVFormat.MYSQL; + break; + default: + dbSettings.csvFormat = CSVFormat.RFC4180; } - if (targetDelimiterField.getText().toLowerCase().equals("tab")) - dbSettings.delimiter = '\t'; - else - dbSettings.delimiter = targetDelimiterField.getText().charAt(0); + } else { dbSettings.dataType = DbSettings.DATABASE; dbSettings.user = targetUserField.getText(); diff --git a/src/org/ohdsi/whiteRabbit/fakeDataGenerator/FakeDataGenerator.java b/src/org/ohdsi/whiteRabbit/fakeDataGenerator/FakeDataGenerator.java index 7b3149b6..d96ccfab 100644 --- a/src/org/ohdsi/whiteRabbit/fakeDataGenerator/FakeDataGenerator.java +++ b/src/org/ohdsi/whiteRabbit/fakeDataGenerator/FakeDataGenerator.java @@ -88,7 +88,7 @@ public void generateData(DbSettings dbSettings, int maxRowsPerTable, String file if (!name.toLowerCase().endsWith(".csv")) name = name + ".csv"; System.out.println("Generating table " + name); - WriteCSVFileWithHeader out = new WriteCSVFileWithHeader(name); + WriteCSVFileWithHeader out = new WriteCSVFileWithHeader(name, dbSettings.csvFormat); for (Row row : generateRows(table)) out.write(row); out.close();