ScanGrid.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using InABox.DynamicGrid;
  5. using InABox.WPF;
  6. using NPOI.SS.Formula.Functions;
  7. using Syncfusion.Pdf;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows.Forms;
  15. namespace PRSDesktop
  16. {
  17. public class ScanGrid : DynamicDataGrid<Scan>
  18. {
  19. public string? AppliesTo { get; set; }
  20. public ScanGrid()
  21. {
  22. Options.BeginUpdate()
  23. .AddRange(DynamicGridOption.MultiSelect, DynamicGridOption.DragSource, DynamicGridOption.FilterRows)
  24. .Remove(DynamicGridOption.ImportData)
  25. .EndUpdate();
  26. }
  27. protected override void OnRowsDragStart(CoreRow[] rows)
  28. {
  29. var table = new CoreTable();
  30. table.Columns.Add(new CoreColumn { ColumnName = "ID", DataType = typeof(Guid) });
  31. foreach(var row in rows)
  32. {
  33. var newRow = table.NewRow();
  34. newRow.Set<Document, Guid>(x => x.ID, row.Get<Scan, Guid>(x => x.Document.ID));
  35. table.Rows.Add(newRow);
  36. }
  37. DragTable(typeof(Document), table);
  38. }
  39. private void UploadDocument(string filename, byte[] data)
  40. {
  41. var document = new Document
  42. {
  43. FileName = filename,
  44. CRC = CoreUtils.CalculateCRC(data),
  45. TimeStamp = DateTime.Now,
  46. Data = data
  47. };
  48. new Client<Document>().Save(document, "");
  49. var scan = new Scan();
  50. scan.Document.ID = document.ID;
  51. scan.AppliesTo = AppliesTo ?? "";
  52. new Client<Scan>().Save(scan, "");
  53. Dispatcher.Invoke(() =>
  54. {
  55. Refresh(false, true);
  56. });
  57. }
  58. private static PdfDocumentBase CombinePages(IEnumerable<DocumentManipulationWindow.Page> pages)
  59. {
  60. var document = new PdfDocument();
  61. foreach (var page in pages)
  62. {
  63. document.ImportPage(page.Pdf, page.PageIndex);
  64. }
  65. return document;
  66. }
  67. public void ShowDocumentWindow(List<DocumentManipulationWindow.Page> pages, string filename)
  68. {
  69. var window = new DocumentManipulationWindow(pages, filename);
  70. if (window.ShowDialog() == true)
  71. {
  72. Progress.ShowModal("Uploading Files", (progress) =>
  73. {
  74. foreach (var group in window.Groups)
  75. {
  76. progress.Report($"Uploading '{group.FileName}'");
  77. var doc = CombinePages(group.Pages);
  78. byte[] data;
  79. using (var ms = new MemoryStream())
  80. {
  81. doc.Save(ms);
  82. data = ms.ToArray();
  83. }
  84. UploadDocument(group.FileName, data);
  85. }
  86. });
  87. }
  88. }
  89. protected override void DoAdd()
  90. {
  91. ShowDocumentWindow(new(), "");
  92. }
  93. protected override Scan CreateItem()
  94. {
  95. var scan = base.CreateItem();
  96. scan.AppliesTo = AppliesTo ?? "";
  97. return scan;
  98. }
  99. protected override void GenerateColumns(DynamicGridColumns columns)
  100. {
  101. base.GenerateColumns(columns);
  102. columns.RemoveAll(x => x.ColumnName == "Document.FileName");
  103. columns.Add<Scan, string>(x => x.Document.FileName, 0, "Filename", "", InABox.Core.Alignment.MiddleLeft);
  104. }
  105. protected override void Reload(Filters<Scan> criteria, Columns<Scan> columns, ref SortOrder<Scan>? sort, Action<CoreTable?, Exception?> action)
  106. {
  107. if (!string.IsNullOrWhiteSpace(AppliesTo))
  108. {
  109. criteria.Add(new Filter<Scan>(x => x.AppliesTo).IsEqualTo(AppliesTo));
  110. }
  111. base.Reload(criteria, columns, ref sort, action);
  112. }
  113. }
  114. }