PosterEngine.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. IPostResult<TPostable>? 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. var poster = (Activator.CreateInstance(PosterType!) as TPoster)!;
  59. poster.Settings = GetSettings();
  60. return poster;
  61. }
  62. private TSettings? _settings;
  63. protected TSettings GetSettings()
  64. {
  65. _settings ??= PosterUtils.LoadPosterSettings<TPostable, TSettings>();
  66. return _settings;
  67. }
  68. protected void SaveSettings(TSettings settings)
  69. {
  70. _settings = settings;
  71. PosterUtils.SavePosterSettings<TPostable, TSettings>(_settings);
  72. }
  73. /// <summary>
  74. /// Returns the <see cref="TSettings.Script"/>, if <see cref="TSettings.ScriptEnabled"/> is <see langword="true"/>;
  75. /// otherwise, returns <see langword="null"/>.
  76. /// </summary>
  77. protected string? GetScript()
  78. {
  79. var settings = GetSettings();
  80. return settings.ScriptEnabled ? settings.Script : null;
  81. }
  82. protected abstract IPostResult<TPostable> DoProcess(IDataModel<TPostable> model);
  83. /// <summary>
  84. /// Process the <paramref name="model"/> before loading;
  85. /// </summary>
  86. /// <param name="model"></param>
  87. /// <returns><see langword="false"/> if the processing must be cancelled.</returns>
  88. public abstract bool BeforePost(IDataModel<TPostable> model);
  89. /// <summary>
  90. /// Prior to saving the <typeparamref name="TPostable"/> entities, make any necessary changes to those entities.
  91. /// This is only called if <see cref="Process(IDataModel{TPostable})"/> returned <see langword="true"/>.
  92. /// </summary>
  93. /// <param name="model"></param>
  94. /// <param name="result">The result object returned by <see cref="DoProcess(IDataModel{TPostable})"/></param>
  95. public abstract void AfterPost(IDataModel<TPostable> model, IPostResult<TPostable> result);
  96. private static void SetFailed(IList<TPostable> entities)
  97. {
  98. foreach (var post in entities)
  99. {
  100. post.PostedStatus = PostedStatus.PostFailed;
  101. post.PostedNote = "Post failed.";
  102. }
  103. new Client<TPostable>().Save(entities, "Post failed by user.");
  104. }
  105. public IPostResult<TPostable>? Process(IDataModel<TPostable> model)
  106. {
  107. if (!BeforePost(model))
  108. {
  109. return null;
  110. }
  111. // Add posted flags; if the columns is null, then we don't need to worry, because it will be loaded.
  112. model.GetColumns<TPostable>()?.Add(x => x.PostedStatus)
  113. .Add(x => x.Posted)
  114. .Add(x => x.PostedNote);
  115. model.LoadModel();
  116. var data = model.GetTable<TPostable>();
  117. if (!data.Rows.Any())
  118. {
  119. throw new EmptyPostException();
  120. }
  121. if(data.Rows.Any(x => x.Get<TPostable, PostedStatus>(x => x.PostedStatus) == PostedStatus.Posted))
  122. {
  123. throw new RepostedException();
  124. }
  125. try
  126. {
  127. var result = DoProcess(model);
  128. AfterPost(model, result);
  129. new Client<TPostable>().Save(result.PostedEntities, "Posted by user.");
  130. foreach (var (type, fragments) in result.Fragments)
  131. {
  132. Client.Create(type).Save(fragments.Cast<Entity>(), "");
  133. }
  134. return result;
  135. }
  136. catch (PostCancelledException)
  137. {
  138. var entities = data.ToObjects<TPostable>().ToList();
  139. SetFailed(entities);
  140. throw;
  141. }
  142. catch(Exception e)
  143. {
  144. Logger.Send(LogType.Error, "", $"Post Failed: {CoreUtils.FormatException(e)}");
  145. var entities = data.ToObjects<TPostable>().ToList();
  146. SetFailed(entities);
  147. throw;
  148. }
  149. }
  150. }
  151. }