Search in sources :

Example 1 with ManagerConnection

use of org.talend.metadata.managment.repository.ManagerConnection in project tdq-studio-se by Talend.

the class ConnectionUtils method isConnectionAvailable.

/**
 * This method is used to check conectiton is avalible for analysis or report ,when analysis or report runs.
 *
 * @param analysisDataProvider
 * @return
 */
public static ReturnCode isConnectionAvailable(Connection analysisDataProvider) {
    ReturnCode returnCode = new ReturnCode();
    if (analysisDataProvider == null) {
        returnCode.setOk(false);
        // $NON-NLS-1$
        returnCode.setMessage(Messages.getString("ConnectionUtils.checkConnFailTitle"));
        return returnCode;
    }
    // check hive connection
    IMetadataConnection metadataConnection = ConvertionHelper.convert(analysisDataProvider);
    if (metadataConnection != null) {
        if (EDatabaseTypeName.HIVE.getXmlName().equalsIgnoreCase(metadataConnection.getDbType())) {
            try {
                // need to do this first when check for hive embed connection.
                if (isHiveEmbedded(analysisDataProvider)) {
                    JavaSqlFactory.doHivePreSetup(analysisDataProvider);
                }
                HiveConnectionManager.getInstance().checkConnection(metadataConnection);
                returnCode.setOk(true);
            } catch (ClassNotFoundException e) {
                returnCode.setOk(false);
                returnCode.setMessage(e.toString());
            } catch (InstantiationException e) {
                returnCode.setOk(false);
                returnCode.setMessage(e.toString());
            } catch (IllegalAccessException e) {
                returnCode.setOk(false);
                returnCode.setMessage(e.toString());
            } catch (SQLException e) {
                returnCode.setOk(false);
                returnCode.setMessage(e.toString());
            }
            return returnCode;
        }
    }
    // MOD klliu check file connection is available
    if (analysisDataProvider instanceof FileConnection) {
        FileConnection fileConn = (FileConnection) analysisDataProvider;
        // ADD msjian TDQ-4559 2012-2-28: when the fileconnection is context mode, getOriginalFileConnection.
        if (fileConn.isContextMode()) {
            IRepositoryContextService service = CoreRuntimePlugin.getInstance().getRepositoryContextService();
            if (service != null) {
                fileConn = service.cloneOriginalValueConnection(fileConn);
            }
        }
        // TDQ-4559 ~
        String filePath = fileConn.getFilePath();
        try {
            BufferedReader filePathAvailable = FilesUtils.isFilePathAvailable(filePath, fileConn);
            if (filePathAvailable == null) {
                returnCode.setOk(false);
                // $NON-NLS-1$ //$NON-NLS-2$
                returnCode.setMessage(Messages.getString("ConnectionUtils.checkConnFailTitle") + " " + filePath);
            }
        } catch (UnsupportedEncodingException e) {
            returnCode.setOk(false);
            returnCode.setMessage(e.toString());
        } catch (FileNotFoundException e) {
            returnCode.setOk(false);
            returnCode.setMessage(e.toString());
        } catch (IOException e) {
            returnCode.setOk(false);
            returnCode.setMessage(e.toString());
        }
        return returnCode;
    }
    // ~
    Properties props = new Properties();
    props.put(TaggedValueHelper.USER, JavaSqlFactory.getUsername(analysisDataProvider));
    props.put(TaggedValueHelper.PASSWORD, JavaSqlFactory.getPassword(analysisDataProvider));
    if (analysisDataProvider instanceof DatabaseConnection) {
        // MOD qiongli TDQ-11507,for GeneralJdbc,should check connection too after validation jar and jdbc driver .
        if (isTcompJdbc(analysisDataProvider) || isGeneralJdbc(analysisDataProvider)) {
            ReturnCode rcJdbc = checkJdbcJarFilePathDriverClassName((DatabaseConnection) analysisDataProvider);
            if (!rcJdbc.isOk()) {
                return rcJdbc;
            }
        }
        // MOD qiongli 2014-5-14 in order to check and connect a dbConnection by a correct driver,replace
        // 'ConnectionUtils.checkConnection(...)' with 'managerConn.check(metadataConnection)'.
        ManagerConnection managerConn = new ManagerConnection();
        returnCode.setOk(managerConn.check(metadataConnection));
        returnCode.setMessage(managerConn.getMessageException());
    }
    return returnCode;
}
Also used : ManagerConnection(org.talend.metadata.managment.repository.ManagerConnection) ReturnCode(org.talend.utils.sugars.ReturnCode) SQLException(java.sql.SQLException) FileNotFoundException(java.io.FileNotFoundException) IRepositoryContextService(org.talend.core.IRepositoryContextService) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IMetadataConnection(org.talend.core.model.metadata.IMetadataConnection) IOException(java.io.IOException) Properties(java.util.Properties) BufferedReader(java.io.BufferedReader) DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection) DelimitedFileConnection(org.talend.core.model.metadata.builder.connection.DelimitedFileConnection) FileConnection(org.talend.core.model.metadata.builder.connection.FileConnection)

