EntityLink.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using Expressive;
  2. using System;
  3. using System.Dynamic;
  4. using System.Linq;
  5. namespace InABox.Core
  6. {
  7. public interface IEntityLink
  8. {
  9. Guid ID { get; set; }
  10. Guid Deleted { get; set; }
  11. bool Synchronise(object entity);
  12. bool Clear();
  13. bool IsValid();
  14. BaseObject GetLinkedParent();
  15. void SetLinkedParent(BaseObject obj);
  16. string GetLinkedPath();
  17. void SetLinkedPath(string path);
  18. }
  19. public interface IEntityLink<T> : IEntityLink
  20. {
  21. }
  22. public abstract class EntityLink<T> : BaseObject, IEntityLink<T> where T : BaseObject, new()
  23. {
  24. /*
  25. private Func<BaseObject>? _linkedentity;
  26. [DoNotSerialize]
  27. protected BaseObject? LinkedEntity() => _linkedentity?.Invoke();*/
  28. private BaseObject _linkedParent;
  29. private string _linkedPath;
  30. public void SetLinkedParent(BaseObject parent)
  31. {
  32. _linkedParent = parent;
  33. }
  34. public void SetLinkedPath(string path)
  35. {
  36. _linkedPath = path;
  37. }
  38. public BaseObject GetLinkedParent() => _linkedParent;
  39. public string GetLinkedPath() => _linkedPath;
  40. /*
  41. [Obsolete("Please supply linked Entity")]
  42. public EntityLink()
  43. {
  44. }
  45. public EntityLink(Func<BaseObject>? entity, string name)
  46. {
  47. _linkedentity = entity;
  48. }*/
  49. [NullEditor]
  50. public abstract Guid ID { get; set; }
  51. [NullEditor]
  52. public Guid Deleted { get; set; }
  53. public virtual bool Synchronise(object Entity)
  54. {
  55. var result = false;
  56. var props = CoreUtils.PropertyList(GetType(), x => (!x.Name.Equals("ID") || Entity == null) && !x.DeclaringType.Equals(typeof(BaseObject)), false);
  57. foreach (var key in props.Keys)
  58. {
  59. if (CoreUtils.HasProperty(Entity.GetType(), key))
  60. {
  61. var prop = CoreUtils.GetProperty(typeof(T), key);
  62. if (prop != null && prop.PropertyType.Equals(props[key]))
  63. {
  64. if (prop.PropertyType.GetInterfaces().Any(x => x == typeof(IEntityLink)))
  65. {
  66. var tgtlink = CoreUtils.GetPropertyValue(this, key) as IEntityLink;
  67. var srclink = CoreUtils.GetPropertyValue(Entity, key) as IEntityLink;
  68. tgtlink.ID = srclink.ID;
  69. tgtlink.Synchronise(srclink);
  70. }
  71. else
  72. {
  73. var oldvalue = CoreUtils.GetPropertyValue(this, key);
  74. var newvalue = CoreUtils.GetPropertyValue(Entity, key);
  75. if ((oldvalue == null && newvalue != null) || (oldvalue != null && !oldvalue.Equals(newvalue)))
  76. {
  77. result = true;
  78. try
  79. {
  80. CoreUtils.SetPropertyValue(this, key, newvalue);
  81. }
  82. catch (Exception e)
  83. {
  84. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  85. }
  86. }
  87. }
  88. }
  89. }
  90. }
  91. foreach (var link in LinkedProperties.Find(this, out var parent))
  92. {
  93. link.Update(this, parent);
  94. result = true;
  95. }
  96. return result;
  97. }
  98. public bool Clear()
  99. {
  100. var result = false;
  101. var props = CoreUtils.PropertyList(GetType(), x => !x.Name.Equals("ID"), false);
  102. foreach (var key in props.Keys)
  103. {
  104. var prop = CoreUtils.GetProperty(typeof(T), key);
  105. if (prop != null && prop.PropertyType.Equals(props[key]))
  106. {
  107. var oldvalue = CoreUtils.GetPropertyValue(this, key);
  108. var newvalue = prop.PropertyType.Equals(typeof(string)) ? "" : Activator.CreateInstance(prop.PropertyType);
  109. if ((oldvalue == null && newvalue != null) || (oldvalue != null && !oldvalue.Equals(newvalue)))
  110. {
  111. result = true;
  112. CoreUtils.SetPropertyValue(this, key, newvalue);
  113. }
  114. }
  115. }
  116. return result;
  117. }
  118. protected override void DoPropertyChanged(string name, object? before, object? after)
  119. {
  120. if (IsObserving())
  121. {
  122. if(LinkedProperties.Find(this, name, out var link, out var parent))
  123. {
  124. link.Update(this, parent);
  125. }
  126. /*var link = LinkedProperties.Find(le, this).FirstOrDefault(x => x.Source.Equals(name));
  127. if (link != null)
  128. link.Update(this, le);*/
  129. }
  130. }
  131. public bool IsValid() => !ID.Equals(Guid.Empty) && Deleted.Equals(Guid.Empty);
  132. }
  133. }