EntityLink.cs 5.0 KB

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