Notifier.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // <copyright file="Notifier.cs" company="Objective Advantage Europe Ltd.">
  2. //
  3. // Copyright (c) Objective Advantage Europe Ltd. 2011 - 2013 All Right Reserved.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. // </copyright>
  18. using System;
  19. using System.Globalization;
  20. using System.Linq;
  21. using Foghorn.Core;
  22. using Foghorn.WcfService.Properties;
  23. using Growl.Connector;
  24. using NLog;
  25. using Notification = Foghorn.Core.Notification;
  26. namespace Foghorn.WcfService
  27. {
  28. public class Notifier
  29. {
  30. private const string FailureMessage = "Growl Notifier had a problem.";
  31. private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
  32. public static Notification Notify(NotificationDto notificationDto, string sendingApplicationName)
  33. {
  34. var dataContext = new FoghornEntities();
  35. var sendingApplication =
  36. dataContext.SendingApplications.FirstOrDefault(x => x.SendingApplicationName == sendingApplicationName);
  37. if (sendingApplication == null)
  38. {
  39. var exception =
  40. new ArgumentException("sendingApplicationName must match a previously registered SendingApplication",
  41. "sendingApplicationName");
  42. Logger.ErrorException(FailureMessage, exception);
  43. throw exception;
  44. }
  45. if (notificationDto == null)
  46. {
  47. var exception = new ArgumentException("notification must not be null", "notificationDto");
  48. Logger.ErrorException(FailureMessage, exception);
  49. throw exception;
  50. }
  51. if (notificationDto.Priority > 2) notificationDto.Priority = 2;
  52. if (notificationDto.Priority < -2) notificationDto.Priority = -2;
  53. var growlNotification = new Growl.Connector.Notification(sendingApplication.SendingApplicationName,
  54. notificationDto.NotificationTypeName,
  55. notificationDto.NotificationId.ToString(
  56. CultureInfo.InvariantCulture),
  57. notificationDto.NotificationTitle,
  58. notificationDto.NotificationMessage)
  59. {
  60. Sticky = notificationDto.Sticky,
  61. Priority = (Priority) notificationDto.Priority
  62. };
  63. var notification = notificationDto.ToEntity();
  64. foreach (var subscriber in sendingApplication.Subscribers)
  65. {
  66. var port = subscriber.Port.HasValue ? subscriber.Port.Value : Settings.Default.GrowlDefaultPort;
  67. var growlConnector = new GrowlConnector(subscriber.Password, subscriber.HostName, port);
  68. growlConnector.Notify(growlNotification);
  69. subscriber.NotificationsSent.Add(notification);
  70. }
  71. notification.SentDateTime = DateTime.UtcNow;
  72. notification.NotificationType =
  73. dataContext.NotificationTypes.FirstOrDefault(
  74. x => x.NotificationTypeName == notificationDto.NotificationTypeName);
  75. dataContext.Notifications.Add(notification);
  76. dataContext.SaveChanges();
  77. return notification;
  78. }
  79. }
  80. }