public CartWebClient(CartWebClientConfig config = null)
     : base()
 {
     CookieContainer = new CookieContainer();
     //_requestTimeout = 30;
     //_sessionTimeout = 30;
     //_bufferSize = 10000000; //10 MB
     _lastuse = DateTime.MinValue;
     _config = config == null ? new CartWebClientConfig() : config;
 }
 public CartWebClient(CartWebClient source)
     : base()
 {
     CookieContainer = new CookieContainer();
     //_requestTimeout = source._requestTimeout;
     //_sessionTimeout = source._sessionTimeout;
     //_bufferSize = source._bufferSize;
     _lastuse = source._lastuse;
     _config = source._config;
     QueryString = new NameValueCollection(source.QueryString);
     Headers = new WebHeaderCollection();
     Headers.Add(source.Headers);
 }
        protected void ParseSettings(XElement settings)
        {
            //site definition
            StoreShortUrl = CleanUpUrl(Input.GetValue(settings, "storeUrl"));
            EnableServiceCallDetailLog = Input.GetValue(settings, "enableServiceCallDetailLog").Equals("true", StringComparison.OrdinalIgnoreCase);
            var zone = Input.GetValue(settings, "siteTimeZone");
            if (!string.IsNullOrEmpty(zone))
            {
                try
                {
                    SiteTimeZone = TimeZoneInfo.FindSystemTimeZoneById(zone);
                }
                catch {}
            }

            //Security Rules
            RequireSecureBoost = Input.GetValue(settings, "requireSecureBoost").Equals("true");
            RequireSecureUpload = Input.GetValue(settings, "requireSecureUpload").Equals("true");
            UploadAddresses = null;
            var ipList = settings.Elements("approvedUploadIP");
            if (ipList.Any())
                UploadAddresses = ipList.Select(ip => ip.Value).ToList();
            AllowManualUpload = !Input.GetValue(settings, "allowManualUpload").Equals("false"); //default is true
            AllowUserExtraction = !Input.GetValue(settings, "allowUserExtraction").Equals("false"); //default is true

            //track last extraction/generation
            var extract = Input.GetValue(settings, "lastExtractionType");
            CartExtractor.ExtractType lastExtractType;
            if (Enum.TryParse(extract, true, out lastExtractType))
                LastExtractionType = lastExtractType;
            extract = Input.GetValue(settings, "lastDynamicUpdateType");
            if (Enum.TryParse(extract, true, out lastExtractType))
                LastDynamicUpdateType = lastExtractType;
            var lastDate = Input.GetValue(settings, "lastExtractionTime");
            LastExtractionTime = Input.SafeDateConvert(lastDate, DateTime.MinValue);
            lastDate = Input.GetValue(settings, "lastDynamicUpdateTime");
            LastDynamicUpdateTime = Input.SafeDateConvert(lastDate, DateTime.MinValue);
            lastDate = Input.GetValue(settings, "lastGeneratorTime");
            LastGeneratorTime = Input.SafeDateConvert(lastDate, DateTime.MinValue);
            lastDate = Input.GetValue(settings, "lastRuleChange");
            LastRuleChangeTime = Input.SafeDateConvert(lastDate, DateTime.MinValue);
            LastExtractorDuration = Input.SafeIntConvert(Input.GetValue(settings, "lastExtractorDuration"));
            if (LastExtractorDuration.Equals(0))
                LastExtractorDuration = Input.SafeIntConvert(Input.GetValue(settings, "lastExtractionDuration")); //legacy
            LastGeneratorDuration = Input.SafeIntConvert(Input.GetValue(settings, "lastGeneratorDuration"));
            var lockout = Input.SafeIntConvert(Input.GetValue(settings, "extractorLockoutMinutes"), -1);
            ExtractorLockoutMinutes = lockout > -1 ? lockout : DefaultExtractorLockoutMinutes;

            //contact info
            UserContact poc = ParseLegacySettings(settings);
            var userElement = settings.Element("users");
            if (userElement != null)
            {
                var users = userElement.Descendants("user");
                if (users.Any())
                {
                    foreach (var u in users)
                    {
                        Users.Add(new User(u));
                    }
                    if (poc == null)
                    {
                        var pocUser = Users.First(x => x.ContactRole.Equals(UserContactRole.Technical));
                        if (pocUser != null)
                            poc = new UserContact { Email = pocUser.Email, Name = pocUser.Name };
                    }
                    //else
                    //	DataLogProxy.Instance.AddSite(Alias, poc); //add client to the log
                }
            }

            #region Data Source Rules

            //Migration rulescatalogMigration
            var migration = settings.Element("catalogMigration");
            if (migration != null)
            {
                var startDate = Input.SafeDateConvert(Input.GetAttribute(migration, "startDate"), DateTime.MinValue);
                var fromField = Input.GetAttribute(migration, "mapFromField");
                var toField = Input.GetAttribute(migration, "mapToField");
                var enabled = !Input.GetAttribute(migration, "enabled").Equals("false");
                var use4TellCatalog = Input.GetAttribute(migration, "use4TellCatalog").Equals("true");
                var use4TellSales = Input.GetAttribute(migration, "use4TellSales").Equals("true");
                MigrationRules = new CatalogMigration(Alias, (int)Tier, startDate, fromField, toField, enabled, true,
                                                                                            SalesMonthsToExport, use4TellCatalog, use4TellSales);
            }

            //set cart extraction defaults
            try
            {
                CartName = Input.GetValue(settings, "cartType");
                PluginVersion = Input.SafeFloatConvert(Input.GetValue(settings, "pluginVersion"));
                CartType cartType;
                CartType = Enum.TryParse(CartName, true, out cartType) ? cartType : CartType.Other;
                var level = Input.GetValue(settings, "cartLevel");
                CartLevel = 0;
                SetCartDefaults(false, level);
            }
            catch (Exception ex)
            {
                CartType = CartType.Other;
                SetCartDefaults();
            }

            //Onboarding Credentials (not used by service but sometimes needed for onboarding)
            AdminUser = Input.GetValue(settings, "adminUser");
            AdminPassword = Input.GetValue(settings, "adminPassword");
            FtpType = Input.GetValue(settings, "ftpType");
            FtpUrl = Input.GetValue(settings, "ftpUrl");
            FtpUser = Input.GetValue(settings, "ftpUser");
            FtpPassword = Input.GetValue(settings, "ftpPassword");

            //API access
            ApiUrl = Input.GetValue(settings, "apiUrl");
            float apiVersion;
            ApiVersion = Input.GetValue(out apiVersion, settings, "apiVersion") ? apiVersion : 0F;	//default zero means no API
            ApiCountEnabled = !Input.GetValue(settings, "apiCountEnabled").Equals("false", StringComparison.OrdinalIgnoreCase);	//default true
            ApiCustomerDateRangeEnabled = !Input.GetValue(settings, "apiCustomerDateRangeEnabled").Equals("false", StringComparison.OrdinalIgnoreCase);	//default true
            ApiSalesDateRangeEnabled = !Input.GetValue(settings, "apiSalesDateRangeEnabled").Equals("false", StringComparison.OrdinalIgnoreCase);	//default true
            ApiHeaderIsOnlyOnFirstRow = Input.GetValue(settings, "apiHeaderIsOnlyOnFirstRow").Equals("true", StringComparison.OrdinalIgnoreCase);	//default false
            ApiRowEnd = Input.GetCsvStringList(settings, "apiRowEnd");
            ApiTrimChars = Input.GetCsvCharList(settings, "apiTrimChars");
            ApiMinimumCatalogSize = Input.SafeIntConvert(Input.GetValue(settings, "apiMinimumCatalogSize"), 0);
            ApiForceAllRowRanges = Input.SafeIntConvert(Input.GetValue(settings, "apiForceAllRowRanges"), 0);
            if (ApiForceAllRowRanges < 1)
                    ApiForceAllRowRanges = Input.SafeIntConvert(Input.GetValue(settings, "apiForceRowRange"), 0); //depricated
            ApiForceCatalogRowRange = Input.SafeIntConvert(Input.GetValue(settings, "apiForceCatalogRowRange"), 0);
            ApiForceCategoryRowRange = Input.SafeIntConvert(Input.GetValue(settings, "apiForceCategoryRowRange"), 0);
            ApiForceSalesRowRange = Input.SafeIntConvert(Input.GetValue(settings, "apiForceSalesRowRange"), 0);
            ApiForceCustomerRowRange = Input.SafeIntConvert(Input.GetValue(settings, "apiForceCustomerRowRange"), 0);
            ApiForceInventoryRowRange = Input.SafeIntConvert(Input.GetValue(settings, "apiForceInventoryRowRange"), 0);
            ApiAllowExtraRows = Input.GetValue(settings, "apiAllowExtraRows").Equals("true", StringComparison.OrdinalIgnoreCase);
            ApiMaxDaysPerRequest = Input.SafeIntConvert(Input.GetValue(settings, "apiMaxDaysPerRequest"), 0);
            ApiUserName = Input.GetValue(settings, "apiUserName");
            ApiKey = Input.GetValue(settings, "apiKey");
            ApiSecret = Input.GetValue(settings, "apiSecret");
            ApiAliasParam = Input.GetValue(settings, "apiAliasParam");
            ApiUserParam = Input.GetValue(settings, "apiUserParam");
            ApiKeyParam = Input.GetValue(settings, "apiKeyParam");
            ApiFieldParam = Input.GetValue(settings, "apiFieldParam");
            ApiIdRangeParam = Input.GetValue(settings, "apiIdRangeParam");
            ApiRowRangeParam = Input.GetValue(settings, "apiRowRangeParam");
            ApiDateRangeParam = Input.GetValue(settings, "apiDateRangeParam");
            ApiYearParam = Input.GetValue(settings, "apiYearParam");
            ApiMonthParam = Input.GetValue(settings, "apiMonthParam");
            ApiModeParam = Input.GetValue(settings, "apiModeParam");
            ApiResponseFormat = Input.GetValue(settings, "apiResponseFormat");
            ApiAcceptHeader = Input.GetValue(settings, "apiAcceptHeader");
            ApiExtraHeaders = Input.SafeIntConvert(Input.GetValue(settings, "apiExtraHeaders"));
            var additionalQueries = settings.Element("apiAddQueries");
            if (additionalQueries != null)
            {
                var queries = additionalQueries.Descendants("addQuery");
                if (queries.Any())
                {
                    ApiAddQueries = new Dictionary<DataGroup, NameValueCollection>();
                    foreach (var q in queries)
                    {
                        //parse query details
                        var queryGroup = Input.GetAttribute(q, "group");
                        if (string.IsNullOrEmpty(queryGroup)) continue;
                        DataGroup dataGroup;
                        if (!Enum.TryParse(queryGroup, true, out dataGroup)) continue;
                        var name = Input.GetAttribute(q, "name");
                        if (string.IsNullOrEmpty(name)) continue;
                        var value = Input.GetAttribute(q, "value");
                        if (string.IsNullOrEmpty(value)) continue;

                        NameValueCollection queryList;
                        if (!ApiAddQueries.TryGetValue(dataGroup, out queryList))
                        {
                            ApiAddQueries.Add(dataGroup, new NameValueCollection {{name, value}});
                        }
                        else ApiAddQueries[dataGroup].Add(name, value);
                    }
                }
            }
            var charMapXml = settings.Element("titleCharMap");
            TitleCharMap = GetCharMapPairs(charMapXml, Alias);
            WebClientConfig = new CartWebClientConfig(settings);

            //cart extraction settings
            CombinedFeedUrl = Input.GetValue(settings, "combinedFeedUrl");
            if (string.IsNullOrEmpty(CombinedFeedUrl))
                CombinedFeedUrl = Input.GetValue(settings, "combinedFeed"); //old name
            CatalogFeedUrl = Input.GetValue(settings, "catalogFeedUrl");
            if (string.IsNullOrEmpty(CatalogFeedUrl))
                CatalogFeedUrl = Input.GetValue(settings, "catalogFeed"); //old name
            SalesFeedUrl = Input.GetValue(settings, "salesFeedUrl");
            if (string.IsNullOrEmpty(SalesFeedUrl))
                SalesFeedUrl = Input.GetValue(settings, "salesFeed"); //old name
            CustomerFeedUrl = Input.GetValue(settings, "customerFeedUrl");
            Att1NameFeedUrl = Input.GetValue(settings, "att1NameFeedUrl");
            if (string.IsNullOrEmpty(Att1NameFeedUrl))
                Att1NameFeedUrl = Input.GetValue(settings, "att1NameFeed"); //old name
            Att2NameFeedUrl = Input.GetValue(settings, "att2NameFeedUrl");
            if (string.IsNullOrEmpty(Att2NameFeedUrl))
                Att2NameFeedUrl = Input.GetValue(settings, "att2NameFeed"); //old name
            DepartmentNameFeedUrl = Input.GetValue(settings, "departmentNameFeedUrl");
            InventoryFeedUrl = Input.GetValue(settings, "inventoryFeedUrl");

            //Access Rules
            var accessCreds = settings.Element("accessCredentials");
            if (accessCreds != null)
                AccessCredentials = new AuthCredentials(accessCreds);
            var extractorCreds = settings.Element("extractorCredentials");
            if (extractorCreds != null)
                ExtractorCredentials = new AuthCredentials(extractorCreds);
            else if (AccessCredentials != null) //if no extractor creds provided, make them match the access creds
                ExtractorCredentials = new AuthCredentials(AccessCredentials);
            var loginUrl = Input.GetValue(settings, "extractorLoginUrl");
            if (loginUrl != null)
                ExtractorLoginUrl = loginUrl;
            var loginCreds = settings.Element("extractorLoginCredentials");
            if (loginCreds != null)
                ExtractorLoginCredentials = new AuthCredentials(loginCreds);

            //Extractor Flags	-- only override defaults if a value is set in siterules

            //general (timeframes are set in legacy settings)
            var flag = Input.GetValue(settings, "unescapeFeedData");
            if (!string.IsNullOrEmpty(flag))
                UnescapeFeedData = flag.Equals("true", StringComparison.OrdinalIgnoreCase);
            flag = Input.GetValue(settings, "reduceXmlFeed");
            if (!string.IsNullOrEmpty(flag))
                ReduceXmlFeed = flag.Equals("true", StringComparison.OrdinalIgnoreCase);
            flag = Input.GetValue(settings, "omitExtraFields");
            if (!string.IsNullOrEmpty(flag))
                OmitExtraFields = flag.Equals("true", StringComparison.OrdinalIgnoreCase);
            UseLargeCatalogHandling = Input.GetValue(settings, "useLargeCatalogHandling").Equals("true", StringComparison.OrdinalIgnoreCase);

            //sales
            flag = Input.GetValue(settings, "extractSalesUpdate");
            if (!string.IsNullOrEmpty(flag))
                ExtractSalesUpdate = flag.Equals("true", StringComparison.OrdinalIgnoreCase);
            flag = Input.GetValue(settings, "extractSalesFull");
            if (!string.IsNullOrEmpty(flag))
                ExtractSalesFull = !flag.Equals("false", StringComparison.OrdinalIgnoreCase); //default true
            flag = Input.GetValue(settings, "extractSalesFromXmlFile");
            if (!string.IsNullOrEmpty(flag))
                ExtractSalesFromXmlFile = flag.Equals("true", StringComparison.OrdinalIgnoreCase);
            flag = Input.GetValue(settings, "orderDateReversed");
            if (!string.IsNullOrEmpty(flag))
                OrderDateReversed = flag.Equals("true", StringComparison.OrdinalIgnoreCase);

            //customers
            flag = Input.GetValue(settings, "extractCustomerData");
            if (!string.IsNullOrEmpty(flag))
                ExtractCustomerData = flag.Equals("true", StringComparison.OrdinalIgnoreCase);
            flag = Input.GetValue(settings, "requireEmailOptIn");
            if (!string.IsNullOrEmpty(flag))
                RequireEmailOptIn = flag.Equals("true", StringComparison.OrdinalIgnoreCase);
            flag = Input.GetValue(settings, "trackShopperActivity");
            if (!string.IsNullOrEmpty(flag))
                TrackShopperActivity = flag.Equals("true", StringComparison.OrdinalIgnoreCase);
            PersonaMappingFields = Input.GetValue(settings, "personaMappingFields");

            //catalog
            flag = Input.GetValue(settings, "invalidatePricesOnExtract");
            if (!string.IsNullOrEmpty(flag))
                InvalidatePricesOnExtract = flag.Equals("true", StringComparison.OrdinalIgnoreCase);
            flag = Input.GetValue(settings, "invalidatePricesOnExtractComplete");
            if (!string.IsNullOrEmpty(flag))
                InvalidatePricesOnExtractComplete = flag.Equals("true", StringComparison.OrdinalIgnoreCase);
            flag = Input.GetValue(settings, "extractCatalogFromXmlFile");
            if (!string.IsNullOrEmpty(flag))
                ExtractCatalogFromXmlFile = flag.Equals("true", StringComparison.OrdinalIgnoreCase);
            flag = Input.GetValue(settings, "extractAtt2Names");
            if (string.IsNullOrEmpty(flag))
                flag = Input.GetValue(settings, "exportAtt2Names"); //legacy name
            if (!string.IsNullOrEmpty(flag))
                ExtractAtt2Names = flag.Equals("true", StringComparison.OrdinalIgnoreCase);
            flag = Input.GetValue(settings, "allowLowerCatalogCount");
            if (!string.IsNullOrEmpty(flag))
                AllowLowerCatalogCount = flag.Equals("true", StringComparison.OrdinalIgnoreCase);
            flag = Input.GetValue(settings, "allowLowerSalesCount");
            if (!string.IsNullOrEmpty(flag))
                AllowLowerSalesCount = flag.Equals("true", StringComparison.OrdinalIgnoreCase);
            flag = Input.GetValue(settings, "allowLowerCustomerCount");
            if (!string.IsNullOrEmpty(flag))
                AllowLowerCustomerCount = flag.Equals("true", StringComparison.OrdinalIgnoreCase);
            flag = Input.GetValue(settings, "mapStockToVisibility");
            if (!string.IsNullOrEmpty(flag))
                MapStockToVisibility = flag.Equals("true", StringComparison.OrdinalIgnoreCase);
            flag = Input.GetValue(settings, "reverseVisibleFlag");
            if (!string.IsNullOrEmpty(flag))
                ReverseVisibleFlag = flag.Equals("true", StringComparison.OrdinalIgnoreCase);
            flag = Input.GetValue(settings, "ignoreStockUseFlag");
            if (!string.IsNullOrEmpty(flag))
                IgnoreStockUseFlag = flag.Equals("true", StringComparison.OrdinalIgnoreCase);
            flag = Input.GetValue(settings, "ignoreStockInPriceRange");
            if (!string.IsNullOrEmpty(flag))
                IgnoreStockInPriceRange = flag.Equals("true", StringComparison.OrdinalIgnoreCase);
            flag = Input.GetValue(settings, "includeChildrenInCatalog");
            if (!string.IsNullOrEmpty(flag))
                IncludeChildrenInCatalog = flag.Equals("true", StringComparison.OrdinalIgnoreCase);
            flag = Input.GetValue(settings, "useAverageChildRating");
            if (!string.IsNullOrEmpty(flag))
                UseAverageChildRating = flag.Equals("true", StringComparison.OrdinalIgnoreCase);
            HiddenSalePriceText = Input.GetValue(settings, "hiddenSalePriceText");

            //categories
            MapCategoriesToFilters = Input.GetValue(settings, "mapCategoriesToFilters")
                                                                        .Equals("true", StringComparison.OrdinalIgnoreCase);
            IncludeCategoryParents = Input.GetValue(settings, "includeCategoryParents")
                                                                 .Equals("true", StringComparison.OrdinalIgnoreCase);
            flag = Input.GetValue(settings, "exportDepartmentNames");
            if (!string.IsNullOrEmpty(flag))
                ExportDepartmentNames = flag.Equals("true", StringComparison.OrdinalIgnoreCase);
            flag = Input.GetValue(settings, "useDepartmentsAsCategories");
            if (!string.IsNullOrEmpty(flag))
                UseDepartmentsAsCategories = flag.Equals("true", StringComparison.OrdinalIgnoreCase);
            var catSep = Input.GetValue(settings, "categorySeparator");
            if (!string.IsNullOrEmpty(catSep))
            {
                char charTest;
                if (Input.TryConvert(catSep, out charTest))
                    CategorySeparator = charTest.ToString();
                else
                    CategorySeparator = catSep;
            }

            //images
            AllowMissingPhotos = false;
            var amp = settings.Element("allowMissingPhotos");
            if (amp != null)
            {
                if (amp.Value != null && amp.Value.Equals("true"))
                    AllowMissingPhotos = true;
                else
                    AllowMissingPhotos = Input.GetAttribute(amp, "enabled").Equals("true");
            }
            var imageThreshold = Input.SafeIntConvert(Input.GetValue(settings, "missingImageThreshold"), -1);
            if (imageThreshold > -1) MissingImageThreshold = imageThreshold;
            flag = Input.GetValue(settings, "extrapolateThumbnailPath");
            if (!string.IsNullOrEmpty(flag))
                ExtrapolateThumbnailPath = flag.Equals("true");
            flag = Input.GetValue(settings, "scrapeCategoryThumbnails");
            if (string.IsNullOrEmpty(flag))
                flag = Input.GetValue(settings, "useCacheThumb"); //depricated
            if (!string.IsNullOrEmpty(flag))
                ScrapeCategoryThumbnails = !flag.Equals("false"); //default true
            flag = Input.GetValue(settings, "forceCategoryTreeScrape");
            if (!string.IsNullOrEmpty(flag))
                ForceCategoryTreeScrape = flag.Equals("true");
            flag = Input.GetValue(settings, "imageLinkBaseUrl");
            if (string.IsNullOrEmpty(flag))
                flag = Input.GetValue(settings, "photoBaseUrl"); //legacy name
            if (!string.IsNullOrEmpty(flag))
                ImageLinkBaseUrl = flag;
            flag = Input.GetValue(settings, "imageLinkFormat"); //a string.Format parameter that the product id gets plugged into
            if (!string.IsNullOrEmpty(flag))
            {
                if (flag.IndexOf("{0}") >= 0)
                    ImageLinkFormat = flag;
                else if (string.IsNullOrEmpty(ImageLinkBaseUrl))	//missing locator for the id so use it as a baseUrl instead
                        ImageLinkBaseUrl = flag;
            }
            if (!string.IsNullOrEmpty(ImageLinkBaseUrl))
            {
                if (!ImageLinkBaseUrl.StartsWith("/") && !ImageLinkBaseUrl.StartsWith("http"))
                    ImageLinkBaseUrl = "/" + ImageLinkBaseUrl; //add initial slash
                if (ImageLinkBaseUrl.EndsWith("/"))
                    ImageLinkBaseUrl = ImageLinkBaseUrl.Substring(0, ImageLinkBaseUrl.Length - 1); //remove final slash
            }
            flag = Input.GetValue(settings, "pageLinkFormat"); //a string.Format parameter that the product id gets plugged into
            if (!string.IsNullOrEmpty(flag) && flag.Contains("{0}"))
                    PageLinkFormat = flag;
            ProductNodeSelector = Input.GetValue(settings, "productNodeSelector");
            ImageUrlSelector = Input.GetValue(settings, "imageUrlSelector");
            ImageUrlPrefix = Input.GetValue(settings, "imageUrlPrefix");
            ImageUrlSuffix = Input.GetValue(settings, "imageUrlSuffix");
            PidSelector = Input.GetValue(settings, "pidSelector");
            PidPrefix = Input.GetValue(settings, "pidPrefix");
            PidSuffix = Input.GetValue(settings, "pidSuffix");
            CommentParseKey = Input.GetValue(settings, "commentParseKey");
            if (string.IsNullOrEmpty(ProductNodeSelector))
                ProductNodeSelector = Input.GetValue(settings, "imageNodeSelector"); //depricated
            if (string.IsNullOrEmpty(ImageUrlPrefix))
                ImageUrlPrefix = Input.GetValue(settings, "imageSrcSelector"); //depricated

            //First check CartRules for defaults
            var cartRules = ReadCartRules(CartType);
            Fields.InitializeFields(cartRules, true);
            Fields.InitializeFields(settings, false);

            //conditional rules
            ExclusionRules = new List<Condition>();
            var exConditions = settings.Element("exclusionConditions");
            if (exConditions != null)
            {
                foreach (var ec in exConditions.Elements("condition"))
                {
                    var name = Input.GetAttribute(ec, "name");
                    var comparison = Input.GetAttribute(ec, "comparison");
                    var value = Input.GetAttribute(ec, "value");
                    var fieldName = Input.GetAttribute(ec, "fieldName");
                    var conditionType = Input.GetAttribute(ec, "type");
                    //var resultField = FieldsIncludeTablePrefix ? StripTablePrefix(fieldName) : fieldName;
                    try
                    {
                        ExclusionRules.Add(new Condition(name, comparison, value, fieldName)); //, resultField));
                    }
                    catch (Exception ex)
                    {
                        if (BoostLog.Instance != null)
                            BoostLog.Instance.WriteEntry(EventLogEntryType.Error, "Error creating exclusion rule", ex, Alias);
                    }
                }
            }
            var exSet = settings.Element("exclusionSet");
            if (exSet != null)
            {
                ExclusionSet = new LogicSet(Alias, exSet);
            }

            FilterParsingRules = null;
            var filterParsing = settings.Element("filterParsingRules");
            if (filterParsing != null)
            {
                FilterParsingRules = new List<ParseGroup>();
                var parseGroups = filterParsing.Elements("parseGroup");
                if (parseGroups != null)
                {
                    var tempVal = "";
                    foreach (var g in parseGroups)
                    {
                        var newGroup = new ParseGroup
                                                                {
                                                                    Delimiter = Input.GetAttribute(g, "delimiter")
                                                                };

                        var parseRules = g.Elements("parseRule");
                        if (parseRules == null) continue;
                        newGroup.ParseRules = new List<ParseRule>();
                        foreach (var pr in parseRules)
                        {
                            newGroup.ParseRules.Add(new ParseRule
                                                                            {
                                                                                FromField = Input.GetAttribute(pr, "fromField"),
                                                                                ToField = Input.GetAttribute(pr, "toField"),
                                                                                RegexMatch = Input.GetAttribute(pr, "regexMatch"),
                                                                                RegexGroup = Input.SafeIntConvert(Input.GetAttribute(pr, "regexGroup")),
                                                                                Expand = Input.GetAttribute(pr, "expand").Equals("true"),
                                                                                Modulo = Input.SafeIntConvert(Input.GetAttribute(pr, "modulo")),
                                                                                Delimiter = Input.GetAttribute(pr, "delimiter"),
                                                                                Format = Input.GetAttribute(pr, "format")
                                                                            });
                        }
                        FilterParsingRules.Add(newGroup);
                    }
                }
            }

            FilterRules = new List<Condition>();
            var filterConditions = settings.Element("filterConditions");
            if (filterConditions != null)
            {
                foreach (var f in filterConditions.Elements("condition"))
                {
                    var name = Input.GetAttribute(f, "name");
                    var comparison = Input.GetAttribute(f, "comparison");
                    var value = Input.GetAttribute(f, "value");
                    var fieldName = Input.GetAttribute(f, "fieldName");
                    //var resultField = FieldsIncludeTablePrefix ? StripTablePrefix(fieldName) : fieldName;
                    try
                    {
                        FilterRules.Add(new Condition(name, comparison, value, fieldName)); //, resultField));
                    }
                    catch (Exception ex)
                    {
                        if (BoostLog.Instance != null)
                            BoostLog.Instance.WriteEntry(EventLogEntryType.Error, "Error creating filter rule", ex, Alias);
                    }
                }
            }
            UniversalFilterName = Input.GetValue(settings, "universalFilterName");
            if (UniversalFilterName.Length < 1)
                UniversalFilterName = "Universal";
            FilterTopSellers = Input.GetValue(settings, "filterTopSellers").Equals("true", StringComparison.CurrentCultureIgnoreCase);

            ReplacementRules = new List<ReplacementCondition>();
            var repConditions = settings.Element("replacementConditions");
            if (repConditions != null)
            {
                foreach (var rep in repConditions.Elements("condition"))
                {
                    var name = Input.GetAttribute(rep, "name");
                    var type = Input.GetAttribute(rep, "type");
                    var oldName = Input.GetAttribute(rep, "oldFieldName");
                    var newName = Input.GetAttribute(rep, "newFieldName");
                    var oldResultField = oldName; //FieldsIncludeTablePrefix ? StripTablePrefix(oldName) : oldName;
                    var newResultField = newName; //FieldsIncludeTablePrefix ? StripTablePrefix(newName) : newName;
                    ReplacementRules.Add(new ReplacementCondition(name, type, oldName, newName, oldResultField, newResultField));
                }
            }

            var catConditions = settings.Element("categoryConditions");
            if (catConditions != null)
            {
                foreach (var cat in catConditions.Elements("condition"))
                {
                    var groupId = Input.GetAttribute(cat, "groupId");
                    var type = Input.GetAttribute(cat, "type");
                    var value = Input.GetAttribute(cat, "value");
                    CategoryRules.AddCat(type, value, groupId);
                }
            }

            //Featured selections
            var featuredConditions = settings.Element("featuredConditions") ??
                                                             settings.Element("manualTopSellConditions"); //depricated
            if (featuredConditions != null)
            {
                FeaturedRules = new List<FeaturedRecCondition>();
                foreach (var cat in featuredConditions.Elements("condition"))
                {
                    var queryField = Input.GetAttribute(cat, "fieldName");
                    var resultField = queryField; //FieldsIncludeTablePrefix ? StripTablePrefix(queryField) : queryField;
                    var include = Input.GetAttribute(cat, "type").Equals("include", StringComparison.OrdinalIgnoreCase);
                    var enabled = !Input.GetAttribute(cat, "enabled").Equals("false", StringComparison.OrdinalIgnoreCase);
                    FeaturedRules.Add(new FeaturedRecCondition(queryField, resultField, include, enabled));
                }
            }

            var featuredCrossSellConditions = settings.Element("featuredCrossSellConditions") ??
                                                                                settings.Element("manualCrossSellConditions");  //depricated
            if (featuredCrossSellConditions != null)
            {
                FeaturedCrossSellRules = new List<FeaturedRecCondition>();
                foreach (var cat in featuredCrossSellConditions.Elements("condition"))
                {
                    var queryField = Input.GetAttribute(cat, "fieldName");
                    var resultField = queryField; //FieldsIncludeTablePrefix ? StripTablePrefix(queryField) : queryField;
                    var include = Input.GetAttribute(cat, "type").Equals("include", StringComparison.OrdinalIgnoreCase);
                    var enabled = !Input.GetAttribute(cat, "enabled").Equals("false", StringComparison.OrdinalIgnoreCase);
                    FeaturedCrossSellRules.Add(new FeaturedRecCondition(queryField, resultField, include, enabled));
                }
            }

            var featuredUpSellConditions = settings.Element("featuredUpSellConditions") ??
                                                                         settings.Element("manualUpSellConditions");  //depricated
            if (featuredUpSellConditions != null)
            {
                FeaturedUpSellRules = new List<FeaturedRecCondition>();
                foreach (var cat in featuredUpSellConditions.Elements("condition"))
                {
                    var queryField = Input.GetAttribute(cat, "fieldName");
                    var resultField = queryField; //FieldsIncludeTablePrefix ? StripTablePrefix(queryField) : queryField;
                    var include = Input.GetAttribute(cat, "type").Equals("include", StringComparison.OrdinalIgnoreCase);
                    var enabled = !Input.GetAttribute(cat, "enabled").Equals("false", StringComparison.OrdinalIgnoreCase);
                    FeaturedUpSellRules.Add(new FeaturedRecCondition(queryField, resultField, include, enabled));
                }
            }

            //timers
            var upTimers = settings.Elements("updateTimer");
            if (!upTimers.Any())
                upTimers = settings.Elements("uploadTimer"); //depricated
            if (upTimers.Any())
            {
                ExtractorSchedules = new List<ExtractorSchedule>();
                foreach (var ut in upTimers)
                {
                    ExtractorSchedules.Add(new ExtractorSchedule(ut));
                }
            }

            //Exclusion Stats
            var exclusionStats = settings.Elements("exclusionStats");
            if (exclusionStats.Any())
            {
                var stats = exclusionStats.Elements("stat");
                if (stats != null && stats.Any())
                {
                    ExclusionStats = new Dictionary<string, int>();
                    foreach (var s in stats)
                    {
                        var key = Input.GetAttribute(s, "name");
                        var value = Input.SafeIntConvert(Input.GetAttribute(s, "value"));
                        ExclusionStats.Add(key, value);
                    }
                }
            }

            #endregion
        }