MailNotificationDataModel.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using GreenTree.Nachtragsmanagement.Core.Plugins;
  2. using GreenTree.Nachtragsmanagement.Services.Misc;
  3. using GreenTree.Nachtragsmanagement.Services.Scheduling;
  4. using GreenTree.Nachtragsmanagement.Web.Models.Admin.User;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Web;
  9. namespace GreenTree.Nachtragsmanagement.Web.Models.Misc
  10. {
  11. public class MailNotificationDataModel
  12. {
  13. public int Id { get; set; }
  14. public string CronExpression { get; set; }
  15. public string CronExpressionDescription { get; set; }
  16. public string NextExecutionTime { get; set; }
  17. public INotificationPlugin NotificationPlugin { get; set; }
  18. public string NotificationPluginSystemName { get; set; }
  19. public string NotificationPluginSystemNameDescription { get; set; }
  20. public string NotificationJobSystemName { get; set; }
  21. public string NotificationJobSystemNameDescription { get; set; }
  22. public ICollection<int> UserValues { get; set; }
  23. public ICollection<string> UserDescriptions { get; set; }
  24. public ICollection<UserDataModel> Users { get; set; }
  25. public string UserDescription
  26. {
  27. get
  28. {
  29. if (UserDescriptions == null)
  30. return String.Empty;
  31. else
  32. return String.Join(", ", UserDescriptions);
  33. }
  34. }
  35. public MailNotificationDataModel()
  36. {
  37. UserValues = new List<int>();
  38. UserDescriptions = new List<string>();
  39. }
  40. public static MailNotificationDataModel FromMailNotification(Core.Domain.Misc.MailNotification mailNotificationEntity,
  41. bool newWhenIsNull, INotificationService service, INotificationScheduler scheduler)
  42. {
  43. if (mailNotificationEntity == null && newWhenIsNull)
  44. return new MailNotificationDataModel
  45. {
  46. Id = -1,
  47. };
  48. if (mailNotificationEntity == null && !newWhenIsNull)
  49. throw new ArgumentNullException("mailNotificationEntity", "Cannot create MailNotificationDataModel from NULL mailNotification entity.");
  50. var notificationPlugin = service.GetNotificationPlugin(mailNotificationEntity.NotificationPluginSystemName);
  51. var nextExecutionTime = scheduler.GetNextExecutionOfJob(mailNotificationEntity);
  52. var notificationDataModel = new MailNotificationDataModel
  53. {
  54. Id = mailNotificationEntity.Id,
  55. CronExpression = mailNotificationEntity.CronExpression,
  56. CronExpressionDescription =
  57. CronExpressionDescriptor.ExpressionDescriptor.GetDescription(
  58. mailNotificationEntity.CronExpression,
  59. new CronExpressionDescriptor.Options { Locale = "de" } ),
  60. NextExecutionTime = nextExecutionTime == DateTime.MinValue
  61. ? String.Empty
  62. : nextExecutionTime.ToString("dd.MM.yyyy - HH:mm \"Uhr\""),
  63. NotificationPlugin = notificationPlugin,
  64. NotificationPluginSystemName = mailNotificationEntity.NotificationPluginSystemName,
  65. NotificationJobSystemName = mailNotificationEntity.NotificationJobSystemName,
  66. UserValues =
  67. mailNotificationEntity.Users
  68. .Select(r => r.Id)
  69. .ToList(),
  70. UserDescriptions =
  71. mailNotificationEntity.Users
  72. .Select(r => String.Format("{0} - ({1})", r.Lastname,
  73. String.Join(", ", r.Roles.Select(u => u.Description))))
  74. .ToList(),
  75. Users =
  76. mailNotificationEntity.Users
  77. .Select(r => UserDataModel.FromUser(r, false))
  78. .ToList(),
  79. };
  80. notificationDataModel.NotificationPluginSystemNameDescription =
  81. notificationPlugin == null
  82. ? String.Empty
  83. : notificationPlugin.Name;
  84. notificationDataModel.NotificationJobSystemNameDescription =
  85. notificationPlugin == null
  86. ? String.Empty
  87. : notificationPlugin.AvailableNotificationJobs
  88. .Any(j => j.SystemName == notificationDataModel.NotificationJobSystemName)
  89. ? notificationPlugin.AvailableNotificationJobs
  90. .First(j => j.SystemName == notificationDataModel.NotificationJobSystemName).Name
  91. : String.Empty;
  92. return notificationDataModel;
  93. }
  94. public Core.Domain.Misc.MailNotification ToMailNotification()
  95. {
  96. return new Core.Domain.Misc.MailNotification
  97. {
  98. Id = this.Id,
  99. CronExpression = this.CronExpression,
  100. NotificationPluginSystemName = this.NotificationPluginSystemName,
  101. NotificationJobSystemName = this.NotificationJobSystemName
  102. };
  103. }
  104. }
  105. }