PostUtils.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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(MissingSettingException e)
  79. {
  80. if (configurePost is not null && Security.CanConfigurePost<T>())
  81. {
  82. if (MessageWindow.ShowYesNo($"'{e.Setting}' has not been set-up for {inflector.Pluralize(typeof(T).Name)}. Would you like to configure this now?",
  83. "Configure Processing?"))
  84. {
  85. if (e.SettingsType.IsAssignableTo(typeof(IGlobalPosterSettings)))
  86. {
  87. PostableSettingsGrid.ConfigureGlobalPosterSettings(e.SettingsType);
  88. }
  89. else
  90. {
  91. PostableSettingsGrid.ConfigurePosterSettings<T>(e.SettingsType);
  92. }
  93. }
  94. else
  95. {
  96. MessageWindow.ShowMessage("Processing cancelled.", "Cancelled");
  97. }
  98. }
  99. else
  100. {
  101. MessageWindow.ShowMessage($"'{e.Setting}' has not been set-up for {inflector.Pluralize(typeof(T).Name)}", "Unconfigured");
  102. }
  103. }
  104. catch (MissingSettingsException)
  105. {
  106. if (configurePost is not null && Security.CanConfigurePost<T>())
  107. {
  108. if (MessageWindow.ShowYesNo($"Processing has not been configured for {inflector.Pluralize(typeof(T).Name)}. Would you like to configure this now?",
  109. "Configure Processing?"))
  110. {
  111. configurePost();
  112. }
  113. else
  114. {
  115. MessageWindow.ShowMessage("Processing cancelled.", "Cancelled");
  116. }
  117. }
  118. else
  119. {
  120. MessageWindow.ShowMessage($"Processing has not been configured for {inflector.Pluralize(typeof(T).Name)}!", "Unconfigured");
  121. }
  122. }
  123. catch (Exception e)
  124. {
  125. MessageWindow.ShowError("Processing failed.", e);
  126. refresh();
  127. }
  128. }
  129. public static void CreateToolbarButtons<T>(IPanelHost host, Func<IDataModel<T>> model, Action refresh, Action? configurePost = null)
  130. where T : Entity, IPostable, IRemotable, IPersistent, new()
  131. {
  132. var postSettings = PosterUtils.LoadPostableSettings<T>();
  133. if (Security.CanPost<T>() && !postSettings.PosterType.IsNullOrWhiteSpace())
  134. {
  135. Bitmap? image = null;
  136. if (postSettings.Thumbnail.ID != Guid.Empty)
  137. {
  138. var icon = new Client<Document>()
  139. .Load(new Filter<Document>(x => x.ID).IsEqualTo(postSettings.Thumbnail.ID)).FirstOrDefault();
  140. if (icon?.Data?.Any() == true)
  141. image = new ImageConverter().ConvertFrom(icon.Data) as Bitmap;
  142. }
  143. host.CreatePanelAction(new PanelAction
  144. {
  145. Caption = postSettings.ButtonName.NotWhiteSpaceOr($"Process {inflector.Pluralize(typeof(T).Name)}"),
  146. Image = image ?? PRSDesktop.Resources.edit,
  147. OnExecute = action =>
  148. {
  149. PostEntities(
  150. model(),
  151. refresh,
  152. configurePost);
  153. }
  154. });
  155. if (postSettings.ShowClearButton)
  156. {
  157. host.CreatePanelAction(new PanelAction
  158. {
  159. Caption = "Clear Posted Flag",
  160. Image = image ?? PRSDesktop.Resources.refresh,
  161. OnExecute = action =>
  162. {
  163. var dataModel = model();
  164. foreach(var (key, table) in dataModel.ModelTables)
  165. {
  166. table.IsDefault = false;
  167. }
  168. dataModel.SetColumns<T>(Columns.Required<T>().Add(x => x.PostedStatus).Add(x => x.PostedReference).Add(x => x.PostedNote).Add(x => x.Posted));
  169. dataModel.SetIsDefault<T>(true);
  170. dataModel.LoadModel();
  171. var items = dataModel.GetTable<T>().ToArray<T>();
  172. foreach(var item in items)
  173. {
  174. item.PostedStatus = PostedStatus.NeverPosted;
  175. item.PostedReference = "";
  176. item.PostedNote = "";
  177. item.Posted = DateTime.MinValue;
  178. }
  179. Client.Save(items, "Cleared posted flag");
  180. refresh();
  181. }
  182. });
  183. }
  184. }
  185. if (configurePost is not null && Security.CanConfigurePost<T>())
  186. {
  187. host.CreateSetupAction(new PanelAction
  188. {
  189. Caption = $"Configure {CoreUtils.Neatify(typeof(T).Name)} Processing",
  190. OnExecute = action =>
  191. {
  192. configurePost();
  193. }
  194. });
  195. }
  196. }
  197. public static void ConfigurePost<T>()
  198. where T : Entity, IPostable, IRemotable, IPersistent, new()
  199. {
  200. var postSettings = PosterUtils.LoadPostableSettings<T>();
  201. var grid = (DynamicGridUtils.CreateDynamicGrid(typeof(DynamicGrid<>), typeof(PostableSettings)) as DynamicGrid<PostableSettings>)!;
  202. if (grid.EditItems(new PostableSettings[] { postSettings }))
  203. {
  204. PosterUtils.SavePostableSettings<T>(postSettings);
  205. }
  206. }
  207. public static void CreateToolbarButtons<T>(IPanelHost host, Func<IDataModel<T>> model, Action refresh, bool allowConfig)
  208. where T : Entity, IPostable, IRemotable, IPersistent, new()
  209. {
  210. CreateToolbarButtons(host, model, refresh, allowConfig ? ConfigurePost<T> : null);
  211. }
  212. #region PostColumn
  213. private static readonly BitmapImage? post = PRSDesktop.Resources.post.AsBitmapImage();
  214. private static readonly BitmapImage? tick = PRSDesktop.Resources.tick.AsBitmapImage();
  215. private static readonly BitmapImage? warning = PRSDesktop.Resources.warning.AsBitmapImage();
  216. private static readonly BitmapImage? refresh = PRSDesktop.Resources.refresh.AsBitmapImage();
  217. public static void AddPostColumn<T>(DynamicGrid<T> grid)
  218. where T : Entity, IPostable, IRemotable, IPersistent, new()
  219. {
  220. grid.HiddenColumns.Add(x => x.PostedStatus);
  221. grid.HiddenColumns.Add(x => x.PostedNote);
  222. grid.ActionColumns.Add(new DynamicImageColumn(
  223. row =>
  224. {
  225. if (row is null)
  226. return post;
  227. return row.Get<T, PostedStatus>(x => x.PostedStatus) switch
  228. {
  229. PostedStatus.PostFailed => warning,
  230. PostedStatus.Posted => tick,
  231. PostedStatus.RequiresRepost => refresh,
  232. PostedStatus.NeverPosted or _ => null,
  233. };
  234. },
  235. null)
  236. {
  237. ToolTip = (column, row) =>
  238. {
  239. if (row is null)
  240. {
  241. return column.TextToolTip($"{CoreUtils.Neatify(typeof(T).Name)} Processed Status");
  242. }
  243. return column.TextToolTip(row.Get<T, PostedStatus>(x => x.PostedStatus) switch
  244. {
  245. PostedStatus.PostFailed => "Post failed: " + row.Get<T, string>(x => x.PostedNote),
  246. PostedStatus.RequiresRepost => "Repost required: " + row.Get<T, string>(x => x.PostedNote),
  247. PostedStatus.Posted => "Processed",
  248. PostedStatus.NeverPosted or _ => "Not posted yet",
  249. });
  250. }
  251. });
  252. }
  253. #endregion
  254. }