Search in sources :

Example 11 with DBEObjectMaker

use of org.jkiss.dbeaver.model.edit.DBEObjectMaker 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 12 with DBEObjectMaker

use of org.jkiss.dbeaver.model.edit.DBEObjectMaker in project dbeaver by serge-rider.

the class NavigatorObjectsDeleter method of.

static NavigatorObjectsDeleter of(final List<Object> selection, final IWorkbenchWindow window) {
    DBPDataSource dataSource = null;
    boolean hasNodesFromDifferentDataSources = false;
    boolean showCascade = false;
    boolean showViewScript = false;
    boolean showKeepContents = false;
    for (Object obj : selection) {
        if (obj instanceof DBNProject) {
            showKeepContents = true;
            continue;
        }
        if (!(obj instanceof DBNDatabaseNode)) {
            continue;
        }
        final DBNDatabaseNode node = (DBNDatabaseNode) obj;
        final DBPDataSource currentDatasource = node.getDataSource();
        if (dataSource == null) {
            dataSource = currentDatasource;
        } else if (!dataSource.equals(currentDatasource)) {
            hasNodesFromDifferentDataSources = true;
        }
        if (!(node.getParentNode() instanceof DBNContainer)) {
            continue;
        }
        final DBSObject object = node.getObject();
        if (object == null) {
            continue;
        }
        final DBEObjectMaker objectMaker = DBWorkbench.getPlatform().getEditorsRegistry().getObjectManager(object.getClass(), DBEObjectMaker.class);
        if (objectMaker == null) {
            continue;
        }
        showCascade |= (objectMaker.getMakerOptions(object.getDataSource()) & DBEObjectMaker.FEATURE_DELETE_CASCADE) != 0;
        final NavigatorHandlerObjectBase.CommandTarget commandTarget;
        try {
            commandTarget = NavigatorHandlerObjectBase.getCommandTarget(window, node.getParentNode(), object.getClass(), false);
        } catch (DBException e) {
            continue;
        }
        if (object.isPersisted() && commandTarget.getEditor() == null && commandTarget.getContext() != null) {
            showViewScript = true;
        }
    }
    return new NavigatorObjectsDeleter(selection, window, hasNodesFromDifferentDataSources, showCascade, showViewScript, showKeepContents);
}
Also used : DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBException(org.jkiss.dbeaver.DBException) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) DBEObjectMaker(org.jkiss.dbeaver.model.edit.DBEObjectMaker)

Example 13 with DBEObjectMaker

use of org.jkiss.dbeaver.model.edit.DBEObjectMaker in project dbeaver by serge-rider.

the class PropertySourceEditable method setPropertyValue.

@Override
@SuppressWarnings("unchecked")
public void setPropertyValue(@Nullable DBRProgressMonitor monitor, Object editableValue, ObjectPropertyDescriptor prop, Object newValue) throws IllegalArgumentException {
    if (prop.getValueTransformer() != null) {
        newValue = prop.getValueTransformer().transform(editableValue, newValue);
    }
    final Object oldValue = getPropertyValue(monitor, editableValue, prop, true);
    if (!updatePropertyValue(monitor, editableValue, prop, newValue, false)) {
        return;
    }
    if (commandContext != null) {
        if (lastCommand == null || lastCommand.getObject() != editableValue || lastCommand.property != prop || !commandContext.isDirty()) {
            // Last command is not applicable (check for isDirty because command queue might be reverted)
            final DBEObjectEditor<DBPObject> objectEditor = getObjectEditor(DBEObjectEditor.class);
            if (objectEditor == null) {
                log.error("Can't obtain object editor for " + getEditableValue());
                return;
            }
            final DBEPropertyHandler<DBPObject> propertyHandler = objectEditor.makePropertyHandler((DBPObject) editableValue, prop);
            PropertyChangeCommand curCommand = new PropertyChangeCommand((DBPObject) editableValue, prop, propertyHandler, oldValue, newValue);
            commandContext.addCommand(curCommand, commandReflector);
            lastCommand = curCommand;
        } else {
            lastCommand.setNewValue(newValue);
            commandContext.updateCommand(lastCommand, commandReflector);
        }
    }
    // To update name-based cache
    if (prop.isNameProperty() && editableValue instanceof DBSObject) {
        DBEObjectMaker objectManager = getObjectEditor(DBEObjectMaker.class);
        if (objectManager != null) {
            DBSObjectCache cache = objectManager.getObjectsCache((DBSObject) editableValue);
            if (cache != null && cache.isFullyCached()) {
                List<? extends DBSObject> cachedObjects = CommonUtils.copyList(cache.getCachedObjects());
                cache.setCache(cachedObjects);
            }
        }
    }
/*
        // Notify listeners
        for (IPropertySourceListener listener : listeners) {
            listener.handlePropertyChange(editableValue, prop, newValue);
        }
*/
}
Also used : DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBSObjectCache(org.jkiss.dbeaver.model.struct.cache.DBSObjectCache) DBPObject(org.jkiss.dbeaver.model.DBPObject) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBPObject(org.jkiss.dbeaver.model.DBPObject) DBPNamedObject(org.jkiss.dbeaver.model.DBPNamedObject) DBEObjectMaker(org.jkiss.dbeaver.model.edit.DBEObjectMaker)

