public IHttpActionResult DoExport(CsvExportInfo exportInfo) { var notification = new ExportNotification(CurrentPrincipal.GetCurrentUserName()) { Title = "Catalog export task", Description = "starting export...." }; _notifier.Upsert(notification); var catalog = _catalogService.GetById(exportInfo.CatalogId); if (catalog == null) { throw new NullReferenceException("catalog"); } var curencySetting = _settingsManager.GetSettingByName("VirtoCommerce.Core.General.Currencies"); var defaultCurrency = EnumUtility.SafeParse <CurrencyCodes>(curencySetting.DefaultValue, CurrencyCodes.USD); var exportJob = new CsvCatalogExportJob(); BackgroundJob.Enqueue(() => exportJob.DoExport(exportInfo.CatalogId, exportInfo.CategoryIds, exportInfo.ProductIds, exportInfo.PriceListId, exportInfo.FulfilmentCenterId, exportInfo.Currency ?? defaultCurrency, catalog.DefaultLanguage.LanguageCode, notification)); return(Ok(notification)); }
// Only public methods can be invoked in the background. (Hangfire) public async Task BackgroundExport(CsvExportInfo exportInfo, ExportNotification notifyEvent) { var currencies = await _currencyService.GetAllCurrenciesAsync(); var defaultCurrency = currencies.First(x => x.IsPrimary); exportInfo.Currency ??= defaultCurrency.Code; var catalog = await _catalogService.GetByIdsAsync(new[] { exportInfo.CatalogId }); if (catalog == null) { throw new InvalidOperationException($"Cannot get catalog with id '{exportInfo.CatalogId}'"); } Action <ExportImportProgressInfo> progressCallback = async x => { notifyEvent.InjectFrom(x); await _notifier.SendAsync(notifyEvent); }; using (var stream = new MemoryStream()) { try { exportInfo.Configuration = CsvProductMappingConfiguration.GetDefaultConfiguration(); await _csvExporter.DoExportAsync(stream, exportInfo, progressCallback); stream.Position = 0; var fileNameTemplate = await _settingsManager.GetValueAsync(CsvModuleConstants.Settings.General.ExportFileNameTemplate.Name, CsvModuleConstants.Settings.General.ExportFileNameTemplate.DefaultValue.ToString()); var fileName = string.Format(fileNameTemplate, DateTime.UtcNow); fileName = Path.ChangeExtension(fileName, ".csv"); var blobRelativeUrl = Path.Combine("temp", fileName); //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; await _notifier.SendAsync(notifyEvent); } } }
// Only public methods can be invoked in the background. (Hangfire) public void BackgroundExport(Data.Model.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 = Data.Model.CsvProductMappingConfiguration.GetDefaultConfiguration(); _csvExporter.DoExport(stream, exportInfo, progressCallback); stream.Position = 0; var fileNameTemplate = _settingsManager.GetValue("CsvCatalogImport.ExportFileNameTemplate", string.Empty); var fileName = string.Format(fileNameTemplate, DateTime.UtcNow); fileName = Path.ChangeExtension(fileName, ".csv"); var blobRelativeUrl = Path.Combine("temp", fileName); //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 BackgroundExport(CsvExportInfo exportInfo, ExportNotification notifyEvent) { var curencySetting = _settingsManager.GetSettingByName("VirtoCommerce.Core.General.Currencies"); var defaultCurrency = EnumUtility.SafeParse <CurrencyCodes>(curencySetting.DefaultValue, CurrencyCodes.USD); exportInfo.Currency = exportInfo.Currency ?? defaultCurrency; 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; //Upload result csv to blob storage var uploadInfo = new UploadStreamInfo { FileName = "Catalog-" + catalog.Name + "-export.csv", FileByteStream = stream, FolderName = "temp" }; var blobKey = _blobStorageProvider.Upload(uploadInfo); //Get a download url notifyEvent.DownloadUrl = _blobUrlResolver.GetAbsoluteUrl(blobKey); notifyEvent.Description = "Export finished"; } catch (Exception ex) { notifyEvent.Description = "Export failed"; notifyEvent.Errors.Add(ex.ExpandExceptionMessage()); } finally { notifyEvent.Finished = DateTime.UtcNow; _notifier.Upsert(notifyEvent); } } }
// 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 IHttpActionResult DoExport(CsvExportInfo exportInfo) { var notification = new ExportNotification(CurrentPrincipal.GetCurrentUserName()) { Title = "Catalog export task", Description = "starting export...." }; _notifier.Upsert(notification); BackgroundJob.Enqueue(() => BackgroundExport(exportInfo, notification)); return(Ok(notification)); }
public IHttpActionResult DoExport(CsvExportInfo exportInfo) { 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 async Task <ActionResult <ExportNotification> > DoExport([FromBody] CsvExportInfo exportInfo) { var hasPermissions = true; if (!exportInfo.ProductIds.IsNullOrEmpty()) { var items = await _itemService.GetByIdsAsync(exportInfo.ProductIds, ItemResponseGroup.ItemInfo.ToString()); hasPermissions = await CheckCatalogPermission(items, CatalogModuleConstants.Security.Permissions.Read); } if (hasPermissions && !exportInfo.CategoryIds.IsNullOrEmpty()) { var categories = await _categoryService.GetByIdsAsync(exportInfo.CategoryIds, CategoryResponseGroup.Info.ToString()); hasPermissions = await CheckCatalogPermission(categories, CatalogModuleConstants.Security.Permissions.Read); } if (hasPermissions && !exportInfo.CatalogId.IsNullOrEmpty()) { var catalogs = await _catalogService.GetByIdsAsync(new[] { exportInfo.CatalogId }, CategoryResponseGroup.Info.ToString()); if (!catalogs.IsNullOrEmpty()) { hasPermissions = await CheckCatalogPermission(catalogs.First(), CatalogModuleConstants.Security.Permissions.Read); } } if (!hasPermissions) { return(Unauthorized()); } var notification = new ExportNotification(_userNameResolver.GetCurrentUserName()) { Title = "Catalog export task", Description = "starting export...." }; await _notifier.SendAsync(notification); BackgroundJob.Enqueue(() => BackgroundExport(exportInfo, notification)); return(Ok(notification)); }
public IHttpActionResult DoExport(StoreExportConfiguration exportConfiguration) { var notification = new ExportNotification(CurrentPrincipal.GetCurrentUserName()) { Title = "Store export task", Description = "starting export...." }; _notifier.Upsert(notification); var store = _storeService.GetById(exportConfiguration.StoreId); if (store == null) { throw new NullReferenceException("store"); } var backupStoreJob = new BackupStoreJob(); BackgroundJob.Enqueue(() => backupStoreJob.DoExport(exportConfiguration, notification)); return(Ok(notification)); }
public virtual void DoExport(string catalogId, string[] exportedCategories, string[] exportedProducts, string pricelistId, string fulfilmentCenterId, CurrencyCodes currency, string languageCode, ExportNotification notification) { var memoryStream = new MemoryStream(); var streamWriter = new StreamWriter(memoryStream); streamWriter.AutoFlush = true; var productPropertyInfos = typeof(CatalogProduct).GetProperties(BindingFlags.Instance | BindingFlags.Public); string catalogName = null; using (var csvWriter = new CsvWriter(streamWriter)) { csvWriter.Configuration.Delimiter = ";"; //Notification notification.Description = "loading products..."; _notifier.Upsert(notification); try { //Load all products to export var products = LoadProducts(catalogId, exportedCategories, exportedProducts); //Notification notification.Description = "loading prices..."; _notifier.Upsert(notification); var allProductIds = products.Select(x => x.Id).ToArray(); //Load prices for products var priceEvalContext = new PriceEvaluationContext { ProductIds = allProductIds, PricelistIds = pricelistId == null ? null : new string[] { pricelistId }, Currency = currency }; var allProductPrices = _pricingService.EvaluateProductPrices(priceEvalContext).ToArray(); foreach (var product in products) { product.Prices = allProductPrices.Where(x => x.ProductId == product.Id).ToList(); } //Load inventories notification.Description = "loading inventory information..."; _notifier.Upsert(notification); var allProductInventories = _inventoryService.GetProductsInventoryInfos(allProductIds); foreach (var product in products) { product.Inventories = allProductInventories.Where(x => x.ProductId == product.Id) .Where(x => fulfilmentCenterId == null ? true : x.FulfillmentCenterId == fulfilmentCenterId).ToList(); } notification.TotalCount = products.Count(); //populate export configuration Dictionary <string, Func <CatalogProduct, string> > exportConfiguration = new Dictionary <string, Func <CatalogProduct, string> >(); PopulateProductExportConfiguration(exportConfiguration, products); //Write header foreach (var cfgItem in exportConfiguration) { csvWriter.WriteField(cfgItem.Key); } csvWriter.NextRecord(); var notifyProductSizeLimit = 50; var counter = 0; //Write products foreach (var product in products) { if (catalogName == null && product.Catalog != null) { catalogName = product.Catalog.Name; } try { foreach (var cfgItem in exportConfiguration) { var fieldValue = String.Empty; if (cfgItem.Value == null) { var propertyInfo = productPropertyInfos.FirstOrDefault(x => x.Name == cfgItem.Key); if (propertyInfo != null) { var objValue = propertyInfo.GetValue(product); fieldValue = objValue != null?objValue.ToString() : fieldValue; } } else { fieldValue = cfgItem.Value(product); } csvWriter.WriteField(fieldValue); } csvWriter.NextRecord(); } catch (Exception ex) { notification.ErrorCount++; notification.Errors.Add(ex.ToString()); _notifier.Upsert(notification); } //Raise notification each notifyProductSizeLimit products counter++; notification.ProcessedCount = counter; notification.Description = string.Format("{0} of {1} products processed", notification.ProcessedCount, notification.TotalCount); if (counter % notifyProductSizeLimit == 0) { _notifier.Upsert(notification); } } memoryStream.Position = 0; //Upload result csv to blob storage var uploadInfo = new UploadStreamInfo { FileName = "Catalog-" + (catalogName ?? catalogId) + "-export.csv", FileByteStream = memoryStream, FolderName = "temp" }; var blobKey = _blobStorageProvider.Upload(uploadInfo); //Get a download url notification.DownloadUrl = _blobUrlResolver.GetAbsoluteUrl(blobKey); notification.Description = "Export finished"; } catch (Exception ex) { notification.Description = "Export error"; notification.ErrorCount++; notification.Errors.Add(ex.ToString()); } finally { notification.Finished = DateTime.UtcNow; _notifier.Upsert(notification); } } }
public virtual void DoExport(StoreExportConfiguration exportConfiguration, ExportNotification notification) { //Notification notification.Description = "loading ..."; _notifier.Upsert(notification); try { var store = _storeService.GetById(exportConfiguration.StoreId); var settings = new List <SettingEntry>(); var backup = new Backup(_blobStorageProvider, _blobUrlResolver); // Add objects to backup backup.AddEntry(_stopeXmlName, store); // Add collections of the same types elements if (!exportConfiguration.IsDisableLanguages) { backup.AddEntry(_storeLanguagesXmlName, store.Languages.ToArray()); } if (!exportConfiguration.IsDisableCurrencies) { backup.AddEntry(_storeCurrenciesXmlName, store.Currencies.ToArray()); } if (!exportConfiguration.IsDisableSeo) { backup.AddEntry(_storeSeoXmlName, store.SeoInfos.ToArray()); } // Add collections of different types elements if (!exportConfiguration.IsDisablePamentMethods) { foreach (var paymentMethod in store.PaymentMethods.Where(x => x.IsActive)) { //don't export Settings because security backup.AddEntry(string.Format("{0}{1}.xml", _paymentMethodXmlNamePrefix, paymentMethod.Code), paymentMethod); } } if (!exportConfiguration.IsDisableShipmentMethods) { foreach (var shippingMethod in store.ShippingMethods.Where(x => x.IsActive)) { backup.AddEntry(string.Format("{0}{1}.xml", _shippingMethodXmlNamePrefix, shippingMethod.Code), shippingMethod); settings.AddRange(shippingMethod.Settings); } } settings.AddRange(store.Settings); backup.AddEntry(_settingsXmlName, settings.ToArray()); // Create backup file var zipUrl = backup.Save("Store-" + (store.Name ?? store.Id) + ".zip"); notification.DownloadUrl = zipUrl; notification.Description = "Export finished"; } catch (Exception ex) { notification.Description = "Export error"; notification.ErrorCount++; notification.Errors.Add(ex.ToString()); } finally { notification.Finished = DateTime.UtcNow; _notifier.Upsert(notification); } }