private void PopulateProducts()
        {
            var products = _data.Products();

            SaveRange(products);

            // Fix MainPictureId
            DataMigrator.FixProductMainPictureIds(_ctx);

            PopulateUrlRecordsFor(products);

            _data.AssignGroupedProducts(products);
        }
        private async Task PopulateProducts()
        {
            var products = _data.Products();

            await SaveRange(products);

            _data.AddDownloads(products);

            // Fix MainPictureId
            await DataMigrator.FixProductMainPictureIds(_db);

            await PopulateUrlRecordsFor(products);

            _data.AssignGroupedProducts(products);
        }
예제 #3
0
        private void PopulateProducts()
        {
            var products = _data.Products();

            SaveRange(products);

            // Fix MainPictureId
            DataMigrator.FixProductMainPictureIds(_ctx);

            // Search engine names
            products.Each(x =>
            {
                Save(new UrlRecord
                {
                    EntityId   = x.Id,
                    EntityName = "Product",
                    LanguageId = 0,
                    Slug       = ValidateSeName(x, x.Name),
                    IsActive   = true
                });
            });

            _data.AssignGroupedProducts(products);
        }
예제 #4
0
        protected override void Import(ImportExecuteContext context)
        {
            var srcToDestId       = new Dictionary <int, ImportProductMapping>();
            var importStartTime   = DateTime.UtcNow;
            var templateViewPaths = _productTemplateService.GetAllProductTemplates().ToDictionarySafe(x => x.ViewPath, x => x.Id);

            using (var scope = new DbContextScope(ctx: _productRepository.Context, hooksEnabled: false, autoDetectChanges: false, proxyCreation: false, validateOnSave: false))
            {
                var segmenter = context.DataSegmenter;

                Initialize(context);

                while (context.Abort == DataExchangeAbortion.None && segmenter.ReadNextBatch())
                {
                    var batch = segmenter.GetCurrentBatch <Product>();

                    // Perf: detach entities
                    _productRepository.Context.DetachEntities(x =>
                    {
                        return(x is Product || x is UrlRecord || x is StoreMapping || x is ProductVariantAttribute || x is LocalizedProperty ||
                               x is ProductBundleItem || x is ProductCategory || x is ProductManufacturer || x is Category || x is Manufacturer ||
                               x is ProductPicture || x is Picture || x is ProductTag || x is TierPrice);
                    });
                    //_productRepository.Context.DetachAll(true);

                    context.SetProgress(segmenter.CurrentSegmentFirstRowIndex - 1, segmenter.TotalRows);

                    // ===========================================================================
                    // 1.) Import products
                    // ===========================================================================
                    int savedProducts = 0;
                    try
                    {
                        savedProducts = ProcessProducts(context, batch, templateViewPaths, srcToDestId);
                    }
                    catch (Exception exception)
                    {
                        context.Result.AddError(exception, segmenter.CurrentSegment, "ProcessProducts");
                    }

                    // reduce batch to saved (valid) products.
                    // No need to perform import operations on errored products.
                    batch = batch.Where(x => x.Entity != null && !x.IsTransient).ToArray();

                    // update result object
                    context.Result.NewRecords      += batch.Count(x => x.IsNew);
                    context.Result.ModifiedRecords += Math.Max(0, savedProducts - context.Result.NewRecords);

                    // ===========================================================================
                    // 2.) Import SEO Slugs
                    // IMPORTANT: Unlike with Products AutoCommitEnabled must be TRUE,
                    //            as Slugs are going to be validated against existing ones in DB.
                    // ===========================================================================
                    if (segmenter.HasColumn("SeName", true) || batch.Any(x => x.IsNew || x.NameChanged))
                    {
                        try
                        {
                            _productRepository.Context.AutoDetectChangesEnabled = true;
                            ProcessSlugs(context, batch, typeof(Product).Name);
                        }
                        catch (Exception exception)
                        {
                            context.Result.AddError(exception, segmenter.CurrentSegment, "ProcessSlugs");
                        }
                        finally
                        {
                            _productRepository.Context.AutoDetectChangesEnabled = false;
                        }
                    }

                    // ===========================================================================
                    // 3.) Import StoreMappings
                    // ===========================================================================
                    if (segmenter.HasColumn("StoreIds"))
                    {
                        try
                        {
                            ProcessStoreMappings(context, batch);
                        }
                        catch (Exception exception)
                        {
                            context.Result.AddError(exception, segmenter.CurrentSegment, "ProcessStoreMappings");
                        }
                    }

                    // ===========================================================================
                    // 4.) Import Localizations
                    // ===========================================================================
                    try
                    {
                        ProcessLocalizations(context, batch, _localizableProperties);
                    }
                    catch (Exception exception)
                    {
                        context.Result.AddError(exception, segmenter.CurrentSegment, "ProcessLocalizations");
                    }

                    // ===========================================================================
                    // 5.) Import product category mappings
                    // ===========================================================================
                    if (segmenter.HasColumn("CategoryIds"))
                    {
                        try
                        {
                            ProcessProductCategories(context, batch);
                        }
                        catch (Exception exception)
                        {
                            context.Result.AddError(exception, segmenter.CurrentSegment, "ProcessProductCategories");
                        }
                    }

                    // ===========================================================================
                    // 6.) Import product manufacturer mappings
                    // ===========================================================================
                    if (segmenter.HasColumn("ManufacturerIds"))
                    {
                        try
                        {
                            ProcessProductManufacturers(context, batch);
                        }
                        catch (Exception exception)
                        {
                            context.Result.AddError(exception, segmenter.CurrentSegment, "ProcessProductManufacturers");
                        }
                    }

                    // ===========================================================================
                    // 7.) Import product picture mappings
                    // ===========================================================================
                    if (segmenter.HasColumn("ImageUrls"))
                    {
                        try
                        {
                            ProcessProductPictures(context, batch);
                        }
                        catch (Exception exception)
                        {
                            context.Result.AddError(exception, segmenter.CurrentSegment, "ProcessProductPictures");
                        }
                    }
                }

                // ===========================================================================
                // 8.) Map parent id of inserted products
                // ===========================================================================
                if (srcToDestId.Any() && segmenter.HasColumn("Id") && segmenter.HasColumn("ParentGroupedProductId") && !segmenter.IsIgnored("ParentGroupedProductId"))
                {
                    segmenter.Reset();

                    while (context.Abort == DataExchangeAbortion.None && segmenter.ReadNextBatch())
                    {
                        var batch = segmenter.GetCurrentBatch <Product>();

                        _productRepository.Context.DetachAll(false);

                        try
                        {
                            ProcessProductMappings(context, batch, srcToDestId);
                        }
                        catch (Exception exception)
                        {
                            context.Result.AddError(exception, segmenter.CurrentSegment, "ProcessParentMappings");
                        }
                    }
                }

                // ===========================================================================
                // 9.) PostProcess: normalization
                // ===========================================================================
                DataMigrator.FixProductMainPictureIds(_productRepository.Context, importStartTime);
            }
        }
예제 #5
0
 public void Seed(SmartObjectContext context)
 {
     DataMigrator.FixProductMainPictureIds(context, true);
 }