Deletion.cs 3.5 KB

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