Skip to content

Commit

Permalink
Adapted the deserialization pattern from 10.1 that seems to work for …
Browse files Browse the repository at this point in the history
…BranchID. Turns out it also works for 9.x, so removed the version gate
  • Loading branch information
cassidydotdk committed May 23, 2021
1 parent a9f0e38 commit c636a47
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 37 deletions.
45 changes: 37 additions & 8 deletions src/Rainbow.Storage.Sc/Deserialization/DefaultDeserializer.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
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;
using Sitecore;
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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ public interface IDefaultDeserializerLogger
void ResetFieldThatDidNotExistInSerialized(Field field);

void SkippedPastingIgnoredField(Item item, IItemFieldValue field);

void LogSystemMessage(string message);
}
}
12 changes: 1 addition & 11 deletions src/Rainbow.Storage.Sc/ItemData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
20 changes: 3 additions & 17 deletions src/Rainbow.Storage.Yaml/OutputModel/YamlItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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())
{
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/SharedAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]

0 comments on commit c636a47

Please sign in to comment.