use of org.talend.designer.filemultischemas.data.CSVArrayAndSeparator in project tdi-studio-se by Talend.
the class MultiSchemasUI method fetchCodes.
@SuppressWarnings("restriction")
private void fetchCodes() {
try {
final ProgressMonitorDialog dialog = new ProgressMonitorDialog(getShell());
dialog.run(true, false, new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
monitor.beginTask("Fetch...", IProgressMonitor.UNKNOWN);
monitor.setCanceled(false);
final CsvArray csvArray = processor.getCsvArray();
Display.getDefault().syncExec(new Runnable() {
public void run() {
SchemasKeyData schemasModel = null;
boolean checked = (csvArray != null && csvArray.getRows().size() > 0);
CSVArrayAndSeparator uniqueCsvArray = null;
if (useMultiSaparators.getSelection()) {
getMultiSchemaManager().setKeyValues(keyValuesText.getText());
}
if (multiSchemasFilePreview.getSelectColumnIndex() < 0 && multiSchemaManager.getSelectedColumnIndex() != 0) {
uniqueCsvArray = getMultiSchemaManager().retrieveCsvArrayInUniqueModel(getProcessDescription(), checked, multiSchemaManager.getSelectedColumnIndex(), useMultiSaparators.getSelection());
schemasModel = getMultiSchemaManager().createSchemasTree(uniqueCsvArray, multiSchemaManager.getSelectedColumnIndex());
} else {
uniqueCsvArray = getMultiSchemaManager().retrieveCsvArrayInUniqueModel(getProcessDescription(), checked, multiSchemasFilePreview.getSelectColumnIndex(), useMultiSaparators.getSelection());
schemasModel = getMultiSchemaManager().createSchemasTree(uniqueCsvArray, multiSchemasFilePreview.getSelectColumnIndex());
getMultiSchemaManager().setSelectedColumnIndex(multiSchemasFilePreview.getSelectColumnIndex());
schemaTreeViewer.setInput(schemasModel);
getUIManager().packSchemaTreeFirstColumn(schemaTreeViewer);
clearSchemaDetail();
checkDialog();
}
}
});
monitor.done();
}
});
} catch (InvocationTargetException e) {
ExceptionHandler.process(e);
} catch (InterruptedException e) {
ExceptionHandler.process(e);
}
}
use of org.talend.designer.filemultischemas.data.CSVArrayAndSeparator in project tdi-studio-se by Talend.
the class MultiSchemasManager method retrieveCsvArrayByCSVOption.
public CSVArrayAndSeparator retrieveCsvArrayByCSVOption(final String filePath, final String encoding, final String fieldSeparator, final String rowSeparator, final boolean needSkpipEmptyRecord, final boolean splitRecord, int selectColumnIndex) {
CSVArrayAndSeparator csvArrayBean = new CSVArrayAndSeparator();
CSVReader multiSchameCsvReader = null;
try {
// read max columns
multiSchameCsvReader = getCSVReader(filePath, encoding, fieldSeparator, rowSeparator, needSkpipEmptyRecord);
List items = multiSchameCsvReader.parse(new File(filePath));
int columsNum = -1;
for (Object item : items) {
String[] values = (String[]) item;
if (columsNum < values.length) {
columsNum = values.length;
}
}
// multiSchameCsvReader.close();
if (columsNum < 0) {
return null;
}
// read data
Set<String> uniqueKey = new HashSet<String>();
//$NON-NLS-1$
String currentRowKey = "";
multiSchameCsvReader = getCSVReader(filePath, encoding, fieldSeparator, rowSeparator, needSkpipEmptyRecord);
items = multiSchameCsvReader.parse(new File(filePath));
for (Object item : items) {
String[] values = (String[]) item;
if (values == null || values.length < 1 || values.length <= selectColumnIndex) {
continue;
}
final String first = values[selectColumnIndex];
if ("".equals(first.trim())) {
// must be contain first //$NON-NLS-1$
continue;
}
currentRowKey = first;
if (uniqueKey.contains(currentRowKey)) {
// existed.
continue;
}
uniqueKey.add(currentRowKey);
csvArrayBean.getCsvArray().add(values);
csvArrayBean.getSeparators().add(fieldSeparator);
}
// while (multiSchameCsvReader.readRecord()) {
// String[] values = multiSchameCsvReader.getValues();
// if (values == null || values.length < 1 || values.length <= selectColumnIndex) {
// continue;
// }
// final String first = values[selectColumnIndex];
// if ("".equals(first.trim())) { // must be contain first //$NON-NLS-1$
// continue;
// }
// currentRowKey = first;
// if (uniqueKey.contains(currentRowKey)) { // existed.
// continue;
// }
// uniqueKey.add(currentRowKey);
// csvArrayBean.getCsvArray().add(values);
// csvArrayBean.getSeparators().add(fieldSeparator);
// }
} catch (IOException e) {
ExceptionHandler.process(e);
return null;
} finally {
// if (multiSchameCsvReader != null) {
// multiSchameCsvReader.close();
// }
}
// return handlerCSVArray(csvArray, selectColumnIndex, multiSchameCsvReader);
return csvArrayBean;
}
use of org.talend.designer.filemultischemas.data.CSVArrayAndSeparator in project tdi-studio-se by Talend.
the class MultiSchemasManager method retrieveCsvArrayByDelimited.
/**
* cli Comment method "retrieveCsvArrayByDelimited".
*/
private CSVArrayAndSeparator retrieveCsvArrayByDelimited(final String filePath, final String encoding, final String fieldSeparator, final String rowSeparator, final boolean needSkpipEmptyRecord, final boolean splitRecord, int selectColumnIndex) {
CSVArrayAndSeparator csvArrayBean = new CSVArrayAndSeparator();
FileInputDelimited fileInputDelimited = null;
try {
fileInputDelimited = new FileInputDelimited(filePath, encoding, fieldSeparator, rowSeparator, needSkpipEmptyRecord, 0, 0, -1, -1, splitRecord);
final int maxColumnCount = FileInputDelimited.getMaxColumnCount(filePath, encoding, fieldSeparator, rowSeparator, needSkpipEmptyRecord, splitRecord, 0, Integer.MAX_VALUE);
if (maxColumnCount < 1) {
return null;
}
Set<String> uniqueKey = new HashSet<String>();
//$NON-NLS-1$
String currentRowKey = "";
while (fileInputDelimited.nextRecord()) {
final String first = fileInputDelimited.get(selectColumnIndex);
if ("".equals(first.trim())) {
// must be contain first //$NON-NLS-1$
continue;
}
currentRowKey = first;
if (uniqueKey.contains(currentRowKey)) {
// existed.
continue;
}
uniqueKey.add(currentRowKey);
handlerDelimitedArray(csvArrayBean, maxColumnCount, first, fileInputDelimited, fieldSeparator);
}
} catch (IOException e) {
ExceptionHandler.process(e);
return null;
} finally {
if (fileInputDelimited != null) {
fileInputDelimited.close();
}
}
return csvArrayBean;
}
use of org.talend.designer.filemultischemas.data.CSVArrayAndSeparator in project tdi-studio-se by Talend.
the class MultiSchemasManager method getCsvArrayForMs.
private CSVArrayAndSeparator getCsvArrayForMs(String filePath, String separators, String encoding, int selectColumnIndex) throws UnsupportedEncodingException, IOException {
File file = new File(TalendTextUtils.removeQuotes(filePath));
separators = TalendTextUtils.removeQuotes(separators);
CSVArrayAndSeparator csvArrayBean = new CSVArrayAndSeparator();
Set<String> uniqueKey = new HashSet<String>();
if (file.exists()) {
BufferedReader reader = new BufferedReader(new FileReader(file));
String readLine = "";
String[] row = null;
while ((readLine = reader.readLine()) != null) {
int count = 0;
boolean added = false;
int sepIndex = 0;
while (count < separators.length()) {
CSVReader csvReader = getCsvReader(new ByteArrayInputStream(readLine.getBytes()), separators.charAt(count), encoding);
List items = csvReader.parse(readLine);
if (items.size() > 0) {
// csvReader.getValues();
row = (String[]) items.get(0);
}
if (row.length > 1 && row.length > selectColumnIndex && isInKeyValues(getKeyValues(), row[selectColumnIndex])) {
// this kind of record will not added in the if that deal with rows that only have one column
if (uniqueKey.contains(row[selectColumnIndex] + String.valueOf(separators.charAt(count)))) {
sepIndex = count;
}
if (!uniqueKey.contains(row[selectColumnIndex] + String.valueOf(separators.charAt(count)))) {
uniqueKey.add(row[selectColumnIndex] + String.valueOf(separators.charAt(count)));
csvArrayBean.getCsvArray().add(row);
csvArrayBean.getSeparators().add(String.valueOf(separators.charAt(count)));
// csvReader.close();
added = true;
break;
}
}
// csvReader.close();
count++;
}
// separator
if (!added && selectColumnIndex < row.length && isInKeyValues(getKeyValues(), row[selectColumnIndex]) && !uniqueKey.contains(row[selectColumnIndex] + String.valueOf(separators.charAt(sepIndex)))) {
uniqueKey.add(row[selectColumnIndex] + String.valueOf(separators.charAt(sepIndex)));
csvArrayBean.getCsvArray().add(row);
csvArrayBean.getSeparators().add(String.valueOf(separators.charAt(sepIndex)));
}
}
}
return csvArrayBean;
}
Aggregations