private static void ProcessPersonList(Dao.Title title, List <OMLEngine.Person> updatedList, PeopleRole role) { IEnumerable <string> originals = from a in title.People where a.Role == (byte)role select a.MetaData.FullName; List <string> added = new List <string>(updatedList.Where(t => !originals.Contains(t.full_name)).Select(t => t.full_name)); List <string> removed = new List <string>(originals.Where(t => !updatedList.Select(r => r.full_name).Contains(t))); // remove ones no longer used foreach (string remove in removed) { Dao.Person person = title.People.SingleOrDefault(p => p.MetaData.FullName == remove && p.Role == (byte)role); if (person != null) { title.People.Remove(person); } } // add the new ones foreach (string add in added) { if (!string.IsNullOrEmpty(add)) { AddActorToTitle(title, add, null, role); } } }
public static void UpdateCollectionsForTitle(Dao.Title title) { if (title.UpdatedActors != null) { IEnumerable <string> originalActors = from a in title.People where a.Role == (byte)PeopleRole.Actor select a.MetaData.FullName; List <string> added = new List <string>(title.UpdatedActors.Where(t => !originalActors.Contains(t.PersonName)).Select(t => t.PersonName)); List <string> removed = new List <string>(originalActors.Where(t => !title.UpdatedActors.Select(r => r.PersonName).Contains(t))); // remove ones no longer used foreach (string remove in removed) { Dao.Person person = title.People.SingleOrDefault(p => p.MetaData.FullName == remove && p.Role == (byte)PeopleRole.Actor); if (person != null) { title.People.Remove(person); } } Dictionary <string, string> actorLookup = new Dictionary <string, string>(); title.UpdatedActors.ForEach(t => actorLookup.Add(t.PersonName, t.RoleName)); // add the new ones foreach (string add in added) { AddActorToTitle(title, add, actorLookup[add], PeopleRole.Actor); } } if (title.UpdatedDirectors != null) { ProcessPersonList(title, title.UpdatedDirectors, PeopleRole.Director); } if (title.UpdatedWriters != null) { ProcessPersonList(title, title.UpdatedWriters, PeopleRole.Writer); } if (title.UpdatedProducers != null) { ProcessPersonList(title, title.UpdatedProducers, PeopleRole.Producers); } // if the genres were modified see how they've changed if (title.UpdatedGenres != null) { // see if there are any genres to add // grab all the original genres IEnumerable <string> originalGenres = from g in title.Genres select g.MetaData.Name; List <string> added = new List <string>(title.UpdatedGenres.Where(t => !originalGenres.Contains(t))); List <string> removed = new List <string>(originalGenres.Where(t => !title.UpdatedGenres.Contains(t))); // remove ones no longer used foreach (string remove in removed) { Dao.Genre genre = title.Genres.SingleOrDefault(g => g.MetaData.Name == remove); if (genre != null) { title.Genres.Remove(genre); } } // add the new ones foreach (string add in added) { AddGenreToTitle(title, add); } } if (title.UpdatedTags != null) { IEnumerable <string> originalTags = from t in title.Tags select t.Name; List <string> added = new List <string>(title.UpdatedTags.Where(t => !originalTags.Contains(t))); List <string> removed = new List <string>(originalTags.Where(t => !title.UpdatedTags.Contains(t))); foreach (string remove in removed) { Dao.Tag tag = title.Tags.SingleOrDefault(t => t.Name == remove); if (tag != null) { title.Tags.Remove(tag); } } foreach (string add in added) { Dao.Tag daoTag = new OMLEngine.Dao.Tag(); daoTag.TitleId = title.Id; daoTag.Name = add; title.Tags.Add(daoTag); } } }