using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Windows; using System.Windows.Media.Imaging; using InABox.Clients; using InABox.Core; using InABox.WPF; using Microsoft.Win32; namespace InABox.DynamicGrid { public delegate String OnGetWatermark(CoreRow row); public class DynamicDocumentGrid : DynamicManyToManyGrid where TEntity : Entity, IPersistent, IRemotable, new() where TDocument : Entity, IEntityDocument, IPersistent, IRemotable, new() // Entity, IPersistent, IRemotable, IManyToMany, new() { private DynamicActionColumn supercedecolumn; public bool ShowSupercededColumn { get { return supercedecolumn.Position != DynamicActionColumnPosition.Hidden; } set { supercedecolumn.Position = value ? DynamicActionColumnPosition.End : DynamicActionColumnPosition.Hidden; } } public DynamicDocumentGrid() { MultiSelect = false; HiddenColumns.Add(x => x.DocumentLink.ID); HiddenColumns.Add(x => x.Superceded); HiddenColumns.Add(x => x.DocumentLink.FileName); ActionColumns.Add(new DynamicActionColumn(DocumentImage, ViewDocument) { Position = DynamicActionColumnPosition.Start }); supercedecolumn = new DynamicActionColumn(SupercededImage, SupercedeDocument); ActionColumns.Add(supercedecolumn); } public override int Order() { return int.MaxValue; } private BitmapImage SupercededImage(CoreRow row) { if (row == null) return Properties.Resources.tick.AsBitmapImage(); if (row.Get(x => x.Superceded) != DateTime.MinValue) return Properties.Resources.warning.AsBitmapImage(); return Properties.Resources.tick.AsBitmapImage(); } private bool SupercedeDocument(CoreRow row) { var id = row.Get(x => x.ID); var document = WorkingList.FirstOrDefault(x => x.ID.Equals(id)); if (document != null) document.Superceded = document.Superceded == DateTime.MinValue ? DateTime.Now : DateTime.MinValue; return true; } private BitmapImage DocumentImage(CoreRow arg) { return Properties.Resources.view.AsBitmapImage(); } private bool ViewDocument(CoreRow row) { var filename = row.Get(x => x.DocumentLink.FileName); if (Path.GetExtension(filename).ToUpper().Equals(".PDF")) { var viewer = new DocumentEditor(row.ToObject()); viewer.Watermark = OnGetWaterMark?.Invoke(row); //viewer.PrintAllowed = true; viewer.SaveAllowed = true; viewer.ShowDialog(); } else { var id = row.Get(x => x.DocumentLink.ID); var docrow = new Client().Query(new Filter(x => x.ID).IsEqualTo(id)).Rows.FirstOrDefault(); if (docrow != null) { var tmpfile = Path.ChangeExtension(Path.GetTempFileName(), Path.GetExtension(filename)); File.WriteAllBytes(tmpfile, docrow.Get(x => x.Data)); var gsProcessInfo = new ProcessStartInfo(); gsProcessInfo.Verb = "open"; gsProcessInfo.WindowStyle = ProcessWindowStyle.Normal; gsProcessInfo.FileName = tmpfile; gsProcessInfo.UseShellExecute = true; Process.Start(gsProcessInfo); } else { MessageBox.Show(string.Format("Unable to retrieve {0}!", filename)); } } //Document doc = new Client().Load(new Filter(x => x.ID).IsEqualTo(id)).FirstOrDefault(); //if (doc != null) //{ // if (System.IO.Path.GetExtension(doc.FileName).ToUpper().Equals(".PDF")) // { // PDFViewer viewer = new PDFViewer(doc); // viewer.ShowDialog(); // } // else // { // String filename = System.IO.Path.ChangeExtension(System.IO.Path.GetTempFileName(), System.IO.Path.GetExtension(doc.FileName)); // System.IO.File.WriteAllBytes(filename, doc.Data); // ProcessStartInfo gsProcessInfo = new ProcessStartInfo(); // gsProcessInfo.Verb = "open"; // gsProcessInfo.WindowStyle = ProcessWindowStyle.Normal; // gsProcessInfo.UseShellExecute = true; // gsProcessInfo.FileName = filename; // Process.Start(gsProcessInfo); // } //} //else // MessageBox.Show("Document does nto exist!"); return false; } public event OnGetWatermark OnGetWaterMark; protected override void DoAdd() { var dlg = new OpenFileDialog(); dlg.Multiselect = true; if (dlg.ShowDialog() == true) { using (new WaitCursor()) { var docs = new List(); foreach (var filename in dlg.FileNames) { // Create a Document var doc = new Document(); doc.FileName = Path.GetFileName(filename).ToLower(); doc.TimeStamp = new FileInfo(dlg.FileName).LastWriteTime; doc.Data = File.ReadAllBytes(filename); doc.CRC = CoreUtils.CalculateCRC(doc.Data); docs.Add(doc); } if (docs.Any()) { new Client().Save(docs, "Initial Upload"); foreach (var doc in docs) { var newitem = CreateItem(); var prop = (IEntityLink)otherproperty.GetValue(newitem); prop.ID = doc.ID; prop.Synchronise(doc); SaveItem(newitem); } } } Refresh(false, true); } } } }