Beispiel #1
0
 /// <summary>
 /// When unit of work is present, it will effect
 /// initRepo-method. (see documentation of initRepo)
 /// </summary>
 public SubplatformManager(UnitOfWorkManager uowManager = null)
 {
     this.uowManager = uowManager;
 }
        /// <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>
        /// 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;
        }
        /// <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>
 /// When unit of work is present, it will effect
 /// initRepo-method. (see documentation of initRepo)
 /// </summary>
 public ItemManager(UnitOfWorkManager uowManager = null)
 {
     this.uowManager = uowManager;
 }