From c636a477990db2ccdd7f2fc921f2690aaa8fc0d2 Mon Sep 17 00:00:00 2001 From: Mark Cassidy Date: Sun, 23 May 2021 18:27:27 +0200 Subject: [PATCH] Adapted the deserialization pattern from 10.1 that seems to work for BranchID. Turns out it also works for 9.x, so removed the version gate --- .../Deserialization/DefaultDeserializer.cs | 45 +++++++++++++++---- .../IDefaultDeserializerLogger.cs | 2 + src/Rainbow.Storage.Sc/ItemData.cs | 12 +---- .../OutputModel/YamlItem.cs | 20 ++------- src/SharedAssemblyInfo.cs | 2 +- 5 files changed, 44 insertions(+), 37 deletions(-) diff --git a/src/Rainbow.Storage.Sc/Deserialization/DefaultDeserializer.cs b/src/Rainbow.Storage.Sc/Deserialization/DefaultDeserializer.cs index b476d62..6cde1fb 100644 --- a/src/Rainbow.Storage.Sc/Deserialization/DefaultDeserializer.cs +++ b/src/Rainbow.Storage.Sc/Deserialization/DefaultDeserializer.cs @@ -1,8 +1,10 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Globalization; using System.IO; using System.Linq; +using System.Reflection; using System.Runtime.Remoting.Messaging; using Rainbow.Filtering; using Rainbow.Model; @@ -10,6 +12,7 @@ using Sitecore.Caching; using Sitecore.Configuration; using Sitecore.Data; +using Sitecore.Data.Events; using Sitecore.Data.Fields; using Sitecore.Data.Items; using Sitecore.Data.Managers; @@ -72,17 +75,37 @@ public IItemData Deserialize(IItemData serializedItemData, IFieldValueManipulato var templateChangeHappened = ChangeTemplateIfNeeded(serializedItemData, targetItem); - bool brancheChangeHappened = false; - if (SitecoreVersionResolver.IsVersionHigherOrEqual(Sc.SitecoreVersionResolver.SitecoreVersion101)) - { - brancheChangeHappened = ChangeBranchIfNeeded(serializedItemData, targetItem, newItemWasCreated); - } + bool brancheChangeHappened = ChangeBranchIfNeeded(serializedItemData, targetItem, newItemWasCreated); var renameHappened = RenameIfNeeded(serializedItemData, targetItem); - ResetTemplateEngineIfItemIsTemplate(targetItem); + targetItem.Database.Engines.TemplateEngine.Reset(); + //ResetTemplateEngineIfItemIsTemplate(targetItem); var sharedFieldsWereChanged = PasteSharedFields(serializedItemData, targetItem, newItemWasCreated, softErrors, fieldValueManipulator); + if (EventDisabler.IsActive) + { + var bindingFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic; + var miHandleItemSaved = targetItem.Database.Engines.TemplateEngine.GetType().GetMethod("HandleItemSaved", bindingFlags); + var miGetFullChanges = targetItem.GetType().GetMethod("GetFullChanges", bindingFlags); + if (miHandleItemSaved != null && miGetFullChanges != null) + { + // Courtesy of Doctor DotPeek + // Reference code in: Sitecore.Data.Serialization.Default.DefaultItemSynchronizer + + var itemChanges = miGetFullChanges.Invoke(targetItem, bindingFlags, null, null, CultureInfo.CurrentCulture); + // targetItem.Database.Engines.TemplateEngine.HandleItemSaved(targetItem, targetItem.GetFullChanges(), false); + miHandleItemSaved.Invoke(targetItem.Database.Engines.TemplateEngine, bindingFlags, null, new[] { targetItem, itemChanges, false }, CultureInfo.CurrentCulture); + } + else + { + _logger.LogSystemMessage($"[E] INTERNAL ERROR. Reflection Target not located. If you get this message, please tell me about it. Sitecore Version: {SitecoreVersionResolver.SitecoreVersionCurrent}. miHandleItemSaved: {miHandleItemSaved != null}, miGetFullChanges: {miGetFullChanges != null}"); + } + } + + ClearCaches(targetItem.Database, targetItem.ID); + targetItem.Reload(); + targetItem.Database.Engines.TemplateEngine.Reset(); var unversionedFieldsWereChanged = PasteUnversionedFields(serializedItemData, targetItem, newItemWasCreated, softErrors, fieldValueManipulator); @@ -210,9 +233,8 @@ protected bool ChangeBranchIfNeeded(IItemData serializedItemData, Item targetIte targetItem.Editing.BeginEdit(); targetItem.RuntimeSettings.ReadOnlyStatistics = true; targetItem.BranchId = ID.Parse(serializedItemData.BranchId); - targetItem.Editing.EndEdit(); // try silent + targetItem.Editing.EndEdit(); - // try only if (!newItemCreated) ClearCaches(targetItem.Database, targetItem.ID); targetItem.Reload(); @@ -472,6 +494,13 @@ protected virtual bool PasteVersions(IItemData serializedItemData, Item targetIt foreach (var syncVersion in serializedItemData.Versions) { var res = PasteVersion(targetItem, syncVersion, newItemWasCreated, softErrors, fieldValueManipulator); + if (targetItem.BranchId.Guid != Guid.Empty) + { + ClearCaches(targetItem.Database, targetItem.ID); + targetItem.Reload(); + PasteSharedFields(serializedItemData, targetItem, newItemWasCreated, softErrors, fieldValueManipulator); + } + var committed = res.Item1; if (committed) anythingChanged = true; diff --git a/src/Rainbow.Storage.Sc/Deserialization/IDefaultDeserializerLogger.cs b/src/Rainbow.Storage.Sc/Deserialization/IDefaultDeserializerLogger.cs index 3ca499d..d247f2e 100644 --- a/src/Rainbow.Storage.Sc/Deserialization/IDefaultDeserializerLogger.cs +++ b/src/Rainbow.Storage.Sc/Deserialization/IDefaultDeserializerLogger.cs @@ -27,5 +27,7 @@ public interface IDefaultDeserializerLogger void ResetFieldThatDidNotExistInSerialized(Field field); void SkippedPastingIgnoredField(Item item, IItemFieldValue field); + + void LogSystemMessage(string message); } } diff --git a/src/Rainbow.Storage.Sc/ItemData.cs b/src/Rainbow.Storage.Sc/ItemData.cs index 5d41404..2d4db35 100644 --- a/src/Rainbow.Storage.Sc/ItemData.cs +++ b/src/Rainbow.Storage.Sc/ItemData.cs @@ -59,17 +59,7 @@ public virtual string Path public virtual string Name => _item.Name; - public virtual Guid BranchId - { - get - { - if (SitecoreVersionResolver.IsVersionHigherOrEqual(SitecoreVersionResolver.SitecoreVersion101)) - { - return _item.BranchId.Guid; - } - return Guid.Empty; - } - } + public virtual Guid BranchId => _item.BranchId.Guid; public virtual Guid TemplateId => _item.TemplateID.Guid; diff --git a/src/Rainbow.Storage.Yaml/OutputModel/YamlItem.cs b/src/Rainbow.Storage.Yaml/OutputModel/YamlItem.cs index 3476945..00b50e3 100644 --- a/src/Rainbow.Storage.Yaml/OutputModel/YamlItem.cs +++ b/src/Rainbow.Storage.Yaml/OutputModel/YamlItem.cs @@ -31,12 +31,7 @@ public virtual void LoadFrom(IItemData itemData, IFieldFormatter[] fieldFormatte ParentId = itemData.ParentId; TemplateId = itemData.TemplateId; Path = itemData.Path; - - // BranchID handling broken prior to 10.1 - if (Sc.SitecoreVersionResolver.IsVersionHigherOrEqual(Sc.SitecoreVersionResolver.SitecoreVersion101)) - { - BranchId = itemData.BranchId; - } + BranchId = itemData.BranchId; foreach (var field in itemData.SharedFields) { @@ -85,11 +80,7 @@ public virtual void WriteYaml(YamlWriter writer) writer.WriteMap("Path", Path); writer.WriteMap("DB", DatabaseName); - // BranchID handling broken prior to 10.1 - if(Sc.SitecoreVersionResolver.IsVersionHigherOrEqual(Sc.SitecoreVersionResolver.SitecoreVersion101)) - { - if (BranchId != default(Guid)) writer.WriteMap("BranchID", BranchId.ToString()); - } + if (BranchId != default(Guid)) writer.WriteMap("BranchID", BranchId.ToString()); if (SharedFields.Any()) { @@ -136,12 +127,7 @@ public virtual void ReadYaml(YamlReader reader) if (branch.HasValue && branch.Value.Key.Equals("BranchID")) { reader.ReadMap(); - - // BranchID handling broken prior to 10.1 - if (Sc.SitecoreVersionResolver.IsVersionHigherOrEqual(Sc.SitecoreVersionResolver.SitecoreVersion101)) - { - BranchId = Guid.Parse(branch.Value.Value); - } + BranchId = Guid.Parse(branch.Value.Value); } var sharedFields = reader.PeekMap(); diff --git a/src/SharedAssemblyInfo.cs b/src/SharedAssemblyInfo.cs index 6c0a24a..ceb2c89 100644 --- a/src/SharedAssemblyInfo.cs +++ b/src/SharedAssemblyInfo.cs @@ -6,5 +6,5 @@ [assembly: ComVisible(false)] [assembly: AssemblyVersion("2.1.4.0")] [assembly: AssemblyFileVersion("2.1.4.0")] -[assembly: AssemblyInformationalVersion("2.1.4-pre2")] +[assembly: AssemblyInformationalVersion("2.1.4-pre3")] [assembly: CLSCompliant(false)]