/// <summary> /// Deletes the indexed documents by calendar. /// </summary> /// <param name="calendarId">The calendar identifier.</param> public void DeleteIndexedDocumentsByCalendarId(int calendarId) { // Ensure provided calendar is indexable var calendar = EventCalendarCache.Get(calendarId); if (calendar == null || !calendar.IsIndexEnabled) { return; } // Get event items for this calendar that are ONLY on this calendar. // We don't want to delete items that are also on another calendar. var eventItems = new EventItemService(new RockContext()) .GetActiveItemsByCalendarId(calendarId) .Where(i => i.EventCalendarItems.Count() == 1) .Select(a => a.Id).ToList(); int eventItemEntityTypeId = EntityTypeCache.GetId <Rock.Model.EventItem>().Value; foreach (var eventItemId in eventItems) { var deleteEntityTypeIndexMsg = new DeleteEntityTypeIndex.Message { EntityTypeId = eventItemEntityTypeId, EntityId = eventItemId }; deleteEntityTypeIndexMsg.Send(); } }
/// <summary> /// Bulks the index documents by calendar. /// </summary> /// <param name="calendarId">The calendar identifier.</param> public void BulkIndexDocumentsByCalendar(int calendarId) { // Ensure provided calendar is indexable var calendar = EventCalendarCache.Get(calendarId); if (calendar == null || !calendar.IsIndexEnabled) { return; } var eventItems = new EventItemService(new RockContext()) .GetActiveItemsByCalendarId(calendarId) .Select(a => a.Id).ToList(); int eventItemEntityTypeId = EntityTypeCache.GetId <Rock.Model.EventItem>().Value; foreach (var eventItemId in eventItems) { var deleteEntityTypeIndexMsg = new DeleteEntityTypeIndex.Message { EntityTypeId = eventItemEntityTypeId, EntityId = eventItemId }; deleteEntityTypeIndexMsg.Send(); } }
/// <summary> /// Indexes the document. /// </summary> /// <param name="id">The identifier.</param> public void IndexDocument(int id) { var eventItemEntity = new EventItemService(new RockContext()).Get(id); // Check to ensure that the event item is on a calendar that is indexed if (eventItemEntity != null && eventItemEntity.EventCalendarItems.Any(c => c.EventCalendar.IsIndexEnabled)) { var indexItem = EventItemIndex.LoadByModel(eventItemEntity); IndexContainer.IndexDocument(indexItem); } }
/// <summary> /// Bulks the index documents. /// </summary> /// <exception cref="System.NotImplementedException"></exception> public void BulkIndexDocuments() { var indexableItems = new List <IndexModelBase>(); var eventItems = new EventItemService(new RockContext()) .GetIndexableActiveItems() .Include(i => i.EventItemAudiences) .Include(i => i.EventItemOccurrences) .Include(i => i.EventItemOccurrences.Select(s => s.Schedule)) .Include(i => i.EventCalendarItems.Select(c => c.EventCalendar)) .AsNoTracking() .ToList(); int recordCounter = 0; foreach (var eventItem in eventItems) { var indexableEventItem = EventItemIndex.LoadByModel(eventItem); if (indexableEventItem.IsNotNull()) { indexableItems.Add(indexableEventItem); } recordCounter++; if (recordCounter > 100) { IndexContainer.IndexDocuments(indexableItems); indexableItems = new List <IndexModelBase>(); recordCounter = 0; } } IndexContainer.IndexDocuments(indexableItems); }
/// <summary> /// Method that will be called on an entity immediately before the item is saved by context /// </summary> /// <param name="dbContext">The database context.</param> /// <param name="entry">The database entity entry.</param> public override void PreSaveChanges(Data.DbContext dbContext, DbEntityEntry entry) { // Set the NextDateTime field to the datetime of the next occurrence, or null if there is no future occurrence. if (entry.State == EntityState.Added || entry.State == EntityState.Modified) { var rockContext = ( RockContext )dbContext; DateTime?nextDate = null; // If the event has a schedule and both the schedule and the event are active, get the next occurrence date. if (ScheduleId != null) { if (Schedule == null) { var scheduleService = new ScheduleService(rockContext); Schedule = scheduleService.Get(ScheduleId.Value); } if (Schedule != null && Schedule.IsActive) { if (EventItem == null) { var eventService = new EventItemService(rockContext); EventItem = eventService.Get(EventItemId); } if (EventItem.IsActive) { nextDate = Schedule.GetNextStartDateTime(RockDateTime.Now); } } } entry.CurrentValues[nameof(NextStartDateTime)] = nextDate; } base.PreSaveChanges(dbContext, entry); }