From a6028aa94796f44bcf590dfd12cae93ef3b952fc Mon Sep 17 00:00:00 2001 From: Kevin Jump Date: Tue, 24 Sep 2019 19:48:47 +0100 Subject: [PATCH] Improve trash handling. --- .../Serializers/ContentSerializer.cs | 33 ++++++++++------ .../Serializers/ContentSerializerBase.cs | 17 ++++++-- .../Serializers/MediaSerializer.cs | 39 +++++++++---------- 3 files changed, 54 insertions(+), 35 deletions(-) diff --git a/uSync8.ContentEdition/Serializers/ContentSerializer.cs b/uSync8.ContentEdition/Serializers/ContentSerializer.cs index 2734e044..54e6df43 100644 --- a/uSync8.ContentEdition/Serializers/ContentSerializer.cs +++ b/uSync8.ContentEdition/Serializers/ContentSerializer.cs @@ -115,16 +115,9 @@ protected override SyncAttempt DeserializeCore(XElement node) var item = FindOrCreate(node); - if (item.Trashed) - { - // TODO: Where has changed trashed state gone? - } - DeserializeBase(item, node); DeserializeTemplate(item, node); - // contentService.Save(item); - return SyncAttempt.Succeed( item.Name, item, @@ -166,10 +159,11 @@ public override SyncAttempt DeserializeSecondPass(IContent item, XElem // sort order var sortOrder = node.Element("Info").Element("SortOrder").ValueOrDefault(-1); - if (sortOrder != -1) - { - item.SortOrder = sortOrder; - } + HandleSortOrder(item, sortOrder); + + + var trashed = node.Element("Info").Element("Trashed").ValueOrDefault(false); + HandleTrashedState(item, trashed); // published status // this does the last save and publish @@ -184,6 +178,21 @@ public override SyncAttempt DeserializeSecondPass(IContent item, XElem // second pass, is when we do the publish and stuff. } + protected override void HandleTrashedState(IContent item, bool trashed) + { + if (!trashed && item.Trashed) + { + // if the item is trashed, then the change of it's parent + // should restore it (as long as we do a move!) + contentService.Move(item, item.ParentId); + } + else if (trashed && !item.Trashed) + { + // move to the recycle bin + contentService.MoveToRecycleBin(item); + } + } + protected virtual Attempt DoSaveOrPublish(IContent item, XElement node) { var info = node.Element("Info"); @@ -227,6 +236,8 @@ protected virtual Attempt DoSaveOrPublish(IContent item, XElement node) } + + #endregion protected override IContent CreateItem(string alias, ITreeEntity parent, string itemType) diff --git a/uSync8.ContentEdition/Serializers/ContentSerializerBase.cs b/uSync8.ContentEdition/Serializers/ContentSerializerBase.cs index a3c0f0a4..8bd93f01 100644 --- a/uSync8.ContentEdition/Serializers/ContentSerializerBase.cs +++ b/uSync8.ContentEdition/Serializers/ContentSerializerBase.cs @@ -25,8 +25,8 @@ public abstract class ContentSerializerBase : SyncTreeSerializerBase DeserializeProperties(TObject item, XElement node) return Attempt.Succeed(item); } + protected void HandleSortOrder(TObject item, int sortOrder) + { + if (sortOrder != -1) + item.SortOrder = sortOrder; + } + + protected abstract void HandleTrashedState(TObject item, bool trashed); + + protected string GetExportValue(object value, PropertyType propertyType, string culture, string segment) { // this is where the mapping magic will happen. @@ -256,7 +265,7 @@ protected override TObject FindOrCreate(XElement node) } var contentTypeAlias = node.Element("Info").Element("ContentType").ValueOrDefault(node.Name.LocalName); - + // var contentTypeAlias = node.Name.LocalName; return CreateItem(alias, parent, contentTypeAlias); @@ -395,6 +404,8 @@ protected TObject FindByPath(IEnumerable folders) } + + #endregion } } diff --git a/uSync8.ContentEdition/Serializers/MediaSerializer.cs b/uSync8.ContentEdition/Serializers/MediaSerializer.cs index 3b8a5eaf..04294f29 100644 --- a/uSync8.ContentEdition/Serializers/MediaSerializer.cs +++ b/uSync8.ContentEdition/Serializers/MediaSerializer.cs @@ -66,28 +66,10 @@ public override SyncAttempt DeserializeSecondPass(IMedia item, XElement var info = node.Element("Info"); var sortOrder = info.Element("SortOrder").ValueOrDefault(-1); - if (sortOrder != -1) - { - item.SortOrder = sortOrder; - } - + HandleSortOrder(item, sortOrder); var trashed = info.Element("Trashed").ValueOrDefault(false); - if (trashed) - { - if (!item.Trashed) - { - mediaService.MoveToRecycleBin(item); - } - return SyncAttempt.Succeed(item.Name, ChangeType.Import); - } - - if (item.Trashed) - { - // remove from bin. - // ? - } - + HandleTrashedState(item, trashed); var attempt = mediaService.Save(item); if (!attempt.Success) @@ -95,10 +77,24 @@ public override SyncAttempt DeserializeSecondPass(IMedia item, XElement // we return no-change so we don't trigger the second save return SyncAttempt.Succeed(item.Name, ChangeType.NoChange ); + } - + protected override void HandleTrashedState(IMedia item, bool trashed) + { + if (!trashed && item.Trashed) + { + // if the item is trashed, then moving it back to the parent value + // restores it. + mediaService.Move(item, item.ParentId); + } + else if (trashed && !item.Trashed) + { + // move to the recycle bin + mediaService.MoveToRecycleBin(item); + } } + protected override SyncAttempt SerializeCore(IMedia item) { var node = InitializeNode(item, item.ContentType.Alias); @@ -203,6 +199,7 @@ protected override void SaveItem(IMedia item) protected override void DeleteItem(IMedia item) => mediaService.Delete(item); + } }