DigitalFormDocumentFactory.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. using InABox.Core;
  8. namespace PRS.Mobile
  9. {
  10. public interface IDigitalFormDocumentHandler
  11. {
  12. void LoadDocument(Guid id, Action<byte[]> callback);
  13. Guid SaveDocument(byte[] data);
  14. void Run(Action<bool> status);
  15. }
  16. public abstract class DigitalFormDocumentHandler : IDigitalFormDocumentHandler
  17. {
  18. private static String FileName(Guid id) => $"{id}.formdocument";
  19. protected abstract String CachePath { get; }
  20. protected abstract bool IsConnected { get; }
  21. public void LoadDocument(Guid id, Action<byte[]> callback)
  22. {
  23. var fullpath = Path.Combine(CachePath, FileName(id));
  24. if (File.Exists(fullpath))
  25. callback(File.ReadAllBytes(fullpath));
  26. else
  27. {
  28. new Client<Document>().Query(
  29. new Filter<Document>(x => x.FileName).IsEqualTo(FileName(id)),
  30. null,
  31. null,
  32. (o, e) =>
  33. {
  34. var row = o?.Rows.FirstOrDefault();
  35. if (row != null)
  36. callback(row.Get<Document, byte[]>(c => c.Data));
  37. }
  38. );
  39. }
  40. }
  41. public Guid SaveDocument(byte[] data)
  42. {
  43. Guid result = Guid.NewGuid();
  44. var filename = Path.Combine(CachePath, FileName(result));
  45. File.WriteAllBytes(filename,data);
  46. return result;
  47. }
  48. public void Run(Action<bool> status)
  49. {
  50. Task.Run(
  51. () =>
  52. {
  53. bool? previouslyActive = null;
  54. while (true)
  55. {
  56. var file = Directory.EnumerateFiles(CachePath, "*.formdocument")
  57. .FirstOrDefault();
  58. var isActive = !String.IsNullOrWhiteSpace(file);
  59. if (isActive != previouslyActive)
  60. {
  61. previouslyActive = isActive;
  62. status(isActive);
  63. }
  64. if (!String.IsNullOrWhiteSpace(file) && File.Exists(file) && IsConnected)
  65. {
  66. var data = File.ReadAllBytes(file);
  67. var document = new Document()
  68. {
  69. FileName = Path.GetFileName(file),
  70. Data = data,
  71. CRC = CoreUtils.CalculateCRC(data),
  72. TimeStamp = DateTime.Now,
  73. Created = DateTime.Now,
  74. CreatedBy = ClientFactory.UserID
  75. };
  76. new Client<Document>().Save(document, "Uploaded from Mobile Device");
  77. File.Delete(file);
  78. }
  79. Thread.Sleep(1000);
  80. }
  81. }
  82. );
  83. }
  84. }
  85. public static class DigitalFormDocumentFactory
  86. {
  87. private static IDigitalFormDocumentHandler _handler;
  88. public static void Run<THandler>(Action<bool> status) where THandler : IDigitalFormDocumentHandler, new()
  89. {
  90. _handler = new THandler();
  91. _handler.Run(status);
  92. }
  93. public static void LoadDocument(Guid id, Action<byte[]> callback) => _handler.LoadDocument(id, callback);
  94. public static Guid SaveDocument(byte[] data) => _handler.SaveDocument(data);
  95. }
  96. }