IssuesGrid.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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.Closed = DateTime.Now;
  84. SaveItem(kanban);
  85. Refresh(false, true);
  86. }
  87. private Column<Kanban>[] AllowedColumns = [
  88. new(x => x.Number),
  89. new(x => x.Title),
  90. new(x => x.Description),
  91. new(x => x.Notes)];
  92. protected override void CustomiseEditor(Kanban[] items, DynamicGridColumn column, BaseEditor editor)
  93. {
  94. base.CustomiseEditor(items, column, editor);
  95. if(!AllowedColumns.Any(x => x.Property == column.ColumnName))
  96. {
  97. editor.Editable = editor.Editable.Combine(Editable.Hidden);
  98. }
  99. }
  100. public virtual CoreTable LookupValues(DataLookupEditor editor, Type parent, string columnname, BaseObject[]? items)
  101. {
  102. var client = ClientFactory(editor.Type);
  103. var filter = LookupFactory.DefineLookupFilter(parent, editor.Type, columnname, items ?? (Array.CreateInstance(parent, 0) as BaseObject[])!);
  104. var columns = LookupFactory.DefineLookupColumns(parent, editor.Type, columnname);
  105. foreach (var key in editor.OtherColumns.Keys)
  106. columns.Add(key);
  107. var sort = LookupFactory.DefineSort(editor.Type);
  108. var result = client.Query(filter, columns, sort);
  109. result.Columns.Add(new CoreColumn { ColumnName = "Display", DataType = typeof(string) });
  110. foreach (var row in result.Rows)
  111. {
  112. row["Display"] = LookupFactory.FormatLookup(parent, editor.Type, row, columnname);
  113. }
  114. return result;
  115. }
  116. protected override void DefineLookups(ILookupEditorControl sender, Kanban[] items, bool async = true)
  117. {
  118. if (sender.EditorDefinition is not DataLookupEditor editor)
  119. {
  120. base.DefineLookups(sender, items, async: async);
  121. return;
  122. }
  123. var colname = sender.ColumnName;
  124. if (async)
  125. {
  126. Task.Run(() =>
  127. {
  128. try
  129. {
  130. var values = LookupValues(editor, typeof(Kanban), colname, items);
  131. Dispatcher.Invoke(
  132. () =>
  133. {
  134. try
  135. {
  136. //Logger.Send(LogType.Information, typeof(T).Name, "Dispatching Results" + colname);
  137. sender.LoadLookups(values);
  138. }
  139. catch (Exception e2)
  140. {
  141. Logger.Send(LogType.Information, typeof(Kanban).Name,
  142. "Exception (2) in LoadLookups: " + e2.Message + "\n" + e2.StackTrace);
  143. }
  144. }
  145. );
  146. }
  147. catch (Exception e)
  148. {
  149. Logger.Send(LogType.Information, typeof(Kanban).Name,
  150. "Exception (1) in LoadLookups: " + e.Message + "\n" + e.StackTrace);
  151. }
  152. });
  153. }
  154. else
  155. {
  156. var values = LookupValues(editor, typeof(Kanban), colname, items);
  157. sender.LoadLookups(values);
  158. }
  159. }
  160. public override DynamicEditorPages LoadEditorPages(Kanban item)
  161. {
  162. var pages = new DynamicEditorPages
  163. {
  164. //new DynamicDocumentGrid<KanbanDocument, Kanban, KanbanLink>()
  165. };
  166. return pages;
  167. }
  168. protected override DynamicGridColumns LoadColumns()
  169. {
  170. var columns = new DynamicGridColumns<Kanban>();
  171. columns.Add(x => x.Number);
  172. columns.Add(x => x.Title);
  173. return columns;
  174. }
  175. #region Grid Stuff
  176. protected override string FormatRecordCount(int count)
  177. {
  178. return IsPaging
  179. ? $"{base.FormatRecordCount(count)} (loading..)"
  180. : base.FormatRecordCount(count);
  181. }
  182. protected override void Reload(
  183. Filters<Kanban> criteria, Columns<Kanban> columns, ref SortOrder<Kanban>? sort,
  184. CancellationToken token, Action<CoreTable?, Exception?> action)
  185. {
  186. criteria.Add(new Filter<Kanban>(x => x.Closed).IsEqualTo(Guid.Empty));
  187. criteria.Add(new Filter<Kanban>(CustomerProperty).IsEqualTo(CustomerID.ToString()));
  188. if(Options.PageSize > 0)
  189. {
  190. var inSort = sort;
  191. Task.Run(() =>
  192. {
  193. var page = CoreRange.Database(Options.PageSize);
  194. var filter = criteria.Combine();
  195. IsPaging = true;
  196. while (!token.IsCancellationRequested)
  197. {
  198. try
  199. {
  200. var data = Client.Query(filter, columns, inSort, page);
  201. data.Offset = page.Offset;
  202. IsPaging = data.Rows.Count == page.Limit;
  203. if (token.IsCancellationRequested)
  204. {
  205. break;
  206. }
  207. action(data, null);
  208. if (!IsPaging)
  209. break;
  210. // Proposal - Let's slow it down a bit to enhance UI responsiveness?
  211. Thread.Sleep(100);
  212. page.Next();
  213. }
  214. catch (Exception e)
  215. {
  216. action(null, e);
  217. break;
  218. }
  219. }
  220. }, token);
  221. }
  222. else
  223. {
  224. Client.Query(criteria.Combine(), columns, sort, null, action);
  225. }
  226. }
  227. public override Kanban[] LoadItems(IList<CoreRow> rows)
  228. {
  229. var results = new List<Kanban>(rows.Count);
  230. for (var i = 0; i < rows.Count; i += ChunkSize)
  231. {
  232. var chunk = rows.Skip(i).Take(ChunkSize);
  233. var filter = new Filter<Kanban>(x => x.ID).InList(chunk.Select(x => x.Get<Kanban, Guid>(x => x.ID)).ToArray());
  234. var columns = DynamicGridUtils.LoadEditorColumns(Columns.None<Kanban>());
  235. var data = Client.Query(filter, columns);
  236. results.AddRange(data.ToObjects<Kanban>());
  237. }
  238. return results.ToArray();
  239. }
  240. public override Kanban LoadItem(CoreRow row)
  241. {
  242. var id = row.Get<Kanban, Guid>(x => x.ID);
  243. return Client.Query(
  244. new Filter<Kanban>(x => x.ID).IsEqualTo(id),
  245. DynamicGridUtils.LoadEditorColumns(Columns.None<Kanban>())).ToObjects<Kanban>().FirstOrDefault()
  246. ?? throw new Exception($"No Kanban with ID {id}");
  247. }
  248. public override void SaveItem(Kanban item)
  249. {
  250. Client.Save(item, "Edited by User");
  251. }
  252. public override void SaveItems(IEnumerable<Kanban> items)
  253. {
  254. Client.Save(items, "Edited by User");
  255. }
  256. public override void DeleteItems(params CoreRow[] rows)
  257. {
  258. var deletes = new List<Kanban>();
  259. foreach (var row in rows)
  260. {
  261. var delete = new Kanban
  262. {
  263. ID = row.Get<Kanban, Guid>(x => x.ID)
  264. };
  265. deletes.Add(delete);
  266. }
  267. Client.Delete(deletes, "Deleted on User Request");
  268. }
  269. #endregion
  270. }