Search in sources :

Example 6 with CsvArray

use of org.talend.core.utils.CsvArray in project tdi-studio-se by Talend.

the class ShadowProcess method runWithErrorOutputAsException.

/**
     * 
     * DOC xye Comment method "runWithErrorOutputAsException".
     * 
     * @param outputErrorAsException
     * @return
     * @throws ProcessorException
     */
public CsvArray runWithErrorOutputAsException(final boolean outputErrorAsException) throws ProcessorException {
    IProcess talendProcess = buildProcess();
    IProcessor processor = ProcessorUtilities.getProcessor(talendProcess, null);
    processor.setProxyParameters(getProxyParameters());
    File previousFile = outPath.toFile();
    if (previousFile.exists()) {
        previousFile.delete();
    }
    IContext context = talendProcess.getContextManager().getDefaultContext();
    processor.setContext(context);
    process = processor.run(IProcessor.NO_STATISTICS, IProcessor.NO_TRACES, null);
    String error = ProcessStreamTrashReader.readErrorStream(process);
    if (error != null) {
        log.warn(error, new ProcessorException(error));
    }
    if (!outPath.toFile().exists()) {
        if (outputErrorAsException && error != null) {
            throw new ProcessorException(error);
        } else {
            //$NON-NLS-1$
            throw new ProcessorException(Messages.getString("ShadowProcess.notGeneratedOutputException"));
        }
    }
    try {
        CsvArray array = new CsvArray();
        array = array.createFrom(outPath.toFile(), currentProcessEncoding);
        return array;
    } catch (IOException ioe) {
        throw new ProcessorException(ioe);
    }
}
Also used : IContext(org.talend.core.model.process.IContext) ProcessorException(org.talend.designer.runprocess.ProcessorException) CsvArray(org.talend.core.utils.CsvArray) IProcessor(org.talend.designer.runprocess.IProcessor) IOException(java.io.IOException) IProcess(org.talend.core.model.process.IProcess) File(java.io.File)

Example 7 with CsvArray

use of org.talend.core.utils.CsvArray in project tdi-studio-se by Talend.

the class JSONFileStep2Form method refreshPreview.

/**
     * refreshPreview use ShadowProcess to refresh the preview.
     */
void refreshPreview() {
    clearPreview();
    // if no file, the process don't be executed
    if (getConnection().getJSONFilePath() == null || !(new File(getConnection().getJSONFilePath()).exists()) && JSONUtil.tempJSONXsdPath == null) {
        previewInformationLabel.setText("   " + "The file path must be specified");
        return;
    }
    // if incomplete settings, , the process don't be executed
    if (!checkFieldsValue()) {
        previewInformationLabel.setText("   " + "The settings must be completed to show the preview");
        return;
    }
    // set row limit
    if (jsonXPathLoopDescriptor.getLimitBoucle() > 0 && jsonXPathLoopDescriptor.getLimitBoucle() < XmlArray.getRowLimit()) {
        XmlArray.setRowLimit(jsonXPathLoopDescriptor.getLimitBoucle());
    }
    previewInformationLabel.setText("   " + "Preview in progress...");
    String shadowProcessType = null;
    if (EJsonReadbyMode.JSONPATH.getValue().equals(getConnection().getReadbyMode())) {
        //$NON-NLS-1$
        shadowProcessType = "FILE_JSON";
    }
    final String finalShadowProcessType = shadowProcessType;
    AsynchronousPreviewHandler<CsvArray> previewHandler = null;
    try {
        previewHandler = ShadowProcessHelper.createPreviewHandler(finalShadowProcessType);
    } catch (CoreException e) {
        previewInError(e);
        return;
    }
    StoppablePreviewLoader previewLoader = new StoppablePreviewLoader<CsvArray>(previewHandler, previewInformationLabel) {

        /*
             * (non-Javadoc)
             * 
             * @see
             * org.talend.repository.ui.wizards.metadata.connection.files.JSON.StoppablePreviewLoader#previewEnded(java
             * .lang.Object)
             */
        @Override
        protected void previewEnded(CsvArray result) {
            jsonFilePreview.refreshTablePreview(result, false, (getConnection().getSchema().get(0)).getSchemaTargets());
            if (jsonFilePreview.isCsvRowsEmpty()) {
                clearPreview();
                Display.getDefault().asyncExec(new Runnable() {

                    @Override
                    public void run() {
                        handleErrorOutput(outputComposite, tabFolder, outputTabItem);
                    }
                });
                log.error("No result for this settings");
                previewButton.setEnabled(true);
            }
        }

        @Override
        public void previewInError(CoreException e) {
            JSONFileStep2Form.this.previewInError(e);
        }

        @Override
        protected String getShadowProcessType() {
            if (finalShadowProcessType != null && !finalShadowProcessType.isEmpty()) {
                return finalShadowProcessType;
            } else {
                return super.getShadowProcessType();
            }
        }
    };
    previewLoader.load(getProcessDescription(false));
}
Also used : CsvArray(org.talend.core.utils.CsvArray) CoreException(org.eclipse.core.runtime.CoreException) StoppablePreviewLoader(org.talend.metadata.managment.ui.preview.StoppablePreviewLoader) File(java.io.File)

