Beispiel #1
0
        private static void AddRestrictionForParentProgrammeCategory(
            Dictionary <string, ProgrammeDictionary> programmeDictionaries,
            DateTime runEndDate,
            AgRestriction agRestriction,
            Restriction restriction,
            SalesArea salesArea,
            List <AgRestriction> agRestrictions,
            string clashCode,
            Dictionary <string, ProgrammeCategoryHierarchy> programmeCategoriesExternalRefIndex,
            Dictionary <int, ProgrammeCategoryHierarchy> programmeCategoriesIdIndex,
            int programmeCategoryId)
        {
            if (!programmeCategoriesIdIndex.TryGetValue(programmeCategoryId, out var category) || string.IsNullOrEmpty(category.ParentExternalRef))
            {
                return;
            }

            var parentCategoryNum = programmeCategoriesExternalRefIndex.TryGetValue(category.ParentExternalRef, out var parentCategory) ? parentCategory.Id : 0;

            if (parentCategoryNum == 0)
            {
                return;
            }

            var agRestrictionClone = agRestriction.Clone();

            agRestrictionClone.ProductCode = restriction.ProductCode;
            agRestrictionClone.ClashCode   = clashCode;
            agRestrictionClone.CopyCode    = string.IsNullOrWhiteSpace(restriction.ClockNumber)
                ? "0"
                : restriction.ClockNumber;
            agRestrictionClone.ClearanceCode  = restriction.ClearanceCode ?? "";
            agRestrictionClone.SalesAreaNo    = salesArea?.CustomId ?? 0;
            agRestrictionClone.ProgCategoryNo = parentCategoryNum;
            agRestrictionClone.ProgrammeNo    =
                !string.IsNullOrWhiteSpace(restriction.ExternalProgRef) && programmeDictionaries.TryGetValue(restriction.ExternalProgRef.ToUpper(), out var programme)
                    ? programme.Id
                    : 0;
            agRestrictionClone.StartDate = restriction.StartDate.ToString("yyyyMMdd");
            agRestrictionClone.EndDate   = restriction.EndDate.HasValue
                ? restriction.EndDate?.ToString("yyyyMMdd")
                : runEndDate.AddYears(10).ToString("yyyyMMdd");
            agRestrictionClone.IndexType              = restriction.IndexType;
            agRestrictionClone.IndexThreshold         = restriction.IndexThreshold;
            agRestrictionClone.PublicHolidayIndicator =
                AgConversions.ToAgIncludeExcludeEither(restriction.PublicHolidayIndicator);
            agRestrictionClone.SchoolHolidayIndicator =
                AgConversions.ToAgIncludeExcludeEither(restriction.SchoolHolidayIndicator);
            agRestrictionClone.RestrictionType         = Convert.ToInt32(restriction.RestrictionType);
            agRestrictionClone.RestrictionDays         = AgConversions.ToAgDaysAsInt(restriction.RestrictionDays);
            agRestrictionClone.StartTime               = restriction.StartTime?.ToString("hhmmss") ?? "0";
            agRestrictionClone.EndTime                 = restriction.EndTime?.ToString("hhmmss") ?? "995959";
            agRestrictionClone.TimeToleranceMinsBefore = restriction.TimeToleranceMinsBefore;
            agRestrictionClone.TimeToleranceMinsAfter  = restriction.TimeToleranceMinsAfter;
            agRestrictionClone.ProgClassCode           = restriction.ProgrammeClassification ?? "";
            agRestrictionClone.ProgClassFlag           = restriction.ProgrammeClassificationIndicator.ToString();
            agRestrictionClone.LiveBroadcastFlag       = restriction.LiveProgrammeIndicator.ToString();

            agRestrictions.Add(agRestrictionClone);
        }