Example 2 with ManagerConnection

use of org.talend.metadata.managment.repository.ManagerConnection in project tdi-studio-se by Talend.

the class MetadataColumnComparator method createNewRepositoryNode.

/**
     * dev Comment method "createNewRepositoryNode".
     * 
     * @param node
     * @param parameters
     * @param connection
     * @param iMetadataConnection
     * @return
     */
@SuppressWarnings("unchecked")
private RepositoryNode createNewRepositoryNode(RepositoryNode node, ConnectionParameters parameters, DatabaseConnection connection, IMetadataConnection iMetadataConnection) {
    ManagerConnection managerConnection = new ManagerConnection();
    boolean status = managerConnection.check(iMetadataConnection);
    connection.setDivergency(!status);
    ConnectionHelper.getTables(connection).clear();
    if (status) {
    // try {
    // List<MetadataTable> tablesFromDB = ExtractMetaDataFromDataBase.returnMetaTablesFormConnection(
    // iMetadataConnection, 500);
    // ExtractMetaDataUtils.getInstance().setReconnect(false);
    // for (MetadataTable table : tablesFromDB) {
    // List<MetadataColumn> columnsFromDB = new ArrayList<MetadataColumn>();
    // columnsFromDB.addAll(ExtractMetaDataFromDataBase.returnMetadataColumnsFormTable(iMetadataConnection,
    // table.getSourceName()));
    // table.getColumns().clear();
    // for (MetadataColumn column : columnsFromDB) {
    // column.setLabel(""); //$NON-NLS-1$
    // table.getColumns().add(column);
    // }
    // table.setLabel(""); //$NON-NLS-1$
    // ConnectionHelper.getTables(connection).add(table);
    // }
    // ExtractMetaDataUtils.getInstance().setReconnect(true);
    // } catch (Exception e) {
    // if (parameters != null) {
    // parameters.setConnectionComment(e.getMessage());
    // }
    // return null;
    // }
    } else {
        if (parameters != null) {
            parameters.setConnectionComment(managerConnection.getMessageException() == null ? "" : //$NON-NLS-1$
            managerConnection.getMessageException());
        }
    }
    DatabaseConnectionItem item = PropertiesFactory.eINSTANCE.createDatabaseConnectionItem();
    Property connectionProperty = PropertiesFactory.eINSTANCE.createProperty();
    connectionProperty.setAuthor(((RepositoryContext) CorePlugin.getContext().getProperty(Context.REPOSITORY_CONTEXT_KEY)).getUser());
    connectionProperty.setVersion(VersionUtils.DEFAULT_VERSION);
    //$NON-NLS-1$
    connectionProperty.setStatusCode("");
    item.setProperty(connectionProperty);
    // TDI-18665
    updatePackage(iMetadataConnection);
    DatabaseConnection dbConn = (DatabaseConnection) iMetadataConnection.getCurrentConnection();
    item.setConnection(dbConn);
    RepositoryObject object = new RepositoryObject(connectionProperty);
    //$NON-NLS-1$
    object.setLabel("");
    ItemState state = PropertiesFactory.eINSTANCE.createItemState();
    state.setDeleted(false);
    item.setState(state);
    if (node == null) {
        node = new RepositoryNode(null, null, ENodeType.SYSTEM_FOLDER);
    }
    RepositoryNode newNode = new RepositoryNode(object, node, ENodeType.SYSTEM_FOLDER);
    return newNode;
}
Also used : ManagerConnection(org.talend.metadata.managment.repository.ManagerConnection) SqlBuilderRepositoryObject(org.talend.sqlbuilder.dbstructure.SqlBuilderRepositoryObject) MetadataTableRepositoryObject(org.talend.sqlbuilder.dbstructure.DBTreeProvider.MetadataTableRepositoryObject) RepositoryObject(org.talend.core.model.repository.RepositoryObject) MetadataColumnRepositoryObject(org.talend.sqlbuilder.dbstructure.DBTreeProvider.MetadataColumnRepositoryObject) ItemState(org.talend.core.model.properties.ItemState) DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection) RepositoryNode(org.talend.repository.model.RepositoryNode) IRepositoryNode(org.talend.repository.model.IRepositoryNode) Property(org.talend.core.model.properties.Property) DatabaseConnectionItem(org.talend.core.model.properties.DatabaseConnectionItem)

