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); } } }
public IEnumerable <ExcludeMatchDateEntity> Import(string?specialHolidaysXmlFile, DateTimePeriod dateLimits, Predicate <Axuno.Tools.GermanHoliday> holidayFilter) { if (!(dateLimits.Start.HasValue && dateLimits.End.HasValue)) { throw new ArgumentException("Lower and upper date limits must be set.", nameof(dateLimits)); } _logger.LogTrace("Starting import of German holidays for period {0} to {1}", dateLimits.Start, dateLimits.End); var currentYear = ((DateTime)dateLimits.Start).Year; // stores all holidays within the dateLimits var holidays = new List <Axuno.Tools.GermanHoliday>(); while (currentYear <= ((DateTime)dateLimits.End).Year) { _logger.LogTrace("Processing year '{0}'", currentYear); var currentYearHolidays = new Axuno.Tools.GermanHolidays(currentYear); _logger.LogTrace("Generated {0} holidays.", currentYearHolidays.Count); // The holidays file must be imported **for each year** if (!string.IsNullOrEmpty(specialHolidaysXmlFile)) { currentYearHolidays.Load(specialHolidaysXmlFile); _logger.LogTrace("Holidays from file '{0}' loaded. Now counts {1} for year {2}", specialHolidaysXmlFile, currentYearHolidays.Count, currentYear); } holidays.AddRange(currentYearHolidays.GetFiltered(holidayFilter)); // Filter is now an argument to Import method: // filter by federal or Bavarian holidays, which are public, custom or school holidays /*holidays.AddRange( * currentYearHolidays.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))); */ currentYear++; } return(Map(holidays, dateLimits)); }