Skip to content

Commit

Permalink
Closes #120
Browse files Browse the repository at this point in the history
Fake data generation now supports five delimited formats
  • Loading branch information
Maxim Moinat committed Dec 22, 2016
1 parent 406ac05 commit 6ba29a6
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
6 changes: 5 additions & 1 deletion src/org/ohdsi/utilities/files/WriteCSVFileWithHeader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
2 changes: 2 additions & 0 deletions src/org/ohdsi/whiteRabbit/DbSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -39,4 +40,5 @@ public class DbSettings {

// CSV file settings
public char delimiter = ',';
public CSVFormat csvFormat = CSVFormat.RFC4180;
}
42 changes: 29 additions & 13 deletions src/org/ohdsi/whiteRabbit/WhiteRabbitMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -103,6 +104,7 @@ public class WhiteRabbitMain implements ActionListener {
private JTextField targetDatabaseField;
private JTextField sourceDelimiterField;
private JTextField targetDelimiterField;
private JComboBox<String> targetCSVFormat;
private JTextField sourceServerField;
private JTextField sourceUserField;
private JTextField sourcePasswordField;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 6ba29a6

Please sign in to comment.