/// <summary> /// Creates a new dashboard based on the user. /// /// NOTE /// THIS METHOD USES UNIT OF WORK /// </summary> public Dashboard AddDashboard(string userId = "-1", DashboardType dashType = DashboardType.Private) { uowManager = new UnitOfWorkManager(); InitRepo(); //Create dashboard Dashboard dashboard = new Dashboard() { DashboardType = dashType, Widgets = new List <UserWidget>(), }; //Get user if (!userId.Equals("-1")) { UserManager userManager = new UserManager(uowManager); User user = userManager.GetUser(userId); if (user == null) { return(null); } else { dashboard.User = user; } } //Create database widgetRepo.CreateDashboard(dashboard); uowManager.Save(); uowManager = null; return(dashboard); }
/// <summary> /// Creates a question in the database /// /// WARNING /// THIS METHOD USES UNIT OF WORK /// /// NOTE /// Unit of work is not realy needed because we work with one context /// Its added just for safety. /// </summary> public Question AddQuestion(int platformId, QuestionType type, string title, string anwser) { uowManager = new UnitOfWorkManager(); InitRepo(); //Get Subplatform SubPlatform platform = GetSubPlatform(platformId); if (platform == null) { return(null); } //Create question Question question = new Question() { QuestionType = type, Title = title, Answer = anwser, SubPlatform = platform }; //Create question in database platformRepo.CreateQuestion(question); uowManager.Save(); uowManager = null; return(question); }
/// <summary> /// Changes the basic information of a specific user /// /// NOTE: This method changes the following things: /// - firstname /// - lastname /// - gender /// - date of birth /// - area /// </summary> public User ChangeUserBasicInfo(string userId, string firstname, string lastname, Gender gender, DateTime dateOfBirth, Area area) { uowManager = new UnitOfWorkManager(); InitRepo(); //Get User User userToUpdate = userRepo.ReadUser(userId); if (userToUpdate == null) { return(null); } //Change user userToUpdate.FirstName = firstname; userToUpdate.LastName = lastname; userToUpdate.Gender = gender; userToUpdate.DateOfBirth = dateOfBirth; userToUpdate.Area = area; //Update database userRepo.UpdateUser(userToUpdate); uowManager.Save(); return(userToUpdate); }
/// <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> /// Updates the organisation of a given person /// </summary> public Person ChangePersonOrganisation(int personId, int organisationId) { uowManager = new UnitOfWorkManager(); InitRepo(); //Get organisation Organisation org = GetOrganisationWithDetails(organisationId); if (org == null) { return(null); } //Get person Person personToUpdate = GetPersonWithDetails(personId); if (personToUpdate == null) { return(null); } //Update item personToUpdate.Organisation = org; //Update database itemRepo.UpdatePerson(personToUpdate); uowManager.Save(); uowManager = null; return(personToUpdate); }
/// <summary> /// Creates a new subscription for a specific user and item /// with a given threshold. /// /// NOTE /// THIS METHOD USES UNIT OF WORK /// </summary> public Subscription CreateSubscription(string userId, int itemId, int threshold = 10) { uowManager = new UnitOfWorkManager(); InitRepo(); //get user IUserManager userManager = new UserManager(uowManager); User user = userManager.GetUser(userId); if (user == null) { return(null); } //get item IItemManager itemManager = new ItemManager(uowManager); Item item = itemManager.GetItem(itemId); if (item == null) { return(null); } //Check if sub already exists //if the counter is 0 then a new subscrption will not be created. IEnumerable <Subscription> subs = GetSubscriptionsWithItemsForUser(userId); if (subs.Where(sub => sub.SubscribedItem.ItemId == itemId).Count() != 0) { return(null); } //make subscription Subscription subscription = new Subscription() { SubscribedUser = user, SubscribedItem = item, Threshold = threshold, DateSubscribed = DateTime.Now, Alerts = new List <SubAlert>() }; item.NumberOfFollowers++; //Save changes to the database subRepo.CreateSubscription(subscription); uowManager.Save(); uowManager = null; return(subscription); }
/// <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> /// Updates the details of the widget. /// </summary> public Widget ChangeWidgetDetails(int widgetId, int rowNbr, int colNbr, List <int> itemIds, int rowspan = 1, int colspan = 1, GraphType graphType = 0) { uowManager = new UnitOfWorkManager(); InitRepo(); ItemManager itemManager = new ItemManager(uowManager); //Get items List <Item> items = itemManager.GetAllItems().Where(item => itemIds.Contains(item.ItemId)).ToList(); //get widget Widget widgetToUpdate = GetWidgetWithAllItems(widgetId); if (widgetToUpdate == null) { return(null); } //update widget widgetToUpdate.RowNumber = rowNbr; widgetToUpdate.ColumnNumber = colNbr; if (items.Count > 0) { widgetToUpdate.Items = items; } widgetToUpdate.RowSpan = rowspan; widgetToUpdate.ColumnSpan = colspan; if (graphType != 0) { widgetToUpdate.GraphType = graphType; } //update database widgetRepo.UpdateWidget(widgetToUpdate); uowManager.Save(); uowManager = null; return(widgetToUpdate); }
/// <summary> /// Adds socialmedia names to person /// </summary> public Person ChangePersonSocialMedia(int personId, string twitter, string facebook) { uowManager = new UnitOfWorkManager(); InitRepo(); //Get item Person personToUpdate = GetPersonWithDetails(personId); if (personToUpdate == null) { return(null); } //Get sources DataManager dataManager = new DataManager(uowManager); IEnumerable <Source> sources = dataManager.GetAllSources(); if (sources == null || sources.Count() == 0) { return(null); } //Update person with with new urls foreach (Source source in sources) { personToUpdate.SocialMediaNames.Add(new SocialMediaName() { Source = source, Username = source.Name }); } //Update database itemRepo.UpdatePerson(personToUpdate); uowManager.Save(); uowManager = null; return(personToUpdate); }
/// <summary> /// Copies a widget to the dashboard /// All attributes of the given Widget are copied and used to generate a new UserWidget. /// </summary> public void MoveWidgetToDashBoard(int widgetId, GraphType graphType, IEnumerable <int> itemIds, string userId) { uowManager = new UnitOfWorkManager(); InitRepo(); ItemManager itemManager = new ItemManager(uowManager); //Get items List <Item> items = itemManager.GetAllItems().Where(item => itemIds.Contains(item.ItemId)).ToList(); //Get dashboard Dashboard dash = GetDashboard(userId); if (dash == null) { return; } //Get widget Widget widget = GetWidgetWithAllData(widgetId); if (widget == null) { return; } //make new widget and attach items to the new widgetwidget.GraphType Widget newWidget = AddWidget(WidgetType.GraphType, widget.Title, 0, 0, proptags: new List <PropertyTag>(), rowspan: widget.RowSpan, colspan: widget.ColumnSpan, dashboardId: dash.DashboardId, items: items, graphType: graphType); uowManager.Save(); //Copy the property tags. widget.PropertyTags.ToList().ForEach(proptag => newWidget.PropertyTags.Add(new PropertyTag() { Name = proptag.Name })); //Create a copy of all graphvalues and widgetDatas List <WidgetData> widgetDataCopy = new List <WidgetData>(); widget.WidgetDatas.ToList().ForEach(data => { //copy graphvalues List <GraphValue> graphValuesCopy = new List <GraphValue>(); data.GraphValues.ToList().ForEach(value => graphValuesCopy.Add(new GraphValue(value))); //copy widgetdata WidgetData newWidgetData = new WidgetData(data); newWidgetData.GraphValues = graphValuesCopy; newWidgetData.Widget = newWidget; AddWidgetData(newWidgetData); widgetDataCopy.Add(newWidgetData); }); newWidget.WidgetDatas = widgetDataCopy; //Update database ChangeWidget(newWidget); uowManager.Save(); uowManager = null; }
/// <summary> /// Gets json which it will convert to Information objects dat are stored into the database afterwards. /// </summary> private void BatchUpdate(string json, int start, int end) { uowManager = new UnitOfWorkManager(); InitRepo(); //Load everything in memory to gain performance IItemManager itemManager = new ItemManager(uowManager); IEnumerable <Item> items = itemManager.GetAllPersons(); List <Theme> themes = itemManager.GetAllThemes().ToList(); IEnumerable <Property> properties = dataRepo.ReadAllProperties(); IEnumerable <Source> sources = dataRepo.ReadAllSources(); dynamic deserializedJson = JsonConvert.DeserializeObject(json); //Map propertyvalues to List <Information> informationList = new List <Information>(); for (int i = start; i < end; i++) { PropertyValue propertyValue; Information information = new Information { PropertieValues = new List <PropertyValue>() }; //Read gender propertyValue = new PropertyValue { Property = properties.Where(x => x.Name.Equals("Gender")).SingleOrDefault(), Value = deserializedJson[i].profile.gender, Confidence = 1 }; information.PropertieValues.Add(propertyValue); //Read age propertyValue = new PropertyValue { Property = properties.Where(x => x.Name.Equals("Age")).SingleOrDefault(), Value = deserializedJson[i].profile.age, Confidence = 1 }; information.PropertieValues.Add(propertyValue); //Read education propertyValue = new PropertyValue { Property = properties.Where(x => x.Name.Equals("Education")).SingleOrDefault(), Value = deserializedJson[i].profile.education, Confidence = 1 }; information.PropertieValues.Add(propertyValue); //Read language propertyValue = new PropertyValue { Property = properties.Where(x => x.Name.Equals("Language")).SingleOrDefault(), Value = deserializedJson[i].profile.language, Confidence = 1 }; information.PropertieValues.Add(propertyValue); //Read personality propertyValue = new PropertyValue { Property = properties.Where(x => x.Name.Equals("Personality")).SingleOrDefault(), Value = deserializedJson[i].profile.personality, Confidence = 1 }; information.PropertieValues.Add(propertyValue); //Read words for (int j = 0; j < deserializedJson[i].words.Count; j++) { propertyValue = new PropertyValue { Property = properties.Where(x => x.Name.Equals("Word")).SingleOrDefault(), Value = deserializedJson[i].words[j], Confidence = 1 }; information.PropertieValues.Add(propertyValue); } //Read sentiment for (int j = 0; j < deserializedJson[i].sentiment.Count; j++) { propertyValue = new PropertyValue { Property = properties.Where(x => x.Name.Equals("Sentiment")).SingleOrDefault(), Value = deserializedJson[i].sentiment[0], Confidence = deserializedJson[i].sentiment[1] }; information.PropertieValues.Add(propertyValue); } //Read hashtags for (int j = 0; j < deserializedJson[i].hashtags.Count; j++) { propertyValue = new PropertyValue { Property = properties.Where(x => x.Name.Equals("Hashtag")).SingleOrDefault(), Value = deserializedJson[i].hashtags[j], Confidence = 1 }; information.PropertieValues.Add(propertyValue); } //Read mentions for (int j = 0; j < deserializedJson[i].mentions.Count; j++) { propertyValue = new PropertyValue { Property = properties.Where(x => x.Name.Equals("Mention")).SingleOrDefault(), Value = deserializedJson[i].mentions[j], Confidence = 1 }; information.PropertieValues.Add(propertyValue); } //Read urls for (int j = 0; j < deserializedJson[i].urls.Count; j++) { propertyValue = new PropertyValue { Property = properties.Where(x => x.Name.Equals("Url")).SingleOrDefault(), Value = deserializedJson[i].urls[j], Confidence = 1 }; information.PropertieValues.Add(propertyValue); } //Read date propertyValue = new PropertyValue { Property = properties.Where(x => x.Name.Equals("Date")).SingleOrDefault(), Value = deserializedJson[i].date, Confidence = 1 }; information.PropertieValues.Add(propertyValue); //Read postid propertyValue = new PropertyValue { Property = properties.Where(x => x.Name.Equals("PostId")).SingleOrDefault(), Value = deserializedJson[i].id, Confidence = 1 }; information.PropertieValues.Add(propertyValue); //Read retweet propertyValue = new PropertyValue { Property = properties.Where(x => x.Name.Equals("Retweet")).SingleOrDefault(), Value = deserializedJson[i].retweet, Confidence = 1 }; information.PropertieValues.Add(propertyValue); //Add connection to Item (Person) //Read persons information.Items = new List <Item>(); for (int j = 0; j < deserializedJson[i].persons.Count; j++) { string name = deserializedJson[i].persons[j]; information.Items.Add(items.Where(x => x.Name.Equals(name)).SingleOrDefault()); } //Add connection to Item (Theme) //Read themes for (int j = 0; j < deserializedJson[i].themes.Count; j++) { string name = deserializedJson[i].themes[j]; information.Items.Add(themes.Where(x => x.Name.Equals(name)).SingleOrDefault()); } //Add other information information.Source = sources.Where(x => x.Name.Equals("Twitter")).SingleOrDefault(); string stringDate = Convert.ToString(deserializedJson[i].date); DateTime infoDate = DateTime.ParseExact(stringDate, "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture); information.CreationDate = infoDate; informationList.Add(information); } dataRepo.CreateInformations(informationList); uowManager.Save(); uowManager = null; }
/// <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); }
/// <summary> /// Generates dafault widgets based on the itemid /// </summary> public void GenerateDefaultItemWidgets(string name, int itemId) { uowManager = new UnitOfWorkManager(); InitRepo(); WidgetManager widgetManager = new WidgetManager(uowManager); List <Widget> itemWidgets = new List <Widget>(); List <int> widgetIds = new List <int>(); List <PropertyTag> proptags; //Get item Item item = GetItemWithAllWidgets(itemId); //1st widget proptags = new List <PropertyTag>(); proptags.Add(new PropertyTag() { Name = "Mentions" }); ItemWidget widget1 = (ItemWidget)widgetManager.AddWidget(WidgetType.GraphType, name + " popularity", 1, 1, proptags: proptags, graphType: GraphType.LineChart, rowspan: 12, colspan: 6); itemWidgets.Add(widget1); widgetIds.Add(widget1.WidgetId); //If item is from type theme or person then //more items shall be added if (item is Theme || item is Person) { //2nd widget proptags = new List <PropertyTag>(); proptags.Add(new PropertyTag() { Name = "Gender" }); ItemWidget widget2 = (ItemWidget)widgetManager.AddWidget(WidgetType.GraphType, name + " gender comparison ", 1, 1, proptags: proptags, graphType: GraphType.PieChart, rowspan: 6, colspan: 6); itemWidgets.Add(widget2); widgetIds.Add(widget2.WidgetId); //3rd widget proptags = new List <PropertyTag>(); proptags.Add(new PropertyTag() { Name = "Age" }); ItemWidget widget3 = (ItemWidget)widgetManager.AddWidget(WidgetType.GraphType, name + " age comparison", 1, 1, proptags: proptags, graphType: GraphType.DonutChart, rowspan: 6, colspan: 6); itemWidgets.Add(widget3); widgetIds.Add(widget3.WidgetId); //4th widget proptags = new List <PropertyTag>(); proptags.Add(new PropertyTag() { Name = "Education" }); ItemWidget widget4 = (ItemWidget)widgetManager.AddWidget(WidgetType.GraphType, name + " education comparison", 1, 1, proptags: proptags, graphType: GraphType.DonutChart, rowspan: 6, colspan: 6); itemWidgets.Add(widget4); widgetIds.Add(widget4.WidgetId); //5th widget proptags = new List <PropertyTag>(); proptags.Add(new PropertyTag() { Name = "Personality" }); ItemWidget widget5 = (ItemWidget)widgetManager.AddWidget(WidgetType.GraphType, name + " personality comparison", 1, 1, proptags: proptags, graphType: GraphType.PieChart, rowspan: 6, colspan: 6); itemWidgets.Add(widget5); widgetIds.Add(widget5.WidgetId); } //Link widgets to item & save changes to database item.ItemWidgets = itemWidgets; itemRepo.UpdateItem(item); uowManager.Save(); uowManager = null; }