Ejemplo n.º 1
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();
            }
        }
Ejemplo n.º 2
0
        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();
            }
        }
Ejemplo n.º 3
0
        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.UtcNow.AddMinutes(timeframe),
                    LockId = AppDomainId
                };
                try
                {
                    context.Synchronizations.InsertOnSubmit(notifLock);
                    context.SubmitChanges();
                    success = true;
                }
                catch (Exception ex)
                {
                    Logger.WriteException(ex);
                }

                return(success);
            }
        }
Ejemplo n.º 4
0
 private void Save()
 {
     using (var context = new DataHandler())
     {
         context.Events.InsertOnSubmit(this);
         context.SubmitChanges();
     }
 }
Ejemplo n.º 5
0
 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");
 }
Ejemplo n.º 6
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();
                }
            }
        }
Ejemplo n.º 7
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();
                }
            }
        }
Ejemplo n.º 8
0
        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.UtcNow.AddMinutes(timeframe);
                context.SubmitChanges();
            }
            Debug.WriteLine("#Notification> ** lock refreshed");
        }
Ejemplo n.º 9
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();
     }
 }
Ejemplo n.º 10
0
        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();
            }
        }
Ejemplo n.º 11
0
        internal static void SetLastProcessTime(NotificationFrequency freq, DateTime lastTime)
        {
            using (var context = new DataHandler())
            {
                var existingEntry = context.LastProcessTimes.FirstOrDefault();
                if (existingEntry == null)
                {
                    context.LastProcessTimes.InsertOnSubmit(GetDefaultInstance(freq, lastTime));
                }
                else
                {
                    SetValue(existingEntry, freq, lastTime);
                }
                context.SubmitChanges();
            }
            switch (freq)
            {
            case NotificationFrequency.Daily: _nextDaily = null; break;

            case NotificationFrequency.Weekly: _nextWeekly = null; break;

            case NotificationFrequency.Monthly: _nextMonthly = null; break;
            }
        }
Ejemplo n.º 12
0
        private static void GenerateMessages(Dictionary <int, List <Subscription> > groupedSubscriptions)
        {
            var configs = new Dictionary <string, NotificationConfig>();

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

                    var msg = GenerateMessage(item.Value, configs);
                    if (msg != null)
                    {
                        context.Messages.InsertOnSubmit(msg);
                    }

                    context.SubmitChanges();
                }
            }
        }