Example #1
0
        private void ImportCoreOuter(DataImporterContext ctx)
        {
            if (ctx.Request.Profile == null || !ctx.Request.Profile.Enabled)
            {
                return;
            }

            var logPath = ctx.Request.Profile.GetImportLogPath();

            FileSystemHelper.Delete(logPath);

            using (var logger = new TraceLogger(logPath))
            {
                try
                {
                    ctx.Log = logger;

                    ctx.ExecuteContext.DataExchangeSettings = _dataExchangeSettings.Value;
                    ctx.ExecuteContext.Services             = _services;
                    ctx.ExecuteContext.Log           = logger;
                    ctx.ExecuteContext.Languages     = _languageService.GetAllLanguages(true);
                    ctx.ExecuteContext.UpdateOnly    = ctx.Request.Profile.UpdateOnly;
                    ctx.ExecuteContext.KeyFieldNames = ctx.Request.Profile.KeyFieldNames.SplitSafe(",");
                    ctx.ExecuteContext.ImportFolder  = ctx.Request.Profile.GetImportFolder();
                    ctx.ExecuteContext.ExtraData     = XmlHelper.Deserialize <ImportExtraData>(ctx.Request.Profile.ExtraData);

                    {
                        var mapConverter = new ColumnMapConverter();
                        ctx.ExecuteContext.ColumnMap = mapConverter.ConvertFrom <ColumnMap>(ctx.Request.Profile.ColumnMapping) ?? new ColumnMap();
                    }

                    var files = ctx.Request.Profile.GetImportFiles();

                    if (files.Count == 0)
                    {
                        throw new SmartException("No files to import.");
                    }

                    if (!HasPermission(ctx))
                    {
                        throw new SmartException("You do not have permission to perform the selected import.");
                    }

                    ctx.Importer = _importerFactory(ctx.Request.Profile.EntityType);

                    files.ForEach(x => ImportCoreInner(ctx, x));
                }
                catch (Exception exception)
                {
                    ctx.ExecuteContext.Result.AddError(exception);
                }
                finally
                {
                    try
                    {
                        // database context sharing problem: if there are entities in modified state left by the provider due to SaveChanges failure,
                        // then all subsequent SaveChanges would fail too (e.g. IImportProfileService.UpdateImportProfile, IScheduledTaskService.UpdateTask...).
                        // so whatever it is, detach\dispose all what the tracker still has tracked.

                        _services.DbContext.DetachAll(false);
                    }
                    catch (Exception exception)
                    {
                        ctx.ExecuteContext.Result.AddError(exception);
                    }

                    try
                    {
                        SendCompletionEmail(ctx);
                    }
                    catch (Exception exception)
                    {
                        ctx.ExecuteContext.Result.AddError(exception);
                    }

                    try
                    {
                        ctx.ExecuteContext.Result.EndDateUtc = DateTime.UtcNow;

                        LogResult(ctx);
                    }
                    catch (Exception exception)
                    {
                        logger.ErrorsAll(exception);
                    }

                    try
                    {
                        ctx.Request.Profile.ResultInfo = XmlHelper.Serialize(ctx.ExecuteContext.Result.Clone());

                        _importProfileService.UpdateImportProfile(ctx.Request.Profile);
                    }
                    catch (Exception exception)
                    {
                        logger.ErrorsAll(exception);
                    }

                    try
                    {
                        ctx.Request.CustomData.Clear();
                        ctx.Log = null;
                    }
                    catch (Exception exception)
                    {
                        logger.ErrorsAll(exception);
                    }
                }
            }
        }
