DynamicDocumentGrid.cs 11 KB

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