Skip to content

Commit

Permalink
Extension data to home workspace (#11553)
Browse files Browse the repository at this point in the history
* workspacemodel thumbnail api + test

* add graphDocumentationURL + serialise and deserialise Author

* Update WorkspaceModel.cs

* handle empty URIs

* Update 2080_JSONTESTCRASH undo_redo.dyn

* move thumbnail and help link to HomeWorkspaceMode

* move extension data to HomeWorkspaceModel
  • Loading branch information
SHKnudsen authored Mar 19, 2021
1 parent b49c661 commit 959130d
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 67 deletions.
46 changes: 45 additions & 1 deletion src/DynamoCore/Graph/Workspaces/HomeWorkspaceModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Xml;
using Dynamo.Core;
using Dynamo.Engine;
using Dynamo.Extensions;
using Dynamo.Graph.Annotations;
using Dynamo.Graph.Nodes;
using Dynamo.Graph.Nodes.NodeLoaders;
Expand Down Expand Up @@ -131,6 +132,14 @@ public string Thumbnail
}
}

/// <summary>
/// List of user defined data from extensions and view extensions stored in the graph
/// </summary>
internal ICollection<ExtensionData> ExtensionData
{
get; set;
}

/// <summary>
/// In near future, the file loading mechanism will be completely moved
/// into WorkspaceModel, that's the time we removed this property setter below.
Expand Down Expand Up @@ -320,6 +329,7 @@ public HomeWorkspaceModel(EngineController engine,
this.verboseLogging = verboseLogging;
IsTestMode = isTestMode;
EngineController = engine;
this.ExtensionData = new List<ExtensionData>();

// The first time the preloaded trace data is set, we cache
// the data as historical. This will be used after the initial
Expand Down Expand Up @@ -811,6 +821,40 @@ internal IList<ISerializable> GetOrphanedSerializablesAndClearHistoricalTraceDat
historicalTraceData = null;

return orphans;
}
}

internal bool TryGetMatchingWorkspaceData(string uniqueId, out Dictionary<string, string> data)
{
data = new Dictionary<string, string>();
if (!ExtensionData.Any())
return false;

var extensionData = ExtensionData.Where(x => x.ExtensionGuid == uniqueId)
.FirstOrDefault();

if (extensionData is null)
return false;

data = extensionData.Data;
return true;
}

internal void UpdateExtensionData(string uniqueId, Dictionary<string, string> data)
{
var extensionData = ExtensionData.Where(x => x.ExtensionGuid == uniqueId)
.FirstOrDefault();

if (extensionData is null)
return;

extensionData.Data = data;
}

internal void CreateNewExtensionData(string uniqueId, string name, string version, Dictionary<string, string> data)
{
// TODO: Figure out how to add extension version when creating new ExtensionData
var extensionData = new ExtensionData(uniqueId, name, version, data);
ExtensionData.Add(extensionData);
}
}
}
16 changes: 9 additions & 7 deletions src/DynamoCore/Graph/Workspaces/SerializationConverters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -646,25 +646,28 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
Enumerable.Empty<PresetModel>(), elementResolver,
info, verboseLogging, isTestMode);

// Thumbnail
if (obj.TryGetValue(nameof(HomeWorkspaceModel.Thumbnail), StringComparison.OrdinalIgnoreCase, out JToken thumbnail))
homeWorkspace.Thumbnail = thumbnail.ToString();

// GraphDocumentaionLink
if (obj.TryGetValue(nameof(HomeWorkspaceModel.GraphDocumentationURL), StringComparison.OrdinalIgnoreCase, out JToken helpLink))
{
if (Uri.TryCreate(helpLink.ToString(), UriKind.Absolute, out Uri uri))
homeWorkspace.GraphDocumentationURL = uri;
}

// ExtensionData
homeWorkspace.ExtensionData = GetExtensionData(serializer, obj);

ws = homeWorkspace;
}

ws.NodeLibraryDependencies = nodeLibraryDependencies.ToList();
ws.ExtensionData = GetExtensionData(serializer, obj);

if (obj.TryGetValue(nameof(WorkspaceModel.Author), StringComparison.OrdinalIgnoreCase, out JToken author))
ws.Author = author.ToString();


return ws;
}

