Skip to content

Commit

Permalink
Merge pull request #119 from forcedotcom/CsvReaderFix
Browse files Browse the repository at this point in the history
Use CSV reader with different setting for system csv from customer csv
  • Loading branch information
xbiansf authored Sep 17, 2016
2 parents 0890bb6 + adaff7d commit 88e3d43
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 16 deletions.
32 changes: 23 additions & 9 deletions src/main/java/com/salesforce/dataloader/dao/csv/CSVFileReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,27 +68,41 @@ public class CSVFileReader implements DataReader {
private char[] csvDelimiters;

public CSVFileReader(Config config) {
this(new File(config.getString(Config.DAO_NAME)), config);
this(new File(config.getString(Config.DAO_NAME)), config, true);
}

public CSVFileReader(String filePath, Controller controller) {
this(new File(filePath), controller.getConfig());
this(new File(filePath), controller.getConfig(), false);
}

public CSVFileReader(String filePath, Controller controller, boolean custDelimiter) {
this(new File(filePath), controller.getConfig(), custDelimiter);
}

// Used only by the test
public CSVFileReader(File file, Config config) {
this(file, config, true);
}

public CSVFileReader(File file, Config config, boolean custDelimiter) {
this.file = file;
forceUTF8 = config.getBoolean(Config.READ_UTF8);
StringBuilder separator = new StringBuilder();
if (config.getBoolean(Config.CSV_DELIMETER_COMMA)) {
if (custDelimiter) {
if (config.getBoolean(Config.CSV_DELIMETER_COMMA)) {
separator.append(",");
}
if (config.getBoolean(Config.CSV_DELIMETER_TAB)) {
separator.append("\t");
}
if (config.getBoolean(Config.CSV_DELIMETER_OTHER)) {
separator.append(config.getString(Config.CSV_DELIMETER_OTHER_VALUE));
}
} else {
separator.append(",");
}
if (config.getBoolean(Config.CSV_DELIMETER_TAB)) {
separator.append("\t");
}
if (config.getBoolean(Config.CSV_DELIMETER_OTHER)) {
separator.append(config.getString(Config.CSV_DELIMETER_OTHER_VALUE));
}
csvDelimiters = separator.toString().toCharArray();

if (csvDelimiters.length == 0) {
String errorMsg = "No csv separator present! You need at least one separator character!";
LOGGER.error(errorMsg);
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/salesforce/dataloader/ui/CSVChooserDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public void widgetSelected(SelectionEvent event) {
FileDialog dlg = new FileDialog(shell, SWT.OPEN);
String fn = dlg.open();
if (fn != null) {
openViewer(fn);
openViewer(fn, true);
}

}
Expand All @@ -257,7 +257,7 @@ public void widgetSelected(SelectionEvent event) {
public void widgetSelected(SelectionEvent event) {
String successFilePath = controller.getConfig().getString(Config.OUTPUT_SUCCESS);
if(StringUtils.hasText(successFilePath)) {
openViewer(successFilePath);
openViewer(successFilePath, false);
} else {
UIUtils.infoMessageBox(shell, Messages.getString("CSVChooser.noSucessOrErrorFile"));
}
Expand All @@ -276,7 +276,7 @@ public void widgetSelected(SelectionEvent event) {
public void widgetSelected(SelectionEvent event) {
String errorFilePath = controller.getConfig().getString(Config.OUTPUT_ERROR);
if(StringUtils.hasText(errorFilePath)) {
openViewer(errorFilePath);
openViewer(errorFilePath, false);
} else {
UIUtils.infoMessageBox(shell, Messages.getString("CSVChooser.noSucessOrErrorFile"));
}
Expand Down Expand Up @@ -315,17 +315,17 @@ public void widgetSelected(SelectionEvent event) {
ok.setLayoutData(data);



// Set the OK button as the default, so
// user can type input and press Enter
// to dismiss
shell.setDefaultButton(ok);
}

private void openViewer(String filename) {
private void openViewer(String filename, boolean useCustomSplitter) {
CSVViewerDialog dlg = new CSVViewerDialog(getParent(), controller);
dlg.setNumberOfRows(Integer.parseInt(textRows.getText()));
dlg.setFileName(filename);
dlg.setUseCustomSplitter(useCustomSplitter);
try {
dlg.open();
} catch (DataAccessObjectInitializationException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,18 @@ public class CSVViewerDialog extends Dialog {

private Logger logger = Logger.getLogger(CSVViewerDialog.class);
private String filename;
private boolean useCustomSplitter = false;
private int numberOfRows;

//the two tableViewers
private TableViewer csvTblViewer;
private final Controller controller;

public void setUseCustomSplitter(boolean useCustomSplitter)
{
this.useCustomSplitter = useCustomSplitter;
}

public void setFileName(String filename) {
this.filename = filename;
}
Expand Down Expand Up @@ -243,7 +249,7 @@ public void widgetSelected(SelectionEvent event) {
private void initializeCSVViewer(Shell shell) throws DataAccessObjectInitializationException {
GridData data;

CSVFileReader csvReader = new CSVFileReader(filename, controller);
CSVFileReader csvReader = new CSVFileReader(filename, controller, useCustomSplitter);

try {
csvReader.open();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ private void openViewer(String filename) {
CSVViewerDialog dlg = new CSVViewerDialog(getParent(), controller);
dlg.setNumberOfRows(200000);
dlg.setFileName(filename);
dlg.setUseCustomSplitter(false);
try {
dlg.open();
} catch (DataAccessObjectInitializationException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ public void widgetSelected(SelectionEvent event) {

private void openViewer(String filename) {
CSVViewerDialog dlg = new CSVViewerDialog(getParent(), controller);
dlg.setUseCustomSplitter(false);
dlg.setNumberOfRows(200000);
dlg.setFileName(filename);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ private String createTask(String fieldToNullName, boolean isDateField) throws Ex
}

private String getCsvFieldValue(String csvFile, String fieldName) throws Exception {
CSVFileReader reader = new CSVFileReader(csvFile, getController());
CSVFileReader reader = new CSVFileReader(csvFile, getController(), false);
reader.open();
assertEquals(1, reader.getTotalRows());
String fieldValue = (String)reader.readRow().get(fieldName);
Expand Down

0 comments on commit 88e3d43

Please sign in to comment.