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);
}
}
}
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);
}
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;
}
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());
}
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);
}
Aggregations