Expand Down Expand Up @@ -781,11 +784,6 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
// NodeLibraryDependencies
writer.WritePropertyName(WorkspaceReadConverter.NodeLibraryDependenciesPropString);
serializer.Serialize(writer, ws.NodeLibraryDependencies);

// ExtensionData
writer.WritePropertyName(WorkspaceReadConverter.EXTENSION_WORKSPACE_DATA);
serializer.Serialize(writer, ws.ExtensionData);


if (!isCustomNode && ws is HomeWorkspaceModel hws)
{
Expand All @@ -796,6 +794,10 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
// GraphDocumentaionLink
writer.WritePropertyName(nameof(HomeWorkspaceModel.GraphDocumentationURL));
writer.WriteValue(hws.GraphDocumentationURL);

// ExtensionData
writer.WritePropertyName(WorkspaceReadConverter.EXTENSION_WORKSPACE_DATA);
serializer.Serialize(writer, hws.ExtensionData);
}

// Graph Author
Expand Down
43 changes: 0 additions & 43 deletions src/DynamoCore/Graph/Workspaces/WorkspaceModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -724,14 +724,6 @@ public string Description
}
}

/// <summary>
/// List of user defined data from extensions and view extensions stored in the graph
/// </summary>
internal ICollection<ExtensionData> ExtensionData
{
get; set;
}

/// <summary>
/// Are there unsaved changes in the workspace?
/// </summary>
Expand Down Expand Up @@ -1045,7 +1037,6 @@ protected WorkspaceModel(
this.annotations = new List<AnnotationModel>(annotations);

this.NodeLibraryDependencies = new List<INodeLibraryDependencyInfo>();
this.ExtensionData = new List<ExtensionData>();

// Set workspace info from WorkspaceInfo object
Name = info.Name;
Expand Down Expand Up @@ -2158,40 +2149,6 @@ private Guid IdToGuidConverter(string id)

return deterministicGuid;
}

internal bool TryGetMatchingWorkspaceData(string uniqueId, out Dictionary<string, string> data)
{
data = new Dictionary<string, string>();
if (!ExtensionData.Any())
return false;

var extensionData = ExtensionData.Where(x => x.ExtensionGuid == uniqueId)
.FirstOrDefault();

if (extensionData is null)
return false;

data = extensionData.Data;
return true;
}

internal void UpdateExtensionData(string uniqueId, Dictionary<string, string> data)
{
var extensionData = ExtensionData.Where(x => x.ExtensionGuid == uniqueId)
.FirstOrDefault();

if (extensionData is null)
return;

extensionData.Data = data;
}

internal void CreateNewExtensionData(string uniqueId, string name, string version, Dictionary<string, string> data)
{
// TODO: Figure out how to add extension version when creating new ExtensionData
var extensionData = new ExtensionData(uniqueId, name, version, data);
ExtensionData.Add(extensionData);
}

/// <summary>
/// Returns a DelayedGraphExecution object.
Expand Down
14 changes: 8 additions & 6 deletions src/DynamoCore/Models/DynamoModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ internal void OnWorkspaceSaving(WorkspaceModel workspace, SaveContext saveContex
if (WorkspaceSaving != null)
{
WorkspaceSaving(workspace, saveContext);
HandleStorageExtensionsOnWorkspaceSaving(workspace, saveContext);
if (workspace is HomeWorkspaceModel hws)
HandleStorageExtensionsOnWorkspaceSaving(hws, saveContext);
}
}

Expand Down Expand Up @@ -194,7 +195,8 @@ internal void OnWorkspaceOpened(WorkspaceModel workspace)
if(WorkspaceOpened != null)
{
WorkspaceOpened.Invoke(workspace);
HandleStorageExtensionsOnWorkspaceOpened(workspace);
if (workspace is HomeWorkspaceModel hws)
HandleStorageExtensionsOnWorkspaceOpened(hws);
}
}

Expand Down Expand Up @@ -972,23 +974,23 @@ private void RemoveExtension(IExtension ext)
}


private void HandleStorageExtensionsOnWorkspaceOpened(WorkspaceModel workspace)
private void HandleStorageExtensionsOnWorkspaceOpened(HomeWorkspaceModel workspace)
{
foreach (var extension in extensionManager.StorageAccessExtensions)
{
RaiseIExtensionStorageAccessWorkspaceOpened(workspace, extension, this.Logger);
}
}

