private bool CheckBookInDB(DBSQLiteModel db, Book book) { List <Genre> listgb; List <Author> listab; List <Book> bookList; listgb = book.Genres.ToList(); listgb.Sort(); listab = book.Authors.ToList(); listab.Sort(); //lock(lockerBook) //lock (lockerZip) //{ bookList = db.Books .Include(b => b.Authors) .Include(b => b.Genres) .Where(b => b.Caption == book.Caption) .ToList(); //} if (bookList.Count() == 0) { return(true); } var res = from b in bookList where b.Caption == book.Caption && CompareDbSets(listgb, b.Genres.ToList()) && CompareDbSets(listab, b.Authors.ToList()) select b.Caption; return(res.Count() == 0); }
private void CheckBooksGenres(DBSQLiteModel db, Book book) { Genre genreDB; List <Genre> list = new List <Genre>(); foreach (Genre genre in book.Genres) { //lock (lockerGenre) //lock (lockerZip) //{ genreDB = db.Genres.FirstOrDefault <Genre>( p => p.Key == genre.Key ); //} if (genreDB != null) { list.Add(genreDB); } } book.Genres.Clear(); if (list.Count() > 0) { foreach (var g in list) { book.Genres.Add(g); } } }
//public bool ProcessUpdate2() //{ // ITreeViewItem item; // try // { // item = GetInitItem(); // }catch(ArgumentOutOfRangeException e) // { // throw e; // //return false; // } // Debug.WriteLine(item.Name); // IEnumerable<TreeViewItem_FileFB2> listFB2 = GetListFB2(item); // if (listFB2.Count() == 0) // { // return true; // } // Debug.WriteLine(listFB2.Count()); // string libPathTest = "Data Source=" + fileDestination + "; version = 3 "; // using (DBModel db = new DBModel(libPathTest)) // { // db.Genres.Load(); // //foreach (var fb2 in listFB2) // //{ // // ProcessUpdateFB2(fb2, db); // //} // ParallelOptions po = new ParallelOptions // { // MaxDegreeOfParallelism = // Environment.ProcessorCount > 2 ? Environment.ProcessorCount - 1 : 1 // }; // Parallel.ForEach(listFB2, po, entry => // { // ProcessUpdateFB2(entry, db); // }); // Debug.WriteLine("! End !"); // } // return true; //} //public bool ProcessUpdate3() //{ // ITreeViewItem item; // try // { // item = GetInitItem(); // } // catch (ArgumentOutOfRangeException e) // { // throw e; // //return false; // } // Debug.WriteLine(item.Name); // List<Book> listBooks = item.GetChilds_Books(); // if (listBooks.Count() == 0) // { // return true; // } // Debug.WriteLine(listBooks.Count()); // string libPathTest = "Data Source=" + fileDestination + "; version = 3 "; // using (DBModel db = new DBModel(libPathTest)) // { // db.Genres.Load(); // foreach (var fb2 in listBooks) // { // //ProcessUpdateFB2(fb2, db); // //ProcessUpdateBook(fb2, db); // } // //ParallelOptions po = new ParallelOptions // //{ // // MaxDegreeOfParallelism = // // Environment.ProcessorCount > 2 ? Environment.ProcessorCount - 1 : 1 // //}; // //Parallel.ForEach(listBooks, po, entry => // //{ // // ProcessUpdateBook(entry, db); // //}); // //Debug.WriteLine("! End !"); // } // return true; //} public bool ProcessUpdate() { ITreeViewItem item; try { item = GetInitItem(); } catch (ArgumentOutOfRangeException e) { throw e; // return false; } Debug.WriteLine("Init Item : {0} ", item.Name); List <DBFile> listFiles = item.GetChilds_Files(); Debug.WriteLine(listFiles.Count); if (listFiles.Count() == 0) { return(true); } Debug.WriteLine(listFiles.Count()); string libPathTest = "Data Source=" + fileDestination + "; version = 3 "; using (DBSQLiteModel db = new DBSQLiteModel(libPathTest)) { db.Genres.Load(); foreach (var file in listFiles) { //ProcessUpdateFB2(fb2, db); //ProcessUpdateBook(fb2, db); ProcessUpdateFile(file, db); } //ParallelOptions po = new ParallelOptions //{ // MaxDegreeOfParallelism = // Environment.ProcessorCount > 2 ? Environment.ProcessorCount - 1 : 1 //}; //Parallel.ForEach(listBooks, po, entry => //{ // ProcessUpdateBook(entry, db); //}); //Debug.WriteLine("! End !"); } return(true); }
private bool CheckBook(DBSQLiteModel db, Book book) { bool addBook; if (book == null) { return(false); } CheckBooksGenres(db, book); addBook = CheckBooksAuthors(db, book) | CheckBooksKeyWords(db, book) | addDuplicated; addBook = addBook || (!checkUnknown4Duplicated && book.Caption.CompareTo("<Unknown>") == 0) || CheckBookInDB(db, book); return(addBook); }
private bool CheckBooksKeyWords(DBSQLiteModel db, Book book) { KeyWord keyWordDB; List <KeyWord> list = new List <KeyWord>(); bool updateList = false, addedItems = false; foreach (KeyWord keyWord in book.KeyWords) { //lock (lockerKeyWord) // lock (lockerZip) //{ keyWordDB = db.KeyWords.FirstOrDefault <KeyWord>( p => p.Word.CompareTo(keyWord.Word) == 0 ); if (keyWordDB == null) { db.KeyWords.Add(keyWord); keyWordDB = keyWord; addedItems = true; } else { updateList = true; } //} list.Add(keyWordDB); } if (list.Count() > 0) { if (updateList) { book.KeyWords.Clear(); foreach (var k in list) { book.KeyWords.Add(k); } } } return(addedItems); }
private bool CheckBooksAuthors(DBSQLiteModel db, Book book) { Author authorDB; List <Author> list = new List <Author>(); bool updateList = false, addedItems = false; foreach (Author author in book.Authors) { //lock (lockerAuthors) //lock (lockerZip) //{ authorDB = db.Authors.FirstOrDefault <Author>( p => p.LastName.CompareTo(author.LastName) == 0 && p.FirstName.CompareTo(author.FirstName) == 0 && p.MiddleName.CompareTo(author.MiddleName) == 0); if (authorDB == null) { db.Authors.Add(author); authorDB = author; addedItems = true; } else { updateList = true; } //} list.Add(authorDB); } if (updateList) { book.Authors.Clear(); foreach (var a in list) { book.Authors.Add(a); } } return(addedItems); }
private void CheckFile(DBSQLiteModel db, DBFile file) { Debug.WriteLine("File :" + file.Path); List <Book> books = new List <Book>(); bool addBook = false; foreach (Book book in file.Books) { if (CheckBook(db, book)) { db.Books.Add(book); books.Add(book); db.SaveChanges(); } } if (books.Count() > 0) { file.Books.Clear(); var fileDb = db.DBFiles.FirstOrDefault <DBFile>( p => p.Path.CompareTo(file.Path) == 0 ); if (fileDb == null) { db.DBFiles.Add(file); fileDb = file; } foreach (var b in books) { fileDb.Books.Add(b); } } }
private void ProcessUpdateFile(DBFile file, DBSQLiteModel db) { CheckFile(db, file); db.SaveChanges(); }