IssuesGrid.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using InABox.Configuration;
  4. using InABox.Core;
  5. using InABox.DynamicGrid;
  6. using InABox.Wpf.Editors;
  7. using InABox.WPF;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Diagnostics.CodeAnalysis;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. namespace PRSDesktop.Forms.Issues;
  16. public class IssuesGrid : DynamicGrid<Kanban>, ISpecificGrid
  17. {
  18. private readonly int ChunkSize = 500;
  19. private IClient<Kanban> _client;
  20. public Func<Type, IClient> ClientFactory { get; set; }
  21. private IClient<Kanban> Client
  22. {
  23. get
  24. {
  25. _client ??= (ClientFactory(typeof(Kanban)) as IClient<Kanban>)!;
  26. return _client;
  27. }
  28. }
  29. public Guid CustomerID { get; set; }
  30. public static CustomProperty CustomerProperty = new CustomProperty
  31. {
  32. Name = "CustomerID",
  33. PropertyType = typeof(string),
  34. ClassType = typeof(Kanban)
  35. };
  36. public IssuesGrid() : base()
  37. {
  38. var cols = LookupFactory.DefineColumns<Kanban>();
  39. // Minimum Columns for Lookup values
  40. foreach (var col in cols)
  41. HiddenColumns.Add(col);
  42. HiddenColumns.Add(x => x.Notes);
  43. ActionColumns.Add(new DynamicMenuColumn(BuildMenu) { Position = DynamicActionColumnPosition.End });
  44. }
  45. protected override void Init()
  46. {
  47. }
  48. protected override void DoReconfigure(DynamicGridOptions options)
  49. {
  50. options.Clear();
  51. options.AddRows = true;
  52. options.EditRows = true;
  53. }
  54. private void BuildMenu(DynamicMenuColumn column, CoreRow? row)
  55. {
  56. if (row is null) return;
  57. var menu = column.GetMenu();
  58. menu.AddItem("Add Note", null, row, AddNote_Click);
  59. menu.AddItem("Close Issue", null, row, CloseTask_Click);
  60. }
  61. public override Kanban CreateItem()
  62. {
  63. var item = base.CreateItem();
  64. item.UserProperties["CustomerID"] = CustomerID.ToString();
  65. // item.Status = KanbanStatus.Open;
  66. return item;
  67. }
  68. private void AddNote_Click(CoreRow row)
  69. {
  70. var kanban = row.ToObject<Kanban>();
  71. var text = "";
  72. if(TextBoxDialog.Execute("Enter note:", ref text))
  73. {
  74. text = string.Format("{0:yyyy-MM-dd HH:mm:ss}: {1}", DateTime.Now, text);
  75. kanban.Notes = kanban.Notes.Concatenate([text]);
  76. SaveItem(kanban);
  77. Refresh(false, true);
  78. }
  79. }
  80. private void CloseTask_Click(CoreRow row)
  81. {
  82. var kanban = row.ToObject<Kanban>();
  83. kanban.Completed = DateTime.Now;
  84. kanban.Closed = DateTime.Now;
  85. SaveItem(kanban);
  86. Refresh(false, true);
  87. }
  88. private Column<Kanban>[] AllowedColumns = [
  89. new(x => x.Number),
  90. new(x => x.Title),
  91. new(x => x.Description),
  92. new(x => x.Notes)];
  93. protected override void CustomiseEditor(Kanban[] items, DynamicGridColumn column, BaseEditor editor)
  94. {
  95. base.CustomiseEditor(items, column, editor);
  96. if(!AllowedColumns.Any(x => x.Property == column.ColumnName))
  97. {
  98. editor.Editable = editor.Editable.Combine(Editable.Hidden);
  99. }
  100. }
  101. public virtual CoreTable LookupValues(DataLookupEditor editor, Type parent, string columnname, BaseObject[]? items)
  102. {
  103. var client = ClientFactory(editor.Type);
  104. var filter = LookupFactory.DefineLookupFilter(parent, editor.Type, columnname, items ?? (Array.CreateInstance(parent, 0) as BaseObject[])!);
  105. var columns = LookupFactory.DefineLookupColumns(parent, editor.Type, columnname);
  106. foreach (var key in editor.OtherColumns.Keys)
  107. columns.Add(key);
  108. var sort = LookupFactory.DefineSort(editor.Type);
  109. var result = client.Query(filter, columns, sort);
  110. result.Columns.Add(new CoreColumn { ColumnName = "Display", DataType = typeof(string) });
  111. foreach (var row in result.Rows)
  112. {
  113. row["Display"] = LookupFactory.FormatLookup(parent, editor.Type, row, columnname);
  114. }
  115. return result;
  116. }
  117. protected override void DefineLookups(ILookupEditorControl sender, Kanban[] items, bool async = true)
  118. {
  119. if (sender.EditorDefinition is not DataLookupEditor editor)
  120. {
  121. base.DefineLookups(sender, items, async: async);
  122. return;
  123. }
  124. var colname = sender.ColumnName;
  125. if (async)
  126. {
  127. Task.Run(() =>
  128. {
  129. try
  130. {
  131. var values = LookupValues(editor, typeof(Kanban), colname, items);
  132. Dispatcher.Invoke(
  133. () =>
  134. {
  135. try
  136. {
  137. //Logger.Send(LogType.Information, typeof(T).Name, "Dispatching Results" + colname);
  138. sender.LoadLookups(values);
  139. }
  140. catch (Exception e2)
  141. {
  142. Logger.Send(LogType.Information, typeof(Kanban).Name,
  143. "Exception (2) in LoadLookups: " + e2.Message + "\n" + e2.StackTrace);
  144. }
  145. }
  146. );
  147. }
  148. catch (Exception e)
  149. {
  150. Logger.Send(LogType.Information, typeof(Kanban).Name,
  151. "Exception (1) in LoadLookups: " + e.Message + "\n" + e.StackTrace);
  152. }
  153. });
  154. }
  155. else
  156. {
  157. var values = LookupValues(editor, typeof(Kanban), colname, items);
  158. sender.LoadLookups(values);
  159. }
  160. }
  161. public override DynamicEditorPages LoadEditorPages(Kanban item)
  162. {
  163. var pages = new DynamicEditorPages
  164. {
  165. //new DynamicDocumentGrid<KanbanDocument, Kanban, KanbanLink>()
  166. };
  167. return pages;
  168. }
  169. protected override DynamicGridColumns LoadColumns()
  170. {
  171. var columns = new DynamicGridColumns<Kanban>();
  172. columns.Add(x => x.Number);
  173. columns.Add(x => x.Title);
  174. return columns;
  175. }
  176. #region Grid Stuff
  177. protected override string FormatRecordCount(int count)
  178. {
  179. return IsPaging
  180. ? $"{base.FormatRecordCount(count)} (loading..)"
  181. : base.FormatRecordCount(count);
  182. }
  183. protected override void Reload(
  184. Filters<Kanban> criteria, Columns<Kanban> columns, ref SortOrder<Kanban>? sort,
  185. CancellationToken token, Action<CoreTable?, Exception?> action)
  186. {
  187. criteria.Add(new Filter<Kanban>(x => x.Closed).IsEqualTo(Guid.Empty));
  188. criteria.Add(new Filter<Kanban>(CustomerProperty).IsEqualTo(CustomerID.ToString()));
  189. if(Options.PageSize > 0)
  190. {
  191. var inSort = sort;
  192. Task.Run(() =>
  193. {
  194. var page = CoreRange.Database(Options.PageSize);
  195. var filter = criteria.Combine();
  196. IsPaging = true;
  197. while (!token.IsCancellationRequested)
  198. {
  199. try
  200. {
  201. var data = Client.Query(filter, columns, inSort, page);
  202. data.Offset = page.Offset;
  203. IsPaging = data.Rows.Count == page.Limit;
  204. if (token.IsCancellationRequested)
  205. {
  206. break;
  207. }
  208. action(data, null);
  209. if (!IsPaging)
  210. break;
  211. // Proposal - Let's slow it down a bit to enhance UI responsiveness?
  212. Thread.Sleep(100);
  213. page.Next();
  214. }
  215. catch (Exception e)
  216. {
  217. action(null, e);
  218. break;
  219. }
  220. }
  221. }, token);
  222. }
  223. else
  224. {
  225. Client.Query(criteria.Combine(), columns, sort, null, action);
  226. }
  227. }
  228. public override Kanban[] LoadItems(IList<CoreRow> rows)
  229. {
  230. var results = new List<Kanban>(rows.Count);
  231. for (var i = 0; i < rows.Count; i += ChunkSize)
  232. {
  233. var chunk = rows.Skip(i).Take(ChunkSize);
  234. var filter = new Filter<Kanban>(x => x.ID).InList(chunk.Select(x => x.Get<Kanban, Guid>(x => x.ID)).ToArray());
  235. var columns = DynamicGridUtils.LoadEditorColumns(Columns.None<Kanban>());
  236. var data = Client.Query(filter, columns);
  237. results.AddRange(data.ToObjects<Kanban>());
  238. }
  239. return results.ToArray();
  240. }
  241. public override Kanban LoadItem(CoreRow row)
  242. {
  243. var id = row.Get<Kanban, Guid>(x => x.ID);
  244. return Client.Query(
  245. new Filter<Kanban>(x => x.ID).IsEqualTo(id),
  246. DynamicGridUtils.LoadEditorColumns(Columns.None<Kanban>())).ToObjects<Kanban>().FirstOrDefault()
  247. ?? throw new Exception($"No Kanban with ID {id}");
  248. }
  249. public override void SaveItem(Kanban item)
  250. {
  251. Client.Save(item, "Edited by User");
  252. }
  253. public override void SaveItems(IEnumerable<Kanban> items)
  254. {
  255. Client.Save(items, "Edited by User");
  256. }
  257. public override void DeleteItems(params CoreRow[] rows)
  258. {
  259. var deletes = new List<Kanban>();
  260. foreach (var row in rows)
  261. {
  262. var delete = new Kanban
  263. {
  264. ID = row.Get<Kanban, Guid>(x => x.ID)
  265. };
  266. deletes.Add(delete);
  267. }
  268. Client.Delete(deletes, "Deleted on User Request");
  269. }
  270. #endregion
  271. }