Inheritance: NotificationsDataContext
Exemple #1
0
 internal static void DeleteAllMessages()
 {
     using (var context = new DataHandler())
     {
         context.ExecuteCommand("DELETE FROM [Messaging.Messages]");
     }
 }
Exemple #2
0
 internal static IEnumerable<Message> GetAllMessages()
 {
     using (var context = new DataHandler())
     {
         return context.Messages.ToArray();
     }
 }
Exemple #3
0
        public void Save()
        {
            using (var context = new DataHandler())
            {
                var existing = context.Subscriptions.Where(x =>
                    x.ContentPath == this.ContentPath &&
                    x.UserPath == this.UserPath);

                var count = existing.Count();
                if (count == 1)
                {
                    // update
                    var item = existing.First();
                    item.Active = this.Active;
                    item.ContentPath = this.ContentPath;
                    item.Frequency = this.Frequency;
                    item.IsActive = this.IsActive;
                    item.UserEmail = this.UserEmail;
                    item.UserId = this.UserId;
                    item.UserName = this.UserName;
                    item.UserPath = this.UserPath;
                    item.Language = this.Language;
                }
                else
                {
                    if (count > 1)
                        context.Subscriptions.DeleteAllOnSubmit(existing);
                    context.Subscriptions.InsertOnSubmit(this);
                }
                context.SubmitChanges();
            }
        }
 private static void ClearMessages()
 {
     using (var context = new DataHandler())
     {
         context.Messages.DeleteAllOnSubmit(context.Messages);
         context.SubmitChanges();
     }
 }
        private static void InsertMessages(int from, int to)
        {
            using (var context = new DataHandler())
            {
                for (int i = from; i < to; i++)
                {
                    var message = new Message
                    {
                        Address = "*****@*****.**",
                        Body = i.ToString(),
                        Subject = i.ToString()
                    };

                    context.Messages.InsertOnSubmit(message);
                }

                context.SubmitChanges();
            }
        }
        private static void ProcessQueuedMessages()
        {
                try
                {
                    while (true)
                    {
                        using (var context = new DataHandler())
                        {
                            try
                            {
                                var messages = GetMessagesToSend(context);
                                if (messages.Count() == 0)
                                    break;

                                foreach (var message in messages)
                                {
                                    ProcessMessage(message, context);
                                }
                            }
                            finally
                            {
                                context.SubmitChanges();
                            }
                        }
                        Debug.WriteLine("#Notification> End of iteration");
                    }
                }
                catch (Exception exception)
                {
                    Debug.WriteLine("#Notification> Rootlevel Exception:" + exception.Message);
                    Logger.WriteException(exception);
                }
                finally
                {
                    ProcessQueuedMessagesFinished();
                }
        }
Exemple #7
0
 public static void DeleteAllSubscriptions()
 {
     using (var context = new DataHandler())
     {
         context.ExecuteCommand("DELETE FROM [Messaging.Subscriptions]");
     }
 }
Exemple #8
0
        public static void UnSubscribeFrom(Node target)
        {
            using (var context = new DataHandler())
            {
                var existing = context.Subscriptions.Where(
                    x => x.ContentPath == target.Path);

                if (existing.Count() > 0)
                {
                    context.Subscriptions.DeleteAllOnSubmit(existing);
                    context.SubmitChanges();
                }
            }
        }
Exemple #9
0
        public static void UnSubscribeAll(User subscriber)
        {
            using (var context = new DataHandler())
            {
                var existing = context.Subscriptions.Where(
                    x => x.UserPath == subscriber.Path);

                if (existing.Count() > 0)
                {
                    context.Subscriptions.DeleteAllOnSubmit(existing);
                    context.SubmitChanges();
                }
            }
        }
Exemple #10
0
 internal static IEnumerable<Event> GetAllEvents()
 {
     using (var context = new DataHandler())
     {
         return context.Events.ToArray();
     }
 }
Exemple #11
0
 internal static IEnumerable<Subscription> GetActiveSubscriptionsByFrequency(NotificationFrequency frequency)
 {
     using (var context = new DataHandler())
     {
         return context.Subscriptions.Where(x => x.FrequencyId == (int)frequency && x.Active != 0).ToArray();
     }
 }
Exemple #12
0
 public static Subscription GetSubscriptionByUser(string subscriberPath, string contentPath, bool isActive)
 {
     using (var context = new DataHandler())
     {
         return context.Subscriptions.Where(x =>
             x.UserPath == subscriberPath &&
             x.ContentPath == contentPath &&
             x.Active == (byte)(isActive ? 1 : 0)).FirstOrDefault();
     }
 }
