DigitalFormDocumentFactory.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using InABox.Clients;
  7. namespace InABox.Core
  8. {
  9. public interface IDigitalFormDocumentHandler
  10. {
  11. void LoadDocument(Guid id, Action<byte[]> callback);
  12. byte[] LoadDocument(Guid id);
  13. Guid SaveDocument(byte[] data);
  14. void Run();
  15. void Stop();
  16. }
  17. public abstract class DigitalFormDocumentHandler : IDigitalFormDocumentHandler
  18. {
  19. private static String FileName(Guid id) => $"{id}.formdocument";
  20. protected abstract String CachePath { get; }
  21. protected abstract bool IsConnected { get; }
  22. private static Action<bool>? _status;
  23. public byte[] LoadDocument(Guid id)
  24. {
  25. var fullpath = Path.Combine(CachePath, FileName(id));
  26. if (File.Exists(fullpath))
  27. return File.ReadAllBytes(fullpath);
  28. var result = new Client<Document>().Query(
  29. new Filter<Document>(x => x.FileName).IsEqualTo(FileName(id)),
  30. null,
  31. null
  32. ).Rows.FirstOrDefault()?.Get<Document,byte[]>(c=>c.Data);
  33. return result;
  34. }
  35. public void LoadDocument(Guid id, Action<byte[]> callback)
  36. {
  37. var fullpath = Path.Combine(CachePath, FileName(id));
  38. if (File.Exists(fullpath))
  39. callback(File.ReadAllBytes(fullpath));
  40. else
  41. {
  42. new Client<Document>().Query(
  43. new Filter<Document>(x => x.FileName).IsEqualTo(FileName(id)),
  44. null,
  45. null,
  46. CoreRange.All,
  47. (o, e) =>
  48. {
  49. var row = o?.Rows.FirstOrDefault();
  50. if (row != null)
  51. callback(row.Get<Document, byte[]>(c => c.Data));
  52. }
  53. );
  54. }
  55. }
  56. public Guid SaveDocument(byte[] data)
  57. {
  58. Guid result = Guid.NewGuid();
  59. var filename = Path.Combine(CachePath, FileName(result));
  60. File.WriteAllBytes(filename,data);
  61. return result;
  62. }
  63. private CancellationTokenSource? _cancel;
  64. public void Stop()
  65. {
  66. if (_cancel != null)
  67. {
  68. _cancel.Cancel();
  69. _cancel = null;
  70. }
  71. }
  72. protected DigitalFormDocumentHandler(Action<bool> status)
  73. {
  74. _status = status;
  75. }
  76. public void Run()
  77. {
  78. Stop();
  79. _cancel = new CancellationTokenSource();
  80. Task.Run(
  81. () =>
  82. {
  83. bool? previouslyActive = null;
  84. while (_cancel?.IsCancellationRequested != true)
  85. {
  86. var file = Directory.EnumerateFiles(CachePath, "*.formdocument")
  87. .FirstOrDefault();
  88. var isActive = !String.IsNullOrWhiteSpace(file);
  89. if (isActive != previouslyActive)
  90. {
  91. previouslyActive = isActive;
  92. _status?.Invoke(isActive);
  93. }
  94. if (!String.IsNullOrWhiteSpace(file) && File.Exists(file) && IsConnected)
  95. {
  96. var data = File.ReadAllBytes(file);
  97. var document = new Document()
  98. {
  99. FileName = Path.GetFileName(file),
  100. Data = data,
  101. CRC = CoreUtils.CalculateCRC(data),
  102. TimeStamp = DateTime.Now,
  103. Created = DateTime.Now,
  104. CreatedBy = ClientFactory.UserID
  105. };
  106. new Client<Document>().Save(document, "Uploaded from Mobile Device");
  107. File.Delete(file);
  108. }
  109. Thread.Sleep(1000);
  110. }
  111. },
  112. _cancel.Token
  113. );
  114. }
  115. }
  116. public static class DigitalFormDocumentFactory
  117. {
  118. private static IDigitalFormDocumentHandler? _handler;
  119. public static void Init<THandler>(THandler handler) where THandler : IDigitalFormDocumentHandler
  120. {
  121. _handler = handler;
  122. }
  123. public static void Run() => _handler?.Run();
  124. public static void Stop() => _handler?.Stop();
  125. public static void LoadDocument(Guid id, Action<byte[]> callback) => _handler?.LoadDocument(id, callback);
  126. public static byte[]? LoadDocument(Guid id) => _handler?.LoadDocument(id);
  127. public static Guid SaveDocument(byte[] data) => _handler?.SaveDocument(data) ?? Guid.Empty;
  128. }
  129. }