Skip to content

Commit

Permalink
#132 Mapping of ids in picker - not overwriting what is already there
Browse files Browse the repository at this point in the history
Updated the mappers for pickers so they a: work b: don't overwrite if
they can't map.
  • Loading branch information
KevinJump committed May 20, 2017
1 parent 84fa796 commit d72ed4a
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 99 deletions.
2 changes: 1 addition & 1 deletion Jumoo.uSync.BackOffice/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
98 changes: 34 additions & 64 deletions Jumoo.uSync.Core/Helpers/uSyncValueMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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<uSyncValueMapper>("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;
Expand All @@ -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))
Expand All @@ -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)
{
Expand All @@ -357,32 +351,8 @@ private string TabToId(string id, string value)
}
}
}
return id;
}


/// <summary>
/// at the end of the match process, we clean all the :zzusync's from our ids
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
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
}
}
64 changes: 30 additions & 34 deletions Jumoo.uSync.Core/Serializers/DataTypeSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
using Umbraco.Core.Logging;
using System.Web;


namespace Jumoo.uSync.Core.Serializers
{
public class DataTypeSerializer : DataTypeSyncBaseSerializer, ISyncChangeDetail
Expand Down Expand Up @@ -49,7 +48,7 @@ internal override SyncAttempt<IDataTypeDefinition> DeserializeCore(XElement node
private SyncAttempt<IDataTypeDefinition> 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);
Expand Down Expand Up @@ -177,30 +176,38 @@ internal override SyncAttempt<IDataTypeDefinition> DesearlizeSecondPassCore(IDat
return DeserializeItem(node, item);
}

private XElement DeserializeGetMappedValues(XElement node)
private XElement DeserializeGetMappedValues(XElement node, IDictionary<string, PreValue> preValues )
{

XElement nodeCopy = new XElement(node);
var id = node.Attribute("Id").ValueOrDefault(string.Empty);
var id = nodeCopy.Attribute("Id").ValueOrDefault(string.Empty);

LogHelper.Debug<DataTypeSerializer>("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<DataTypeSerializer>("Setting Mapped Value: {0}", () => value);
preValueNode.Attribute("Value").Value = value;
}

preValueNode.Attribute("MapGuid").Remove();
}
}
}

Expand All @@ -212,14 +219,14 @@ private XElement DeserializeGetMappedValues(XElement node)

private void DeserializeUpdatePreValues(IDataTypeDefinition item, XElement node)
{
LogHelper.Debug<DataTypeSerializer>("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<string> preValsToRemove = new List<string>();

foreach (var preValue in itemPreValues)
Expand All @@ -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
{
Expand Down Expand Up @@ -264,25 +274,12 @@ private void DeserializeUpdatePreValues(IDataTypeDefinition item, XElement node)
{
if (!itemPreValues.ContainsKey(alias))
{
LogHelper.Debug<DataTypeSerializer>("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);
}
*/
}
}

Expand Down Expand Up @@ -357,7 +354,6 @@ private XElement SerializePreValues(IDataTypeDefinition item, XElement node)

nodePreValues.Add(preValueNode);
}

return nodePreValues;
}

Expand Down

0 comments on commit d72ed4a

Please sign in to comment.