public RuleMatchData GetCustomLabelValue(IProductData productData, FeedRuleType feedRuleType)
        {
            RuleMatchData         result     = null;
            IRuleEvaluationResult ruleResult = null;

            switch (feedRuleType)
            {
            case (FeedRuleType.Custom_Label_0):
            {
                ruleResult = _customLabel0Service.GetEvaluationResult(productData, feedRuleType);
                break;
            }

            case (FeedRuleType.Custom_Label_1):
            {
                ruleResult = _customLabel1Service.GetEvaluationResult(productData, feedRuleType);
                break;
            }

            case (FeedRuleType.Custom_Label_2):
            {
                ruleResult = _customLabel2Service.GetEvaluationResult(productData, feedRuleType);
                break;
            }

            case (FeedRuleType.Custom_Label_3):
            {
                ruleResult = _customLabel3Service.GetEvaluationResult(productData, feedRuleType);
                break;
            }

            case (FeedRuleType.Custom_Label_4):
            {
                ruleResult = _customLabel4Service.GetEvaluationResult(productData, feedRuleType);
                break;
            }
            }
            if (ruleResult != null && ruleResult.HasMatch)
            {
                result = new RuleMatchData {
                    IsDefaultMatch = ruleResult.IsDefaultMatch, Value = ruleResult.MatchingRulePayLoads.First()
                }
            }
            ;

            return(result);
        }
