Search in sources :

Example 11 with CsvArray

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

the class GuessSchemaProcess method run.

public CsvArray run() throws ProcessorException {
    CsvArray array = new CsvArray();
    buildProcess();
    IProcessor processor = ProcessorUtilities.getProcessor(process, null);
    processor.setContext(selectContext);
    File previousFile = temppath.toFile();
    if (previousFile.exists()) {
        previousFile.delete();
    }
    java.lang.Process executeprocess = processor.run(IProcessor.NO_STATISTICS, IProcessor.NO_TRACES, null);
    StringBuffer buffer = new StringBuffer();
    ProcessStreamTrashReaderUtil.readAndForget(executeprocess, buffer);
    final String errorMessage = buffer.toString();
    boolean checkError = !info.isHive() | !previousFile.exists();
    if (checkError && !"".equals(buffer.toString())) {
        throw new ProcessorException(errorMessage) {

            private static final long serialVersionUID = 1L;

            /*
                 * (non-Javadoc)
                 * 
                 * @see java.lang.Throwable#initCause(java.lang.Throwable)
                 */
            @Override
            public synchronized Throwable initCause(Throwable cause) {
                // TODO Auto-generated method stub
                return super.initCause(cause);
            }

            /*
                 * (non-Javadoc)
                 * 
                 * @see java.lang.Throwable#getMessage()
                 */
            @Override
            public String getMessage() {
                StringTokenizer tokenizer = new StringTokenizer(errorMessage, "\n");
                StringBuilder sqlError = new StringBuilder();
                if (tokenizer.countTokens() > 2) {
                    tokenizer.nextToken();
                    sqlError.append(tokenizer.nextToken()).append("\n");
                }
                return sqlError.toString();
            }
        };
    } else {
        try {
            array = array.createFrom(previousFile, currentProcessEncoding);
        } catch (IOException ioe) {
            throw new ProcessorException(ioe) {

                /*
                     * (non-Javadoc)
                     * 
                     * @see java.lang.Throwable#getMessage()
                     */
                @Override
                public String getMessage() {
                    //$NON-NLS-1$ //$NON-NLS-2$                        
                    return Messages.getString("GuessSchemaController.0", System.getProperty("line.separator"));
                }
            };
        }
    }
    return array;
}
Also used : ProcessorException(org.talend.designer.runprocess.ProcessorException) IProcessor(org.talend.designer.runprocess.IProcessor) IOException(java.io.IOException) StringTokenizer(java.util.StringTokenizer) CsvArray(org.talend.core.utils.CsvArray) File(java.io.File)

Example 12 with CsvArray

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

the class GuessSchemaController method runShadowProcess.