Example 14 with DBEObjectMaker

use of org.jkiss.dbeaver.model.edit.DBEObjectMaker 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)

Example 15 with DBEObjectMaker

use of org.jkiss.dbeaver.model.edit.DBEObjectMaker in project dbeaver by dbeaver.

the class NavigatorObjectsDeleter method appendScript.

private void appendScript(final DBRProgressMonitor monitor, final StringBuilder sql, final DBNDatabaseNode node) throws InvocationTargetException {
    if (!(node.getParentNode() instanceof DBNContainer)) {
        return;
    }
    final DBSObject object = node.getObject();
    if (object == null) {
        return;
    }
    final DBEObjectMaker objectMaker = DBWorkbench.getPlatform().getEditorsRegistry().getObjectManager(object.getClass(), DBEObjectMaker.class);
    if (objectMaker == null) {
        return;
    }
    final boolean supportsCascade = (objectMaker.getMakerOptions(object.getDataSource()) & DBEObjectMaker.FEATURE_DELETE_CASCADE) != 0;
    final NavigatorHandlerObjectBase.CommandTarget commandTarget;
    try {
        commandTarget = NavigatorHandlerObjectBase.getCommandTarget(window, node.getParentNode(), object.getClass(), false);
    } catch (DBException e) {
        log.warn(e);
        return;
    }
    if (commandContext == null) {
        commandContext = commandTarget.getContext();
    }
    if (!object.isPersisted() || commandTarget.getEditor() != null) {
        return;
    }
    final Map<String, Object> deleteOptions;
    if (supportsCascade && deleteCascade) {
        deleteOptions = OPTIONS_CASCADE;
    } else {
        deleteOptions = Collections.emptyMap();
    }
    try {
        objectMaker.deleteObject(commandTarget.getContext(), node.getObject(), deleteOptions);
    } catch (DBException e) {
        log.warn(e);
        return;
    }
    final StringBuilder script = new StringBuilder();
    final DBECommandContext commandContext = commandTarget.getContext();
    Collection<? extends DBECommand> commands = commandContext.getFinalCommands();
    try {
        for (DBECommand command : commands) {
            final DBEPersistAction[] persistActions = command.getPersistActions(monitor, commandContext.getExecutionContext(), deleteOptions);
            script.append(SQLUtils.generateScript(commandContext.getExecutionContext().getDataSource(), persistActions, false));
            if (script.length() == 0) {
                script.append(SQLUtils.generateComments(commandContext.getExecutionContext().getDataSource(), persistActions, false));
            }
        }
    } catch (DBException e) {
        throw new InvocationTargetException(e);
    }
    commandTarget.getContext().resetChanges(true);
    if (sql.length() != 0) {
        sql.append("\n");
    }
    sql.append(script);
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBEObjectMaker(org.jkiss.dbeaver.model.edit.DBEObjectMaker) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBECommand(org.jkiss.dbeaver.model.edit.DBECommand) DBECommandContext(org.jkiss.dbeaver.model.edit.DBECommandContext) DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject)

Aggregations

DBEObjectMaker (org.jkiss.dbeaver.model.edit.DBEObjectMaker)18 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)16 DBException (org.jkiss.dbeaver.DBException)12 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)5 DBPObject (org.jkiss.dbeaver.model.DBPObject)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 DBPRefreshableObject (org.jkiss.dbeaver.model.DBPRefreshableObject)4 DBSWrapper (org.jkiss.dbeaver.model.struct.DBSWrapper)4 DBPScriptObject (org.jkiss.dbeaver.model.DBPScriptObject)3 HashMap (java.util.HashMap)2 IContributionItem (org.eclipse.jface.action.IContributionItem)2 Display (org.eclipse.swt.widgets.Display)2 DBPNamedObject (org.jkiss.dbeaver.model.DBPNamedObject)2 DBPOrderedObject (org.jkiss.dbeaver.model.DBPOrderedObject)2 DBECommand (org.jkiss.dbeaver.model.edit.DBECommand)2 DBECommandContext (org.jkiss.dbeaver.model.edit.DBECommandContext)2 DBEObjectRenamer (org.jkiss.dbeaver.model.edit.DBEObjectRenamer)2 DBEObjectReorderer (org.jkiss.dbeaver.model.edit.DBEObjectReorderer)2 DBEPersistAction (org.jkiss.dbeaver.model.edit.DBEPersistAction)2 DBNContainer (org.jkiss.dbeaver.model.navigator.DBNContainer)2