protected override void Export(ExportExecuteContext context)
        {
            var ignored     = 0;
            var categoryIds = context.CustomProperties["CategoryIds"] as HashSet <int>;

            using (var writer = XmlWriter.Create(context.DataStream, ShopConnectorService.DefaultSettings))
            {
                var helper = new ExportXmlHelper(writer, true);

                writer.WriteStartElement("Content");
                writer.WriteStartElement("Categories");
                writer.WriteAttributeString("Version", SmartStoreVersion.CurrentVersion);

                while (context.Abort == DataExchangeAbortion.None && context.DataSegmenter.ReadNextSegment())
                {
                    var segment = context.DataSegmenter.CurrentSegment;

                    foreach (dynamic category in segment)
                    {
                        if (context.Abort != DataExchangeAbortion.None)
                        {
                            break;
                        }

                        Category entity = category.Entity;

                        try
                        {
                            if (categoryIds != null && !categoryIds.Contains(entity.Id))
                            {
                                ++ignored;
                            }
                            else
                            {
                                helper.WriteCategory(category, "Category");

                                ++context.RecordsSucceeded;
                            }
                        }
                        catch (OutOfMemoryException)
                        {
                            context.Abort = DataExchangeAbortion.Hard;
                            throw;
                        }
                        catch (Exception ex)
                        {
                            context.RecordException(ex, entity.Id);
                        }
                    }
                }

                writer.WriteElementString("Success", context.RecordsSucceeded.ToString());
                writer.WriteElementString("Failure", context.RecordsFailed.ToString());
                writer.WriteElementString("TotalRecords", (context.DataSegmenter.TotalRecords - ignored).ToString());

                writer.WriteEndElement();   // Categories
                writer.WriteEndElement();   // Content
            }
        }
Exemplo n.º 2
0
        protected override void Export(ExportExecuteContext context)
        {
            var categoryIds = context.CustomProperties["CategoryIds"] as HashSet <int>;
            var storeIds    = new HashSet <int>(context.CustomProperties["StoreIds"] as int[]);
            var domain      = context.CustomProperties["Domain"] as string;

            var categoryToStoreMappings = GetCategoryToStoreMappings();

            var allCategoryIds = _categoryRepository.TableUntracked
                                 .Where(x => !x.Deleted)
                                 .Select(x => new { x.Id, x.ParentCategoryId })
                                 .ToDictionary(x => x.Id, x => x.ParentCategoryId);

            using (var writer = XmlWriter.Create(context.DataStream, ShopConnectorService.DefaultSettings))
            {
                var helper = new ExportXmlHelper(writer, true);
                helper.Exclude = ExportXmlExclude.Category;

                writer.WriteStartElement("Content");
                writer.WriteStartElement("Products");
                writer.WriteAttributeString("Version", SmartStoreVersion.CurrentVersion);

                while (context.Abort == DataExchangeAbortion.None && context.DataSegmenter.ReadNextSegment())
                {
                    var segment     = context.DataSegmenter.CurrentSegment;
                    var skuMappings = new Dictionary <int, string>();

                    if (domain.HasValue())
                    {
                        var productIds = segment.Select(x => (int)((dynamic)x).Id).ToArray();
                        var mappings   = _shopConnectorService.Value.GetSkuMappingsByProductIds(domain, productIds);
                        skuMappings = mappings.ToDictionarySafe(x => x.ProductId, x => x.Sku);
                    }

                    foreach (dynamic product in segment)
                    {
                        if (context.Abort != DataExchangeAbortion.None)
                        {
                            break;
                        }

                        try
                        {
                            Product entity = product.Entity;

                            // SKU mapping.
                            if (skuMappings.TryGetValue(entity.Id, out var sku))
                            {
                                product.Sku = sku;
                            }

                            helper.WriteProduct(product, "Product");

                            if (product.ProductCategories != null)
                            {
                                foreach (dynamic productCategory in product.ProductCategories)
                                {
                                    if (productCategory.Category != null)
                                    {
                                        var categoryId = (int)productCategory.Category.Id;

                                        if (IsCategoryAllowed(categoryId, storeIds, allCategoryIds, categoryToStoreMappings))
                                        {
                                            IncludeIdAndAllParentIds(categoryId, categoryIds, allCategoryIds);
                                        }
                                    }
                                }
                            }

                            ++context.RecordsSucceeded;
                        }
                        catch (OutOfMemoryException)
                        {
                            context.Abort = DataExchangeAbortion.Hard;
                            throw;
                        }
                        catch (Exception ex)
                        {
                            context.RecordException(ex, (int)product.Id);
                        }
                    }
                }

                writer.WriteElementString("Success", context.RecordsSucceeded.ToString());
                writer.WriteElementString("Failure", context.RecordsFailed.ToString());
                writer.WriteElementString("TotalRecords", context.DataSegmenter.TotalRecords.ToString());

                writer.WriteEndElement();   // Products
                writer.WriteEndElement();   // Content

                var publicKey       = (string)context.CustomProperties[ShopConnectorCore.Header.PublicKey];
                var controllingData = ConnectionCache.ControllingData();
                var connection      = controllingData.Connections.FirstOrDefault(x => x.PublicKey == publicKey && x.IsForExport);
                if (connection != null)
                {
                    connection.LastProductCallUtc = DateTime.UtcNow;
                    ConnectionCache.ControllingData().ConnectionsUpdated = true;
                }
            }
        }