Esempio n. 1
0
        public int RegisterSendingApplication(SendingApplicationDto sendingApplicationDto,
                                              IEnumerable <NotificationTypeDto> notificationTypeDtos)
        {
            var notificationTypes = new List <NotificationType>();

            foreach (var notificationTypeDto in notificationTypeDtos)
            {
                var notificationType =
                    DataContext.NotificationTypes.FirstOrDefault(
                        x => x.NotificationTypeName == notificationTypeDto.NotificationTypeName);
                if (notificationType == null)
                {
                    notificationType = notificationTypeDto.ToEntity();
                    DataContext.NotificationTypes.Add(notificationType);
                    Logger.Trace("Adding a new NotificationType: {0}, for application: {1}",
                                 notificationTypeDto.NotificationTypeName, sendingApplicationDto.SendingApplicationName);
                }
                notificationType.NotificationTypeIcon        = notificationTypeDto.NotificationTypeIcon;
                notificationType.NotificationTypeDisplayName = notificationTypeDto.NotificationTypeDisplayName;
                notificationTypes.Add(notificationType);
            }

            var sendingApplication =
                DataContext.SendingApplications.FirstOrDefault(
                    x => x.SendingApplicationName == sendingApplicationDto.SendingApplicationName);

            if (sendingApplication == null)
            {
                sendingApplication = sendingApplicationDto.ToEntity();
                Logger.Trace("Adding a new SendingApplication: {0}", sendingApplicationDto.SendingApplicationName);
                DataContext.SendingApplications.Add(sendingApplication);
            }
            else
            {
                sendingApplication.SendingApplicationIcon = sendingApplicationDto.SendingApplicationIcon;
            }

            sendingApplication.NotificationTypes.Clear();
            foreach (var notificationType in notificationTypes)
            {
                sendingApplication.NotificationTypes.Add(notificationType);
            }
            DataContext.SaveChanges();
            return(sendingApplication.SendingApplicationId);
        }
Esempio n. 2
0
        private void DeleteTestApplications(FoghornEntities dataContext)
        {
            var testApplications =
                dataContext.SendingApplications.Where(x => x.SendingApplicationName.StartsWith(ApplicationTestName));

            foreach (var sendingApplication in testApplications)
            {
                var subscribersToRemove = sendingApplication.Subscribers;
                foreach (var subscriber in subscribersToRemove)
                {
                    var notifications = subscriber.NotificationsSent.ToList();
                    foreach (var notification in notifications)
                    {
                        dataContext.Notifications.Remove(notification);
                    }
                }
                dataContext.SendingApplications.Remove(sendingApplication);
            }
            dataContext.SaveChanges();
            _testApplicationId = 0;
        }
Esempio n. 3
0
        public static Notification Notify(NotificationDto notificationDto, string sendingApplicationName)
        {
            var dataContext = new FoghornEntities();

            var sendingApplication =
                dataContext.SendingApplications.FirstOrDefault(x => x.SendingApplicationName == sendingApplicationName);

            if (sendingApplication == null)
            {
                var exception =
                    new ArgumentException("sendingApplicationName must match a previously registered SendingApplication",
                                          "sendingApplicationName");
                Logger.ErrorException(FailureMessage, exception);
                throw exception;
            }

            if (notificationDto == null)
            {
                var exception = new ArgumentException("notification must not be null", "notificationDto");
                Logger.ErrorException(FailureMessage, exception);
                throw exception;
            }

            if (notificationDto.Priority > 2)
            {
                notificationDto.Priority = 2;
            }
            if (notificationDto.Priority < -2)
            {
                notificationDto.Priority = -2;
            }

            var growlNotification = new Growl.Connector.Notification(sendingApplication.SendingApplicationName,
                                                                     notificationDto.NotificationTypeName,
                                                                     notificationDto.NotificationId.ToString(
                                                                         CultureInfo.InvariantCulture),
                                                                     notificationDto.NotificationTitle,
                                                                     notificationDto.NotificationMessage)
            {
                Sticky   = notificationDto.Sticky,
                Priority = (Priority)notificationDto.Priority
            };

            var notification = notificationDto.ToEntity();

            foreach (var subscriber in sendingApplication.Subscribers)
            {
                var port           = subscriber.Port.HasValue ? subscriber.Port.Value : Settings.Default.GrowlDefaultPort;
                var growlConnector = new GrowlConnector(subscriber.Password, subscriber.HostName, port);
                growlConnector.Notify(growlNotification);
                subscriber.NotificationsSent.Add(notification);
            }

            notification.SentDateTime     = DateTime.UtcNow;
            notification.NotificationType =
                dataContext.NotificationTypes.FirstOrDefault(
                    x => x.NotificationTypeName == notificationDto.NotificationTypeName);
            dataContext.Notifications.Add(notification);
            dataContext.SaveChanges();
            return(notification);
        }