DynamicDocumentGrid.cs 8.3 KB

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