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, DBNDatabaseNode copyFrom) {
try {
DBNContainer container = null;
if (element instanceof DBNContainer && !(element instanceof DBNDataSource)) {
container = (DBNContainer) element;
} else {
DBNNode parentNode = element.getParentNode();
if (parentNode instanceof DBNContainer) {
container = (DBNContainer) parentNode;
}
}
if (container == null) {
throw new DBException("Can't detect container for '" + element.getNodeName() + "'");
}
Class<?> childType = container.getChildrenClass();
if (childType == null) {
throw new DBException("Can't determine child element type for container '" + container + "'");
}
if (childType == IProject.class) {
return NavigatorHandlerProjectCreate.createNewProject(workbenchWindow);
}
DBSObject sourceObject = copyFrom == null ? null : copyFrom.getObject();
// Do not check for type - manager must do it. Potentially we can copy anything into anything.
// if (sourceObject != null && !childType.isAssignableFrom(sourceObject.getClass())) {
// throw new DBException("Can't create '" + childType.getName() + "' from '" + sourceObject.getClass().getName() + "'");
// }
final EntityEditorsRegistry editorsRegistry = EntityEditorsRegistry.getInstance();
DBEObjectManager<?> objectManager = editorsRegistry.getObjectManager(childType);
if (objectManager == null) {
throw new DBException("Object manager not found for type '" + childType.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, childType, openEditor);
// Parent is model object - not node
final Object parentObject = container.getValueObject();
if (parentObject instanceof DBPObject) {
createDatabaseObject(commandTarget, objectMaker, (DBPObject) parentObject, sourceObject);
} else {
throw new DBException("Parent object type is not supported: " + parentObject);
}
} catch (Throwable e) {
DBUserInterface.getInstance().showError("Create object", null, e);
return false;
}
return true;
}
use of org.jkiss.dbeaver.model.edit.DBEObjectMaker in project dbeaver by dbeaver.
the class NavigatorHandlerObjectDelete method deleteObject.
private boolean deleteObject(IWorkbenchWindow workbenchWindow, DBNDatabaseNode node) {
try {
if (!(node.getParentNode() instanceof DBNContainer)) {
throw new DBException("Node '" + node + "' doesn't have a container");
}
final DBNContainer container = (DBNContainer) node.getParentNode();
// Try to delete object using object manager
DBSObject object = node.getObject();
if (object == null) {
throw new DBException("Can't delete node with null object");
}
DBEObjectMaker objectMaker = EntityEditorsRegistry.getInstance().getObjectManager(object.getClass(), DBEObjectMaker.class);
if (objectMaker == null) {
// $NON-NLS-2$
throw new DBException("Object maker not found for type '" + object.getClass().getName() + "'");
}
boolean supportsCascade = (objectMaker.getMakerOptions(object.getDataSource()) & DBEObjectMaker.FEATURE_DELETE_CASCADE) != 0;
CommandTarget commandTarget = getCommandTarget(workbenchWindow, container, object.getClass(), false);
if (deleteAll == null || !deleteAll) {
this.deleteOptions.clear();
}
ConfirmResult confirmResult = ConfirmResult.YES;
if (!object.isPersisted() || commandTarget.getEditor() != null) {
// There should be command context somewhere
if (deleteNewObject(workbenchWindow, node)) {
return true;
}
// No direct editor host found for this object -
// try to find corresponding command context
// and execute command within it
} else {
// Persisted object - confirm delete
// Show "View script" only if we are not in some editor (because it have its own "View script" button)
confirmResult = confirmObjectDelete(workbenchWindow, node, supportsCascade, deleteOptions, commandTarget.getContext() != null && commandTarget.getEditor() == null);
if (confirmResult == ConfirmResult.NO) {
return false;
}
}
objectMaker.deleteObject(commandTarget.getContext(), node.getObject(), deleteOptions);
if (confirmResult == ConfirmResult.DETAILS) {
if (!showScript(workbenchWindow, commandTarget.getContext(), deleteOptions, CoreMessages.actions_navigator_delete_script)) {
commandTarget.getContext().resetChanges();
// Show confirmation again
return deleteObject(workbenchWindow, node);
}
}
if (commandTarget.getEditor() == null && commandTarget.getContext() != null) {
// Persist object deletion - only if there is no host editor and we have a command context
ObjectSaver deleter = new ObjectSaver(commandTarget.getContext(), deleteOptions);
// DBeaverUI.runInProgressDialog(deleter);
tasksToExecute.add(deleter);
}
} catch (Throwable e) {
DBUserInterface.getInstance().showError(CoreMessages.actions_navigator_error_dialog_delete_object_title, "Can't delete object '" + node.getNodeName() + "'", e);
return false;
}
return true;
}
use of org.jkiss.dbeaver.model.edit.DBEObjectMaker in project dbeaver by serge-rider.
the class NavigatorObjectsDeleter method deleteDatabaseNode.
private void deleteDatabaseNode(final DBNDatabaseNode node) {
try {
if (!(node.getParentNode() instanceof DBNContainer)) {
throw new DBException("Node '" + node + "' doesn't have a container");
}
final DBSObject object = node.getObject();
if (object == null) {
throw new DBException("Can't delete node with null object");
}
final DBEObjectMaker objectMaker = DBWorkbench.getPlatform().getEditorsRegistry().getObjectManager(object.getClass(), DBEObjectMaker.class);
if (objectMaker == null) {
// $NON-NLS-2$
throw new DBException("Object maker not found for type '" + object.getClass().getName() + "'");
}
final boolean supportsCascade = (objectMaker.getMakerOptions(object.getDataSource()) & DBEObjectMaker.FEATURE_DELETE_CASCADE) != 0;
final NavigatorHandlerObjectBase.CommandTarget commandTarget = NavigatorHandlerObjectBase.getCommandTarget(window, node.getParentNode(), object.getClass(), false);
if (!object.isPersisted() || commandTarget.getEditor() != null) {
// There should be command context somewhere
if (deleteNewObject(node)) {
return;
}
// No direct editor host found for this object -
// try to find corresponding command context
// and execute command within it
}
Map<String, Object> deleteOptions = Collections.emptyMap();
if (deleteCascade && supportsCascade) {
deleteOptions = OPTIONS_CASCADE;
}
objectMaker.deleteObject(commandTarget.getContext(), node.getObject(), deleteOptions);
if (commandTarget.getEditor() == null && commandTarget.getContext() != null) {
// Persist object deletion - only if there is no host editor and we have a command context
final NavigatorHandlerObjectBase.ObjectSaver deleter = new NavigatorHandlerObjectBase.ObjectSaver(commandTarget.getContext(), deleteOptions);
tasksToExecute.add(deleter);
}
} catch (Throwable e) {
DBWorkbench.getPlatformUI().showError(UINavigatorMessages.actions_navigator_error_dialog_delete_object_title, NLS.bind(UINavigatorMessages.actions_navigator_error_dialog_delete_object_message, node.getNodeName()), e);
}
}
use of org.jkiss.dbeaver.model.edit.DBEObjectMaker in project dbeaver by serge-rider.
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);
}
use of org.jkiss.dbeaver.model.edit.DBEObjectMaker in project dbeaver by dbeaver.
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);
}
*/
}
Aggregations