MailNotificationDataModel.cs 4.4 KB

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