use of org.jkiss.dbeaver.model.struct.DBSObject in project dbeaver by serge-rider.
the class EntityEditor method saveCommandContext.
private boolean saveCommandContext(final DBRProgressMonitor monitor, Map<String, Object> options) {
monitor.beginTask("Save entity", 1);
Throwable error = null;
final DBECommandContext commandContext = getCommandContext();
if (commandContext == null) {
log.warn("Null command context");
return true;
}
DBCExecutionContext executionContext = getExecutionContext();
if (executionContext == null) {
log.warn("Null execution context");
return true;
}
boolean isNewObject = getDatabaseObject() == null || !getDatabaseObject().isPersisted();
if (!isNewObject) {
// Check for any new nested objects
for (DBECommand cmd : commandContext.getFinalCommands()) {
if (cmd.getObject() instanceof DBSObject && !((DBSObject) cmd.getObject()).isPersisted()) {
isNewObject = true;
break;
}
}
}
try {
DBExecUtils.tryExecuteRecover(monitor, executionContext.getDataSource(), param -> {
try {
commandContext.saveChanges(monitor, options);
} catch (DBException e) {
throw new InvocationTargetException(e);
}
});
} catch (DBException e) {
error = e;
}
if (getDatabaseObject() instanceof DBPStatefulObject) {
try {
((DBPStatefulObject) getDatabaseObject()).refreshObjectState(monitor);
} catch (DBCException e) {
// Just report an error
log.error(e);
}
}
if (error == null) {
// Refresh underlying node
// It'll refresh database object and all it's descendants
// So we'll get actual data from database
final DBNDatabaseNode treeNode = getEditorInput().getNavigatorNode();
try {
boolean doRefresh = isNewObject;
UIUtils.runInProgressService(monitor1 -> {
try {
treeNode.refreshNode(monitor1, doRefresh ? DBNEvent.FORCE_REFRESH : DBNEvent.UPDATE_ON_SAVE);
} catch (DBException e) {
throw new InvocationTargetException(e);
}
});
} catch (InvocationTargetException e) {
error = e.getTargetException();
} catch (InterruptedException e) {
// ok
}
}
monitor.done();
if (error == null) {
return true;
} else {
// Try to handle error in nested editors
final Throwable vError = error;
UIUtils.syncExec(() -> {
final IErrorVisualizer errorVisualizer = getAdapter(IErrorVisualizer.class);
if (errorVisualizer != null) {
errorVisualizer.visualizeError(monitor, vError);
}
});
// Show error dialog
UIUtils.asyncExec(() -> DBWorkbench.getPlatformUI().showError("Can't save '" + getDatabaseObject().getName() + "'", null, vError));
return false;
}
}
use of org.jkiss.dbeaver.model.struct.DBSObject in project dbeaver by serge-rider.
the class DatabaseProducerPageInputObjects method autoAssignMappings.
private void autoAssignMappings(List<DBSObject> containerObjects) {
boolean chooseConsumer = getWizard().getSettings().isConsumerOptional();
for (TableItem item : mappingTable.getItems()) {
DataTransferPipe pipe = (DataTransferPipe) item.getData();
if ((chooseConsumer && (pipe.getConsumer() == null || pipe.getConsumer().getDatabaseObject() == null)) || (!chooseConsumer && (pipe.getProducer() == null || pipe.getProducer().getDatabaseObject() == null))) {
DBSObject objectToMap = chooseConsumer ? pipe.getProducer().getDatabaseObject() : pipe.getConsumer().getDatabaseObject();
if (objectToMap == null) {
continue;
}
DBSObject object = DBUtils.findObject(containerObjects, objectToMap.getName());
if (object != null) {
if (chooseConsumer) {
if (object instanceof DBSDataManipulator) {
pipe.setConsumer(new DatabaseTransferConsumer((DBSDataManipulator) object));
}
} else {
if (object instanceof DBSDataContainer) {
pipe.setProducer(new DatabaseTransferProducer((DBSDataContainer) object));
}
}
updateItemData(item, pipe);
}
}
}
updatePageCompletion();
}
use of org.jkiss.dbeaver.model.struct.DBSObject in project dbeaver by serge-rider.
the class PreviewMappingDialog method loadImportPreview.
private void loadImportPreview(DBRProgressMonitor monitor) throws DBException {
PreviewConsumer previewConsumer = new PreviewConsumer(monitor, mappingContainer);
IDataTransferProducer producer = pipe.getProducer();
IDataTransferSettings producerSettings = getNodeSettings(producer);
IDataTransferSettings consumerSettings = getNodeSettings(pipe.getConsumer());
try {
IDataTransferConsumer realConsumer = pipe.getConsumer();
try {
pipe.setConsumer(previewConsumer);
pipe.initPipe(dtSettings, 0, 1);
producer.transferData(previewConsumer.getCtlMonitor(), previewConsumer, dtSettings.getProcessor() == null ? null : dtSettings.getProcessor().getInstance(), producerSettings, null);
} finally {
pipe.setConsumer(realConsumer);
}
} finally {
previewConsumer.close();
}
List<Object[]> rows = previewConsumer.getRows();
List<String[]> strRows = new ArrayList<>(rows.size());
DBSObject target = mappingContainer.getTarget();
if (target == null) {
if (consumerSettings instanceof DatabaseConsumerSettings) {
target = ((DatabaseConsumerSettings) consumerSettings).getContainer();
}
}
if (target == null) {
throw new DBException("Can not determine target container");
}
try (DBCSession session = DBUtils.openUtilSession(monitor, target, "Generate preview values")) {
DatabaseTransferConsumer.ColumnMapping[] columnMappings = previewConsumer.getColumnMappings();
for (Object[] row : rows) {
String[] strRow = new String[row.length];
for (DatabaseTransferConsumer.ColumnMapping attr : columnMappings) {
if (attr == null) {
continue;
}
Object srcValue = row[attr.targetIndex];
Object value = attr.sourceValueHandler.getValueFromObject(session, attr.sourceAttr, srcValue, false, true);
DBSAttributeBase attrTarget = attr.targetAttr.getTarget();
if (attrTarget == null) {
// New column - it is ok for preview only
attrTarget = attr.sourceAttr;
}
String valueStr = attr.targetValueHandler.getValueDisplayString(attrTarget, value, DBDDisplayFormat.UI);
strRow[attr.targetIndex] = CommonUtils.getSingleLineString(valueStr);
}
strRows.add(strRow);
}
}
UIUtils.asyncExec(() -> {
previewTable.setRedraw(false);
try {
previewTable.removeAll();
for (TableColumn column : previewTable.getColumns()) {
column.dispose();
}
for (DatabaseTransferConsumer.ColumnMapping columnMapping : previewConsumer.getColumnMappings()) {
if (columnMapping == null) {
continue;
}
TableColumn column = new TableColumn(previewTable, SWT.NONE);
column.setText(columnMapping.targetAttr.getTargetName());
DBSAttributeBase attr = columnMapping.targetAttr.getTarget();
if (attr == null) {
// We can use icon from source attribute
attr = columnMapping.sourceAttr;
}
column.setImage(DBeaverIcons.getImage(DBValueFormatting.getObjectImage(attr)));
column.setData(columnMapping);
}
for (String[] row : strRows) {
TableItem previewItem = new TableItem(previewTable, SWT.NONE);
for (int i = 0; i < row.length; i++) {
if (row[i] != null) {
previewItem.setText(i, row[i]);
}
}
}
UIUtils.packColumns(previewTable);
} finally {
previewTable.setRedraw(true);
}
});
}
use of org.jkiss.dbeaver.model.struct.DBSObject in project dbeaver by serge-rider.
the class DataTransferPagePipes method loadConsumers.
private void loadConsumers() {
DataTransferSettings settings = getWizard().getSettings();
Collection<DBSObject> objects = settings.getSourceObjects();
List<TransferTarget> transferTargets = new ArrayList<>();
for (DataTransferNodeDescriptor consumer : DataTransferRegistry.getInstance().getAvailableConsumers(objects)) {
Collection<DataTransferProcessorDescriptor> processors = consumer.getAvailableProcessors(objects);
if (CommonUtils.isEmpty(processors)) {
transferTargets.add(new TransferTarget(consumer, null));
} else {
for (DataTransferProcessorDescriptor processor : processors) {
transferTargets.add(new TransferTarget(consumer, processor));
}
}
}
nodesTable.setInput(transferTargets);
}
use of org.jkiss.dbeaver.model.struct.DBSObject in project dbeaver by serge-rider.
the class DataTransferSettings method setDataPipes.
public void setDataPipes(List<DataTransferPipe> dataPipes, boolean isExport) {
this.dataPipes = dataPipes;
// Now determine main producer and consumer and processor
DataTransferRegistry registry = DataTransferRegistry.getInstance();
this.consumerOptional = isExport;
this.producerOptional = !isExport;
// Don't update producer and consumer if it's not required (#9687)
if (!nodeUpdateRestricted) {
this.producer = null;
this.consumer = null;
if (!dataPipes.isEmpty()) {
DataTransferPipe pipe = dataPipes.get(0);
this.producer = pipe.getProducer() == null ? null : registry.getNodeByType(pipe.getProducer().getClass());
this.consumer = pipe.getConsumer() == null ? null : registry.getNodeByType(pipe.getConsumer().getClass());
}
}
DataTransferProcessorDescriptor savedProcessor = this.processor;
if (this.consumerOptional && this.consumer != null) {
this.selectConsumer(this.consumer, savedProcessor, false);
}
if (this.producerOptional && this.producer != null) {
this.selectProducer(this.producer, savedProcessor, false);
}
// Collect objects
initObjects.clear();
for (DataTransferPipe pipe : dataPipes) {
DBSObject object = isExport ? pipe.getProducer().getDatabaseObject() : pipe.getConsumer().getDatabaseObject();
if (object != null) {
initObjects.add(object);
}
}
}
Aggregations