TimberlinePosterEngine.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using InABox.Core;
  2. using InABox.Core.Postable;
  3. using InABox.Scripting;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Formats.Asn1;
  7. using System.Globalization;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace InABox.Poster.Timberline
  12. {
  13. public class TimberlinePosterEngine<TPostable, TSettings> : PosterEngine<TPostable, ITimberlinePoster<TPostable, TSettings>, TSettings>
  14. where TPostable : Entity, IPostable, IRemotable, IPersistent, new()
  15. where TSettings : TimberlinePosterSettings<TPostable>, new()
  16. {
  17. private ScriptDocument? _script;
  18. private bool _hasCheckedScript;
  19. protected override ITimberlinePoster<TPostable, TSettings> CreatePoster()
  20. {
  21. var poster = base.CreatePoster();
  22. poster.Script = GetScriptDocument();
  23. return poster;
  24. }
  25. private ScriptDocument? GetScriptDocument()
  26. {
  27. if (_hasCheckedScript)
  28. {
  29. return _script;
  30. }
  31. var settings = GetSettings();
  32. if (settings.ScriptEnabled && !string.IsNullOrWhiteSpace(settings.Script))
  33. {
  34. var document = new ScriptDocument(settings.Script);
  35. if (!document.Compile())
  36. {
  37. throw new Exception("Script failed to compile!");
  38. }
  39. _script = document;
  40. }
  41. else
  42. {
  43. _script = null;
  44. }
  45. _hasCheckedScript = true;
  46. return _script;
  47. }
  48. public override bool BeforePost(IDataModel<TPostable> model)
  49. {
  50. return Poster.BeforePost(model);
  51. }
  52. protected override IPostResult<TPostable> DoProcess(IDataModel<TPostable> model)
  53. {
  54. return Poster.Process(model);
  55. }
  56. public override void AfterPost(IDataModel<TPostable> model, IPostResult<TPostable> result)
  57. {
  58. Poster.AfterPost(model, result);
  59. }
  60. }
  61. }