public LocalDataContext(bool readOnly)
        {
            lock (lockObject) {
                db = new OMLDataDataContext();
                //db.DeferredLoadingEnabled = false;
            //                if (readOnly)
            //                    db.ObjectTrackingEnabled = false;

                System.Data.Linq.DataLoadOptions loadOptions = new System.Data.Linq.DataLoadOptions();
                loadOptions.LoadWith<Title>(i => i.Images);
                db.LoadOptions = loadOptions;
                //db.Log = OMLDBContextLogger.Logger();

                db.Connection.ConnectionString = OMLEngine.DatabaseManagement.DatabaseInformation.OMLDatabaseConnectionString;
            }
        }
        /// <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 void Dispose()
 {
     if (db != null)
     {
         db.Dispose();
         db = null;
     }
 }
        /// <summary>
        /// Go through all the people in the movie and send them in batch to sql to see who exists
        /// as a person already
        /// </summary>
        /// <param name="title"></param>
        /// <returns></returns>
        private static Dictionary<string, BioData> GetAllExistingPeople(OMLDataDataContext context, OMLEngine.Title title)
        {
            List<string> names = new List<string>(title.ActingRoles.Count + title.Writers.Count + title.Directors.Count + title.Producers.Count);

            foreach (Role person in title.ActingRoles)
                names.Add(person.PersonName);

            foreach (OMLEngine.Person person in title.Writers)
                names.Add(person.full_name);

            foreach (OMLEngine.Person person in title.Directors)
                names.Add(person.full_name);

            foreach (OMLEngine.Person person in title.Producers)
                names.Add(person.full_name);

            // now that we have all the people - query to see who exists
            var actors = from actor in context.BioDatas
                         where names.Contains(actor.FullName)
                         select actor;

            Dictionary<string, BioData> existingPeople = new Dictionary<string, BioData>();

            System.Diagnostics.Trace.WriteLine("Actors in current Context - " + actors.Count().ToString());

            foreach (BioData data in actors)
                if (!existingPeople.ContainsKey(data.FullName))
                    existingPeople.Add(data.FullName, data);

            return existingPeople;
        }
        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);*/
        }