Example 8 with CsvArray

use of org.talend.core.utils.CsvArray in project tdi-studio-se by Talend.

the class MultiSchemasUI method launch.

private void launch() {
    try {
        String filePath = this.multiSchemaManager.getOriginalValue(this.getConnection().getFilePath());
        CsvArray csvArray = getMultiSchemaManager().getCsvArrayForMultiSchemaDelimited(filePath, multiSeparatorsText.getText(), encodingCombo.getText(), keyValuesText.getText(), multiSchemaManager.getSelectedColumnIndex());
        processor.setCsvArray(csvArray);
        Display.getDefault().asyncExec(new Runnable() {

            public void run() {
                processor.execute2();
            }
        });
    } catch (FileNotFoundException e) {
        ExceptionHandler.process(e);
    } catch (IOException e) {
        ExceptionHandler.process(e);
    }
}
Also used : CsvArray(org.talend.core.utils.CsvArray) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Example 9 with CsvArray

use of org.talend.core.utils.CsvArray 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 10 with CsvArray

use of org.talend.core.utils.CsvArray in project tdi-studio-se by Talend.

the class MultiSchemasManager method getCsvArrayForMultiSchemaDelimited.

public CsvArray getCsvArrayForMultiSchemaDelimited(String filePath, String separators, String encoding, String keyValues, int keyIndex) throws UnsupportedEncodingException, IOException {
    File file = new File(TalendTextUtils.removeQuotes(filePath));
    separators = TalendTextUtils.removeQuotes(separators);
    CsvArray csvArray = new CsvArray();
    if (file.exists()) {
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String readLine = "";
        String[] row = null;
        int i = 0;
        while ((readLine = reader.readLine()) != null && i < maximumRowsToPreview) {
            boolean added = false;
            int count = 0;
            while (count < separators.length()) {
                CSVReader csvReader = getCsvReader(new ByteArrayInputStream(readLine.getBytes()), separators.charAt(count), encoding);
                List items = csvReader.parse(readLine);
                // csvReader.readRecord();
                if (items.size() > 0) {
                    // csvReader.getValues();
                    row = (String[]) items.get(0);
                }
                if (row.length > 1 && row.length > keyIndex) {
                    if (isInKeyValues(keyValues, row[keyIndex])) {
                        added = true;
                        csvArray.add(row);
                        // csvReader.close();
                        i++;
                        break;
                    }
                }
                // csvReader.close();
                count++;
            }
            if (!added && row != null && row.length > keyIndex) {
                if (isInKeyValues(keyValues, row[keyIndex])) {
                    csvArray.add(row);
                    i++;
                }
            }
        }
    }
    return csvArray;
}
Also used : CsvArray(org.talend.core.utils.CsvArray) CSVReader(org.skife.csv.CSVReader) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) List(java.util.List) ArrayList(java.util.ArrayList) File(java.io.File)

Aggregations

CsvArray (org.talend.core.utils.CsvArray)19 ProcessorException (org.talend.designer.runprocess.ProcessorException)10 File (java.io.File)7 CoreException (org.eclipse.core.runtime.CoreException)7 IOException (java.io.IOException)6 IProcessor (org.talend.designer.runprocess.IProcessor)5 Status (org.eclipse.core.runtime.Status)4 IContext (org.talend.core.model.process.IContext)4 IProcess (org.talend.core.model.process.IProcess)4 IProcessDescription (org.talend.core.repository.model.preview.IProcessDescription)4 ArrayList (java.util.ArrayList)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 IPreview (org.talend.core.repository.model.preview.IPreview)2 EShadowProcessType (org.talend.designer.runprocess.shadow.ShadowProcess.EShadowProcessType)2 EJSONShadowProcessType (org.talend.repository.json.ui.shadow.JSONShadowProcess.EJSONShadowProcessType)2 JSONFileConnection (org.talend.repository.model.json.JSONFileConnection)2 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1