Search in sources :

Example 11 with IMetadataConnection

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

the class DbTableController method openDbTableSelectorJob.

/**
     * qzhang Comment method "openDbTableSelectorJob".
     * 
     * @param openListTable
     */
private void openDbTableSelectorJob(final Button openListTable) {
    Job job = new //$NON-NLS-1$
    Job(//$NON-NLS-1$
    Messages.getString("DbTableController.openSelectionDialog")) {

        @Override
        protected IStatus run(final IProgressMonitor monitor) {
            //$NON-NLS-1$
            monitor.beginTask(Messages.getString("DbTableController.waitingForOpen"), IProgressMonitor.UNKNOWN);
            DatabaseConnection existConnection = getExistConnection();
            if (existConnection == null) {
                if (connParameters == null) {
                    initConnectionParameters();
                }
                existConnection = TracesConnectionUtils.createConnection(connParameters);
            }
            final DatabaseConnection con = existConnection;
            IMetadataConnection iMetadataConnection = null;
            final IMetadataConnection[] iMetadata = new IMetadataConnection[1];
            boolean isStatus = false;
            if (existConnection != null) {
                Display.getDefault().syncExec(new Runnable() {

                    @Override
                    public void run() {
                        IMetadataConnection convert = ConvertionHelper.convert(con);
                        iMetadata[0] = convert;
                    }
                });
                iMetadataConnection = iMetadata[0];
                // Added by Marvin Wang for bug TDI-24288.
                if (EDatabaseTypeName.HIVE.getProduct().equalsIgnoreCase(con.getDatabaseType())) {
                    if (EDatabaseVersion4Drivers.HIVE_EMBEDDED.getVersionValue().equalsIgnoreCase(con.getDbVersionString())) {
                        try {
                            HiveConnectionManager.getInstance().checkConnection(iMetadataConnection);
                            isStatus = true;
                        } catch (ClassNotFoundException e) {
                            isStatus = false;
                            ExceptionHandler.process(e);
                        } catch (InstantiationException e) {
                            isStatus = false;
                            ExceptionHandler.process(e);
                        } catch (IllegalAccessException e) {
                            isStatus = false;
                            ExceptionHandler.process(e);
                        } catch (SQLException e) {
                            isStatus = false;
                            ExceptionHandler.process(e);
                        }
                    }
                } else {
                    isStatus = checkConnection(iMetadataConnection);
                }
            }
            String type = null;
            if (iMetadataConnection != null) {
                type = iMetadataConnection.getDbType();
            }
            final String dbType = type;
            if (!monitor.isCanceled()) {
                try {
                    if (isStatus) {
                        // Added by Marvin Wang for bug TDI-24288.
                        if (EDatabaseTypeName.HIVE.getProduct().equalsIgnoreCase(con.getDatabaseType())) {
                            if (EDatabaseVersion4Drivers.HIVE_EMBEDDED.getVersionValue().equalsIgnoreCase(con.getDbVersionString())) {
                                JavaSqlFactory.doHivePreSetup((DatabaseConnection) iMetadataConnection.getCurrentConnection());
                                returnTablesFormConnection = ExtractMetaDataFromDataBase.fetchAllTablesForHiveEmbeddedModel(iMetadataConnection);
                                JavaSqlFactory.doHiveConfigurationClear();
                            }
                        } else {
                            returnTablesFormConnection = ExtractMetaDataFromDataBase.returnTablesFormConnection(iMetadataConnection);
                        }
                        Display.getDefault().asyncExec(new Runnable() {

                            @Override
                            public void run() {
                                final DbTableSelectorObject object = new DbTableSelectorObject();
                                DbTableSelectorObject connO = new DbTableSelectorObject();
                                if (dbType != null && dbType.equals(EDatabaseTypeName.ORACLE_OCI.getDisplayName())) {
                                    connO.setLabel(connParameters.getLocalServiceName());
                                } else if ("".equals(connParameters.getDbName())) {
                                    //$NON-NLS-1$
                                    connO.setLabel(connParameters.getDatasource());
                                } else {
                                    connO.setLabel(connParameters.getDbName());
                                }
                                // ...
                                if (connO.getLabel() == null || connO.getLabel().equals("")) {
                                    //$NON-NLS-1$
                                    if (elem instanceof INode) {
                                        connO.setLabel(elem.getElementName());
                                    } else {
                                        connO.setLabel("tJDBCConnection");
                                    }
                                }
                                connO.setType(ObjectType.DB);
                                if (monitor.isCanceled()) {
                                    monitor.done();
                                    return;
                                }
                                for (String string : returnTablesFormConnection) {
                                    DbTableSelectorObject tableO = new DbTableSelectorObject();
                                    tableO.setLabel(string);
                                    tableO.setType(ObjectType.TABLE);
                                    connO.addChildren(tableO);
                                }
                                object.addChildren(connO);
                                String propertyName = (String) openListTable.getData(PARAMETER_NAME);
                                DbTableSelectorDialog selectorDialog = new DbTableSelectorDialog(composite.getShell(), object);
                                if (selectorDialog.open() == DbTableSelectorDialog.OK) {
                                    String name = selectorDialog.getSelectName();
                                    if (name != null) {
                                        Command dbSelectorCommand = new PropertyChangeCommand(elem, propertyName, TalendTextUtils.addQuotes(name));
                                        executeCommand(dbSelectorCommand);
                                        Text labelText = (Text) hashCurControls.get(propertyName);
                                        labelText.setText(TalendTextUtils.addQuotes(name));
                                    }
                                }
                            }
                        });
                    } else {
                        Display.getDefault().asyncExec(new Runnable() {

                            @Override
                            public void run() {
                                //$NON-NLS-1$
                                String pid = "org.talend.sqlbuilder";
                                //$NON-NLS-1$
                                String mainMsg = "Database connection is failed. ";
                                ErrorDialogWithDetailAreaAndContinueButton dialog = new ErrorDialogWithDetailAreaAndContinueButton(composite.getShell(), pid, mainMsg, connParameters.getConnectionComment());
                                if (dialog.getCodeOfButton() == Window.OK) {
                                    openParamemerDialog(openListTable, part.getProcess().getContextManager());
                                }
                            }
                        });
                    }
                } catch (SQLException e) {
                    // Added by Marvin Wang for bug TDI-24288.
                    JavaSqlFactory.doHiveConfigurationClear();
                    ExceptionHandler.process(e);
                    Display.getDefault().asyncExec(new Runnable() {

                        @Override
                        public void run() {
                            MessageDialog.openError(openListTable.getShell(), //$NON-NLS-1$
                            Messages.getString("DbTableController.dialog.title"), //$NON-NLS-1$
                            Messages.getString("DbTableController.dialog.contents.fetchTableFailed"));
                        }
                    });
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            monitor.done();
            return Status.OK_STATUS;
        }
    };
    if (part != null) {
        IWorkbenchSiteProgressService siteps = (IWorkbenchSiteProgressService) part.getSite().getAdapter(IWorkbenchSiteProgressService.class);
        siteps.showInDialog(composite.getShell(), job);
    } else {
        PlatformUI.getWorkbench().getProgressService().showInDialog(composite.getShell(), job);
    }
    job.setUser(true);
    job.schedule();
}
Also used : ErrorDialogWithDetailAreaAndContinueButton(org.talend.commons.ui.swt.dialogs.ErrorDialogWithDetailAreaAndContinueButton) INode(org.talend.core.model.process.INode) SQLException(java.sql.SQLException) IMetadataConnection(org.talend.core.model.metadata.IMetadataConnection) Text(org.eclipse.swt.widgets.Text) DbTableSelectorDialog(org.talend.core.ui.metadata.dialog.DbTableSelectorDialog) DbTableSelectorObject(org.talend.core.ui.metadata.dialog.DbTableSelectorObject) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) PropertyChangeCommand(org.talend.designer.core.ui.editor.cmd.PropertyChangeCommand) IWorkbenchSiteProgressService(org.eclipse.ui.progress.IWorkbenchSiteProgressService) Command(org.eclipse.gef.commands.Command) PropertyChangeCommand(org.talend.designer.core.ui.editor.cmd.PropertyChangeCommand) DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection) Job(org.eclipse.core.runtime.jobs.Job)

