Search in sources :

Example 56 with DBSObject

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

the class ToolsRegistry method getTools.

public List<ToolDescriptor> getTools(IStructuredSelection selection) {
    List<DBSObject> objects = NavigatorUtils.getSelectedObjects(selection);
    List<ToolDescriptor> result = new ArrayList<ToolDescriptor>();
    for (ToolDescriptor descriptor : tools) {
        if (descriptor.isSingleton() && objects.size() > 1) {
            continue;
        }
        boolean applies = true;
        for (DBSObject object : objects) {
            if (!descriptor.appliesTo(object)) {
                applies = false;
                break;
            }
        }
        if (applies) {
            result.add(descriptor);
        }
    }
    return result;
}
Also used : DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) ArrayList(java.util.ArrayList)

Example 57 with DBSObject

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

the class CompareObjectsExecutor method compareProperties.

private void compareProperties(DBRProgressMonitor monitor, List<DBNDatabaseNode> nodes) throws DBException, InterruptedException {
    // Clear compare singletons
    this.initializedCount = 0;
    this.initializeError = null;
    this.propertyValues.clear();
    StringBuilder title = new StringBuilder();
    // Initialize nodes
    {
        monitor.subTask("Initialize nodes");
        for (DBNDatabaseNode node : nodes) {
            if (title.length() > 0)
                title.append(", ");
            title.append(node.getNodeFullName());
            node.initializeNode(null, initializeFinisher);
            monitor.worked(1);
        }
        while (initializedCount != nodes.size()) {
            if (initializeError != null) {
                throw new DBException(initializeError.getMessage());
            }
            Thread.sleep(100);
            if (monitor.isCanceled()) {
                throw new InterruptedException();
            }
        }
    }
    monitor.subTask("Compare " + title.toString());
    boolean compareLazyProperties = false;
    DBNDatabaseNode firstNode = nodes.get(0);
    List<ObjectPropertyDescriptor> properties = ObjectPropertyDescriptor.extractAnnotations(null, firstNode.getObject().getClass(), getDataSourceFilter(firstNode));
    for (ObjectPropertyDescriptor prop : properties) {
        if (prop.isLazy()) {
            compareLazyProperties = true;
            break;
        }
    }
    compareLazyProperties = compareLazyProperties && settings.isCompareLazyProperties();
    // Load all properties
    for (DBNDatabaseNode node : nodes) {
        if (monitor.isCanceled()) {
            throw new InterruptedException();
        }
        DBSObject databaseObject = node.getObject();
        Map<DBPPropertyDescriptor, Object> nodeProperties = propertyValues.get(databaseObject);
        if (nodeProperties == null) {
            nodeProperties = new IdentityHashMap<>();
            propertyValues.put(databaseObject, nodeProperties);
        }
        PropertyCollector propertySource = new PropertyCollector(databaseObject, compareLazyProperties);
        for (ObjectPropertyDescriptor prop : properties) {
            Object propertyValue = propertySource.getPropertyValue(monitor, databaseObject, prop);
            synchronized (PROPS_LOCK) {
                if (propertyValue instanceof DBPNamedObject) {
                    // Compare just object names
                    propertyValue = ((DBPNamedObject) propertyValue).getName();
                }
                nodeProperties.put(prop, propertyValue);
            }
        }
        monitor.worked(1);
    }
    // Compare properties
    for (ObjectPropertyDescriptor prop : properties) {
        reportPropertyCompare(prop);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBPNamedObject(org.jkiss.dbeaver.model.DBPNamedObject) DBPPropertyDescriptor(org.jkiss.dbeaver.model.preferences.DBPPropertyDescriptor) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBPSystemObject(org.jkiss.dbeaver.model.DBPSystemObject) DBPNamedObject(org.jkiss.dbeaver.model.DBPNamedObject) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Example 58 with DBSObject

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

the class PropertySourceEditable method updatePropertyValue.

private boolean updatePropertyValue(@Nullable DBRProgressMonitor monitor, Object editableValue, ObjectPropertyDescriptor prop, Object value, boolean force) throws IllegalArgumentException {
    // Write property value
    try {
        // If value should be a named object then try to obtain it from list provider
        if (value != null && value.getClass() == String.class) {
            final Object[] items = prop.getPossibleValues(editableValue);
            if (items != null) {
                boolean found = false;
                if (items.length > 0) {
                    for (int i = 0, itemsLength = items.length; i < itemsLength; i++) {
                        if ((items[i] instanceof DBPNamedObject && value.equals(((DBPNamedObject) items[i]).getName())) || (items[i] instanceof Enum && value.equals(((Enum) items[i]).name()))) {
                            value = items[i];
                            found = true;
                            break;
                        }
                    }
                }
                if (!found) {
                    if (value.getClass() != prop.getDataType()) {
                        value = null;
                    }
                }
            }
        }
        final Object oldValue = getPropertyValue(monitor, editableValue, prop);
        if (CommonUtils.equalObjects(oldValue, value)) {
            return false;
        }
        prop.writeValue(editableValue, value);
        // Fire object update event
        if (editableValue instanceof DBSObject) {
            DBUtils.fireObjectUpdate((DBSObject) editableValue, prop);
        }
        return true;
    } catch (Throwable e) {
        if (e instanceof InvocationTargetException) {
            e = ((InvocationTargetException) e).getTargetException();
        }
        log.error("Can't write property '" + prop.getDisplayName() + "' value", e);
        return false;
    }
}
Also used : DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBPNamedObject(org.jkiss.dbeaver.model.DBPNamedObject) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBPObject(org.jkiss.dbeaver.model.DBPObject) DBPNamedObject(org.jkiss.dbeaver.model.DBPNamedObject) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 59 with DBSObject

use of org.jkiss.dbeaver.model.struct.DBSObject 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);
    if (!updatePropertyValue(monitor, editableValue, prop, newValue, false)) {
        return;
    }
    if (commandContext != null) {
        if (lastCommand == null || lastCommand.getObject() != editableValue || lastCommand.property != prop) {
            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.impl.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 60 with DBSObject

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

the class NavigatorAdapterFactory method getAdapter.

@Override
public <T> T getAdapter(Object adaptableObject, Class<T> adapterType) {
    if (adapterType == DBPDataSourceContainer.class) {
        if (adaptableObject instanceof DBNDataSource) {
            return adapterType.cast(((DBNDataSource) adaptableObject).getDataSourceContainer());
        }
        DBSObject object = DBUtils.getFromObject(adaptableObject);
        if (object == null) {
            return null;
        }
        if (object instanceof DBPDataSourceContainer) {
            return adapterType.cast(object);
        }
        DBPDataSource dataSource = object.getDataSource();
        return dataSource == null ? null : adapterType.cast(dataSource.getContainer());
    } else if (DBPObject.class.isAssignableFrom(adapterType)) {
        DBPObject object = null;
        if (adaptableObject instanceof DBSWrapper) {
            object = ((DBSWrapper) adaptableObject).getObject();
        } else if (adaptableObject instanceof DBPObject) {
            object = (DBPObject) adaptableObject;
        }
        if (object != null && adapterType.isAssignableFrom(object.getClass())) {
            return adapterType.cast(object);
        }
    } else if (IResource.class.isAssignableFrom(adapterType)) {
        if (adaptableObject instanceof DBNResource) {
            return ((DBNResource) adaptableObject).getAdapter(adapterType);
        }
    } else if (adapterType == IPropertySource.class) {
        DBPObject dbObject = null;
        if (adaptableObject instanceof DBSWrapper) {
            dbObject = ((DBSWrapper) adaptableObject).getObject();
        } else if (adaptableObject instanceof DBPObject) {
            dbObject = (DBPObject) adaptableObject;
        }
        if (dbObject instanceof IPropertySource) {
            return adapterType.cast(dbObject);
        }
        if (dbObject instanceof DBPPropertySource) {
            return adapterType.cast(new PropertySourceDelegate((DBPPropertySource) dbObject));
        }
        if (dbObject instanceof IAdaptable) {
            Object adapter = ((IAdaptable) dbObject).getAdapter(IPropertySource.class);
            if (adapter != null) {
                return adapterType.cast(adapter);
            }
            adapter = ((IAdaptable) dbObject).getAdapter(DBPPropertySource.class);
            if (adapter != null) {
                return adapterType.cast(new PropertySourceDelegate((DBPPropertySource) adapter));
            }
        }
        if (dbObject != null) {
            PropertyCollector props = new PropertyCollector(adaptableObject, dbObject, true);
            props.collectProperties();
            if (props.isEmpty() && adaptableObject instanceof DBSObject) {
                // Add default properties
                DBSObject meta = (DBSObject) adaptableObject;
                //$NON-NLS-1$
                props.addProperty(null, DBConstants.PROP_ID_NAME, CoreMessages.model_navigator_Name, meta.getName());
                //$NON-NLS-1$
                props.addProperty(null, "desc", CoreMessages.model_navigator_Description, meta.getDescription());
            }
            return adapterType.cast(new PropertySourceDelegate(props));
        }
    } else if (adapterType == IWorkbenchAdapter.class) {
        // Workbench adapter
        if (adaptableObject instanceof DBNNode) {
            final DBNNode node = (DBNNode) adaptableObject;
            WorkbenchAdapter workbenchAdapter = new WorkbenchAdapter() {

                @Override
                public ImageDescriptor getImageDescriptor(Object object) {
                    return DBeaverIcons.getImageDescriptor(node.getNodeIconDefault());
                }

                @Override
                public String getLabel(Object o) {
                    return node.getNodeName();
                }

                @Override
                public Object getParent(Object o) {
                    return node.getParentNode();
                }
            };
            return adapterType.cast(workbenchAdapter);
        } else {
            return null;
        }
    }
    return null;
}
Also used : IAdaptable(org.eclipse.core.runtime.IAdaptable) PropertySourceDelegate(org.jkiss.dbeaver.ui.properties.PropertySourceDelegate) DBSWrapper(org.jkiss.dbeaver.model.struct.DBSWrapper) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) DBNDataSource(org.jkiss.dbeaver.model.navigator.DBNDataSource) DBPPropertySource(org.jkiss.dbeaver.model.preferences.DBPPropertySource) PropertyCollector(org.jkiss.dbeaver.runtime.properties.PropertyCollector) IPropertySource(org.eclipse.ui.views.properties.IPropertySource) WorkbenchAdapter(org.eclipse.ui.model.WorkbenchAdapter) IWorkbenchAdapter(org.eclipse.ui.model.IWorkbenchAdapter) DBNResource(org.jkiss.dbeaver.model.navigator.DBNResource) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject)

Aggregations

DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)68 DBException (org.jkiss.dbeaver.DBException)19 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)13 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)12 DBNNode (org.jkiss.dbeaver.model.navigator.DBNNode)9 ISelection (org.eclipse.jface.viewers.ISelection)8 JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)8 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)8 AbstractObjectReference (org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference)8 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)7 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)7 ArrayList (java.util.ArrayList)6 GridData (org.eclipse.swt.layout.GridData)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 IWorkbenchPart (org.eclipse.ui.IWorkbenchPart)5 DBEObjectMaker (org.jkiss.dbeaver.model.edit.DBEObjectMaker)4 DBSObjectContainer (org.jkiss.dbeaver.model.struct.DBSObjectContainer)4 DBSObjectSelector (org.jkiss.dbeaver.model.struct.DBSObjectSelector)4 DBSWrapper (org.jkiss.dbeaver.model.struct.DBSWrapper)4 ToolWizardDialog (org.jkiss.dbeaver.ui.dialogs.tools.ToolWizardDialog)4