提交 4fdebd34 编写于 作者: S serge-rider

#6570 ERD save (for entities and containers). EntityEditor save prompt fix (show sub-editor names)

上级 baea716f
......@@ -33,7 +33,7 @@ import org.jkiss.dbeaver.model.exec.DBExecUtils;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.runtime.load.DatabaseLoadService;
import org.jkiss.dbeaver.model.struct.*;
import org.jkiss.dbeaver.model.virtual.DBVEntity;
import org.jkiss.dbeaver.model.virtual.DBVObject;
import org.jkiss.dbeaver.model.virtual.DBVUtils;
import org.jkiss.dbeaver.runtime.DBWorkbench;
import org.jkiss.dbeaver.ui.IActiveWorkbenchPart;
......@@ -322,17 +322,17 @@ public class ERDEditorEmbedded extends ERDEditorPart implements IDatabaseEditor,
public void doSave(IProgressMonitor monitor) {
try {
// Save in virtual model as entity property.
DBVEntity vEntity = this.getVirtualEntity();
if (vEntity == null) {
DBVObject vObject = this.getVirtualObject();
if (vObject == null) {
return;
}
Map<String, Object> diagramStateMap = new LinkedHashMap<>();
vEntity.setProperty(PROP_DIAGRAM_STATE, diagramStateMap);
vObject.setProperty(PROP_DIAGRAM_STATE, diagramStateMap);
String diagramState = DiagramLoader.serializeDiagram(RuntimeUtils.makeMonitor(monitor), getDiagramPart(), getDiagram(), false, true);
diagramStateMap.put(PROPS_DIAGRAM_SERIALIZED, diagramState);
vEntity.persistConfiguration();
vObject.persistConfiguration();
getCommandStack().markSaveLocation();
} catch (Exception e) {
......@@ -341,12 +341,13 @@ public class ERDEditorEmbedded extends ERDEditorPart implements IDatabaseEditor,
}
@Nullable
private DBVEntity getVirtualEntity() {
private DBVObject getVirtualObject() {
DBSObject rootObject = getRootObject();
if (rootObject instanceof DBSEntity) {
return DBVUtils.getVirtualEntity((DBSEntity) rootObject, true);
} else {
return DBVUtils.getVirtualObject(rootObject, true);
}
return null;
}
}
......@@ -23,6 +23,7 @@ import org.jkiss.dbeaver.model.DBPDataSource;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.struct.DBSObject;
import org.jkiss.dbeaver.model.struct.DBSObjectContainer;
import org.jkiss.utils.CommonUtils;
import java.util.Collection;
import java.util.LinkedHashMap;
......@@ -34,6 +35,7 @@ import java.util.Map;
public class DBVContainer extends DBVObject implements DBSObjectContainer {
static final String ENTITY_PREFIX = ":";
static final String CONFIG_PREFIX = "@";
private final DBVContainer parent;
private String name;
......@@ -58,6 +60,7 @@ public class DBVContainer extends DBVObject implements DBSObjectContainer {
for (Map.Entry<String, DBVEntity> ee : source.entities.entrySet()) {
this.entities.put(ee.getKey(), new DBVEntity(this, ee.getValue()));
}
super.copyFrom(source);
}
DBVContainer(DBVContainer parent, String name, Map<String, Object> map) {
......@@ -67,7 +70,11 @@ public class DBVContainer extends DBVObject implements DBSObjectContainer {
String id = element.getKey();
if (id.startsWith(ENTITY_PREFIX)) {
DBVEntity entity = new DBVEntity(this, id.substring(ENTITY_PREFIX.length()), (Map<String, Object>) element.getValue());
entities.put(entity.getName(), entity);
} else if (id.startsWith(CONFIG_PREFIX)) {
String configMap = id.substring(CONFIG_PREFIX.length());
if (configMap.equals("properties")) {
loadPropertiesFrom(map, id);
}
} else if (element.getValue() instanceof Map) {
DBVContainer child = new DBVContainer(this, id, (Map<String, Object>) element.getValue());
containers.put(child.getName(), child);
......@@ -161,6 +168,9 @@ public class DBVContainer extends DBVObject implements DBSObjectContainer {
@Override
public boolean hasValuableData() {
if (!CommonUtils.isEmpty(getProperties())) {
return true;
}
for (DBVEntity entity : getEntities()) {
if (entity.hasValuableData()) {
return true;
......
......@@ -62,7 +62,6 @@ public class DBVEntity extends DBVObject implements DBSEntity, DBPQualifiedObjec
private List<DBVEntityConstraint> entityConstraints;
private List<DBVEntityForeignKey> entityForeignKeys;
private List<DBVEntityAttribute> entityAttributes;
private Map<String, Object> properties;
private List<DBVColorOverride> colorOverrides;
public DBVEntity(@NotNull DBVContainer container, @NotNull String name, String descriptionColumnNames) {
......@@ -99,15 +98,13 @@ public class DBVEntity extends DBVObject implements DBSEntity, DBPQualifiedObjec
this.entityAttributes.add(new DBVEntityAttribute(this, null, attribute));
}
}
if (!CommonUtils.isEmpty(src.properties)) {
this.properties = new LinkedHashMap<>(src.properties);
}
if (!CommonUtils.isEmpty(src.colorOverrides)) {
this.colorOverrides = new ArrayList<>(src.colorOverrides.size());
for (DBVColorOverride co : src.colorOverrides) {
this.colorOverrides.add(new DBVColorOverride(co));
}
}
super.copyFrom(src);
}
DBVEntity(@NotNull DBVContainer container, @NotNull String name, @NotNull Map<String, Object> map) {
......@@ -176,7 +173,7 @@ public class DBVEntity extends DBVObject implements DBSEntity, DBPQualifiedObjec
}
addColorOverride(curColor);
}
properties = JSONUtils.deserializeProperties(map, "properties");
loadPropertiesFrom(map, "properties");
}
@NotNull
......@@ -236,31 +233,6 @@ public class DBVEntity extends DBVObject implements DBSEntity, DBPQualifiedObjec
return DBSEntityType.VIRTUAL_ENTITY;
}
/**
* Property value can be String, Number, Boolean, List or Map
* @param name property name
*/
@Nullable
public Object getProperty(@NotNull String name) {
return CommonUtils.isEmpty(properties) ? null : properties.get(name);
}
public void setProperty(String name, @Nullable Object value) {
if (properties == null) {
properties = new LinkedHashMap<>();
}
if (value == null) {
properties.remove(name);
} else {
properties.put(name, value);
}
}
@NotNull
public Map<String, Object> getProperties() {
return properties == null ? Collections.emptyMap() : properties;
}
public List<DBVEntityAttribute> getEntityAttributes() {
return entityAttributes;
}
......@@ -554,7 +526,7 @@ public class DBVEntity extends DBVObject implements DBSEntity, DBPQualifiedObjec
@Override
public boolean hasValuableData() {
if (!CommonUtils.isEmpty(descriptionColumnNames) ||
!CommonUtils.isEmpty(properties) ||
!CommonUtils.isEmpty(getProperties()) ||
!CommonUtils.isEmpty(entityForeignKeys) ||
!CommonUtils.isEmpty(colorOverrides))
{
......@@ -598,13 +570,6 @@ public class DBVEntity extends DBVObject implements DBSEntity, DBPQualifiedObjec
}
}
public void persistConfiguration() {
DBPDataSource dataSource = getDataSource();
if (dataSource != null) {
dataSource.getContainer().persistConfiguration();
}
}
@Override
public <T> T getAdapter(Class<T> adapter) {
return null;
......
......@@ -121,6 +121,32 @@ public class DBVModel extends DBVContainer {
return container.getEntity(entity.getName(), createNew);
}
public DBVObject findObject(DBSObject source, boolean create) {
DBSObject[] path = DBUtils.getObjectPath(source, true);
if (path.length == 0) {
log.warn("Empty entity path");
return null;
}
if (path[0] != dataSourceContainer) {
log.warn("Entity's root must be datasource container '" + dataSourceContainer.getName() + "'");
return null;
}
DBVContainer container = this;
for (int i = 1; i < path.length; i++) {
DBSObject item = path[i];
DBVContainer childContainer = container.getContainer(item.getName(), create);
if (childContainer == null) {
if (i == path.length - 1) {
return container.getEntity(item.getName(), create);
}
return null;
} else {
container = childContainer;
}
}
return container;
}
public void serialize(DBRProgressMonitor monitor, JsonWriter json) throws IOException, DBException {
DBVModelSerializerModern.serializeContainer(monitor, json, this);
}
......
......@@ -53,6 +53,7 @@ class DBVModelSerializerModern implements DBVModelSerializer
}
json.beginObject();
JSONUtils.serializeProperties(json, DBVContainer.CONFIG_PREFIX + "properties", object.getProperties());
for (DBVEntity entity : object.getEntities()) {
if (entity.hasValuableData()) {
serializeEntity(monitor, json, entity);
......
......@@ -16,8 +16,18 @@
*/
package org.jkiss.dbeaver.model.virtual;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.DBPDataSource;
import org.jkiss.dbeaver.model.data.json.JSONUtils;
import org.jkiss.dbeaver.model.struct.DBSObject;
import org.jkiss.utils.CommonUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* Virtual model object
......@@ -27,6 +37,7 @@ public abstract class DBVObject implements DBSObject {
static final Log log = Log.getLog(DBVObject.class);
private DBVTransformSettings transformSettings;
private Map<String, Object> properties;
@Override
public abstract DBVContainer getParentObject();
......@@ -46,4 +57,47 @@ public abstract class DBVObject implements DBSObject {
abstract public boolean hasValuableData();
/**
* Property value can be String, Number, Boolean, List or Map
* @param name property name
*/
@Nullable
public Object getProperty(@NotNull String name) {
return CommonUtils.isEmpty(properties) ? null : properties.get(name);
}
public void setProperty(String name, @Nullable Object value) {
if (properties == null) {
properties = new LinkedHashMap<>();
}
if (value == null) {
properties.remove(name);
} else {
properties.put(name, value);
}
}
@NotNull
public Map<String, Object> getProperties() {
return properties == null ? Collections.emptyMap() : properties;
}
protected void copyFrom(@NotNull DBVObject src) {
if (!CommonUtils.isEmpty(src.properties)) {
this.properties = new LinkedHashMap<>(src.properties);
}
}
protected void loadPropertiesFrom(@NotNull Map<String, Object> map, String elemName) {
properties = JSONUtils.deserializeProperties(map, elemName);
}
public void persistConfiguration() {
DBPDataSource dataSource = getDataSource();
if (dataSource != null) {
dataSource.getContainer().persistConfiguration();
}
}
}
......@@ -303,4 +303,10 @@ public abstract class DBVUtils {
return entity;
}
public static DBVObject getVirtualObject(DBSObject source, boolean create) {
if (source instanceof DBVObject) {
return (DBVObject) source;
}
return source.getDataSource().getContainer().getVirtualModel().findObject(source, create);
}
}
......@@ -704,6 +704,9 @@ class DataSourceSerializerModern implements DataSourceSerializer
json.beginObject();
for (DBPConnectionEventType eventType : connectionInfo.getDeclaredEvents()) {
DBRShellCommand command = connectionInfo.getEvent(eventType);
if (!command.isEnabled()) {
continue;
}
json.name(eventType.name());
json.beginObject();
JSONUtils.field(json, RegistryConstants.ATTR_ENABLED, command.isEnabled());
......
......@@ -61,6 +61,7 @@ import org.jkiss.dbeaver.ui.controls.folders.ITabbedFolderContainer;
import org.jkiss.dbeaver.ui.controls.folders.ITabbedFolderListener;
import org.jkiss.dbeaver.ui.dialogs.ConfirmationDialog;
import org.jkiss.dbeaver.ui.editors.*;
import org.jkiss.dbeaver.ui.editors.entity.properties.ObjectPropertiesEditor;
import org.jkiss.dbeaver.ui.internal.UINavigatorMessages;
import org.jkiss.dbeaver.ui.navigator.NavigatorPreferences;
import org.jkiss.dbeaver.ui.navigator.actions.NavigatorHandlerObjectOpen;
......@@ -682,13 +683,22 @@ public class EntityEditor extends MultiPageDatabaseEditor
public int promptToSaveOnClose()
{
List<String> changedSubEditors = new ArrayList<>();
final DBECommandContext commandContext = getCommandContext();
if (commandContext != null && commandContext.isDirty()) {
changedSubEditors.add(UINavigatorMessages.registry_entity_editor_descriptor_name);
}
for (IEditorPart editor : editorMap.values()) {
if (editor.isDirty()) {
changedSubEditors.add(editor.getTitle());
EntityEditorDescriptor editorDescriptor = EntityEditorsRegistry.getInstance().getEntityEditor(editor);
if (editorDescriptor != null) {
changedSubEditors.add(editorDescriptor.getName());
}
}
}
String subEditorsString = "(" + String.join(", ", changedSubEditors) + ")";
String subEditorsString = changedSubEditors.isEmpty() ? "" : "(" + String.join(", ", changedSubEditors) + ")";
final int result = ConfirmationDialog.showConfirmDialog(
ResourceBundle.getBundle(UINavigatorMessages.BUNDLE_NAME),
getSite().getShell(),
......
......@@ -174,6 +174,10 @@ public class EntityEditorDescriptor extends AbstractContextDescriptor
}
}
public ObjectType getEditorType() {
return editorType;
}
@Override
public String toString() {
return id;
......
......@@ -20,6 +20,7 @@ package org.jkiss.dbeaver.ui.editors.entity;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;
import org.eclipse.ui.IEditorPart;
import org.jkiss.dbeaver.model.DBPObject;
import org.jkiss.utils.CommonUtils;
......@@ -114,4 +115,12 @@ public class EntityEditorsRegistry {
return null;
}
public EntityEditorDescriptor getEntityEditor(IEditorPart editor) {
for (EntityEditorDescriptor ed : entityEditors) {
if (ed.getEditorType().getImplName().equals(editor.getClass().getName())) {
return ed;
}
}
return null;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册