public void Initialize(int feedId, bool isIncremental, DateTime?fromTime, DateTime executionTime, GoogleRunFeedType runFeedType)
        {
            _log.Debug("Inside Initialize() of GooglePlaFeedRuleHelper.");
            _isIncrementalRun = isIncremental;
            _fromTime         = fromTime;
            _runFeedType      = runFeedType;

            // First get rules associated with this feed
            var rules = _feedRuleService.GetFeedRuleModels(feedId, executionTime).ToList();

            _exclusionRules.AddRange(rules.Where(r => r.FeedRule.FeedRuleType == FeedRuleType.Exclusion));
            var cpcRuleModels               = rules.Where(frm => frm.FeedRule.FeedRuleType == FeedRuleType.CPC_Value).ToList();
            var customLabel0RuleModels      = rules.Where(frm => frm.FeedRule.FeedRuleType == FeedRuleType.Custom_Label_0).ToList();
            var customLabel1RuleModels      = rules.Where(frm => frm.FeedRule.FeedRuleType == FeedRuleType.Custom_Label_1).ToList();
            var customLabel2RuleModels      = rules.Where(frm => frm.FeedRule.FeedRuleType == FeedRuleType.Custom_Label_2).ToList();
            var customLabel3RuleModels      = rules.Where(frm => frm.FeedRule.FeedRuleType == FeedRuleType.Custom_Label_3).ToList();
            var customLabel4RuleModels      = rules.Where(frm => frm.FeedRule.FeedRuleType == FeedRuleType.Custom_Label_4).ToList();
            var dynamicMerchLabelRuleModels = rules.Where(frm => frm.FeedRule.FeedRuleType == FeedRuleType.Dynamic_Merch_Label).ToList();

            // Instantiate the IFeedGeneratorCmsDataService
            _feedGeneratorCmsDataService = new FeedGeneratorCmsDataService(_feedCmsProductArchiveEntryService, null);
            // Instantiate the rule evaluator services
            _exclusionRuleEvaluatorService = new RuleEvaluatorService(_exclusionRules, _feedGeneratorCmsDataService, _feedGeneratorIndigoCategoryService, _allowRuleOptimizations, _allowRuleEntryRemovals, _allowIEnumerableRuleEvaluations);
            _cpcRuleEvaluatorService       = new RuleEvaluatorService(cpcRuleModels, _feedGeneratorCmsDataService, _feedGeneratorIndigoCategoryService, _allowRuleOptimizations, _allowRuleEntryRemovals, _allowIEnumerableRuleEvaluations);
            _customLabel0Service           = new RuleEvaluatorService(customLabel0RuleModels, _feedGeneratorCmsDataService, _feedGeneratorIndigoCategoryService, _allowRuleOptimizations, _allowRuleEntryRemovals, _allowIEnumerableRuleEvaluations);
            _customLabel1Service           = new RuleEvaluatorService(customLabel1RuleModels, _feedGeneratorCmsDataService, _feedGeneratorIndigoCategoryService, _allowRuleOptimizations, _allowRuleEntryRemovals, _allowIEnumerableRuleEvaluations);
            _customLabel2Service           = new RuleEvaluatorService(customLabel2RuleModels, _feedGeneratorCmsDataService, _feedGeneratorIndigoCategoryService, _allowRuleOptimizations, _allowRuleEntryRemovals, _allowIEnumerableRuleEvaluations);
            _customLabel3Service           = new RuleEvaluatorService(customLabel3RuleModels, _feedGeneratorCmsDataService, _feedGeneratorIndigoCategoryService, _allowRuleOptimizations, _allowRuleEntryRemovals, _allowIEnumerableRuleEvaluations);
            _customLabel4Service           = new RuleEvaluatorService(customLabel4RuleModels, _feedGeneratorCmsDataService, _feedGeneratorIndigoCategoryService, _allowRuleOptimizations, _allowRuleEntryRemovals, _allowIEnumerableRuleEvaluations);
            _dynamicMerchLabelService      = new RuleEvaluatorService(dynamicMerchLabelRuleModels, _feedGeneratorCmsDataService, _feedGeneratorIndigoCategoryService, _allowRuleOptimizations, _allowRuleEntryRemovals, _allowIEnumerableRuleEvaluations);

            // If it's an incremental run, get the rule set at the time of of the previous execution
            // Then make comparisons to check if there were updates to rules. If there were rule updates,
            // then populate the secondary rule services which will be used to determine if a product is modified
            // even if the product data hasn't changed
            if (!_isIncrementalRun || !_fromTime.HasValue)
            {
                _log.Debug("Exiting Initialize() of GooglePlaFeedRuleHelper without initializing past rules.");
                return;
            }

            var previousRules = _feedRuleService.GetFeedRuleModels(feedId, _fromTime.Value).ToList();

            _exclusionRulesPast.AddRange(previousRules.Where(r => r.FeedRule.FeedRuleType == FeedRuleType.Exclusion));
            _feedGeneratorCmsDataServicePast   = new FeedGeneratorCmsDataService(_feedCmsProductArchiveEntryService, fromTime);
            _exclusionRuleEvaluatorServicePast = new RuleEvaluatorService(_exclusionRulesPast, _feedGeneratorCmsDataServicePast, _feedGeneratorIndigoCategoryService, _allowRuleOptimizations, _allowRuleEntryRemovals, _allowIEnumerableRuleEvaluations);
            _haveExclusionRulesChanged         = !AreSameRules(new RuleComparisonData {
                RuleSet = _exclusionRules, FeedGeneratorCmsDataService = _feedGeneratorCmsDataService
            },
                                                               new RuleComparisonData {
                RuleSet = _exclusionRulesPast, FeedGeneratorCmsDataService = _feedGeneratorCmsDataServicePast
            });

            _log.DebugFormat("Exiting Initialize() of GooglePlaFeedRuleHelper after initializing past rules. HaveExclusionRulesChanged is {0}.", _haveExclusionRulesChanged);
        }
