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 TimetableAppointmentController(
     IOrchardServices services, 
     ITimetableService TimetableService, 
     ITimetableAppointmentService TimetableAppointmentService,
     IFeedManager feedManager,
     IShapeFactory shapeFactory,
     IAuthenticationService authService) {
     _services = services;
     _TimetableService = TimetableService;
     _TimetableAppointmentService = TimetableAppointmentService;
     _feedManager = feedManager;
     _authService = authService;
     T = NullLocalizer.Instance;
     Shape = shapeFactory;
 }
        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)));
        }
 public TimetableController(
     IOrchardServices services, 
     ITimetableService TimetableService,
     ITimetableAppointmentService TimetableAppointmentService,
     ITimetableSlugConstraint TimetableSlugConstraint,
     IFeedManager feedManager, 
     IShapeFactory shapeFactory,
     IWorkContextAccessor workContextAccessor,
     IEnumerable<IHomePageProvider> homePageProviders,
     ISiteService siteService) {
     _services = services;
     _TimetableService = TimetableService;
     _TimetableAppointmentService = TimetableAppointmentService;
     _TimetableSlugConstraint = TimetableSlugConstraint;
     _feedManager = feedManager;
     _workContextAccessor = workContextAccessor;
     _siteService = siteService;
     _routableHomePageProvider = homePageProviders.SingleOrDefault(p => p.GetProviderName() == RoutableHomePageProvider.Name);
     Logger = NullLogger.Instance;
     Shape = shapeFactory;
 }
 public TimetablePartArchiveHandler(IRepository<TimetablePartArchiveRecord> TimetableArchiveRepository, ITimetableAppointmentService TimetableAppointmentService) {
     OnPublished<TimetableAppointmentPart>((context, bp) => RecalculateTimetableArchive(TimetableArchiveRepository, TimetableAppointmentService, bp));
     OnUnpublished<TimetableAppointmentPart>((context, bp) => RecalculateTimetableArchive(TimetableArchiveRepository, TimetableAppointmentService, bp));
     OnRemoved<TimetableAppointmentPart>((context, bp) => RecalculateTimetableArchive(TimetableArchiveRepository, TimetableAppointmentService, bp));
 }