diff --git a/Jumoo.uSync.BackOffice/Properties/AssemblyInfo.cs b/Jumoo.uSync.BackOffice/Properties/AssemblyInfo.cs index 8f020729..60d3b718 100644 --- a/Jumoo.uSync.BackOffice/Properties/AssemblyInfo.cs +++ b/Jumoo.uSync.BackOffice/Properties/AssemblyInfo.cs @@ -32,4 +32,4 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("3.3.3.740")] -[assembly: AssemblyVersion("3.3.3.740")] +[assembly: AssemblyVersion("3.3.4.740")] diff --git a/Jumoo.uSync.Core/Helpers/uSyncValueMapper.cs b/Jumoo.uSync.Core/Helpers/uSyncValueMapper.cs index 47e35c9e..139d74da 100644 --- a/Jumoo.uSync.Core/Helpers/uSyncValueMapper.cs +++ b/Jumoo.uSync.Core/Helpers/uSyncValueMapper.cs @@ -236,9 +236,15 @@ private string TabToGeneric(string id) #region ToId (coming in) - public string MapToId(XElement valueNode) + public string MapToId(XElement valueNode, PreValue preVal) { - var value = valueNode.Attribute("Value").Value; + // existing values are appended with zzusync + // and as they are replace that is removed + // it stops us double mapping + + // at the end we remove the zzusync + Regex regx = new Regex(@"\d+"); + var value = regx.Replace(valueNode.Attribute("Value").Value, "$0:zzusync"); var mapGuid = valueNode.Attribute("MapGuid"); if (mapGuid == null) @@ -248,73 +254,61 @@ public string MapToId(XElement valueNode) .Where(x => x.Attribute("MapGuid").Value == mapGuid.Value) .ToList(); - foreach(var mapNode in mappedNodes) + foreach (var mapNode in mappedNodes) { var type = mapNode.Attribute("Type").Value; var val = mapNode.Attribute("Value").Value; - var id = mapNode.Attribute("Id").Value; - - var valueSubString = GetValueMatchSubstring(value); + var id = mapNode.Attribute("Id").Value + ":zzusync"; - var localId = GetMappedId(id, val, type); - // all the zz swapping here, to stop false positives... - Regex exsitingRegEx = new Regex(string.Format("{0}(?!:zzusync)", localId)); - if (exsitingRegEx.IsMatch(valueSubString)) + var localId = GetMappedId(val, type); // now returns empty string if it's can't map the id. + if (localId.IsNullOrWhiteSpace()) // if it is empty then we get the value from the existing pre-value (so no mapping) { - // what's happened here is the target value string already contains our - // target id - so we add some strings to our target, to stop us - // from confusing the id we're putting in with anything else. - Regex rgx = new Regex(@"\d{1}(?!:zzusync)"); - localId = "\"" + rgx.Replace(localId, "$0:zzusync") + "\""; - // at the end of our mapping process we clean out the extra bits. + LogHelper.Debug("Mapping LocalId back to value in umbraco"); + localId = GetValueMatchSubstring(preVal.Value); } - // replace the mapped id with the new local one, - // ** but only if it doesn't have :zzusync appended to it ** - Regex mapRegEx = new Regex(string.Format("{0}(?!:zzusync)", id)); - var targetSubString = mapRegEx.Replace(valueSubString, localId); - - value = value.Replace(valueSubString, targetSubString); + // replace any instances of id with the value in localId + value = value.Replace(id, localId); } - return CleanValue(value); + return value.Replace(":zzusync", ""); } - public string GetMappedId(string id, string value, string type) + public string GetMappedId(string value, string type) { switch(type) { case "content": - return ContentToId(id, value); + return ContentToId(value); case "media": - return MediaToId(id, value); + return MediaToId(value); case "tab": - return TabToId(id, value); + return TabToId(value); case "mediatype": - return MediaTypeToId(id, value); + return MediaTypeToId(value); case "doctype": - return ContentTypeToId(id, value); + return ContentTypeToId(value); } - return id; + return string.Empty; } - private string MediaToId(string id, string value) + private string MediaToId(string value) { var walker = new uSyncTreeWalker(UmbracoObjectTypes.Media); var mappedId = walker.GetIdFromPath(value); - return (mappedId != -1) ? mappedId.ToString() : id; + return (mappedId != -1) ? mappedId.ToString() : string.Empty; } - private string ContentToId(string id, string value) + private string ContentToId(string value) { var walker = new uSyncTreeWalker(UmbracoObjectTypes.Document); var mappedId = walker.GetIdFromPath(value); - return (mappedId != -1) ? mappedId.ToString() : id; + return (mappedId != -1) ? mappedId.ToString() : string.Empty; } - private string MediaTypeToId(string id, string value) + private string MediaTypeToId(string value) { var itemKey = Guid.Empty; @@ -324,10 +318,10 @@ private string MediaTypeToId(string id, string value) if (item != null) return item.Id.ToString(); } - return id; + return string.Empty; } - private string ContentTypeToId(string id, string value) + private string ContentTypeToId(string value) { var itemKey = Guid.Empty; if (Guid.TryParse(value, out itemKey)) @@ -336,10 +330,10 @@ private string ContentTypeToId(string id, string value) if (item != null) return item.Id.ToString(); } - return id; + return string.Empty; } - private string TabToId(string id, string value) + private string TabToId(string value) { if (value.Contains("|") && value.Split('|').Count() == 2) { @@ -357,32 +351,8 @@ private string TabToId(string id, string value) } } } - return id; - } - - - /// - /// at the end of the match process, we clean all the :zzusync's from our ids - /// - /// - /// - private string CleanValue(string value) - { - var looper = 0; - while (value.Contains(":zzusync") && looper < 5) - { - looper++; - Regex rgx = new Regex("\"?(\\d{1,4})(:zzusync\"?)"); - var cleaned = rgx.Replace(value, "$1"); - value = cleaned; - } - - if (value.Contains(":zzusync")) - value = value.Replace(":zzusync", ""); - - return value; + return string.Empty; } - #endregion } } diff --git a/Jumoo.uSync.Core/Serializers/DataTypeSerializer.cs b/Jumoo.uSync.Core/Serializers/DataTypeSerializer.cs index ba5d6973..e1551b1b 100644 --- a/Jumoo.uSync.Core/Serializers/DataTypeSerializer.cs +++ b/Jumoo.uSync.Core/Serializers/DataTypeSerializer.cs @@ -16,7 +16,6 @@ using Umbraco.Core.Logging; using System.Web; - namespace Jumoo.uSync.Core.Serializers { public class DataTypeSerializer : DataTypeSyncBaseSerializer, ISyncChangeDetail @@ -49,7 +48,7 @@ internal override SyncAttempt DeserializeCore(XElement node private SyncAttempt DeserializeItem(XElement node, IDataTypeDefinition item) { // pre import - var mappedNode = DeserializeGetMappedValues(node); + Guid key = node.Attribute("Key").ValueOrDefault(Guid.Empty); var name = node.Attribute("Name").ValueOrDefault(string.Empty); @@ -177,30 +176,38 @@ internal override SyncAttempt DesearlizeSecondPassCore(IDat return DeserializeItem(node, item); } - private XElement DeserializeGetMappedValues(XElement node) + private XElement DeserializeGetMappedValues(XElement node, IDictionary preValues ) { + XElement nodeCopy = new XElement(node); - var id = node.Attribute("Id").ValueOrDefault(string.Empty); + var id = nodeCopy.Attribute("Id").ValueOrDefault(string.Empty); + + LogHelper.Debug("Mapping Guids {0}", ()=> id); var mapper = LoadMapper(nodeCopy, id); - var preValues = nodeCopy.Element("PreValues"); + var preValuesElements = nodeCopy.Element("PreValues"); - if (mapper != null && preValues != null && preValues.HasElements) + if (mapper != null && preValuesElements != null && preValuesElements.HasElements) { - foreach (var preValue in preValues.Descendants() - .Where(x => x.Attribute("MapGuid") != null) + foreach (var preValueNode in preValuesElements.Descendants() + .Where(x => x.Attribute("MapGuid") != null && x.Attribute("Alias") != null) .ToList()) { + var alias = preValueNode.Attribute("Alias").Value; - var value = mapper.MapToId(preValue); - - if (!string.IsNullOrEmpty(value)) + if (preValues.ContainsKey(alias)) { - preValue.Attribute("Value").Value = value; - } + var value = mapper.MapToId(preValueNode, preValues[alias] ); - preValue.Attribute("MapGuid").Remove(); + if (!string.IsNullOrEmpty(value)) + { + LogHelper.Debug("Setting Mapped Value: {0}", () => value); + preValueNode.Attribute("Value").Value = value; + } + + preValueNode.Attribute("MapGuid").Remove(); + } } } @@ -212,14 +219,14 @@ private XElement DeserializeGetMappedValues(XElement node) private void DeserializeUpdatePreValues(IDataTypeDefinition item, XElement node) { - LogHelper.Debug("Deserializing DataType PreValues: {0}", ()=> item.Name); + var itemPreValues = _dataTypeService.GetPreValuesCollectionByDataTypeId(item.Id) + .FormatAsDictionary(); + + var mappedNode = DeserializeGetMappedValues(node, itemPreValues); - var preValueRootNode = node.Element("PreValues"); + var preValueRootNode = mappedNode.Element("PreValues"); if (preValueRootNode != null) { - var itemPreValues = _dataTypeService.GetPreValuesCollectionByDataTypeId(item.Id) - .FormatAsDictionary(); - List preValsToRemove = new List(); foreach (var preValue in itemPreValues) @@ -232,7 +239,10 @@ private void DeserializeUpdatePreValues(IDataTypeDefinition item, XElement node) if (preValNode != null) { // set the value of preValue value to the value of the value attribute :) - preValue.Value.Value = preValNode.Attribute("Value").Value; + if (preValue.Value.Value != preValNode.Attribute("Value").Value) + { + preValue.Value.Value = preValNode.Attribute("Value").Value; + } } else { @@ -264,25 +274,12 @@ private void DeserializeUpdatePreValues(IDataTypeDefinition item, XElement node) { if (!itemPreValues.ContainsKey(alias)) { - LogHelper.Debug("Adding PreValue {0} for {1}", () => alias, () => item.Name); itemPreValues.Add(alias, new PreValue(value)); } } } _dataTypeService.SavePreValues(item, itemPreValues); - - /* - var valuesSansKeys = preValueRootNode.Elements("PreValue") - .Where(x => ((string)x.Attribute("Alias")).IsNullOrWhiteSpace() == false) - .Select(x => x.Attribute("Value").Value); - - /// this is marked as obsolete? but don't some prevalues still have no keys? - if (valuesSansKeys.Any()) - { - _dataTypeService.SavePreValues(item.Id, valuesSansKeys); - } - */ } } @@ -357,7 +354,6 @@ private XElement SerializePreValues(IDataTypeDefinition item, XElement node) nodePreValues.Add(preValueNode); } - return nodePreValues; }