Skip to content

Commit

Permalink
fix - relation types on 8.6+
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Jump committed Dec 15, 2020
1 parent 60d9ee8 commit fcee121
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 15 deletions.
2 changes: 1 addition & 1 deletion uSync8.ContentEdition/Handlers/RelationTypeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public override IEnumerable<uSyncAction> ExportAll(string folder, HandlerSetting
/// Relations that by default we exclude, if the exlude setting is used,then it will override these values
/// and they will be included if not explicity set;
/// </summary>
private const string defaultRelations = "relateParentDocumentOnDelete,relateParentMediaFolderOnDelete,relateDocumentOnCopy";
private const string defaultRelations = "relateParentDocumentOnDelete,relateParentMediaFolderOnDelete,relateDocumentOnCopy,umbMedia,umbDocument";

/// <summary>
/// Workout if we are excluding this relationType from export/import
Expand Down
101 changes: 87 additions & 14 deletions uSync8.ContentEdition/Serializers/RelationTypeSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,16 @@ protected override SyncAttempt<IRelationType> DeserializeCore(XElement node, Syn
var info = node.Element("Info");

var name = info.Element("Name").ValueOrDefault(string.Empty);
var parentType = info.Element("ParentType").ValueOrDefault(Guid.Empty);
var childType = info.Element("ChildType").ValueOrDefault(Guid.Empty);
var parentType = info.Element("ParentType").ValueOrDefault<Guid?>(null) ;
var childType = info.Element("ChildType").ValueOrDefault<Guid?>(null);
var bidirectional = info.Element("Bidirectional").ValueOrDefault(false);

var item = FindItem(node);

if (item == null)
{
item = new RelationType(childType, parentType, alias);
item = CreateRelation(name, alias, bidirectional, parentType, childType);
// item = new RelationType(childType.Value, parentType.Value, alias);
}

var details = new List<uSyncChange>();
Expand All @@ -70,16 +71,18 @@ protected override SyncAttempt<IRelationType> DeserializeCore(XElement node, Syn
item.Alias = alias;
}

if (item.ParentObjectType != parentType)
var currentParentType = GetGuidValue(item, nameof(item.ParentObjectType));
if (currentParentType != parentType)
{
details.AddUpdate("ParentType", item.ParentObjectType, parentType);
item.ParentObjectType = parentType;
details.AddUpdate("ParentType", currentParentType, parentType);
SetGuidValue(item, nameof(item.ParentObjectType), parentType);
}

if (item.ChildObjectType != childType)
var currentChildType = GetGuidValue(item, nameof(item.ChildObjectType));
if (currentChildType != childType)
{
details.AddUpdate("ChildType", item.ChildObjectType, childType);
item.ChildObjectType = childType;
details.AddUpdate("ChildType", currentChildType, childType);
SetGuidValue(item, nameof(item.ChildObjectType), childType);
}

if (item.IsBidirectional = bidirectional)
Expand Down Expand Up @@ -125,7 +128,6 @@ private IEnumerable<uSyncChange> DeserializeRelations(XElement node, IRelationTy

if (parentKey == Guid.Empty || childKey == Guid.Empty) continue;


var parentItem = entityService.Get(parentKey);
var childItem = entityService.Get(childKey);

Expand Down Expand Up @@ -165,21 +167,21 @@ public override bool IsValid(XElement node)
return base.IsValid(node);
}

protected override SyncAttempt<XElement> SerializeCore(IRelationType item, SyncSerializerOptions options)
protected override SyncAttempt<XElement> SerializeCore(IRelationType item, SyncSerializerOptions options)
{
var node = this.InitializeBaseNode(item, item.Alias);

node.Add(new XElement("Info",
new XElement("Name", item.Name),
new XElement("ParentType", item.ParentObjectType),
new XElement("ChildType", item.ChildObjectType),
new XElement("ParentType", GetGuidValue(item, nameof(item.ParentObjectType))),
new XElement("ChildType", GetGuidValue(item, nameof(item.ChildObjectType))),
new XElement("Bidirectional", item.IsBidirectional)));

if (options.GetSetting<bool>("IncludeRelations", true))
{
node.Add(SerializeRelations(item));
}

return SyncAttempt<XElement>.SucceedIf(
node != null,
item.Name,
Expand All @@ -188,6 +190,77 @@ protected override SyncAttempt<XElement> SerializeCore(IRelationType item, SyncS
ChangeType.Export);
}


private RelationType CreateRelation(string name, string alias, bool isBidrectional, Guid? parent, Guid? child)
{
// public RelationType(string name, string alias, bool isBidrectional, Guid? parentObjectType, Guid? childObjectType)

var t = typeof(RelationType);
var types = new Type[5];
types[0] = typeof(string);
types[1] = typeof(string);
types[2] = typeof(bool);
types[3] = typeof(Guid?);
types[4] = typeof(Guid?);

var constructorInfoObj = t.GetConstructor(types);
if (constructorInfoObj != null)
{
var parameters = new object[5];
parameters[0] = name;
parameters[1] = alias;
parameters[2] = isBidrectional;
parameters[3] = parent;
parameters[4] = child;

var obj = constructorInfoObj.Invoke(parameters);
return (RelationType)obj;
}
else
{
return new RelationType(child.Value, parent.Value, alias);
}

}

/// <summary>
/// gets a value from the interface that might be Guid or Guid?
/// </summary>
/// <remarks>
/// works around the interface changing v8.6 from Guid to Guid?
/// </remarks>
private Guid? GetGuidValue(IRelationType item, string propertyName)
{
var propertyInfo = item.GetType().GetProperty(propertyName);
if (propertyInfo == null) return null;

var value = propertyInfo.GetValue(item);
if (value == null) return null;

if (value is Guid guid)
{
return guid;
}

return null;
}

private void SetGuidValue(object item, string propertyName, Guid? value)
{
var propertyInfo = item.GetType().GetProperty(propertyName);
if (propertyInfo == null) return;

if (propertyInfo.PropertyType == typeof(Guid?))
{
propertyInfo.SetValue(item, value);
}
else if (propertyInfo.PropertyType == typeof(Guid) && value != null)
{
propertyInfo.SetValue(item, value.Value);
}
}


private XElement SerializeRelations(IRelationType item)
{
var relations = relationService.GetAllRelationsByRelationType(item.Id);
Expand Down

0 comments on commit fcee121

Please sign in to comment.