protected void Initialize(ImportExecuteContext context)
        {
            UtcNow              = DateTime.UtcNow;
            DownloadedItems     = new Dictionary <string, string>();
            ImageDownloadFolder = Path.Combine(context.ImportFolder, _imageDownloadFolder);

            var settings = context.DataExchangeSettings;

            if (settings.ImageImportFolder.HasValue())
            {
                ImageFolder = Path.Combine(context.ImportFolder, settings.ImageImportFolder);
            }
            else
            {
                ImageFolder = context.ImportFolder;
            }

            if (!System.IO.Directory.Exists(ImageDownloadFolder))
            {
                System.IO.Directory.CreateDirectory(ImageDownloadFolder);
            }

            DownloaderContext = new FileDownloadManagerContext
            {
                Timeout           = TimeSpan.FromMinutes(settings.ImageDownloadTimeout),
                Logger            = context.Log,
                CancellationToken = context.CancellationToken
            };

            context.Result.TotalRecords = context.DataSegmenter.TotalRows;
        }
        protected virtual int ProcessLocalizations <TEntity>(
            ImportExecuteContext context,
            IEnumerable <ImportRow <TEntity> > batch,
            IDictionary <string, Expression <Func <TEntity, string> > > localizableProperties) where TEntity : BaseEntity, ILocalizedEntity
        {
            Guard.ArgumentNotNull(() => context);
            Guard.ArgumentNotNull(() => batch);
            Guard.ArgumentNotNull(() => localizableProperties);

            // Perf: determine whether our localizable properties actually have
            // counterparts in the source BEFORE import batch begins. This way we spare ourself
            // to query over and over for values.
            var localizedProps = (from kvp in localizableProperties
                                  where context.DataSegmenter.GetColumnIndexes(kvp.Key).Length > 0
                                  select kvp.Key).ToArray();

            if (localizedProps.Length == 0)
            {
                return(0);
            }

            var localizedEntityService = context.Services.Resolve <ILocalizedEntityService>();

            bool shouldSave = false;

            foreach (var row in batch)
            {
                foreach (var prop in localizedProps)
                {
                    var lambda = localizableProperties[prop];
                    foreach (var lang in context.Languages)
                    {
                        var    code = lang.UniqueSeoCode;
                        string value;

                        if (row.TryGetDataValue(prop /* ColumnName */, code, out value))
                        {
                            localizedEntityService.SaveLocalizedValue(row.Entity, lambda, value, lang.Id);
                            shouldSave = true;
                        }
                    }
                }
            }

            if (shouldSave)
            {
                // commit whole batch at once
                return(context.Services.DbContext.SaveChanges());
            }

            return(0);
        }
        protected virtual int ProcessStoreMappings <TEntity>(
            ImportExecuteContext context,
            IEnumerable <ImportRow <TEntity> > batch) where TEntity : BaseEntity, IStoreMappingSupported
        {
            var storeMappingService    = context.Services.Resolve <IStoreMappingService>();
            var storeMappingRepository = context.Services.Resolve <IRepository <StoreMapping> >();

            storeMappingRepository.AutoCommitEnabled = false;

            foreach (var row in batch)
            {
                var storeIds = row.GetDataValue <List <int> >("StoreIds");
                if (!storeIds.IsNullOrEmpty())
                {
                    storeMappingService.SaveStoreMappings(row.Entity, storeIds.ToArray());
                }
            }

            // commit whole batch at once
            return(context.Services.DbContext.SaveChanges());
        }
 protected abstract void Import(ImportExecuteContext context);
 public void Execute(ImportExecuteContext context)
 {
     Import(context);
 }
        protected virtual int ProcessSlugs <TEntity>(
            ImportExecuteContext context,
            IEnumerable <ImportRow <TEntity> > batch,
            string entityName) where TEntity : BaseEntity, ISlugSupported
        {
            var       slugMap   = new Dictionary <string, UrlRecord>();
            UrlRecord urlRecord = null;

            var urlRecordService    = context.Services.Resolve <IUrlRecordService>();
            var urlRecordRepository = context.Services.Resolve <IRepository <UrlRecord> >();
            var seoSettings         = context.Services.Resolve <SeoSettings>();

            Func <string, UrlRecord> slugLookup = ((s) =>
            {
                return(slugMap.ContainsKey(s) ? slugMap[s] : null);
            });

            foreach (var row in batch)
            {
                try
                {
                    string seName        = null;
                    string localizedName = null;

                    if (row.TryGetDataValue("SeName", out seName) || row.IsNew || row.NameChanged)
                    {
                        seName = row.Entity.ValidateSeName(seName, row.EntityDisplayName, true, urlRecordService, seoSettings, extraSlugLookup: slugLookup);

                        if (row.IsNew)
                        {
                            // dont't bother validating SeName for new entities.
                            urlRecord = new UrlRecord
                            {
                                EntityId   = row.Entity.Id,
                                EntityName = entityName,
                                Slug       = seName,
                                LanguageId = 0,
                                IsActive   = true,
                            };
                            urlRecordRepository.Insert(urlRecord);
                        }
                        else
                        {
                            urlRecord = urlRecordService.SaveSlug(row.Entity, seName, 0);
                        }

                        if (urlRecord != null)
                        {
                            // a new record was inserted to the store: keep track of it for this batch.
                            slugMap[seName] = urlRecord;
                        }
                    }

                    // process localized SeNames
                    foreach (var lang in context.Languages)
                    {
                        var hasSeName        = row.TryGetDataValue("SeName", lang.UniqueSeoCode, out seName);
                        var hasLocalizedName = row.TryGetDataValue("Name", lang.UniqueSeoCode, out localizedName);

                        if (hasSeName || hasLocalizedName)
                        {
                            seName    = row.Entity.ValidateSeName(seName, localizedName, false, urlRecordService, seoSettings, lang.Id, slugLookup);
                            urlRecord = urlRecordService.SaveSlug(row.Entity, seName, lang.Id);
                            if (urlRecord != null)
                            {
                                slugMap[seName] = urlRecord;
                            }
                        }
                    }
                }
                catch (Exception exception)
                {
                    context.Result.AddWarning(exception.Message, row.GetRowInfo(), "SeName");
                }
            }

            // commit whole batch at once
            return(context.Services.DbContext.SaveChanges());
        }