123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461 |
- using Comal.Classes;
- using InABox.Core;
- using InABox.DynamicGrid;
- using InABox.WPF;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Controls;
- using System.Windows.Media;
- using InABox.Wpf;
- namespace PRSDesktop.Forms.Issues;
- public class IssuesGrid : DynamicGrid<Kanban>, ISpecificGrid
- {
- private readonly int ChunkSize = 500;
- public IQueryProviderFactory ClientFactory { get; set; }
- private IQueryProvider<Kanban>? _kanbanClient;
- private IQueryProvider<Kanban> KanbanClient
- {
- get
- {
- _kanbanClient ??= ClientFactory.Create<Kanban>();
- return _kanbanClient;
- }
- }
-
- private IQueryProvider<Job>? _jobClient;
- private IQueryProvider<Job> JobClient
- {
- get
- {
- _jobClient ??= ClientFactory.Create<Job>();
- return _jobClient;
- }
- }
- public Guid CustomerID { get; set; }
- // public static CustomProperty CustomerProperty = new CustomProperty
- // {
- // Name = "CustomerID",
- // PropertyType = typeof(string),
- // ClassType = typeof(Kanban)
- // };
- public IssuesGrid() : base()
- {
- var cols = LookupFactory.DefineColumns<Kanban>();
- // Minimum Columns for Lookup values
- foreach (var col in cols)
- HiddenColumns.Add(col);
- HiddenColumns.Add(x => x.Notes);
- ActionColumns.Add(new DynamicMenuColumn(BuildMenu) { Position = DynamicActionColumnPosition.End });
- }
-
- private class UIComponent : DynamicGridGridUIComponent<Kanban>
- {
- private IssuesGrid Grid;
- public UIComponent(IssuesGrid grid)
- {
- Grid = grid;
- Parent = grid;
- }
- protected override Brush? GetCellBackground(CoreRow row, DynamicColumnBase column)
- {
- var status = row.Get<Kanban, KanbanStatus>(x => x.Status);
- var color = status == KanbanStatus.Open
- ? Colors.Orange
- : status == KanbanStatus.InProgress
- ? Colors.Plum
- : status == KanbanStatus.Waiting
- ? Colors.LightGreen
- : Colors.Silver;
- return color.ToBrush(0.5);
- }
- }
-
- protected override IDynamicGridUIComponent<Kanban> CreateUIComponent()
- {
- return new UIComponent(this);
- }
-
- protected override void Init()
- {
- AddButton("Check for Updates", PRSDesktop.Resources.autoupdate.AsBitmapImage(), CheckForUpdates);
- AddButton("Open Support Session", PRSDesktop.Resources.appicon.AsBitmapImage(), OpenSupportSession);
- }
- private bool OpenSupportSession(Button button, CoreRow[] rows)
- {
- SupportUtils.OpenSupportSession();
- return false;
- }
-
- private bool CheckForUpdates(Button button, CoreRow[] rows)
- {
- if (!SupportUtils.CheckForUpdates())
- {
- if (MessageWindow.ShowYesNo(
- "You appear to be using the latest version already!\n\nRun the installer anyway?", "Update"))
- {
- SupportUtils.DownloadAndRunInstaller();
- }
- }
- return false;
- }
- protected override void DoReconfigure(DynamicGridOptions options)
- {
- options.Clear();
- options.AddRows = true;
- options.EditRows = true;
- options.FilterRows = true;
- options.HideDatabaseFilters = true;
- }
- private void BuildMenu(DynamicMenuColumn column, CoreRow? row)
- {
- if (row is null) return;
- var menu = column.GetMenu();
- menu.AddItem("Add note", null, row, AddNote_Click);
- menu.AddItem("Attach system logs", null, row, AttachLogs_Click);
- menu.AddSeparator();
- menu.AddItem("Close issue", null, row, CloseTask_Click);
- }
- private void AttachLogs_Click(CoreRow row)
- {
- var logFile = CoreUtils.GetLogFile();
- var data = File.ReadAllBytes(logFile);
- var doc = new Document();
- doc.Data = data;
- doc.CRC = CoreUtils.CalculateCRC(data);
- doc.FileName = Path.GetFileName(logFile);
- doc.TimeStamp = File.GetLastWriteTime(logFile);
- ClientFactory.Save(doc, "Attached logs to task.");
- var kanbanDocument = new KanbanDocument();
- kanbanDocument.DocumentLink.CopyFrom(doc);
- kanbanDocument.EntityLink.CopyFrom(row.ToObject<Kanban>());
- ClientFactory.Save(kanbanDocument, "Attached logs to task.");
- }
- public override Kanban CreateItem()
- {
- var item = base.CreateItem();
- item.UserProperties["CustomerID"] = CustomerID.ToString();
- item.Notes = [
- $"Created on PRS {CoreUtils.GetVersion()} by {App.EmployeeName} ({App.EmployeeEmail})"
- ];
- // item.Status = KanbanStatus.Open;
- return item;
- }
- private void AddNote_Click(CoreRow row)
- {
- var kanban = row.ToObject<Kanban>();
- var text = "";
- if(TextBoxDialog.Execute("Enter note:", ref text))
- {
- text = string.Format("{0:yyyy-MM-dd HH:mm:ss}: {1}", DateTime.Now, text);
- kanban.Notes = kanban.Notes.Concatenate([text]);
- kanban.Status = KanbanStatus.InProgress;
- SaveItem(kanban);
- Refresh(false, true);
- }
- }
- private void CloseTask_Click(CoreRow row)
- {
- var kanban = row.ToObject<Kanban>();
- kanban.Completed = DateTime.Now;
- kanban.Closed = DateTime.Now;
- SaveItem(kanban);
- Refresh(false, true);
- }
- private Column<Kanban>[] AllowedColumns = [
- new(x => x.Number),
- new(x => x.Title),
- new(x => x.Description),
- new(x => x.Notes)];
- protected override void CustomiseEditor(Kanban[] items, DynamicGridColumn column, BaseEditor editor)
- {
- base.CustomiseEditor(items, column, editor);
- if(!AllowedColumns.Any(x => x.Property == column.ColumnName))
- {
- editor.Editable = editor.Editable.Combine(Editable.Hidden);
- }
- }
- public virtual CoreTable LookupValues(DataLookupEditor editor, Type parent, string columnname, BaseObject[]? items)
- {
- var client = ClientFactory.Create(editor.Type);
- var filter = LookupFactory.DefineLookupFilter(parent, editor.Type, columnname, items ?? (Array.CreateInstance(parent, 0) as BaseObject[])!);
- var columns = LookupFactory.DefineLookupColumns(parent, editor.Type, columnname);
- foreach (var key in editor.OtherColumns.Keys)
- columns.Add(key);
- var sort = LookupFactory.DefineSort(editor.Type);
- var result = client.Query(filter, columns, sort);
- result.Columns.Add(new CoreColumn { ColumnName = "Display", DataType = typeof(string) });
- foreach (var row in result.Rows)
- {
- row["Display"] = LookupFactory.FormatLookup(parent, editor.Type, row, columnname);
- }
- return result;
- }
- protected override void DefineLookups(ILookupEditorControl sender, Kanban[] items, bool async = true)
- {
- if (sender.EditorDefinition is not DataLookupEditor editor)
- {
- base.DefineLookups(sender, items, async: async);
- return;
- }
- var colname = sender.ColumnName;
- if (async)
- {
- Task.Run(() =>
- {
- try
- {
- var values = LookupValues(editor, typeof(Kanban), colname, items);
- Dispatcher.Invoke(
- () =>
- {
- try
- {
- //Logger.Send(LogType.Information, typeof(T).Name, "Dispatching Results" + colname);
- sender.LoadLookups(values);
- }
- catch (Exception e2)
- {
- Logger.Send(LogType.Information, typeof(Kanban).Name,
- "Exception (2) in LoadLookups: " + e2.Message + "\n" + e2.StackTrace);
- }
- }
- );
- }
- catch (Exception e)
- {
- Logger.Send(LogType.Information, typeof(Kanban).Name,
- "Exception (1) in LoadLookups: " + e.Message + "\n" + e.StackTrace);
- }
- });
- }
- else
- {
- var values = LookupValues(editor, typeof(Kanban), colname, items);
- sender.LoadLookups(values);
- }
- }
- public override DynamicEditorPages LoadEditorPages(Kanban item)
- {
- var pages = new DynamicEditorPages
- {
- new DynamicDocumentGrid<KanbanDocument, Kanban, KanbanLink>
- {
- Client = ClientFactory
- }
- };
- return pages;
- }
- protected override DynamicGridColumns LoadColumns()
- {
- var columns = new DynamicGridColumns<Kanban>();
- columns.Add(x => x.Number, caption: "Ticket", width: 60, alignment: Alignment.MiddleCenter);
- columns.Add(x => x.Title);
- columns.Add(x => x.CreatedBy, caption: "Created By", width: 150);
- columns.Add(x => x.EmployeeLink.Name, caption: "Assigned To", width: 150);
- columns.Add(x => x.Type.Description, caption: "Type", width: 100, alignment: Alignment.MiddleCenter);
- columns.Add(x => x.Status, caption: "Status", width: 80, alignment: Alignment.MiddleCenter);
- return columns;
- }
- #region Grid Stuff
- protected override string FormatRecordCount(int count)
- {
- return IsPaging
- ? $"{base.FormatRecordCount(count)} (loading..)"
- : base.FormatRecordCount(count);
- }
- protected override void Reload(
- Filters<Kanban> criteria, Columns<Kanban> columns, ref SortOrder<Kanban>? sort,
- CancellationToken token, Action<CoreTable?, Exception?> action)
- {
- criteria.Add(new Filter<Kanban>(x => x.Closed).IsEqualTo(Guid.Empty));
- criteria.Add(new Filter<Kanban>(x => x.Status).IsNotEqualTo(KanbanStatus.Complete));
- criteria.Add(new Filter<Kanban>(x => x.JobLink.Customer.ID).IsEqualTo(CustomerID));
-
- //criteria.Add(new Filter<Kanban>(CustomerProperty).IsEqualTo(CustomerID.ToString()));
-
- if(Options.PageSize > 0)
- {
- var inSort = sort;
- Task.Run(() =>
- {
-
- var page = CoreRange.Database(Options.PageSize);
- var filter = criteria.Combine();
-
- IsPaging = true;
- while (!token.IsCancellationRequested)
- {
- try
- {
- var data = KanbanClient.Query(filter, columns, inSort, page);
- data.Offset = page.Offset;
- IsPaging = data.Rows.Count == page.Limit;
- if (token.IsCancellationRequested)
- {
- break;
- }
- action(data, null);
- if (!IsPaging)
- break;
-
- // Proposal - Let's slow it down a bit to enhance UI responsiveness?
- Thread.Sleep(100);
-
- page.Next();
- }
- catch (Exception e)
- {
- action(null, e);
- break;
- }
- }
- }, token);
- }
- else
- {
- KanbanClient.Query(criteria.Combine(), columns, sort, null, action);
- }
- }
- public override Kanban[] LoadItems(IList<CoreRow> rows)
- {
- var results = new List<Kanban>(rows.Count);
- for (var i = 0; i < rows.Count; i += ChunkSize)
- {
- var chunk = rows.Skip(i).Take(ChunkSize);
- var filter = new Filter<Kanban>(x => x.ID).InList(chunk.Select(x => x.Get<Kanban, Guid>(x => x.ID)).ToArray());
- var columns = DynamicGridUtils.LoadEditorColumns(Columns.None<Kanban>());
- var data = KanbanClient.Query(filter, columns);
- results.AddRange(data.ToObjects<Kanban>());
- }
- return results.ToArray();
- }
- public override Kanban LoadItem(CoreRow row)
- {
- var id = row.Get<Kanban, Guid>(x => x.ID);
- return KanbanClient.Query(
- new Filter<Kanban>(x => x.ID).IsEqualTo(id),
- DynamicGridUtils.LoadEditorColumns(Columns.None<Kanban>())).ToObjects<Kanban>().FirstOrDefault()
- ?? throw new Exception($"No Kanban with ID {id}");
- }
- public override void SaveItem(Kanban item)
- {
- CheckJob(item);
- KanbanClient.Save(item, "Edited by User");
- }
- private void CheckJob(Kanban item)
- {
- if (item.ID == Guid.Empty)
- {
- item.CreatedBy = App.EmployeeName;
-
- // Check if there is an open Project Job (ie installation or periodic billing) for this Client
- var job = JobClient.Query(
- new Filter<Job>(x => x.Customer.ID).IsEqualTo(CustomerID)
- .And(x => x.JobType).IsEqualTo(JobType.Project)
- .And(x => x.JobStatus.Active).IsEqualTo(true),
- Columns.None<Job>()
- .Add(x => x.ID)
- .Add(x=>x.DefaultScope.ID)
- ).ToObjects<Job>().FirstOrDefault();
-
- // No Job ? Create a service job for this ticket
- if (job == null)
- {
- job = new Job();
- job.Name = item.Title;
- job.Customer.ID = CustomerID;
- job.JobType = JobType.Service;
- job.Notes = item.Notes?.ToList().ToArray() ?? [];
- job.UserProperties.Clear();
- JobClient.Save(job, "Created by Client Issues Screen");
- }
- // Created Tickets should always have a job #!
- item.JobLink.ID = job.ID;
- item.JobScope.ID = job.DefaultScope.ID;
- }
- }
- public override void SaveItems(IEnumerable<Kanban> items)
- {
- var list = items.ToArray();
- foreach (var item in list)
- CheckJob(item);
- KanbanClient.Save(list, "Edited by User");
- }
- public override void DeleteItems(params CoreRow[] rows)
- {
- var deletes = new List<Kanban>();
- foreach (var row in rows)
- {
- var delete = new Kanban
- {
- ID = row.Get<Kanban, Guid>(x => x.ID)
- };
- deletes.Add(delete);
- }
- KanbanClient.Delete(deletes, "Deleted on User Request");
- }
- #endregion
- }
|