private void runShadowProcess(final Property property, final Node inputNode, final IContext selectContext, final IElementParameter switchParam) {
    DatabaseConnection connt = TracesConnectionUtils.createConnection(connParameters);
    String dbmsId = connt.getDbmsId();
    if (dbmsId == null) {
        Shell shell = Display.getCurrent().getActiveShell();
        MessageDialog.openError(shell, "No info about DB found !", "Please choose the correct mapping file.\n Note: This is normal when using JDBC component");
        MappingFileSelectDialog dialog = new MappingFileSelectDialog(shell);
        dialog.open();
        dbmsId = dialog.getSelectId();
    }
    GuessSchemaProcess gsp = new GuessSchemaProcess(property, inputNode, selectContext, memoSQL, info, part.getProcess());
    try {
        List<Integer> indexsForSameNamedColumn = new ArrayList<Integer>();
        CsvArray array = gsp.run();
        List<String[]> schemaContent = array.getRows();
        List<String> columnLabels = new ArrayList<String>();
        if (columns != null) {
            columns.clear();
        }
        if (!schemaContent.isEmpty()) {
            int numbOfColumn = schemaContent.get(0).length;
            for (int i = 1; i <= numbOfColumn; i++) {
                indexsForSameNamedColumn.clear();
                Boolean b = false;
                IMetadataColumn oneColum = new MetadataColumn();
                // get the column name from the temp file genenrated by GuessSchemaProcess.java
                String labelName = (schemaContent.get(0))[i - 1];
                if (info != null && info.isHive()) {
                    if (labelName != null && labelName.indexOf(TalendTextUtils.JAVA_END_STRING) != -1) {
                        labelName = labelName.substring(labelName.lastIndexOf(TalendTextUtils.JAVA_END_STRING) + 1, labelName.length());
                    }
                }
                String name = labelName;
                String sub = "";
                String sub2 = "";
                if (labelName != null && labelName.length() > 0 && labelName.startsWith("_")) {
                    //$NON-NLS-1$
                    sub = labelName.substring(1);
                    if (sub != null && sub.length() > 0) {
                        sub2 = sub.substring(1);
                    }
                }
                if (KeywordsValidator.isKeyword(labelName) || KeywordsValidator.isKeyword(sub) || KeywordsValidator.isKeyword(sub2)) {
                    labelName = "_" + labelName;
                    b = true;
                }
                // String label = labelName;
                // findSameNamedColumnAndReplaceTheIndex(indexsForSameNamedColumn, i, oneColum, labelName);
                oneColum.setLabel(MetadataToolHelper.validateColumnName(labelName, i, columnLabels));
                //                    if (b && label != null && label.length() > 0 && label.startsWith("_")) { //$NON-NLS-1$
                // String substring = label.substring(1);
                // if (label.startsWith("_")
                // && (KeywordsValidator.isKeyword(substring) || KeywordsValidator.isKeyword(sub) ||
                // KeywordsValidator
                // .isKeyword(sub2))) {
                // label = substring;
                // }
                // }
                oneColum.setOriginalDbColumnName(name);
                if (schemaContent.size() > 5) {
                    oneColum.setPrecision(Integer.parseInt(schemaContent.get(2)[i - 1]));
                    oneColum.setLength(Integer.parseInt(schemaContent.get(3)[i - 1]));
                }
                try {
                    String talendType = null;
                    // to see if the language is java or perl
                    if (LanguageManager.getCurrentLanguage() == ECodeLanguage.JAVA) {
                        if (schemaContent.size() >= 5) {
                            talendType = MetadataTalendType.getMappingTypeRetriever(dbmsId).getDefaultSelectedTalendType(schemaContent.get(4)[i - 1]);
                        } else {
                            talendType = JavaTypesManager.STRING.getId();
                        }
                    } else {
                        if (schemaContent.size() >= 5) {
                            talendType = PerlDataTypeHelper.getNewTalendTypeOfValue(schemaContent.get(4)[i - 1]);
                        } else {
                            talendType = PerlTypesManager.STRING;
                        }
                    }
                    oneColum.setTalendType(talendType);
                    if (dbmsId != null) {
                        if (!TypesManager.checkDBType(dbmsId, oneColum.getTalendType(), oneColum.getType())) {
                            oneColum.setType(TypesManager.getDBTypeFromTalendType(dbmsId, oneColum.getTalendType()));
                        }
                    }
                // oneColum.setTalendType(JavaTypesManager.STRING.getId());
                } catch (Exception e) {
                    /*
                         * the table have no data at all ,to do nothing
                         */
                    ExceptionHandler.process(e);
                }
                // get if a column is nullable from the temp file genenrated by GuessSchemaProcess.java
                oneColum.setNullable((schemaContent.get(1))[i - 1].equals(Boolean.TRUE.toString()) ? true : false);
                columns.add(oneColum);
                columnLabels.add(oneColum.getLabel());
            }
            if (columns.size() > 0) {
                IElementParameter dqRule = elem.getElementParameter("DQRULES_LIST");
                if (dqRule != null) {
                    ITDQRuleService ruleService = null;
                    try {
                        ruleService = (ITDQRuleService) GlobalServiceRegister.getDefault().getService(ITDQRuleService.class);
                    } catch (RuntimeException e) {
                    // nothing to do
                    }
                    IElementParameter queryParam = elem.getElementParameter("QUERY");
                    if (ruleService != null && queryParam != null) {
                        ruleService.updateOriginalColumnNames(columns, queryParam);
                    }
                }
            }
            IMetadataTable tempMetatable = new MetadataTable();
            /* for bug 20973 */
            if (tempMetatable.getTableName() == null) {
                tempMetatable.setTableName(inputNode.getUniqueName());
            }
            IMetadataTable outputMetaCopy, originaleOutputTable;
            String propertyName = "";
            if (!btn.isDisposed()) {
                propertyName = (String) btn.getData(PARAMETER_NAME);
            }
            IElementParameter param = inputNode.getElementParameter(propertyName);
            for (IElementParameter eParam : elem.getElementParameters()) {
                if (eParam.getContext() != null) {
                    param = eParam;
                }
            }
            originaleOutputTable = inputNode.getMetadataFromConnector(param.getContext());
            if (originaleOutputTable != null) {
                outputMetaCopy = originaleOutputTable.clone();
            }
            tempMetatable.setListColumns(columns);
            tempMetatable.setDbms(dbmsId);
            MetadataDialog metaDialog = new MetadataDialog(composite.getShell(), tempMetatable, inputNode, getCommandStack());
            if (metaDialog != null) {
                //$NON-NLS-1$
                metaDialog.setText(Messages.getString("AbstractSchemaController.schema.title", inputNode.getLabel()));
            }
            // ok pressed
            if (metaDialog.open() == MetadataDialog.OK) {
                outputMetaCopy = metaDialog.getOutputMetaData();
                boolean modified = false;
                if (!outputMetaCopy.sameMetadataAs(originaleOutputTable, IMetadataColumn.OPTIONS_NONE)) {
                    modified = true;
                }
                if (modified) {
                    if (switchParam != null) {
                        switchParam.setValue(Boolean.FALSE);
                    }
                    changeMetadataCommand = new ChangeMetadataCommand(inputNode, param, originaleOutputTable, outputMetaCopy);
                    changeMetadataCommand.execute();
                }
            }
        }
    } catch (ProcessorException e) {
        ExtractMetaDataUtils.getInstance().closeConnection();
        final String strExcepton = e.getMessage();
        Display.getDefault().asyncExec(new Runnable() {

            @Override
            public void run() {
                MessageDialog.openWarning(composite.getShell(), Messages.getString("GuessSchemaController.connectionError"), //$NON-NLS-1$
                strExcepton);
            }
        });
        ExceptionHandler.process(e);
    }
}
Also used : ArrayList(java.util.ArrayList) IMetadataColumn(org.talend.core.model.metadata.IMetadataColumn) MetadataColumn(org.talend.core.model.metadata.MetadataColumn) Shell(org.eclipse.swt.widgets.Shell) CsvArray(org.talend.core.utils.CsvArray) IMetadataTable(org.talend.core.model.metadata.IMetadataTable) MetadataTable(org.talend.core.model.metadata.MetadataTable) MetadataDialog(org.talend.core.ui.metadata.dialog.MetadataDialog) ChangeMetadataCommand(org.talend.designer.core.ui.editor.cmd.ChangeMetadataCommand) IElementParameter(org.talend.core.model.process.IElementParameter) DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection) MappingFileSelectDialog(org.talend.metadata.managment.ui.dialog.MappingFileSelectDialog) ITDQRuleService(org.talend.core.ITDQRuleService) ProcessorException(org.talend.designer.runprocess.ProcessorException) IMetadataColumn(org.talend.core.model.metadata.IMetadataColumn) Point(org.eclipse.swt.graphics.Point) InvocationTargetException(java.lang.reflect.InvocationTargetException) SQLException(java.sql.SQLException) ProcessorException(org.talend.designer.runprocess.ProcessorException) IMetadataTable(org.talend.core.model.metadata.IMetadataTable)

