Search in sources :

Example 6 with MetadataColumn

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

the class Table method formColumns.

/**
     * Form the columns within table depends on the metadata columns in metadata table.
     * 
     * yzhang Comment method "formColumns". If null, means select all column.
     * 
     * @param selectedColumns
     */
private void formColumns(List<MetadataColumn> selectedColumns) {
    if (metadataTable != null) {
        EList metadataColumns = metadataTable.getColumns();
        Column col = new Column();
        //$NON-NLS-1$
        col.setElementName("*");
        col.setSelected(true);
        col.setTable(this);
        addColumn(col);
        Iterator iterator = metadataColumns.iterator();
        while (iterator.hasNext()) {
            MetadataColumn metadataColumn = (MetadataColumn) iterator.next();
            Column column = new Column();
            column.setMetadataColumn(metadataColumn);
            if (selectedColumns == null) {
                column.setSelected(true);
            } else if (selectedColumns.contains(metadataColumn)) {
                column.setSelected(true);
            }
            column.setTable(this);
            addColumn(column);
        }
    }
}
Also used : MetadataColumn(org.talend.core.model.metadata.builder.connection.MetadataColumn) EList(org.eclipse.emf.common.util.EList) MetadataColumn(org.talend.core.model.metadata.builder.connection.MetadataColumn) Iterator(java.util.Iterator)

Example 7 with MetadataColumn

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

the class Table method setMetadataTable.

/**
     * Sets the metadataTable for this table. The selectedColumns contains the selected columns. if selectedColumns is
     * null, means select all.
     * 
     * @param metadataTable
     * @param selectedColumns
     */
public void setMetadataTable(MetadataTable metadataTable, List<MetadataColumn> selectedColumns) {
    this.metadataTable = metadataTable;
    formColumns(selectedColumns);
    size.height = 17;
    String sourceName = this.metadataTable.getSourceName();
    size.width = (sourceName == null ? 0 : sourceName.length()) * 7 + 6;
    for (Object object : columns) {
        if (object instanceof Column) {
            Dimension dimension = ((Column) object).getSize();
            size.height += dimension.height;
            if (dimension.width > size.width) {
                size.width = dimension.width;
            }
        }
    }
    maxHeight = size.height;
    maxWidth = size.width;
    if (maxHeight > 220) {
        size.height = 220;
    }
    fireStructureChange(PROP_COLUMNS, this.columns);
}
Also used : MetadataColumn(org.talend.core.model.metadata.builder.connection.MetadataColumn) Dimension(org.eclipse.draw2d.geometry.Dimension)

Example 8 with MetadataColumn

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

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

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

the class MetadataColumnComparator method saveEMFMetadataColumn.

@SuppressWarnings("unchecked")
private static void saveEMFMetadataColumn(String id, List<MetadataColumn> columnNodes) {
    DatabaseConnectionItem item = getEMFItem(id);
    final DatabaseConnection connection = (DatabaseConnection) item.getConnection();
    IMetadataConnection iMetadataConnection = ConvertionHelper.convert(connection);
    Set<MetadataTable> tableset = ConnectionHelper.getTables(connection);
    List<MetadataTable> tables = new ArrayList<MetadataTable>();
    tables.addAll(tableset);
    List<MetadataColumn> emfCols = new ArrayList<MetadataColumn>();
    List<MetadataColumn> dbCols = new ArrayList<MetadataColumn>();
    for (MetadataColumn col : columnNodes) {
        for (MetadataTable table : tables) {
            if (table.getLabel().equals(col.getTable().getLabel())) {
                List<TdColumn> returnCols = ExtractMetaDataFromDataBase.returnMetadataColumnsFormTable(iMetadataConnection, table.getSourceName());
                for (MetadataColumn emfcolumn : table.getColumns()) {
                    for (MetadataColumn column : returnCols) {
                        if (emfcolumn.getLabel().equals(col.getLabel()) && column.getLabel().equals(col.getOriginalField())) {
                            emfCols.add(emfcolumn);
                            dbCols.add(column);
                        }
                    }
                }
            }
        }
    }
    saveOneMetadataColumn(emfCols, columnNodes, dbCols);
    saveMetaData(item);
}
Also used : MetadataColumn(org.talend.core.model.metadata.builder.connection.MetadataColumn) TdColumn(org.talend.cwm.relational.TdColumn) MetadataTable(org.talend.core.model.metadata.builder.connection.MetadataTable) ArrayList(java.util.ArrayList) DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection) IMetadataConnection(org.talend.core.model.metadata.IMetadataConnection) DatabaseConnectionItem(org.talend.core.model.properties.DatabaseConnectionItem)

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