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;
}
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);
}
}
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;
}
}
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);
}
*/
}
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;
}
Aggregations