custom column names for separator based importer

This commit is contained in:
xseris 2018-09-12 15:45:52 +02:00
parent 5a0304f363
commit deab8ceeb0
3 changed files with 51 additions and 0 deletions

View File

@ -101,6 +101,28 @@ public class SeparatorBasedImporter extends TabularImportingParserBase {
boolean processQuotes = JSONUtilities.getBoolean(options, "processQuotes", true);
boolean strictQuotes = JSONUtilities.getBoolean(options, "strictQuotes", false);
List<Object> retrievedColumnNames = null;
if (options.has("columnNames")) {
String[] strings = JSONUtilities.getStringArray(options, "columnNames");
if (strings.length > 0) {
retrievedColumnNames = new ArrayList<Object>();
for (String s : strings) {
s = s.trim();
if (!s.isEmpty()) {
retrievedColumnNames.add(s);
}
}
if (retrievedColumnNames.size() > 0) {
JSONUtilities.safePut(options, "headerLines", 1);
} else {
retrievedColumnNames = null;
}
}
}
final List<Object> columnNames = retrievedColumnNames;
Character quote = CSVParser.DEFAULT_QUOTE_CHARACTER;
String quoteCharacter = JSONUtilities.getString(options, "quoteCharacter", null);
if (quoteCharacter != null && quoteCharacter.trim().length() == 1) {
@ -118,8 +140,13 @@ public class SeparatorBasedImporter extends TabularImportingParserBase {
final LineNumberReader lnReader = new LineNumberReader(reader);
TableDataReader dataReader = new TableDataReader() {
boolean usedColumnNames = false;
@Override
public List<Object> getNextRowOfCells() throws IOException {
if (columnNames != null && !usedColumnNames) {
usedColumnNames = true;
return columnNames;
} else {
String line = lnReader.readLine();
if (line == null) {
return null;
@ -127,6 +154,7 @@ public class SeparatorBasedImporter extends TabularImportingParserBase {
return getCells(line, parser, lnReader);
}
}
}
};
TabularImportingParserBase.readTable(project, metadata, job, dataReader, fileSource, limit, options, exceptions);

View File

@ -24,6 +24,8 @@
<td><label for="$column-separator-custom" id="or-import-custom"></label>
<input bind="columnSeparatorInput" type="text" class="lightweight" size="5" /></td></tr>
<tr><td colspan="2" id="or-import-escape"></td></tr>
<tr><td colspan="2" id="or-import-columnNames"></td></tr>
<tr><td><input style="width: 33em;" bind="columnNamesInput" /></td><td id="or-import-optional"></td></tr>
</table></div></td>
<td colspan="2"><div class="grid-layout layout-tightest"><table>

View File

@ -118,6 +118,11 @@ Refine.SeparatorBasedParserUI.prototype.getOptions = function() {
options.storeBlankCellsAsNulls = this._optionContainerElmts.storeBlankCellsAsNullsCheckbox[0].checked;
options.includeFileSources = this._optionContainerElmts.includeFileSourcesCheckbox[0].checked;
var columnNames = this._optionContainerElmts.columnNamesInput.val();
if (columnNames != undefined && columnNames != null && columnNames != '') {
options.columnNames = columnNames.split(",");
}
return options;
};
@ -136,6 +141,10 @@ Refine.SeparatorBasedParserUI.prototype._initialize = function() {
$('#or-import-tabs').html($.i18n._('core-index-parser')["tabs"]);
$('#or-import-custom').html($.i18n._('core-index-parser')["custom"]);
$('#or-import-escape').html($.i18n._('core-index-parser')["escape"]);
$('#or-import-columnNames').html($.i18n._('core-index-parser')["column-names-label"] + ':');
$('#or-import-optional').html($.i18n._('core-index-parser')["column-names-optional"]);
self._optionContainerElmts.columnNamesInput.prop('disabled', true);
$('#or-import-ignore').text($.i18n._('core-index-parser')["ignore-first"]);
$('#or-import-lines').text($.i18n._('core-index-parser')["lines-beg"]);
@ -160,6 +169,18 @@ Refine.SeparatorBasedParserUI.prototype._initialize = function() {
});
});
this._optionContainerElmts.headerLinesCheckbox.on("click", function() {
if ($(this).is(':checked')) {
var isDisabled = $('textbox').prop('disabled');
if (!isDisabled) {
self._optionContainerElmts.columnNamesInput.prop('disabled', true);
self._optionContainerElmts.columnNamesInput.val('');
}
} else {
self._optionContainerElmts.columnNamesInput.prop('disabled', false);
}
});
var columnSeparatorValue = (this._config.separator == ",") ? 'comma' :
((this._config.separator == "\\t") ? 'tab' : 'custom');
this._optionContainer.find(