From f35be2079e2fabef0808cbf889aed2187ecec323 Mon Sep 17 00:00:00 2001 From: Kevin Jump Date: Fri, 12 Aug 2016 14:17:31 +0100 Subject: [PATCH] Language / Dictionary Comparison improvement #73 using our own export - so we can control the order things get written to the xml. --- .../Extensions/XElementTrackerExtension.cs | 14 ++++ .../Helpers/uSyncChangeTracker.cs | 70 ++++++++++++------- .../Serializers/DictionarySerializer.cs | 15 +++- 3 files changed, 71 insertions(+), 28 deletions(-) diff --git a/Jumoo.uSync.Core/Extensions/XElementTrackerExtension.cs b/Jumoo.uSync.Core/Extensions/XElementTrackerExtension.cs index 5667c04c..8fcc88cf 100644 --- a/Jumoo.uSync.Core/Extensions/XElementTrackerExtension.cs +++ b/Jumoo.uSync.Core/Extensions/XElementTrackerExtension.cs @@ -63,6 +63,20 @@ public static string GetSyncHash(this XElement node) } } + if (copy.Name.LocalName == "Language" && copy.Attribute("Id") != null) + { + copy.Attribute("Id").Remove(); + } + + if (copy.Name.LocalName == "DictionaryItem") + { + foreach(var val in copy.Elements("Value")) + { + if (val.Attribute("LanguageId") != null) + val.Attribute("LanguageId").Remove(); + } + } + return MakeHash(copy); } diff --git a/Jumoo.uSync.Core/Helpers/uSyncChangeTracker.cs b/Jumoo.uSync.Core/Helpers/uSyncChangeTracker.cs index c969487b..a85372e3 100644 --- a/Jumoo.uSync.Core/Helpers/uSyncChangeTracker.cs +++ b/Jumoo.uSync.Core/Helpers/uSyncChangeTracker.cs @@ -26,13 +26,15 @@ public class uSyncChangeTracker private static Dictionary nodeKeys = new Dictionary() { { "GenericProperty", new ChangeKeyPair("Key", ChangeValueType.Element) }, - { "PreValue", new ChangeKeyPair("Alias", ChangeValueType.Attribute) } + { "PreValue", new ChangeKeyPair("Alias", ChangeValueType.Attribute) }, + { "Value", new ChangeKeyPair("LanguageCultureAlias", ChangeValueType.Attribute) } }; private static Dictionary nodeNames = new Dictionary() { { "GenericProperty", new ChangeKeyPair("Name", ChangeValueType.Element) }, - { "PreValue", new ChangeKeyPair("Alias", ChangeValueType.Attribute) } + { "PreValue", new ChangeKeyPair("Alias", ChangeValueType.Attribute) }, + { "Value", new ChangeKeyPair("LanguageCultureAlias", ChangeValueType.Attribute) } }; // nodes where we match them on the internal values of the elements. @@ -41,6 +43,14 @@ public class uSyncChangeTracker { "Template" }, {"Tab"} }; + /// + /// attributes we ignore (becuase they are internal ids we don't care about) + /// + private static List ignoreAttribs = new List() + { + { "LanguageId" }, { "Id" } + }; + /// /// gets the changes between to xml files, will recurse down a tree and /// note any changes in attribute, value or elements. @@ -70,32 +80,35 @@ public static List GetChanges(XElement source, XElement target, str { foreach (var sourceAttrib in source.Attributes()) { - var targetAttrib = target.Attribute(sourceAttrib.Name); - if (targetAttrib == null) - { - changes.Add(new uSyncChange - { - Path = path, - Name = sourceAttrib.Name.LocalName, - Change = ChangeDetailType.Delete, - ValueType = ChangeValueType.Attribute, - OldVal = "attribute" - }); - } - else + if (!ignoreAttribs.Contains(sourceAttrib.Name.LocalName)) { - if (sourceAttrib.Value != targetAttrib.Value) + var targetAttrib = target.Attribute(sourceAttrib.Name); + if (targetAttrib == null) { changes.Add(new uSyncChange { Path = path, Name = sourceAttrib.Name.LocalName, - Change = ChangeDetailType.Update, - OldVal = sourceAttrib.Value, - NewVal = targetAttrib.Value, - ValueType = ChangeValueType.Attribute + Change = ChangeDetailType.Delete, + ValueType = ChangeValueType.Attribute, + OldVal = "attribute" }); } + else + { + if (sourceAttrib.Value != targetAttrib.Value) + { + changes.Add(new uSyncChange + { + Path = path, + Name = sourceAttrib.Name.LocalName, + Change = ChangeDetailType.Update, + OldVal = sourceAttrib.Value, + NewVal = targetAttrib.Value, + ValueType = ChangeValueType.Attribute + }); + } + } } } } @@ -105,15 +118,18 @@ public static List GetChanges(XElement source, XElement target, str { foreach (var targetAttrib in target.Attributes()) { - if (source.Attribute(targetAttrib.Name) == null) + if (!ignoreAttribs.Contains(targetAttrib.Name.LocalName)) { - changes.Add(new uSyncChange + if (source.Attribute(targetAttrib.Name) == null) { - Path = path, - Name = targetAttrib.Name.LocalName, - Change = ChangeDetailType.Create, - OldVal = "attribute" - }); + changes.Add(new uSyncChange + { + Path = path, + Name = targetAttrib.Name.LocalName, + Change = ChangeDetailType.Create, + OldVal = "attribute" + }); + } } } } diff --git a/Jumoo.uSync.Core/Serializers/DictionarySerializer.cs b/Jumoo.uSync.Core/Serializers/DictionarySerializer.cs index 5bd6e326..3f7a7849 100644 --- a/Jumoo.uSync.Core/Serializers/DictionarySerializer.cs +++ b/Jumoo.uSync.Core/Serializers/DictionarySerializer.cs @@ -102,9 +102,22 @@ private IDictionaryItem UpdateDictionaryValues(XElement node, Guid? parent, List internal override SyncAttempt SerializeCore(IDictionaryItem item) { + /* var node = _packagingService.Export(item, true); + */ + var node = new XElement(Constants.Packaging.DictionaryItemNodeName, + new XAttribute("Key", item.ItemKey)); + + foreach(var translation in item.Translations.OrderBy(x => x.Language.IsoCode)) + { + node.Add(new XElement("Value", + new XAttribute("LanguageId", translation.LanguageId), + new XAttribute("LanguageCultureAlias", translation.Language.IsoCode), + new XCData(translation.Value))); + } + return SyncAttempt.SucceedIf( - node != null, + node != null, node != null ? item.ItemKey : node.NameFromNode(), node, typeof(IDictionaryItem),