提交 7f1b8720 编写于 作者: E Evgeny Fradkin

Mock Data. Monitor usages are reworked - VoidMonitor is avoid.

上级 efcbd5da
......@@ -33,7 +33,6 @@ import org.jkiss.dbeaver.model.edit.DBEPersistAction;
import org.jkiss.dbeaver.model.exec.*;
import org.jkiss.dbeaver.model.impl.AbstractExecutionSource;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.runtime.VoidProgressMonitor;
import org.jkiss.dbeaver.model.sql.SQLUtils;
import org.jkiss.dbeaver.model.struct.DBSAttributeBase;
import org.jkiss.dbeaver.model.struct.DBSDataManipulator;
......@@ -84,7 +83,7 @@ public class MockDataExecuteWizard extends AbstractToolWizard<DBSDataManipulato
public boolean canFinish() {
try {
Collection<? extends DBSEntityAttribute> attributes =
mockDataSettings.getEntity().getAttributes(new VoidProgressMonitor());
mockDataSettings.getEntity().getAttributes(mockDataSettings.getMonitor());
return super.canFinish() && !CommonUtils.isEmpty(DBUtils.getRealAttributes(attributes));
} catch (DBException ex) {
log.error("Error accessing DB entity " + mockDataSettings.getEntity().getName(), ex);
......
......@@ -19,20 +19,16 @@ package org.jkiss.dbeaver.ext.mockdata;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.core.DBeaverUI;
import org.jkiss.dbeaver.ext.mockdata.model.MockGeneratorDescriptor;
import org.jkiss.dbeaver.ext.mockdata.model.MockGeneratorRegistry;
import org.jkiss.dbeaver.model.DBUtils;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress;
import org.jkiss.dbeaver.model.runtime.VoidProgressMonitor;
import org.jkiss.dbeaver.model.struct.*;
import org.jkiss.dbeaver.runtime.properties.PropertySourceCustom;
import org.jkiss.dbeaver.runtime.ui.DBUserInterface;
import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.utils.CommonUtils;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
public class MockDataSettings {
......@@ -49,6 +45,7 @@ public class MockDataSettings {
private DBSEntity entity;
private Collection<DBSAttributeBase> attributes;
private DBRProgressMonitor monitor;
private boolean removeOldData;
private long rowsNumber = 1000;
......@@ -58,45 +55,32 @@ public class MockDataSettings {
private Map<String, AttributeGeneratorProperties> attributeGenerators = new HashMap<>(); // attribute.name -> generators properties
// populate attribute generators properties map
public void init(MockDataExecuteWizard wizard) throws DBException {
public void init(DBRProgressMonitor monitor, MockDataExecuteWizard wizard) throws DBException {
this.monitor = monitor;
List<DBSDataManipulator> databaseObjects = wizard.getDatabaseObjects();
DBSDataManipulator dataManipulator = databaseObjects.iterator().next(); // TODO only the first
entity = (DBSEntity) dataManipulator;
attributes = new ArrayList<>();
try {
DBeaverUI.run(wizard.getContainer(), true, true, new DBRRunnableWithProgress() {
@Override
public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
attributes.addAll(DBUtils.getRealAttributes(entity.getAttributes(monitor)));
MockGeneratorRegistry generatorRegistry = MockGeneratorRegistry.getInstance();
for (DBSAttributeBase attribute : attributes) {
AttributeGeneratorProperties generatorProperties = new AttributeGeneratorProperties(attribute);
attributeGenerators.put(attribute.getName(), generatorProperties);
//((JDBCColumnKeyType) attribute).isInUniqueKey()
List<DBSEntityReferrer> attributeReferrers = DBUtils.getAttributeReferrers(monitor, (DBSEntityAttribute) attribute);
if (!CommonUtils.isEmpty(attributeReferrers)) {
MockGeneratorDescriptor generator = generatorRegistry.getGenerator(FK_GENERATOR_ID);
putGenerator(generatorProperties, generator);
} else {
List<MockGeneratorDescriptor> generators = generatorRegistry.findAllGenerators(dataManipulator.getDataSource(), attribute);
for (MockGeneratorDescriptor generator : generators) {
putGenerator(generatorProperties, generator);
}
}
}
} catch (DBException e) {
throw new InvocationTargetException(e);
}
attributes.addAll(DBUtils.getRealAttributes(entity.getAttributes(monitor)));
MockGeneratorRegistry generatorRegistry = MockGeneratorRegistry.getInstance();
for (DBSAttributeBase attribute : attributes) {
AttributeGeneratorProperties generatorProperties = new AttributeGeneratorProperties(attribute);
attributeGenerators.put(attribute.getName(), generatorProperties);
//((JDBCColumnKeyType) attribute).isInUniqueKey()
List<DBSEntityReferrer> attributeReferrers = DBUtils.getAttributeReferrers(monitor, (DBSEntityAttribute) attribute);
if (!CommonUtils.isEmpty(attributeReferrers)) {
MockGeneratorDescriptor generator = generatorRegistry.getGenerator(FK_GENERATOR_ID);
putGenerator(generatorProperties, generator);
} else {
List<MockGeneratorDescriptor> generators = generatorRegistry.findAllGenerators(dataManipulator.getDataSource(), attribute);
for (MockGeneratorDescriptor generator : generators) {
putGenerator(generatorProperties, generator);
}
});
} catch (InvocationTargetException e) {
DBUserInterface.getInstance().showError("Transfer init failed", "Can't start data transfer", e.getTargetException());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
......@@ -160,6 +144,10 @@ public class MockDataSettings {
return attributeGenerators.get(attribute.getName());
}
public DBRProgressMonitor getMonitor() {
return monitor;
}
public void loadFrom(IDialogSettings dialogSettings) {
removeOldData = dialogSettings.getBoolean(PROP_REMOVE_OLD_DATA);
try {
......
......@@ -35,7 +35,6 @@ import org.jkiss.dbeaver.ext.mockdata.model.MockGeneratorDescriptor;
import org.jkiss.dbeaver.model.DBPEvaluationContext;
import org.jkiss.dbeaver.model.DBUtils;
import org.jkiss.dbeaver.model.DBValueFormatting;
import org.jkiss.dbeaver.model.runtime.VoidProgressMonitor;
import org.jkiss.dbeaver.model.struct.DBSAttributeBase;
import org.jkiss.dbeaver.runtime.properties.PropertySourceCustom;
import org.jkiss.dbeaver.ui.DBeaverIcons;
......@@ -46,6 +45,7 @@ import org.jkiss.dbeaver.ui.dialogs.ActiveWizardPage;
import org.jkiss.dbeaver.ui.properties.PropertyTreeViewer;
import org.jkiss.utils.CommonUtils;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.*;
import java.util.List;
......@@ -177,7 +177,6 @@ public class MockDataWizardPageSettings extends ActiveWizardPage<MockDataExecute
}
});
VoidProgressMonitor monitor = new VoidProgressMonitor(); // TODO VoidProgressMonitor
CellLabelProvider labelProvider = new CellLabelProvider() {
@Override
public void update(ViewerCell cell) {
......@@ -187,7 +186,7 @@ public class MockDataWizardPageSettings extends ActiveWizardPage<MockDataExecute
cell.setImage(DBeaverIcons.getImage(DBValueFormatting.getTypeImage(attribute)));
cell.setText(attribute.getName());
try {
if (DBUtils.checkUnique(monitor, mockDataSettings.getEntity(), attribute)) {
if (DBUtils.checkUnique(mockDataSettings.getMonitor(), mockDataSettings.getEntity(), attribute)) {
cell.setFont(boldFont);
}
} catch (DBException e) {
......@@ -384,54 +383,63 @@ public class MockDataWizardPageSettings extends ActiveWizardPage<MockDataExecute
@Override
public void activatePage() {
try {
// init the generators properties
if (firstInit) {
firstInit = false;
MockDataExecuteWizard wizard = getWizard();
mockDataSettings.init(wizard);
wizard.loadSettings();
removeOldDataCheck.setSelection(mockDataSettings.isRemoveOldData());
rowsText.setText(String.valueOf(mockDataSettings.getRowsNumber()));
generatorsTableViewer.setInput(mockDataSettings.getAttributes());
// init the generators properties
if (firstInit) {
try {
DBeaverUI.run(this.getContainer(), true, true, monitor -> {
try {
firstInit = false;
MockDataExecuteWizard wizard = getWizard();
mockDataSettings.init(monitor, wizard);
wizard.loadSettings();
} catch (DBException ex) {
throw new InvocationTargetException(ex);
}
});
} catch (InvocationTargetException ex) {
log.error("Error Mock Data Settings initialization", ex.getTargetException());
}
catch (InterruptedException e) {
log.error("Mock Data Settings initialization interrupted", e);
}
entityNameText.setText(DBUtils.getObjectFullName(mockDataSettings.getEntity(), DBPEvaluationContext.DML));
propsEditor.getControl().setFocus();
removeOldDataCheck.setSelection(mockDataSettings.isRemoveOldData());
rowsText.setText(String.valueOf(mockDataSettings.getRowsNumber()));
generatorsTableViewer.setInput(mockDataSettings.getAttributes());
}
// select the attributes table item
final Table table = generatorsTableViewer.getTable();
if (table.getItemCount() > 0) {
int selectedItemIndex = 0;
TableItem selectedItem = null;
String selectedAttribute = mockDataSettings.getSelectedAttribute();
if (selectedAttribute != null) {
for (int i = 0; i < table.getItemCount(); i++) {
if (selectedAttribute.equals(table.getItem(i).getText())) {
selectedItemIndex = i;
selectedItem = table.getItem(i);
break;
}
entityNameText.setText(DBUtils.getObjectFullName(mockDataSettings.getEntity(), DBPEvaluationContext.DML));
propsEditor.getControl().setFocus();
// select the attributes table item
final Table table = generatorsTableViewer.getTable();
if (table.getItemCount() > 0) {
int selectedItemIndex = 0;
TableItem selectedItem = null;
String selectedAttribute = mockDataSettings.getSelectedAttribute();
if (selectedAttribute != null) {
for (int i = 0; i < table.getItemCount(); i++) {
if (selectedAttribute.equals(table.getItem(i).getText())) {
selectedItemIndex = i;
selectedItem = table.getItem(i);
break;
}
}
table.select(selectedItemIndex);
if (selectedItem != null) {
table.showItem(selectedItem);
}
// and notify the listeners
Event event = new Event();
event.widget = table;
event.display = table.getDisplay();
event.item = table.getItem(selectedItemIndex);
event.type = SWT.Selection;
table.notifyListeners(SWT.Selection, event);
} else {
noGeneratorInfoLabel.setText("No attributes in the table");
noGeneratorInfoLabel.setVisible(true);
}
} catch (DBException ex) {
log.error("Error of initializing the Mock Data settings", ex);
table.select(selectedItemIndex);
if (selectedItem != null) {
table.showItem(selectedItem);
}
// and notify the listeners
Event event = new Event();
event.widget = table;
event.display = table.getDisplay();
event.item = table.getItem(selectedItemIndex);
event.type = SWT.Selection;
table.notifyListeners(SWT.Selection, event);
} else {
noGeneratorInfoLabel.setText("No attributes in the table");
noGeneratorInfoLabel.setVisible(true);
}
updatePageCompletion();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册