DynamicDocumentGrid.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. if (!ReadOnly)
  126. {
  127. var id = row.Get<TDocument, Guid>(x => x.ID);
  128. var document = WorkingList.FirstOrDefault(x => x.ID.Equals(id));
  129. if (document != null)
  130. document.Superceded = document.Superceded == DateTime.MinValue ? DateTime.Now : DateTime.MinValue;
  131. return true;
  132. }
  133. else
  134. {
  135. return false;
  136. }
  137. }
  138. private BitmapImage DocumentImage(CoreRow? arg)
  139. {
  140. return Wpf.Resources.view.AsBitmapImage();
  141. }
  142. private bool ViewDocument(CoreRow? row)
  143. {
  144. var filename = row.Get<TDocument, string>(x => x.DocumentLink.FileName);
  145. if (Path.GetExtension(filename).ToUpper().Equals(".PDF"))
  146. {
  147. var viewer = new DocumentEditor(row.ToObject<TDocument>());
  148. viewer.Watermark = OnGetWaterMark?.Invoke(row);
  149. //viewer.PrintAllowed = true;
  150. viewer.SaveAllowed = true;
  151. viewer.ShowDialog();
  152. }
  153. else
  154. {
  155. var id = row.Get<TDocument, Guid>(x => x.DocumentLink.ID);
  156. var docrow = new Client<Document>().Query(new Filter<Document>(x => x.ID).IsEqualTo(id)).Rows.FirstOrDefault();
  157. if (docrow != null)
  158. {
  159. var tmpfile = Path.ChangeExtension(Path.GetTempFileName(), Path.GetExtension(filename));
  160. File.WriteAllBytes(tmpfile, docrow.Get<Document, byte[]>(x => x.Data));
  161. var gsProcessInfo = new ProcessStartInfo();
  162. gsProcessInfo.Verb = "open";
  163. gsProcessInfo.WindowStyle = ProcessWindowStyle.Normal;
  164. gsProcessInfo.FileName = tmpfile;
  165. gsProcessInfo.UseShellExecute = true;
  166. Process.Start(gsProcessInfo);
  167. }
  168. else
  169. {
  170. MessageBox.Show(string.Format("Unable to retrieve {0}!", filename));
  171. }
  172. }
  173. //Document doc = new Client<Document>().Load(new Filter<Document>(x => x.ID).IsEqualTo(id)).FirstOrDefault();
  174. //if (doc != null)
  175. //{
  176. // if (System.IO.Path.GetExtension(doc.FileName).ToUpper().Equals(".PDF"))
  177. // {
  178. // PDFViewer viewer = new PDFViewer(doc);
  179. // viewer.ShowDialog();
  180. // }
  181. // else
  182. // {
  183. // String filename = System.IO.Path.ChangeExtension(System.IO.Path.GetTempFileName(), System.IO.Path.GetExtension(doc.FileName));
  184. // System.IO.File.WriteAllBytes(filename, doc.Data);
  185. // ProcessStartInfo gsProcessInfo = new ProcessStartInfo();
  186. // gsProcessInfo.Verb = "open";
  187. // gsProcessInfo.WindowStyle = ProcessWindowStyle.Normal;
  188. // gsProcessInfo.UseShellExecute = true;
  189. // gsProcessInfo.FileName = filename;
  190. // Process.Start(gsProcessInfo);
  191. // }
  192. //}
  193. //else
  194. // MessageBox.Show("Document does nto exist!");
  195. return false;
  196. }
  197. public event OnGetWatermark OnGetWaterMark;
  198. protected override void OnDragEnd(Type entity, CoreTable table, DragEventArgs e)
  199. {
  200. if (entity == typeof(Document))
  201. {
  202. var refresh = false;
  203. var docIDS = table.Rows.Select(x => x.Get<Document, Guid>(x => x.ID)).ToArray();
  204. var columns = new Columns<Document>(x => x.ID);
  205. foreach (var column in VisibleColumns)
  206. {
  207. if (column.ColumnName.StartsWith("DocumentLink."))
  208. {
  209. columns.Add(string.Join('.', column.ColumnName.Split('.').Skip(1)));
  210. }
  211. }
  212. var docs = new Client<Document>()
  213. .Query(
  214. new Filter<Document>(x => x.ID).InList(docIDS),
  215. columns);
  216. foreach (var doc in docs.ToObjects<Document>())
  217. {
  218. var entityDocument = new TDocument();
  219. entityDocument.EntityLink.ID = Item.ID;
  220. entityDocument.DocumentLink.ID = doc.ID;
  221. entityDocument.DocumentLink.Synchronise(doc);
  222. SaveItem(entityDocument);
  223. refresh = true;
  224. }
  225. if (refresh)
  226. {
  227. Refresh(false, true);
  228. }
  229. }
  230. else
  231. {
  232. base.OnDragEnd(entity, table, e);
  233. }
  234. }
  235. protected override void DoAdd(bool OpenEditorOnDirectEdit = false)
  236. {
  237. var dlg = new OpenFileDialog();
  238. dlg.Multiselect = true;
  239. if (dlg.ShowDialog() == true)
  240. {
  241. using (new WaitCursor())
  242. {
  243. var docs = new List<Document>();
  244. foreach (var filename in dlg.FileNames)
  245. {
  246. // Create a Document
  247. var doc = new Document();
  248. doc.FileName = Path.GetFileName(filename).ToLower();
  249. doc.TimeStamp = new FileInfo(dlg.FileName).LastWriteTime;
  250. doc.Data = File.ReadAllBytes(filename);
  251. doc.CRC = CoreUtils.CalculateCRC(doc.Data);
  252. docs.Add(doc);
  253. }
  254. if (docs.Any())
  255. {
  256. new Client<Document>().Save(docs, "Initial Upload");
  257. foreach (var doc in docs)
  258. {
  259. var newitem = CreateItem();
  260. var prop = (IEntityLink)otherproperty.GetValue(newitem);
  261. prop.ID = doc.ID;
  262. prop.Synchronise(doc);
  263. SaveItem(newitem);
  264. }
  265. }
  266. }
  267. Refresh(false, true);
  268. }
  269. }
  270. }
  271. }