Exemple #2
0
 internal void OnRuleEvaluated(IRuleEvaluationResult result)
 {
     RuleEvaluated?.Invoke(result);
 }
        private XE EntryAttribute(string catalog, IDataReader reader, StringDictionary dict
                                  , string gId
                                  , string sanitizedTitle
                                  , string linkCatalog
                                  , string linkSku
                                  , string gPrice
                                  , string gAdjustedPrice
                                  , string gAvailability
                                  , string gBrandName,
                                  string publisherName,
                                  XE contributors,
                                  string breadcrumb, bool isZeroCommissionProduct, string merchandiseType, IRuleEvaluationResult promotionalTextEvaluationResult, string description)
        {
            //entry
            var entry = new XE("product");

            sanitizedTitle = GetCjString(sanitizedTitle, MaxTitleLength);
            var name = new XE("name", sanitizedTitle);

            entry.Add(name);

            var keywordsValue = string.Empty;

            // First get the major contributor text
            if (!string.IsNullOrWhiteSpace(gBrandName))
            {
                keywordsValue = gBrandName;
            }
            else if (contributors != null)
            {
                keywordsValue = contributors.Value;
            }

            // Now add the breadcrumb
            if (!string.IsNullOrWhiteSpace(keywordsValue))
            {
                keywordsValue += ", ";
            }

            keywordsValue += string.Join(", ", breadcrumb.Split(new[] { BreadcrumbTrailSplitter }, StringSplitOptions.RemoveEmptyEntries)) + ", ";
            keywordsValue += sanitizedTitle;
            var keywords = new XE("keywords", GetCjString(keywordsValue, MaxKeywordLength));

            entry.Add(keywords);

            var descriptionValue = GetCjString(FeedUtils.RemoveHtmlTags(description), MaxDescriptionLength);

            if (string.IsNullOrWhiteSpace(descriptionValue))
            {
                descriptionValue = sanitizedTitle;
            }
            var descriptionXe = new XE("description", descriptionValue);

            entry.Add(descriptionXe);

            var gaId = new XE("sku", gId);

            entry.Add(gaId);

            var aLink = FeedEntryLink(GetFeedEntryLinkValue(reader, linkCatalog, linkSku));

            entry.Add(aLink);

            var available = new XE("available", "Yes");

            entry.Add(available);

            var aImgLink = EntryImageLink(reader, linkSku);

            entry.Add(aImgLink);

            Decimal price;
            bool    isSpecial = false;

            if (DisplaySalePriceInfo)
            {
                if (Decimal.TryParse(gPrice, out price))
                {
                    var gaPrice = new XE("price", price.ToString("F", CultureInfo.InvariantCulture));
                    entry.Add(gaPrice);

                    Decimal salePrice;
                    if (!string.IsNullOrWhiteSpace(gAdjustedPrice) && Decimal.TryParse(gAdjustedPrice, out salePrice) && salePrice != price)
                    {
                        isSpecial = true;
                        var gaSalePrice = new XE("saleprice", salePrice.ToString("F", CultureInfo.InvariantCulture));
                        entry.Add(gaSalePrice);
                    }
                }
            }
            else
            {
                price = Decimal.Parse(gAdjustedPrice);
                var gaPrice = new XE("price", price.ToString("F", CultureInfo.InvariantCulture));
                entry.Add(gaPrice);
            }

            entry.Add(new XE("currency", "CAD"));

            // CJ expects the optional xml nodes in a certain order
            var isBook = catalog.Equals("books", StringComparison.OrdinalIgnoreCase);

            if (!isBook && (linkSku.Length == 12 || linkSku.Length == 13))
            {
                entry.Add(new XE("upc", linkSku));
            }

            if (promotionalTextEvaluationResult.HasMatch)
            {
                if (!promotionalTextEvaluationResult.IsDefaultMatch)
                {
                    isSpecial = true;
                }

                var promoTextValue = GetCjString(promotionalTextEvaluationResult.MatchingRulePayLoads.First(), MaxPromotionalTextLength);
                entry.Add(new XE("promotionaltext", promoTextValue));
            }

            var advertiserCategoryText = breadcrumb.Replace(BreadcrumbTrailSplitter, ">");

            entry.Add(new XE("advertisercategory", GetCjString(advertiserCategoryText, MaxAdvertiserCategoryLength)));

            if (!string.IsNullOrWhiteSpace(gBrandName))
            {
                var manufacturer = GetCjString(gBrandName, MaxManufacturerLength);
                entry.Add(new XE("manufacturer", manufacturer));
                entry.Add(new XE("manufacturerid", linkSku));
            }

            if (isBook)
            {
                entry.Add(new XE("isbn", linkSku));

                if (contributors != null)
                {
                    entry.Add(new XE("author", GetCjString(contributors.Value, MaxManufacturerLength)));
                }

                if (!string.IsNullOrWhiteSpace(publisherName))
                {
                    entry.Add(new XE("publisher", GetCjString(publisherName, MaxManufacturerLength)));
                }
            }
            else
            {
                if (contributors != null)
                {
                    entry.Add(new XE("artist", GetCjString(contributors.Value, MaxManufacturerLength)));
                }
            }

            entry.Add(new XE("title", sanitizedTitle));

            if (!isBook)
            {
                if (!string.IsNullOrWhiteSpace(publisherName))
                {
                    entry.Add(new XE("label", GetCjString(publisherName, MaxManufacturerLength)));
                }

                if (dict.ContainsKey("format"))
                {
                    var format = reader[dict["format"]].ToString();
                    if (!string.IsNullOrWhiteSpace(format))
                    {
                        entry.Add(new XE("format", format));
                    }
                }
            }

            entry.Add(new XE("special", (isSpecial) ? "Yes" : "No"));

            if (catalog.Equals("generalmerchandise", StringComparison.OrdinalIgnoreCase))
            {
                entry.Add(new XE("gift", "Yes"));
            }

            entry.Add(new XE("instock", (string.IsNullOrWhiteSpace(gAvailability)) ? "No" : "Yes"));
            entry.Add(new XE("condition", "New"));

            if (!string.IsNullOrWhiteSpace(DefaultShippingCost))
            {
                entry.Add(new XE("standardshippingcost", DefaultShippingCost));
            }

            if (string.IsNullOrWhiteSpace(merchandiseType))
            {
                entry.Add(new XE("merchandisetype", ZeroCommissionListName));
            }
            else
            {
                if (isZeroCommissionProduct)
                {
                    entry.Add(new XE("merchandisetype", ZeroCommissionListName));
                    // If the merchandise type has a different value and it's not equal to zero commission list
                    // and if we're putting the item on the zero commission list, add it to the list of items to be
                    // updated inside the
                    if (!merchandiseType.Equals(ZeroCommissionListName, StringComparison.OrdinalIgnoreCase))
                    {
                        _updatedMerchandiseTypeProductPids.Add(decimal.Parse(gId));
                    }

                    Log.DebugFormat("Product {0} was put in zero commission list.", gId);
                }
                else
                {
                    entry.Add(new XE("merchandisetype", merchandiseType));
                }
            }

            return(entry);
        }
        private void WriteFileComponentContent(XmlWriter xmlWriter, FeedGenerationFileLineItem fileComponent, IDataReader reader, string identifier, ref Tuple <int, int, int> countRec)
        {
            var attributesDictionary = ConfigurationManager.GetSection(fileComponent.Catalogattributesection) as StringDictionary;

            if (countRec == null)
            {
                throw new ArgumentNullException("countRec");
            }
            var countProcessed = 0;
            var countDeleted   = 0;
            var countError     = 0;

            //<entry>
            while (reader.Read())
            {
                Log.DebugFormat("{0}::Processing record [{1}]: {2}", identifier, (countProcessed + countError + countDeleted), reader["PID"]);

                var id    = reader[attributesDictionary["gId"]].ToString();
                var title = reader[attributesDictionary["title"]].ToString();
                try
                {
                    var haveRulesChanged = _runnerHelper.HaveRulesChanged;
                    var linkSku          = reader[attributesDictionary["linkSku"]].ToString();
                    var brandName        = !attributesDictionary.ContainsKey("gBrand") ? string.Empty : reader[attributesDictionary["gBrand"]].ToString();
                    var cContributors    = GetContributors(attributesDictionary, reader);
                    // Get the breadcrumb value
                    var defaultCategory = FeedUtils.GetFeedGeneratorIndigoCategory(_feedGeneratorCategoryService, reader, attributesDictionary, fileComponent.Catalog, Log);
                    var productData     = FeedUtils.GetProductData(attributesDictionary, reader, linkSku, fileComponent.Catalog, brandName, cContributors, defaultCategory);
                    var sanitizedTitle  = (string.IsNullOrWhiteSpace(title)) ? string.Empty : FeedUtils.SanitizeString(title);
                    var gAvailability   = !attributesDictionary.ContainsKey("gAvailability") ? FeedUtils.GetGoogleAvailability(1) : FeedUtils.GetGoogleAvailability((int)reader[attributesDictionary["gAvailability"]]);
                    var availability    = !attributesDictionary.ContainsKey("gAvailability") ? 1 : (int)reader[attributesDictionary["gAvailability"]];
                    var recordType      = !attributesDictionary.ContainsKey("recordType") ? string.Empty : reader[attributesDictionary["recordType"]].ToString();
                    var merchandiseType = !attributesDictionary.ContainsKey("merchandiseType") ? string.Empty : reader[attributesDictionary["merchandiseType"]].ToString();
                    if (string.IsNullOrWhiteSpace(merchandiseType))
                    {
                        _missingMerchandiseTypeProductInfos.Add(new MissingMerchandiseTypeProductInfo {
                            Breadcrumb = defaultCategory.Breadcrumb, Sku = linkSku, Title = sanitizedTitle
                        });
                    }

                    IRuleEvaluationResult newZeroCommissionResult  = null;
                    IRuleEvaluationResult newPromotionalTextResult = null;
                    if (_isIncrementalRun)
                    {
                        var isModifiedData = int.Parse(reader["IsModified"].ToString());
                        if (isModifiedData == 0)
                        {
                            if (!haveRulesChanged)
                            {
                                Log.DebugFormat(
                                    "Product with pid {0} is skipped in incremental mode as it wasn't modified and the rules haven't changed.",
                                    id);
                                continue;
                            }

                            var oldZeroCommissionResult = _runnerHelper.GetZeroCommissionRuleResult(productData, true);
                            newZeroCommissionResult = _runnerHelper.GetZeroCommissionRuleResult(productData, false);
                            var oldPromotionalTextResult = _runnerHelper.GetPromotionalTextRuleResult(productData, true);
                            newPromotionalTextResult = _runnerHelper.GetPromotionalTextRuleResult(productData, false);
                            if (oldZeroCommissionResult.HasMatch == newZeroCommissionResult.HasMatch &&
                                oldPromotionalTextResult.HasMatch == newPromotionalTextResult.HasMatch &&
                                oldPromotionalTextResult.MatchingRulePayLoads.First()
                                .Equals(newPromotionalTextResult.MatchingRulePayLoads.First()))
                            {
                                countDeleted++;
                                Log.DebugFormat(
                                    "Product with pid {0} is skipped in incremental mode as it wasn't modified and rule evaluations yielded same results.",
                                    id);
                                continue;
                            }

                            // At this point, we know that rules have changed, which means we need to resend this product as modified
                        }
                        else
                        {
                            newZeroCommissionResult  = _runnerHelper.GetZeroCommissionRuleResult(productData, false);
                            newPromotionalTextResult = _runnerHelper.GetPromotionalTextRuleResult(productData, false);
                        }
                    }
                    else
                    {
                        newZeroCommissionResult  = _runnerHelper.GetZeroCommissionRuleResult(productData, false);
                        newPromotionalTextResult = _runnerHelper.GetPromotionalTextRuleResult(productData, false);
                    }

                    var isZeroCommissionElement = newZeroCommissionResult.HasMatch || IsZeroCommissionElement(sanitizedTitle, _feedId, true, availability, recordType);
                    if (isZeroCommissionElement)
                    {
                        Log.DebugFormat("Product with pid {0} is being placed in zero commission list due to either data issues or zero commission rules.)", id);
                    }

                    var linkCatalog = attributesDictionary["linkCatalog"];
                    if (string.IsNullOrWhiteSpace(sanitizedTitle))
                    {
                        sanitizedTitle = "(No Title)";
                    }
                    var description = reader[attributesDictionary["description"]].ToString();
                    description = string.IsNullOrWhiteSpace(description) ? sanitizedTitle : FeedUtils.SanitizeString(description);

                    // Get the breadcrumb value
                    var breadcrumb     = defaultCategory.Breadcrumb;
                    var gPrice         = string.IsNullOrEmpty(attributesDictionary["price"]) ? "" : reader[attributesDictionary["price"]].ToString();
                    var gAdjustedPrice = string.IsNullOrEmpty(attributesDictionary["adjustedPrice"]) ? string.Empty : reader[attributesDictionary["adjustedPrice"]].ToString();
                    var publisherName  = !attributesDictionary.ContainsKey("publisherName") ? string.Empty : reader[attributesDictionary["publisherName"]].ToString();
                    if (!string.IsNullOrWhiteSpace(recordType) &&
                        recordType.Equals("GCard_Electronic", StringComparison.OrdinalIgnoreCase))
                    {
                        gPrice         = DefaultEGiftCardPrice;
                        gAdjustedPrice = DefaultEGiftCardPrice;
                    }


                    var entry = EntryAttribute(fileComponent.Catalog, reader, attributesDictionary
                                               , id
                                               , sanitizedTitle
                                               , linkCatalog, linkSku
                                               , gPrice
                                               , gAdjustedPrice
                                               , gAvailability
                                               , brandName, publisherName, cContributors, breadcrumb, isZeroCommissionElement, merchandiseType, newPromotionalTextResult, description);

                    countProcessed++;
                    //out of memory exception will be thrown if we keep the xml entries in memory
                    entry.WriteTo(xmlWriter);
                }
                catch (Exception e)
                {
                    countError++;
                    Log.ErrorFormat("Can't process the item. Id:{0};title:{1}", id, title);
                    Log.DebugFormat("Error stack trace: {0}", e);
                    _executionLogLogger.AddCustomMessage(string.Format("Can't process the item. Id: {0};title: {1}, file identifier: {2}", id, title, identifier));
                    if (!AllowItemErrorsInFiles || _isIncrementalRun)
                    {
                        _hasError = true;
                    }
                }
            }

            // Now process the deleted items for the range & producttypeid and put them in the xml file as "deleted" items
            if (_isIncrementalRun)
            {
                // Don't do it for gift card file component
                if (!fileComponent.StoredProcedureName.Equals("uspCJFeedGeneralMerchandiseGiftCard",
                                                              StringComparison.OrdinalIgnoreCase))
                {
                    foreach (var deletedProductInfo in GetDeletedProductInfos(identifier))
                    {
                        GetDeletedElement(deletedProductInfo).WriteTo(xmlWriter);
                        countDeleted++;
                    }
                }
            }

            Log.InfoFormat("{0} completed. Processed record count: {1}, Error record count: {2}, deleted/skipped record count {3}", identifier, countProcessed, countError, countDeleted);
            countRec = new Tuple <int, int, int>(countRec.Item1 + countProcessed, countRec.Item2 + countError, countRec.Item3 + countDeleted);
        }