private static void RecalculateTimetableArchive(IRepository<TimetablePartArchiveRecord> TimetableArchiveRepository, ITimetableAppointmentService TimetableAppointmentService, TimetableAppointmentPart TimetableAppointmentPart) {
            TimetableArchiveRepository.Flush();

            // remove all current Timetable archive records
            var TimetableArchiveRecords =
                from bar in TimetableArchiveRepository.Table
                where bar.TimetablePart == TimetableAppointmentPart.TimetablePart.Record
                select bar;
            TimetableArchiveRecords.ToList().ForEach(TimetableArchiveRepository.Delete);

            // get all Timetable appointments for the current Timetable
            var appointments = TimetableAppointmentService.Get(TimetableAppointmentPart.TimetablePart, VersionOptions.Published);

            // create a dictionary of all the year/month combinations and their count of appointments that are published in this Timetable
            var inMemoryTimetableArchives = new Dictionary<DateTime, int>(appointments.Count());
            foreach (var appointment in appointments) {
                if (!appointment.Has<CommonPart>())
                    continue;

                var commonPart = appointment.As<CommonPart>();
                var key = new DateTime(commonPart.PublishedUtc.Value.Year, commonPart.PublishedUtc.Value.Month, 1);

                if (inMemoryTimetableArchives.ContainsKey(key))
                    inMemoryTimetableArchives[key]++;
                else
                    inMemoryTimetableArchives[key] = 1;
            }

            // create the new Timetable archive records based on the in memory values
            foreach (KeyValuePair<DateTime, int> item in inMemoryTimetableArchives) {
                TimetableArchiveRepository.Create(new TimetablePartArchiveRecord {TimetablePart = TimetableAppointmentPart.TimetablePart.Record, Year = item.Key.Year, Month = item.Key.Month, AppointmentCount = item.Value});
            }
        }
        public TimetableAppointmentPartHandler(ITimetableService TimetableService, ITimetableAppointmentService TimetableAppointmentService, RequestContext requestContext) {
            _TimetableService = TimetableService;
            _TimetableAppointmentService = TimetableAppointmentService;

            OnGetDisplayShape<TimetableAppointmentPart>(SetModelProperties);
            OnGetEditorShape<TimetableAppointmentPart>(SetModelProperties);
            OnUpdateEditorShape<TimetableAppointmentPart>(SetModelProperties);

            OnCreated<TimetableAppointmentPart>((context, part) => UpdateTimetableAppointmentCount(part));
            OnPublished<TimetableAppointmentPart>((context, part) => UpdateTimetableAppointmentCount(part));
            OnUnpublished<TimetableAppointmentPart>((context, part) => UpdateTimetableAppointmentCount(part));
            OnVersioned<TimetableAppointmentPart>((context, part, newVersionPart) => UpdateTimetableAppointmentCount(newVersionPart));
            OnRemoved<TimetableAppointmentPart>((context, part) => UpdateTimetableAppointmentCount(part));

            OnRemoved<TimetablePart>(
                (context, b) =>
                TimetableAppointmentService.Get(context.ContentItem.As<TimetablePart>()).ToList().ForEach(
                    TimetableAppointment => context.ContentManager.Remove(TimetableAppointment.ContentItem)));
        }