public void DoImport(Stream inputStream, CsvImportInfo importInfo, Action <ExportImportProgressInfo> progressCallback) { var csvProducts = new List <CsvProduct>(); var progressInfo = new ExportImportProgressInfo { Description = "Reading products from csv..." }; progressCallback(progressInfo); using (var reader = new CsvReader(new StreamReader(inputStream))) { reader.Configuration.Delimiter = importInfo.Configuration.Delimiter; reader.Configuration.RegisterClassMap(new CsvProductMap(importInfo.Configuration)); reader.Configuration.WillThrowOnMissingField = false; while (reader.Read()) { try { var csvProduct = reader.GetRecord <CsvProduct>(); csvProducts.Add(csvProduct); } catch (Exception ex) { var error = ex.Message; if (ex.Data.Contains("CsvHelper")) { error += ex.Data["CsvHelper"]; } progressInfo.Errors.Add(error); progressCallback(progressInfo); } } } var catalog = _catalogService.GetById(importInfo.CatalogId); SaveCategoryTree(catalog, csvProducts, progressInfo, progressCallback); SaveProducts(catalog, csvProducts, progressInfo, progressCallback); }
public void DoImport(Stream inputStream, CsvImportInfo importInfo, Action<ExportImportProgressInfo> progressCallback) { var csvProducts = new List<CsvProduct>(); var progressInfo = new ExportImportProgressInfo { Description = "Reading products from csv..." }; progressCallback(progressInfo); using (var reader = new CsvReader(new StreamReader(inputStream))) { reader.Configuration.Delimiter = importInfo.Configuration.Delimiter; reader.Configuration.RegisterClassMap(new CsvProductMap(importInfo.Configuration)); reader.Configuration.WillThrowOnMissingField = false; while (reader.Read()) { try { var csvProduct = reader.GetRecord<CsvProduct>(); csvProducts.Add(csvProduct); } catch (Exception ex) { var error = ex.Message; if (ex.Data.Contains("CsvHelper")) { error += ex.Data["CsvHelper"]; } progressInfo.Errors.Add(error); progressCallback(progressInfo); } } } var catalog = _catalogService.GetById(importInfo.CatalogId); SaveCategoryTree(catalog, csvProducts, progressInfo, progressCallback); SaveProducts(catalog, csvProducts, progressInfo, progressCallback); }
// Only public methods can be invoked in the background. (Hangfire) public void BackgroundImport(CsvImportInfo importInfo, ImportNotification notifyEvent) { Action<ExportImportProgressInfo> progressCallback = (x) => { notifyEvent.InjectFrom(x); _notifier.Upsert(notifyEvent); }; using (var stream = _blobStorageProvider.OpenRead(importInfo.FileUrl)) { try { _csvImporter.DoImport(stream, importInfo, progressCallback); } catch (Exception ex) { notifyEvent.Description = "Export error"; notifyEvent.ErrorCount++; notifyEvent.Errors.Add(ex.ToString()); } finally { notifyEvent.Finished = DateTime.UtcNow; notifyEvent.Description = "Import finished" + (notifyEvent.Errors.Any() ? " with errors" : " successfully"); _notifier.Upsert(notifyEvent); } } }
public IHttpActionResult DoImport(CsvImportInfo importInfo) { base.CheckCurrentUserHasPermissionForObjects(CatalogPredefinedPermissions.Import, importInfo); var notification = new ImportNotification(CurrentPrincipal.GetCurrentUserName()) { Title = "Import catalog from CSV", Description = "starting import...." }; _notifier.Upsert(notification); BackgroundJob.Enqueue(() => BackgroundImport(importInfo, notification)); return Ok(notification); }
public IHttpActionResult DoImport(CsvImportInfo importInfo) { var notification = new ImportNotification(CurrentPrincipal.GetCurrentUserName()) { Title = "Import catalog from CSV", Description = "starting import...." }; _notifier.Upsert(notification); BackgroundJob.Enqueue(() => BackgroundImport(importInfo, notification)); return Ok(notification); }