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