DigitalFormDocumentDataModel.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using InABox.Clients;
  5. namespace InABox.Core
  6. {
  7. public delegate void QAFormDocumentsLoadedHandler(IQAFormDocumentDataModel model);
  8. public delegate void StartedEventHandler();
  9. public interface IQAFormDocumentDataModel
  10. {
  11. List<Document> Documents { get; }
  12. List<IEntityDocument> DocumentLinks { get; }
  13. event QAFormDocumentsLoadedHandler OnDocumentsLoaded;
  14. }
  15. public class DigitalFormDocumentDataModel<TEntity, TEntityLink, TInstance, TDocument> : DigitalFormDataModel<TEntity, TEntityLink, TInstance>,
  16. IQAFormDocumentDataModel
  17. where TEntity : Entity, IRemotable, IPersistent, new()
  18. where TEntityLink : EntityLink<TEntity>
  19. where TInstance : Entity, IRemotable, IPersistent, IDigitalFormInstance<TEntityLink>, new()
  20. where TDocument : Entity, IRemotable, IPersistent, IEntityDocument, new()
  21. {
  22. public DigitalFormDocumentDataModel(Guid entityid, Guid instanceid, DigitalFormVariable[] variables) : base(entityid, instanceid, variables)
  23. {
  24. Documents = new List<Document>();
  25. DocumentLinks = new List<IEntityDocument>();
  26. }
  27. public List<Document> Documents { get; }
  28. public List<IEntityDocument> DocumentLinks { get; }
  29. public event QAFormDocumentsLoadedHandler? OnDocumentsLoaded;
  30. public override void AddQueries(MultiQuery client)
  31. {
  32. base.AddQueries(client);
  33. client.Add(
  34. new QueryDef<TDocument>(
  35. new Filter<TDocument>("EntityLink.ID").IsEqualTo(Entity.ID),
  36. null,
  37. null
  38. ),
  39. typeof(TDocument)
  40. );
  41. }
  42. public override void ParseQueries(MultiQuery client)
  43. {
  44. base.ParseQueries(client);
  45. if (client.Contains(typeof(TDocument)))
  46. {
  47. DocumentLinks.AddRange(client.Get(typeof(TDocument)).Rows.Select(x => x.ToObject<TDocument>()));
  48. var docids = DocumentLinks.Select(x => x.DocumentLink.ID).ToArray();
  49. new Client<Document>().Query(
  50. new Filter<Document>(x => x.ID).InList(docids),
  51. null,
  52. null,
  53. CoreRange.All,
  54. (table, error) =>
  55. {
  56. if (table != null)
  57. {
  58. foreach (var row in table.Rows)
  59. Documents.Add(row.ToObject<Document>());
  60. OnDocumentsLoaded?.Invoke(this);
  61. }
  62. else if(error != null)
  63. {
  64. Logger.Send(LogType.Error, "", CoreUtils.FormatException(error));
  65. }
  66. }
  67. );
  68. }
  69. }
  70. protected override void AddPrimaryUpdates(MultiSave client)
  71. {
  72. base.AddPrimaryUpdates(client);
  73. foreach (var doc in Documents.Where(x => x.ID.Equals(Guid.Empty)))
  74. client.Add(typeof(Document), doc);
  75. }
  76. protected override void AddSecondaryUpdates(MultiSave client)
  77. {
  78. base.AddSecondaryUpdates(client);
  79. foreach (var doc in Documents)
  80. if (!DocumentLinks.Any(x => x.DocumentLink.ID.Equals(doc.ID)))
  81. {
  82. var link = new TDocument();
  83. link.DocumentLink.ID = doc.ID;
  84. DocumentLinks.Add(link);
  85. }
  86. foreach (var link in DocumentLinks)
  87. {
  88. CoreUtils.SetPropertyValue(link, "EntityLink.ID", Entity.ID);
  89. if (link.IsChanged())
  90. client.Add(typeof(TDocument), (Entity)link);
  91. }
  92. }
  93. }
  94. }