FoghornService.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // <copyright file="FoghornService.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.Collections.Generic;
  20. using System.Diagnostics;
  21. using System.Linq;
  22. using Foghorn.Core;
  23. using Foghorn.WcfService.Properties;
  24. using Growl.Connector;
  25. using Growl.CoreLibrary;
  26. using NLog;
  27. using NotificationType = Foghorn.Core.NotificationType;
  28. namespace Foghorn.WcfService
  29. {
  30. public class FoghornService : IFoghornService
  31. {
  32. private const string FailureMessage = "Something went wrong receiving a service call.";
  33. private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
  34. private static readonly FoghornEntities DataContext = new FoghornEntities();
  35. #region IFoghornService Members
  36. public int RegisterSendingApplication(SendingApplicationDto sendingApplicationDto,
  37. IEnumerable<NotificationTypeDto> notificationTypeDtos)
  38. {
  39. var notificationTypes = new List<NotificationType>();
  40. foreach (var notificationTypeDto in notificationTypeDtos)
  41. {
  42. var notificationType =
  43. DataContext.NotificationTypes.FirstOrDefault(
  44. x => x.NotificationTypeName == notificationTypeDto.NotificationTypeName);
  45. if (notificationType == null)
  46. {
  47. notificationType = notificationTypeDto.ToEntity();
  48. DataContext.NotificationTypes.Add(notificationType);
  49. Logger.Trace("Adding a new NotificationType: {0}, for application: {1}",
  50. notificationTypeDto.NotificationTypeName, sendingApplicationDto.SendingApplicationName);
  51. }
  52. notificationType.NotificationTypeIcon = notificationTypeDto.NotificationTypeIcon;
  53. notificationType.NotificationTypeDisplayName = notificationTypeDto.NotificationTypeDisplayName;
  54. notificationTypes.Add(notificationType);
  55. }
  56. var sendingApplication =
  57. DataContext.SendingApplications.FirstOrDefault(
  58. x => x.SendingApplicationName == sendingApplicationDto.SendingApplicationName);
  59. if (sendingApplication == null)
  60. {
  61. sendingApplication = sendingApplicationDto.ToEntity();
  62. Logger.Trace("Adding a new SendingApplication: {0}", sendingApplicationDto.SendingApplicationName);
  63. DataContext.SendingApplications.Add(sendingApplication);
  64. }
  65. else
  66. {
  67. sendingApplication.SendingApplicationIcon = sendingApplicationDto.SendingApplicationIcon;
  68. }
  69. sendingApplication.NotificationTypes.Clear();
  70. foreach (var notificationType in notificationTypes)
  71. {
  72. sendingApplication.NotificationTypes.Add(notificationType);
  73. }
  74. DataContext.SaveChanges();
  75. return sendingApplication.SendingApplicationId;
  76. }
  77. public Guid RegisterSubscription(SubscriberDto subscriberDto, string sendingApplicationName)
  78. {
  79. var sendingApplication =
  80. DataContext.SendingApplications.FirstOrDefault(x => x.SendingApplicationName == sendingApplicationName);
  81. if (sendingApplication == null)
  82. {
  83. var exception =
  84. new Exception("Cannot register a subscription for a SendingApplication that is not registered.");
  85. Logger.ErrorException(FailureMessage, exception);
  86. throw exception;
  87. }
  88. Subscriber subscriber;
  89. if (subscriberDto.SubscriberId != Guid.Empty)
  90. {
  91. subscriber = DataContext.Subscribers.FirstOrDefault(x => x.SubscriberId == subscriberDto.SubscriberId);
  92. }
  93. else
  94. {
  95. subscriber = subscriberDto.ToEntity();
  96. subscriber.SubscriberId = Guid.NewGuid();
  97. DataContext.Subscribers.Add(subscriber);
  98. }
  99. var port = subscriberDto.Port.HasValue ? subscriberDto.Port.Value : Settings.Default.GrowlDefaultPort;
  100. var growlConnector = new GrowlConnector(subscriberDto.Password, subscriberDto.HostName, port);
  101. var growlNotificationTypes = new List<Growl.Connector.NotificationType>();
  102. foreach (var notificationType in sendingApplication.NotificationTypes)
  103. {
  104. var growlNotificationType = new Growl.Connector.NotificationType(notificationType.NotificationTypeName,
  105. notificationType.NotificationTypeDisplayName);
  106. if (notificationType.NotificationTypeIcon != null && notificationType.NotificationTypeIcon.Length > 0)
  107. {
  108. growlNotificationType.Icon = new BinaryData(notificationType.NotificationTypeIcon);
  109. }
  110. growlNotificationTypes.Add(growlNotificationType);
  111. }
  112. var growlApplication = new Application(sendingApplicationName);
  113. if (sendingApplication.SendingApplicationIcon != null && sendingApplication.SendingApplicationIcon.Length > 0)
  114. {
  115. growlApplication.Icon = new BinaryData(sendingApplication.SendingApplicationIcon);
  116. }
  117. growlConnector.Register(growlApplication, growlNotificationTypes.ToArray());
  118. if (sendingApplication.Subscribers.Contains(subscriber)) return Guid.Empty;
  119. Debug.Assert(subscriber != null, "subscriber != null");
  120. subscriber.SendingApplication = sendingApplication;
  121. DataContext.SaveChanges();
  122. return subscriber.SubscriberId;
  123. }
  124. public NotificationDto Notify(NotificationDto notificationDto, string sendingApplicationName)
  125. {
  126. return Notifier.Notify(notificationDto, sendingApplicationName).ToDto();
  127. }
  128. public ICollection<SendingApplicationDto> GetRegisteredApplications()
  129. {
  130. return DataContext.SendingApplications.ToDtos();
  131. }
  132. public ICollection<SubscriberDto> GetRegisteredSubscribers()
  133. {
  134. return DataContext.Subscribers.ToDtos();
  135. }
  136. public ICollection<NotificationDto> GetNotifications(string subscriberId)
  137. {
  138. var subscriberIdGuid = new Guid(subscriberId);
  139. var subscriber = DataContext.Subscribers.FirstOrDefault(x => x.SubscriberId == subscriberIdGuid);
  140. return subscriber != null ? subscriber.NotificationsSent.ToDtos() : new List<NotificationDto>();
  141. }
  142. public SendingApplicationDto GetSendingApplication(string sendingApplicationName)
  143. {
  144. return
  145. DataContext.SendingApplications.FirstOrDefault(x => x.SendingApplicationName == sendingApplicationName)
  146. .ToDto();
  147. }
  148. #endregion
  149. }
  150. }