using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Media.Imaging; using Comal.Classes; using InABox.Clients; using InABox.Core; using InABox.DynamicGrid; using InABox.WPF; namespace PRSDesktop { public class EquipmentGrid : DynamicDataGrid { private readonly BitmapImage docs = PRSDesktop.Resources.doc_misc.AsBitmapImage(); private readonly BitmapImage specification = PRSDesktop.Resources.doc_pdf.AsBitmapImage(); public EquipmentGrid() { Options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.SelectColumns, DynamicGridOption.FilterRows); ActionColumns.Add(new DynamicActionColumn(SpecificationImage, ShowSpecificationSheet) { Position = DynamicActionColumnPosition.Start }); //HiddenColumns.Add(x => x.Specification.FileName); HiddenColumns.Add(x => x.SpecificationSheet.ID); ActionColumns.Add(new DynamicScheduleEditorColumn()); HiddenColumns.Add(x => x.ActiveSchedules); ActionColumns.Add(new DynamicActionColumn(DocumentsImage, DocumentsClick)); HiddenColumns.Add(x => x.Documents); ActionColumns.Add(new DynamicMapColumn(this, x => x.TrackerLink.Location)); AddButton("Duplicate", PRSDesktop.Resources.copy.AsBitmapImage(), DuplicateEquipment); } private bool DocumentsClick(CoreRow arg) { if (arg == null) return false; var docs = new List(); using (new WaitCursor()) { var deliveryid = arg.Get(x => x.ID); var table = new Client().Query( new Filter(x => x.EntityLink.ID).IsEqualTo(deliveryid) ); foreach (var row in table.Rows) docs.Add(row.ToObject()); } if (docs.Any()) { var editor = new DocumentEditor(docs.ToArray()); editor.PrintAllowed = true; editor.SaveAllowed = true; editor.ShowDialog(); } else { MessageBox.Show("No Documents Available!"); } return false; } private BitmapImage DocumentsImage(CoreRow arg) { if (arg == null) return docs; return arg.Get(x => x.Documents) > 0 ? docs : null; } private bool DuplicateEquipment(Button sender, CoreRow[] rows) { if (rows.Length != 1) { MessageBox.Show("Please select one equipment item to duplicate!"); return false; } var row = rows.First(); var res = MessageBox.Show("Do you wish to copy the attached documents along with this item?", "Copy Documents", MessageBoxButton.YesNoCancel); if (res == MessageBoxResult.Cancel) return false; Progress.Show(""); var equipment = new Client().Load(new Filter(x => x.ID).IsEqualTo(row.Get(x => x.ID))) .FirstOrDefault(); Progress.SetMessage("Duplicating Equipment"); var id = equipment.ID; equipment.ID = Guid.Empty; equipment.Code = equipment.Code + " - COPY"; equipment.ActiveSchedules = 0; equipment.CommitChanges(); new Client().Save(equipment, "Equipment Item Duplicated"); Progress.SetMessage("Duplicating Schedules"); var schedules = new Client().Load(new Filter(x => x.DocumentID).IsEqualTo(id)); foreach (var schedule in schedules) { schedule.ID = Guid.Empty; schedule.DocumentID = equipment.ID; schedule.CommitChanges(); } new Client().Save(schedules, "Equipment Item Duplicated"); if (res == MessageBoxResult.Yes) { Progress.SetMessage("Duplicating Document References"); var docs = new Client().Load(new Filter(x => x.EntityLink.ID).IsEqualTo(id)); foreach (var doc in docs) { doc.ID = Guid.Empty; doc.EntityLink.ID = equipment.ID; doc.CommitChanges(); } new Client().Save(docs, "Equipment Item Duplicated"); } Progress.Close(); MessageBox.Show(string.Format("{0} created!", equipment.Code)); return true; } protected override void Reload(Filters criteria, Columns columns, ref SortOrder sort, Action action) { sort = new SortOrder(x => x.Code); base.Reload(criteria, columns, ref sort, action); } private bool ShowSpecificationSheet(CoreRow arg) { var id = Entity.EntityLinkID(x => x.SpecificationSheet, arg); //String file = arg.Get(x => x.Specification.FileName); if (id != null) return false; var doc = new Client().Load(new Filter(x => x.ID).IsEqualTo(id)).FirstOrDefault(); if (doc == null) { MessageBox.Show("Unable to Load Document"); return false; } var ext = Path.GetExtension(doc.FileName); var tmpfile = Path.ChangeExtension(Path.GetTempFileName(), ext); File.WriteAllBytes(tmpfile, doc.Data); ProcessStartInfo gsProcessInfo = new ProcessStartInfo(); gsProcessInfo.Verb = "open"; gsProcessInfo.WindowStyle = ProcessWindowStyle.Normal; gsProcessInfo.FileName = tmpfile; gsProcessInfo.UseShellExecute = true; Process.Start(gsProcessInfo); return false; } private BitmapImage? SpecificationImage(CoreRow arg) { if (arg == null) return specification; var id = Entity.EntityLinkID(x => x.SpecificationSheet, arg); return id == null ? null : specification; } } }