Skip to content

Commit

Permalink
Merge pull request #35 from SitecoreUnicorn/develop
Browse files Browse the repository at this point in the history
Merge for release 2.1.1
  • Loading branch information
cassidydotdk authored Jul 21, 2019
2 parents 49e26a2 + 40d5dd7 commit f6b1686
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 33 deletions.
112 changes: 86 additions & 26 deletions src/Rainbow.Storage.Sc/Deserialization/DefaultDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using Rainbow.Filtering;
using Rainbow.Model;
using Sitecore;
Expand Down Expand Up @@ -68,27 +69,35 @@ public IItemData Deserialize(IItemData serializedItemData, IFieldValueManipulato

try
{
ChangeTemplateIfNeeded(serializedItemData, targetItem);
bool changeHappened = false;

var templateChangeHappened = ChangeTemplateIfNeeded(serializedItemData, targetItem);

bool brancheChangeHappened = false;
if (!IgnoreBranchId)
{
ChangeBranchIfNeeded(serializedItemData, targetItem, newItemWasCreated);
brancheChangeHappened = ChangeBranchIfNeeded(serializedItemData, targetItem, newItemWasCreated);
}

RenameIfNeeded(serializedItemData, targetItem);
var renameHappened = RenameIfNeeded(serializedItemData, targetItem);

ResetTemplateEngineIfItemIsTemplate(targetItem);

PasteSharedFields(serializedItemData, targetItem, newItemWasCreated, softErrors, fieldValueManipulator);
var sharedFieldsWereChanged = PasteSharedFields(serializedItemData, targetItem, newItemWasCreated, softErrors, fieldValueManipulator);

PasteUnversionedFields(serializedItemData, targetItem, newItemWasCreated, softErrors, fieldValueManipulator);
var unversionedFieldsWereChanged = PasteUnversionedFields(serializedItemData, targetItem, newItemWasCreated, softErrors, fieldValueManipulator);

UpdateFieldSharingIfNeeded(serializedItemData, targetItem);
var fieldSharingWasUpdated = UpdateFieldSharingIfNeeded(serializedItemData, targetItem);

PasteVersions(serializedItemData, targetItem, newItemWasCreated, softErrors, fieldValueManipulator);
var versionChangeHappened = PasteVersions(serializedItemData, targetItem, newItemWasCreated, softErrors, fieldValueManipulator);

if (softErrors.Count > 0) throw TemplateMissingFieldException.Merge(softErrors);

changeHappened = templateChangeHappened | brancheChangeHappened | renameHappened | sharedFieldsWereChanged | unversionedFieldsWereChanged | fieldSharingWasUpdated | versionChangeHappened;

if (!changeHappened)
return null;

return new ItemData(targetItem, ParentDataStore);
}
catch (ParentForMovedItemNotFoundException)
Expand Down Expand Up @@ -193,9 +202,9 @@ protected virtual Item GetOrCreateTargetItem(IItemData serializedItemData, out b
return targetItem;
}

protected void ChangeBranchIfNeeded(IItemData serializedItemData, Item targetItem, bool newItemWasCreated)
protected bool ChangeBranchIfNeeded(IItemData serializedItemData, Item targetItem, bool newItemWasCreated)
{
if (targetItem.BranchId.Guid.Equals(serializedItemData.BranchId)) return;
if (targetItem.BranchId.Guid.Equals(serializedItemData.BranchId)) return false;

Guid oldBranchId = targetItem.BranchId.Guid;

Expand All @@ -210,12 +219,15 @@ protected void ChangeBranchIfNeeded(IItemData serializedItemData, Item targetIte
if (oldBranchId != serializedItemData.BranchId && !newItemWasCreated)
{
_logger.ChangedBranchTemplate(targetItem, new ID(oldBranchId).ToString());
return true;
}

return false;
}

protected void RenameIfNeeded(IItemData serializedItemData, Item targetItem)
protected bool RenameIfNeeded(IItemData serializedItemData, Item targetItem)
{
if (targetItem.Name.Equals(serializedItemData.Name, StringComparison.Ordinal)) return;
if (targetItem.Name.Equals(serializedItemData.Name, StringComparison.Ordinal)) return false;

string oldName = targetItem.Name;

Expand All @@ -228,12 +240,17 @@ protected void RenameIfNeeded(IItemData serializedItemData, Item targetItem)
targetItem.Reload();

if (oldName != serializedItemData.Name)
{
_logger.RenamedItem(targetItem, oldName);
return true;
}

return false;
}

protected void ChangeTemplateIfNeeded(IItemData serializedItemData, Item targetItem)
protected bool ChangeTemplateIfNeeded(IItemData serializedItemData, Item targetItem)
{
if (targetItem.TemplateID.Guid == serializedItemData.TemplateId) return;
if (targetItem.TemplateID.Guid == serializedItemData.TemplateId) return false;

var oldTemplate = targetItem.Template;
var newTemplate = targetItem.Database.Templates[new ID(serializedItemData.TemplateId)];
Expand Down Expand Up @@ -273,13 +290,15 @@ protected void ChangeTemplateIfNeeded(IItemData serializedItemData, Item targetI
targetItem.Reload();

_logger.ChangedTemplate(targetItem, oldTemplate);

return true;
}

/// <summary>
/// When you change a template field from versioned to shared or unversioned (or vice versa), the conversion of the values
/// in existing items with that field is NOT automatic. This method causes the requisite field updates (moving the values between db tables)
/// </summary>
protected void UpdateFieldSharingIfNeeded(IItemData serializedItemData, Item targetItem)
protected bool UpdateFieldSharingIfNeeded(IItemData serializedItemData, Item targetItem)
{
// This is what Sitecore's internal deserializer does instead. Will investigate if this is a better option. Also depends on which Sitecore version introduced this.
//if (EventDisabler.IsActive)
Expand All @@ -289,7 +308,7 @@ protected void UpdateFieldSharingIfNeeded(IItemData serializedItemData, Item tar
Assert.ArgumentNotNull(serializedItemData, nameof(serializedItemData));
Assert.ArgumentNotNull(targetItem, nameof(targetItem));

if (serializedItemData.TemplateId != TemplateIDs.TemplateField.Guid) return;
if (serializedItemData.TemplateId != TemplateIDs.TemplateField.Guid) return false;

var shared = serializedItemData.SharedFields.FirstOrDefault(field => field.FieldId == TemplateFieldIDs.Shared.Guid);
var unversioned = serializedItemData.SharedFields.FirstOrDefault(field => field.FieldId == TemplateFieldIDs.Unversioned.Guid);
Expand All @@ -316,9 +335,11 @@ protected void UpdateFieldSharingIfNeeded(IItemData serializedItemData, Item tar
if (templateField.IsShared) templateSharedness = TemplateFieldSharing.Shared;
else if (templateField.IsUnversioned) templateSharedness = TemplateFieldSharing.Unversioned;

if (templateSharedness == sharedness) return;
if (templateSharedness == sharedness) return false;

TemplateManager.ChangeFieldSharing(templateField, sharedness, targetItem.Database);

return true;
}

protected Item CreateTargetItem(IItemData serializedItemData, Item destinationParentItem)
Expand All @@ -345,7 +366,7 @@ protected Item CreateTargetItem(IItemData serializedItemData, Item destinationPa
return targetItem;
}

protected virtual void PasteSharedFields(IItemData serializedItemData, Item targetItem, bool newItemWasCreated, List<TemplateMissingFieldException> softErrors, IFieldValueManipulator fieldValueManipulator)
protected virtual bool PasteSharedFields(IItemData serializedItemData, Item targetItem, bool newItemWasCreated, List<TemplateMissingFieldException> softErrors, IFieldValueManipulator fieldValueManipulator)
{
bool commitEdit = false;

Expand Down Expand Up @@ -392,14 +413,18 @@ protected virtual void PasteSharedFields(IItemData serializedItemData, Item targ
}
}

// Whatever remains here are field transformers that are NOT represented in the serialized data
// Whatever remains here are field transformers that are NOT represented in the serialized data shared fields
foreach (var t in transformersList)
{
var fieldMeta = targetItem.Template.GetField(t.FieldName);

// If the field doesn't exist on the target template, it's time to just skip
if (fieldMeta != null)
{
// We're only dealing with Shared Fields in this method
if(!fieldMeta.IsShared)
continue;

var existingField = targetItem.Fields[fieldMeta.ID];
if (t.ShouldDeployFieldValue(existingField.Value, null))
{
Expand All @@ -420,16 +445,20 @@ protected virtual void PasteSharedFields(IItemData serializedItemData, Item targ
targetItem.Reload();

ResetTemplateEngineIfItemIsTemplate(targetItem);

return true;
}
}
finally
{
if (targetItem.Editing.IsEditing)
targetItem.Editing.CancelEdit();
}

return false;
}

protected virtual void PasteVersions(IItemData serializedItemData, Item targetItem, bool newItemWasCreated, List<TemplateMissingFieldException> softErrors, IFieldValueManipulator fieldValueManipulator)
protected virtual bool PasteVersions(IItemData serializedItemData, Item targetItem, bool newItemWasCreated, List<TemplateMissingFieldException> softErrors, IFieldValueManipulator fieldValueManipulator)
{
Hashtable versionTable = CommonUtils.CreateCIHashtable();

Expand All @@ -438,24 +467,35 @@ protected virtual void PasteVersions(IItemData serializedItemData, Item targetIt
foreach (Item version in targetItem.Versions.GetVersions(true))
versionTable[version.Uri] = null;

bool anythingChanged = false;

foreach (var syncVersion in serializedItemData.Versions)
{
var version = PasteVersion(targetItem, syncVersion, newItemWasCreated, softErrors, fieldValueManipulator);
var res = PasteVersion(targetItem, syncVersion, newItemWasCreated, softErrors, fieldValueManipulator);
var committed = res.Item1;
if (committed)
anythingChanged = true;

var version = res.Item2;
if (versionTable.ContainsKey(version.Uri))
versionTable.Remove(version.Uri);
}

foreach (ItemUri uri in versionTable.Keys)
{
anythingChanged = true;

var versionToRemove = Database.GetItem(uri);

_logger.RemovingOrphanedVersion(versionToRemove);

versionToRemove.Versions.RemoveVersion();
}

return anythingChanged;
}

protected virtual Item PasteVersion(Item item, IItemVersion serializedVersion, bool creatingNewItem, List<TemplateMissingFieldException> softErrors, IFieldValueManipulator fieldValueManipulator)
protected virtual Tuple<bool,Item> PasteVersion(Item item, IItemVersion serializedVersion, bool creatingNewItem, List<TemplateMissingFieldException> softErrors, IFieldValueManipulator fieldValueManipulator)
{
Language language = Language.Parse(serializedVersion.Language.Name);
var targetVersion = Version.Parse(serializedVersion.VersionNumber);
Expand Down Expand Up @@ -555,14 +595,18 @@ protected virtual Item PasteVersion(Item item, IItemVersion serializedVersion, b
}
}

// Whatever remains here are field transformers that are NOT represented in the serialized data
// Whatever remains here are field transformers that are NOT represented in the serialized data for Versioned fields
foreach (var t in transformersList)
{
var fieldMeta = languageVersionItem.Template.GetField(t.FieldName);

// If the field doesn't exist on the target template, it's time to just skip
if (fieldMeta != null)
{
// We're only dealing with Versioned fields here
if (fieldMeta.IsUnversioned || fieldMeta.IsShared)
continue;

var existingField = languageVersionItem.Fields[fieldMeta.ID];
if (t.ShouldDeployFieldValue(existingField.Value, null))
{
Expand Down Expand Up @@ -604,6 +648,8 @@ protected virtual Item PasteVersion(Item item, IItemVersion serializedVersion, b
}
finally
{
commitEdit = false;

if (languageVersionItem.Editing.IsEditing)
languageVersionItem.Editing.CancelEdit();
}
Expand All @@ -614,18 +660,24 @@ protected virtual Item PasteVersion(Item item, IItemVersion serializedVersion, b
ResetTemplateEngineIfItemIsTemplate(languageVersionItem);
}

return languageVersionItem;
return new Tuple<bool, Item>(commitEdit, languageVersionItem);
}

protected virtual void PasteUnversionedFields(IItemData serializedItemData, Item targetItem, bool newItemWasCreated, List<TemplateMissingFieldException> softErrors, IFieldValueManipulator fieldValueManipulator)
protected virtual bool PasteUnversionedFields(IItemData serializedItemData, Item targetItem, bool newItemWasCreated, List<TemplateMissingFieldException> softErrors, IFieldValueManipulator fieldValueManipulator)
{
bool changeHappened = false;

foreach (var language in serializedItemData.UnversionedFields)
{
PasteUnversionedLanguage(targetItem, language, newItemWasCreated, softErrors, fieldValueManipulator);
bool result = PasteUnversionedLanguage(targetItem, language, newItemWasCreated, softErrors, fieldValueManipulator);
if (result)
changeHappened = true;
}

return changeHappened;
}

protected virtual void PasteUnversionedLanguage(Item item, IItemLanguage serializedLanguage, bool newItemWasCreated, List<TemplateMissingFieldException> softErrors, IFieldValueManipulator fieldValueManipulator)
protected virtual bool PasteUnversionedLanguage(Item item, IItemLanguage serializedLanguage, bool newItemWasCreated, List<TemplateMissingFieldException> softErrors, IFieldValueManipulator fieldValueManipulator)
{
Language language = Language.Parse(serializedLanguage.Language.Name);

Expand Down Expand Up @@ -694,6 +746,9 @@ protected virtual void PasteUnversionedLanguage(Item item, IItemLanguage seriali
// If the field doesn't exist on the target template, it's time to just skip
if (fieldMeta != null)
{
if (!fieldMeta.IsUnversioned)
continue;

var existingField = targetItem.Fields[fieldMeta.ID];
if (t.ShouldDeployFieldValue(existingField.Value, null))
{
Expand All @@ -706,13 +761,18 @@ protected virtual void PasteUnversionedLanguage(Item item, IItemLanguage seriali

// we commit the edit context - and write to the DB - only if we changed something
if (commitEdit)
{
targetItem.Editing.EndEdit();
return true;
}
}
finally
{
if (targetItem.Editing.IsEditing)
targetItem.Editing.CancelEdit();
}

return false;
}

protected virtual bool PasteField(Item item, IItemFieldValue field, bool creatingNewItem, IFieldValueManipulator fieldValueManipulator)
Expand Down
5 changes: 3 additions & 2 deletions src/Rainbow.Storage.Sc/SitecoreDataStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ public virtual IEnumerable<string> GetDatabaseNames()
return Factory.GetDatabaseNames();
}

public virtual void Save(IItemData item, IFieldValueManipulator fieldValueManipulator)
public virtual bool Save(IItemData item, IFieldValueManipulator fieldValueManipulator)
{
Assert.ArgumentNotNull(item, "item");

_deserializer.Deserialize(item, fieldValueManipulator);
var result = _deserializer.Deserialize(item, fieldValueManipulator);
return result != null;
}

public virtual void MoveOrRenameItem(IItemData itemWithFinalPath, string oldPath)
Expand Down
2 changes: 1 addition & 1 deletion src/Rainbow/Storage/IDataStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface IDataStore
/// <summary>
/// Saves an item into the store
/// </summary>
void Save(IItemData item, IFieldValueManipulator fieldValueManipulator);
bool Save(IItemData item, IFieldValueManipulator fieldValueManipulator);

/// <remarks>
/// Note: for moved items, pass in the FINAL path to move to, not the path being moved from (if it's a move within the store, we'll know the old path by ID from current state)
Expand Down
4 changes: 3 additions & 1 deletion src/Rainbow/Storage/SerializationFileSystemDataStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public virtual IEnumerable<IItemData> GetSnapshot()
return Trees.SelectMany(tree => tree.GetSnapshot());
}

public virtual void Save(IItemData item, IFieldValueManipulator fieldValueManipulator)
public virtual bool Save(IItemData item, IFieldValueManipulator fieldValueManipulator)
{
// fieldValueManipulator deliberately ignored. We never transform field values going to the file system

Expand All @@ -54,6 +54,8 @@ public virtual void Save(IItemData item, IFieldValueManipulator fieldValueManipu
if (tree == null) throw new InvalidOperationException("No trees contained the global path " + item.Path);

tree.Save(item);

return true;
}

/*
Expand Down
6 changes: 3 additions & 3 deletions src/SharedAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

[assembly: AssemblyProduct("Rainbow")]
[assembly: ComVisible(false)]
[assembly: AssemblyVersion("2.1.0.0")]
[assembly: AssemblyFileVersion("2.1.0.0")]
[assembly: AssemblyInformationalVersion("2.1.0")]
[assembly: AssemblyVersion("2.1.1.0")]
[assembly: AssemblyFileVersion("2.1.1.0")]
[assembly: AssemblyInformationalVersion("2.1.1")]
[assembly: CLSCompliant(false)]

0 comments on commit f6b1686

Please sign in to comment.