Search in sources :

Example 6 with ProcessDescription

use of org.talend.metadata.managment.ui.preview.ProcessDescription in project tdi-studio-se by Talend.

the class JSONWizard method resetMetadata.

private void resetMetadata(List<SchemaTarget> schemaTarget, boolean flag) {
    // TODO
    JSONFileConnection connection2 = JSONConnectionContextUtils.getJSONOriginalValueConnection(connection, this.connectionItem, connection.isContextMode(), true);
    ProcessDescription processDescription = JSONShadowProcessHelper.getProcessDescription(connection2, getTempJsonPath());
    CsvArray csvArray = null;
    try {
        if (EJsonReadbyMode.JSONPATH.getValue().equals(getReadbyMode())) {
            //$NON-NLS-1$
            csvArray = JSONShadowProcessHelper.getCsvArray(processDescription, "FILE_JSON");
        } else {
            //$NON-NLS-1$
            csvArray = JSONShadowProcessHelper.getCsvArray(processDescription, "FILE_XML");
        }
    } catch (CoreException e) {
        ExceptionHandler.process(e);
    }
    List<MetadataColumn> newColumns = new ArrayList<MetadataColumn>();
    String file = ((JSONFileConnection) this.connectionItem.getConnection()).getJSONFilePath();
    if (connection.isContextMode()) {
        ContextType contextType = ConnectionContextHelper.getContextTypeForContextMode(connectionItem.getConnection(), true);
        file = TalendQuoteUtils.removeQuotes(ConnectionContextHelper.getOriginalValue(contextType, file));
    }
    if (file != null) {
        // prepareColumnsFromXSD(file, newColumns, schemaTarget);
        return;
    }
    if (csvArray == null || csvArray.getRows().isEmpty()) {
        return;
    } else {
        List<String[]> csvRows = csvArray.getRows();
        String[] fields = csvRows.get(0);
        int numberOfCol = fields.length;
        // define the label to the metadata width the content of the first row
        int firstRowToExtractMetadata = 0;
        // the first rows is used to define the label of any metadata
        String[] label = new String[numberOfCol];
        for (int i = 0; i < numberOfCol; i++) {
            label[i] = DEFAULT_LABEL + i;
            if (firstRowToExtractMetadata == 0) {
                if (schemaTarget.get(i).getTagName() != null && !schemaTarget.get(i).getTagName().equals("")) {
                    //$NON-NLS-1$
                    //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                    label[i] = "" + schemaTarget.get(i).getTagName().trim().replaceAll(" ", "_");
                    label[i] = MetadataToolHelper.validateColumnName(label[i], i);
                }
            }
        }
        for (int i = 0; i < numberOfCol; i++) {
            // define the first currentType and assimile it to globalType
            String globalType = null;
            int lengthValue = 0;
            int precisionValue = 0;
            int current = firstRowToExtractMetadata;
            while (globalType == null) {
                if (LanguageManager.getCurrentLanguage() == ECodeLanguage.JAVA) {
                    if (i >= csvRows.get(current).length) {
                        //$NON-NLS-1$
                        globalType = "id_String";
                    } else {
                        globalType = JavaDataTypeHelper.getTalendTypeOfValue(csvRows.get(current)[i]);
                        current++;
                    // if (current == csvRows.size()) {
                    // globalType = "id_String"; //$NON-NLS-1$
                    // }
                    }
                } else {
                    if (i >= csvRows.get(current).length) {
                        //$NON-NLS-1$
                        globalType = "String";
                    } else {
                        globalType = PerlDataTypeHelper.getTalendTypeOfValue(csvRows.get(current)[i]);
                        current++;
                    // if (current == csvRows.size()) {
                    // globalType = "String"; //$NON-NLS-1$
                    // }
                    }
                }
            }
            // for another lines
            for (int f = firstRowToExtractMetadata; f < csvRows.size(); f++) {
                fields = csvRows.get(f);
                if (fields.length > i) {
                    String value = fields[i];
                    if (!value.equals("")) {
                        if (LanguageManager.getCurrentLanguage() == ECodeLanguage.JAVA) {
                            if (!JavaDataTypeHelper.getTalendTypeOfValue(value).equals(globalType)) {
                                globalType = JavaDataTypeHelper.getCommonType(globalType, JavaDataTypeHelper.getTalendTypeOfValue(value));
                            }
                        } else {
                            if (!PerlDataTypeHelper.getTalendTypeOfValue(value).equals(globalType)) {
                                globalType = PerlDataTypeHelper.getCommonType(globalType, PerlDataTypeHelper.getTalendTypeOfValue(value));
                            }
                        }
                        if (lengthValue < value.length()) {
                            lengthValue = value.length();
                        }
                        int positionDecimal = 0;
                        if (value.indexOf(',') > -1) {
                            positionDecimal = value.lastIndexOf(',');
                            precisionValue = lengthValue - positionDecimal;
                        } else if (value.indexOf('.') > -1) {
                            positionDecimal = value.lastIndexOf('.');
                            precisionValue = lengthValue - positionDecimal;
                        }
                    } else {
                        IPreferenceStore corePreferenceStore = null;
                        if (GlobalServiceRegister.getDefault().isServiceRegistered(IDesignerCoreUIService.class)) {
                            IDesignerCoreUIService designerCoreUiService = (IDesignerCoreUIService) GlobalServiceRegister.getDefault().getService(IDesignerCoreUIService.class);
                            corePreferenceStore = designerCoreUiService.getPreferenceStore();
                        }
                        if (corePreferenceStore != null && corePreferenceStore.getString(MetadataTypeLengthConstants.VALUE_DEFAULT_TYPE) != null && !corePreferenceStore.getString(MetadataTypeLengthConstants.VALUE_DEFAULT_TYPE).equals("")) {
                            //$NON-NLS-1$
                            globalType = corePreferenceStore.getString(MetadataTypeLengthConstants.VALUE_DEFAULT_TYPE);
                            if (corePreferenceStore.getString(MetadataTypeLengthConstants.VALUE_DEFAULT_LENGTH) != null && !corePreferenceStore.getString(MetadataTypeLengthConstants.VALUE_DEFAULT_LENGTH).equals("")) {
                                //$NON-NLS-1$
                                lengthValue = Integer.parseInt(corePreferenceStore.getString(MetadataTypeLengthConstants.VALUE_DEFAULT_LENGTH));
                            }
                        }
                    }
                }
            }
            // define the metadataColumn to field i
            MetadataColumn metadataColumn = ConnectionFactory.eINSTANCE.createMetadataColumn();
            // hshen bug7249
            //$NON-NLS-1$
            metadataColumn.setPattern("\"dd-MM-yyyy\"");
            // Convert javaType to TalendType
            String talendType = null;
            talendType = globalType;
            if (globalType.equals(JavaTypesManager.FLOAT.getId()) || globalType.equals(JavaTypesManager.DOUBLE.getId())) {
                metadataColumn.setPrecision(precisionValue);
            } else {
                metadataColumn.setPrecision(0);
            }
            metadataColumn.setTalendType(talendType);
            metadataColumn.setLength(lengthValue);
            metadataColumn.setLabel(label[i]);
            newColumns.add(i, metadataColumn);
        }
    }
    EList columns = ConnectionHelper.getTables(connection).toArray(new MetadataTable[0])[0].getColumns();
    if (!flag) {
        for (int i = 0; i < newColumns.size(); i++) {
            for (int j = 0; j < columns.size(); j++) {
                if (newColumns.get(i).getLabel().equals(((MetadataColumn) columns.get(j)).getLabel())) {
                    newColumns.remove(i);
                    newColumns.add(i, (MetadataColumn) columns.get(j));
                }
            }
        }
    }
    columns.clear();
    columns.addAll(newColumns);
}
Also used : ContextType(org.talend.designer.core.model.utils.emf.talendfile.ContextType) JSONFileConnection(org.talend.repository.model.json.JSONFileConnection) ArrayList(java.util.ArrayList) IDesignerCoreUIService(org.talend.core.ui.services.IDesignerCoreUIService) ProcessDescription(org.talend.metadata.managment.ui.preview.ProcessDescription) MetadataColumn(org.talend.core.model.metadata.builder.connection.MetadataColumn) CsvArray(org.talend.core.utils.CsvArray) EList(org.eclipse.emf.common.util.EList) CoreException(org.eclipse.core.runtime.CoreException) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore)