Beispiel #2
0
        private List <AgRestriction> LoadAgRestriction(List <Restriction> restrictions,
                                                       List <ProgrammeCategoryHierarchy> programmeCategories,
                                                       IReadOnlyCollection <ProgrammeDictionary> programmeDictionaries,
                                                       List <SalesArea> allSalesAreas,
                                                       DateTime runEndDate,
                                                       List <Clash> allClashes,
                                                       AgRestriction agRestriction)
        {
            var agRestrictions      = new List <AgRestriction>();
            var salesAreaNamesIndex = allSalesAreas.ToDictionary(s => s.Name);
            var programmeDictionaryExternalRefsIndex  = programmeDictionaries.ToDictionary(c => c.ExternalReference.ToUpper());
            var programmeCategoryIdsIndex             = new Dictionary <int, ProgrammeCategoryHierarchy>();
            var programmeCategoryNamesIndex           = new Dictionary <string, ProgrammeCategoryHierarchy>();
            var programmeCategoryExternalRefsIndex    = new Dictionary <string, ProgrammeCategoryHierarchy>();
            var clashesExternalRefsIndex              = new Dictionary <string, Clash>();
            var clashesParentExternalIdentifiersIndex = new Dictionary <string, List <Clash> >();

            foreach (var programmeCategory in programmeCategories)
            {
                programmeCategoryIdsIndex.Add(programmeCategory.Id, programmeCategory);
                programmeCategoryNamesIndex.Add(programmeCategory.Name.ToUpper(), programmeCategory);

                if (!string.IsNullOrWhiteSpace(programmeCategory.ExternalRef))
                {
                    programmeCategoryExternalRefsIndex.Add(programmeCategory.ExternalRef, programmeCategory);
                }
            }

            foreach (var clash in allClashes)
            {
                clashesExternalRefsIndex.Add(clash.Externalref, clash);

                if (string.IsNullOrWhiteSpace(clash.ParentExternalidentifier))
                {
                    continue;
                }

                if (!clashesParentExternalIdentifiersIndex.ContainsKey(clash.ParentExternalidentifier))
                {
                    clashesParentExternalIdentifiersIndex.Add(clash.ParentExternalidentifier, new List <Clash>());
                }

                clashesParentExternalIdentifiersIndex[clash.ParentExternalidentifier].Add(clash);
            }

            foreach (var restriction in restrictions)
            {
                List <SalesArea> salesAreas;

                if (restriction.SalesAreas == null || !restriction.SalesAreas.Any())
                {
                    salesAreas = allSalesAreas;
                }
                else
                {
                    salesAreas = new List <SalesArea>();

                    foreach (var item in restriction.SalesAreas)
                    {
                        if (salesAreaNamesIndex.TryGetValue(item, out SalesArea area))
                        {
                            salesAreas.Add(area);
                        }
                    }
                }

                foreach (var salesArea in salesAreas)
                {
                    if (restriction.RestrictionBasis == RestrictionBasis.Clash)
                    {
                        IEnumerable <Clash> clashes = Enumerable.Empty <Clash>();

                        if (clashesParentExternalIdentifiersIndex.TryGetValue(restriction.ClashCode, out var childClashes))
                        {
                            clashes = clashes.Union(childClashes);
                        }

                        if (clashesExternalRefsIndex.TryGetValue(restriction.ClashCode, out var item))
                        {
                            clashes = clashes.Append(item);
                        }

                        foreach (var clash in clashes)
                        {
                            var agRestrictionClone = agRestriction.Clone();

                            agRestrictionClone.ProductCode = restriction.ProductCode;
                            agRestrictionClone.ClashCode   = clash.Externalref;
                            agRestrictionClone.CopyCode    = string.IsNullOrWhiteSpace(restriction.ClockNumber)
                                ? "0"
                                : restriction.ClockNumber;
                            agRestrictionClone.ClearanceCode  = restriction.ClearanceCode ?? "";
                            agRestrictionClone.SalesAreaNo    = salesArea?.CustomId ?? 0;
                            agRestrictionClone.ProgCategoryNo =
                                !string.IsNullOrWhiteSpace(restriction.ProgrammeCategory) && programmeCategoryNamesIndex.TryGetValue(restriction.ProgrammeCategory.ToUpper(), out var category)
                                    ? category.Id
                                    : 0;
                            agRestrictionClone.ProgrammeNo =
                                !string.IsNullOrWhiteSpace(restriction.ExternalProgRef) && programmeDictionaryExternalRefsIndex.TryGetValue(restriction.ExternalProgRef.ToUpper(), out var programme)
                                    ? programme.Id
                                    : 0;
                            agRestrictionClone.StartDate = restriction.StartDate.ToString("yyyyMMdd");
                            agRestrictionClone.EndDate   = restriction.EndDate.HasValue
                                ? restriction.EndDate?.ToString("yyyyMMdd")
                                : runEndDate.AddYears(10).ToString("yyyyMMdd");
                            agRestrictionClone.IndexType              = restriction.IndexType;
                            agRestrictionClone.IndexThreshold         = restriction.IndexThreshold;
                            agRestrictionClone.PublicHolidayIndicator =
                                AgConversions.ToAgIncludeExcludeEither(restriction.PublicHolidayIndicator);
                            agRestrictionClone.SchoolHolidayIndicator =
                                AgConversions.ToAgIncludeExcludeEither(restriction.SchoolHolidayIndicator);
                            agRestrictionClone.RestrictionType         = Convert.ToInt32(restriction.RestrictionType);
                            agRestrictionClone.RestrictionDays         = AgConversions.ToAgDaysAsInt(restriction.RestrictionDays);
                            agRestrictionClone.StartTime               = restriction.StartTime?.ToString("hhmmss") ?? "0";
                            agRestrictionClone.EndTime                 = restriction.EndTime?.ToString("hhmmss") ?? "995959";
                            agRestrictionClone.TimeToleranceMinsBefore = restriction.TimeToleranceMinsBefore;
                            agRestrictionClone.TimeToleranceMinsAfter  = restriction.TimeToleranceMinsAfter;
                            agRestrictionClone.ProgClassCode           = restriction.ProgrammeClassification ?? "";
                            agRestrictionClone.ProgClassFlag           = restriction.ProgrammeClassificationIndicator.ToString();
                            agRestrictionClone.LiveBroadcastFlag       = restriction.LiveProgrammeIndicator.ToString();
                            agRestrictionClone.EpisodeNo               = restriction.EpisodeNumber;

                            agRestrictions.Add(agRestrictionClone);

                            if (agRestrictionClone.ProgCategoryNo != 0)
                            {
                                var clashCode = clash.Externalref;
                                AddRestrictionForParentProgrammeCategory(
                                    programmeDictionaryExternalRefsIndex,
                                    runEndDate,
                                    agRestriction,
                                    restriction,
                                    salesArea,
                                    agRestrictions,
                                    clashCode,
                                    programmeCategoryExternalRefsIndex,
                                    programmeCategoryIdsIndex,
                                    agRestrictionClone.ProgCategoryNo
                                    );
                            }
                        }
                    }
                    else
                    {
                        var agRestrictionClone = agRestriction.Clone();

                        agRestrictionClone.ProductCode = restriction.ProductCode;
                        agRestrictionClone.ClashCode   = restriction.ClashCode ?? "";
                        agRestrictionClone.CopyCode    = string.IsNullOrWhiteSpace(restriction.ClockNumber)
                            ? "0"
                            : restriction.ClockNumber;
                        agRestrictionClone.ClearanceCode  = restriction.ClearanceCode ?? "";
                        agRestrictionClone.SalesAreaNo    = salesArea?.CustomId ?? 0;
                        agRestrictionClone.ProgCategoryNo =
                            !string.IsNullOrWhiteSpace(restriction.ProgrammeCategory) && programmeCategoryNamesIndex.TryGetValue(restriction.ProgrammeCategory, out var category)
                                ? category.Id
                                : 0;
                        agRestrictionClone.ProgrammeNo =
                            !string.IsNullOrWhiteSpace(restriction.ExternalProgRef) && programmeDictionaryExternalRefsIndex.TryGetValue(restriction.ExternalProgRef.ToUpper(), out var programme)
                                ? programme.Id
                                : 0;
                        agRestrictionClone.StartDate = restriction.StartDate.ToString("yyyyMMdd");
                        agRestrictionClone.EndDate   = restriction.EndDate.HasValue
                            ? restriction.EndDate?.ToString("yyyyMMdd")
                            : runEndDate.AddYears(10).ToString("yyyyMMdd");
                        agRestrictionClone.IndexType              = restriction.IndexType;
                        agRestrictionClone.IndexThreshold         = restriction.IndexThreshold;
                        agRestrictionClone.PublicHolidayIndicator =
                            AgConversions.ToAgIncludeExcludeEither(restriction.PublicHolidayIndicator);
                        agRestrictionClone.SchoolHolidayIndicator =
                            AgConversions.ToAgIncludeExcludeEither(restriction.SchoolHolidayIndicator);
                        agRestrictionClone.RestrictionType         = Convert.ToInt32(restriction.RestrictionType);
                        agRestrictionClone.RestrictionDays         = AgConversions.ToAgDaysAsInt(restriction.RestrictionDays);
                        agRestrictionClone.StartTime               = restriction.StartTime?.ToString("hhmmss") ?? "0";
                        agRestrictionClone.EndTime                 = restriction.EndTime?.ToString("hhmmss") ?? "995959";
                        agRestrictionClone.TimeToleranceMinsBefore = restriction.TimeToleranceMinsBefore;
                        agRestrictionClone.TimeToleranceMinsAfter  = restriction.TimeToleranceMinsAfter;
                        agRestrictionClone.ProgClassCode           = restriction.ProgrammeClassification ?? "";
                        agRestrictionClone.ProgClassFlag           = restriction.ProgrammeClassificationIndicator.ToString();
                        agRestrictionClone.LiveBroadcastFlag       = restriction.LiveProgrammeIndicator.ToString();
                        agRestrictionClone.EpisodeNo               = restriction.EpisodeNumber;

                        agRestrictions.Add(agRestrictionClone);

                        if (agRestrictionClone.ProgCategoryNo != 0)
                        {
                            var clashCode = restriction.ClashCode ?? "";

                            AddRestrictionForParentProgrammeCategory(
                                programmeDictionaryExternalRefsIndex,
                                runEndDate,
                                agRestriction,
                                restriction,
                                salesArea,
                                agRestrictions,
                                clashCode,
                                programmeCategoryExternalRefsIndex,
                                programmeCategoryIdsIndex,
                                agRestrictionClone.ProgCategoryNo
                                );
                        }
                    }
                }
            }

            return(agRestrictions);
        }