Skip to content

Commit

Permalink
#4387 MockData: skip generator for a column. Skip auto-generated attr…
Browse files Browse the repository at this point in the history
…ibutes by default
  • Loading branch information
serge-rider committed Nov 4, 2018
1 parent d0ec090 commit 9c7a882
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 78 deletions.
Expand Up @@ -36,6 +36,7 @@
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.sql.SQLUtils;
import org.jkiss.dbeaver.model.struct.*;
import org.jkiss.dbeaver.runtime.properties.PropertySourceCustom;
import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.dbeaver.ui.dialogs.tools.AbstractToolWizard;
import org.jkiss.utils.CommonUtils;
Expand Down Expand Up @@ -200,23 +201,23 @@ public boolean executeProcess(DBRProgressMonitor monitor, DBSDataManipulator dat

logPage.appendLog(NLS.bind(MockDataMessages.tools_mockdata_wizard_log_inserting_into, dataManipulator.getName()));
DBCStatistics insertStats = new DBCStatistics();
persistActions = new ArrayList<>();

// build and init the generators
generators.clear();
DBSEntity dbsEntity = (DBSEntity) dataManipulator;
Collection<? extends DBSAttributeBase> attributes = DBUtils.getRealAttributes(dbsEntity.getAttributes(monitor));
for (DBSAttributeBase attribute : attributes) {
MockGeneratorDescriptor generatorDescriptor = mockDataSettings.getGeneratorDescriptor(mockDataSettings.getAttributeGeneratorProperties(attribute).getSelectedGeneratorId());
if (generatorDescriptor != null) {
MockValueGenerator generator = generatorDescriptor.createGenerator();
MockGeneratorDescriptor attrGenerator = mockDataSettings.getAttributeGeneratorProperties(attribute).getSelectedGenerator();
if (attrGenerator != null) {
MockValueGenerator generator = attrGenerator.createGenerator();

MockDataSettings.AttributeGeneratorProperties generatorPropertySource = this.mockDataSettings.getAttributeGeneratorProperties(attribute);
String selectedGenerator = generatorPropertySource.getSelectedGeneratorId();
Map<Object, Object> generatorProperties =
generatorPropertySource.getGeneratorPropertySource(selectedGenerator).getPropertiesWithDefaults();
generator.init(dataManipulator, attribute, generatorProperties);
generators.put(attribute.getName(), generator);
PropertySourceCustom generatorProperties = generatorPropertySource.getGeneratorProperties();
if (generatorProperties != null) {
Map<Object, Object> propValues = generatorProperties.getPropertiesWithDefaults();
generator.init(dataManipulator, attribute, propValues);
generators.put(attribute.getName(), generator);
}
}
}

Expand Down
Expand Up @@ -82,4 +82,6 @@ public MockDataMessages() {
public static String tools_mockdata_wizard_log_error_inserting;
public static String tools_mockdata_wizard_log_error_generating;

public static String tools_mockdata_attribute_generator_skip;

}
Expand Up @@ -45,3 +45,4 @@ tools_mockdata_wizard_log_inserting_into=Inserting mock data into the "{0}".\n
tools_mockdata_wizard_log_inserted_rows={0} rows inserted
tools_mockdata_wizard_log_error_inserting= Error inserting mock data: {0}
tools_mockdata_wizard_log_error_generating= Error generating mock data: {0}
tools_mockdata_attribute_generator_skip = <Skip>
Expand Up @@ -42,6 +42,10 @@ public class MockDataSettings {
public static final String KEY_PRESET_ID = "presetId"; //$NON-NLS-1$
public static final String KEY_GENERATOR_SECTION = "GENERATOR_SECTION"; //$NON-NLS-1$

static final String NO_GENERATOR_ID = "<no generator>";

static final String NO_GENERATOR_LABEL = MockDataMessages.tools_mockdata_attribute_generator_skip;

private DBSEntity entity;
private Collection<DBSAttributeBase> attributes;
private DBRProgressMonitor monitor;
Expand All @@ -63,7 +67,12 @@ public void init(DBRProgressMonitor monitor, MockDataExecuteWizard wizard) throw
entity = (DBSEntity) dataManipulator;
attributes = new ArrayList<>();

attributes.addAll(DBUtils.getRealAttributes(entity.getAttributes(monitor)));
for (DBSAttributeBase attr : CommonUtils.safeCollection(entity.getAttributes(monitor))) {
if (DBUtils.isPseudoAttribute(attr) || DBUtils.isHiddenObject(attr)) {
continue;
}
attributes.add(attr);
}

MockGeneratorRegistry generatorRegistry = MockGeneratorRegistry.getInstance();
for (DBSAttributeBase attribute : attributes) {
Expand Down Expand Up @@ -102,7 +111,7 @@ public void setSelectedAttribute(String selectedAttribute) {

private void putGenerator(AttributeGeneratorProperties generatorProperties, MockGeneratorDescriptor generator) {
generatorDescriptors.put(generator.getId(), generator);
generatorProperties.putGeneratorPropertySource(generator.getId(), new PropertySourceCustom(generator.getProperties(), null));
generatorProperties.putGeneratorProperties(generator.getId(), new PropertySourceCustom(generator.getProperties(), null));
}

public MockGeneratorDescriptor getGeneratorDescriptor(String generatorId) {
Expand Down Expand Up @@ -182,11 +191,17 @@ public void loadFrom(IDialogSettings dialogSettings) {
AttributeGeneratorProperties attrGeneratorProperties = entry.getValue();

// set the saved generator
if (savedGeneratorId != null) {
attrGeneratorProperties.setSelectedGeneratorId(savedGeneratorId);
attrGeneratorProperties.setPresetId(attributeSection.get(KEY_PRESET_ID));
if (NO_GENERATOR_ID.equals(savedGeneratorId)) {
attrGeneratorProperties.setSelectedGenerator(null);
attrGeneratorProperties.setPresetId(null);
} else if (!CommonUtils.isEmpty(savedGeneratorId)) {
MockGeneratorDescriptor generatorDescriptor = attrGeneratorProperties.getGenerator(savedGeneratorId);
if (generatorDescriptor != null) {
attrGeneratorProperties.setSelectedGenerator(generatorDescriptor);
attrGeneratorProperties.setPresetId(attributeSection.get(KEY_PRESET_ID));
}

PropertySourceCustom generatorPropertySource = attrGeneratorProperties.getGeneratorPropertySource(savedGeneratorId);
PropertySourceCustom generatorPropertySource = attrGeneratorProperties.getGeneratorProperties();
IDialogSettings generatorSection = UIUtils.getSettingsSection(attributeSection, KEY_GENERATOR_SECTION);
if (generatorPropertySource != null) {
Map<Object, Object> properties = generatorPropertySource.getPropertiesWithDefaults();
Expand All @@ -208,6 +223,11 @@ public void loadFrom(IDialogSettings dialogSettings) {

public void autoAssignGenerator(AttributeGeneratorProperties attrGeneratorProperties) {
DBSAttributeBase attribute = attrGeneratorProperties.getAttribute();
if (attribute.isAutoGenerated()) {
// Do not generate it by default
attrGeneratorProperties.setSelectedGenerator(null);
return;
}
String attributeName = attribute.getName().toLowerCase();
Set<String> attrGeneratorIds = attrGeneratorProperties.getGenerators();
boolean found = false;
Expand All @@ -216,12 +236,14 @@ public void autoAssignGenerator(AttributeGeneratorProperties attrGeneratorProper
for (String tag : generatorDescriptor.getTags()) {
// find & set the appropriate generator
if (attributeName.contains(tag)) {
attrGeneratorProperties.setSelectedGeneratorId(generatorId);
attrGeneratorProperties.setSelectedGenerator(generatorDescriptor);
found = true;
break;
}
}
if (found) break;
if (found) {
break;
}
}
if (!found) {
// set the default generator
Expand All @@ -244,8 +266,10 @@ public void autoAssignGenerator(AttributeGeneratorProperties attrGeneratorProper

// prevents set non-acceptable generator
private void setSelectedGenerator(AttributeGeneratorProperties attrGeneratorProperties, String generatorId) {
if (attrGeneratorProperties.getGenerators().contains(generatorId)) {
attrGeneratorProperties.setSelectedGeneratorId(generatorId);
if (generatorId == null) {
attrGeneratorProperties.setSelectedGenerator(null);
} else {
attrGeneratorProperties.setSelectedGenerator(attrGeneratorProperties.getGenerator(generatorId));
}
}

Expand All @@ -262,13 +286,16 @@ void saveTo(IDialogSettings dialogSettings) {

AttributeGeneratorProperties attrGeneratorProperties = attrEntry.getValue();
IDialogSettings attributeSection = UIUtils.getSettingsSection(tableSection, attributeName);
String selectedGeneratorId = attrGeneratorProperties.getSelectedGeneratorId();
attributeSection.put(KEY_SELECTED_GENERATOR, selectedGeneratorId);
attributeSection.put(KEY_PRESET_ID, attrGeneratorProperties.getPresetId());
MockGeneratorDescriptor selectedGenerator = attrGeneratorProperties.getSelectedGenerator();
if (selectedGenerator == null) {
attributeSection.put(KEY_SELECTED_GENERATOR, NO_GENERATOR_ID);
attributeSection.put(KEY_PRESET_ID, (String)null);
} else {
attributeSection.put(KEY_SELECTED_GENERATOR, selectedGenerator.getId());
attributeSection.put(KEY_PRESET_ID, attrGeneratorProperties.getPresetId());

IDialogSettings generatorSection = UIUtils.getSettingsSection(attributeSection, KEY_GENERATOR_SECTION);
if (selectedGeneratorId != null) {
PropertySourceCustom generatorPropertySource = attrGeneratorProperties.getGeneratorPropertySource(selectedGeneratorId);
IDialogSettings generatorSection = UIUtils.getSettingsSection(attributeSection, KEY_GENERATOR_SECTION);
PropertySourceCustom generatorPropertySource = attrGeneratorProperties.getGeneratorProperties();
if (generatorPropertySource != null) {
Map<Object, Object> properties = generatorPropertySource.getPropertiesWithDefaults();
for (Map.Entry<Object, Object> propEntry : properties.entrySet()) {
Expand All @@ -281,7 +308,7 @@ void saveTo(IDialogSettings dialogSettings) {

public class AttributeGeneratorProperties {
private final DBSAttributeBase attribute;
private String selectedGeneratorId = null;
private MockGeneratorDescriptor selectedGenerator = null;
private String presetId = null;
private Map<String, PropertySourceCustom> generators = new TreeMap<>(); // generatorId -> PropertySourceCustom

Expand All @@ -293,37 +320,34 @@ public DBSAttributeBase getAttribute() {
return attribute;
}

public String getSelectedGeneratorId() {
if (selectedGeneratorId == null && !CommonUtils.isEmpty(getGenerators())) {
selectedGeneratorId = getGenerators().iterator().next();
}
return selectedGeneratorId;
public MockGeneratorDescriptor getSelectedGenerator() {
return selectedGenerator;
}

public Set<String> getGenerators() {
return generators.keySet();
}

public String setSelectedGeneratorId(String selectedGeneratorId) {
if ((selectedGeneratorId == null || !generatorDescriptors.keySet().contains(selectedGeneratorId)) && !CommonUtils.isEmpty(getGenerators())) {
selectedGeneratorId = getGenerators().iterator().next();
}
if (!CommonUtils.equalObjects(this.selectedGeneratorId, selectedGeneratorId)) {
this.selectedGeneratorId = selectedGeneratorId;
presetId = null;
public void setSelectedGenerator(MockGeneratorDescriptor generator) {
this.selectedGenerator = generator;
}

public MockGeneratorDescriptor autoDetectGenerator() {
if (!CommonUtils.isEmpty(getGenerators())) {
String genId = getGenerators().iterator().next();
selectedGenerator = generatorDescriptors.get(genId);
}
return selectedGeneratorId;
this.presetId = null;

return selectedGenerator;
}

public void putGeneratorPropertySource(String generatorId, PropertySourceCustom propertySource) {
public void putGeneratorProperties(String generatorId, PropertySourceCustom propertySource) {
generators.put(generatorId, propertySource);
}

public PropertySourceCustom getGeneratorPropertySource(String generatorId) {
if (generatorId == null) {
generatorId = getSelectedGeneratorId();
}
return generatorId == null ? null : generators.get(generatorId);
public PropertySourceCustom getGeneratorProperties() {
return selectedGenerator == null ? null : generators.get(selectedGenerator.getId());
}

public String getPresetId() {
Expand All @@ -337,5 +361,13 @@ public void setPresetId(String presetId) {
public boolean isEmpty() {
return generators.isEmpty();
}

public boolean isSkip() {
return selectedGenerator == null;
}

public MockGeneratorDescriptor getGenerator(String generatorId) {
return generatorDescriptors.get(generatorId);
}
}
}

0 comments on commit 9c7a882

Please sign in to comment.