PostUtils.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using InABox.Core.Postable;
  5. using InABox.DynamicGrid;
  6. using InABox.Wpf;
  7. using InABox.WPF;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Drawing;
  11. using System.Globalization;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows;
  16. using System.Windows.Media.Imaging;
  17. namespace PRSDesktop;
  18. public static class PostUtils
  19. {
  20. private static readonly Inflector.Inflector inflector = new(new CultureInfo("en"));
  21. public static void PostEntities<T>(IDataModel<T> model, Action refresh, Action? configurePost = null)
  22. where T : Entity, IPostable, IRemotable, IPersistent, new()
  23. {
  24. try
  25. {
  26. var result = PosterUtils.Process(model);
  27. if(result is null)
  28. {
  29. MessageWindow.ShowMessage($"Processing failed", "Processing failed");
  30. refresh();
  31. }
  32. else
  33. {
  34. var failedMessages = new List<string>();
  35. var successCount = 0;
  36. foreach(var entity in result.PostedEntities)
  37. {
  38. if(entity.PostedStatus == PostedStatus.PostFailed)
  39. {
  40. failedMessages.Add(entity.PostedNote);
  41. }
  42. else
  43. {
  44. successCount++;
  45. }
  46. }
  47. if(successCount == 0)
  48. {
  49. MessageWindow.ShowMessage($"Processing failed:\n - {string.Join("\n - ", failedMessages)}", "Processing failed.");
  50. }
  51. else if(failedMessages.Count == 0)
  52. {
  53. MessageWindow.ShowMessage($"Processing successful; {successCount} items processed", "Processing successful.");
  54. }
  55. else
  56. {
  57. MessageWindow.ShowMessage($"{successCount} items succeeded, but {failedMessages.Count} failed:\n - {string.Join("\n - ", failedMessages)}", "Partial success");
  58. }
  59. refresh();
  60. }
  61. }
  62. catch (EmptyPostException)
  63. {
  64. MessageWindow.ShowMessage($"Please select at least one {typeof(T).Name}.", "Select items");
  65. }
  66. catch (PostFailedMessageException e)
  67. {
  68. MessageWindow.ShowMessage(e.Message, "Post failed");
  69. }
  70. catch (RepostedException)
  71. {
  72. MessageWindow.ShowMessage("At least one of the items you selected has already been processed. Processing cancelled.", "Already processed");
  73. }
  74. catch (PostCancelledException)
  75. {
  76. MessageWindow.ShowMessage("Processing cancelled.", "Cancelled");
  77. }
  78. catch (MissingSettingsException)
  79. {
  80. if (configurePost is not null && Security.CanConfigurePost<T>())
  81. {
  82. if (MessageWindow.ShowYesNo($"Processing has not been configured for {inflector.Pluralize(typeof(T).Name)}. Would you like to configure this now?",
  83. "Configure Processing?"))
  84. {
  85. configurePost();
  86. }
  87. else
  88. {
  89. MessageWindow.ShowMessage("Processing cancelled.", "Cancelled");
  90. }
  91. }
  92. else
  93. {
  94. MessageWindow.ShowMessage($"Processing has not been configured for {inflector.Pluralize(typeof(T).Name)}!", "Unconfigured");
  95. }
  96. }
  97. catch (Exception e)
  98. {
  99. MessageWindow.ShowError("Processing failed.", e);
  100. refresh();
  101. }
  102. }
  103. public static void CreateToolbarButtons<T>(IPanelHost host, Func<IDataModel<T>> model, Action refresh, Action? configurePost = null)
  104. where T : Entity, IPostable, IRemotable, IPersistent, new()
  105. {
  106. var postSettings = PosterUtils.LoadPostableSettings<T>();
  107. if (Security.CanPost<T>() && !postSettings.PosterType.IsNullOrWhiteSpace())
  108. {
  109. Bitmap? image = null;
  110. if (postSettings.Thumbnail.ID != Guid.Empty)
  111. {
  112. var icon = new Client<Document>()
  113. .Load(new Filter<Document>(x => x.ID).IsEqualTo(postSettings.Thumbnail.ID)).FirstOrDefault();
  114. if (icon?.Data?.Any() == true)
  115. image = new ImageConverter().ConvertFrom(icon.Data) as Bitmap;
  116. }
  117. host.CreatePanelAction(new PanelAction
  118. {
  119. Caption = postSettings.ButtonName.NotWhiteSpaceOr($"Process {inflector.Pluralize(typeof(T).Name)}"),
  120. Image = image ?? PRSDesktop.Resources.edit,
  121. OnExecute = action =>
  122. {
  123. PostEntities(
  124. model(),
  125. refresh,
  126. configurePost);
  127. }
  128. });
  129. if (postSettings.ShowClearButton)
  130. {
  131. host.CreatePanelAction(new PanelAction
  132. {
  133. Caption = "Clear Posted Flag",
  134. Image = image ?? PRSDesktop.Resources.refresh,
  135. OnExecute = action =>
  136. {
  137. var dataModel = model();
  138. foreach(var (key, table) in dataModel.ModelTables)
  139. {
  140. table.IsDefault = false;
  141. }
  142. dataModel.SetColumns<T>(Columns.Required<T>().Add(x => x.PostedStatus).Add(x => x.PostedReference).Add(x => x.PostedNote).Add(x => x.Posted));
  143. dataModel.SetIsDefault<T>(true);
  144. dataModel.LoadModel();
  145. var items = dataModel.GetTable<T>().ToArray<T>();
  146. foreach(var item in items)
  147. {
  148. item.PostedStatus = PostedStatus.NeverPosted;
  149. item.PostedReference = "";
  150. item.PostedNote = "";
  151. item.Posted = DateTime.MinValue;
  152. }
  153. Client.Save(items, "Cleared posted flag");
  154. refresh();
  155. }
  156. });
  157. }
  158. }
  159. if (configurePost is not null && Security.CanConfigurePost<T>())
  160. {
  161. host.CreateSetupAction(new PanelAction
  162. {
  163. Caption = $"Configure {CoreUtils.Neatify(typeof(T).Name)} Processing",
  164. OnExecute = action =>
  165. {
  166. configurePost();
  167. }
  168. });
  169. }
  170. }
  171. public static void ConfigurePost<T>()
  172. where T : Entity, IPostable, IRemotable, IPersistent, new()
  173. {
  174. var postSettings = PosterUtils.LoadPostableSettings<T>();
  175. var grid = (DynamicGridUtils.CreateDynamicGrid(typeof(DynamicGrid<>), typeof(PostableSettings)) as DynamicGrid<PostableSettings>)!;
  176. if (grid.EditItems(new PostableSettings[] { postSettings }))
  177. {
  178. PosterUtils.SavePostableSettings<T>(postSettings);
  179. }
  180. }
  181. public static void CreateToolbarButtons<T>(IPanelHost host, Func<IDataModel<T>> model, Action refresh, bool allowConfig)
  182. where T : Entity, IPostable, IRemotable, IPersistent, new()
  183. {
  184. CreateToolbarButtons(host, model, refresh, allowConfig ? ConfigurePost<T> : null);
  185. }
  186. #region PostColumn
  187. private static readonly BitmapImage? post = PRSDesktop.Resources.post.AsBitmapImage();
  188. private static readonly BitmapImage? tick = PRSDesktop.Resources.tick.AsBitmapImage();
  189. private static readonly BitmapImage? warning = PRSDesktop.Resources.warning.AsBitmapImage();
  190. private static readonly BitmapImage? refresh = PRSDesktop.Resources.refresh.AsBitmapImage();
  191. public static void AddPostColumn<T>(DynamicGrid<T> grid)
  192. where T : Entity, IPostable, IRemotable, IPersistent, new()
  193. {
  194. grid.HiddenColumns.Add(x => x.PostedStatus);
  195. grid.HiddenColumns.Add(x => x.PostedNote);
  196. grid.ActionColumns.Add(new DynamicImageColumn(
  197. row =>
  198. {
  199. if (row is null)
  200. return post;
  201. return row.Get<T, PostedStatus>(x => x.PostedStatus) switch
  202. {
  203. PostedStatus.PostFailed => warning,
  204. PostedStatus.Posted => tick,
  205. PostedStatus.RequiresRepost => refresh,
  206. PostedStatus.NeverPosted or _ => null,
  207. };
  208. },
  209. null)
  210. {
  211. ToolTip = (column, row) =>
  212. {
  213. if (row is null)
  214. {
  215. return column.TextToolTip($"{CoreUtils.Neatify(typeof(T).Name)} Processed Status");
  216. }
  217. return column.TextToolTip(row.Get<T, PostedStatus>(x => x.PostedStatus) switch
  218. {
  219. PostedStatus.PostFailed => "Post failed: " + row.Get<T, string>(x => x.PostedNote),
  220. PostedStatus.RequiresRepost => "Repost required: " + row.Get<T, string>(x => x.PostedNote),
  221. PostedStatus.Posted => "Processed",
  222. PostedStatus.NeverPosted or _ => "Not posted yet",
  223. });
  224. }
  225. });
  226. }
  227. #endregion
  228. }