MailNotificationDataModel.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 => new
  73. {
  74. LastName = r.Lastname,
  75. Roles = String.Join(", ", r.Roles.Select(u => u.Description))
  76. })
  77. .OrderBy(r => r.Roles)
  78. .Select(r => String.Format("{0}|({1})", r.LastName, r.Roles))
  79. .ToList(),
  80. Users =
  81. mailNotificationEntity.Users
  82. .Select(r => UserDataModel.FromUser(r, false))
  83. .ToList(),
  84. };
  85. notificationDataModel.NotificationPluginSystemNameDescription =
  86. notificationPlugin == null
  87. ? String.Empty
  88. : notificationPlugin.Name;
  89. notificationDataModel.NotificationJobSystemNameDescription =
  90. notificationPlugin == null
  91. ? String.Empty
  92. : notificationPlugin.AvailableNotificationJobs
  93. .Any(j => j.SystemName == notificationDataModel.NotificationJobSystemName)
  94. ? notificationPlugin.AvailableNotificationJobs
  95. .First(j => j.SystemName == notificationDataModel.NotificationJobSystemName).Name
  96. : String.Empty;
  97. return notificationDataModel;
  98. }
  99. public Core.Domain.Misc.MailNotification ToMailNotification()
  100. {
  101. return new Core.Domain.Misc.MailNotification
  102. {
  103. Id = this.Id,
  104. CronExpression = this.CronExpression,
  105. NotificationPluginSystemName = this.NotificationPluginSystemName,
  106. NotificationJobSystemName = this.NotificationJobSystemName
  107. };
  108. }
  109. }
  110. }