private void HandleStorageExtensionsOnWorkspaceSaving(WorkspaceModel workspace, SaveContext saveContext)
private void HandleStorageExtensionsOnWorkspaceSaving(HomeWorkspaceModel workspace, SaveContext saveContext)
{
foreach (var extension in extensionManager.StorageAccessExtensions)
{
RaiseIExtensionStorageAccessWorkspaceSaving(workspace, extension, saveContext, this.Logger);
}
}

internal static void RaiseIExtensionStorageAccessWorkspaceOpened(WorkspaceModel workspace, IExtensionStorageAccess extension, ILogger logger)
internal static void RaiseIExtensionStorageAccessWorkspaceOpened(HomeWorkspaceModel workspace, IExtensionStorageAccess extension, ILogger logger)
{
workspace.TryGetMatchingWorkspaceData(extension.UniqueId, out Dictionary<string, string> data);
var extensionDataCopy = new Dictionary<string, string>(data);
Expand All @@ -1004,7 +1006,7 @@ internal static void RaiseIExtensionStorageAccessWorkspaceOpened(WorkspaceModel
}
}

internal static void RaiseIExtensionStorageAccessWorkspaceSaving(WorkspaceModel workspace, IExtensionStorageAccess extension, SaveContext saveContext, ILogger logger)
internal static void RaiseIExtensionStorageAccessWorkspaceSaving(HomeWorkspaceModel workspace, IExtensionStorageAccess extension, SaveContext saveContext, ILogger logger)
{
var assemblyName = Assembly.GetAssembly(extension.GetType()).GetName();
var version = $"{assemblyName.Version.Major}.{assemblyName.Version.Minor}";
Expand Down
10 changes: 8 additions & 2 deletions src/DynamoCoreWpf/Views/Core/DynamoView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,17 +227,23 @@ public DynamoView(DynamoViewModel dynamoViewModel)

private void OnWorkspaceOpened(WorkspaceModel workspace)
{
if (!(workspace is HomeWorkspaceModel hws))
return;

foreach (var extension in viewExtensionManager.StorageAccessViewExtensions)
{
DynamoModel.RaiseIExtensionStorageAccessWorkspaceOpened(workspace, extension, dynamoViewModel.Model.Logger);
DynamoModel.RaiseIExtensionStorageAccessWorkspaceOpened(hws, extension, dynamoViewModel.Model.Logger);
}
}

private void OnWorkspaceSaving(WorkspaceModel workspace, Graph.SaveContext saveContext)
{
if (!(workspace is HomeWorkspaceModel hws))
return;

foreach (var extension in viewExtensionManager.StorageAccessViewExtensions)
{
DynamoModel.RaiseIExtensionStorageAccessWorkspaceSaving(workspace, extension, saveContext, dynamoViewModel.Model.Logger);
DynamoModel.RaiseIExtensionStorageAccessWorkspaceSaving(hws, extension, saveContext, dynamoViewModel.Model.Logger);
}
}

Expand Down
22 changes: 14 additions & 8 deletions test/DynamoCoreTests/StorageExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
using System.Text;
using System.Threading.Tasks;
using Dynamo.Extensions;
using Dynamo.Graph.Workspaces;
using Dynamo.Models;
using Dynamo.Scheduler;
using Moq;
using NUnit.Framework;

namespace Dynamo.Tests
{
[TestFixture]
public class StorageExtensionTests
{
private const string MOCK_EXTENSION_GUID = "7de691ac-b1ea-4353-9b9b-1f57b2967895";
Expand Down Expand Up @@ -124,7 +126,8 @@ public void OnWorkspaceOpenIsCalledWhenWorkspaceIsOpened()
public void OnWorkspaceOpenDoesNotAddToWorkspaceExtensionData()
{
// Arrange
var extensionDataBeforeWorkspaceOpen = model.CurrentWorkspace.ExtensionData.Count;
var homeworkspace = model.CurrentWorkspace as HomeWorkspaceModel;
var extensionDataBeforeWorkspaceOpen = homeworkspace.ExtensionData.Count;

var dataDictionary = new Dictionary<string, string>
{
Expand All @@ -135,7 +138,7 @@ public void OnWorkspaceOpenDoesNotAddToWorkspaceExtensionData()

// Act
model.OnWorkspaceOpened(model.CurrentWorkspace);
var extensionDataAfterWorkspaceOpen = model.CurrentWorkspace.ExtensionData.Count;
var extensionDataAfterWorkspaceOpen = homeworkspace.ExtensionData.Count;

// Assert
Assert.AreEqual(extensionDataBeforeWorkspaceOpen, extensionDataAfterWorkspaceOpen);
Expand All @@ -145,6 +148,7 @@ public void OnWorkspaceOpenDoesNotAddToWorkspaceExtensionData()
public void OnWorkspaceOpenDoesNotModifyExistingExtensionDataDictionary()
{
// Arrange
var homeworkspace = model.CurrentWorkspace as HomeWorkspaceModel;
var initialDict = new Dictionary<string, string>
{
{"A","a"},
Expand All @@ -161,11 +165,11 @@ public void OnWorkspaceOpenDoesNotModifyExistingExtensionDataDictionary()

// Act
model.OnWorkspaceSaving(model.CurrentWorkspace, Graph.SaveContext.Save);
var extensionDataDictionaryBeforeOpen = model.CurrentWorkspace.ExtensionData
var extensionDataDictionaryBeforeOpen = homeworkspace.ExtensionData
.Where(x=>x.ExtensionGuid == MOCK_EXTENSION_GUID).FirstOrDefault();

model.OnWorkspaceOpened(model.CurrentWorkspace);
var extensionDataDictionaryAfterOpen = model.CurrentWorkspace.ExtensionData
var extensionDataDictionaryAfterOpen = homeworkspace.ExtensionData
.Where(x => x.ExtensionGuid == MOCK_EXTENSION_GUID).FirstOrDefault();

// Assert
Expand All @@ -178,7 +182,8 @@ public void OnWorkspaceOpenDoesNotModifyExistingExtensionDataDictionary()
public void OnWorkspaceSavingAddsToStoredExtensionData()
{
// Arrange
var initialDataDictionary = model.CurrentWorkspace.ExtensionData
var homeworkspace = model.CurrentWorkspace as HomeWorkspaceModel;
var initialDataDictionary = homeworkspace.ExtensionData
.Where(x => x.ExtensionGuid == MOCK_EXTENSION_GUID)
.FirstOrDefault();

Expand All @@ -193,7 +198,7 @@ public void OnWorkspaceSavingAddsToStoredExtensionData()

// Act
model.OnWorkspaceSaving(model.CurrentWorkspace, Graph.SaveContext.Save);
var extensionDataDictionaryAfterSave = model.CurrentWorkspace.ExtensionData
var extensionDataDictionaryAfterSave = homeworkspace.ExtensionData
.Where(x => x.ExtensionGuid == MOCK_EXTENSION_GUID).FirstOrDefault();

// Assert
Expand All @@ -207,13 +212,14 @@ public void OnWorkspaceSavingAddsToStoredExtensionData()
public void OnWorkspaceSavingModifiesExistingExtensionDataDictionary()
{
// Arrange
var homeworkspace = model.CurrentWorkspace as HomeWorkspaceModel;
var initialDataDictionary = new Dictionary<string, string>
{
{"A","a" },
{"B","b" }
};
var extensionData = new ExtensionData(MOCK_EXTENSION_GUID, MOCK_EXTENSION_NAME, "0.0", initialDataDictionary);
model.CurrentWorkspace.ExtensionData.Add(extensionData);
homeworkspace.ExtensionData.Add(extensionData);

var dictionaryToAddOnSaving = new Dictionary<string, string>
{
Expand All @@ -230,7 +236,7 @@ public void OnWorkspaceSavingModifiesExistingExtensionDataDictionary()

// Act
model.OnWorkspaceSaving(model.CurrentWorkspace, Graph.SaveContext.Save);
var extensionDataAfterSave = model.CurrentWorkspace.ExtensionData
var extensionDataAfterSave = homeworkspace.ExtensionData
.Where(x => x.ExtensionGuid == MOCK_EXTENSION_GUID).FirstOrDefault();

// Assert
Expand Down

0 comments on commit 959130d

Please sign in to comment.