using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Windows; using System.Windows.Controls; using Comal.Classes; using InABox.Core; using InABox.Clients; using InABox.DynamicGrid; using Microsoft.Win32; using SaveFileDialog = Microsoft.Win32.SaveFileDialog; namespace PRSDesktop; public class QualificationManyToManyGrid : DynamicManyToManyGrid where T : Entity, new() { public QualificationManyToManyGrid() { HiddenColumns.Add(x => x.Qualification.Renewal); HiddenColumns.Add(x => x.Expiry); } protected override void DoReconfigure(DynamicGridOptions options) { base.DoReconfigure(options); options.RecordCount = true; options.SelectColumns = true; options.MultiSelect = true; } protected override void DoReconfigureEditors(DynamicEditorGrid grid, EmployeeQualification[] items) { base.DoReconfigureEditors(grid, items); var renewal = grid.GetPropertyValue("Qualification.Renewal"); var expiry = grid.FindEditor("Expiry"); expiry.SetEnabled(true);// renewal.Equals(QualificationRenewal.Manual)); } protected override Dictionary EditorValueChanged(IDynamicEditorForm editor, EmployeeQualification[] items, string name, object value) { var result = base.EditorValueChanged(editor, items, name, value); if (name.Equals("Qualification.Renewal")) foreach (var item in items) { var expiry = item.CalculateExpiry(item.Qualified, (QualificationRenewal)value, item.Qualification.Period, item.Expiry); if (expiry != item.Expiry) { item.Expiry = expiry; result["Expiry"] = expiry; } } return result; } } public class QualificationEmployeeGrid : QualificationManyToManyGrid { } public class EmployeeQualificationGrid : QualificationManyToManyGrid { private readonly Button LoadQualificationTemplate; private readonly Button SaveQualificationTemplate; public EmployeeQualificationGrid() { LoadQualificationTemplate = AddButton("Load Template", null, "Load Template", LoadQualificationTemplate_Click); LoadQualificationTemplate.Visibility = Visibility.Collapsed; SaveQualificationTemplate = AddButton("Save Template", null, "Save Template", SaveQualificationTemplate_Click); SaveQualificationTemplate.Visibility = Visibility.Collapsed; HiddenColumns.Add(x => x.Qualification.ID); } private bool LoadQualificationTemplate_Click(Button sender, CoreRow[] selectedRows) { var dialog = new MultiSelectDialog(null, null, false); if (dialog.ShowDialog()) { var templateID = dialog.IDs().First(); var qualificationIds = InABox.Clients.Client.Query( new Filter(x => x.Template.ID).IsEqualTo(templateID), Columns.None().Add(x => x.Qualification.ID)) .ExtractValues(x => x.Qualification.ID); var qualifications = new Client().Load(new Filter(x => x.ID).InList(qualificationIds)); var guids = CurrentGuids(); var newItems = new List(); foreach (var qualification in qualifications) { if (!guids.Contains(qualification.ID)) { var newQualification = CreateItem(); newQualification.Qualification.ID = qualification.ID; newQualification.Qualification.Synchronise(qualification); newItems.Add(newQualification); } } SaveItems(newItems.ToArray()); return true; } return false; /*var dlg = new OpenFileDialog(); dlg.Filter = "JSON files (*.json)|*.json|All files (*.*)|*.*"; if(dlg.ShowDialog() == true) { Guid[] list; using (var stream = dlg.OpenFile()) { list = Serialization.Deserialize(stream); } var qualifications = new Client().Load( new Filter(x => x.ID).InList(list)); var guids = CurrentGuids(); var newItems = new List(); foreach (var qualification in qualifications) { if (!guids.Contains(qualification.ID)) { var newQualification = CreateItem(); newQualification.Qualification.ID = qualification.ID; newQualification.Qualification.Synchronise(qualification); newItems.Add(newQualification); } } SaveItems(newItems.ToArray()); return true; } return false;*/ } private bool SaveQualificationTemplate_Click(Button sender, CoreRow[] selectedRows) { var dialog = new MultiSelectDialog(null, null, false); if (dialog.ShowDialog()) { var templateID = dialog.IDs().First(); var templateItems = Client.Query( new Filter(x => x.Template.ID).IsEqualTo(templateID), Columns.None().Add(x => x.ID)).Rows.Select(x => x.ToObject()).ToArray(); InABox.Clients.Client.Delete(templateItems, "Deleted because re-save of template"); List newItems = new(); foreach(var item in Data.Rows) { var newItem = new EmployeeQualificationTemplateItem(); newItem.Template.ID = templateID; newItem.Qualification.ID = item.Get(x => x.Qualification.ID); newItems.Add(newItem); } InABox.Clients.Client.Save(newItems, "Save to template"); } return false; /* var list = new List(); foreach(var item in Data.Rows) { var qualification = item.ToObject(); list.Add(qualification.Qualification.ID); } var dlg = new SaveFileDialog(); dlg.Filter = "JSON files (*.json)|*.json|All files (*.*)|*.*"; dlg.AddExtension = true; if(dlg.ShowDialog() == true) { using(var stream = dlg.OpenFile()) { Serialization.Serialize(list, stream); } } return false; */ } protected override void DoAdd(bool openEditorOnDirectEdit = false) { var filter = GetFilter() as Filter; var dlg = new MultiSelectDialog(filter, null, true); if (dlg.ShowDialog()) { var guids = CurrentGuids(); var items = dlg.Items(null); var single = items.Length == 1; if (single) { var item = items.First(); var newItem = CreateItem(); newItem.Qualification.ID = item.ID; newItem.Qualification.Synchronise(item); if (EditItems(new EmployeeQualification[] { newItem })) { Refresh(false, true); } } else { foreach (var item in items) { if (!guids.Contains(item.ID)) { var newItem = CreateItem(); newItem.Qualification.ID = item.ID; newItem.Qualification.Synchronise(item); SaveItem(newItem); } } Refresh(false, true); } } } }