Search in sources :

Example 1 with CSVArrayAndSeparator

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);
    }
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CsvArray(org.talend.core.utils.CsvArray) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) CSVArrayAndSeparator(org.talend.designer.filemultischemas.data.CSVArrayAndSeparator) SchemasKeyData(org.talend.designer.filemultischemas.data.SchemasKeyData) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress)

Example 2 with CSVArrayAndSeparator

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;
}
Also used : CSVReader(org.skife.csv.CSVReader) CSVArrayAndSeparator(org.talend.designer.filemultischemas.data.CSVArrayAndSeparator) List(java.util.List) ArrayList(java.util.ArrayList) IOException(java.io.IOException) File(java.io.File) HashSet(java.util.HashSet)

Example 3 with CSVArrayAndSeparator

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;
}
Also used : FileInputDelimited(org.talend.fileprocess.FileInputDelimited) CSVArrayAndSeparator(org.talend.designer.filemultischemas.data.CSVArrayAndSeparator) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 4 with CSVArrayAndSeparator

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;
}
Also used : CSVReader(org.skife.csv.CSVReader) CSVArrayAndSeparator(org.talend.designer.filemultischemas.data.CSVArrayAndSeparator) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) List(java.util.List) ArrayList(java.util.ArrayList) File(java.io.File) HashSet(java.util.HashSet)

Aggregations

CSVArrayAndSeparator (org.talend.designer.filemultischemas.data.CSVArrayAndSeparator)4 HashSet (java.util.HashSet)3 File (java.io.File)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 CSVReader (org.skife.csv.CSVReader)2 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileReader (java.io.FileReader)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 ProgressMonitorDialog (org.eclipse.jface.dialogs.ProgressMonitorDialog)1 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)1 CsvArray (org.talend.core.utils.CsvArray)1 SchemasKeyData (org.talend.designer.filemultischemas.data.SchemasKeyData)1 FileInputDelimited (org.talend.fileprocess.FileInputDelimited)1