Example 12 with IMetadataConnection

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

the class ReadQueriesAction method doRun.

@Override
protected void doRun() {
    IStructuredSelection selection = (IStructuredSelection) getSelection();
    IProxyRepositoryFactory factory = ProxyRepositoryFactory.getInstance();
    if (repositoryNode == null && selection != null) {
        repositoryNode = (RepositoryNode) selection.getFirstElement();
    }
    DatabaseConnectionItem dbConnectionItem = null;
    ConnectionParameters connParameters = new ConnectionParameters();
    if (repositoryNode.getObjectType() == ERepositoryObjectType.METADATA_CONNECTIONS) {
        dbConnectionItem = (DatabaseConnectionItem) repositoryNode.getObject().getProperty().getItem();
        connParameters.setRepositoryName(repositoryNode.getObject().getLabel());
        connParameters.setRepositoryId(repositoryNode.getObject().getId());
        //$NON-NLS-1$
        connParameters.setQuery("");
    } else if (repositoryNode.getObjectType() == ERepositoryObjectType.METADATA_CON_QUERY) {
        QueryRepositoryObject queryRepositoryObject = (QueryRepositoryObject) repositoryNode.getObject();
        dbConnectionItem = (DatabaseConnectionItem) queryRepositoryObject.getProperty().getItem();
        connParameters.setRepositoryName(dbConnectionItem.getProperty().getLabel());
        connParameters.setRepositoryId(dbConnectionItem.getProperty().getId());
        connParameters.setQueryObject(queryRepositoryObject.getQuery());
        connParameters.setQuery(queryRepositoryObject.getQuery().getValue());
        // first open Sql Builder,set true
        connParameters.setFirstOpenSqlBuilder(true);
    }
    Display display = Display.getCurrent();
    if (display == null) {
        display = Display.getDefault();
    }
    Shell parentShell = new Shell(display);
    TextUtil.setDialogTitle(TextUtil.SQL_BUILDER_TITLE_REP);
    Connection connection = dbConnectionItem.getConnection();
    String selectedContext = null;
    if (connection.isContextMode()) {
        ContextItem contextItem = ContextUtils.getContextItemById2(connection.getContextId());
        if (contextItem != null && connection.isContextMode()) {
            ContextSetsSelectionDialog setsDialog = new ContextSetsSelectionDialog(null, contextItem, false);
            setsDialog.open();
            selectedContext = setsDialog.getSelectedContext();
        }
    }
    SQLBuilderDialog dial = new SQLBuilderDialog(parentShell, repositoryNode, selectedContext);
    dial.setReadOnly(true);
    if (connection instanceof DatabaseConnection) {
        IMetadataConnection imetadataConnection = ConvertionHelper.convert(connection, true);
        connParameters.setSchema(imetadataConnection.getSchema() == null ? "" : imetadataConnection.getSchema());
        UIUtils.checkConnection(parentShell, imetadataConnection);
    }
    connParameters.setNodeReadOnly(true);
    connParameters.setFromRepository(true);
    dial.setConnParameters(connParameters);
    dial.open();
    refresh(repositoryNode);
}
Also used : ContextItem(org.talend.core.model.properties.ContextItem) ConnectionParameters(org.talend.core.sqlbuilder.util.ConnectionParameters) DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection) IMetadataConnection(org.talend.core.model.metadata.IMetadataConnection) Connection(org.talend.core.model.metadata.builder.connection.Connection) IMetadataConnection(org.talend.core.model.metadata.IMetadataConnection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) DatabaseConnectionItem(org.talend.core.model.properties.DatabaseConnectionItem) QueryRepositoryObject(org.talend.core.repository.model.repositoryObject.QueryRepositoryObject) Shell(org.eclipse.swt.widgets.Shell) ContextSetsSelectionDialog(org.talend.metadata.managment.ui.wizard.metadata.ContextSetsSelectionDialog) DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection) SQLBuilderDialog(org.talend.sqlbuilder.ui.SQLBuilderDialog) IProxyRepositoryFactory(org.talend.repository.model.IProxyRepositoryFactory) Display(org.eclipse.swt.widgets.Display)

