public IHttpActionResult DoExport(CsvExportInfo exportInfo) { base.CheckCurrentUserHasPermissionForObjects(CatalogPredefinedPermissions.Export, exportInfo); var notification = new ExportNotification(_userNameResolver.GetCurrentUserName()) { Title = "Catalog export task", Description = "starting export...." }; _notifier.Upsert(notification); BackgroundJob.Enqueue(() => BackgroundExport(exportInfo, notification)); return Ok(notification); }
public void DoExport(Stream outStream, CsvExportInfo exportInfo, Action<ExportImportProgressInfo> progressCallback) { var prodgressInfo = new ExportImportProgressInfo { Description = "loading products..." }; var streamWriter = new StreamWriter(outStream, Encoding.UTF8, 1024, true); streamWriter.AutoFlush = true; using (var csvWriter = new CsvWriter(streamWriter)) { //Notification progressCallback(prodgressInfo); //Load all products to export var products = LoadProducts(exportInfo.CatalogId, exportInfo.CategoryIds, exportInfo.ProductIds); var allProductIds = products.Select(x => x.Id).ToArray(); //Load prices for products var allProductPrices = new List<Price>(); prodgressInfo.Description = "loading prices..."; progressCallback(prodgressInfo); var priceEvalContext = new PriceEvaluationContext { ProductIds = allProductIds, PricelistIds = exportInfo.PriceListId == null ? null : new string[] { exportInfo.PriceListId }, Currency = exportInfo.Currency }; allProductPrices = _pricingService.EvaluateProductPrices(priceEvalContext).ToList(); //Load inventories var allProductInventories = new List<InventoryInfo>(); prodgressInfo.Description = "loading inventory information..."; progressCallback(prodgressInfo); allProductInventories = _inventoryService.GetProductsInventoryInfos(allProductIds).Where(x => exportInfo.FulfilmentCenterId == null ? true : x.FulfillmentCenterId == exportInfo.FulfilmentCenterId).ToList(); //Export configuration exportInfo.Configuration.PropertyCsvColumns = products.SelectMany(x => x.PropertyValues).Select(x => x.PropertyName).Distinct().ToArray(); csvWriter.Configuration.Delimiter = exportInfo.Configuration.Delimiter; csvWriter.Configuration.RegisterClassMap(new CsvProductMap(exportInfo.Configuration)); //Write header csvWriter.WriteHeader<CsvProduct>(); prodgressInfo.TotalCount = products.Count(); var notifyProductSizeLimit = 50; var counter = 0; foreach (var product in products) { try { var csvProduct = new CsvProduct(product, _blobUrlResolver, allProductPrices.FirstOrDefault(x => x.ProductId == product.Id), allProductInventories.FirstOrDefault(x => x.ProductId == product.Id)); csvWriter.WriteRecord(csvProduct); } catch (Exception ex) { prodgressInfo.Errors.Add(ex.ToString()); progressCallback(prodgressInfo); } //Raise notification each notifyProductSizeLimit products counter++; prodgressInfo.ProcessedCount = counter; prodgressInfo.Description = string.Format("{0} of {1} products processed", prodgressInfo.ProcessedCount, prodgressInfo.TotalCount); if (counter % notifyProductSizeLimit == 0 || counter == prodgressInfo.TotalCount) { progressCallback(prodgressInfo); } } } }
// Only public methods can be invoked in the background. (Hangfire) public void BackgroundExport(CsvExportInfo exportInfo, ExportNotification notifyEvent) { var currencies = _commerceService.GetAllCurrencies(); var defaultCurrency = currencies.First(x => x.IsPrimary); exportInfo.Currency = exportInfo.Currency ?? defaultCurrency.Code; var catalog = _catalogService.GetById(exportInfo.CatalogId); if (catalog == null) { throw new NullReferenceException("catalog"); } Action<ExportImportProgressInfo> progressCallback = (x) => { notifyEvent.InjectFrom(x); _notifier.Upsert(notifyEvent); }; using (var stream = new MemoryStream()) { try { exportInfo.Configuration = CsvProductMappingConfiguration.GetDefaultConfiguration(); _csvExporter.DoExport(stream, exportInfo, progressCallback); stream.Position = 0; var blobRelativeUrl = "temp/Catalog-" + catalog.Name + "-export.csv"; //Upload result csv to blob storage using (var blobStream = _blobStorageProvider.OpenWrite(blobRelativeUrl)) { stream.CopyTo(blobStream); } //Get a download url notifyEvent.DownloadUrl = _blobUrlResolver.GetAbsoluteUrl(blobRelativeUrl); notifyEvent.Description = "Export finished"; } catch (Exception ex) { notifyEvent.Description = "Export failed"; notifyEvent.Errors.Add(ex.ExpandExceptionMessage()); } finally { notifyEvent.Finished = DateTime.UtcNow; _notifier.Upsert(notifyEvent); } } }
public void DoExport(Stream outStream, CsvExportInfo exportInfo, Action <ExportImportProgressInfo> progressCallback) { var prodgressInfo = new ExportImportProgressInfo { Description = "loading products..." }; var streamWriter = new StreamWriter(outStream, Encoding.UTF8, 1024, true); streamWriter.AutoFlush = true; using (var csvWriter = new CsvWriter(streamWriter)) { //Notification progressCallback(prodgressInfo); //Load all products to export var products = LoadProducts(exportInfo.CatalogId, exportInfo.CategoryIds, exportInfo.ProductIds); var allProductIds = products.Select(x => x.Id).ToArray(); //Load prices for products var allProductPrices = new List <Price>(); prodgressInfo.Description = "loading prices..."; progressCallback(prodgressInfo); var priceEvalContext = new PriceEvaluationContext { ProductIds = allProductIds, PricelistIds = exportInfo.PriceListId == null ? null : new string[] { exportInfo.PriceListId }, Currency = exportInfo.Currency }; allProductPrices = _pricingService.EvaluateProductPrices(priceEvalContext).ToList(); //Load inventories var allProductInventories = new List <InventoryInfo>(); prodgressInfo.Description = "loading inventory information..."; progressCallback(prodgressInfo); allProductInventories = _inventoryService.GetProductsInventoryInfos(allProductIds).Where(x => exportInfo.FulfilmentCenterId == null ? true : x.FulfillmentCenterId == exportInfo.FulfilmentCenterId).ToList(); //Export configuration exportInfo.Configuration.PropertyCsvColumns = products.SelectMany(x => x.PropertyValues).Select(x => x.PropertyName).Distinct().ToArray(); csvWriter.Configuration.Delimiter = exportInfo.Configuration.Delimiter; csvWriter.Configuration.RegisterClassMap(new CsvProductMap(exportInfo.Configuration)); //Write header csvWriter.WriteHeader <CsvProduct>(); prodgressInfo.TotalCount = products.Count(); var notifyProductSizeLimit = 50; var counter = 0; foreach (var product in products) { try { var csvProduct = new CsvProduct(product, _blobUrlResolver, allProductPrices.FirstOrDefault(x => x.ProductId == product.Id), allProductInventories.FirstOrDefault(x => x.ProductId == product.Id)); csvWriter.WriteRecord(csvProduct); } catch (Exception ex) { prodgressInfo.Errors.Add(ex.ToString()); progressCallback(prodgressInfo); } //Raise notification each notifyProductSizeLimit products counter++; prodgressInfo.ProcessedCount = counter; prodgressInfo.Description = string.Format("{0} of {1} products processed", prodgressInfo.ProcessedCount, prodgressInfo.TotalCount); if (counter % notifyProductSizeLimit == 0 || counter == prodgressInfo.TotalCount) { progressCallback(prodgressInfo); } } } }