Jelajahi Sumber

Refactored DigitalFormDocumentFactory

Frank van den Bos 1 tahun lalu
induk
melakukan
60f42f932a
1 mengubah file dengan 114 tambahan dan 0 penghapusan
  1. 114 0
      InABox.Core/DigitalForms/DigitalFormDocumentFactory.cs

+ 114 - 0
InABox.Core/DigitalForms/DigitalFormDocumentFactory.cs

@@ -0,0 +1,114 @@
+using System;
+using System.IO;
+using System.Linq;
+using System.Threading;
+using System.Threading.Tasks;
+using InABox.Clients;
+using InABox.Core;
+
+namespace PRS.Mobile
+{
+
+    public interface IDigitalFormDocumentHandler
+    {
+        void LoadDocument(Guid id, Action<byte[]> callback);
+        Guid SaveDocument(byte[] data);
+        void Run(Action<bool> status);
+    }
+
+    public abstract class DigitalFormDocumentHandler : IDigitalFormDocumentHandler
+    {
+
+        private static String FileName(Guid id) => $"{id}.formdocument";
+        
+        protected abstract String CachePath { get; }
+        
+        protected abstract bool IsConnected { get; }
+        
+        public void LoadDocument(Guid id, Action<byte[]> callback)
+        {
+            var fullpath = Path.Combine(CachePath, FileName(id));
+            if (File.Exists(fullpath))
+                callback(File.ReadAllBytes(fullpath));
+            else
+            {
+                new Client<Document>().Query(
+                    new Filter<Document>(x => x.FileName).IsEqualTo(FileName(id)),
+                    null,
+                    null,
+                    (o, e) =>
+                    {
+                        var row = o?.Rows.FirstOrDefault();
+                        if (row != null)
+                            callback(row.Get<Document, byte[]>(c => c.Data));
+                    }
+                );
+            }
+        }
+
+        public Guid SaveDocument(byte[] data)
+        {
+            Guid result = Guid.NewGuid();
+            var filename = Path.Combine(CachePath, FileName(result));
+            File.WriteAllBytes(filename,data);
+            return result;
+        }
+        
+        public void Run(Action<bool> status)
+        {
+            Task.Run(
+                () =>
+                {
+                    bool? previouslyActive = null;
+                    while (true)
+                    {
+                        var file = Directory.EnumerateFiles(CachePath, "*.formdocument")
+                            .FirstOrDefault();
+
+                        var isActive = !String.IsNullOrWhiteSpace(file);
+                        if (isActive != previouslyActive)
+                        {
+                            previouslyActive = isActive;
+                            status(isActive);
+                        }
+
+                        if (!String.IsNullOrWhiteSpace(file) && File.Exists(file) && IsConnected)
+                        {
+                            var data = File.ReadAllBytes(file);
+                            var document = new Document()
+                            {
+                                FileName = Path.GetFileName(file),
+                                Data = data,
+                                CRC = CoreUtils.CalculateCRC(data),
+                                TimeStamp = DateTime.Now,
+                                Created = DateTime.Now,
+                                CreatedBy = ClientFactory.UserID
+                            };
+                            new Client<Document>().Save(document, "Uploaded from Mobile Device");
+                            File.Delete(file);
+                        }
+
+                        Thread.Sleep(1000);
+                    }
+                }
+            );
+        }
+    }
+
+    public static class DigitalFormDocumentFactory
+    {
+
+        private static IDigitalFormDocumentHandler _handler;
+
+        public static void Run<THandler>(Action<bool> status) where THandler : IDigitalFormDocumentHandler, new()
+        {
+            _handler = new THandler();
+            _handler.Run(status);
+        }
+        
+        public static void LoadDocument(Guid id, Action<byte[]> callback) => _handler.LoadDocument(id, callback);
+
+        public static Guid SaveDocument(byte[] data) => _handler.SaveDocument(data);
+        
+    }
+}