Example 13 with IMetadataConnection

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

the class EditQueriesAction method doRun.

@Override
protected void doRun() {
    IStructuredSelection selection = (IStructuredSelection) getSelection();
    if (repositoryNode == null && selection != null) {
        repositoryNode = (RepositoryNode) selection.getFirstElement();
    }
    IProxyRepositoryFactory factory = ProxyRepositoryFactory.getInstance();
    IRepositoryViewObject nodeObject = repositoryNode.getObject();
    boolean locked = false;
    if (!factory.getRepositoryContext().isEditableAsReadOnly()) {
        if (nodeObject.getRepositoryStatus() == ERepositoryStatus.LOCK_BY_OTHER) {
            locked = true;
        }
    }
    // Avoid to delete node which is locked.
    if (locked || RepositoryManager.isOpenedItemInEditor(nodeObject)) {
        final String title = "Impossible to edit queries";
        String nodeName = nodeObject.getRepositoryObjectType().getLabel();
        final String message = "item is already locked by another user.";
        Display.getDefault().syncExec(new Runnable() {

            @Override
            public void run() {
                MessageDialog dialog = new MessageDialog(new Shell(), title, null, message, MessageDialog.ERROR, new String[] { IDialogConstants.OK_LABEL }, 0);
                dialog.open();
            }
        });
        return;
    }
    DatabaseConnectionItem dbConnectionItem = null;
    boolean readOnly = false;
    ConnectionParameters connParameters = new ConnectionParameters();
    if (repositoryNode.getObjectType() == ERepositoryObjectType.METADATA_CONNECTIONS) {
        dbConnectionItem = (DatabaseConnectionItem) repositoryNode.getObject().getProperty().getItem();
        connParameters.setRepositoryName(repositoryNode.getObject().getLabel());
        connParameters.setRepositoryId(repositoryNode.getObject().getId());
        //$NON-NLS-1$
        connParameters.setQuery("");
    } else if (repositoryNode.getObjectType() == ERepositoryObjectType.METADATA_CON_QUERY) {
        QueryRepositoryObject queryRepositoryObject = (QueryRepositoryObject) repositoryNode.getObject();
        readOnly = SubItemHelper.isDeleted(queryRepositoryObject.getAbstractMetadataObject());
        dbConnectionItem = (DatabaseConnectionItem) queryRepositoryObject.getProperty().getItem();
        connParameters.setRepositoryName(dbConnectionItem.getProperty().getLabel());
        connParameters.setRepositoryId(dbConnectionItem.getProperty().getId());
        connParameters.setQueryObject(queryRepositoryObject.getQuery());
        connParameters.setQuery(queryRepositoryObject.getQuery().getValue());
        // first open Sql Builder,set true
        connParameters.setFirstOpenSqlBuilder(true);
    } else if (repositoryNode.getObjectType() == ERepositoryObjectType.METADATA_CON_TABLE) {
        dbConnectionItem = (DatabaseConnectionItem) repositoryNode.getObject().getProperty().getItem();
        connParameters.setRepositoryName(dbConnectionItem.getProperty().getLabel());
        connParameters.setRepositoryId(dbConnectionItem.getProperty().getId());
        connParameters.setMetadataTable((MetadataTableRepositoryObject) repositoryNode.getObject());
        //$NON-NLS-1$
        connParameters.setQuery("");
    }
    IRepositoryView viewPart = getViewPart();
    Display display = null;
    if (viewPart != null) {
        display = viewPart.getViewer().getControl().getDisplay();
    } else {
        display = Display.getCurrent();
        if (display == null) {
            display = Display.getDefault();
        }
    }
    Shell parentShell = new Shell(display);
    TextUtil.setDialogTitle(TextUtil.SQL_BUILDER_TITLE_REP);
    Connection connection = dbConnectionItem.getConnection();
    String selectedContext = null;
    if (connection.isContextMode()) {
        ContextItem contextItem = ContextUtils.getContextItemById2(connection.getContextId());
        if (contextItem != null && connection.isContextMode()) {
            ContextSetsSelectionDialog setsDialog = new ContextSetsSelectionDialog(null, contextItem, false);
            setsDialog.open();
            selectedContext = setsDialog.getSelectedContext();
        }
    }
    SQLBuilderDialog dial = new SQLBuilderDialog(parentShell, repositoryNode, selectedContext);
    dial.setReadOnly(readOnly);
    if (connection instanceof DatabaseConnection) {
        IMetadataConnection imetadataConnection = ConvertionHelper.convert(connection, true);
        connParameters.setSchema(imetadataConnection.getSchema() == null ? "" : imetadataConnection.getSchema());
        UIUtils.checkConnection(parentShell, imetadataConnection);
    }
    connParameters.setNodeReadOnly(readOnly);
    connParameters.setFromRepository(true);
    dial.setConnParameters(connParameters);
    dial.open();
    IRepositoryView view = getViewPart();
    if (view != null) {
        view.refreshView();
    }
}
Also used : ContextItem(org.talend.core.model.properties.ContextItem) ConnectionParameters(org.talend.core.sqlbuilder.util.ConnectionParameters) DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection) IMetadataConnection(org.talend.core.model.metadata.IMetadataConnection) Connection(org.talend.core.model.metadata.builder.connection.Connection) IMetadataConnection(org.talend.core.model.metadata.IMetadataConnection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IRepositoryView(org.talend.repository.ui.views.IRepositoryView) DatabaseConnectionItem(org.talend.core.model.properties.DatabaseConnectionItem) Shell(org.eclipse.swt.widgets.Shell) QueryRepositoryObject(org.talend.core.repository.model.repositoryObject.QueryRepositoryObject) ContextSetsSelectionDialog(org.talend.metadata.managment.ui.wizard.metadata.ContextSetsSelectionDialog) IRepositoryViewObject(org.talend.core.model.repository.IRepositoryViewObject) DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection) MessageDialog(org.eclipse.jface.dialogs.MessageDialog) SQLBuilderDialog(org.talend.sqlbuilder.ui.SQLBuilderDialog) IProxyRepositoryFactory(org.talend.repository.model.IProxyRepositoryFactory) Display(org.eclipse.swt.widgets.Display)

