private static void UpdateDatabase(Book book) { try { List <Chapter> chapterList = BooksOnServerAccessor.GetChapters(book.ID); List <Page> pageList = BooksOnServerAccessor.GetPages(book.ID); // Update notes if (BooksOnDeviceAccessor.GetNotes(book.ID) != null) { // Remove orphans BooksOnDeviceAccessor.RemoveOrphanNotes(book.ID, pageList); } // Update bookmarks if (BooksOnDeviceAccessor.GetBookmarks(book.ID) != null) { // Remove orphans BooksOnDeviceAccessor.RemoveOrphanBookmarks(book.ID, pageList); } // Update new chapters if (BooksOnDeviceAccessor.GetChapters(book.ID) == null) { BooksOnDeviceAccessor.AddChapters(book.ID, chapterList); } else { BooksOnDeviceAccessor.UpdateChapters(book.ID, chapterList); } BooksOnDeviceAccessor.MapPagesToChapter(chapterList, pageList); // ML: 4/9/2013 We need to map them after each update // Update new pages if (BooksOnDeviceAccessor.GetPages(book.ID) == null) { BooksOnDeviceAccessor.AddPages(book.ID, pageList); } else { BooksOnDeviceAccessor.UpdatePages(book.ID, pageList); } // Remove chapters and pages list from BooksOnServer because they now exist on the device BooksOnServerAccessor.RemoveChapters(book.ID); BooksOnServerAccessor.RemovePages(book.ID); // Update new book book.New = true; book.Status = Book.BookStatus.DOWNLOADED; book.UserAddedDate = DateTime.UtcNow; if (BooksOnDeviceAccessor.GetBook(book.ID) == null) { BooksOnDeviceAccessor.AddBook(book); } else { if (BooksDataAccessor.IsFavorite(book.ID)) { book.IsFavorite = true; } BooksOnDeviceAccessor.UpdateBook(book); } } catch (Exception ex) { Logger.WriteLineDebugging("BookUpdater - UpdateDatabase: {0}", ex.ToString()); } }