/// <summary> /// Gives back all the widgets for monitoring activities /// </summary> public IEnumerable <Widget> GetWidgetsForActivities(int platformId) { InitRepo(); //Get widgets IEnumerable <Widget> widgets = widgetRepo.ReadActivityWidgets().AsEnumerable(); if (widgets == null || widgets.Count() == 0) { return(widgets); } //Check lastUpdated DateTime?lastUpdated = new SubplatformManager().GetSubPlatform(platformId).LastUpdatedActivities; //If lastUpdated was to long ago, then the activities shall be udpated if (lastUpdated == null || !lastUpdated.Value.ToString("dd-MM-yy").Equals(DateTime.Now.ToString("dd-MM-yy"))) { widgets = ChangeWidgetActities(widgets, platformId); } //Link widdatas to widgets foreach (Widget widget in widgets) { widget.WidgetDatas.ToList().AddRange(GetWidgetDatasForWidgetId(widget.WidgetId)); } return(widgets); }
/// <summary> /// Updates the subplatform of a given item /// </summary> public Item ChangeItemPlatform(int itemId, int subplatformId) { uowManager = new UnitOfWorkManager(); InitRepo(); //Get subplatform SubPlatform platform = new SubplatformManager(uowManager).GetSubPlatform(subplatformId); if (platform == null) { return(null); } //Get item Item itemToUpdate = GetItemWithSubPlatform(itemId); if (itemToUpdate == null) { return(null); } //Update item itemToUpdate.SubPlatform = platform; //Update database itemRepo.UpdateItem(itemToUpdate); uowManager.Save(); uowManager = null; return(itemToUpdate); }
/// <summary> /// If the last time that the old trending percentage of the item was updated 7 days ago, /// then the old trending percentage will be updated. /// </summary> public void RefreshItemData(int platformId) { InitRepo(); //Get timestamp DateTime?lastUpdated = new SubplatformManager().GetSubPlatform(platformId).LastUpdatedWeeklyReview; //Get all items IEnumerable <Item> items = itemRepo.ReadAllItemsWithPlatforms(); if (items == null || items.Count() == 0) { return; } //Refresh old data if (lastUpdated == null || lastUpdated < DateTime.Now.AddDays(-7)) { foreach (Item item in items) { item.TrendingPercentageOld = item.TrendingPercentage; } } //Update database itemRepo.UpdateItems(items); }
/// <summary> /// Checks if organisations used in json already exist, if not they will be made. /// </summary> private void CheckOrganisations(string json, int subPlatformID) { uowManager = new UnitOfWorkManager(); InitRepo(); //Get subplatform SubPlatform platform = new SubplatformManager(uowManager).GetSubPlatform(subPlatformID); if (platform == null) { return; } //Extract organisations out of the JSON dynamic deserializedJson = JsonConvert.DeserializeObject(json); List <Item> organisations = new List <Item>(); for (int i = 0; i < deserializedJson.Count; i++) { string name = deserializedJson[i].organisation; Item organisation = itemRepo.ReadOrganisation(name); if (organisation == null) { organisation = new Organisation() { ItemType = ItemType.Organisation, Name = name, CreationDate = DateTime.Now, LastUpdatedInfo = DateTime.Now, NumberOfFollowers = 0, TrendingPercentage = 0.0, Baseline = 10.0, Informations = new List <Information>(), SocialMediaUrls = new List <SocialMediaName>(), SubPlatform = platform }; itemRepo.CreateItem(organisation); uowManager.Save(); organisations.Add(organisation); } } //Generate default widgets for items foreach (Item item in organisations) { GenerateDefaultItemWidgets(item.Name, item.ItemId); } uowManager = null; }
/// <summary> /// Gives back all the widgetdata for monitoring the /// registerd users per day /// </summary> public WidgetData GetUserActivitiesData(ActivityType type, DateTime?timestamp = null) { InitRepo(); //Create widgetdata WidgetData widgetData = new WidgetData() { GraphValues = new List <GraphValue>(), KeyValue = "activity" }; //Check timestamp if (timestamp == null) { timestamp = DateTime.Now.AddDays(-30); } //Get actitivies IEnumerable <UserActivity> activities = new SubplatformManager().GetUserActivities(type, timestamp); if (activities == null || activities.Count() == 0) { return(widgetData); } int count = activities.Count(); //Query data DateTime startdate = DateTime.Now; UserActivity activity; while (timestamp <= startdate) { GraphValue graphValue = new GraphValue() { NumberOfTimes = 0, Value = startdate.ToString("dd-MM") }; activity = activities.Where(act => act.TimeStamp.ToString("dd-MM-yy").Equals(startdate.ToString("dd-MM-yy"))).SingleOrDefault(); if (activity != null) { graphValue.NumberOfTimes = activity.NumberOfTimes; } startdate = startdate.AddDays(-1); widgetData.GraphValues.Add(graphValue); } return(widgetData); }
/// <summary> /// Forces an update for the activity widgets /// </summary> public IEnumerable <Widget> ChangeWidgetActities(IEnumerable <Widget> widgets, int platformId) { //** Create new widgetDatas **// DataManager dataManager = new DataManager(); //1st widget WidgetData loginData = dataManager.GetUserActivitiesData(ActivityType.LoginActivity, DateTime.Now.AddDays(-30)); Widget loginWidget = widgets.Where(widget => widget.PropertyTags.All(tag => tag.Name.ToLower().Contains("login"))).SingleOrDefault(); loginWidget.WidgetDatas = new List <WidgetData> { loginData }; loginData.Widget = loginWidget; widgetRepo.CreateWidgetData(loginData); //2nd widget WidgetData registerData = dataManager.GetUserActivitiesData(ActivityType.RegisterActivity, DateTime.Now.AddDays(-30)); Widget registerWidget = widgets.Where(widget => widget.PropertyTags.All(tag => tag.Name.ToLower().Contains("register"))).SingleOrDefault(); loginWidget.WidgetDatas = new List <WidgetData> { loginData }; registerData.Widget = registerWidget; widgetRepo.CreateWidgetData(registerData); //3rd widget WidgetData visitData = dataManager.GetUserActivitiesData(ActivityType.VisitActitiy, DateTime.Now.AddDays(-30)); Widget visitWidget = widgets.Where(widget => widget.PropertyTags.All(tag => tag.Name.ToLower().Contains("visit"))).SingleOrDefault(); loginWidget.WidgetDatas = new List <WidgetData> { loginData }; visitData.Widget = visitWidget; widgetRepo.CreateWidgetData(visitData); //Get last updated SubplatformManager platformManager = new SubplatformManager(); SubPlatform platform = platformManager.GetSubPlatform(platformId); platform.LastUpdatedActivities = DateTime.Now; platformManager.ChangeSubplatform(platform); return(widgets); }
/// <summary> /// Generate alerts for the weekly review /// </summary> public bool GenerateAlertsForWeeklyReview(int platformId) { InitRepo(); //Get timepstamp for weekly review SubplatformManager platformManager = new SubplatformManager(); SubPlatform platform = platformManager.GetSubPlatform(platformId); if (platform.LastUpdatedWeeklyReview != null && platform.LastUpdatedWeeklyReview > DateTime.Now.AddDays(-7)) { return(false); } platform.LastUpdatedWeeklyReview = DateTime.Now; platformManager.ChangeSubplatform(platform); //Get all users IEnumerable <User> users = userRepo.ReadAllUsersWithAlerts(); if (users == null || users.Count() == 0) { return(false); } //Generate weekly review alerts foreach (User user in users) { UserAlert alert = new UserAlert() { User = user, Subject = "Nieuwe Weekly Review", IsRead = false, TimeStamp = DateTime.Now, AlertType = AlertType.Weekly_Review }; user.Alerts.Add(alert); } //Update database & send emails SendWeeklyReviewEmails(platformId, users.Where(user => user.AlertsViaEmail)); userRepo.UpdateUsers(users); return(true); }
/// <summary> /// Reads json and makes item objects which will be saved afterwards into the database. /// </summary> private bool AddItemsFromJson(string json, int subPlatformID) { uowManager = new UnitOfWorkManager(); InitRepo(); dynamic deserializedJson = JsonConvert.DeserializeObject(json); UserManager userManager = new UserManager(uowManager); DataManager dataManager = new DataManager(uowManager); SubPlatform subPlatform = new SubplatformManager(uowManager).GetSubPlatform(subPlatformID); //Needs to be in memory to gain preformance IEnumerable <Area> areas = userManager.GetAreas(); IEnumerable <Source> sources = dataManager.GetAllSources(); IEnumerable <Item> organisations = GetAllOrganisations(); IEnumerable <Item> persons = GetAllPersons(); List <Item> items = new List <Item>(); //Extract perons out of the JSON for (int i = 0; i < deserializedJson.Count; i++) { string fullname = deserializedJson[i].full_name; if (persons.Where(person => person.SubPlatform.SubPlatformId == subPlatformID) .Where(x => x.Name.Equals(fullname)).SingleOrDefault() == null) { string gender = deserializedJson[i].gender; string postalCode = deserializedJson[i].postal_code; string organisation = deserializedJson[i].organisation; string twitter = deserializedJson[i].twitter; string facebook = deserializedJson[i].facebook; string stringDate = Convert.ToString(deserializedJson[i].dateOfBirth); string town = deserializedJson[i].town; string level = deserializedJson[i].level; string site = deserializedJson[i].site; string district = deserializedJson[i].district; string position = deserializedJson[i].position; Gender personGender = (gender == "M") ? Gender.Man : Gender.Woman; DateTime?dateOfBirth = DateTime.ParseExact(stringDate, "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture); Person person = (Person)AddItem(itemType: ItemType.Person, name: fullname, gender: personGender, district: district, level: level, site: site, position: position, dateOfBirth: dateOfBirth); person.SubPlatform = subPlatform; person.Area = areas.Where(area => area.PostalCode.Equals(postalCode) && area.Residence.ToLower().Equals(town.ToLower())).SingleOrDefault(); if (!string.IsNullOrEmpty(twitter)) { SocialMediaName twitterSocial = new SocialMediaName() { Username = twitter, Source = sources.Where(src => src.Name.Equals("Twitter")).SingleOrDefault() }; person.SocialMediaNames.Add(twitterSocial); } if (!string.IsNullOrEmpty(facebook)) { SocialMediaName facebookSocial = new SocialMediaName() { Username = facebook, Source = sources.Where(src => src.Name.Equals("Facebook")).SingleOrDefault() }; person.SocialMediaNames.Add(facebookSocial); } person.Organisation = (Organisation)organisations.Where(org => org.Name.Equals(organisation)).SingleOrDefault(); items.Add(person); } } //Save items to the database if (items.Count > 0) { itemRepo.CreateItems(items); uowManager.Save(); foreach (Item item in items) { GenerateDefaultItemWidgets(item.Name, item.ItemId); } uowManager = null; return(true); } else { return(false); } }
/// <summary> /// Import all the themes from the json file /// </summary> public bool ImportThemes(string json, int subPlatformID) { uowManager = new UnitOfWorkManager(); InitRepo(); //Get platform SubPlatform platform = new SubplatformManager(uowManager).GetSubPlatform(subPlatformID); if (platform == null) { return(false); } //Get themes IEnumerable <Item> themes = GetAllThemes(); if (themes == null) { return(false); } List <Item> newThemes = new List <Item>(); dynamic deserializedJson = JsonConvert.DeserializeObject(json); //Extract themes from JSON for (int i = 0; i < deserializedJson.Count; i++) { string name = deserializedJson[i].name; Theme theme = null; List <Keyword> keywords = new List <Keyword>(); for (int j = 0; j < deserializedJson[i].keywords.Count; j++) { keywords.Add(new Keyword() { Name = deserializedJson[i].keywords[j] }); } //Create new theme if (themes .Where(t => t.SubPlatform.SubPlatformId == subPlatformID) .Where(t => t.Name.Equals(name)).SingleOrDefault() == null) { theme = new Theme() { ItemType = ItemType.Theme, Name = name, CreationDate = DateTime.Now, LastUpdatedInfo = DateTime.Now, NumberOfFollowers = 0, TrendingPercentage = 0.0, Baseline = 0.0, Informations = new List <Information>(), SubPlatform = platform, Keywords = keywords }; itemRepo.CreateItem(theme); uowManager.Save(); newThemes.Add(theme); } } //Generate default widgets for items foreach (Item item in newThemes) { GenerateDefaultItemWidgets(item.Name, item.ItemId); } uowManager = null; return(true); }