FoghornService.cs 7.7 KB

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