Skip to content

Commit

Permalink
Language / Dictionary Comparison improvement #73
Browse files Browse the repository at this point in the history
using our own export - so we can control the order things get written to
the xml.
  • Loading branch information
KevinJump committed Aug 12, 2016
1 parent 42e0ecf commit f35be20
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 28 deletions.
14 changes: 14 additions & 0 deletions Jumoo.uSync.Core/Extensions/XElementTrackerExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
70 changes: 43 additions & 27 deletions Jumoo.uSync.Core/Helpers/uSyncChangeTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ public class uSyncChangeTracker
private static Dictionary<string, ChangeKeyPair> nodeKeys = new Dictionary<string, ChangeKeyPair>()
{
{ "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<string, ChangeKeyPair> nodeNames = new Dictionary<string, ChangeKeyPair>()
{
{ "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.
Expand All @@ -41,6 +43,14 @@ public class uSyncChangeTracker
{ "Template" }, {"Tab"}
};

/// <summary>
/// attributes we ignore (becuase they are internal ids we don't care about)
/// </summary>
private static List<string> ignoreAttribs = new List<string>()
{
{ "LanguageId" }, { "Id" }
};

/// <summary>
/// gets the changes between to xml files, will recurse down a tree and
/// note any changes in attribute, value or elements.
Expand Down Expand Up @@ -70,32 +80,35 @@ public static List<uSyncChange> 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
});
}
}
}
}
}
Expand All @@ -105,15 +118,18 @@ public static List<uSyncChange> 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"
});
}
}
}
}
Expand Down
15 changes: 14 additions & 1 deletion Jumoo.uSync.Core/Serializers/DictionarySerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,22 @@ private IDictionaryItem UpdateDictionaryValues(XElement node, Guid? parent, List

internal override SyncAttempt<XElement> 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<XElement>.SucceedIf(
node != null,
node != null,
node != null ? item.ItemKey : node.NameFromNode(),
node,
typeof(IDictionaryItem),
Expand Down

0 comments on commit f35be20

Please sign in to comment.