Search in sources :

Example 36 with MetadataColumn

use of org.talend.core.model.metadata.builder.connection.MetadataColumn 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 37 with MetadataColumn

use of org.talend.core.model.metadata.builder.connection.MetadataColumn in project tdi-studio-se by Talend.

the class EMFRepositoryNodeManager method getTables.

@SuppressWarnings("unchecked")
public List<MetadataTable> getTables(List<IRepositoryNode> nodes, List<MetadataColumn> selectedColumns) {
    List<MetadataTable> tables = new ArrayList<MetadataTable>();
    for (IRepositoryNode node : nodes) {
        RepositoryNodeType type = SQLBuilderRepositoryNodeManager.getRepositoryType((RepositoryNode) node);
        if (type == RepositoryNodeType.DATABASE) {
            root = (RepositoryNode) node;
            DatabaseConnection connection = (DatabaseConnection) SQLBuilderRepositoryNodeManager.getItem((RepositoryNode) node).getConnection();
            for (MetadataTable table : ConnectionHelper.getTables(connection)) {
                if (!tables.contains(table)) {
                    tables.add(table);
                    selectedColumns.addAll(table.getColumns());
                }
            }
            // if database is selected , It does not need to check others.
            break;
        } else if (type == RepositoryNodeType.TABLE) {
            MetadataTable table = ((MetadataTableRepositoryObject) node.getObject()).getTable();
            if (!tables.contains(table)) {
                tables.add(table);
                selectedColumns.addAll(table.getColumns());
            }
            root = SQLBuilderRepositoryNodeManager.getRoot((RepositoryNode) node);
        } else if (type == RepositoryNodeType.COLUMN) {
            MetadataColumn column = ((MetadataColumnRepositoryObject) node.getObject()).getColumn();
            if (!selectedColumns.contains(column)) {
                selectedColumns.add(column);
            }
            MetadataTable table = column.getTable();
            if (!tables.contains(table)) {
                tables.add(table);
            }
            root = SQLBuilderRepositoryNodeManager.getRoot((RepositoryNode) node);
        }
        setRoot(null);
    }
    return tables;
}
Also used : MetadataColumn(org.talend.core.model.metadata.builder.connection.MetadataColumn) IRepositoryNode(org.talend.repository.model.IRepositoryNode) RepositoryNodeType(org.talend.sqlbuilder.dbstructure.RepositoryNodeType) ArrayList(java.util.ArrayList) MetadataTable(org.talend.core.model.metadata.builder.connection.MetadataTable) DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection) MetadataColumnRepositoryObject(org.talend.sqlbuilder.dbstructure.DBTreeProvider.MetadataColumnRepositoryObject) RepositoryNode(org.talend.repository.model.RepositoryNode) IRepositoryNode(org.talend.repository.model.IRepositoryNode)

Example 38 with MetadataColumn

use of org.talend.core.model.metadata.builder.connection.MetadataColumn in project tdi-studio-se by Talend.

the class MetadataColumnComparator method sortTableColumn.

/**
     * dev Comment method "sortTableColumn".
     * 
     * @param set
     * @return
     */
@SuppressWarnings("unchecked")
private static List<MetadataTable> sortTableColumn(Collection<MetadataTable> set) {
    List<MetadataTable> sysTables = new ArrayList<MetadataTable>();
    List<MetadataTable> divTables = new ArrayList<MetadataTable>();
    List<MetadataTable> grayTables = new ArrayList<MetadataTable>();
    List<MetadataTable> norTables = new ArrayList<MetadataTable>();
    List<MetadataTable> sortTables = new ArrayList<MetadataTable>();
    for (MetadataTable object : set) {
        boolean isTableNormal = true;
        if (object.isDivergency()) {
            divTables.add(object);
            isTableNormal = false;
            continue;
        }
        if (object.getLabel() == null || "".equals(object.getLabel())) {
            //$NON-NLS-1$
            grayTables.add(object);
            isTableNormal = false;
            continue;
        }
        List<MetadataColumn> columns = sortColumn(object.getColumns());
        object.getColumns().clear();
        object.getColumns().addAll(columns);
        for (MetadataColumn column : columns) {
            if (column.isSynchronised()) {
                sysTables.add(object);
                isTableNormal = false;
                break;
            }
            if (column.isDivergency()) {
                divTables.add(object);
                isTableNormal = false;
                break;
            }
            if (column.getLabel() == null || "".equals(column.getLabel())) {
                //$NON-NLS-1$
                grayTables.add(object);
                isTableNormal = false;
                break;
            }
        }
        if (isTableNormal) {
            norTables.add(object);
        }
    }
    MetadataTableComparator metadataTableComparator = new MetadataTableComparator();
    Collections.sort(norTables, metadataTableComparator);
    Collections.sort(sysTables, metadataTableComparator);
    Collections.sort(divTables, metadataTableComparator);
    Collections.sort(grayTables, metadataTableComparator);
    sortTables.addAll(norTables);
    sortTables.addAll(sysTables);
    sortTables.addAll(divTables);
    sortTables.addAll(grayTables);
    return sortTables;
}
Also used : MetadataColumn(org.talend.core.model.metadata.builder.connection.MetadataColumn) ArrayList(java.util.ArrayList) MetadataTable(org.talend.core.model.metadata.builder.connection.MetadataTable)

Example 39 with MetadataColumn

use of org.talend.core.model.metadata.builder.connection.MetadataColumn in project tdi-studio-se by Talend.

the class MetadataColumnComparator method sortColumn.

/**
     * dev Comment method "sortColumn".
     * 
     * @param columns
     * @return
     */
