Esempio n. 1
0
        private void CreateFeed(FeedFileCreationContext fileCreation, TaskExecutionContext taskContext)
        {
            var xmlSettings = new XmlWriterSettings
            {
                Encoding        = Encoding.UTF8,
                CheckCharacters = false
            };

            using (var writer = XmlWriter.Create(fileCreation.Stream, xmlSettings))
            {
                try
                {
                    fileCreation.Logger.Information("Log file - Google Merchant Center feed.");

                    var searchContext = new ProductSearchContext()
                    {
                        OrderBy  = ProductSortingEnum.CreatedOn,
                        PageSize = int.MaxValue,
                        StoreId  = fileCreation.Store.Id,
                        VisibleIndividuallyOnly = true
                    };

                    string breakingError          = null;
                    var    qualifiedProducts      = new List <Product>();
                    var    currency               = Helper.GetUsedCurrency(Settings.CurrencyId);
                    var    products               = _productService.SearchProducts(searchContext);
                    var    measureWeightSystemKey = _measureService.GetMeasureWeightById(_measureSettings.BaseWeightId).SystemKeyword;

                    if (fileCreation.TotalRecords == 0)
                    {
                        fileCreation.TotalRecords = products.Count * fileCreation.StoreCount;
                    }

                    writer.WriteStartDocument();
                    writer.WriteStartElement("rss");
                    writer.WriteAttributeString("version", "2.0");
                    writer.WriteAttributeString("xmlns", "g", null, _googleNamespace);
                    writer.WriteStartElement("channel");
                    writer.WriteElementString("title", "{0} - Feed for Google Merchant Center".FormatWith(fileCreation.Store.Name));
                    writer.WriteElementString("link", "http://base.google.com/base/");
                    writer.WriteElementString("description", "Information about products");

                    foreach (var product in products)
                    {
                        fileCreation.Report();

                        Helper.GetQualifiedProductsByProduct(product, fileCreation.Store, qualifiedProducts);

                        foreach (var qualifiedProduct in qualifiedProducts)
                        {
                            writer.WriteStartElement("item");

                            try
                            {
                                breakingError = WriteItem(writer, fileCreation.Store, qualifiedProduct, currency, measureWeightSystemKey);
                            }
                            catch (Exception exc)
                            {
                                fileCreation.Logger.Error(exc.Message, exc);
                            }

                            writer.WriteEndElement();                             // item
                        }

                        if (breakingError.HasValue())
                        {
                            fileCreation.Logger.Error(breakingError);
                            break;
                        }
                        if (taskContext.CancellationToken.IsCancellationRequested)
                        {
                            fileCreation.Logger.Warning("A cancellation has been requested");
                            break;
                        }
                    }

                    writer.WriteEndElement();                     // channel
                    writer.WriteEndElement();                     // rss
                    writer.WriteEndDocument();

                    if (breakingError.HasValue())
                    {
                        throw new SmartException(breakingError);
                    }
                }
                catch (Exception exc)
                {
                    fileCreation.Logger.Error(exc.Message, exc);
                }
            }
        }
Esempio n. 2
0
        private void CreateFeed(FeedFileCreationContext fileCreation, TaskExecutionContext taskContext)
        {
            var xmlSettings = new XmlWriterSettings
            {
                Encoding        = Encoding.UTF8,
                CheckCharacters = false
            };

            using (var writer = XmlWriter.Create(fileCreation.Stream, xmlSettings))
            {
                try
                {
                    fileCreation.Logger.Information("Log file - Google Merchant Center feed.");

                    var searchContext = new ProductSearchContext
                    {
                        OrderBy  = ProductSortingEnum.CreatedOn,
                        PageSize = Settings.PageSize,
                        StoreId  = fileCreation.Store.Id,
                        VisibleIndividuallyOnly = true
                    };

                    var currency = Helper.GetUsedCurrency(Settings.CurrencyId);
                    var measureWeightSystemKey = _measureService.GetMeasureWeightById(_measureSettings.BaseWeightId).SystemKeyword;

                    writer.WriteStartDocument();
                    writer.WriteStartElement("rss");
                    writer.WriteAttributeString("version", "2.0");
                    writer.WriteAttributeString("xmlns", "g", null, _googleNamespace);
                    writer.WriteStartElement("channel");
                    writer.WriteElementString("title", "{0} - Feed for Google Merchant Center".FormatWith(fileCreation.Store.Name));
                    writer.WriteElementString("link", "http://base.google.com/base/");
                    writer.WriteElementString("description", "Information about products");

                    for (int i = 0; i < 9999999; ++i)
                    {
                        searchContext.PageIndex = i;

                        // Perf
                        _dbContext.DetachAll();

                        var products = _productService.SearchProducts(searchContext);

                        if (fileCreation.TotalRecords == 0)
                        {
                            fileCreation.TotalRecords = products.TotalCount * fileCreation.StoreCount;                                  // approx
                        }
                        foreach (var product in products)
                        {
                            fileCreation.Report();

                            if (product.ProductType == ProductType.SimpleProduct || product.ProductType == ProductType.BundledProduct)
                            {
                                WriteItem(fileCreation, writer, product, currency, measureWeightSystemKey);
                            }
                            else if (product.ProductType == ProductType.GroupedProduct)
                            {
                                var associatedSearchContext = new ProductSearchContext
                                {
                                    OrderBy  = ProductSortingEnum.CreatedOn,
                                    PageSize = int.MaxValue,
                                    StoreId  = fileCreation.Store.Id,
                                    VisibleIndividuallyOnly = false,
                                    ParentGroupedProductId  = product.Id
                                };

                                foreach (var associatedProduct in _productService.SearchProducts(associatedSearchContext))
                                {
                                    WriteItem(fileCreation, writer, associatedProduct, currency, measureWeightSystemKey);
                                }
                            }

                            if (taskContext.CancellationToken.IsCancellationRequested)
                            {
                                fileCreation.Logger.Warning("A cancellation has been requested");
                                break;
                            }
                        }

                        if (!products.HasNextPage || taskContext.CancellationToken.IsCancellationRequested)
                        {
                            break;
                        }
                    }

                    writer.WriteEndElement();                     // channel
                    writer.WriteEndElement();                     // rss
                    writer.WriteEndDocument();

                    if (fileCreation.ErrorMessage.HasValue())
                    {
                        fileCreation.Logger.Error(fileCreation.ErrorMessage);
                    }
                }
                catch (Exception exc)
                {
                    fileCreation.Logger.Error(exc.Message, exc);
                }
            }
        }