PosterEngine.cs 8.2 KB

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