DynamicDocumentGrid.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Windows;
  7. using System.Windows.Media.Imaging;
  8. using InABox.Clients;
  9. using InABox.Core;
  10. using InABox.WPF;
  11. using Microsoft.Win32;
  12. namespace InABox.DynamicGrid
  13. {
  14. public delegate String OnGetWatermark(CoreRow row);
  15. public class DynamicDocumentGrid<TDocument, TEntity> : DynamicManyToManyGrid<TDocument, TEntity>
  16. where TEntity : Entity, IPersistent, IRemotable, new()
  17. where TDocument : Entity, IEntityDocument, IPersistent, IRemotable, new() // Entity, IPersistent, IRemotable, IManyToMany<TEntity, Document>, new()
  18. {
  19. private DynamicActionColumn supercedecolumn;
  20. public bool ShowSupercededColumn
  21. {
  22. get
  23. {
  24. return supercedecolumn.Position != DynamicActionColumnPosition.Hidden;
  25. }
  26. set
  27. {
  28. supercedecolumn.Position = value ? DynamicActionColumnPosition.End : DynamicActionColumnPosition.Hidden;
  29. }
  30. }
  31. public DynamicDocumentGrid()
  32. {
  33. MultiSelect = false;
  34. HiddenColumns.Add(x => x.DocumentLink.ID);
  35. HiddenColumns.Add(x => x.Superceded);
  36. HiddenColumns.Add(x => x.DocumentLink.FileName);
  37. ActionColumns.Add(new DynamicActionColumn(DocumentImage, ViewDocument) { Position = DynamicActionColumnPosition.Start });
  38. supercedecolumn = new DynamicActionColumn(SupercededImage, SupercedeDocument);
  39. ActionColumns.Add(supercedecolumn);
  40. }
  41. public override int Order()
  42. {
  43. return int.MaxValue;
  44. }
  45. private BitmapImage SupercededImage(CoreRow row)
  46. {
  47. if (row == null)
  48. return Properties.Resources.tick.AsBitmapImage();
  49. if (row.Get<TDocument, DateTime>(x => x.Superceded) != DateTime.MinValue)
  50. return Properties.Resources.warning.AsBitmapImage();
  51. return Properties.Resources.tick.AsBitmapImage();
  52. }
  53. private bool SupercedeDocument(CoreRow row)
  54. {
  55. var id = row.Get<TDocument, Guid>(x => x.ID);
  56. var document = WorkingList.FirstOrDefault(x => x.ID.Equals(id));
  57. if (document != null)
  58. document.Superceded = document.Superceded == DateTime.MinValue ? DateTime.Now : DateTime.MinValue;
  59. return true;
  60. }
  61. private BitmapImage DocumentImage(CoreRow arg)
  62. {
  63. return Properties.Resources.view.AsBitmapImage();
  64. }
  65. private bool ViewDocument(CoreRow row)
  66. {
  67. var filename = row.Get<TDocument, string>(x => x.DocumentLink.FileName);
  68. if (Path.GetExtension(filename).ToUpper().Equals(".PDF"))
  69. {
  70. var viewer = new DocumentEditor(row.ToObject<TDocument>());
  71. viewer.Watermark = OnGetWaterMark?.Invoke(row);
  72. viewer.PrintAllowed = true;
  73. viewer.SaveAllowed = true;
  74. viewer.ShowDialog();
  75. }
  76. else
  77. {
  78. var id = row.Get<TDocument, Guid>(x => x.DocumentLink.ID);
  79. var docrow = new Client<Document>().Query(new Filter<Document>(x => x.ID).IsEqualTo(id)).Rows.FirstOrDefault();
  80. if (docrow != null)
  81. {
  82. var tmpfile = Path.ChangeExtension(Path.GetTempFileName(), Path.GetExtension(filename));
  83. File.WriteAllBytes(tmpfile, docrow.Get<Document, byte[]>(x => x.Data));
  84. var gsProcessInfo = new ProcessStartInfo();
  85. gsProcessInfo.Verb = "open";
  86. gsProcessInfo.WindowStyle = ProcessWindowStyle.Normal;
  87. gsProcessInfo.FileName = tmpfile;
  88. gsProcessInfo.UseShellExecute = true;
  89. Process.Start(gsProcessInfo);
  90. }
  91. else
  92. {
  93. MessageBox.Show(string.Format("Unable to retrieve {0}!", filename));
  94. }
  95. }
  96. //Document doc = new Client<Document>().Load(new Filter<Document>(x => x.ID).IsEqualTo(id)).FirstOrDefault();
  97. //if (doc != null)
  98. //{
  99. // if (System.IO.Path.GetExtension(doc.FileName).ToUpper().Equals(".PDF"))
  100. // {
  101. // PDFViewer viewer = new PDFViewer(doc);
  102. // viewer.ShowDialog();
  103. // }
  104. // else
  105. // {
  106. // String filename = System.IO.Path.ChangeExtension(System.IO.Path.GetTempFileName(), System.IO.Path.GetExtension(doc.FileName));
  107. // System.IO.File.WriteAllBytes(filename, doc.Data);
  108. // ProcessStartInfo gsProcessInfo = new ProcessStartInfo();
  109. // gsProcessInfo.Verb = "open";
  110. // gsProcessInfo.WindowStyle = ProcessWindowStyle.Normal;
  111. // gsProcessInfo.UseShellExecute = true;
  112. // gsProcessInfo.FileName = filename;
  113. // Process.Start(gsProcessInfo);
  114. // }
  115. //}
  116. //else
  117. // MessageBox.Show("Document does nto exist!");
  118. return false;
  119. }
  120. public event OnGetWatermark OnGetWaterMark;
  121. protected override void DoAdd()
  122. {
  123. var dlg = new OpenFileDialog();
  124. dlg.Multiselect = true;
  125. if (dlg.ShowDialog() == true)
  126. {
  127. using (new WaitCursor())
  128. {
  129. var docs = new List<Document>();
  130. foreach (var filename in dlg.FileNames)
  131. {
  132. // Create a Document
  133. var doc = new Document();
  134. doc.FileName = Path.GetFileName(filename).ToLower();
  135. doc.TimeStamp = new FileInfo(dlg.FileName).LastWriteTime;
  136. doc.Data = File.ReadAllBytes(filename);
  137. doc.CRC = CoreUtils.CalculateCRC(doc.Data);
  138. docs.Add(doc);
  139. }
  140. if (docs.Any())
  141. {
  142. new Client<Document>().Save(docs, "Initial Upload");
  143. foreach (var doc in docs)
  144. {
  145. var newitem = CreateItem();
  146. var prop = (IEntityLink)otherproperty.GetValue(newitem);
  147. prop.ID = doc.ID;
  148. prop.Synchronise(doc);
  149. SaveItem(newitem);
  150. }
  151. }
  152. }
  153. Refresh(false, true);
  154. }
  155. }
  156. }
  157. }