-
No results found.
Try your search with a different keyword or use * as a wildcard.
ImportManager.cs
using ClosedXML.Excel;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.DependencyInjection;
using Nop.Core;
using Nop.Core.Domain.Catalog;
using Nop.Core.Domain.Common;
using Nop.Core.Domain.Customers;
using Nop.Core.Domain.Directory;
using Nop.Core.Domain.Localization;
using Nop.Core.Domain.Media;
using Nop.Core.Domain.Messages;
using Nop.Core.Domain.Orders;
using Nop.Core.Domain.Payments;
using Nop.Core.Domain.Security;
using Nop.Core.Domain.Shipping;
using Nop.Core.Domain.Stores;
using Nop.Core.Domain.Tax;
using Nop.Core.Domain.Vendors;
using Nop.Core.Http;
using Nop.Core.Infrastructure;
using Nop.Data;
using Nop.Services.Catalog;
using Nop.Services.Common;
using Nop.Services.Customers;
using Nop.Services.Directory;
using Nop.Services.ExportImport.Help;
using Nop.Services.Localization;
using Nop.Services.Logging;
using Nop.Services.Media;
using Nop.Services.Messages;
using Nop.Services.Orders;
using Nop.Services.Seo;
using Nop.Services.Shipping;
using Nop.Services.Shipping.Date;
using Nop.Services.Stores;
using Nop.Services.Tax;
using Nop.Services.Vendors;
namespace Nop.Services.ExportImport;
///
/// Import manager
///
public partial class ImportManager : IImportManager
{
#region Fields
protected readonly CatalogSettings _catalogSettings;
protected readonly IAddressService _addressService;
protected readonly IBackInStockSubscriptionService _backInStockSubscriptionService;
protected readonly ICategoryService _categoryService;
protected readonly ICountryService _countryService;
protected readonly ICustomerActivityService _customerActivityService;
protected readonly ICustomerService _customerService;
protected readonly ICustomNumberFormatter _customNumberFormatter;
protected readonly INopDataProvider _dataProvider;
protected readonly IDateRangeService _dateRangeService;
protected readonly IGenericAttributeService _genericAttributeService;
protected readonly IHttpClientFactory _httpClientFactory;
protected readonly ILanguageService _languageService;
protected readonly ILocalizationService _localizationService;
protected readonly ILocalizedEntityService _localizedEntityService;
protected readonly ILogger _logger;
protected readonly IManufacturerService _manufacturerService;
protected readonly IMeasureService _measureService;
protected readonly INewsLetterSubscriptionService _newsLetterSubscriptionService;
protected readonly INopFileProvider _fileProvider;
protected readonly IOrderService _orderService;
protected readonly IPictureService _pictureService;
protected readonly IProductAttributeService _productAttributeService;
protected readonly IProductService _productService;
protected readonly IProductTagService _productTagService;
protected readonly IProductTemplateService _productTemplateService;
protected readonly IServiceScopeFactory _serviceScopeFactory;
protected readonly IShippingService _shippingService;
protected readonly ISpecificationAttributeService _specificationAttributeService;
protected readonly IStateProvinceService _stateProvinceService;
protected readonly IStoreContext _storeContext;
protected readonly IStoreMappingService _storeMappingService;
protected readonly IStoreService _storeService;
protected readonly ITaxCategoryService _taxCategoryService;
protected readonly IUrlRecordService _urlRecordService;
protected readonly IVendorService _vendorService;
protected readonly IWorkContext _workContext;
protected readonly MediaSettings _mediaSettings;
protected readonly SecuritySettings _securitySettings;
protected readonly TaxSettings _taxSettings;
protected readonly VendorSettings _vendorSettings;
private static readonly string[] _separator = [">>"];
#endregion
#region Ctor
public ImportManager(CatalogSettings catalogSettings,
IAddressService addressService,
IBackInStockSubscriptionService backInStockSubscriptionService,
ICategoryService categoryService,
ICountryService countryService,
ICustomerActivityService customerActivityService,
ICustomerService customerService,
ICustomNumberFormatter customNumberFormatter,
INopDataProvider dataProvider,
IDateRangeService dateRangeService,
IGenericAttributeService genericAttributeService,
IHttpClientFactory httpClientFactory,
ILanguageService languageService,
ILocalizationService localizationService,
ILocalizedEntityService localizedEntityService,
ILogger logger,
IManufacturerService manufacturerService,
IMeasureService measureService,
INewsLetterSubscriptionService newsLetterSubscriptionService,
INopFileProvider fileProvider,
IOrderService orderService,
IPictureService pictureService,
IProductAttributeService productAttributeService,
IProductService productService,
IProductTagService productTagService,
IProductTemplateService productTemplateService,
IServiceScopeFactory serviceScopeFactory,
IShippingService shippingService,
ISpecificationAttributeService specificationAttributeService,
IStateProvinceService stateProvinceService,
IStoreContext storeContext,
IStoreMappingService storeMappingService,
IStoreService storeService,
ITaxCategoryService taxCategoryService,
IUrlRecordService urlRecordService,
IVendorService vendorService,
IWorkContext workContext,
MediaSettings mediaSettings,
SecuritySettings securitySettings,
TaxSettings taxSettings,
VendorSettings vendorSettings)
{
_addressService = addressService;
_backInStockSubscriptionService = backInStockSubscriptionService;
_catalogSettings = catalogSettings;
_categoryService = categoryService;
_countryService = countryService;
_customerActivityService = customerActivityService;
_customerService = customerService;
_customNumberFormatter = customNumberFormatter;
_dataProvider = dataProvider;
_dateRangeService = dateRangeService;
_genericAttributeService = genericAttributeService;
_httpClientFactory = httpClientFactory;
_fileProvider = fileProvider;
_languageService = languageService;
_localizationService = localizationService;
_localizedEntityService = localizedEntityService;
_logger = logger;
_manufacturerService = manufacturerService;
_measureService = measureService;
_newsLetterSubscriptionService = newsLetterSubscriptionService;
_orderService = orderService;
_pictureService = pictureService;
_productAttributeService = productAttributeService;
_productService = productService;
_productTagService = productTagService;
_productTemplateService = productTemplateService;
_serviceScopeFactory = serviceScopeFactory;
_shippingService = shippingService;
_specificationAttributeService = specificationAttributeService;
_stateProvinceService = stateProvinceService;
_storeContext = storeContext;
_storeMappingService = storeMappingService;
_storeService = storeService;
_taxCategoryService = taxCategoryService;
_urlRecordService = urlRecordService;
_vendorService = vendorService;
_workContext = workContext;
_mediaSettings = mediaSettings;
_securitySettings = securitySettings;
_taxSettings = taxSettings;
_vendorSettings = vendorSettings;
}
#endregion
#region Utilities
protected virtual ExportedAttributeType GetTypeOfExportedAttribute(IXLWorksheet defaultWorksheet, List localizedWorksheets, PropertyManager productAttributeManager, PropertyManager specificationAttributeManager, int iRow)
{
productAttributeManager.ReadDefaultFromXlsx(defaultWorksheet, iRow, ExportProductAttribute.ProductAttributeCellOffset);
if (productAttributeManager.IsCaption)
{
foreach (var worksheet in localizedWorksheets)
productAttributeManager.ReadLocalizedFromXlsx(worksheet, iRow, ExportProductAttribute.ProductAttributeCellOffset);
return ExportedAttributeType.ProductAttribute;
}
specificationAttributeManager.ReadDefaultFromXlsx(defaultWorksheet, iRow, ExportProductAttribute.ProductAttributeCellOffset);
if (specificationAttributeManager.IsCaption)
{
foreach (var worksheet in localizedWorksheets)
specificationAttributeManager.ReadLocalizedFromXlsx(worksheet, iRow, ExportProductAttribute.ProductAttributeCellOffset);
return ExportedAttributeType.SpecificationAttribute;
}
return ExportedAttributeType.NotSpecified;
}
/// A task that represents the asynchronous operation
protected virtual async Task SetOutLineForSpecificationAttributeRowAsync(object cellValue, IXLWorksheet worksheet, int endRow)
{
var attributeType = (cellValue ?? string.Empty).ToString();
if (attributeType.Equals("AttributeType", StringComparison.InvariantCultureIgnoreCase))
{
worksheet.Row(endRow).OutlineLevel = 1;
}
else
{
if ((await SpecificationAttributeType.Option.ToSelectListAsync(useLocalization: false))
.Any(p => p.Text.Equals(attributeType, StringComparison.InvariantCultureIgnoreCase)))
worksheet.Row(endRow).OutlineLevel = 1;
else if (int.TryParse(attributeType, out var attributeTypeId) && Enum.IsDefined(typeof(SpecificationAttributeType), attributeTypeId))
worksheet.Row(endRow).OutlineLevel = 1;
}
}
protected virtual void CopyDataToNewFile(ImportProductMetadata metadata, IXLWorksheet worksheet, string filePath, int startRow, int endRow, int endCell)
{
using var workbook = new XLWorkbook();
// get handles to the worksheets
var outWorksheet = workbook.Worksheets.Add(nameof(Product));
metadata.Manager.WriteDefaultCaption(outWorksheet);
var outRow = 2;
for (var row = startRow; row <= endRow; row++)
{
outWorksheet.Row(outRow).OutlineLevel = worksheet.Row(row).OutlineLevel;
for (var cell = 1; cell <= endCell; cell++)
outWorksheet.Row(outRow).Cell(cell).Value = worksheet.Row(row).Cell(cell).Value;
outRow += 1;
}
workbook.SaveAs(filePath);
}
protected virtual int GetColumnIndex(string[] properties, string columnName)
{
ArgumentNullException.ThrowIfNull(properties);
ArgumentNullException.ThrowIfNull(columnName);
for (var i = 0; i < properties.Length; i++)
if (properties[i].Equals(columnName, StringComparison.InvariantCultureIgnoreCase))
return i + 1; //excel indexes start from 1
return 0;
}
protected virtual string GetMimeTypeFromFilePath(string filePath)
{
new FileExtensionContentTypeProvider().TryGetContentType(filePath, out var mimeType);
//set to jpeg in case mime type cannot be found
return mimeType ?? _pictureService.GetPictureContentTypeByFileExtension(_fileProvider.GetFileExtension(filePath));
}
///
/// Creates or loads the image
///
/// The path to the image file
/// The name of the object
/// Image identifier, may be null
///
/// A task that represents the asynchronous operation
/// The task result contains the image or null if the image has not changed
///
protected virtual async Task