Search in sources :

Example 1 with DBSInstanceLazy

use of org.jkiss.dbeaver.model.struct.DBSInstanceLazy in project dbeaver by serge-rider.

the class NavigatorHandlerObjectCreateBase method createNewObject.

protected boolean createNewObject(final IWorkbenchWindow workbenchWindow, DBNNode element, @Nullable Class<?> newObjectType, DBNDatabaseNode copyFrom, boolean isFolder) {
    try {
        DBNNode container = null;
        if (isFolder || (element instanceof DBNContainer && !(element instanceof DBNDataSource))) {
            container = element;
        } else {
            DBNNode parentNode = element.getParentNode();
            if (parentNode instanceof DBNContainer) {
                container = parentNode;
            }
        }
        if (container == null) {
            throw new DBException("Can't detect container for '" + element.getNodeName() + "'");
        }
        if (container instanceof DBNDatabaseNode && ObjectPropertyTester.isMetadataChangeDisabled((DBNDatabaseNode) container)) {
            throw new DBException("Object create not available in simple view mode");
        }
        if (newObjectType == null) {
            Class<?> childType = container instanceof DBNContainer ? ((DBNContainer) container).getChildrenClass() : null;
            if (childType == null) {
                throw new DBException("Can't determine child element type for container '" + container + "'");
            }
            newObjectType = childType;
        }
        if (newObjectType == IProject.class) {
            return NavigatorHandlerProjectCreate.createNewProject(workbenchWindow);
        }
        DBSObject sourceObject = copyFrom == null ? null : copyFrom.getObject();
        final Object parentObject;
        if (container instanceof DBNDatabaseNode) {
            parentObject = ((DBNDatabaseNode) container).getValueObject();
        } else if (container instanceof DBNProject) {
            parentObject = ((DBNProject) container).getProject();
        } else if (container instanceof DBNProjectDatabases) {
            parentObject = container.getOwnerProject();
        } else if (container instanceof DBNLocalFolder) {
            parentObject = ((DBNLocalFolder) container).getFolder();
        } else {
            parentObject = null;
        }
        // Check that child nodes are read an cached
        if (container.hasChildren(false) || parentObject instanceof DBSInstanceLazy) {
            try {
                DBNNode finalContainer = container;
                UIUtils.runInProgressService(monitor -> {
                    try {
                        if (finalContainer.hasChildren(false)) {
                            finalContainer.getChildren(monitor);
                        }
                        if (parentObject instanceof DBSInstanceLazy) {
                            ((DBSInstanceLazy) parentObject).checkInstanceConnection(monitor);
                        }
                    } catch (DBException e) {
                        throw new InvocationTargetException(e);
                    }
                });
            } catch (InvocationTargetException e) {
                DBWorkbench.getPlatformUI().showError("New object", "Error creating new object", e);
            } catch (InterruptedException e) {
            // ignore
            }
        }
        DBEObjectManager<?> objectManager = DBWorkbench.getPlatform().getEditorsRegistry().getObjectManager(newObjectType);
        if (objectManager == null) {
            throw new DBException("Object manager not found for type '" + newObjectType.getName() + "'");
        }
        DBEObjectMaker objectMaker = (DBEObjectMaker) objectManager;
        DBPDataSource dataSource = container instanceof DBNDatabaseNode ? ((DBNDatabaseNode) container).getDataSource() : null;
        final boolean openEditor = dataSource != null && (objectMaker.getMakerOptions(dataSource) & DBEObjectMaker.FEATURE_EDITOR_ON_CREATE) != 0;
        CommandTarget commandTarget = getCommandTarget(workbenchWindow, container, newObjectType, openEditor);
        // Parent is model object - not node
        Map<String, Object> options = new HashMap<>();
        options.put(DBEObjectMaker.OPTION_CONTAINER, container);
        options.put(DBEObjectMaker.OPTION_OBJECT_TYPE, newObjectType);
        createDatabaseObject(commandTarget, objectMaker, parentObject instanceof DBPObject ? (DBPObject) parentObject : null, sourceObject, options);
    } catch (Throwable e) {
        DBWorkbench.getPlatformUI().showError("Create object", null, e);
        return false;
    }
    return true;
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBPObject(org.jkiss.dbeaver.model.DBPObject) HashMap(java.util.HashMap) DBEObjectMaker(org.jkiss.dbeaver.model.edit.DBEObjectMaker) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBPScriptObject(org.jkiss.dbeaver.model.DBPScriptObject) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBPRefreshableObject(org.jkiss.dbeaver.model.DBPRefreshableObject) DBPObject(org.jkiss.dbeaver.model.DBPObject) DBSInstanceLazy(org.jkiss.dbeaver.model.struct.DBSInstanceLazy)

Example 2 with DBSInstanceLazy

use of org.jkiss.dbeaver.model.struct.DBSInstanceLazy in project dbeaver by dbeaver.

the class NavigatorHandlerObjectCreateBase method createNewObject.

protected boolean createNewObject(final IWorkbenchWindow workbenchWindow, DBNNode element, @Nullable Class<?> newObjectType, DBNDatabaseNode copyFrom, boolean isFolder) {
    try {
        DBNNode container = null;
        if (isFolder || (element instanceof DBNContainer && !(element instanceof DBNDataSource))) {
            container = element;
        } else {
            DBNNode parentNode = element.getParentNode();
            if (parentNode instanceof DBNContainer) {
                container = parentNode;
            }
        }
        if (container == null) {
            throw new DBException("Can't detect container for '" + element.getNodeName() + "'");
        }
        if (container instanceof DBNDatabaseNode && ObjectPropertyTester.isMetadataChangeDisabled((DBNDatabaseNode) container)) {
            throw new DBException("Object create not available in simple view mode");
        }
        if (newObjectType == null) {
            Class<?> childType = container instanceof DBNContainer ? ((DBNContainer) container).getChildrenClass() : null;
            if (childType == null) {
                throw new DBException("Can't determine child element type for container '" + container + "'");
            }
            newObjectType = childType;
        }
        if (newObjectType == IProject.class) {
            return NavigatorHandlerProjectCreate.createNewProject(workbenchWindow);
        }
        DBSObject sourceObject = copyFrom == null ? null : copyFrom.getObject();
        final Object parentObject;
        if (container instanceof DBNDatabaseNode) {
            parentObject = ((DBNDatabaseNode) container).getValueObject();
        } else if (container instanceof DBNProject) {
            parentObject = ((DBNProject) container).getProject();
        } else if (container instanceof DBNProjectDatabases) {
            parentObject = container.getOwnerProject();
        } else if (container instanceof DBNLocalFolder) {
            parentObject = ((DBNLocalFolder) container).getFolder();
        } else {
            parentObject = null;
        }
        // Check that child nodes are read an cached
        if (container.hasChildren(false) || parentObject instanceof DBSInstanceLazy) {
            try {
                DBNNode finalContainer = container;
                UIUtils.runInProgressService(monitor -> {
                    try {
                        if (finalContainer.hasChildren(false)) {
                            finalContainer.getChildren(monitor);
                        }
                        if (parentObject instanceof DBSInstanceLazy) {
                            ((DBSInstanceLazy) parentObject).checkInstanceConnection(monitor);
                        }
                    } catch (DBException e) {
                        throw new InvocationTargetException(e);
                    }
                });
            } catch (InvocationTargetException e) {
                DBWorkbench.getPlatformUI().showError("New object", "Error creating new object", e);
            } catch (InterruptedException e) {
            // ignore
            }
        }
        DBEObjectManager<?> objectManager = DBWorkbench.getPlatform().getEditorsRegistry().getObjectManager(newObjectType);
        if (objectManager == null) {
            throw new DBException("Object manager not found for type '" + newObjectType.getName() + "'");
        }
        DBEObjectMaker objectMaker = (DBEObjectMaker) objectManager;
        DBPDataSource dataSource = container instanceof DBNDatabaseNode ? ((DBNDatabaseNode) container).getDataSource() : null;
        final boolean openEditor = dataSource != null && (objectMaker.getMakerOptions(dataSource) & DBEObjectMaker.FEATURE_EDITOR_ON_CREATE) != 0;
        CommandTarget commandTarget = getCommandTarget(workbenchWindow, container, newObjectType, openEditor);
        // Parent is model object - not node
        Map<String, Object> options = new HashMap<>();
        options.put(DBEObjectMaker.OPTION_CONTAINER, container);
        options.put(DBEObjectMaker.OPTION_OBJECT_TYPE, newObjectType);
        createDatabaseObject(commandTarget, objectMaker, parentObject instanceof DBPObject ? (DBPObject) parentObject : null, sourceObject, options);
    } catch (Throwable e) {
        DBWorkbench.getPlatformUI().showError("Create object", null, e);
        return false;
    }
    return true;
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBPObject(org.jkiss.dbeaver.model.DBPObject) HashMap(java.util.HashMap) DBEObjectMaker(org.jkiss.dbeaver.model.edit.DBEObjectMaker) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBPScriptObject(org.jkiss.dbeaver.model.DBPScriptObject) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBPRefreshableObject(org.jkiss.dbeaver.model.DBPRefreshableObject) DBPObject(org.jkiss.dbeaver.model.DBPObject) DBSInstanceLazy(org.jkiss.dbeaver.model.struct.DBSInstanceLazy)

Aggregations

InvocationTargetException (java.lang.reflect.InvocationTargetException)2 HashMap (java.util.HashMap)2 DBException (org.jkiss.dbeaver.DBException)2 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)2 DBPObject (org.jkiss.dbeaver.model.DBPObject)2 DBPRefreshableObject (org.jkiss.dbeaver.model.DBPRefreshableObject)2 DBPScriptObject (org.jkiss.dbeaver.model.DBPScriptObject)2 DBEObjectMaker (org.jkiss.dbeaver.model.edit.DBEObjectMaker)2 DBSInstanceLazy (org.jkiss.dbeaver.model.struct.DBSInstanceLazy)2 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)2