Example 14 with IMetadataConnection

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

the class SQLBuilderService method openSQLBuilderDialog.

public Dialog openSQLBuilderDialog(Shell parentShell, String processName, ConnectionParameters connParameters) {
    SQLBuilderDialog sqlBuilder = new SQLBuilderDialog(parentShell);
    UIUtils.addSqlBuilderDialog(processName, sqlBuilder);
    sqlBuilder.setConnParameters(connParameters);
    // display a error message if the db connection is failed.
    if (connParameters != null) {
        DatabaseConnection connection = createConnection(connParameters);
        if (connection != null) {
            IMetadataConnection metadataConnection = ConvertionHelper.convert(connection);
            UIUtils.checkConnection(parentShell, metadataConnection);
        }
    }
    return sqlBuilder;
}
Also used : DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection) IMetadataConnection(org.talend.core.model.metadata.IMetadataConnection) SQLBuilderDialog(org.talend.sqlbuilder.ui.SQLBuilderDialog)

Example 15 with IMetadataConnection

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

the class DBTreeProvider method getChildren.

//$NON-NLS-1$
@SuppressWarnings("static-access")
public Object[] getChildren(Object parentElement) {
    RepositoryNode repositoryNode = (RepositoryNode) parentElement;
    RepositoryNode rootNode = repositoryNodeManager.getRoot(repositoryNode);
    boolean isBuildIn = ((SqlBuilderRepositoryObject) rootNode.getObject()).isBuildIn();
    if (isRefresh) {
        refreshRootNode(rootNode);
        rootNode.getChildren().clear();
        DatabaseConnection metadataConnection = (DatabaseConnection) ((ConnectionItem) rootNode.getObject().getProperty().getItem()).getConnection();
        createTables(rootNode, rootNode.getObject(), metadataConnection, isBuildIn);
        createQueries(rootNode, rootNode.getObject(), metadataConnection, isBuildIn);
        isRefresh = false;
        return repositoryNode.getChildren().toArray();
    } else {
        // if intialized already > same as now, retrieve the childrens
        if (!repositoryNode.isInitialized()) {
            // retrieve columns of the specified table
            RepositoryNodeType type = (RepositoryNodeType) repositoryNode.getProperties(EProperties.CONTENT_TYPE);
            if (type == RepositoryNodeType.TABLE) {
                try {
                    MetadataTable table = ((MetadataTableRepositoryObject) repositoryNode.getObject()).getTable();
                    DatabaseConnectionItem connItem = repositoryNodeManager.getItem(repositoryNode);
                    DatabaseConnection dbConn = (DatabaseConnection) connItem.getConnection();
                    IMetadataConnection iMetadataConnection = ConvertionHelper.convert(dbConn);
                    repositoryNode.getChildren().clear();
                    repositoryNodeManager.modifyOldRepositoryNode(dbConn, iMetadataConnection, repositoryNode);
                    createColumns(repositoryNode, rootNode.getObject(), table, isBuildIn);
                    // set node intialized to true.
                    repositoryNode.setInitialized(true);
                } catch (Exception e) {
                    ExceptionHandler.process(e);
                }
            }
        }
        return repositoryNode.getChildren().toArray();
    }
}
Also used : MetadataTable(org.talend.core.model.metadata.builder.connection.MetadataTable) DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection) IMetadataConnection(org.talend.core.model.metadata.IMetadataConnection) RepositoryNode(org.talend.repository.model.RepositoryNode) BusinessException(org.talend.commons.exception.BusinessException) PersistenceException(org.talend.commons.exception.PersistenceException) DatabaseConnectionItem(org.talend.core.model.properties.DatabaseConnectionItem)