Example 7 with ProcessDescription

use of org.talend.metadata.managment.ui.preview.ProcessDescription in project tbd-studio-se by Talend.

the class ExtractTextFileSchemaService method getProcessDescription.

private ProcessDescription getProcessDescription(HDFSConnection connection, File tmpFile) throws IOException {
    ProcessDescription processDescription = new ProcessDescription();
    Charset guessedCharset = CharsetToolkit.guessEncoding(tmpFile, 4096);
    processDescription.setEncoding(TalendQuoteUtils.addQuotesIfNotExist(guessedCharset.displayName()));
    processDescription.setFieldSeparator(TalendQuoteUtils.addQuotesIfNotExist(ContextParameterUtils.getOriginalValue(ConnectionContextHelper.getContextTypeForContextMode(connection), connection.getFieldSeparator())));
    processDescription.setRowSeparator(TalendQuoteUtils.addQuotesIfNotExist(ContextParameterUtils.getOriginalValue(ConnectionContextHelper.getContextTypeForContextMode(connection), connection.getRowSeparator())));
    processDescription.setFilepath(TalendQuoteUtils.addQuotesIfNotExist(formatFilePath(tmpFile.getAbsolutePath())));
    processDescription.setFooterRow(0);
    int i = 0;
    if (connection.isUseHeader()) {
        String header = ContextParameterUtils.getOriginalValue(ConnectionContextHelper.getContextTypeForContextMode(connection), connection.getHeaderValue());
        i = ConnectionContextHelper.convertValue(header);
        if (i != -1) {
            i--;
        }
    }
    processDescription.setHeaderRow(i);
    processDescription.setCSVOption(false);
    processDescription.setLimitRows(DEFAULT_READ_LINE_NUM);
    processDescription.setPattern(TalendQuoteUtils.addQuotesIfNotExist(ContextParameterUtils.getOriginalValue(ConnectionContextHelper.getContextTypeForContextMode(connection), connection.getFieldSeparator())));
    processDescription.setRemoveEmptyRow(false);
    processDescription.setServer(TalendQuoteUtils.addQuotesIfNotExist(DEFAULT_FILE_SERVER));
    processDescription.setSplitRecord(false);
    return processDescription;
}
Also used : ProcessDescription(org.talend.metadata.managment.ui.preview.ProcessDescription) Charset(java.nio.charset.Charset)

Aggregations

ProcessDescription (org.talend.metadata.managment.ui.preview.ProcessDescription)7 ArrayList (java.util.ArrayList)3 JSONFileConnection (org.talend.repository.model.json.JSONFileConnection)3 MetadataColumn (org.talend.core.model.metadata.builder.connection.MetadataColumn)2 CsvArray (org.talend.core.utils.CsvArray)2 File (java.io.File)1 Charset (java.nio.charset.Charset)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CoreException (org.eclipse.core.runtime.CoreException)1 EList (org.eclipse.emf.common.util.EList)1 IPreferenceStore (org.eclipse.jface.preference.IPreferenceStore)1 IDesignerCoreUIService (org.talend.core.ui.services.IDesignerCoreUIService)1 ContextType (org.talend.designer.core.model.utils.emf.talendfile.ContextType)1 HDFSFile (org.talend.designer.hdfsbrowse.model.HDFSFile)1 SchemaTarget (org.talend.repository.model.json.SchemaTarget)1