Search in sources :

Example 11 with MetadataTable

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

the class CreateTableCommand method execute.

@Override
public void execute() {
    for (MetadataTable metadataTable : metaTables) {
        if (!tableNames.contains(metadataTable.getSourceName())) {
            Table table = new Table();
            table.setMetadataTable(metadataTable, null);
            table.setErDiagram(erDiagram);
            erDiagram.addTable(table);
            erDiagram.getMetadataTables().add(metadataTable);
        }
    }
    IRunnableWithProgress progress = new IRunnableWithProgress() {

        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            //$NON-NLS-1$
            monitor.beginTask("", IProgressMonitor.UNKNOWN);
            try {
                fks = EMFRepositoryNodeManager.getInstance().getPKFromTables(erDiagram.getMetadataTables(), erDiagram.getErDiagramComposite().getDialog().getSelectedContext());
            } finally {
                monitor.done();
            }
        }
    };
    ISQLBuilderDialog dialog = erDiagram.getErDiagramComposite().getDialog();
    UIUtils.runWithProgress(progress, true, dialog.getProgressMonitor(), dialog.getShell());
    erDiagram.setRelations(fks);
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Table(org.talend.sqlbuilder.erdiagram.ui.nodes.Table) MetadataTable(org.talend.core.model.metadata.builder.connection.MetadataTable) ISQLBuilderDialog(org.talend.sqlbuilder.ui.ISQLBuilderDialog) MetadataTable(org.talend.core.model.metadata.builder.connection.MetadataTable) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress)

Example 12 with MetadataTable

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

the class MetadataUtils method createTable.

/**
     * Create a MetadataTable instance with ComponentProperties
     * 
     * @param tableName is the table name
     * @param columnList is a map which key is column attribute key from {@link IMetadataTableConstants} and value is
     * attribute value
     * @param schemaProperties is the ComponentProperties which contained component schema
     * @return
     */
public static MetadataTable createTable(String tableName, List<Map<String, Object>> columnList, SchemaProperties schemaProperties) {
    MetadataTable table = ConnectionFactory.eINSTANCE.createMetadataTable();
    table.setName(tableName);
    table.setLabel(table.getName());
    for (Map<String, Object> columnAttrMap : columnList) {
        MetadataColumn column = createColumn(columnAttrMap);
        if (column != null) {
            table.getColumns().add(column);
        }
    }
    Schema avroSchema = MetadataToolAvroHelper.convertToAvro(table);
    schemaProperties.schema.setValue(avroSchema);
    // Set the component properties and schema property name into MetadataTable.
    TaggedValue serializedPropsTV = CoreFactory.eINSTANCE.createTaggedValue();
    serializedPropsTV.setTag(IComponentConstants.COMPONENT_PROPERTIES_TAG);
    serializedPropsTV.setValue(schemaProperties.toSerialized());
    table.getTaggedValue().add(serializedPropsTV);
    TaggedValue schemaPropertyTV = CoreFactory.eINSTANCE.createTaggedValue();
    schemaPropertyTV.setTag(IComponentConstants.COMPONENT_SCHEMA_TAG);
    schemaPropertyTV.setValue(schemaProperties.schema.getName());
    table.getTaggedValue().add(schemaPropertyTV);
    return table;
}
Also used : MetadataColumn(org.talend.core.model.metadata.builder.connection.MetadataColumn) TaggedValue(orgomg.cwm.objectmodel.core.TaggedValue) Schema(org.apache.avro.Schema) MetadataTable(org.talend.core.model.metadata.builder.connection.MetadataTable)

Example 13 with MetadataTable

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

the class SchemaUtilsTest method testUpdateComponentSchema.

