Example #1
0
        private void ImportCoreInner(DataImporterContext ctx, ImportFile file)
        {
            var context = ctx.ExecuteContext;
            var profile = ctx.Request.Profile;

            if (context.Abort == DataExchangeAbortion.Hard)
            {
                return;
            }

            if (!File.Exists(file.Path))
            {
                throw new SmartException($"File does not exist {file.Path}.");
            }

            using (var stream = new FileStream(file.Path, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                var csvConfiguration = file.IsCsv
                    ? (new CsvConfigurationConverter().ConvertFrom <CsvConfiguration>(profile.FileTypeConfiguration) ?? CsvConfiguration.ExcelFriendlyConfiguration)
                    : CsvConfiguration.ExcelFriendlyConfiguration;

                context.DataTable = LightweightDataTable.FromFile(
                    file.Name,
                    stream,
                    stream.Length,
                    csvConfiguration,
                    profile.Skip,
                    profile.Take > 0 ? profile.Take : int.MaxValue);

                context.ColumnMap = file.RelatedType.HasValue ? new ColumnMap() : ctx.ColumnMap;
                context.File      = file;

                try
                {
                    ctx.Importer.Execute(context);
                }
                catch (Exception ex)
                {
                    context.Abort = DataExchangeAbortion.Hard;
                    context.Result.AddError(ex, $"The importer failed: {ex.ToAllMessages()}.");
                }
                finally
                {
                    context.Result.EndDateUtc = DateTime.UtcNow;

                    if (context.IsMaxFailures)
                    {
                        context.Result.AddWarning("Import aborted. The maximum number of failures has been reached.");
                    }
                    if (ctx.CancellationToken.IsCancellationRequested)
                    {
                        context.Result.AddWarning("Import aborted. A cancellation has been requested.");
                    }
                }
            }
        }
Example #2
0
        private void ImportCoreInner(DataImporterContext ctx, string filePath)
        {
            if (ctx.ExecuteContext.Abort == DataExchangeAbortion.Hard)
            {
                return;
            }

            {
                var logHead = new StringBuilder();
                logHead.AppendLine();
                logHead.AppendLine(new string('-', 40));
                logHead.AppendLine("SmartStore.NET:\t\tv." + SmartStoreVersion.CurrentFullVersion);
                logHead.Append("Import profile:\t\t" + ctx.Request.Profile.Name);
                logHead.AppendLine(ctx.Request.Profile.Id == 0 ? " (volatile)" : " (Id {0})".FormatInvariant(ctx.Request.Profile.Id));

                logHead.AppendLine("Entity:\t\t\t" + ctx.Request.Profile.EntityType.ToString());
                logHead.AppendLine("File:\t\t\t" + Path.GetFileName(filePath));

                var customer = _services.WorkContext.CurrentCustomer;
                logHead.Append("Executed by:\t\t" + (customer.Email.HasValue() ? customer.Email : customer.SystemName));

                ctx.Log.Information(logHead.ToString());
            }

            if (!File.Exists(filePath))
            {
                throw new SmartException("File does not exist {0}.".FormatInvariant(filePath));
            }

            CsvConfiguration csvConfiguration = null;
            var extension = Path.GetExtension(filePath);

            if ((new string[] { ".csv", ".txt", ".tab" }).Contains(extension, StringComparer.OrdinalIgnoreCase))
            {
                var converter = new CsvConfigurationConverter();
                csvConfiguration = converter.ConvertFrom <CsvConfiguration>(ctx.Request.Profile.FileTypeConfiguration);
            }

            if (csvConfiguration == null)
            {
                csvConfiguration = CsvConfiguration.ExcelFriendlyConfiguration;
            }

            using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                ctx.ExecuteContext.DataTable = LightweightDataTable.FromFile(
                    Path.GetFileName(filePath),
                    stream,
                    stream.Length,
                    csvConfiguration,
                    ctx.Request.Profile.Skip,
                    ctx.Request.Profile.Take > 0 ? ctx.Request.Profile.Take : int.MaxValue
                    );

                try
                {
                    ctx.Importer.Execute(ctx.ExecuteContext);
                }
                catch (Exception exception)
                {
                    ctx.ExecuteContext.Abort = DataExchangeAbortion.Hard;
                    ctx.ExecuteContext.Result.AddError(exception, "The importer failed: {0}.".FormatInvariant(exception.ToAllMessages()));
                }

                if (ctx.ExecuteContext.IsMaxFailures)
                {
                    ctx.ExecuteContext.Result.AddWarning("Import aborted. The maximum number of failures has been reached.");
                }

                if (ctx.CancellationToken.IsCancellationRequested)
                {
                    ctx.ExecuteContext.Result.AddWarning("Import aborted. A cancellation has been requested.");
                }
            }
        }