public static void AddTitle(OMLDataDataContext context, Title title) { context.Titles.InsertOnSubmit(title); // Find actual SQL run to process the update - debugging purposes /*string s = context.GetType().GetMethod("GetChangeText", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).Invoke(context, null) as string; * * System.Data.Linq.ChangeSet cs = context.GetChangeSet(); * foreach (var insert in cs.Inserts) * { * if (insert is BioData) * { * BioData bd = insert as BioData; * System.Diagnostics.Trace.WriteLine(bd.FullName + " - " + bd.Id); * } * }*/ context.SubmitChanges(); }
/// <summary> /// Creates a person object from a given name and role. Will check the db first to see if that person exists /// </summary> /// <param name="name"></param> /// <param name="role"></param> /// <returns></returns> public static Person CreatePerson(OMLDataDataContext context, string name, string characterName, PeopleRole role, Dictionary <string, BioData> existingPeople) { BioData metaData = null; // see if the actor exists already in the in memory cache existingPeople.TryGetValue(name, out metaData); if (metaData == null) { // Ok, we may not have allready created but first check if sql thinks it has // SQL thinks 'æ' and 'ae' are so lets double check the database metaData = Dao.TitleCollectionDao.GetPersonBioDataByName(context, name); } if (metaData == null) { System.Diagnostics.Trace.WriteLine("Adding Bio for - " + name); // if it doesn't exist create a new one metaData = new BioData(); metaData.FullName = name; context.BioDatas.InsertOnSubmit(metaData); context.SubmitChanges(); // add the new metaData we added to the dictionary so we don't add it again existingPeople.Add(name, metaData); } else { System.Diagnostics.Trace.WriteLine("Found Bio for - " + name + " : Fullname - " + metaData.FullName); } // setup the person Person person = new Person(); person.MetaData = metaData; person.Role = (byte)role; if (!string.IsNullOrEmpty(characterName)) { person.CharacterName = characterName; } return(person); }
public static void SetupCollectionsToBeAdded(OMLDataDataContext context, OMLEngine.Title title) { Title daoTitle = title.DaoTitle; // patch up the sort name if it's missing if (string.IsNullOrEmpty(daoTitle.SortName)) { daoTitle.SortName = daoTitle.Name; } // add the genres foreach (string genre in title.Genres) { // see if we've added this genre locally already Genre daoGenre = daoTitle.Genres.FirstOrDefault(t => t.MetaData.Name.Equals(genre)); // genres must be unique if (daoGenre != null) { continue; } // try to see if the genre exists GenreMetaData metaData = context.GenreMetaDatas.SingleOrDefault(t => t.Name.ToLower() == genre.ToLower()); if (metaData == null) { // if it doesn't exist create a new one metaData = new GenreMetaData(); metaData.Name = genre; context.GenreMetaDatas.InsertOnSubmit(metaData); context.SubmitChanges(); } // setup the genre daoGenre = new Genre(); daoGenre.MetaData = metaData; // add the genre to the title daoTitle.Genres.Add(daoGenre); } // add the tags foreach (string name in title.Tags) { // see if we've added this tag locally already Tag tag = daoTitle.Tags.FirstOrDefault(t => t.Name.Equals(name)); // tags must be unique if (tag != null) { continue; } // try to see if the tag exists in the db already //tag = context.Tags.SingleOrDefault(t => t.Name.ToLower() == name.ToLower()); if (tag == null) { // if it doesn't exist create a new one tag = new Tag(); tag.Name = name; } // add the tag daoTitle.Tags.Add(tag); } // grab from the db who we know about already Dictionary <string, BioData> existingPeople = GetAllExistingPeople(context, title); int actorIndex = 0; // add the actors foreach (Role actor in title.DaoTitle.UpdatedActors) { Person person = CreatePerson(context, actor.PersonName, actor.RoleName, PeopleRole.Actor, existingPeople); // maintain the order person.Sort = (short)(actorIndex++); // add them to the title daoTitle.People.Add(person); } // add the directors foreach (OMLEngine.Person director in title.DaoTitle.UpdatedDirectors) { Person person = CreatePerson(context, director.full_name, null, PeopleRole.Director, existingPeople); // maintain the order person.Sort = (short)(actorIndex++); // add them to the title var e = (from p in daoTitle.People where p.MetaData.Id == person.MetaData.Id select p); if (e.Count() == 0) { daoTitle.People.Add(person); } } // add the writers foreach (OMLEngine.Person writer in title.DaoTitle.UpdatedWriters) { Person person = CreatePerson(context, writer.full_name, null, PeopleRole.Writer, existingPeople); // maintain the order person.Sort = (short)(actorIndex++); // add them to the title daoTitle.People.Add(person); } // add the producers foreach (OMLEngine.Person name in title.DaoTitle.UpdatedProducers) { Person person = CreatePerson(context, name.full_name, null, PeopleRole.Producers, existingPeople); // maintain the order person.Sort = (short)(actorIndex++); // add them to the title daoTitle.People.Add(person); } // Debugging code var b = (from p in daoTitle.People select p); foreach (Person pr in b) { System.Diagnostics.Trace.WriteLine("Adding " + pr.Role + " - " + pr.MetaData.FullName + " as " + pr.CharacterName + " [" + pr.MetaData.Id + "]"); } // ignore the rest for now // add all the disks /*foreach (OMLEngine.Disk disk in title.Disks) * { * Disk daoDisk = new Disk(); * daoDisk.Name = disk.Name; * daoDisk.Path = disk.Path; * daoDisk.VideoFormat = (byte)disk.Format; * * daoTitle.Disks.Add(daoDisk); * } * * // add the audio tracks * daoTitle.AudioTracks = GetDelimitedStringFromCollection(title.AudioTracks); * * // add the subtitles * daoTitle.Subtitles = GetDelimitedStringFromCollection(title.Subtitles); * * // add the trailers * daoTitle.Trailers = GetDelimitedStringFromCollection(title.Trailers);*/ }