Example 3 with ManagerConnection

use of org.talend.metadata.managment.repository.ManagerConnection in project tdi-studio-se by Talend.

the class MetadataColumnComparator method modifyOldRepositoryNode.

/**
     * dev Comment method "modifyOldRepositoryNode".
     * 
     * @param connection
     * @param iMetadataConnection
     */
@SuppressWarnings("unchecked")
public void modifyOldRepositoryNode(DatabaseConnection connection, IMetadataConnection iMetadataConnection, RepositoryNode oldNode) throws Exception {
    boolean status = new ManagerConnection().check(iMetadataConnection);
    connection.setDivergency(!status);
    if (status) {
        // /Get MetadataTable From DB
        List<MetadataTable> tablesFromDB = ExtractMetaDataFromDataBase.returnMetaTablesFormConnection(iMetadataConnection);
        ExtractMetaDataUtils.getInstance().setReconnect(false);
        // Get MetadataTable From EMF(Old RepositoryNode)
        Set<MetadataTable> tablesetFromEMF = ConnectionHelper.getTables(connection);
        List<MetadataTable> tablesFromEMF = new ArrayList<MetadataTable>();
        tablesFromEMF.addAll(tablesetFromEMF);
        if (oldNode.getProperties(EProperties.CONTENT_TYPE) == RepositoryNodeType.DATABASE) {
            if (tablesFromEMF.size() < tablesFromDB.size()) {
                modifyOldConnection(tablesFromEMF, iMetadataConnection, tablesFromDB, oldNode);
            }
            restoreConnection(connection, tablesFromEMF);
        } else if (oldNode.getProperties(EProperties.CONTENT_TYPE) == RepositoryNodeType.TABLE) {
            MetadataTable metadataTable = ((MetadataTableRepositoryObject) oldNode.getObject()).getTable();
            modifyOldOneTableFromDB(tablesFromDB, metadataTable);
            MetadataTable tableFromDB = null;
            for (MetadataTable table : tablesFromDB) {
                if (table.getSourceName().equals(metadataTable.getSourceName())) {
                    tableFromDB = table;
                }
            }
            if (tableFromDB != null) {
                List<MetadataColumn> columnsFromDB = new ArrayList<MetadataColumn>();
                columnsFromDB.addAll(ExtractMetaDataFromDataBase.returnMetadataColumnsFormTable(iMetadataConnection, tableFromDB.getSourceName()));
                fixedColumns(columnsFromDB, metadataTable.getColumns());
            }
            // for (MetadataColumn metadataColumn : (List<MetadataColumn>) metadataTable.getColumns()) {
            // modifyOneColumnFromDB(iMetadataConnection, tablesFromDB, metadataColumn);
            // }
            restoreConnection(connection, tablesFromEMF);
        } else if (oldNode.getProperties(EProperties.CONTENT_TYPE) == RepositoryNodeType.COLUMN) {
            MetadataColumn metadataColumn = ((MetadataColumnRepositoryObject) oldNode.getObject()).getColumn();
            modifyOneColumnFromDB(iMetadataConnection, tablesFromDB, metadataColumn);
        }
    } else {
        Set<MetadataTable> tableset = ConnectionHelper.getTables(connection);
        List<MetadataTable> tablesFromEMF = new ArrayList<MetadataTable>();
        tablesFromEMF.addAll(tableset);
        for (MetadataTable tableFromEMF : tablesFromEMF) {
            List<MetadataColumn> columnsFromEMF = tableFromEMF.getColumns();
            for (MetadataColumn column : columnsFromEMF) {
                //$NON-NLS-1$
                column.setOriginalField(" ");
                column.setDivergency(true);
                column.setSynchronised(false);
            }
            //$NON-NLS-1$
            tableFromEMF.setSourceName(" ");
            tableFromEMF.setDivergency(true);
        }
    }
    ExtractMetaDataUtils.getInstance().setReconnect(true);
}
Also used : MetadataColumn(org.talend.core.model.metadata.builder.connection.MetadataColumn) ManagerConnection(org.talend.metadata.managment.repository.ManagerConnection) MetadataTable(org.talend.core.model.metadata.builder.connection.MetadataTable) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) MetadataColumnRepositoryObject(org.talend.sqlbuilder.dbstructure.DBTreeProvider.MetadataColumnRepositoryObject)