private static List<MetadataColumn> sortColumn(Collection<MetadataColumn> columns) {
    List<MetadataColumn> sortColumns = new ArrayList<MetadataColumn>();
    List<MetadataColumn> sysColumns = new ArrayList<MetadataColumn>();
    List<MetadataColumn> grayColumns = new ArrayList<MetadataColumn>();
    List<MetadataColumn> divColumns = new ArrayList<MetadataColumn>();
    List<MetadataColumn> norColumns = new ArrayList<MetadataColumn>();
    for (MetadataColumn column : columns) {
        if (column.isSynchronised()) {
            sysColumns.add(column);
            continue;
        }
        if (column.isDivergency()) {
            divColumns.add(column);
            continue;
        }
        if (column.getLabel() == null || "".equals(column.getLabel())) {
            //$NON-NLS-1$
            grayColumns.add(column);
            continue;
        }
        norColumns.add(column);
    }
    MetadataColumnComparator metadataColumnComparator = new MetadataColumnComparator();
    // TUP-17181 fixed:for the normal tables,no need to sort it since it cause the display result different with
    // before
    // Collections.sort(norColumns, metadataColumnComparator);
    Collections.sort(sysColumns, metadataColumnComparator);
    Collections.sort(divColumns, metadataColumnComparator);
    Collections.sort(grayColumns, metadataColumnComparator);
    sortColumns.addAll(norColumns);
    sortColumns.addAll(sysColumns);
    sortColumns.addAll(divColumns);
    sortColumns.addAll(grayColumns);
    return sortColumns;
}
Also used : MetadataColumn(org.talend.core.model.metadata.builder.connection.MetadataColumn) ArrayList(java.util.ArrayList)

Example 40 with MetadataColumn

use of org.talend.core.model.metadata.builder.connection.MetadataColumn in project tdi-studio-se by Talend.

the class MetadataColumnComparator method saveOneMetadataColumn.

@SuppressWarnings("unchecked")
private static void saveOneMetadataColumn(List<MetadataColumn> emfCols, List<MetadataColumn> columnNodes, List<MetadataColumn> dbCols) {
    for (int i = 0; i < columnNodes.size(); i++) {
        MetadataColumn columnNode = columnNodes.get(i);
        MetadataColumn emfCol = emfCols.get(i);
        MetadataColumn metadataColumn = dbCols.get(i);
        if (metadataColumn.getLabel().equals(columnNode.getOriginalField())) {
            if (columnNode.getLabel().equals("")) {
                //$NON-NLS-1$
                columnNode.setLabel(columnNode.getOriginalField());
            }
            columnNode.setComment(metadataColumn.getComment());
            columnNode.setDefaultValue(metadataColumn.getDefaultValue());
            columnNode.setKey(metadataColumn.isKey());
            columnNode.setLength(metadataColumn.getLength());
            columnNode.setNullable(metadataColumn.isNullable());
            columnNode.setPattern(metadataColumn.getPattern());
            columnNode.setPrecision(metadataColumn.getPrecision());
            columnNode.setSourceType(metadataColumn.getSourceType());
            columnNode.setTalendType(metadataColumn.getTalendType());
            columnNode.setDivergency(false);
            columnNode.setSynchronised(false);
            if (emfCol.getLabel().equals("")) {
                //$NON-NLS-1$
                emfCol.setLabel(emfCol.getOriginalField());
            }
            emfCol.setComment(metadataColumn.getComment());
            emfCol.setDefaultValue(metadataColumn.getDefaultValue());
            emfCol.setKey(metadataColumn.isKey());
            emfCol.setLength(metadataColumn.getLength());
            emfCol.setNullable(metadataColumn.isNullable());
            emfCol.setPattern(metadataColumn.getPattern());
            emfCol.setPrecision(metadataColumn.getPrecision());
            emfCol.setSourceType(metadataColumn.getSourceType());
            emfCol.setTalendType(metadataColumn.getTalendType());
            emfCol.setDivergency(false);
            emfCol.setSynchronised(false);
        }
    }
}
Also used : MetadataColumn(org.talend.core.model.metadata.builder.connection.MetadataColumn)

Aggregations

MetadataColumn (org.talend.core.model.metadata.builder.connection.MetadataColumn)59 ArrayList (java.util.ArrayList)27 MetadataTable (org.talend.core.model.metadata.builder.connection.MetadataTable)22 List (java.util.List)13 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)10 MetadataEmfTableEditor (org.talend.core.ui.metadata.editor.MetadataEmfTableEditor)10 IModel (org.talend.designer.hl7.model.IModel)9 IMetadataTable (org.talend.core.model.metadata.IMetadataTable)8 Command (org.eclipse.gef.commands.Command)7 EList (org.eclipse.emf.common.util.EList)6 HashMap (java.util.HashMap)5 IMetadataColumn (org.talend.core.model.metadata.IMetadataColumn)5 DatabaseConnection (org.talend.core.model.metadata.builder.connection.DatabaseConnection)5 ExtendedTableResetDBTypesCommand (org.talend.core.ui.metadata.extended.command.ExtendedTableResetDBTypesCommand)5 MetadataEmfExportXmlCommand (org.talend.core.ui.metadata.extended.command.MetadataEmfExportXmlCommand)5 MetadataEmfImportXmlCommand (org.talend.core.ui.metadata.extended.command.MetadataEmfImportXmlCommand)5 MetadataEmfPasteCommand (org.talend.core.ui.metadata.extended.command.MetadataEmfPasteCommand)5 HL7MultiSchemaUI (org.talend.designer.hl7.ui.HL7MultiSchemaUI)5 Iterator (java.util.Iterator)4 Map (java.util.Map)4