use of org.talend.core.model.metadata.builder.connection.DatabaseConnection in project tdi-studio-se by Talend.
the class EncryptDbPasswordMigrationTask method encryptItem.
/**
* DOC chuang Comment method "encryptItem".
*
* @param item
* @return
*/
private ExecutionResult encryptItem(DatabaseConnectionItem item) {
Connection conn = item.getConnection();
if (conn instanceof DatabaseConnection) {
DatabaseConnection dbConn = (DatabaseConnection) conn;
if (needEncrypted(dbConn)) {
try {
encryptPassword(dbConn);
factory.save(item, true);
} catch (Exception e) {
ExceptionHandler.process(e);
return ExecutionResult.FAILURE;
}
}
}
return ExecutionResult.SUCCESS_NO_ALERT;
}
use of org.talend.core.model.metadata.builder.connection.DatabaseConnection in project tdi-studio-se by Talend.
the class MergeTosMetadataMigrationTask method execute.
@Override
public ExecutionResult execute(Item item) {
if (item instanceof ConnectionItem) {
try {
URI itemURI = getItemURI(item);
if (itemURI != null) {
URI itemResourceURI = getItemResourceURI(itemURI);
if (metadata400to410 == null) {
metadata400to410 = new TosMetadataMigrationFrom400to410();
}
Resource migratedResource = metadata400to410.migrate(itemResourceURI.toString(), new NullProgressMonitor());
HashMap<String, Object> xmlSaveOtions = XML_SAVE_OTIONS_1_0;
if (migratedResource != null) {
// check for DB connection caus we need to setup Schema and Catalog properly
EObject content = migratedResource.getContents().get(0);
if (content != null && "DatabaseConnection".equals(content.eClass().getName())) {
// resource is dynamic EMF so convert it to static model by serialising it and reloading it
ByteArrayOutputStream tempStream = new ByteArrayOutputStream();
try {
// serialize into memory
try {
migratedResource.save(tempStream, XML_SAVE_OTIONS_1_0);
} catch (Exception e) {
// try with version 1.1
tempStream = new ByteArrayOutputStream();
xmlSaveOtions = XML_SAVE_OTIONS_1_1;
migratedResource.save(tempStream, xmlSaveOtions);
} finally {
tempStream.close();
}
// create a resource to laod the inmemory resource that should be a static EMF model
migratedResource = resourceSet.createResource(URI.createURI(//$NON-NLS-1$
"http://talend/dummy_static.metadata"));
migratedResource.load(new ByteArrayInputStream(tempStream.toByteArray()), xmlSaveOtions);
// check that DBConnection is firdt element
DatabaseConnection databaseConnection = SwitchHelpers.DATABASECONNECTION_SWITCH.doSwitch(migratedResource.getContents().get(0));
// do not check for null caus DB connection is already check above
String databaseType = databaseConnection.getDatabaseType();
databaseConnection.setDriverClass(ExtractMetaDataUtils.getInstance().getDriverClassByDbType(databaseType));
EDatabaseTypeName currentType = EDatabaseTypeName.getTypeFromDbType(databaseType);
EDatabaseSchemaOrCatalogMapping curCatalog = currentType.getCatalogMappingField();
EDatabaseSchemaOrCatalogMapping curSchema = currentType.getSchemaMappingField();
// all the DB connection are migrated with a Schema by default
if (!curCatalog.equals(EDatabaseSchemaOrCatalogMapping.None)) {
List<Schema> schemas = ConnectionHelper.getSchema(databaseConnection);
if (!curSchema.equals(EDatabaseSchemaOrCatalogMapping.None)) {
// we need to place the current schemas into a catalogs
ConnectionHelper.removeSchemas(schemas, databaseConnection);
for (Schema schema : schemas) {
// compute the name of the schema and the catalogs
String schemaName = computeSchemaName(schema, databaseConnection, curSchema);
String catalogName = computeCatalogName(databaseConnection, curCatalog);
schema.setName(schemaName);
Catalog catalog = RelationalFactory.eINSTANCE.createCatalog();
// catalogs are not in a contained reference
migratedResource.getContents().add(catalog);
catalog.setName(catalogName);
// add the schema to the catalog and the the catalog to the connection
CatalogHelper.addSchemas(Collections.singleton(schema), catalog);
ConnectionHelper.addCatalog(catalog, databaseConnection);
}
} else {
// we need to replace the Schemas with a Catalogs
for (Schema schema : schemas) {
// compute the name the catalog
String catalogName = computeCatalogName(databaseConnection, curCatalog);
// use owned elements to get everything regardless of tables or views or
// else
Catalog catalog = RelationalFactory.eINSTANCE.createCatalog();
// catalogs are not in a contained reference
migratedResource.getContents().add(catalog);
catalog.setName(catalogName);
catalog.getOwnedElement().addAll(schema.getOwnedElement());
ConnectionHelper.addCatalog(catalog, databaseConnection);
ConnectionHelper.removeSchemas(Collections.singleton(schema), databaseConnection);
}
}
} else if (!curSchema.equals(EDatabaseSchemaOrCatalogMapping.None)) {
List<Schema> schemas = ConnectionHelper.getSchema(databaseConnection);
for (Schema schema : schemas) {
String schemaName = computeSchemaName(schema, databaseConnection, curSchema);
schema.setName(schemaName);
}
}
// else no catalog so we keep the schema as is
} catch (Exception e) {
// we have an exception finalising the migration but we trap it caus we still try to
// save it
log.error("Cannot complete merge metadata migration on file:" + itemResourceURI.toString(), e);
ExceptionHandler.process(e);
} finally {
tempStream.close();
}
}
// else not a DB connection so persist
OutputStream outputStream = item.eResource().getResourceSet().getURIConverter().createOutputStream(itemResourceURI, null);
try {
migratedResource.save(outputStream, xmlSaveOtions);
} finally {
outputStream.close();
}
}
return ExecutionResult.SUCCESS_WITH_ALERT;
}
} catch (ATLCoreException e) {
log.error(e);
ExceptionHandler.process(e);
return ExecutionResult.SUCCESS_NO_ALERT;
} catch (IOException e) {
log.error(e);
ExceptionHandler.process(e);
return ExecutionResult.SUCCESS_NO_ALERT;
} finally {
resourceSet.getResources().clear();
}
}
return ExecutionResult.NOTHING_TO_DO;
}
use of org.talend.core.model.metadata.builder.connection.DatabaseConnection in project tdi-studio-se by Talend.
the class QueryGuessCommand method generateNewQuery.
private String generateNewQuery() {
// used for generating new Query.
ExtractMetaDataUtils extractMeta = ExtractMetaDataUtils.getInstance();
if (realDBType != null) {
dbType = realDBType;
}
//
if (node != null && node instanceof INode) {
process = ((INode) node).getProcess();
}
if (this.realTableId != null && this.dbNameAndDbTypeMap.containsKey(this.realTableId)) {
dbType = this.dbNameAndDbTypeMap.get(this.realTableId);
}
if (dbType == null || dbType.equals("")) {
//$NON-NLS-1$
IElementParameter ptParam = node.getElementParameterFromField(EParameterFieldType.PROPERTY_TYPE);
if (ptParam != null && ptParam.getRepositoryValue() != null) {
if (ptParam.getRepositoryValue().endsWith(EDatabaseTypeName.GENERAL_JDBC.getProduct())) {
dbType = EDatabaseTypeName.GENERAL_JDBC.getDisplayName();
}
}
}
boolean isJdbc = false;
INode connectionNode = null;
IElementParameter existConnection = node.getElementParameter("USE_EXISTING_CONNECTION");
boolean useExistConnection = (existConnection == null ? false : (Boolean) existConnection.getValue());
if (useExistConnection) {
IElementParameter connector = node.getElementParameter("CONNECTION");
if (connector != null) {
String connectorValue = connector.getValue().toString();
List<? extends INode> graphicalNodes = process.getGeneratingNodes();
for (INode node : graphicalNodes) {
if (node.getUniqueName().equals(connectorValue)) {
connectionNode = node;
break;
}
}
}
}
// hywang add for bug 7575
if (dbType != null && dbType.equals(EDatabaseTypeName.GENERAL_JDBC.getDisplayName())) {
isJdbc = true;
String driverClassName = node.getElementParameter("DRIVER_CLASS").getValue().toString();
if (connectionNode != null) {
driverClassName = connectionNode.getElementParameter("DRIVER_CLASS").getValue().toString();
}
driverClassName = TalendTextUtils.removeQuotes(driverClassName);
//
if (driverClassName != null && !"".equals(driverClassName)) {
boolean isContextModeDriverClass = ContextParameterUtils.containContextVariables(driverClassName);
if (isContextModeDriverClass) {
driverClassName = JavaProcessUtil.getContextOriginalValue(process, driverClassName);
}
}
// specil handle Sybase Database's driverClassName
if (driverClassName != null && !"".equals(driverClassName)) {
if (driverClassName.equals("com.sybase.jdbc3.jdbc.SybDataSource")) {
driverClassName = EDatabase4DriverClassName.SYBASEASE.getDriverClass();
}
}
// DRIVER_JAR:
String driverJarName = node.getElementParameter("DRIVER_JAR").getValue().toString();
if (connectionNode != null) {
driverJarName = connectionNode.getElementParameter("DRIVER_JAR").getValue().toString();
}
if (driverJarName != null && driverJarName.startsWith("[") && driverJarName.endsWith("]")) {
driverJarName = driverJarName.substring(1, driverJarName.length() - 1);
if (driverJarName != null && driverJarName.startsWith("{") && driverJarName.endsWith("}")) {
driverJarName = driverJarName.substring(1, driverJarName.length() - 1);
}
}
if (driverJarName != null && !"".equals(driverJarName)) {
boolean isContextMode = ContextParameterUtils.containContextVariables(driverJarName);
if (isContextMode) {
driverJarName = JavaProcessUtil.getContextOriginalValue(process, driverJarName);
}
dbType = extractMeta.getDbTypeByClassNameAndDriverJar(driverClassName, driverJarName);
} else {
dbType = extractMeta.getDbTypeByClassName(driverClassName);
}
DatabaseConnection dbConn = null;
if (dbType == null) {
// handle context mode
if (conn != null) {
if (conn instanceof DatabaseConnection) {
dbConn = (DatabaseConnection) conn;
}
driverClassName = DatabaseConnectionParameterUtil.getTrueParamValue(dbConn, driverClassName);
dbType = extractMeta.getDbTypeByClassName(driverClassName);
}
}
if (dbType == null) {
// if we can not get the DB Type from the existing driver list, just set back the type to ORACLE
// since it's one DB unknown from Talend.
// it might not work directly for all DB, but it will generate a standard query.
dbType = EDatabaseTypeName.ORACLE_OCI.getDisplayName();
}
// data view, conn=null
// need add code here for dbtype(oracle)
}
if (dbNameAndSchemaMap != null) {
schema = this.dbNameAndSchemaMap.get(this.realTableId);
}
String propertyType = (String) node.getPropertyValue(EParameterName.PROPERTY_TYPE.getName());
boolean isTeradata = false;
if (dbType != null) {
isTeradata = dbType.equalsIgnoreCase(EDatabaseTypeName.TERADATA.getDisplayName());
}
if (propertyType != null && !propertyType.equals(EmfComponent.REPOSITORY)) {
List<? extends IElementParameter> elementParameters = this.node.getElementParameters();
if (useExistConnection) {
elementParameters = connectionNode.getElementParameters();
}
for (IElementParameter param : elementParameters) {
if (param.getRepositoryValue() != null) {
if (//$NON-NLS-1$
(!isTeradata && param.getRepositoryValue().equals("SCHEMA")) || (isTeradata && param.getRepositoryValue().equals("SID"))) {
// check if dbtype is //$NON-NLS-1$
// Teradata, always keep the
// query style like
// "dbname.tablename.columnname" on build-in mode
schema = (String) param.getValue();
//$NON-NLS-1$ //$NON-NLS-2$
schema = schema.replace("\"", "");
//$NON-NLS-1$ //$NON-NLS-2$
schema = schema.replace("\'", "");
break;
}
}
}
} else if (schema == null) {
IElementParameter param = node.getElementParameter(EParameterName.REPOSITORY_PROPERTY_TYPE.getName());
if (param != null) {
try {
IRepositoryViewObject object = DesignerPlugin.getDefault().getRepositoryService().getProxyRepositoryFactory().getLastVersion((String) param.getValue());
if (object != null) {
Item item = object.getProperty().getItem();
if (item != null && item instanceof DatabaseConnectionItem) {
if (isTeradata) {
schema = (String) RepositoryToComponentProperty.getValue(((DatabaseConnectionItem) item).getConnection(), "SID", //$NON-NLS-1$
null);
} else {
schema = (String) RepositoryToComponentProperty.getValue(((DatabaseConnectionItem) item).getConnection(), "SCHEMA", //$NON-NLS-1$
null);
}
schema = TalendTextUtils.removeQuotes(schema);
}
}
} catch (PersistenceException e) {
//
}
}
}
// if (conn instanceof DatabaseConnection && conn.isContextMode()) {
// schema = DatabaseConnectionParameterUtil.getContextTrueValue((DatabaseConnection) conn, schema);
// }
String newQuery = null;
realTableName = QueryUtil.getTableName(node, newOutputMetadataTable, schema, dbType, realTableName);
if (realTableName.startsWith(TalendTextUtils.QUOTATION_MARK) && realTableName.endsWith(TalendTextUtils.QUOTATION_MARK) && realTableName.length() > 2) {
realTableName = realTableName.substring(1, realTableName.length() - 1);
}
if (conn != null && (isJdbc || dbType.equals(EDatabaseTypeName.JAVADB_EMBEDED.getDisplayName()) || (StringUtils.isEmpty(schema) && (EDatabaseTypeName.ORACLE_CUSTOM.equals(EDatabaseTypeName.getTypeFromDbType(dbType)) || EDatabaseTypeName.ORACLEFORSID.equals(EDatabaseTypeName.getTypeFromDbType(dbType)) || EDatabaseTypeName.ORACLESN.equals(EDatabaseTypeName.getTypeFromDbType(dbType)) || EDatabaseTypeName.ORACLE_OCI.equals(EDatabaseTypeName.getTypeFromDbType(dbType)))))) {
schema = getDefaultSchema(realTableName);
}
newQuery = QueryUtil.generateNewQuery(node, newOutputMetadataTable, isJdbc, dbType, schema, realTableName);
// Added yyin TDQ-5616: if there are where clause, append it to the query
if (whereClause != null) {
// the where clause is inputted by the user, so no need to modify it.
//$NON-NLS-1$
newQuery = newQuery.substring(0, newQuery.length() - 1) + whereClause + "\"";
}
return TalendTextUtils.addSQLQuotes(newQuery);
}
use of org.talend.core.model.metadata.builder.connection.DatabaseConnection in project tdi-studio-se by Talend.
the class AbstractElementPropertySectionController method isConnectionExist.
protected boolean isConnectionExist() {
ISQLBuilderService service = null;
if (GlobalServiceRegister.getDefault().isServiceRegistered(ISQLBuilderService.class)) {
service = (ISQLBuilderService) GlobalServiceRegister.getDefault().getService(ISQLBuilderService.class);
}
if (service == null) {
return false;
}
if (contextManager != null && contextManager.getDefaultContext().getContextParameterList().size() != 0) {
initConnectionParametersWithContext(elem, contextManager.getDefaultContext());
}
DatabaseConnection connection = service.createConnection(connParameters);
if (connection != null) {
IMetadataConnection metadataConnection = null;
metadataConnection = ConvertionHelper.convert(connection);
return checkExistConnections(metadataConnection);
}
return false;
}
use of org.talend.core.model.metadata.builder.connection.DatabaseConnection in project tdi-studio-se by Talend.
the class RepositoryValueUtils method updateRepositoryList.
private void updateRepositoryList() {
IProxyRepositoryFactory factory = DesignerPlugin.getDefault().getProxyRepositoryFactory();
tableIdAndDbTypeMap = new HashMap<String, String>();
tableIdAndDbSchemaMap = new HashMap<String, String>();
repositoryTableMap = new HashMap<String, IMetadataTable>();
repositoryQueryStoreMap = new HashMap<String, Query>();
repositoryConnectionItemMap = new HashMap<String, ConnectionItem>();
List<ConnectionItem> metadataConnectionsItem = null;
try {
metadataConnectionsItem = factory.getMetadataConnectionsItem();
} catch (PersistenceException e) {
throw new RuntimeException(e);
}
if (metadataConnectionsItem != null) {
repositoryTableMap.clear();
repositoryQueryStoreMap.clear();
repositoryConnectionItemMap.clear();
tablesMap.clear();
queriesMap.clear();
List<String> tableNamesList = new ArrayList<String>();
List<String> tableValuesList = new ArrayList<String>();
List<String> queryStoreNameList = new ArrayList<String>();
List<String> queryStoreValuesList = new ArrayList<String>();
for (ConnectionItem connectionItem : metadataConnectionsItem) {
Connection connection = connectionItem.getConnection();
if (!connection.isReadOnly()) {
//$NON-NLS-1$
repositoryConnectionItemMap.put(connectionItem.getProperty().getId() + "", connectionItem);
repositoryDBIdAndNameMap.put(connectionItem.getProperty().getId(), getRepositoryAliasName(connectionItem) + //$NON-NLS-1$
":" + connectionItem.getProperty().getLabel());
for (Object tableObj : ConnectionHelper.getTables(connection)) {
org.talend.core.model.metadata.builder.connection.MetadataTable table;
table = (org.talend.core.model.metadata.builder.connection.MetadataTable) tableObj;
if (factory.getStatus(connectionItem) != ERepositoryStatus.DELETED) {
if (!factory.isDeleted(table)) {
String name = //$NON-NLS-1$
getRepositoryAliasName(connectionItem) + ":" + connectionItem.getProperty().getLabel() + " - " + //$NON-NLS-1$
table.getLabel();
//$NON-NLS-1$
String value = connectionItem.getProperty().getId() + " - " + table.getLabel();
IMetadataTable newTable = ConvertionHelper.convert(table);
repositoryTableMap.put(value, newTable);
if (connection instanceof DatabaseConnection) {
String dbType = ((DatabaseConnection) connection).getDatabaseType();
String schema = ((DatabaseConnection) connection).getUiSchema();
tableIdAndDbTypeMap.put(newTable.getId(), dbType);
if (schema != null && !schema.equals("")) {
//$NON-NLS-1$
tableIdAndDbSchemaMap.put(newTable.getId(), schema);
}
}
tableNamesList.add(name);
tableValuesList.add(value);
}
}
}
}
tablesMap.put(connectionItem.getProperty().getId(), tableValuesList);
if (connection instanceof DatabaseConnection && !connection.isReadOnly()) {
DatabaseConnection dbConnection = (DatabaseConnection) connection;
QueriesConnection queriesConnection = dbConnection.getQueries();
if (queriesConnection != null) {
List<Query> qs = queriesConnection.getQuery();
for (Query query : qs) {
String name = //$NON-NLS-1$
getRepositoryAliasName(connectionItem) + ":" + connectionItem.getProperty().getLabel() + " - " + //$NON-NLS-1$
query.getLabel();
//$NON-NLS-1$
String value = connectionItem.getProperty().getId() + " - " + query.getLabel();
repositoryQueryStoreMap.put(value, query);
queryStoreNameList.add(name);
queryStoreValuesList.add(value);
}
}
}
queriesMap.put(connectionItem.getProperty().getId(), queryStoreValuesList);
}
}
}
Aggregations