Example 13 with CsvArray

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

the class DesignerCoreService method convertNode.

/*
     * (non-Javadoc)
     * 
     * @see org.talend.designer.core.IDesignerCoreService#convertNode(org.talend.core.model.properties.ConnectionItem,
     * java.lang.String)
     */
@Override
public CsvArray convertNode(ConnectionItem connectionItem, IMetadataConnection convertedConnection, String tableName) throws ProcessorException {
    ConvertRepositoryNodeToProcessNode convertMove = new ConvertRepositoryNodeToProcessNode(connectionItem, convertedConnection, tableName);
    CsvArray array = null;
    array = convertMove.runMockProcess();
    return array;
}
Also used : ConvertRepositoryNodeToProcessNode(org.talend.designer.core.ui.editor.process.ConvertRepositoryNodeToProcessNode) CsvArray(org.talend.core.utils.CsvArray)

Example 14 with CsvArray

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

the class ShadowProcess method run.

public CsvArray run() throws ProcessorException {
    IProcess talendProcess = buildProcess();
    IProcessor processor = ProcessorUtilities.getProcessor(talendProcess, null);
    processor.setProxyParameters(getProxyParameters());
    // try {
    // Delete previous Perl generated file
    File previousFile = outPath.toFile();
    if (previousFile.exists()) {
        previousFile.delete();
    }
    // Process ps = processor.run(process.getContextManager().getDefaultContext(), Processor.NO_STATISTICS,
    // Processor.NO_TRACES,Processor.WATCH_ALLOWED);//Old
    IContext context = talendProcess.getContextManager().getDefaultContext();
    processor.setContext(context);
    process = processor.run(IProcessor.NO_STATISTICS, IProcessor.NO_TRACES, null);
    ProcessStreamTrashReader.readAndForget(process);
    if (!outPath.toFile().exists()) {
        //$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 15 with CsvArray

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

the class JSONFileStep3Form method runShadowProcess.

/**
     * run a ShadowProcess to determined the Metadata.
     */
protected void runShadowProcess(Boolean flag) {
    // getConnection().getXsdFilePath() != null && !getConnection().getXsdFilePath().equals("") &&
    JSONFileConnection connection2 = getConnection();
    String tempJSONFilePath = getContextJSONPath(connection2);
    try {
        informationLabel.setText("   " + "Guess in progress...");
        CsvArray csvArray = null;
        if (EJsonReadbyMode.JSONPATH.getValue().equals(connection2.getReadbyMode())) {
            //$NON-NLS-1$
            csvArray = JSONShadowProcessHelper.getCsvArray(getProcessDescription(false), "FILE_JSON");
        } else {
            //$NON-NLS-1$
            csvArray = JSONShadowProcessHelper.getCsvArray(getProcessDescription(false), "FILE_XML");
        }
        if (csvArray == null) {
            informationLabel.setText("   " + "Guess failure");
        } else {
            refreshMetaDataTable(csvArray, connection2.getSchema().get(0).getSchemaTargets(), flag);
        }
    } catch (CoreException e) {
        if (getParent().getChildren().length == 1) {
            new ErrorDialogWidthDetailArea(getShell(), PID, "Guess failure" + "\n" + "Guess based on preview to step 2", e.getMessage());
        } else {
            new ErrorDialogWidthDetailArea(getShell(), PID, "Guess failure", e.getMessage());
        }
        log.error("Guess failure" + " " + e.getMessage());
    }
    checkFieldsValue();
}
Also used : JSONFileConnection(org.talend.repository.model.json.JSONFileConnection) CsvArray(org.talend.core.utils.CsvArray) CoreException(org.eclipse.core.runtime.CoreException) ErrorDialogWidthDetailArea(org.talend.commons.ui.swt.dialogs.ErrorDialogWidthDetailArea)

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