Skip to content

Commit 2393e9c

Browse files
committed
Hack: Delete unmatched topics
While the `Import()` extension method will successfully remove unmatched topics from the topic graph—assuming e.g. `DeleteUnmatchedChildren` is enabled—it doesn't have access to the `ITopicRepository` in order to delete them. The `ITopicRepository` doesn't have a way to detect internal deletions either, however. In the future, we should evaluate ways of baking this into `ITopicRepository` e.g. by temporarily moving deleted topics to a `Topic.DeletedChildren` collection. For now, however, we can hack around this by taking a snapshot of all topics in the graph before hand, finding the difference afterwards, and deleting those topics that are no longer present. Technically, this will result in some topics being deleted which are already deleted (e.g., because their parent was already deleted), but, for better or for worse, the `ITopicRepository` handles that situation gracefully (i.e., without throwing an exception). This is an ugly hack, and especially because it's not intuitive behavior when using the Data Transfer library. It doesn't require too much code, however, and works reliably, so it's an acceptable workaround until a more sophisticated solution can be accounted for in the `ITopicRepository`.
1 parent 94b9842 commit 2393e9c

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

OnTopic.Editor.AspNetCore/Controllers/EditorController.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
| Project Topics Library
55
\=============================================================================================================================*/
66
using System;
7+
using System.Collections.Generic;
78
using System.IO;
89
using System.Linq;
910
using System.Text;
@@ -23,6 +24,7 @@
2324
using OnTopic.Internal.Diagnostics;
2425
using OnTopic.Mapping;
2526
using OnTopic.Metadata;
27+
using OnTopic.Querying;
2628
using OnTopic.Repositories;
2729

2830
namespace OnTopic.Editor.AspNetCore.Controllers {
@@ -694,6 +696,11 @@ public async Task<IActionResult> Import(IFormFile jsonFile, [Bind(Prefix = "Impo
694696
return View(editorViewModel);
695697
}
696698

699+
/*------------------------------------------------------------------------------------------------------------------------
700+
| INDEX TOPICS IN SCOPE
701+
\-----------------------------------------------------------------------------------------------------------------------*/
702+
var topics = target.FindAll(t => t.Id >= 0).ToList();
703+
697704
/*------------------------------------------------------------------------------------------------------------------------
698705
| IMPORT INTO TOPIC GRAPH
699706
>-------------------------------------------------------------------------------------------------------------------------
@@ -704,6 +711,20 @@ public async Task<IActionResult> Import(IFormFile jsonFile, [Bind(Prefix = "Impo
704711
target.Import(topicData, options);
705712
target.Import(topicData, options);
706713

714+
/*------------------------------------------------------------------------------------------------------------------------
715+
| DELETE UNMATCHED TOPICS
716+
>-------------------------------------------------------------------------------------------------------------------------
717+
| ### HACK JJC20200327: The Data Transfer library doesnt have access to the ITopicRepository, so it can't delete topics.
718+
| Instead, it removes them from the topic graph. But the ITopicRepository implementations don't have a means of detecting
719+
| removed topics during a recursive save and, therefore, the deletions aren't persited to the database. To mitigate this,
720+
| we evaluate the topic graph after the save, and then delete any orphans.
721+
\-----------------------------------------------------------------------------------------------------------------------*/
722+
var unmatchedTopics = topics.Except(target.FindAll(t => t.Id >= 0));
723+
724+
foreach (var unmatchedTopic in unmatchedTopics) {
725+
TopicRepository.Delete(unmatchedTopic);
726+
}
727+
707728
/*------------------------------------------------------------------------------------------------------------------------
708729
| SAVE
709730
>-------------------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)