PosterEngine.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 IGlobalSettingsPosterEngine
  12. {
  13. IGlobalPosterSettings GetGlobalSettings();
  14. }
  15. /// <summary>
  16. /// Marks an <see cref="IPosterEngine{TPostable}"/> as having global settings, not specific to certain entities.
  17. /// </summary>
  18. /// <typeparam name="TGlobalSettings"></typeparam>
  19. public interface IGlobalSettingsPosterEngine<TPoster, TGlobalSettings> : IGlobalSettingsPosterEngine
  20. where TPoster : IGlobalSettingsPoster<TGlobalSettings>
  21. where TGlobalSettings : BaseObject, IGlobalPosterSettings, new()
  22. {
  23. new TGlobalSettings GetGlobalSettings() => PosterUtils.LoadGlobalPosterSettings<TGlobalSettings>();
  24. IGlobalPosterSettings IGlobalSettingsPosterEngine.GetGlobalSettings() => GetGlobalSettings();
  25. }
  26. public interface IPosterEngine<TPostable>
  27. where TPostable : Entity, IPostable, IRemotable, IPersistent, new()
  28. {
  29. IPostResult<TPostable>? Process(IDataModel<TPostable> model);
  30. }
  31. public interface IPosterEngine<TPostable, TPoster, TSettings> : IPosterEngine<TPostable>
  32. where TPostable : Entity, IPostable, IRemotable, IPersistent, new()
  33. where TPoster : IPoster<TPostable, TSettings>
  34. where TSettings : PosterSettings, new()
  35. {
  36. }
  37. internal static class PosterEngineUtils
  38. {
  39. private static Type[]? _posters;
  40. public static Type GetPoster(Type TPoster)
  41. {
  42. _posters ??= CoreUtils.TypeList(
  43. AppDomain.CurrentDomain.GetAssemblies(),
  44. x => x.IsClass
  45. && !x.IsAbstract
  46. && !x.IsGenericType
  47. && x.HasInterface(typeof(IPoster<,>))
  48. ).ToArray();
  49. return _posters.Where(x => TPoster.IsAssignableFrom(x)).FirstOrDefault()
  50. ?? throw new Exception($"No poster of type {TPoster}.");
  51. }
  52. }
  53. /// <summary>
  54. /// A base class for all <see cref="IPosterEngine{TPostable}"/>. A concrete instance of this will be loaded by
  55. /// <see cref="PosterUtils.Process{T}(IDataModel{T})"/>; a new instance is guaranteed to be created each time that method is called.
  56. /// </summary>
  57. /// <typeparam name="TPostable"></typeparam>
  58. /// <typeparam name="TPoster"></typeparam>
  59. /// <typeparam name="TSettings"></typeparam>
  60. public abstract class PosterEngine<TPostable, TPoster, TSettings> : IPosterEngine<TPostable, TPoster, TSettings>
  61. where TPostable : Entity, IPostable, IRemotable, IPersistent, new()
  62. where TPoster : class, IPoster<TPostable, TSettings>
  63. where TSettings : PosterSettings, new()
  64. {
  65. protected TPoster Poster;
  66. public PosterEngine()
  67. {
  68. Poster = CreatePoster();
  69. }
  70. private static readonly Type? PosterType = PosterEngineUtils.GetPoster(typeof(TPoster));
  71. protected virtual TPoster CreatePoster()
  72. {
  73. var poster = (Activator.CreateInstance(PosterType!) as TPoster)!;
  74. poster.Settings = GetSettings();
  75. if(this is IGlobalSettingsPosterEngine globalSettingsEngine && poster is IGlobalSettingsPoster globalSettingsPoster)
  76. {
  77. globalSettingsPoster.GlobalSettings = globalSettingsEngine.GetGlobalSettings();
  78. }
  79. return poster;
  80. }
  81. private TSettings? _settings;
  82. protected TSettings GetSettings()
  83. {
  84. _settings ??= PosterUtils.LoadPosterSettings<TPostable, TSettings>();
  85. return _settings;
  86. }
  87. protected void SaveSettings(TSettings settings)
  88. {
  89. _settings = settings;
  90. PosterUtils.SavePosterSettings<TPostable, TSettings>(_settings);
  91. }
  92. /// <summary>
  93. /// Returns the <see cref="TSettings.Script"/>, if <see cref="TSettings.ScriptEnabled"/> is <see langword="true"/>;
  94. /// otherwise, returns <see langword="null"/>.
  95. /// </summary>
  96. protected string? GetScript()
  97. {
  98. var settings = GetSettings();
  99. return settings.ScriptEnabled ? settings.Script : null;
  100. }
  101. protected abstract IPostResult<TPostable> DoProcess(IDataModel<TPostable> model);
  102. /// <summary>
  103. /// Process the <paramref name="model"/> before loading;
  104. /// </summary>
  105. /// <param name="model"></param>
  106. /// <returns><see langword="false"/> if the processing must be cancelled.</returns>
  107. public abstract bool BeforePost(IDataModel<TPostable> model);
  108. /// <summary>
  109. /// Prior to saving the <typeparamref name="TPostable"/> entities, make any necessary changes to those entities.
  110. /// This is only called if <see cref="Process(IDataModel{TPostable})"/> returned <see langword="true"/>.
  111. /// </summary>
  112. /// <param name="model"></param>
  113. /// <param name="result">The result object returned by <see cref="DoProcess(IDataModel{TPostable})"/></param>
  114. public abstract void AfterPost(IDataModel<TPostable> model, IPostResult<TPostable> result);
  115. private static void SetFailed(IList<TPostable> entities)
  116. {
  117. foreach (var post in entities)
  118. {
  119. post.PostedStatus = PostedStatus.PostFailed;
  120. post.PostedNote = "Post failed.";
  121. }
  122. new Client<TPostable>().Save(entities, "Post failed by user.");
  123. }
  124. public IPostResult<TPostable>? Process(IDataModel<TPostable> model)
  125. {
  126. if (!BeforePost(model))
  127. {
  128. return null;
  129. }
  130. // Add posted flags; if the columns is null, then we don't need to worry, because it will be loaded.
  131. model.GetColumns<TPostable>()?.Add(x => x.PostedStatus)
  132. .Add(x => x.Posted)
  133. .Add(x => x.PostedNote);
  134. model.LoadModel();
  135. var data = model.GetTable<TPostable>();
  136. if (!data.Rows.Any())
  137. {
  138. throw new EmptyPostException();
  139. }
  140. if(data.Rows.Any(x => x.Get<TPostable, PostedStatus>(x => x.PostedStatus) == PostedStatus.Posted))
  141. {
  142. throw new RepostedException();
  143. }
  144. try
  145. {
  146. var result = DoProcess(model);
  147. AfterPost(model, result);
  148. new Client<TPostable>().Save(result.PostedEntities, "Posted by user.");
  149. foreach (var (type, fragments) in result.Fragments)
  150. {
  151. Client.Create(type).Save(fragments.Cast<Entity>(), "");
  152. }
  153. return result;
  154. }
  155. catch (PostCancelledException)
  156. {
  157. var entities = data.ToObjects<TPostable>().ToList();
  158. SetFailed(entities);
  159. throw;
  160. }
  161. catch (PostFailedMessageException)
  162. {
  163. throw;
  164. }
  165. catch(Exception e)
  166. {
  167. Logger.Send(LogType.Error, "", $"Post Failed: {CoreUtils.FormatException(e)}");
  168. var entities = data.ToObjects<TPostable>().ToList();
  169. SetFailed(entities);
  170. throw;
  171. }
  172. }
  173. }
  174. }