PostUtils.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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>(IEnumerable<T> entities, Action refresh, Action? configurePost = null)
  20. where T : Entity, IPostable, IRemotable, IPersistent, new()
  21. {
  22. var items = entities.AsList();
  23. if (!items.Any())
  24. {
  25. MessageBox.Show($"Please select at least one {typeof(T).Name}.");
  26. return;
  27. }
  28. try
  29. {
  30. if (PosterUtils.Process(items))
  31. {
  32. MessageBox.Show("Processing successful!");
  33. refresh();
  34. }
  35. else
  36. {
  37. MessageBox.Show("Processing failed.");
  38. refresh();
  39. }
  40. }
  41. catch (RepostedException)
  42. {
  43. MessageBox.Show("At least one of the items you selected has already been processed. Processing cancelled.");
  44. }
  45. catch (PostCancelledException)
  46. {
  47. MessageBox.Show("Processing cancelled.");
  48. }
  49. catch (MissingSettingsException)
  50. {
  51. if (configurePost is not null && Security.CanConfigurePost<T>())
  52. {
  53. if (MessageBox.Show($"Processing has not been configured for {inflector.Pluralize(typeof(T).Name)}. Would you like to configure this now?",
  54. "Configure Processing?", MessageBoxButton.YesNoCancel) == MessageBoxResult.Yes)
  55. {
  56. configurePost();
  57. }
  58. else
  59. {
  60. MessageBox.Show("Processing cancelled.");
  61. }
  62. }
  63. else
  64. {
  65. MessageBox.Show($"Processing has not been configured for {inflector.Pluralize(typeof(T).Name)}!");
  66. }
  67. }
  68. catch (Exception e)
  69. {
  70. MessageBox.Show($"Processing failed: {e.Message}");
  71. refresh();
  72. }
  73. }
  74. public static void CreateToolbarButtons<T>(IPanelHost host, Func<IEnumerable<T>> entities, Action refresh, Action? configurePost = null)
  75. where T : Entity, IPostable, IRemotable, IPersistent, new()
  76. {
  77. var postSettings = PosterUtils.LoadPostableSettings<T>();
  78. if (Security.CanPost<T>())
  79. {
  80. Bitmap? image = null;
  81. if (postSettings.Thumbnail.ID != Guid.Empty)
  82. {
  83. var icon = new Client<Document>()
  84. .Load(new Filter<Document>(x => x.ID).IsEqualTo(postSettings.Thumbnail.ID)).FirstOrDefault();
  85. if (icon is not null)
  86. {
  87. image = new ImageConverter().ConvertFrom(icon.Data) as Bitmap;
  88. }
  89. }
  90. host.CreatePanelAction(new PanelAction
  91. {
  92. Caption = postSettings.ButtonName.NotWhiteSpaceOr($"Process {inflector.Pluralize(typeof(T).Name)}"),
  93. Image = image ?? PRSDesktop.Resources.edit,
  94. OnExecute = action =>
  95. {
  96. PostEntities(
  97. entities(),
  98. refresh,
  99. configurePost);
  100. }
  101. });
  102. }
  103. if (configurePost is not null && Security.CanConfigurePost<T>())
  104. {
  105. host.CreateSetupAction(new PanelAction
  106. {
  107. Caption = $"Configure {typeof(T).Name} Processing",
  108. OnExecute = action =>
  109. {
  110. configurePost();
  111. }
  112. });
  113. }
  114. }
  115. public static void ConfigurePost<T>()
  116. where T : Entity, IPostable, IRemotable, IPersistent, new()
  117. {
  118. var postSettings = PosterUtils.LoadPostableSettings<T>();
  119. var grid = (DynamicGridUtils.CreateDynamicGrid(typeof(DynamicGrid<>), typeof(PostableSettings)) as DynamicGrid<PostableSettings>)!;
  120. if (grid.EditItems(new PostableSettings[] { postSettings }))
  121. {
  122. PosterUtils.SavePostableSettings<T>(postSettings);
  123. }
  124. }
  125. public static void CreateToolbarButtons<T>(IPanelHost host, Func<IEnumerable<T>> entities, Action refresh, bool allowConfig)
  126. where T : Entity, IPostable, IRemotable, IPersistent, new()
  127. {
  128. CreateToolbarButtons(host, entities, refresh, allowConfig ? ConfigurePost<T> : null);
  129. }
  130. }
  131. }