提交 0f7126fd 编写于 作者: S serge-rider

#130 Column transfer mapping type edit

上级 a9445fcb
...@@ -48,6 +48,7 @@ import org.jkiss.dbeaver.tools.transfer.wizard.DataTransferPipe; ...@@ -48,6 +48,7 @@ import org.jkiss.dbeaver.tools.transfer.wizard.DataTransferPipe;
import org.jkiss.dbeaver.tools.transfer.wizard.DataTransferWizard; import org.jkiss.dbeaver.tools.transfer.wizard.DataTransferWizard;
import org.jkiss.dbeaver.ui.DBeaverIcons; import org.jkiss.dbeaver.ui.DBeaverIcons;
import org.jkiss.dbeaver.ui.UIUtils; import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.dbeaver.ui.controls.CustomTableEditor;
import org.jkiss.dbeaver.ui.dialogs.ActiveWizardPage; import org.jkiss.dbeaver.ui.dialogs.ActiveWizardPage;
import org.jkiss.utils.CommonUtils; import org.jkiss.utils.CommonUtils;
...@@ -124,6 +125,56 @@ public class StreamProducerPagePreview extends ActiveWizardPage<DataTransferWiza ...@@ -124,6 +125,56 @@ public class StreamProducerPagePreview extends ActiveWizardPage<DataTransferWiza
UIUtils.createTableColumn(mappingsTable, SWT.LEFT, DTMessages.data_transfer_wizard_final_column_target); UIUtils.createTableColumn(mappingsTable, SWT.LEFT, DTMessages.data_transfer_wizard_final_column_target);
UIUtils.createTableColumn(mappingsTable, SWT.LEFT, DTMessages.data_transfer_wizard_final_column_source); UIUtils.createTableColumn(mappingsTable, SWT.LEFT, DTMessages.data_transfer_wizard_final_column_source);
UIUtils.createTableColumn(mappingsTable, SWT.LEFT, DTMessages.data_transfer_wizard_settings_column_mapping_type); UIUtils.createTableColumn(mappingsTable, SWT.LEFT, DTMessages.data_transfer_wizard_settings_column_mapping_type);
final CustomTableEditor tableEditor = new CustomTableEditor(mappingsTable) {
@Override
protected Control createEditor(Table table, final int index, final TableItem item) {
if (index == 2) {
StreamProducerSettings.AttributeMapping am = (StreamProducerSettings.AttributeMapping) item.getData();
final Combo mappingCombo = new Combo(table, SWT.DROP_DOWN | SWT.READ_ONLY);
for (StreamProducerSettings.AttributeMapping.MappingType mapping : StreamProducerSettings.AttributeMapping.MappingType.values()) {
mappingCombo.add(mapping.getTitle());
}
mappingCombo.setText(am.getMappingType().getTitle());
return mappingCombo;
}
return null;
}
@Override
protected void saveEditorValue(Control control, int index, TableItem item) {
StreamProducerSettings.AttributeMapping am = (StreamProducerSettings.AttributeMapping) item.getData();
if (index == 2) {
final Combo mappingCombo = (Combo) control;
StreamProducerSettings.AttributeMapping.MappingType newMapping = null;
String newTypeTitle = mappingCombo.getText();
for (StreamProducerSettings.AttributeMapping.MappingType mapping : StreamProducerSettings.AttributeMapping.MappingType.values()) {
if (newTypeTitle.equals(mapping.getTitle())) {
newMapping = mapping;
break;
}
}
if (newMapping != null && newMapping != am.getMappingType()) {
item.setText(2, newMapping.getTitle());
am.setMappingType(newMapping);
updatePageCompletion();
if (currentObject instanceof DBSEntity) {
final DBSEntity entity = (DBSEntity) currentObject;
final StreamProducerSettings settings = getWizard().getPageSettings(StreamProducerPagePreview.this, StreamProducerSettings.class);
StreamProducerSettings.EntityMapping entityMapping = settings.getEntityMapping(entity);
UIUtils.asyncExec(() -> {
refreshPreviewTable(entityMapping);
});
}
}
}
}
};
} }
mapSash.setWeights(new int[] { 300, 700 } ); mapSash.setWeights(new int[] { 300, 700 } );
...@@ -241,7 +292,7 @@ public class StreamProducerPagePreview extends ActiveWizardPage<DataTransferWiza ...@@ -241,7 +292,7 @@ public class StreamProducerPagePreview extends ActiveWizardPage<DataTransferWiza
monitor.done(); monitor.done();
} catch (DBException e) { } catch (Throwable e) {
throw new InvocationTargetException(e); throw new InvocationTargetException(e);
} }
}); });
...@@ -261,6 +312,58 @@ public class StreamProducerPagePreview extends ActiveWizardPage<DataTransferWiza ...@@ -261,6 +312,58 @@ public class StreamProducerPagePreview extends ActiveWizardPage<DataTransferWiza
}); });
} }
private void refreshPreviewTable(StreamProducerSettings.EntityMapping entityMapping) {
previewTable.removeAll();
for (TableColumn column : previewTable.getColumns()) {
column.dispose();
}
for (StreamProducerSettings.AttributeMapping am : entityMapping.getAttributeMappings()) {
if (!am.isValuable()) {
continue;
}
// Create preview column
UIUtils.createTableColumn(previewTable, SWT.LEFT, am.getTargetAttributeName());
}
DataTransferProcessorDescriptor processor = getWizard().getSettings().getProcessor();
final DBSEntity entity = (DBSEntity) currentObject;
StreamTransferProducer currentProducer = (StreamTransferProducer) getCurrentPipe().getProducer();
Throwable error = null;
try {
getWizard().getContainer().run(true, true, mon -> {
try {
IDataTransferProcessor importer = processor.getInstance();
DBRProgressMonitor monitor = new DefaultProgressMonitor(mon);
monitor.beginTask("Load preview", 1);
// Load preview
monitor.subTask("Load import preview");
if (importer instanceof IStreamDataImporter) {
loadImportPreview(monitor, (IStreamDataImporter)importer, entity, currentProducer);
}
monitor.worked(1);
monitor.done();
} catch (Throwable e) {
throw new InvocationTargetException(e);
}
});
} catch (InvocationTargetException e) {
error = e.getTargetException();
} catch (InterruptedException e) {
// Ignore
}
Throwable finalError = error;
UIUtils.asyncExec(() -> {
UIUtils.packColumns(previewTable, false);
if (finalError != null) {
DBUserInterface.getInstance().showError("OReview data", "Can't load preview data", finalError);
}
});
}
private void updateAttributeMappings(StreamProducerSettings.EntityMapping entityMapping) { private void updateAttributeMappings(StreamProducerSettings.EntityMapping entityMapping) {
for (StreamProducerSettings.AttributeMapping am : entityMapping.getAttributeMappings()) { for (StreamProducerSettings.AttributeMapping am : entityMapping.getAttributeMappings()) {
// Create mapping item // Create mapping item
...@@ -277,8 +380,10 @@ public class StreamProducerPagePreview extends ActiveWizardPage<DataTransferWiza ...@@ -277,8 +380,10 @@ public class StreamProducerPagePreview extends ActiveWizardPage<DataTransferWiza
mappingItem.setText(1, sourceName); mappingItem.setText(1, sourceName);
mappingItem.setText(2, am.getMappingType().getTitle()); mappingItem.setText(2, am.getMappingType().getTitle());
// Create preview column if (am.isValuable()) {
UIUtils.createTableColumn(previewTable, SWT.LEFT, am.getTargetAttributeName()); // Create preview column
UIUtils.createTableColumn(previewTable, SWT.LEFT, am.getTargetAttributeName());
}
} }
} }
...@@ -340,7 +445,6 @@ public class StreamProducerPagePreview extends ActiveWizardPage<DataTransferWiza ...@@ -340,7 +445,6 @@ public class StreamProducerPagePreview extends ActiveWizardPage<DataTransferWiza
private void loadImportPreview(DBRProgressMonitor monitor, IStreamDataImporter importer, DBSEntity entity, StreamTransferProducer currentProducer) throws DBException { private void loadImportPreview(DBRProgressMonitor monitor, IStreamDataImporter importer, DBSEntity entity, StreamTransferProducer currentProducer) throws DBException {
final StreamProducerSettings settings = getWizard().getPageSettings(this, StreamProducerSettings.class); final StreamProducerSettings settings = getWizard().getPageSettings(this, StreamProducerSettings.class);
final Map<Object, Object> processorProperties = getWizard().getSettings().getProcessorProperties();
PreviewConsumer previewConsumer = new PreviewConsumer(settings, entity); PreviewConsumer previewConsumer = new PreviewConsumer(settings, entity);
...@@ -360,11 +464,16 @@ public class StreamProducerPagePreview extends ActiveWizardPage<DataTransferWiza ...@@ -360,11 +464,16 @@ public class StreamProducerPagePreview extends ActiveWizardPage<DataTransferWiza
List<StreamProducerSettings.AttributeMapping> attributeMappings = previewConsumer.getEntityMapping().getAttributeMappings(); List<StreamProducerSettings.AttributeMapping> attributeMappings = previewConsumer.getEntityMapping().getAttributeMappings();
for (Object[] row : rows) { for (Object[] row : rows) {
String[] strRow = new String[row.length]; String[] strRow = new String[row.length];
int columnIndex = 0;
for (int i = 0; i < attributeMappings.size(); i++) { for (int i = 0; i < attributeMappings.size(); i++) {
StreamProducerSettings.AttributeMapping attr = attributeMappings.get(i); StreamProducerSettings.AttributeMapping attr = attributeMappings.get(i);
Object value = attr.getTargetValueHandler().getValueFromObject(session, attr.getTargetAttribute(), row[i], false); if (!attr.isValuable()) {
continue;
}
Object value = attr.getTargetValueHandler().getValueFromObject(session, attr.getTargetAttribute(), row[columnIndex], false);
String valueStr = attr.getTargetValueHandler().getValueDisplayString(attr.getTargetAttribute(), value, DBDDisplayFormat.UI); String valueStr = attr.getTargetValueHandler().getValueDisplayString(attr.getTargetAttribute(), value, DBDDisplayFormat.UI);
strRow[i] = valueStr; strRow[columnIndex] = valueStr;
columnIndex++;
} }
strRows.add(strRow); strRows.add(strRow);
} }
...@@ -417,11 +526,13 @@ public class StreamProducerPagePreview extends ActiveWizardPage<DataTransferWiza ...@@ -417,11 +526,13 @@ public class StreamProducerPagePreview extends ActiveWizardPage<DataTransferWiza
private DBSEntity sampleObject; private DBSEntity sampleObject;
private final StreamProducerSettings.EntityMapping entityMapping; private final StreamProducerSettings.EntityMapping entityMapping;
private DBCResultSetMetaData meta; private DBCResultSetMetaData meta;
private final List<StreamProducerSettings.AttributeMapping> attributes;
public PreviewConsumer(StreamProducerSettings settings, DBSEntity sampleObject) { public PreviewConsumer(StreamProducerSettings settings, DBSEntity sampleObject) {
this.settings = settings; this.settings = settings;
this.sampleObject = sampleObject; this.sampleObject = sampleObject;
this.entityMapping = this.settings.getEntityMapping(sampleObject); this.entityMapping = this.settings.getEntityMapping(sampleObject);
this.attributes = entityMapping.getValuableAttributeMappings();
} }
public StreamProducerSettings.EntityMapping getEntityMapping() { public StreamProducerSettings.EntityMapping getEntityMapping() {
...@@ -450,16 +561,15 @@ public class StreamProducerPagePreview extends ActiveWizardPage<DataTransferWiza ...@@ -450,16 +561,15 @@ public class StreamProducerPagePreview extends ActiveWizardPage<DataTransferWiza
@Override @Override
public void fetchStart(DBCSession session, DBCResultSet resultSet, long offset, long maxRows) throws DBCException { public void fetchStart(DBCSession session, DBCResultSet resultSet, long offset, long maxRows) throws DBCException {
meta = resultSet.getMeta(); meta = resultSet.getMeta();
if (meta.getAttributes().size() != entityMapping.getAttributeMappings().size()) { if (meta.getAttributes().size() != entityMapping.getValuableAttributeMappings().size()) {
throw new DBCException("Corrupted stream source metadata. Attribute number (" + meta.getAttributes().size() + ") doesn't match target entity attribute number (" + entityMapping.getAttributeMappings().size() + ")"); throw new DBCException("Corrupted stream source metadata. Attribute number (" + meta.getAttributes().size() + ") doesn't match target entity attribute number (" + entityMapping.getAttributeMappings().size() + ")");
} }
} }
@Override @Override
public void fetchRow(DBCSession session, DBCResultSet resultSet) throws DBCException { public void fetchRow(DBCSession session, DBCResultSet resultSet) throws DBCException {
List<StreamProducerSettings.AttributeMapping> attrs = entityMapping.getAttributeMappings(); Object[] row = new Object[attributes.size()];
Object[] row = new Object[attrs.size()]; for (int i = 0; i < attributes.size(); i++) {
for (int i = 0; i < attrs.size(); i++) {
row[i] = resultSet.getAttributeValue(i); row[i] = resultSet.getAttributeValue(i);
} }
rows.add(row); rows.add(row);
......
...@@ -59,8 +59,14 @@ public class StreamProducerSettings implements IDataTransferSettings { ...@@ -59,8 +59,14 @@ public class StreamProducerSettings implements IDataTransferSettings {
return attributeMappings; return attributeMappings;
} }
public void setAttributeMappings(List<AttributeMapping> attributeMappings) { public List<AttributeMapping> getValuableAttributeMappings() {
this.attributeMappings = attributeMappings; List<AttributeMapping> result = new ArrayList<>();
for (AttributeMapping am : attributeMappings) {
if (am.isValuable()) {
result.add(am);
}
}
return result;
} }
public AttributeMapping getAttributeMapping(DBSEntityAttribute attr) { public AttributeMapping getAttributeMapping(DBSEntityAttribute attr) {
...@@ -193,6 +199,10 @@ public class StreamProducerSettings implements IDataTransferSettings { ...@@ -193,6 +199,10 @@ public class StreamProducerSettings implements IDataTransferSettings {
this.sourceColumn = sourceColumn; this.sourceColumn = sourceColumn;
} }
public boolean isValuable() {
return mappingType == MappingType.IMPORT || mappingType == MappingType.DEFAULT_VALUE;
}
} }
private Map<String, EntityMapping> entityMapping = new HashMap<>(); private Map<String, EntityMapping> entityMapping = new HashMap<>();
......
...@@ -36,12 +36,13 @@ public class StreamTransferResultSet implements DBCResultSet { ...@@ -36,12 +36,13 @@ public class StreamTransferResultSet implements DBCResultSet {
private StreamProducerSettings.EntityMapping entityMapping; private StreamProducerSettings.EntityMapping entityMapping;
private List<DBCAttributeMetaData> metaAttrs; private List<DBCAttributeMetaData> metaAttrs;
private Object[] streamRow; private Object[] streamRow;
private final List<StreamProducerSettings.AttributeMapping> attributeMappings;
public StreamTransferResultSet(StreamTransferSession session, DBCStatement statement, StreamProducerSettings.EntityMapping entityMapping) { public StreamTransferResultSet(StreamTransferSession session, DBCStatement statement, StreamProducerSettings.EntityMapping entityMapping) {
this.session = session; this.session = session;
this.statement = statement; this.statement = statement;
this.entityMapping = entityMapping; this.entityMapping = entityMapping;
List<StreamProducerSettings.AttributeMapping> attributeMappings = this.entityMapping.getAttributeMappings(); this.attributeMappings = this.entityMapping.getValuableAttributeMappings();
this.metaAttrs = new ArrayList<>(attributeMappings.size()); this.metaAttrs = new ArrayList<>(attributeMappings.size());
for (int i = 0; i < attributeMappings.size(); i++) { for (int i = 0; i < attributeMappings.size(); i++) {
StreamProducerSettings.AttributeMapping attr = attributeMappings.get(i); StreamProducerSettings.AttributeMapping attr = attributeMappings.get(i);
...@@ -65,7 +66,7 @@ public class StreamTransferResultSet implements DBCResultSet { ...@@ -65,7 +66,7 @@ public class StreamTransferResultSet implements DBCResultSet {
@Override @Override
public Object getAttributeValue(int index) throws DBCException { public Object getAttributeValue(int index) throws DBCException {
StreamProducerSettings.AttributeMapping attr = entityMapping.getAttributeMappings().get(index); StreamProducerSettings.AttributeMapping attr = this.attributeMappings.get(index);
StreamDataImporterColumnInfo sourceColumn = attr.getSourceColumn(); StreamDataImporterColumnInfo sourceColumn = attr.getSourceColumn();
if (sourceColumn != null) { if (sourceColumn != null) {
return streamRow[sourceColumn.getColumnIndex()]; return streamRow[sourceColumn.getColumnIndex()];
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册