internal IEnumerable <IEntity> ConvertToAds(PromotionDataTableMapper promotionDataTableMapper, PromotionEntryCodeProvider promotionEntryCodeProvider)
        {
            var promotionLanguageMappedColumns = ToMappedColumnInfos(promotionDataTableMapper.PromotionLanguageDataTable, promotionEntryCodeProvider).ToArray();
            var promotionMappedColumns         = ToMappedColumnInfos(promotionDataTableMapper.PromotionDataTable).ToArray();
            var campaignMappedColumns          = ToMappedColumnInfos(promotionDataTableMapper.CampaignDataTable).ToArray();

            foreach (var plpc in promotionDataTableMapper.Promotions)
            {
                var plpc1        = plpc;
                var plAttributes = promotionLanguageMappedColumns
                                   .Select(plmc => new Attribute(plmc.SpecificESalesName, plmc.GetValue(plpc1.PromotionLanguageRow)))
                                   .Where(a => !string.IsNullOrWhiteSpace(a.Values.FirstOrDefault()));
                var pAttributes = promotionMappedColumns
                                  .Select(pmc => new Attribute(pmc.SpecificESalesName, pmc.GetValue(plpc1.PromotionRow)))
                                  .Where(a => !string.IsNullOrWhiteSpace(a.Values.FirstOrDefault()));
                var cAttributes = campaignMappedColumns
                                  .Select(cmc => new Attribute(cmc.SpecificESalesName, cmc.GetValue(plpc1.CampaignRow)))
                                  .Where(a => !string.IsNullOrWhiteSpace(a.Values.FirstOrDefault()));

                //Unique attribute names. PromotionLanguage is most important, then Promotion and last Campaign.
                yield return(new Ad(
                                 cAttributes.Where(ca => !pAttributes.Select(pa => pa.Name).Contains(ca.Name)).Where(ca => !plAttributes.Select(pla => pla.Name).Contains(ca.Name))
                                 .Concat(pAttributes.Where(pa => !plAttributes.Select(pla => pla.Name).Contains(pa.Name)))
                                 .Concat(plAttributes)
                                 .Concat(StaticAttributes)));
            }
        }
 public AdsIndexBuilder(
     IAppConfig appConfig, IFileSystem fileSystem, FileHelper fileHelper, PromotionDataTableMapper dataTableMapper,
     PromotionEntryCodeProvider promotionEntryCodeProvider, IIndexSystemMapper indexSystem,
     IEnumerable <IAdsAppender> appenderPlugins, IAdConverter converterPlugin = null)
 {
     _appConfig                  = appConfig;
     _fileSystem                 = fileSystem;
     _fileHelper                 = fileHelper;
     _dataTableMapper            = dataTableMapper;
     _promotionEntryCodeProvider = promotionEntryCodeProvider;
     _indexSystem                = indexSystem;
     _converterPlugin            = converterPlugin;
     _appenderPlugins            = appenderPlugins.ToArray();
 }
        public PromotionEntryCodeProvider(PromotionDataTableMapper promotionDataTableMapper, IEnumerable <IPromotionEntryCodes> promotionEntryCodes)
        {
            var promotions      = promotionDataTableMapper.Promotions;
            var providers       = promotionEntryCodes.ToDictionary(pec => pec.PromotionType);
            var adKeysWithCodes = (from p in promotions
                                   where providers.ContainsKey(p.PromotionRow.PromotionType)
                                   let provider = providers[p.PromotionRow.PromotionType]
                                                  select new
            {
                AdKey = AttributeHelper.CreateKey(p.PromotionRow.PromotionId.ToString(), p.PromotionLanguageRow.LanguageCode),
                Included = provider.Included(p),
                Excluded = provider.Excluded(p)
            }).ToArray();

            _included = adKeysWithCodes
                        .SelectMany(a => a.Included.Select(i => new { Included = i, a.AdKey }))
                        .ToLookup(i => i.Included, i => i.AdKey);
            _excluded = adKeysWithCodes
                        .SelectMany(a => a.Excluded.Select(e => new { Excluded = e, a.AdKey }))
                        .ToLookup(e => e.Excluded, e => e.AdKey);
            _adsWithIncluded = new HashSet <string>(adKeysWithCodes.Where(a => a.Included.Any()).Select(a => a.AdKey));
            _adsWithExcluded = new HashSet <string>(adKeysWithCodes.Where(a => a.Excluded.Any()).Select(a => a.AdKey));
        }
        public void IndexAds()
        {
            var builder = new ContainerBuilder();

            var          appConfigMock   = new Mock <IAppConfig>();
            const string applicationName = "Test";
            const string scope           = "TestScope";

            appConfigMock.Setup(c => c.ApplicationId).Returns(() => applicationName);
            appConfigMock.Setup(c => c.Scope).Returns(() => scope);
            appConfigMock.Setup(c => c.Enable).Returns(() => true);
            appConfigMock.Setup(c => c.EnableAds).Returns(() => true);
            builder.Register(c => appConfigMock.Object);

            var fileSystemMock = new Mock <IFileSystem>();
            var xmlStream      = new MemoryStream();

            fileSystemMock.Setup(fsm => fsm.Open(It.IsAny <string>(), It.IsAny <FileMode>(), It.IsAny <FileAccess>())).Returns(xmlStream);
            builder.Register(c => fileSystemMock.Object);

            builder.RegisterType <FileHelper>();

            var          promotionLanguageDataTable = new PromotionDto.PromotionLanguageDataTable().WithRow(1, 1000001, "en-us", "20% off on Chateau");
            const string promotionTypeInclude       = "BuyXGetYDiscounted";
            var          applicationId      = Guid.Parse("024b3344-402a-4100-8764-384263b729c1");
            var          promotionDataTable = new PromotionDto.PromotionDataTable().WithRow(1000001, "20% off on Chateau [NO LANGUAGE]", promotionTypeInclude, 1, applicationId,
                                                                                            startDate: new DateTime(2012, 1, 1, 8, 17, 0, DateTimeKind.Local),
                                                                                            endDate: new DateTime(2020, 12, 31, 8, 17, 0, DateTimeKind.Local),
                                                                                            created: new DateTime(2012, 9, 21, 7, 19, 11, DateTimeKind.Local),
                                                                                            modified: new DateTime(2012, 10, 4, 7, 6, 28, DateTimeKind.Local));
            var campaignDataTable = new CampaignDto.CampaignDataTable().WithRow(
                1, "Wine", applicationId, created: new DateTime(2008, 10, 7, 22, 14, 13, DateTimeKind.Local),
                exported: new DateTime(1970, 1, 1, 1, 0, 0, DateTimeKind.Local), modified: new DateTime(2012, 10, 4, 7, 5, 9, DateTimeKind.Local));
            var promotionDataTableMapper = new PromotionDataTableMapper(promotionLanguageDataTable, promotionDataTable, campaignDataTable);

            builder.Register(c => promotionDataTableMapper);

            var promotionEntryCodes1 = new Mock <IPromotionEntryCodes>();

            promotionEntryCodes1.Setup(pm => pm.PromotionType).Returns(promotionTypeInclude);
            promotionEntryCodes1
            .Setup(pm => pm.Included(It.IsAny <Promotion>()))
            .Returns <Promotion>(p => p.PromotionRow.PromotionType == promotionTypeInclude ? new[] { "WHATEVER" } : Enumerable.Empty <string>());
            promotionEntryCodes1.Setup(pm => pm.Excluded(It.IsAny <Promotion>())).Returns <Promotion>(p => Enumerable.Empty <string>());
            builder.Register(c => promotionEntryCodes1.Object);

            builder.RegisterType <PromotionEntryCodeProvider>();

            var indexSystemMock = new Mock <IIndexSystemMapper>();

            builder.Register(c => indexSystemMock.Object);

            builder.RegisterType <AdsIndexBuilder>();

            var expectedXElement = XElement.Parse(@"<operations>
                                                        <clear>
                                                          <ad/>
                                                        </clear>
                                                        <add>
                                                          <ad>
                                                            <campaign_name>Wine</campaign_name>
                                                            <campaign_created>1223410453000</campaign_created>
                                                            <campaign_exported>0</campaign_exported>
                                                            <campaign_modified>1349327109000</campaign_modified>
                                                            <campaign_modified_by>admin</campaign_modified_by>
                                                            <campaign_is_active>True</campaign_is_active>
                                                            <campaign_is_archived>False</campaign_is_archived>
                                                            <name>20% off on Chateau</name>
                                                            <application_id>" + applicationId + @"</application_id>
                                                            <status>active</status>
                                                            <start_time>2012-01-01T07:17:00Z</start_time>
                                                            <end_time>2020-12-31T07:17:00Z</end_time>
                                                            <offer_amount>20.00</offer_amount>
                                                            <offer_type>1</offer_type>
                                                            <promotion_group>entry</promotion_group>
                                                            <campaign_key>1</campaign_key>
                                                            <exclusivity_type>none</exclusivity_type>
                                                            <priority>1</priority>
                                                            <created>1348204751000</created>
                                                            <modified>1349327188000</modified>
                                                            <modified_by>admin</modified_by>
                                                            <promotion_type>BuyXGetYDiscounted</promotion_type>
                                                            <per_order_limit>0</per_order_limit>
                                                            <application_limit>0</application_limit>
                                                            <customer_limit>0</customer_limit>
                                                            <locale>en_US</locale>
                                                            <locale_filter>en_US</locale_filter>
                                                            <ad_key>1000001_en_US</ad_key>
                                                            <included>locale_filter:'en_US' AND ad_key_included:'1000001_en_US'</included>
                                                            <live_products>4</live_products>
                                                          </ad>
                                                        </add>
                                                      </operations>");

            var container = builder.Build();

            container.Resolve <AdsIndexBuilder>().Build(false);
            XElement xml;

            using (var reader = new StreamReader(new MemoryStream(xmlStream.ToArray())))
            {
                xml = XElement.Parse(reader.ReadToEnd());
            }

            try
            {
                Assert.That(xml.ElementsEquivalentTo(expectedXElement));
            }
            catch (AssertionException)
            {
                Console.WriteLine("Expected:");
                Console.WriteLine(expectedXElement);
                Console.WriteLine();
                Console.WriteLine("Actual:");
                Console.WriteLine(xml);
                throw;
            }
        }
        public void Setup()
        {
            _adAttributeHelper = new AdAttributeHelper();

            _attributes = _adAttributeHelper.ConvertToAttributes(new PromotionDto.PromotionLanguageDataTable(), new PromotionDto.PromotionDataTable(),
                                                                 new CampaignDto.CampaignDataTable());
            var search = new SearchOptions(null, Format.PipeSeparated, true, true);
            var filter = new FilterOptions(Format.PipeSeparated, Tokenization.CaseInsensitive);
            var sort   = new SortOptions(Normalization.CaseInsensitive);

            _expectedConfigurationAttributes = new[]
            {
                new ConfigurationAttribute("ad_key", type.@string, Present.Yes, search, filter, sort),
                new ConfigurationAttribute("name", type.@string, Present.Yes, search, filter, sort),
                new ConfigurationAttribute("application_id", type.@string, Present.Yes, search, filter, sort),
                new ConfigurationAttribute("status", type.@string, Present.Yes, search, filter, sort),
                new ConfigurationAttribute("coupon_code", type.@string, Present.Yes, search, filter, sort),
                new ConfigurationAttribute("offer_amount", type.@string, Present.Yes, search, filter, sort),
                new ConfigurationAttribute("offer_type", type.@string, Present.Yes, search, filter, sort),
                new ConfigurationAttribute("promotion_group", type.@string, Present.Yes, search, filter, sort),
                new ConfigurationAttribute("campaign_key", type.@string, Present.Yes, search, filter, sort),
                new ConfigurationAttribute("exclusivity_type", type.@string, Present.Yes, search, filter, sort),
                new ConfigurationAttribute("priority", type.@string, Present.Yes, search, filter, sort),
                new ConfigurationAttribute("created", type.@long, Present.Yes, search, filter, sort),
                new ConfigurationAttribute("modified", type.@long, Present.Yes, search, filter, sort),
                new ConfigurationAttribute("modified_by", type.@string, Present.Yes, search, filter, sort),
                new ConfigurationAttribute("promotion_type", type.@string, Present.Yes, search, filter, sort),
                new ConfigurationAttribute("per_order_limit", type.@int, Present.Yes, search, filter, sort),
                new ConfigurationAttribute("application_limit", type.@int, Present.Yes, search, filter, sort),
                new ConfigurationAttribute("customer_limit", type.@int, Present.Yes, search, filter, sort),
                new ConfigurationAttribute("campaign_name", type.@string, Present.Yes, search, filter, sort),
                new ConfigurationAttribute("campaign_created", type.@long, Present.Yes, search, filter, sort),
                new ConfigurationAttribute("campaign_exported", type.@long, Present.Yes, search, filter, sort),
                new ConfigurationAttribute("campaign_modified", type.@long, Present.Yes, search, filter, sort),
                new ConfigurationAttribute("campaign_modified_by", type.@string, Present.Yes, search, filter, sort),
                new ConfigurationAttribute("campaign_is_active", type.@string, Present.Yes, search, filter, sort),
                new ConfigurationAttribute("campaign_is_archived", type.@string, Present.Yes, search, filter, sort),
                new ConfigurationAttribute("campaign_comments", type.@string, Present.Yes, search, filter, sort),
                new ConfigurationAttribute("locale_filter", type.@string, Present.Yes, search, filter, sort)
            };

            var campaignTable = new CampaignDto.CampaignDataTable();
            var campaignId    =
                campaignTable.AddCampaignRow(Guid.Parse("0351ca45-b49f-4002-8433-1008a1f6fa6b"), DateTime.MinValue, DateTime.MinValue, "CampaignNameTest",
                                             new DateTime(2012, 08, 20, 07, 07, 11, DateTimeKind.Local), new DateTime(2012, 08, 21, 0, 0, 0, DateTimeKind.Local),
                                             new DateTime(2012, 08, 21, 13, 02, 37, DateTimeKind.Local), "CampaignModifiedByTest", true, false, "CommentsTest")
                .CampaignId;
            var promotionTable = new PromotionDto.PromotionDataTable();
            var promotionRow   = promotionTable.AddPromotionRow("NameTest", Guid.Parse("0351ca45-b49f-4002-8433-1008a1f6fa6b"), "active",
                                                                new DateTime(2012, 08, 20, 07, 37, 00, DateTimeKind.Local),
                                                                new DateTime(2012, 08, 23, 07, 37, 00, DateTimeKind.Local), "CouponCodeTest", 100.5m, 1,
                                                                "PromotionGroupTest", campaignId, "ExclusivityTypeTest", 3,
                                                                new DateTime(2012, 08, 20, 07, 39, 07, DateTimeKind.Local),
                                                                new DateTime(2012, 11, 06, 12, 43, 16, DateTimeKind.Local), "ModifiedByTest", "PromotionTypeTest", 4, 5,
                                                                new byte[] { 1, 2, 3 }, 6, 10);
            var promotionLanguageTable = new PromotionDto.PromotionLanguageDataTable();

            promotionLanguageTable.AddPromotionLanguageRow("DisplayNameTestSE", "sv-SE", promotionRow);
            promotionLanguageTable.AddPromotionLanguageRow("DisplayNameTestFI", "sv-FI", promotionRow);

            var promotionDataTableMapper   = new PromotionDataTableMapper(promotionLanguageTable, promotionTable, campaignTable);
            var promotionEntryCodeProvider = new PromotionEntryCodeProvider(promotionDataTableMapper, Enumerable.Empty <IPromotionEntryCodes>());

            _ads = _adAttributeHelper.ConvertToAds(promotionDataTableMapper, promotionEntryCodeProvider);

            _expectedAds = new[]
            {
                new Ad(new[]
                {
                    //CampaignTable
                    new Attribute("campaign_name", "CampaignNameTest"),
                    new Attribute("campaign_created", "1345439231000"),
                    new Attribute("campaign_exported", "1345500000000"),
                    new Attribute("campaign_modified", "1345546957000"),
                    new Attribute("campaign_modified_by", "CampaignModifiedByTest"),
                    new Attribute("campaign_is_active", "True"),
                    new Attribute("campaign_is_archived", "False"),
                    new Attribute("campaign_comments", "CommentsTest"),

                    //PromotionTable
                    new Attribute("application_id", "0351ca45-b49f-4002-8433-1008a1f6fa6b"),
                    new Attribute("status", "active"),
                    new Attribute("start_time", "2012-08-20T05:37:00Z"),
                    new Attribute("end_time", "2012-08-23T05:37:00Z"),
                    new Attribute("coupon_code", "CouponCodeTest"),
                    new Attribute("offer_amount", "100.50"),
                    new Attribute("offer_type", "1"),
                    new Attribute("promotion_group", "PromotionGroupTest"),
                    new Attribute("campaign_key", campaignId.ToString()),
                    new Attribute("exclusivity_type", "ExclusivityTypeTest"),
                    new Attribute("priority", "3"),
                    new Attribute("created", "1345441147000"),
                    new Attribute("modified", "1352202196000"),
                    new Attribute("modified_by", "ModifiedByTest"),
                    new Attribute("promotion_type", "PromotionTypeTest"),
                    new Attribute("per_order_limit", "4"),
                    new Attribute("application_limit", "5"),
                    new Attribute("customer_limit", "6"),
                    new Attribute("max_entry_discount_quantity", "10"),

                    //PromotionLanguageTable
                    new Attribute("ad_key", promotionRow.PromotionId + "_sv_SE"),
                    new Attribute("name", "DisplayNameTestSE"),
                    new Attribute("locale", "sv_SE"),
                    new Attribute("locale_filter", "sv_SE"),
                    new Attribute("included", "locale_filter:'sv_SE'"),

                    //Static Attributes
                    new Attribute("live_products", "4")
                }),
                new Ad(new[]
                {
                    //CampaignTable
                    new Attribute("campaign_name", "CampaignNameTest"),
                    new Attribute("campaign_created", "1345439231000"),
                    new Attribute("campaign_exported", "1345500000000"),
                    new Attribute("campaign_modified", "1345546957000"),
                    new Attribute("campaign_modified_by", "CampaignModifiedByTest"),
                    new Attribute("campaign_is_active", "True"),
                    new Attribute("campaign_is_archived", "False"),
                    new Attribute("campaign_comments", "CommentsTest"),

                    //PromotionTable
                    new Attribute("application_id", "0351ca45-b49f-4002-8433-1008a1f6fa6b"),
                    new Attribute("status", "active"),
                    new Attribute("start_time", "2012-08-20T05:37:00Z"),
                    new Attribute("end_time", "2012-08-23T05:37:00Z"),
                    new Attribute("coupon_code", "CouponCodeTest"),
                    new Attribute("offer_amount", "100.50"),
                    new Attribute("offer_type", "1"),
                    new Attribute("promotion_group", "PromotionGroupTest"),
                    new Attribute("campaign_key", campaignId.ToString()),
                    new Attribute("exclusivity_type", "ExclusivityTypeTest"),
                    new Attribute("priority", "3"),
                    new Attribute("created", "1345441147000"),
                    new Attribute("modified", "1352202196000"),
                    new Attribute("modified_by", "ModifiedByTest"),
                    new Attribute("promotion_type", "PromotionTypeTest"),
                    new Attribute("per_order_limit", "4"),
                    new Attribute("application_limit", "5"),
                    new Attribute("customer_limit", "6"),
                    new Attribute("max_entry_discount_quantity", "10"),

                    //PromotionLanguageTable
                    new Attribute("ad_key", promotionRow.PromotionId + "_sv_FI"),
                    new Attribute("name", "DisplayNameTestFI"),
                    new Attribute("locale", "sv_FI"),
                    new Attribute("locale_filter", "sv_FI"),
                    new Attribute("included", "locale_filter:'sv_FI'"),

                    //Static Attributes
                    new Attribute("live_products", "4")
                })
            };
        }