Aggregations

IMetadataConnection (org.talend.core.model.metadata.IMetadataConnection)19 DatabaseConnection (org.talend.core.model.metadata.builder.connection.DatabaseConnection)17 ArrayList (java.util.ArrayList)9 SQLException (java.sql.SQLException)8 List (java.util.List)6 Connection (org.talend.core.model.metadata.builder.connection.Connection)5 DatabaseConnectionItem (org.talend.core.model.properties.DatabaseConnectionItem)5 Shell (org.eclipse.swt.widgets.Shell)4 PersistenceException (org.talend.commons.exception.PersistenceException)4 ErrorDialogWithDetailAreaAndContinueButton (org.talend.commons.ui.swt.dialogs.ErrorDialogWithDetailAreaAndContinueButton)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)3 MetadataColumn (org.talend.core.model.metadata.builder.connection.MetadataColumn)3 MetadataTable (org.talend.core.model.metadata.builder.connection.MetadataTable)3 SQLBuilderDialog (org.talend.sqlbuilder.ui.SQLBuilderDialog)3 ResultSet (java.sql.ResultSet)2 ResultSetMetaData (java.sql.ResultSetMetaData)2 ProgressMonitorDialog (org.eclipse.jface.dialogs.ProgressMonitorDialog)2 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)2 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)2