コード例 #1
0
        /// <summary>
        /// Creates an <see cref="ExcludeMatchDateEntity"/> from an <see cref="Ical.Net.CalendarComponents.CalendarEvent"/> if plausibility criteria are met.
        /// The <see cref="ExcludeMatchDateEntity.DateFrom"/> and <see cref="ExcludeMatchDateEntity.DateTo"/> are in UTC.
        /// </summary>
        /// <param name="calendarEvent"></param>
        /// <param name="defaultTimeZoneId">The time zone to use, if the it is not included in the start and end date/time of the event.</param>
        /// <returns>Returns the <see cref="ExcludeMatchDateEntity"/> created from the <see cref="Ical.Net.CalendarComponents.CalendarEvent"/>.</returns>
        private static ExcludeMatchDateEntity CreateEntity(Ical.Net.CalendarComponents.CalendarEvent calendarEvent, string defaultTimeZoneId)
        {
            var excludeMatchDate = new ExcludeMatchDateEntity();
            
            if (calendarEvent.Start == null)
            {
                throw new ArgumentException($"Could not create {nameof(ExcludeMatchDateEntity)} from {nameof(Ical.Net.CalendarComponents.CalendarEvent)} Start={calendarEvent.Start}, End={calendarEvent.End}, Name={calendarEvent.Description}", nameof(calendarEvent));
            }

            calendarEvent.Start.TzId ??= defaultTimeZoneId;
            var start = calendarEvent.Start.AsUtc;

            DateTime end;
            if (calendarEvent.End != null)
            {
                calendarEvent.End.TzId ??= defaultTimeZoneId;
                end = calendarEvent.End.AsUtc;
            }
            else
            {
                end = start.Date;
            }

            // Swap if necessary
            if (start > end) (start, end) = (end, start);

            excludeMatchDate.DateFrom = start;
            excludeMatchDate.DateTo = end;
            excludeMatchDate.Reason = calendarEvent.Summary;

            return excludeMatchDate;
        }
コード例 #2
0
        internal void GenerateExcludedDates(string specialHolidaysXmlFile)
        {
            var allRoundLegs = (_matchPlanner.Tournament.Rounds.SelectMany(r => r.RoundLegs.Select(rl => rl))).ToList();
            var minDate      = allRoundLegs.Min(leg => leg.StartDateTime);
            var maxDate      = allRoundLegs.Min(leg => leg.EndDateTime);

            var minYear = minDate.Year;

            var holidays = new List <Axuno.Tools.GermanHoliday>();

            while (minYear <= maxDate.Year)
            {
                var hd = new Axuno.Tools.GermanHolidays(minYear);

                if (!string.IsNullOrEmpty(specialHolidaysXmlFile))
                {
                    hd.Load(specialHolidaysXmlFile);
                }

                holidays.AddRange(
                    hd.GetFiltered(
                        h =>
                        (h.PublicHolidayStateIds.Count == 0 ||
                         h.PublicHolidayStateIds.Contains(Axuno.Tools.GermanFederalStates.Id.Bayern)) &&
                        (h.Type == Axuno.Tools.GermanHolidays.Type.Public || h.Type == Axuno.Tools.GermanHolidays.Type.Custom ||
                         h.Type == Axuno.Tools.GermanHolidays.Type.School)));

                minYear++;
            }

            // remove existing excluded dates for the tournament
            var filter = new RelationPredicateBucket(ExcludeMatchDateFields.TournamentId == _matchPlanner.Tournament.Id);

            var adapter = _appDb.DbContext.GetNewAdapter();

            adapter.DeleteEntitiesDirectly(typeof(ExcludeMatchDateEntity), filter);

            foreach (var h in holidays)
            {
                // save holidays which are within the tournaments' legs
                if (h.Date >= minDate && h.Date <= maxDate)
                {
                    var excluded = new ExcludeMatchDateEntity();
                    excluded.TournamentId = _matchPlanner.Tournament.Id;
                    excluded.RoundId      = null;
                    excluded.TeamId       = null;
                    excluded.DateFrom     = excluded.DateTo = h.Date;
                    excluded.Reason       = h.Name;
                    excluded.CreatedOn    = DateTime.Now;
                    excluded.ModifiedOn   = excluded.CreatedOn;
                    adapter.SaveEntity(excluded);
                }
            }
        }