Example #2
0
        private void ImportCoreOuter(DataImporterContext ctx)
        {
            if (ctx.Request.Profile == null || !ctx.Request.Profile.Enabled)
            {
                return;
            }

            var logPath = ctx.Request.Profile.GetImportLogPath();

            FileSystemHelper.Delete(logPath);

            using (var logger = new TraceLogger(logPath))
            {
                try
                {
                    ctx.Log = logger;

                    ctx.ExecuteContext.Log           = logger;
                    ctx.ExecuteContext.Languages     = _languageService.Value.GetAllLanguages(true);
                    ctx.ExecuteContext.UpdateOnly    = ctx.Request.Profile.UpdateOnly;
                    ctx.ExecuteContext.KeyFieldNames = ctx.Request.Profile.KeyFieldNames.SplitSafe(",");
                    ctx.ExecuteContext.ImportFolder  = ctx.Request.Profile.GetImportFolder();

                    {
                        var mapConverter = new ColumnMapConverter();
                        ctx.ExecuteContext.ColumnMap = mapConverter.ConvertFrom <ColumnMap>(ctx.Request.Profile.ColumnMapping) ?? new ColumnMap();
                    }

                    var files = ctx.Request.Profile.GetImportFiles();

                    if (files.Count == 0)
                    {
                        throw new SmartException("No files to import found.");
                    }

                    if (!HasPermission(ctx))
                    {
                        throw new SmartException("You do not have permission to perform the selected import.");
                    }

                    if (ctx.Request.Profile.EntityType == ImportEntityType.Product)
                    {
                        ctx.Importer = new ProductImporter(
                            _productPictureRepository.Value,
                            _productManufacturerRepository.Value,
                            _productCategoryRepository.Value,
                            _urlRecordRepository.Value,
                            _productRepository.Value,
                            _services,
                            _localizedEntityService.Value,
                            _pictureService.Value,
                            _manufacturerService.Value,
                            _categoryService.Value,
                            _productService.Value,
                            _urlRecordService.Value,
                            _productTemplateService.Value,
                            _storeMappingService.Value,
                            _fileDownloadManager.Value,
                            _seoSettings.Value,
                            _dataExchangeSettings.Value);
                    }
                    else if (ctx.Request.Profile.EntityType == ImportEntityType.Customer)
                    {
                        ctx.Importer = new CustomerImporter(
                            _customerRepository.Value,
                            _pictureRepository.Value,
                            _services,
                            _genericAttributeService.Value,
                            _customerService,
                            _pictureService.Value,
                            _affiliateService.Value,
                            _countryService.Value,
                            _stateProvinceService.Value,
                            _fileDownloadManager.Value,
                            _customerSettings.Value,
                            _dateTimeSettings.Value,
                            _forumSettings.Value,
                            _dataExchangeSettings.Value);
                    }
                    else if (ctx.Request.Profile.EntityType == ImportEntityType.NewsLetterSubscription)
                    {
                        ctx.Importer = new NewsLetterSubscriptionImporter(
                            _services,
                            _subscriptionRepository.Value);
                    }
                    else if (ctx.Request.Profile.EntityType == ImportEntityType.Category)
                    {
                        ctx.Importer = new CategoryImporter(
                            _categoryRepository.Value,
                            _urlRecordRepository.Value,
                            _pictureRepository.Value,
                            _services,
                            _urlRecordService.Value,
                            _categoryTemplateService.Value,
                            _storeMappingService.Value,
                            _pictureService.Value,
                            _localizedEntityService.Value,
                            _fileDownloadManager.Value,
                            _seoSettings.Value,
                            _dataExchangeSettings.Value);
                    }
                    else
                    {
                        throw new SmartException("Unsupported entity type {0}.".FormatInvariant(ctx.Request.Profile.EntityType.ToString()));
                    }

                    files.ForEach(x => ImportCoreInner(ctx, x));
                }
                catch (Exception exception)
                {
                    ctx.ExecuteContext.Result.AddError(exception);
                }
                finally
                {
                    try
                    {
                        // database context sharing problem: if there are entities in modified state left by the provider due to SaveChanges failure,
                        // then all subsequent SaveChanges would fail too (e.g. IImportProfileService.UpdateImportProfile, IScheduledTaskService.UpdateTask...).
                        // so whatever it is, detach\dispose all that the tracker still has tracked.

                        _services.DbContext.DetachAll(false);
                    }
                    catch (Exception exception)
                    {
                        ctx.ExecuteContext.Result.AddError(exception);
                    }

                    try
                    {
                        SendCompletionEmail(ctx);
                    }
                    catch (Exception exception)
                    {
                        ctx.ExecuteContext.Result.AddError(exception);
                    }

                    try
                    {
                        ctx.ExecuteContext.Result.EndDateUtc = DateTime.UtcNow;

                        LogResult(ctx);
                    }
                    catch (Exception exception)
                    {
                        logger.ErrorsAll(exception);
                    }

                    try
                    {
                        ctx.Request.Profile.ResultInfo = XmlHelper.Serialize(ctx.ExecuteContext.Result.Clone());

                        _importProfileService.UpdateImportProfile(ctx.Request.Profile);
                    }
                    catch (Exception exception)
                    {
                        logger.ErrorsAll(exception);
                    }

                    try
                    {
                        ctx.Request.CustomData.Clear();
                        ctx.Log = null;
                    }
                    catch (Exception exception)
                    {
                        logger.ErrorsAll(exception);
                    }
                }
            }
        }