Beispiel #1
0
        protected override void ProcessItem(IContent content)
        {
            Type typeOfContent = content.GetType();

            string regexMatchText = string.Empty;

            if (typeOfContent == typeof(TextContent))
            {
                regexMatchText = ((TextContent)content).Content;
            }
            else if (typeOfContent == typeof(HtmlContent))
            {
                regexMatchText = ((HtmlContent)content).Content.ToString();
            }
            else if (typeOfContent == typeof(XmlContent))
            {
                regexMatchText = ((XmlContent)content).Content.ToString();
            }

            if (!string.IsNullOrEmpty(regexMatchText))
            {
                List <TChild> childObjs = CheckForMatches(regexMatchText);

                DataContext context = DataManager.GetDataContext <TParent>();
                context.AddObjects(childObjs);

                context.Save();
            }
        }
        /// <summary>
        /// Adds new artists to the database
        /// </summary>
        /// <param name="artists"></param>
        public List <Artist> PopulateArtistObjects(IEnumerable <Artist> artists)
        {
            // get the context for artists
            DataContext dataContext = DataManager.GetDataContext <Artist>();

            // get list of existing artists in the db
            List <Artist> existingArtists = dataContext.GetAll <Artist>(false);

            List <Artist> artistsToAdd     = new List <Artist>();
            List <Artist> populatedArtists = new List <Artist>();

            // go through the list of artists and find any that do not already exist in the db
            foreach (Artist artist in artists)
            {
                if (!populatedArtists.Any(a => string.Compare(a.Name, artist.Name, true) == 0))
                {
                    if (!existingArtists.Any(a => string.Compare(a.Name, artist.Name, true) == 0))
                    {
                        populatedArtists.Add(artist);
                        artistsToAdd.Add(artist);
                    }
                    else
                    {
                        populatedArtists.Add(existingArtists.First(a => string.Compare(a.Name, artist.Name, true) == 0));
                    }
                }
            }

            // add any new artists found
            if (artistsToAdd.Count > 0)
            {
                dataContext.AddObjects(artistsToAdd);
                dataContext.Save();
            }

            return(populatedArtists);
        }
        /// <summary>
        /// Adds a list of artists for a user
        /// </summary>
        /// <param name="userID">The ID of the user to add artists for</param>
        /// <param name="artists">The list of artists to be added</param>
        public void AddArtistsForUser(int userID, IEnumerable <Artist> artistNames)
        {
            // add records for any artists that do not already exist in the db
            List <Artist> artists = PopulateArtistObjects(artistNames);

            // get the datacontext for artists
            DataContext dataContext = DataManager.GetDataContext <Artist>();

            // get the existing artists for this user
            IEnumerable <Artist> existingUserArtists = GetArtistsForUser(userID);

            // create list of artists that do not already exist for this user
            List <UserArtist> newUserArtists =
                artists.Where(b => !existingUserArtists.Any(eua => string.Compare(eua.Name, b.Name, true) == 0))
                .Select(b => new UserArtist()
            {
                UserID = userID, ArtistID = b.ID
            })
                .ToList();

            // add the new artists and save
            dataContext.AddObjects(newUserArtists);
            dataContext.Save();
        }