StagingSetoutGrid.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using InABox.DynamicGrid;
  5. using javax.print.attribute.standard;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. namespace PRSDesktop
  14. {
  15. public class StagingSetoutGrid : DynamicDataGrid<StagingSetout>
  16. {
  17. List<string> _existingFileNames = new List<string>();
  18. List<Job> _jobs = new List<Job>();
  19. public StagingSetoutGrid()
  20. {
  21. HiddenColumns.Add(x => x.Setout.ID);
  22. HiddenColumns.Add(x => x.JobLink.ID);
  23. Options.Add(DynamicGridOption.FilterRows);
  24. Options.Add(DynamicGridOption.SelectColumns);
  25. Options.Add(DynamicGridOption.RecordCount);
  26. LoadJobs();
  27. ScanFiles();
  28. }
  29. private void LoadJobs()
  30. {
  31. CoreTable table = new Client<Job>().Query(null, new Columns<Job>(x => x.ID, x => x.JobNumber, x => x.Name));
  32. if (table.Rows.Any())
  33. foreach (CoreRow row in table.Rows)
  34. _jobs.Add(row.ToObject<Job>());
  35. }
  36. public void ScanFiles()
  37. {
  38. IEnumerable<FileInfo> files = GetFiles();
  39. LoadExistingStaging();
  40. List<Document> documents = new List<Document>();
  41. List<StagingSetoutDocument> stagingdocs = new List<StagingSetoutDocument>();
  42. foreach (var file in files)
  43. {
  44. if (!file.FullName.EndsWith(".pdf"))
  45. continue;
  46. if (CheckStagingSetoutExists(file))
  47. continue;
  48. documents.Add(ReadFile(file));
  49. }
  50. new Client<Document>().Save(documents, "Created from sync setout function");
  51. var messages = new List<string>();
  52. foreach (var doc in documents)
  53. {
  54. var stagingsetout = new StagingSetout();
  55. stagingsetout.Number = doc.FileName.Substring(0, doc.FileName.Length - 4);
  56. stagingsetout.JobLink.ID = FindJob(doc.FileName.Substring(0, 4));
  57. new Client<StagingSetout>().Save(stagingsetout, "Created from sync setout function");
  58. var stagingsetoutdoc = new StagingSetoutDocument();
  59. stagingsetoutdoc.EntityLink.ID = stagingsetout.ID;
  60. stagingsetoutdoc.DocumentLink.ID = doc.ID;
  61. stagingdocs.Add(stagingsetoutdoc);
  62. if (stagingsetout.JobLink.ID == Guid.Empty)
  63. messages.Add(doc.FileName);
  64. }
  65. if (messages.Any())
  66. MessageBox.Show("Job(s) not found for the following document(s): " + string.Join("," + Environment.NewLine, messages), "Warning");
  67. new Client<StagingSetoutDocument>().Save(stagingdocs, "Created from sync setout function");
  68. }
  69. private Guid FindJob(string filePreFix)
  70. {
  71. var job = _jobs.FirstOrDefault(x => x.JobNumber.Equals(filePreFix));
  72. if (job == null)
  73. return Guid.Empty;
  74. else
  75. return job.ID;
  76. }
  77. private Document ReadFile(FileInfo file)
  78. {
  79. Document doc = new Document();
  80. doc.FileName = file.Name;
  81. doc.Data = GetData(file);
  82. return doc;
  83. }
  84. private byte[] GetData(FileInfo file)
  85. {
  86. Stream stream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.Read);
  87. byte[] pdfData = new byte[stream.Length];
  88. stream.Read(pdfData, 0, System.Convert.ToInt32(pdfData.Length));
  89. stream.Dispose();
  90. return pdfData;
  91. }
  92. private IEnumerable<FileInfo> GetFiles()
  93. {
  94. string folder = @"U:\" + "9999" + " " + "Test Job" + @"\COM-AL SETOUTS";
  95. var dir = new DirectoryInfo(folder);
  96. //return dir.EnumerateFiles();
  97. return dir.GetFiles();
  98. }
  99. public void ReloadFile(IEntityDocument document)
  100. {
  101. var files = GetFiles();
  102. foreach (var file in files)
  103. {
  104. if (file.Name == document.DocumentLink.FileName)
  105. {
  106. CoreTable table = new Client<Document>().Query(new Filter<Document>(x => x.ID).IsEqualTo(document.DocumentLink.ID));
  107. var doc = table.Rows.FirstOrDefault().ToObject<Document>();
  108. doc.Data = GetData(file);
  109. new Client<Document>().Save(doc, "Reloaded Marked up Document");
  110. return;
  111. }
  112. }
  113. }
  114. private void LoadExistingStaging()
  115. {
  116. CoreTable stagingseouttable = new Client<StagingSetout>().Query(
  117. new Filter<StagingSetout>(x => x.Archived).IsEqualTo(DateTime.MinValue),
  118. new Columns<StagingSetout>(x => x.Number));
  119. var ids = new List<Guid>();
  120. foreach (var row in stagingseouttable.Rows)
  121. _existingFileNames.Add(row.Get<StagingSetout, string>(x => x.Number));
  122. }
  123. private bool CheckStagingSetoutExists(FileInfo file)
  124. {
  125. foreach (var existing in _existingFileNames)
  126. {
  127. if (file.Name.Substring(0, file.Name.Length - 4).Equals(existing))
  128. return true;
  129. }
  130. return false;
  131. }
  132. protected override void Reload(Filters<StagingSetout> criteria, Columns<StagingSetout> columns, ref SortOrder<StagingSetout>? sort, Action<CoreTable?, Exception?> action)
  133. {
  134. criteria.Add(new Filter<StagingSetout>(x => x.Archived).IsEqualTo(DateTime.MinValue));
  135. base.Reload(criteria, columns, ref sort, action);
  136. }
  137. public void DeleteFile(IEntityDocument document)
  138. {
  139. var file = GetFiles().FirstOrDefault(x => x.Name == document.DocumentLink.FileName);
  140. if (file != null)
  141. file.Delete();
  142. }
  143. }
  144. }