Exemple #13
0
 public static int GetCountOfSubscriptions()
 {
     using (var context = new DataHandler())
     {
         return context.Subscriptions.Count();
     }
 }
        internal static void RefreshLock(double timeframe)
        {
            using (var context = new DataHandler())
            {
                var notificationLock = context.Synchronizations.SingleOrDefault(lockItem => lockItem.LockName == NOTIFICATIONLOCKNAME);
                if (notificationLock == null)
                    throw new InvalidOperationException("Could not update the Notification lock, because it doesn't exist. (the lock must be present when RefreshLock is called.)");

                notificationLock.LockedUntil = DateTime.Now.AddMinutes(timeframe);
                context.SubmitChanges();
            }
            Debug.WriteLine("#Notification> ** lock refreshed");
        }
        //-------------------------------------------------

        //TODO: AcquireLock, RefreshLock, Release lock csak a teszteles miatt internal, private jobb lenne
        internal static bool AcquireLock(double timeframe)
        {
            Synchronization notificationLock;
            
            using (var context = new DataHandler())
            {
                notificationLock = context.Synchronizations.SingleOrDefault(lockItem => lockItem.LockName == NOTIFICATIONLOCKNAME);
                if (notificationLock == null)
                    return InsertLock(timeframe);
            }
            
            if (notificationLock.Locked && (notificationLock.LockId != AppDomainId) && notificationLock.LockedUntil > DateTime.Now)
                return false;

            return UpdateLock(notificationLock, timeframe);
        }
 private static IEnumerable<Message> GetMessagesToSend(DataHandler context)
 {
     //var messages = context.Messages.Take(Configuration.TakeCount);
     //return messages;
     var lockId = Guid.NewGuid().ToString(); //TODO: #notif: LockId (machine+domain+thread?)
     if(SignMessagesToSend(lockId))
         return SelectSignedMessages(context, lockId);
     return Message.EmptyMessages;
 }
        private static void ProcessMessage(Message message, DataHandler context)
        {
            if (String.IsNullOrEmpty(message.Address))
            {
                Debug.WriteLine("#Notification> Message is not sent and removed from the queue. Reason: Address is not provided. ");
                Logger.WriteWarning("Message is not sent. Reason: Address is not provided.");
                context.Messages.DeleteOnSubmit(message);
                return;
            }

            var retryCount = Configuration.RetryCount;
            while (true)
            {
                try
                {
                    SendMessage(message);
                    context.Messages.DeleteOnSubmit(message);
                    break;
                }
                catch (SmtpFailedRecipientException exception)
                {
                    SmtpStatusCode statusCode = exception.StatusCode;

                    if ((--retryCount != 0) &&
                        (statusCode == SmtpStatusCode.MailboxBusy || statusCode == SmtpStatusCode.MailboxUnavailable || statusCode == SmtpStatusCode.TransactionFailed))
                    {
                        Debug.WriteLine("#Notification SmtpFailedRecipientException RETRY> " + exception.Message + exception.StatusCode);
                        Logger.WriteException(exception);
                        Thread.Sleep(Configuration.RetryDelay);
                    }
                    else
                    {
                        context.Messages.DeleteOnSubmit(message);
                        Debug.WriteLine("#Notification SmtpFailedRecipientException> " + exception.Message + exception.StatusCode);
                        Debug.WriteLine("#Notification SmtpFailedRecipientException> Message is removed from the queue.");
                        Logger.WriteException(exception);
                        Logger.WriteWarning("Message is removed from queue.");
                        break;
                    }
                }
            }
        }
 internal static IEnumerable<Message> SelectSignedMessages(DataHandler context, string lockId)
 {
     return context.Messages.Where(m => m.LockId == lockId);
 }
Exemple #19
0
 internal static int GetCountOfEvents()
 {
     using (var context = new DataHandler())
     {
         return context.Events.Count();
     }
 }
Exemple #20
0
 private static void SetActivation(User subscriber, Node target, bool value)
 {
     using (var context = new DataHandler())
     {
         var subscription = context.Subscriptions.Where(x =>
             x.UserPath == subscriber.Path &&
             x.ContentPath == target.Path &&
             x.Active == (byte)(value ? 0 : 1)).FirstOrDefault();
         if (subscription == null)
             return;
         subscription.IsActive = value;
         context.SubmitChanges();
     }
 }
Exemple #21
0
 private void Save()
 {
     using (var context = new DataHandler())
     {
         context.Events.InsertOnSubmit(this);
         context.SubmitChanges();
     }
 }
