private static void UploadDataFromWiki() { using (Entities context = new Entities()) { foreach (Author author in context.Authors.Where(x => !x.FromWiki)) { new AuthorsDataFromWiki(context, author); context.SaveChanges(); ApplicationLogger.WriteStringToLog("Processed \"" + author.AuthorId.ToString() + "\": " + author.Name); } } }
private void UploadBookAndEntities() { using (Entities context = new Entities()) { try { AuthorLoader authorLoader = new AuthorLoader(bookSources.BookRecord, context); if (authorLoader.Authors != null) { GenresLoader genresLoader = new GenresLoader(bookSources.BookRecord, context); SeriesLoader seriesLoader = new SeriesLoader(bookSources.BookRecord, context); InpBookLoader bookLoader = new InpBookLoader(bookSources.BookRecord, authorLoader, genresLoader, seriesLoader, context); if (bookLoader.Book != null) { try { FB2BookUploader fb2Loader = new FB2BookUploader(bookLoader.Book, bookSources.BookFileName, context); try { LoadAuthorsDataFromWiki(context, bookLoader.Book.Authors); } catch { } context.SaveChanges(); ApplicationLogger.WriteStringToLog("Book \"" + bookLoader.Book.Name + "\" has been uploaded successfully."); } catch (Exception ex) { ApplicationLogger.WriteStringToLog("Book from file: \"" + Path.GetFileName(bookSources.BookFileName) + "\" has not been uploaded because: \r\n" + ex.Message); } } else { ApplicationLogger.WriteStringToLog("Book from file: \"" + Path.GetFileName(bookSources.BookFileName) + "\" has not been uploaded because exist in the database."); } } else { ApplicationLogger.WriteStringToLog("Book from file: \"" + Path.GetFileName(bookSources.BookFileName) + "\" has not been uploaded because couldn't parse authors."); } } catch (Exception ex) { //transaction.Rollback(); throw new Exception("UploadBookAndEntities:\r\n" + ex.Message); } } }
private static void ProcessBook(Guid bookID, int iterator) { try { using (Entities context = new Entities()) { Book book = context.Books.FirstOrDefault(x => x.BookId == bookID); if (book.BookContents.Count > 0) { ApplicationLogger.WriteStringToLog("Process book: " + book.BookId.ToString()); Flag flag = context.Flags.FirstOrDefault(x => x.Name.ToLower().Equals("sources uploaded") && x.BookId == bookID); UploadBookSources(book, flag); context.SaveChanges(); } } } catch (Exception e) { if (iterator <= 10) { ApplicationLogger.WriteStringToError(e.Message); Thread.Sleep(2000); iterator = iterator++; ProcessBook(bookID, iterator); } else { ApplicationLogger.WriteStringToLog("Application can't commit cahnges to the database because:\r\n"); throw e; } } }
private void GoThroughBookRecords() { using (Entities context = new Entities()) { try { IQueryable<Guid> books = null; if (!string.IsNullOrEmpty(orderByClause)) { books = context.Books.AsNoTracking().Where(x => x.FromGoogleBooks == null || x.FromGoogleBooks == false).OrderBy(x => x.Name).Select(x => x.BookId); } else { books = context.Books.AsNoTracking().Where(x => x.FromGoogleBooks == null || x.FromGoogleBooks == false).OrderBy(x => x.BookId).Select(x => x.BookId); } foreach (var bookID in books) { Book book = context.Books.FirstOrDefault(x => x.BookId == bookID); ApplicationLogger.WriteStringToLog("Start to process book: " + book.BookId.ToString()); try { GoogleBookJsonResponse booksDataFromGoogle = GetBooksDataFromGoogle(book.Name, book.Authors.FirstOrDefault().Name, 0); if (booksDataFromGoogle != null && booksDataFromGoogle.totalItems > 0) { ApplicationLogger.WriteStringToLog("Processed book: " + book.BookId.ToString() + " - Success"); BookItem bookItem = ChooseBookItem(booksDataFromGoogle, book.Name, book.Authors.FirstOrDefault().Name); if (bookItem != null) { UpdateBookRecord(context, book, bookItem); context.SaveChanges(); } else { UpdateBookRecordAsProcessed(context, book); context.SaveChanges(); } } else { UpdateBookRecordAsProcessed(context, book); context.SaveChanges(); } } catch { } Thread.Sleep(2000); } } catch (SqlException ex) { if (ex.Message.Contains("provider: TCP Provider, error: 0")) { string currentTime = Convert.ToString(DateTime.Now); ApplicationLogger.WriteStringToLog("Error: can't connect to SQL server."); Thread.Sleep(5000); GoThroughBookRecords(); } } catch (Exception ex) { ApplicationLogger.WriteStringToLog(ex.Message); throw ex; } } }