use of org.jkiss.dbeaver.model.DBPObject in project dbeaver by serge-rider.
the class AbstractCommandContext method getCommandQueues.
private List<CommandQueue> getCommandQueues() {
if (commandQueues != null) {
return commandQueues;
}
commandQueues = new ArrayList<>();
CommandInfo aggregator = null;
// Create queues from commands
for (CommandInfo commandInfo : commands) {
if (commandInfo.command instanceof DBECommandAggregator) {
aggregator = commandInfo;
}
DBPObject object = commandInfo.command.getObject();
CommandQueue queue = null;
if (!commandQueues.isEmpty()) {
for (CommandQueue tmpQueue : commandQueues) {
if (tmpQueue.getObject() == object) {
queue = tmpQueue;
break;
}
}
}
if (queue == null) {
DBEObjectManager<?> objectManager = executionContext.getDataSource().getContainer().getPlatform().getEditorsRegistry().getObjectManager(object.getClass());
if (objectManager == null) {
throw new IllegalStateException("Can't find object manager for '" + object.getClass().getName() + "'");
}
queue = new CommandQueue(objectManager, null, object);
commandQueues.add(queue);
}
queue.addCommand(commandInfo);
}
// Merge commands
for (CommandQueue queue : commandQueues) {
final Map<DBECommand, CommandInfo> mergedByMap = new IdentityHashMap<>();
final List<CommandInfo> mergedCommands = new ArrayList<>();
for (int i = 0; i < queue.commands.size(); i++) {
CommandInfo lastCommand = queue.commands.get(i);
lastCommand.mergedBy = null;
CommandInfo firstCommand = null;
DBECommand<?> result = lastCommand.command;
if (mergedCommands.isEmpty()) {
result = lastCommand.command.merge(null, userParams);
} else {
boolean skipCommand = false;
for (int k = mergedCommands.size(); k > 0; k--) {
firstCommand = mergedCommands.get(k - 1);
result = lastCommand.command.merge(firstCommand.command, userParams);
if (result == null) {
// Remove first and skip last command
mergedCommands.remove(firstCommand);
skipCommand = true;
} else if (result != lastCommand.command) {
break;
}
}
if (skipCommand) {
continue;
}
}
mergedCommands.add(lastCommand);
if (result == lastCommand.command) {
// No changes
// firstCommand.mergedBy = lastCommand;
} else if (firstCommand != null && result == firstCommand.command) {
// Remove last command from queue
lastCommand.mergedBy = firstCommand;
} else {
// Some other command
// May be it is some earlier command from queue or some new command (e.g. composite)
CommandInfo mergedBy = mergedByMap.get(result);
if (mergedBy == null) {
// Try to find in command stack
for (int k = i; k >= 0; k--) {
if (queue.commands.get(k).command == result) {
mergedBy = queue.commands.get(k);
break;
}
}
if (mergedBy == null) {
// Create new command info
mergedBy = new CommandInfo(result, null);
}
mergedByMap.put(result, mergedBy);
}
lastCommand.mergedBy = mergedBy;
if (!mergedCommands.contains(mergedBy)) {
mergedCommands.add(mergedBy);
}
}
}
queue.commands = mergedCommands;
}
// Filter commands
for (CommandQueue queue : commandQueues) {
if (queue.objectManager instanceof DBECommandFilter) {
((DBECommandFilter) queue.objectManager).filterCommands(queue);
}
}
// Aggregate commands
if (aggregator != null) {
((DBECommandAggregator) aggregator.command).resetAggregatedCommands();
for (CommandQueue queue : commandQueues) {
for (CommandInfo cmd : queue.commands) {
if (cmd.command != aggregator.command && cmd.mergedBy == null && ((DBECommandAggregator) aggregator.command).aggregateCommand(cmd.command)) {
cmd.mergedBy = aggregator;
}
}
}
}
// Move rename commands in the head (#7512)
for (CommandQueue queue : commandQueues) {
int headIndex = 0;
for (CommandInfo cmd : new ArrayList<>(queue.commands)) {
if (cmd.mergedBy == null && cmd.command instanceof DBECommandRename) {
queue.commands.remove(cmd);
queue.commands.add(headIndex++, cmd);
}
}
}
return commandQueues;
}
use of org.jkiss.dbeaver.model.DBPObject in project dbeaver by dbeaver.
the class DataImportHandler method adaptTransferNode.
@Override
protected IDataTransferNode adaptTransferNode(Object object) {
final DBSDataManipulator adapted = RuntimeUtils.getObjectAdapter(object, DBSDataManipulator.class);
if (adapted != null) {
return new DatabaseTransferConsumer(adapted);
} else {
IFile file = RuntimeUtils.getObjectAdapter(object, IFile.class);
if (file != null) {
return getNodeByFile(file);
}
DBSObjectContainer objectContainer = RuntimeUtils.getObjectAdapter(object, DBSObjectContainer.class);
if (objectContainer == null) {
if (object instanceof DBSWrapper) {
object = ((DBSWrapper) object).getObject();
}
if (object instanceof DBPObject) {
object = DBUtils.getPublicObject((DBSObject) object);
}
if (object instanceof DBSObjectContainer) {
objectContainer = (DBSObjectContainer) object;
}
}
if (objectContainer != null) {
if (isObjectContainerSupportsImport(objectContainer)) {
return new DatabaseTransferConsumer(objectContainer);
} else {
DBWorkbench.getPlatformUI().showError("Wrong container", objectContainer.getName() + " doesn't support direct data import");
}
}
return null;
}
}
use of org.jkiss.dbeaver.model.DBPObject 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);
}
*/
}
use of org.jkiss.dbeaver.model.DBPObject in project dbeaver by dbeaver.
the class SQLGeneratorContributor method getContributionItems.
// ////////////////////////////////////////////////////////
// Contributors
@Override
protected IContributionItem[] getContributionItems() {
IWorkbenchPart part = UIUtils.getActiveWorkbenchWindow().getActivePage().getActivePart();
IStructuredSelection structuredSelection = NavigatorUtils.getSelectionFromPart(part);
if (structuredSelection == null || structuredSelection.isEmpty()) {
return new IContributionItem[0];
}
List<IContributionItem> menu = new ArrayList<>();
if (structuredSelection instanceof IResultSetSelection) {
// Results
makeResultSetContributions(menu, (IResultSetSelection) structuredSelection);
} else {
List<DBPObject> objects = new ArrayList<>();
for (Object obj : structuredSelection.toList()) {
DBSObject adaptedObject = GeneralUtils.adapt(obj, DBSObject.class);
if (adaptedObject != null) {
objects.add(adaptedObject);
} else if (obj instanceof DBSWrapper) {
objects.add(((DBSWrapper) obj).getObject());
} else if (obj instanceof DBPObject) {
objects.add((DBPObject) obj);
}
}
List<SQLGeneratorDescriptor> generators = SQLGeneratorConfigurationRegistry.getInstance().getApplicableGenerators(objects, structuredSelection);
int lastGrand = 0;
for (SQLGeneratorDescriptor gen : generators) {
int order = gen.getOrder();
if (order > 0 && order / 1000 > lastGrand) {
menu.add(new Separator());
}
lastGrand = order / 1000;
menu.add(makeAction(gen.getLabel(), gen, objects));
}
}
return menu.toArray(new IContributionItem[0]);
}
use of org.jkiss.dbeaver.model.DBPObject in project dbeaver by dbeaver.
the class AbstractCommandContext method getCommandQueues.
private List<CommandQueue> getCommandQueues() {
if (commandQueues != null) {
return commandQueues;
}
commandQueues = new ArrayList<>();
CommandInfo aggregator = null;
// Create queues from commands
for (CommandInfo commandInfo : commands) {
if (commandInfo.command instanceof DBECommandAggregator) {
aggregator = commandInfo;
}
DBPObject object = commandInfo.command.getObject();
CommandQueue queue = null;
if (!commandQueues.isEmpty()) {
for (CommandQueue tmpQueue : commandQueues) {
if (tmpQueue.getObject() == object) {
queue = tmpQueue;
break;
}
}
}
if (queue == null) {
DBEObjectManager<?> objectManager = executionContext.getDataSource().getContainer().getPlatform().getEditorsRegistry().getObjectManager(object.getClass());
if (objectManager == null) {
throw new IllegalStateException("Can't find object manager for '" + object.getClass().getName() + "'");
}
queue = new CommandQueue(objectManager, null, object);
commandQueues.add(queue);
}
queue.addCommand(commandInfo);
}
// Merge commands
for (CommandQueue queue : commandQueues) {
final Map<DBECommand, CommandInfo> mergedByMap = new IdentityHashMap<>();
final List<CommandInfo> mergedCommands = new ArrayList<>();
for (int i = 0; i < queue.commands.size(); i++) {
CommandInfo lastCommand = queue.commands.get(i);
lastCommand.mergedBy = null;
CommandInfo firstCommand = null;
DBECommand<?> result = lastCommand.command;
if (mergedCommands.isEmpty()) {
result = lastCommand.command.merge(null, userParams);
} else {
boolean skipCommand = false;
for (int k = mergedCommands.size(); k > 0; k--) {
firstCommand = mergedCommands.get(k - 1);
result = lastCommand.command.merge(firstCommand.command, userParams);
if (result == null) {
// Remove first and skip last command
mergedCommands.remove(firstCommand);
skipCommand = true;
} else if (result != lastCommand.command) {
break;
}
}
if (skipCommand) {
continue;
}
}
mergedCommands.add(lastCommand);
if (result == lastCommand.command) {
// No changes
// firstCommand.mergedBy = lastCommand;
} else if (firstCommand != null && result == firstCommand.command) {
// Remove last command from queue
lastCommand.mergedBy = firstCommand;
} else {
// Some other command
// May be it is some earlier command from queue or some new command (e.g. composite)
CommandInfo mergedBy = mergedByMap.get(result);
if (mergedBy == null) {
// Try to find in command stack
for (int k = i; k >= 0; k--) {
if (queue.commands.get(k).command == result) {
mergedBy = queue.commands.get(k);
break;
}
}
if (mergedBy == null) {
// Create new command info
mergedBy = new CommandInfo(result, null);
}
mergedByMap.put(result, mergedBy);
}
lastCommand.mergedBy = mergedBy;
if (!mergedCommands.contains(mergedBy)) {
mergedCommands.add(mergedBy);
}
}
}
queue.commands = mergedCommands;
}
// Filter commands
for (CommandQueue queue : commandQueues) {
if (queue.objectManager instanceof DBECommandFilter) {
((DBECommandFilter) queue.objectManager).filterCommands(queue);
}
}
// Aggregate commands
if (aggregator != null) {
((DBECommandAggregator) aggregator.command).resetAggregatedCommands();
for (CommandQueue queue : commandQueues) {
for (CommandInfo cmd : queue.commands) {
if (cmd.command != aggregator.command && cmd.mergedBy == null && ((DBECommandAggregator) aggregator.command).aggregateCommand(cmd.command)) {
cmd.mergedBy = aggregator;
}
}
}
}
// Move rename commands in the head (#7512)
for (CommandQueue queue : commandQueues) {
int headIndex = 0;
for (CommandInfo cmd : new ArrayList<>(queue.commands)) {
if (cmd.mergedBy == null && cmd.command instanceof DBECommandRename) {
queue.commands.remove(cmd);
queue.commands.add(headIndex++, cmd);
}
}
}
return commandQueues;
}
Aggregations