예제 #1
0
        public List <Alert> GenerateProfileAlerts(Profile profile)
        {
            InitNonExistingRepo();
            //Alle items uit profile subscriptions halen
            if (profile == null)
            {
                throw new Exception("U heeft nog geen account geselecteerd, gelieve er eerst een te kiezen");
            }
            if (profile.Subscriptions == null || profile.Subscriptions.Count == 0)
            {
                throw new Exception(
                          "U heeft nog geen subscriptions toegevoegd aan uw account, gelieve er eerst enkele te kiezen");
            }
            List <Item> subscribedItems = profile.Subscriptions;

            //Items opdelen in Subklasses [Person, Organisation, Theme]
            List <Person>       people        = subscribedItems.Where(i => i is Person).ToList().Select(i => (Person)i).ToList();
            List <Organisation> organisations = new List <Organisation>(); // Alerts op organisaties;
            List <Theme>        themes        = new List <Theme>();        // Alerts op thema's

            /*
             * Person Alerts
             */
            //Print all subscribed items
            Console.WriteLine("========= SUBSCRIBED =========");
            subscribedItems.ForEach(Console.WriteLine);

            //Check trends voor people
            List <Alert> alerts = Trendspotter.GenerateAllAlertTypes(profile.Subscriptions);


            /*
             * Linking Alerts
             */
            //Replace generated alerts with existing alerts
            AlertRepo.ReadAlerts().ToList().ForEach(a =>
            {
                Alert alert = alerts.FirstOrDefault(al => al.Equals(a));
                if (alert != null)
                {
                    alerts.Remove(a);
                }
            });

            //Link alerts aan profile
            List <Alert> alertsToCreate = new List <Alert>();
            List <Alert> alertsToUpdate = new List <Alert>();

            Console.WriteLine("========= NIEUWE ALERTS ========");
            alerts.ForEach(a =>
            {
                ProfileAlert profileAlert = new ProfileAlert
                {
                    AlertId   = a.AlertId,
                    Alert     = a,
                    UserId    = profile.Id,
                    Profile   = profile,
                    IsRead    = false,
                    TimeStamp = DateTime.Now,
                    WeeklyReviewsProfileAlerts = new List <WeeklyReviewProfileAlert>()
                };

                if (!a.ProfileAlerts.Contains(profileAlert) && !profile.ProfileAlerts.Contains(profileAlert))
                {
                    a.ProfileAlerts.Add(profileAlert);
                    profile.ProfileAlerts.Add(profileAlert);
                    Console.WriteLine(a);

                    if (a.AlertId == 0)
                    {
                        alertsToCreate.Add(a);
                    }
                    else
                    {
                        alertsToUpdate.Add(a);
                    }
                }
            });

            //Persist alerts
            AlertRepo.CreatAlerts(alertsToCreate).ToList();
            alertsToUpdate.ForEach(AlertRepo.UpdateAlert);

            // Save all pending changes
            UowManager.Save();

            return(alerts);
        }
예제 #2
0
        public async Task <List <Alert> > GenerateAllAlertsAsync(IEnumerable <Item> allItems)
        {
            InitNonExistingRepo();

            //Create alerts list
            List <Alert> alerts = new List <Alert>();

            if (allItems.Count() > 0)
            {
                // Update trending items
                List <Item> itemsToUpdate = Trendspotter.CheckTrendingItems(allItems.ToList(), 10, ref alerts);

                // Get all profiles with subscriptions
                List <Profile> Profiles = ProfileRepo.ReadProfiles().Where(p =>
                                                                           p.Subscriptions.Count > 0 &&
                                                                           p.Settings.Find(us => us.SettingName.Equals(Setting.Account.WANTS_SITE_NOTIFICATIONS)).boolValue
                                                                           ).ToList();

                // Alle subscriptions uit profiles halen
                List <Item> Subscriptions = Profiles.SelectMany(p => p.Subscriptions).Distinct().ToList();

                // Check trends voor people
                alerts.AddRange(Trendspotter.GenerateAllAlertTypes(Subscriptions));

                //Replace generated alerts with existing alerts
                AlertRepo.ReadAlerts().ToList().ForEach(a =>
                {
                    Alert alert = alerts.FirstOrDefault(al => al.Equals(a));
                    if (alert != null)
                    {
                        alerts[alerts.IndexOf(alert)] = a;
                    }
                });

                //Link alerts aan profile
                List <Alert> alertsToCreate = new List <Alert>();
                List <Alert> alertsToUpdate = new List <Alert>();

                alerts.ForEach(alert =>
                {
                    bool changed = false;
                    Profiles.ForEach(profile =>
                    {
                        if (profile.Subscriptions.Contains(alert.Item))
                        {
                            ProfileAlert profileAlert = new ProfileAlert
                            {
                                AlertId   = alert.AlertId,
                                Alert     = alert,
                                UserId    = profile.Id,
                                Profile   = profile,
                                IsRead    = false,
                                TimeStamp = DateTime.Now,
                                WeeklyReviewsProfileAlerts = new List <WeeklyReviewProfileAlert>()
                            };

                            if (!alert.ProfileAlerts.Contains(profileAlert) &&
                                !profile.ProfileAlerts.Contains(profileAlert))
                            {
                                changed = true;
                                alert.ProfileAlerts.Add(profileAlert);
                                profile.ProfileAlerts.Add(profileAlert);
                                Console.WriteLine(alert);
                            }
                        }
                    });

                    if (alert.AlertId == 0)
                    {
                        alertsToCreate.Add(alert);
                    }
                    else if (changed)
                    {
                        alertsToUpdate.Add(alert);
                    }
                });

                //Persist alerts
                AlertRepo.CreatAlerts(alertsToCreate).ToList();
                alertsToUpdate.ForEach(AlertRepo.UpdateAlert);
            }
            await UowManager.SaveAsync();

            return(alerts);
        }