PosterEngine.cs 5.4 KB

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