Exemple #22
0
 public static IEnumerable<Subscription> GetAllSubscriptions()
 {
     using (var context = new DataHandler())
     {
         return context.Subscriptions.ToArray();
     }
 }
Exemple #23
0
 internal static void Reset()
 {
     using (var context = new DataHandler())
     {
         context.ExecuteCommand("DELETE FROM [Messaging.LastProcessTime]");
     }
 }
Exemple #24
0
 internal static IEnumerable<Subscription> GetSubscriptionsByUser(string subscriberPath, bool isActive)
 {
     using (var context = new DataHandler())
     {
         return context.Subscriptions.Where(
             x => x.UserPath == subscriberPath && x.Active == (byte)(isActive ? 1 : 0))
             .ToArray();
     }
 }
        private static IEnumerable<Subscription> CollectEventsPerSubscription(NotificationFrequency freq, DateTime now)
        {
            var lastTime = now;
            var subscriptions = Subscription.GetActiveSubscriptionsByFrequency(freq);
            if (subscriptions.Count() == 0)
                return null;
            //var time = GetTimeLimit(freq);
            var time = LastProcessTime.GetLastProcessTime(freq);

            using (var context = new DataHandler())
            {
                var events = (time == DateTime.MinValue)?
                    context.Events.Where(x => x.When <= lastTime) :
                    context.Events.Where(x => x.When > time && x.When <= lastTime);
                foreach (var @event in events.OrderBy(x => x.When))
                    foreach (var subscription in subscriptions)
                        if (IsRelatedPath(@event.ContentPath, subscription.ContentPath))
                            if (HasPermission(subscription, @event))
                                subscription.AddRelatedEvent(@event);
            }
            LastProcessTime.SetLastProcessTime(freq, lastTime);
            return subscriptions;
        }
Exemple #26
0
 public static IEnumerable<Subscription> GetSubscriptionsByContent(string contentPath)
 {
     using (var context = new DataHandler())
     {
         return context.Subscriptions.Where(x => x.ContentPath == contentPath).ToArray();
     }
 }
        private static void GenerateMessages(IEnumerable<Subscription> subscriptions)
        {
            var configs = new Dictionary<string, NotificationConfig>();

            using (var context = new DataHandler())
            {
                foreach (var subscription in subscriptions)
                {
                    // gather notification configs (handler: NotificationConfig.cs) for all related content of subscriptions
                    GatherConfigsForSubscription(subscription, configs);

                    // generate messges for content whose notification are controlled by a notification config
                    var msgs = GenerateMessagesForConfig(subscription, configs);
                    foreach (var message in msgs)
                    {
                        context.Messages.InsertOnSubmit(message);
                    }

                    // generate messages for content whose notification are NOT controlled by a notification config (send a generic notification message)
                    var msg = GenerateMessage(subscription, configs);
                    if (msg != null)
                        context.Messages.InsertOnSubmit(msg);
                }
                context.SubmitChanges();
            }
        }
        internal static void ReleaseLock()
        {
            using (var context = new DataHandler())
            {
                var notificationLock = context.Synchronizations.SingleOrDefault(lockItem => lockItem.LockName == NOTIFICATIONLOCKNAME);
                if (notificationLock == null)
                    throw new Exception(); //TODO: #notifB: ?? what is this?

                notificationLock.Locked = false;
                context.SubmitChanges();
            }
            Debug.WriteLine("#Notification> ** lock released");
        }
Exemple #29
0
 internal static void DeleteOldEvents(DateTime now)
 {
     using (var context = new DataHandler())
     {
         context.ExecuteCommand(String.Format("DELETE FROM [Messaging.Events] WHERE [When] < '{0}'", now.AddMonths(-2).ToString("yyyy-MM-dd HH:mm:ss")));
     }
 }
        private static bool InsertLock(double timeframe)
        {
            using (var context = new DataHandler())
            {
                var success = false;
                var notifLock = new Synchronization
                                    {
                                        LockName = NOTIFICATIONLOCKNAME,
                                        Locked = true,
                                        ComputerName = Environment.MachineName,
                                        LockedUntil =
                                            DateTime.Now.AddMinutes(timeframe),
                                        LockId = AppDomainId
                                    };
                try
                {
                    context.Synchronizations.InsertOnSubmit(notifLock);
                    context.SubmitChanges();
                    success = true;
                }
                catch (Exception ex)
                {
                    Logger.WriteException(ex);
                }

                return success;
            }
        }
 internal static IEnumerable <Message> SelectSignedMessages(DataHandler context, string lockId)
 {
     return(context.Messages.Where(m => m.LockId == lockId));
 }