Example 4 with ManagerConnection

use of org.talend.metadata.managment.repository.ManagerConnection in project tdi-studio-se by Talend.

the class UIUtils method checkConnection.

/**
     * Display a error message if the db connection is failed DOC Comment method "checkConnection".
     * 
     * @param parentShell
     * @param imetadataConnection
     */
public static void checkConnection(final Shell parentShell, IMetadataConnection imetadataConnection) {
    // display a error message if the db connection is failed.
    final ManagerConnection managerConnection = new ManagerConnection();
    managerConnection.check(imetadataConnection, true);
    if (!managerConnection.getIsValide()) {
        Display.getDefault().syncExec(new Runnable() {

            @Override
            public void run() {
                new ErrorDialogWidthDetailArea(parentShell, SqlBuilderPlugin.PLUGIN_ID, Messages.getString(//$NON-NLS-1$
                "UIUtils.DBConnectionFailure"), managerConnection.getMessageException());
            }
        });
    }
}
Also used : ManagerConnection(org.talend.metadata.managment.repository.ManagerConnection) ErrorDialogWidthDetailArea(org.talend.commons.ui.swt.dialogs.ErrorDialogWidthDetailArea)

Aggregations

ManagerConnection (org.talend.metadata.managment.repository.ManagerConnection)4 DatabaseConnection (org.talend.core.model.metadata.builder.connection.DatabaseConnection)2 MetadataColumnRepositoryObject (org.talend.sqlbuilder.dbstructure.DBTreeProvider.MetadataColumnRepositoryObject)2 BufferedReader (java.io.BufferedReader)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Properties (java.util.Properties)1 ErrorDialogWidthDetailArea (org.talend.commons.ui.swt.dialogs.ErrorDialogWidthDetailArea)1 IRepositoryContextService (org.talend.core.IRepositoryContextService)1 IMetadataConnection (org.talend.core.model.metadata.IMetadataConnection)1 DelimitedFileConnection (org.talend.core.model.metadata.builder.connection.DelimitedFileConnection)1 FileConnection (org.talend.core.model.metadata.builder.connection.FileConnection)1 MetadataColumn (org.talend.core.model.metadata.builder.connection.MetadataColumn)1 MetadataTable (org.talend.core.model.metadata.builder.connection.MetadataTable)1 DatabaseConnectionItem (org.talend.core.model.properties.DatabaseConnectionItem)1 ItemState (org.talend.core.model.properties.ItemState)1