PostUtils.cs 5.1 KB

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