PostUtils.cs 6.2 KB

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