Deletion.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using InABox.Core;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace InABox.Core
  9. {
  10. public class SetNullData
  11. {
  12. public Guid EntityID { get; set; }
  13. public Guid ParentID { get; set; }
  14. public string Property { get; set; }
  15. public SetNullData(Guid entityID, string property, Guid parentID)
  16. {
  17. EntityID = entityID;
  18. ParentID = parentID;
  19. Property = property;
  20. }
  21. }
  22. public class DeletionData
  23. {
  24. public Dictionary<string, List<SetNullData>> SetNulls { get; set; } = new Dictionary<string, List<SetNullData>>();
  25. public Dictionary<string, CoreTable> Cascades { get; set; } = new Dictionary<string, CoreTable>();
  26. private static bool IsDeletionColumn(IProperty property)
  27. {
  28. if (property.IsCalculated) return false;
  29. if (property is StandardProperty standardProperty && standardProperty.Property.GetCustomAttribute<DoNotPersist>() != null)
  30. return false;
  31. if (property.Parent is null) return true;
  32. if (property.Parent.IsEntityLink && !property.Name.EndsWith(".ID")) return false;
  33. if (property.Parent.HasParentEntityLink()) return false;
  34. return true;
  35. }
  36. public static IColumns DeletionColumns(Type T)
  37. {
  38. var columns = Columns.Create(T);
  39. foreach(var property in DatabaseSchema.Properties(T))
  40. {
  41. if (IsDeletionColumn(property))
  42. columns.Add(property.Name);
  43. }
  44. return columns;
  45. }
  46. public static Columns<T> DeletionColumns<T>() => (DeletionColumns(typeof(T)) as Columns<T>)!;
  47. public void DeleteEntity<T>(T entity)
  48. where T : Entity, new()
  49. {
  50. var entityName = typeof(T).EntityName();
  51. if(!Cascades.TryGetValue(entityName, out var table))
  52. {
  53. table = new CoreTable();
  54. table.LoadColumns(DeletionColumns<T>());
  55. Cascades.Add(entityName, table);
  56. }
  57. table.LoadRow(entity);
  58. }
  59. public void DeleteEntity<T>(CoreRow row)
  60. where T : Entity, new()
  61. {
  62. var entityName = typeof(T).EntityName();
  63. if(!Cascades.TryGetValue(entityName, out var table))
  64. {
  65. table = new CoreTable();
  66. table.LoadColumns(DeletionColumns<T>());
  67. Cascades.Add(entityName, table);
  68. }
  69. table.LoadRows(new CoreRow[] { row });
  70. }
  71. public void SetNullEntity<T>(Guid entityID, string property, Guid parentID)
  72. where T : Entity, new()
  73. {
  74. var entityName = typeof(T).EntityName();
  75. if (!SetNulls.TryGetValue(entityName, out var list))
  76. {
  77. list = new List<SetNullData>();
  78. SetNulls.Add(entityName, list);
  79. }
  80. list.Add(new SetNullData(entityID, property, parentID));
  81. }
  82. }
  83. // A Deletion is not IRemotable; *no one* but the database should know about it.
  84. public class Deletion : Entity, ILicense<CoreLicense>, IPersistent
  85. {
  86. [DateTimeEditor(Editable = Editable.Disabled)]
  87. public DateTime DeletionDate { get; set; }
  88. [TextBoxEditor(Editable = Editable.Disabled)]
  89. public string DeletedBy { get; set; }
  90. [TextBoxEditor(Editable = Editable.Disabled)]
  91. public string HeadTable { get; set; }
  92. [TextBoxEditor(Editable = Editable.Disabled)]
  93. public string Description { get; set; }
  94. [NullEditor]
  95. public string Data { get; set; }
  96. }
  97. }