@Test
public void testUpdateComponentSchema() {
    //$NON-NLS-1$
    String TEST_TABLE_NAME = "testTable";
    //$NON-NLS-1$
    String TEST_COL_NAME = "userId";
    //$NON-NLS-1$
    String ADDED_COL_NAME = "added";
    //$NON-NLS-1$
    String SCHEMA_PROP_NAME = "schema.schema";
    // Create the test MetadataTable.
    MetadataTable table = createMetadataTable(TEST_TABLE_NAME);
    MetadataColumn testColumn = ConnectionFactory.eINSTANCE.createMetadataColumn();
    testColumn.setName(TEST_COL_NAME);
    testColumn.setLabel(testColumn.getName());
    //$NON-NLS-1$
    testColumn.setDefaultValue("1");
    testColumn.setTalendType(JavaTypesManager.STRING.getId());
    table.getColumns().add(testColumn);
    // Create one component properties which has one schema property.
    //$NON-NLS-1$
    TestProperties props = (TestProperties) new TestProperties("test").init();
    Schema oldSchema = SchemaBuilder.record(TEST_TABLE_NAME).fields().name(TEST_COL_NAME).type().stringType().noDefault().endRecord();
    props.schema.schema.setValue(oldSchema);
    // Set the component properties and schema property name into MetadataTable.
    TaggedValue serializedPropsTV = CoreFactory.eINSTANCE.createTaggedValue();
    serializedPropsTV.setTag(IComponentConstants.COMPONENT_PROPERTIES_TAG);
    serializedPropsTV.setValue(props.toSerialized());
    table.getTaggedValue().add(serializedPropsTV);
    TaggedValue schemaPropertyTV = CoreFactory.eINSTANCE.createTaggedValue();
    schemaPropertyTV.setTag(IComponentConstants.COMPONENT_SCHEMA_TAG);
    schemaPropertyTV.setValue(SCHEMA_PROP_NAME);
    table.getTaggedValue().add(schemaPropertyTV);
    // Add another MetadataColumn into MetadataTable.
    MetadataColumn addedColumn = ConnectionFactory.eINSTANCE.createMetadataColumn();
    addedColumn.setName(ADDED_COL_NAME);
    addedColumn.setLabel(addedColumn.getName());
    //$NON-NLS-1$
    addedColumn.setDefaultValue("x");
    addedColumn.setTalendType(JavaTypesManager.STRING.getId());
    table.getColumns().add(addedColumn);
    // Invoke updateComponentSchema() method.
    SchemaUtils.updateComponentSchema(table, null);
    // Check if the schema object is updated correctly.
    String componentPropertiesStr = null;
    String schemaPropertyName = null;
    EList<TaggedValue> taggedValues = table.getTaggedValue();
    for (TaggedValue taggedValue : taggedValues) {
        String tag = taggedValue.getTag();
        String tagValue = taggedValue.getValue();
        if (IComponentConstants.COMPONENT_PROPERTIES_TAG.equals(tag)) {
            componentPropertiesStr = tagValue;
        } else if (IComponentConstants.COMPONENT_SCHEMA_TAG.equals(tag)) {
            schemaPropertyName = tagValue;
        }
    }
    ComponentProperties componentProperties = ComponentsUtils.getComponentPropertiesFromSerialized(componentPropertiesStr, null);
    Object schemaValue = componentProperties.getValuedProperty(schemaPropertyName).getValue();
    Schema avroSchema = getAvroSchema(schemaValue);
    props.schema.schema.setValue(avroSchema);
    assertNotNull(avroSchema.getField(TEST_COL_NAME));
    assertNotNull(avroSchema.getField(ADDED_COL_NAME));
    assertEquals(2, avroSchema.getFields().size());
    // Test method updateComponentSchema(ComponentProperties componentProperties, String schemaPropertyName,
    // IMetadataTable metadataTable)
    IMetadataTable iMetadataTable = MetadataToolHelper.convert(table);
    iMetadataTable.getListColumns().remove(1);
    SchemaUtils.updateComponentSchema(props, SCHEMA_PROP_NAME, iMetadataTable);
    schemaValue = props.getValuedProperty(schemaPropertyName).getValue();
    avroSchema = getAvroSchema(schemaValue);
    assertNotNull(avroSchema.getField(TEST_COL_NAME));
    assertNull(avroSchema.getField(ADDED_COL_NAME));
    assertEquals(1, avroSchema.getFields().size());
}
Also used : IMetadataTable(org.talend.core.model.metadata.IMetadataTable) MetadataColumn(org.talend.core.model.metadata.builder.connection.MetadataColumn) ComponentProperties(org.talend.components.api.properties.ComponentProperties) TaggedValue(orgomg.cwm.objectmodel.core.TaggedValue) Schema(org.apache.avro.Schema) IMetadataTable(org.talend.core.model.metadata.IMetadataTable) MetadataTable(org.talend.core.model.metadata.builder.connection.MetadataTable) Test(org.junit.Test)

Example 14 with MetadataTable

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

the class SessionTreeNodeManager method convert2INode.

/**
     * DOC qianbing Comment method "convert2INode". Converts the RepositoryNode input to INode. INode is used for the
     * sql editor,result viewer and the detail viewer.
     * 
     * @param repositoryNode RepositoryNode
     * @param selectedContext
     * @return INode
     */
