DynamicDocumentGrid.cs 11 KB

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