public DataImportRequest(ImportProfile profile)
        {
            Guard.ArgumentNotNull(() => profile);

            Profile = profile;
            ProgressValueSetter = _voidProgressValueSetter;

            EntitiesToImport = new List<int>();
            CustomData = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
        }
        public virtual void DeleteImportProfile(ImportProfile profile)
        {
            if (profile == null)
                throw new ArgumentNullException("profile");

            var scheduleTaskId = profile.SchedulingTaskId;
            var folder = profile.GetImportFolder();

            _importProfileRepository.Delete(profile);

            var scheduleTask = _scheduleTaskService.GetTaskById(scheduleTaskId);
            _scheduleTaskService.DeleteTask(scheduleTask);

            _eventPublisher.EntityDeleted(profile);

            if (System.IO.Directory.Exists(folder))
            {
                FileSystemHelper.ClearDirectory(folder, true);
            }
        }
        private void PrepareProfileModel(ImportProfileModel model, ImportProfile profile, bool forEdit, ColumnMap invalidMap = null)
        {
            model.Id = profile.Id;
            model.Name = profile.Name;
            model.EntityType = profile.EntityType;
            model.Enabled = profile.Enabled;
            model.Skip = (profile.Skip == 0 ? (int?)null : profile.Skip);
            model.Take = (profile.Take == 0 ? (int?)null : profile.Take);
            model.UpdateOnly = profile.UpdateOnly;
            model.KeyFieldNames = profile.KeyFieldNames.SplitSafe(",").Distinct().ToArray();
            model.ScheduleTaskId = profile.SchedulingTaskId;
            model.ScheduleTaskName = profile.ScheduleTask.Name.NaIfEmpty();
            model.IsTaskRunning = profile.ScheduleTask.IsRunning;
            model.IsTaskEnabled = profile.ScheduleTask.Enabled;
            model.LogFileExists = System.IO.File.Exists(profile.GetImportLogPath());
            model.EntityTypeName = profile.EntityType.GetLocalizedEnum(Services.Localization, Services.WorkContext);

            model.ExistingFileNames = profile.GetImportFiles()
                .Select(x => Path.GetFileName(x))
                .ToList();

            if (profile.ResultInfo.HasValue())
                model.ImportResult = XmlHelper.Deserialize<SerializableImportResult>(profile.ResultInfo);

            if (!forEdit)
                return;

            CsvConfiguration csvConfiguration = null;

            if (profile.FileType == ImportFileType.CSV)
            {
                var csvConverter = new CsvConfigurationConverter();
                csvConfiguration = csvConverter.ConvertFrom<CsvConfiguration>(profile.FileTypeConfiguration) ?? CsvConfiguration.ExcelFriendlyConfiguration;

                model.CsvConfiguration = new CsvConfigurationModel(csvConfiguration);
            }
            else
            {
                csvConfiguration = CsvConfiguration.ExcelFriendlyConfiguration;
            }

            // common configuration
            var extraData = XmlHelper.Deserialize<ImportExtraData>(profile.ExtraData);
            model.ExtraData.NumberOfPictures = extraData.NumberOfPictures;

            // column mapping
            model.AvailableSourceColumns = new List<ColumnMappingItemModel>();
            model.AvailableEntityProperties = new List<ColumnMappingItemModel>();
            model.AvailableKeyFieldNames = new List<SelectListItem>();
            model.ColumnMappings = new List<ColumnMappingItemModel>();

            try
            {
                string[] availableKeyFieldNames = null;
                string[] disabledDefaultFieldNames = GetDisabledDefaultFieldNames(profile);
                var mapConverter = new ColumnMapConverter();
                var storedMap = mapConverter.ConvertFrom<ColumnMap>(profile.ColumnMapping);
                var map = (invalidMap ?? storedMap) ?? new ColumnMap();

                // property name to localized property name
                var allProperties = _importProfileService.GetImportableEntityProperties(profile.EntityType);

                switch (profile.EntityType)
                {
                    case ImportEntityType.Product:
                        availableKeyFieldNames = ProductImporter.SupportedKeyFields;
                        break;
                    case ImportEntityType.Category:
                        availableKeyFieldNames = CategoryImporter.SupportedKeyFields;
                        break;
                    case ImportEntityType.Customer:
                        availableKeyFieldNames = CustomerImporter.SupportedKeyFields;
                        break;
                    case ImportEntityType.NewsLetterSubscription:
                        availableKeyFieldNames = NewsLetterSubscriptionImporter.SupportedKeyFields;
                        break;
                }

                model.AvailableEntityProperties = allProperties
                    .Select(x =>
                    {
                        var mapping = new ColumnMappingItemModel
                        {
                            Property = x.Key,
                            PropertyDescription = x.Value,
                            IsDefaultDisabled = IsDefaultValueDisabled(x.Key, x.Key, disabledDefaultFieldNames)
                        };

                        return mapping;
                    })
                    .ToList();

                model.AvailableKeyFieldNames = availableKeyFieldNames
                    .Select(x =>
                    {
                        var item = new SelectListItem { Value = x, Text = x };

                        if (x == "Id")
                            item.Text = T("Admin.Common.Entity.Fields.Id");
                        else if (allProperties.ContainsKey(x))
                            item.Text = allProperties[x];

                        return item;
                    })
                    .ToList();

                model.ColumnMappings = map.Mappings
                    .Select(x =>
                    {
                        var mapping = new ColumnMappingItemModel
                        {
                            Column = x.Value.MappedName,
                            Property = x.Key,
                            Default = x.Value.Default
                        };

                        if (x.Value.IgnoreProperty)
                        {
                            // explicitly ignore the property
                            mapping.Column = null;
                            mapping.Default = null;
                        }

                        mapping.PropertyDescription = GetPropertyDescription(allProperties, mapping.Property);
                        mapping.IsDefaultDisabled = IsDefaultValueDisabled(mapping.Column, mapping.Property, disabledDefaultFieldNames);

                        return mapping;
                    })
                    .ToList();

                var files = profile.GetImportFiles();
                if (!files.Any())
                    return;

                var filePath = files.First();

                using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    var dataTable = LightweightDataTable.FromFile(Path.GetFileName(filePath), stream, stream.Length, csvConfiguration, 0, 1);

                    foreach (var column in dataTable.Columns.Where(x => x.Name.HasValue()))
                    {
                        string columnWithoutIndex, columnIndex;
                        ColumnMap.ParseSourceName(column.Name, out columnWithoutIndex, out columnIndex);

                        model.AvailableSourceColumns.Add(new ColumnMappingItemModel
                        {
                            Index = dataTable.Columns.IndexOf(column),
                            Column = column.Name,
                            ColumnWithoutIndex = columnWithoutIndex,
                            ColumnIndex = columnIndex,
                            PropertyDescription = GetPropertyDescription(allProperties, column.Name)
                        });

                        // auto map where field equals property name
                        if (!model.ColumnMappings.Any(x => x.Column == column.Name))
                        {
                            var kvp = allProperties.FirstOrDefault(x => x.Key.IsCaseInsensitiveEqual(column.Name));
                            if (kvp.Key.IsEmpty())
                            {
                                var alternativeName = LightweightDataTable.GetAlternativeColumnNameFor(column.Name);
                                kvp = allProperties.FirstOrDefault(x => x.Key.IsCaseInsensitiveEqual(alternativeName));
                            }

                            if (kvp.Key.HasValue() && !model.ColumnMappings.Any(x => x.Property == kvp.Key))
                            {
                                model.ColumnMappings.Add(new ColumnMappingItemModel
                                {
                                    Column = column.Name,
                                    Property = kvp.Key,
                                    PropertyDescription = kvp.Value,
                                    IsDefaultDisabled = IsDefaultValueDisabled(column.Name, kvp.Key, disabledDefaultFieldNames)
                                });
                            }
                        }
                    }

                    // sorting
                    model.AvailableSourceColumns = model.AvailableSourceColumns
                        .OrderBy(x => x.PropertyDescription)
                        .ToList();

                    model.AvailableEntityProperties = model.AvailableEntityProperties
                        .OrderBy(x => x.PropertyDescription)
                        .ToList();

                    model.ColumnMappings = model.ColumnMappings
                        .OrderBy(x => x.PropertyDescription)
                        .ToList();
                }
            }
            catch (Exception exception)
            {
                NotifyError(exception, true, false);
            }
        }
 private string[] GetDisabledDefaultFieldNames(ImportProfile profile)
 {
     switch (profile.EntityType)
     {
         case ImportEntityType.Product:
             return new string[] { "Name", "Sku", "ManufacturerPartNumber", "Gtin", "SeName" };
         case ImportEntityType.Category:
             return new string[] { "Name", "SeName" };
         case ImportEntityType.Customer:
             return new string[] { "CustomerGuid", "Email" };
         case ImportEntityType.NewsLetterSubscription:
             return new string[] { "Email" };
         default:
             return new string[0];
     }
 }
        public virtual void UpdateImportProfile(ImportProfile profile)
        {
            if (profile == null)
                throw new ArgumentNullException("profile");

            _importProfileRepository.Update(profile);

            _eventPublisher.EntityUpdated(profile);
        }
        public virtual ImportProfile InsertImportProfile(string fileName, string name, ImportEntityType entityType)
        {
            Guard.ArgumentNotEmpty(() => fileName);

            if (name.IsEmpty())
                name = GetNewProfileName(entityType);

            var task = new ScheduleTask
            {
                CronExpression = "0 */24 * * *",
                Type = typeof(DataImportTask).AssemblyQualifiedNameWithoutVersion(),
                Enabled = false,
                StopOnError = false,
                IsHidden = true
            };

            task.Name = string.Concat(name, " Task");

            _scheduleTaskService.InsertTask(task);

            var profile = new ImportProfile
            {
                Name = name,
                EntityType = entityType,
                Enabled = true,
                SchedulingTaskId = task.Id
            };

            if (Path.GetExtension(fileName).IsCaseInsensitiveEqual(".xlsx"))
                profile.FileType = ImportFileType.XLSX;
            else
                profile.FileType = ImportFileType.CSV;

            string[] keyFieldNames = null;

            switch (entityType)
            {
                case ImportEntityType.Product:
                    keyFieldNames = ProductImporter.DefaultKeyFields;
                    break;
                case ImportEntityType.Category:
                    keyFieldNames = CategoryImporter.DefaultKeyFields;
                    break;
                case ImportEntityType.Customer:
                    keyFieldNames = CustomerImporter.DefaultKeyFields;
                    break;
                case ImportEntityType.NewsLetterSubscription:
                    keyFieldNames = NewsLetterSubscriptionImporter.DefaultKeyFields;
                    break;
            }

            profile.KeyFieldNames = string.Join(",", keyFieldNames);

            profile.FolderName = SeoHelper.GetSeName(name, true, false)
                .ToValidPath()
                .Truncate(_dataExchangeSettings.MaxFileNameLength);

            profile.FolderName = FileSystemHelper.CreateNonExistingDirectoryName(CommonHelper.MapPath("~/App_Data/ImportProfiles"), profile.FolderName);

            _importProfileRepository.Insert(profile);

            task.Alias = profile.Id.ToString();
            _scheduleTaskService.UpdateTask(task);

            _eventPublisher.EntityInserted(profile);

            return profile;
        }