PostUtils.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 System;
  8. using System.Collections.Generic;
  9. using System.Drawing;
  10. using System.Globalization;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. namespace PRSDesktop
  16. {
  17. public static class PostUtils
  18. {
  19. private static readonly Inflector.Inflector inflector = new(new CultureInfo("en"));
  20. public static void PostEntities<T>(IDataModel<T> model, Action refresh, Action? configurePost = null)
  21. where T : Entity, IPostable, IRemotable, IPersistent, new()
  22. {
  23. try
  24. {
  25. var result = PosterUtils.Process(model);
  26. if(result is null)
  27. {
  28. MessageWindow.ShowMessage($"Processing failed", "Processing failed");
  29. refresh();
  30. }
  31. else
  32. {
  33. var failedMessages = new List<string>();
  34. var successCount = 0;
  35. foreach(var entity in result.PostedEntities)
  36. {
  37. if(entity.PostedStatus == PostedStatus.PostFailed)
  38. {
  39. failedMessages.Add(entity.PostedNote);
  40. }
  41. else
  42. {
  43. successCount++;
  44. }
  45. }
  46. if(successCount == 0)
  47. {
  48. MessageWindow.ShowMessage($"Processing failed:\n - {string.Join("\n - ", failedMessages)}", "Processing failed.");
  49. }
  50. else if(failedMessages.Count == 0)
  51. {
  52. MessageWindow.ShowMessage($"Processing successful; {successCount} items processed", "Processing successful.");
  53. }
  54. else
  55. {
  56. MessageWindow.ShowMessage($"{successCount} items succeeded, but {failedMessages.Count} failed:\n - {string.Join("\n - ", failedMessages)}", "Partial success");
  57. }
  58. refresh();
  59. }
  60. }
  61. catch (EmptyPostException)
  62. {
  63. MessageWindow.ShowMessage($"Please select at least one {typeof(T).Name}.", "Select items");
  64. }
  65. catch (PostFailedMessageException e)
  66. {
  67. MessageWindow.ShowMessage(e.Message, "Post failed");
  68. }
  69. catch (RepostedException)
  70. {
  71. MessageWindow.ShowMessage("At least one of the items you selected has already been processed. Processing cancelled.", "Already processed");
  72. }
  73. catch (PostCancelledException)
  74. {
  75. MessageWindow.ShowMessage("Processing cancelled.", "Cancelled");
  76. }
  77. catch (MissingSettingsException)
  78. {
  79. if (configurePost is not null && Security.CanConfigurePost<T>())
  80. {
  81. if (MessageWindow.ShowYesNo($"Processing has not been configured for {inflector.Pluralize(typeof(T).Name)}. Would you like to configure this now?",
  82. "Configure Processing?"))
  83. {
  84. configurePost();
  85. }
  86. else
  87. {
  88. MessageWindow.ShowMessage("Processing cancelled.", "Cancelled");
  89. }
  90. }
  91. else
  92. {
  93. MessageWindow.ShowMessage($"Processing has not been configured for {inflector.Pluralize(typeof(T).Name)}!", "Unconfigured");
  94. }
  95. }
  96. catch (Exception e)
  97. {
  98. MessageWindow.ShowError("Processing failed.", e);
  99. refresh();
  100. }
  101. }
  102. public static void CreateToolbarButtons<T>(IPanelHost host, Func<IDataModel<T>> model, Action refresh, Action? configurePost = null)
  103. where T : Entity, IPostable, IRemotable, IPersistent, new()
  104. {
  105. var postSettings = PosterUtils.LoadPostableSettings<T>();
  106. if (Security.CanPost<T>() && !postSettings.PosterType.IsNullOrWhiteSpace())
  107. {
  108. Bitmap? image = null;
  109. if (postSettings.Thumbnail.ID != Guid.Empty)
  110. {
  111. var icon = new Client<Document>()
  112. .Load(new Filter<Document>(x => x.ID).IsEqualTo(postSettings.Thumbnail.ID)).FirstOrDefault();
  113. if (icon?.Data?.Any() == true)
  114. image = new ImageConverter().ConvertFrom(icon.Data) as Bitmap;
  115. }
  116. host.CreatePanelAction(new PanelAction
  117. {
  118. Caption = postSettings.ButtonName.NotWhiteSpaceOr($"Process {inflector.Pluralize(typeof(T).Name)}"),
  119. Image = image ?? PRSDesktop.Resources.edit,
  120. OnExecute = action =>
  121. {
  122. PostEntities(
  123. model(),
  124. refresh,
  125. configurePost);
  126. }
  127. });
  128. }
  129. if (configurePost is not null && Security.CanConfigurePost<T>())
  130. {
  131. host.CreateSetupAction(new PanelAction
  132. {
  133. Caption = $"Configure {CoreUtils.Neatify(typeof(T).Name)} Processing",
  134. OnExecute = action =>
  135. {
  136. configurePost();
  137. }
  138. });
  139. }
  140. }
  141. public static void ConfigurePost<T>()
  142. where T : Entity, IPostable, IRemotable, IPersistent, new()
  143. {
  144. var postSettings = PosterUtils.LoadPostableSettings<T>();
  145. var grid = (DynamicGridUtils.CreateDynamicGrid(typeof(DynamicGrid<>), typeof(PostableSettings)) as DynamicGrid<PostableSettings>)!;
  146. if (grid.EditItems(new PostableSettings[] { postSettings }))
  147. {
  148. PosterUtils.SavePostableSettings<T>(postSettings);
  149. }
  150. }
  151. public static void CreateToolbarButtons<T>(IPanelHost host, Func<IDataModel<T>> model, Action refresh, bool allowConfig)
  152. where T : Entity, IPostable, IRemotable, IPersistent, new()
  153. {
  154. CreateToolbarButtons(host, model, refresh, allowConfig ? ConfigurePost<T> : null);
  155. }
  156. }
  157. }