public async Task <ActionResult> ManualImport(HttpPostedFileBase file, int accountId, ImporterType importer) { var watch = new Stopwatch(); var currentUser = await GetCurrentUser(); var currentClient = await GetCurrentClient(); var account = await Db.Accounts.FirstOrDefaultAsync(x => x.Id == accountId && x.ClientId == currentClient.Id); var viewModel = (await new ManualImportViewModel().Fill(Db, currentClient)).PreSelect(accountId, importer); watch.Start(); try { if (account != null && file != null) { using (var reader = new StreamReader(file.InputStream, Encoding.Default)) { var concreteImporter = new ImporterFactory().GetImporter(reader, importer); viewModel.ImportResult = await concreteImporter.LoadFileAndImport(Db, currentClient.Id, account.Id, new RulesApplier()); watch.Stop(); // save to import log Db.ImportLog.Add(new ImportLog { AccountId = account.Id, UserId = currentUser.Id, Date = DateTime.UtcNow, LinesDuplicatesCount = viewModel.ImportResult.DuplicateLinesCount, LinesFoundCount = viewModel.ImportResult.DuplicateLinesCount, LinesImportedCount = viewModel.ImportResult.NewLinesCount, Type = ImportLogType.Manual, Milliseconds = (int)watch.ElapsedMilliseconds, Log = null }); } } } catch (Exception ex) { viewModel.ErrorMessage = ex.Message; } return(View(viewModel)); }
private static async Task <ImporterBase.ImportResult> ParseAndSaveToDatabase(Db db, int clientId, int accountId, string tempFilePath, ConfigurationContainer config, ILogger logger) { logger?.Trace("Parsing file and saving to database ..."); logger?.Trace("File size: " + new FileInfo(tempFilePath).Length + " bytes."); ImporterBase.ImportResult importResult; var importerType = (ImporterType)Enum.Parse(typeof(ImporterType), config.Erbsenzaehler.Importer); using (var reader = new StreamReader(tempFilePath, Encoding.UTF8)) { var concreteImporter = new ImporterFactory().GetImporter(reader, importerType); importResult = await concreteImporter.LoadFileAndImport(db, clientId, accountId, new RulesApplier()); logger?.Info(importResult.NewLinesCount + " line(s) created, " + importResult.DuplicateLinesCount + " duplicate line(s)."); } return(importResult); }
public async Task <ActionResult> Import(ImportViewModel model) { var stopwatch = new Stopwatch(); stopwatch.Start(); try { if (string.IsNullOrEmpty(model.Username) || string.IsNullOrEmpty(model.Password)) { return(new HttpStatusCodeResult(HttpStatusCode.Unauthorized, "Username or password empty.")); } var signInStatus = await SignInManager.PasswordSignInAsync(model.Username, model.Password, false, false); switch (signInStatus) { case SignInStatus.Success: break; case SignInStatus.LockedOut: return(new HttpStatusCodeResult(HttpStatusCode.Unauthorized, "User locked.")); default: return(new HttpStatusCodeResult(HttpStatusCode.Unauthorized, "Username or password invalid.")); } var currentUser = await Db.Users.FirstAsync(x => x.UserName == model.Username); var currentClient = currentUser.Client; var account = await Db.Accounts.FirstOrDefaultAsync(x => x.ClientId == currentClient.Id && x.Name == model.Account); if (account == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Invalid account.")); } ImporterType importer; if (!Enum.TryParse(model.Importer, true, out importer)) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Invalid importer.")); } if (model.File == null || model.File.Length <= 0) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Empty file.")); } var result = new ImportResultViewModel(); var importLog = new ImportLog { AccountId = account.Id, UserId = currentUser.Id, Date = DateTime.UtcNow, Type = ImportLogType.AutomaticOnClient }; try { using (var stream = new MemoryStream(model.File)) { using (var reader = new StreamReader(stream, Encoding.UTF8)) { var concreteImporter = new ImporterFactory().GetImporter(reader, importer); var importResult = await concreteImporter.LoadFileAndImport(Db, currentClient.Id, account.Id, new RulesApplier()); result.IgnoredCount = importResult.DuplicateLinesCount; result.ImportedCount = importResult.NewLinesCount; importLog.LinesDuplicatesCount = importResult.DuplicateLinesCount; importLog.LinesFoundCount = importResult.DuplicateLinesCount + importResult.NewLinesCount; importLog.LinesImportedCount = importResult.NewLinesCount; } } } catch (Exception ex) { importLog.Log = ex.Message; return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Import failed: " + ex.Message)); } finally { // save to import log stopwatch.Stop(); importLog.Milliseconds = (int)stopwatch.ElapsedMilliseconds; Db.ImportLog.Add(importLog); await Db.SaveChangesAsync(); } return(Json(result)); } catch (Exception ex) { return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, ex.Message)); } }