public INode convert2INode(RepositoryNode repositoryNode, String selectedContext, SessionTreeNode sessionTreeNode) throws Exception {
    // Creates the SessionTreeNode.
    RepositoryNodeType nodeType = getRepositoryType(repositoryNode);
    if (nodeType.equals(RepositoryNodeType.DATABASE)) {
        // processes the database
        DatabaseModel model = sessionTreeNode.getDbModel();
        INode[] nodes = model.getChildNodes();
        DatabaseNode dn = (DatabaseNode) nodes[0];
        return dn;
    } else if (nodeType.equals(RepositoryNodeType.TABLE)) {
        // processes the table
        MetadataTableRepositoryObject tableObject = (MetadataTableRepositoryObject) repositoryNode.getObject();
        MetadataTable table = tableObject.getTable();
        String realName = table.getSourceName();
        DatabaseModel model = sessionTreeNode.getDbModel();
        INode[] nodes = model.getChildNodes();
        DatabaseNode dn = (DatabaseNode) nodes[0];
        nodes = dn.getChildNodes();
        CatalogNode cn = (CatalogNode) nodes[0];
        nodes = cn.getChildNodes();
        if (nodes != null && nodes.length > 0) {
            for (INode node : nodes) {
                TableNode tableNode = (TableNode) node;
                if (tableNode.getName().equals(realName)) {
                    return node;
                }
            }
        }
    } else if (nodeType.equals(RepositoryNodeType.COLUMN)) {
        // Processes the column.
        // Gets the repositoryNode's parent,should be the repositoryNode of table infomation.
        repositoryNode = repositoryNode.getParent();
        return convert2INode(repositoryNode, selectedContext, sessionTreeNode);
    }
    return null;
}
Also used : INode(org.talend.sqlbuilder.dbstructure.nodes.INode) DatabaseNode(org.talend.sqlbuilder.dbstructure.nodes.DatabaseNode) CatalogNode(org.talend.sqlbuilder.dbstructure.nodes.CatalogNode) MetadataTable(org.talend.core.model.metadata.builder.connection.MetadataTable) TableNode(org.talend.sqlbuilder.dbstructure.nodes.TableNode) MetadataTableRepositoryObject(org.talend.sqlbuilder.dbstructure.DBTreeProvider.MetadataTableRepositoryObject)

Example 15 with MetadataTable

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

the class SQLBuilderDialog method deleteNoUseTable.

private void deleteNoUseTable() {
    // add for bug TDI-17097
    connection = (DatabaseConnection) SQLBuilderRepositoryNodeManager.getItem(SQLBuilderRepositoryNodeManager.getRoot(nodeInEditor)).getConnection();
    if (SQLBuilderRepositoryNodeManager.tList instanceof List) {
        if (SQLBuilderRepositoryNodeManager.tList.size() == 0) {
            SQLBuilderRepositoryNodeManager.tList.addAll(ConnectionHelper.getTables(connection));
        }
    } else {
        SQLBuilderRepositoryNodeManager.tList = new ArrayList<MetadataTable>();
        SQLBuilderRepositoryNodeManager.tList.addAll(ConnectionHelper.getTables(connection));
    }
    if (SQLBuilderRepositoryNodeManager.tList == null || SQLBuilderRepositoryNodeManager.tList.size() == 0) {
        return;
    }
    // changed for bug TDI-19892
    if (node == null) {
        SQLBuilderRepositoryNodeManager.tList = null;
        return;
    }
    IRepositoryViewObject repositoryObject = node.getObject();
    Item item = repositoryObject.getProperty().getItem();
    if (item instanceof DatabaseConnectionItem) {
        manager.deleteNouseTables(((DatabaseConnectionItem) item).getConnection());
    }
}
Also used : ContextItem(org.talend.core.model.properties.ContextItem) Item(org.talend.core.model.properties.Item) DatabaseConnectionItem(org.talend.core.model.properties.DatabaseConnectionItem) ConnectionItem(org.talend.core.model.properties.ConnectionItem) CTabItem(org.eclipse.swt.custom.CTabItem) MetadataTable(org.talend.core.model.metadata.builder.connection.MetadataTable) IRepositoryViewObject(org.talend.core.model.repository.IRepositoryViewObject) List(java.util.List) ArrayList(java.util.ArrayList) DatabaseConnectionItem(org.talend.core.model.properties.DatabaseConnectionItem)

Aggregations

MetadataTable (org.talend.core.model.metadata.builder.connection.MetadataTable)153 ArrayList (java.util.ArrayList)53 MetadataColumn (org.talend.core.model.metadata.builder.connection.MetadataColumn)51 IMetadataTable (org.talend.core.model.metadata.IMetadataTable)25 IRepositoryViewObject (org.talend.core.model.repository.IRepositoryViewObject)23 ModelElement (orgomg.cwm.objectmodel.core.ModelElement)22 List (java.util.List)18 Test (org.junit.Test)17 PersistenceException (org.talend.commons.exception.PersistenceException)17 DatabaseConnection (org.talend.core.model.metadata.builder.connection.DatabaseConnection)15 MetadataTableRepositoryObject (org.talend.core.repository.model.repositoryObject.MetadataTableRepositoryObject)14 TaggedValue (orgomg.cwm.objectmodel.core.TaggedValue)14 Connection (org.talend.core.model.metadata.builder.connection.Connection)12 RepositoryNode (org.talend.repository.model.RepositoryNode)12 ConnectionItem (org.talend.core.model.properties.ConnectionItem)10 TdColumn (org.talend.cwm.relational.TdColumn)9 IProxyRepositoryFactory (org.talend.repository.model.IProxyRepositoryFactory)9 IRepositoryNode (org.talend.repository.model.IRepositoryNode)9 DelimitedFileConnection (org.talend.core.model.metadata.builder.connection.DelimitedFileConnection)8 HashMap (java.util.HashMap)7