PosterEngine.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using InABox.Clients;
  2. using InABox.Configuration;
  3. using InABox.Core.Postable;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Text;
  9. namespace InABox.Core
  10. {
  11. public interface IPosterEngine<TPostable>
  12. where TPostable : Entity, IPostable, IRemotable, IPersistent, new()
  13. {
  14. bool Process(IDataModel<TPostable> model);
  15. }
  16. public interface IPosterEngine<TPostable, TPoster, TSettings> : IPosterEngine<TPostable>
  17. where TPostable : Entity, IPostable, IRemotable, IPersistent, new()
  18. where TPoster : IPoster<TPostable, TSettings>
  19. where TSettings : PosterSettings, new()
  20. {
  21. }
  22. internal static class PosterEngineUtils
  23. {
  24. private static Type[]? _posters;
  25. public static Type GetPoster(Type TPoster)
  26. {
  27. _posters ??= CoreUtils.TypeList(
  28. AppDomain.CurrentDomain.GetAssemblies(),
  29. x => x.IsClass
  30. && !x.IsAbstract
  31. && !x.IsGenericType
  32. && x.HasInterface(typeof(IPoster<,>))
  33. ).ToArray();
  34. return _posters.Where(x => TPoster.IsAssignableFrom(x)).FirstOrDefault()
  35. ?? throw new Exception($"No poster of type {TPoster}.");
  36. }
  37. }
  38. /// <summary>
  39. /// A base class for all <see cref="IPosterEngine{TPostable}"/>. A concrete instance of this will be loaded by
  40. /// <see cref="PosterUtils.Process{T}(IDataModel{T})"/>; a new instance is guaranteed to be created each time that method is called.
  41. /// </summary>
  42. /// <typeparam name="TPostable"></typeparam>
  43. /// <typeparam name="TPoster"></typeparam>
  44. /// <typeparam name="TSettings"></typeparam>
  45. public abstract class PosterEngine<TPostable, TPoster, TSettings> : IPosterEngine<TPostable, TPoster, TSettings>
  46. where TPostable : Entity, IPostable, IRemotable, IPersistent, new()
  47. where TPoster : class, IPoster<TPostable, TSettings>
  48. where TSettings : PosterSettings, new()
  49. {
  50. protected TPoster Poster;
  51. public PosterEngine()
  52. {
  53. Poster = CreatePoster();
  54. }
  55. private static readonly Type? PosterType = PosterEngineUtils.GetPoster(typeof(TPoster));
  56. protected virtual TPoster CreatePoster()
  57. {
  58. return (Activator.CreateInstance(PosterType!) as TPoster)!;
  59. }
  60. protected static TSettings GetSettings()
  61. {
  62. return PosterUtils.LoadPosterSettings<TPostable, TSettings>();
  63. }
  64. protected static void SaveSettings(TSettings settings)
  65. {
  66. PosterUtils.SavePosterSettings<TPostable, TSettings>(settings);
  67. }
  68. /// <summary>
  69. /// Returns the <see cref="TSettings.Script"/>, if <see cref="TSettings.ScriptEnabled"/> is <see langword="true"/>;
  70. /// otherwise, returns <see langword="null"/>.
  71. /// </summary>
  72. protected static string? GetScript()
  73. {
  74. var settings = GetSettings();
  75. return settings.ScriptEnabled ? settings.Script : null;
  76. }
  77. protected abstract bool DoProcess(IDataModel<TPostable> model);
  78. /// <summary>
  79. /// Process the <paramref name="model"/> before loading;
  80. /// </summary>
  81. /// <param name="model"></param>
  82. /// <returns><see langword="false"/> if the processing must be cancelled.</returns>
  83. public abstract bool BeforePost(IDataModel<TPostable> model);
  84. /// <summary>
  85. /// Prior to saving the <typeparamref name="TPostable"/> entities, make any necessary changes to those entities.
  86. /// This is only called if <see cref="Process(IDataModel{TPostable})"/> returned <see langword="true"/>.
  87. /// </summary>
  88. /// <param name="model"></param>
  89. public abstract void AfterPost(IDataModel<TPostable> model);
  90. private static void SetFailed(IList<TPostable> entities)
  91. {
  92. foreach (var post in entities)
  93. {
  94. post.PostedStatus = PostedStatus.PostFailed;
  95. post.PostedNote = "Post failed.";
  96. }
  97. new Client<TPostable>().Save(entities, "Post failed by user.");
  98. }
  99. public bool Process(IDataModel<TPostable> model)
  100. {
  101. if (!BeforePost(model))
  102. {
  103. return false;
  104. }
  105. model.LoadModel();
  106. var data = model.GetTable<TPostable>();
  107. if (!data.Rows.Any())
  108. {
  109. throw new EmptyPostException();
  110. }
  111. if(data.Rows.Any(x => x.Get<TPostable, PostedStatus>(x => x.PostedStatus) == PostedStatus.Posted))
  112. {
  113. throw new RepostedException();
  114. }
  115. try
  116. {
  117. var success = DoProcess(model);
  118. if (success)
  119. {
  120. AfterPost(model);
  121. }
  122. var entities = data.ToObjects<TPostable>().ToList();
  123. if (success)
  124. {
  125. foreach (var post in entities)
  126. {
  127. post.Posted = DateTime.Now;
  128. post.PostedStatus = PostedStatus.Posted;
  129. post.PostedNote = "";
  130. }
  131. new Client<TPostable>().Save(entities, "Posted by user.");
  132. }
  133. else
  134. {
  135. SetFailed(entities);
  136. }
  137. return success;
  138. }
  139. catch (PostCancelledException)
  140. {
  141. var entities = data.ToObjects<TPostable>().ToList();
  142. SetFailed(entities);
  143. throw;
  144. }
  145. catch(Exception e)
  146. {
  147. Logger.Send(LogType.Error, "", $"Post Failed: {CoreUtils.FormatException(e)}");
  148. var entities = data.ToObjects<TPostable>().ToList();
  149. SetFailed(entities);
  150. throw;
  151. }
  152. }
  153. }
  154. }