Exemple #2
0
        public static void StartXmlDocument(XmlWriter xmlWriter, GoogleRunFeedType feedType, DateTime fileTime)
        {
            xmlWriter.WriteStartDocument();

            xmlWriter.WriteStartElement("feed", FeedXmlns.NamespaceName);
            if (feedType == GoogleRunFeedType.Google)
            {
                xmlWriter.WriteAttributeString("xmlns", NsG, string.Empty, FeedXmlsnsG.NamespaceName);
            }
            //<title>
            var roottitle = new XElement(FeedXmlns + "title", GoogleFeedTitle);

            roottitle.WriteTo(xmlWriter);
            //<link ref="">
            var rootlink = new XElement(FeedXmlns + "link", new XAttribute("href", BaseUrl));

            rootlink.WriteTo(xmlWriter);
            //<updated>
            var rootupdated = new XElement(FeedXmlns + "updated", fileTime.ToUniversalTime().ToString(CultureInfo.InvariantCulture));

            rootupdated.WriteTo(xmlWriter);
        }
        private XElement EntryAttribute(IDataReader reader
                                        , string gId
                                        , string sanitizedTitle, string sanitizedDescription, string googleProductCategory
                                        , string linkCatalog
                                        , string linkSku
                                        , decimal regularPrice
                                        , decimal?salePrice
                                        , string gAvailability //, string availability
                                        , string gtin
                                        , string gBrandName,
                                        bool hasBrandName,
                                        string defaultBreadcrumb,
                                        IEnumerable <string> allBreadcrumbs,
                                        string cpcValue,
                                        string customLabel0,
                                        string customLabel1,
                                        string customLabel2,
                                        string customLabel3,
                                        string customLabel4,
                                        GoogleRunFeedType feedType)
        {
            var googleNs = (feedType == GoogleRunFeedType.Google) ? FeedXmlsnsG : FeedXmlns;
            //entry
            var entry = new XElement(FeedXmlns + "entry");
            //g:id
            var gaId = new XElement(googleNs + "id", gId);

            entry.Add(gaId);
            //title
            var aTitle = new XElement(FeedXmlns + "title", new XCData(sanitizedTitle));

            entry.Add(aTitle);
            //description
            var aDescription = new XElement(FeedXmlns + "description", new XCData(sanitizedDescription));

            entry.Add(aDescription);
            //<g:google_product_category>
            var gaGoogleProductCategory = new XElement(googleNs + "google_product_category", googleProductCategory);

            entry.Add(gaGoogleProductCategory);
            //write optional product type - NOT required by google
            foreach (var breadcrumb in allBreadcrumbs.Take(MaximumBreadcrumbsToSend))
            {
                var aProductType = new XElement(googleNs + "product_type", breadcrumb);
                entry.Add(aProductType);
            }

            //link
            var url = PlaRelatedFeedUtils.GetFeedEntryLinkValue(reader, linkCatalog, linkSku);

            if (feedType == GoogleRunFeedType.Yahoo)
            {
                url += YahooItemPageUrlSuffix;
            }
            var aLink = PlaRelatedFeedUtils.FeedEntryLink(url);

            entry.Add(aLink);

            //g:image_link
            var aImgLink = PlaRelatedFeedUtils.EntryImageLink(reader, linkSku, googleNs, feedType);

            entry.Add(aImgLink);
            //g:condition
            var gaCondition = new XElement(googleNs + "condition", "new");

            entry.Add(gaCondition);
            //google search availability
            var gaAvailability = new XElement(googleNs + "availability", gAvailability);

            entry.Add(gaAvailability);

            //g:price, g:sale_price, sale_price_effective_date
            if (DisplaySalePriceInfo)
            {
                var gaPrice = new XElement(googleNs + "price", regularPrice.ToString("F", CultureInfo.InvariantCulture) + " CAD");
                entry.Add(gaPrice);

                if (salePrice.HasValue)
                {
                    var gaSalePrice = new XElement(googleNs + "sale_price", salePrice.Value.ToString("F", CultureInfo.InvariantCulture) + " CAD");
                    entry.Add(gaSalePrice);
                    if (DisplaySalePriceTimeSpanInfo)
                    {
                        var          time = DateTime.Now.AddDays(SalePriceInfoTimeSpanBegin);
                        const string timeToStringFormat = "yyyy-MM-ddTHH:mmK";
                        entry.Add(new XElement(googleNs + "sale_price_effective_date", string.Format("{0}/{1}", time.ToString(timeToStringFormat), time.AddDays(SalePriceInfoTimeSpanEnd).ToString(timeToStringFormat))));
                    }
                }
            }
            else
            {
                var gaPrice = new XElement(googleNs + "price", regularPrice.ToString("F", CultureInfo.InvariantCulture) + " CAD");
                entry.Add(gaPrice);
            }

            // Items to be sent only to Google
            if (feedType == GoogleRunFeedType.Google)
            {
                if (DisplayDefaultShoppingInfo)
                {
                    //g:shipping
                    var gShipping = PlaRelatedFeedUtils.ShippingAttribute(null, null, null, googleNs);
                    entry.Add(gShipping);
                }

                //write mandatory brand - required by google - only for items under Apparel & Accessories
                // (if brand name isn't available, set identifier_exists to false --> Unique Product Identifiers section)
                // https://support.google.com/merchants/answer/188494?hl=en&ref_topic=3404778
                if (hasBrandName && !string.IsNullOrWhiteSpace(gBrandName))
                {
                    entry.Add(new XElement(googleNs + "brand", gBrandName.Trim()));
                }

                //google search unique product identifiers -> g:gtin
                var gaGtin = new XElement(googleNs + "gtin", gtin);
                entry.Add(gaGtin);

                // Add the adwords_redirect node
                if (!string.IsNullOrWhiteSpace(GoogleAdwordsRedirectUrlSuffix))
                {
                    entry.Add(new XElement(googleNs + "adwords_redirect", url + GoogleAdwordsRedirectUrlSuffix));
                }
                else
                {
                    entry.Add(new XElement(googleNs + "adwords_redirect", url));
                }

                entry.Add(new XElement(googleNs + "adwords_grouping", defaultBreadcrumb));
                if (!string.IsNullOrWhiteSpace(customLabel0))
                {
                    entry.Add(new XElement(googleNs + "custom_label_0", customLabel0));
                }
                if (!string.IsNullOrWhiteSpace(customLabel1))
                {
                    entry.Add(new XElement(googleNs + "custom_label_1", customLabel1));
                }
                if (!string.IsNullOrWhiteSpace(customLabel2))
                {
                    entry.Add(new XElement(googleNs + "custom_label_2", customLabel2));
                }
                if (!string.IsNullOrWhiteSpace(customLabel3))
                {
                    entry.Add(new XElement(googleNs + "custom_label_3", customLabel3));
                }
                if (!string.IsNullOrWhiteSpace(customLabel4))
                {
                    entry.Add(new XElement(googleNs + "custom_label_4", customLabel4));
                }
            }

            if (feedType == GoogleRunFeedType.Yahoo)
            {
                entry.Add(new XElement(FeedXmlns + "cpc", cpcValue));
            }

            return(entry);
        }
Exemple #4
0
        public static XElement EntryImageLink(IDataRecord reader, string sku, XNamespace ns, GoogleRunFeedType feedType)
        {
            var imgSection      = FeedUtils.GetImageFileSectionName(reader);
            var imagePathSuffix = (feedType == GoogleRunFeedType.Google) ? GoogleImagePathSuffix : YahooImagePathSuffix;
            var imgUrl          = string.Format("{0}/{1}/{2}.jpg{3}", ImgBaseUrl, imgSection, sku, imagePathSuffix);

            return(new XElement(ns + "image_link", imgUrl));
        }