PosterEngine.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. /// <summary>
  52. /// A set of fragments that also need to be saved along with the <see cref="IPostable"/> entities.
  53. /// </summary>
  54. private Dictionary<Type, List<IPostableFragment>> Fragments = new Dictionary<Type, List<IPostableFragment>>();
  55. public PosterEngine()
  56. {
  57. Poster = CreatePoster();
  58. }
  59. private static readonly Type? PosterType = PosterEngineUtils.GetPoster(typeof(TPoster));
  60. protected virtual TPoster CreatePoster()
  61. {
  62. return (Activator.CreateInstance(PosterType!) as TPoster)!;
  63. }
  64. protected static TSettings GetSettings()
  65. {
  66. return PosterUtils.LoadPosterSettings<TPostable, TSettings>();
  67. }
  68. protected static void SaveSettings(TSettings settings)
  69. {
  70. PosterUtils.SavePosterSettings<TPostable, TSettings>(settings);
  71. }
  72. /// <summary>
  73. /// Returns the <see cref="TSettings.Script"/>, if <see cref="TSettings.ScriptEnabled"/> is <see langword="true"/>;
  74. /// otherwise, returns <see langword="null"/>.
  75. /// </summary>
  76. protected static string? GetScript()
  77. {
  78. var settings = GetSettings();
  79. return settings.ScriptEnabled ? settings.Script : null;
  80. }
  81. protected abstract bool DoProcess(IDataModel<TPostable> model);
  82. /// <summary>
  83. /// Process the <paramref name="model"/> before loading;
  84. /// </summary>
  85. /// <param name="model"></param>
  86. /// <returns><see langword="false"/> if the processing must be cancelled.</returns>
  87. public abstract bool BeforePost(IDataModel<TPostable> model);
  88. /// <summary>
  89. /// Prior to saving the <typeparamref name="TPostable"/> entities, make any necessary changes to those entities.
  90. /// This is only called if <see cref="Process(IDataModel{TPostable})"/> returned <see langword="true"/>.
  91. /// </summary>
  92. /// <param name="model"></param>
  93. public abstract void AfterPost(IDataModel<TPostable> model);
  94. private static void SetFailed(IList<TPostable> entities)
  95. {
  96. foreach (var post in entities)
  97. {
  98. post.PostedStatus = PostedStatus.PostFailed;
  99. post.PostedNote = "Post failed.";
  100. }
  101. new Client<TPostable>().Save(entities, "Post failed by user.");
  102. }
  103. protected void AddFragment(IPostableFragment fragment)
  104. {
  105. var type = fragment.GetType();
  106. if(!Fragments.TryGetValue(type, out var fragments))
  107. {
  108. fragments = new List<IPostableFragment>();
  109. Fragments[type] = fragments;
  110. }
  111. fragments.Add(fragment);
  112. }
  113. public bool Process(IDataModel<TPostable> model)
  114. {
  115. if (!BeforePost(model))
  116. {
  117. return false;
  118. }
  119. model.LoadModel();
  120. var data = model.GetTable<TPostable>();
  121. if (!data.Rows.Any())
  122. {
  123. throw new EmptyPostException();
  124. }
  125. if(data.Rows.Any(x => x.Get<TPostable, PostedStatus>(x => x.PostedStatus) == PostedStatus.Posted))
  126. {
  127. throw new RepostedException();
  128. }
  129. try
  130. {
  131. var success = DoProcess(model);
  132. if (success)
  133. {
  134. AfterPost(model);
  135. }
  136. var entities = data.ToObjects<TPostable>().ToList();
  137. if (success)
  138. {
  139. foreach (var post in entities)
  140. {
  141. post.Posted = DateTime.Now;
  142. post.PostedStatus = PostedStatus.Posted;
  143. post.PostedNote = "";
  144. }
  145. new Client<TPostable>().Save(entities, "Posted by user.");
  146. foreach(var (type, fragments) in Fragments)
  147. {
  148. Client.Create(type).Save(fragments.Cast<Entity>(), "");
  149. }
  150. }
  151. else
  152. {
  153. SetFailed(entities);
  154. }
  155. return success;
  156. }
  157. catch (PostCancelledException)
  158. {
  159. var entities = data.ToObjects<TPostable>().ToList();
  160. SetFailed(entities);
  161. throw;
  162. }
  163. catch(Exception e)
  164. {
  165. Logger.Send(LogType.Error, "", $"Post Failed: {CoreUtils.FormatException(e)}");
  166. var entities = data.ToObjects<TPostable>().ToList();
  167. SetFailed(entities);
  168. throw;
  169. }
  170. }
  171. }
  172. }