/// <summary> /// Called after the save operation has been executed /// </summary> /// <remarks> /// This method is only called if <see cref="M:Rock.Data.EntitySaveHook`1.PreSave" /> returns /// without error. /// </remarks> protected override void PostSave() { if (PreSaveState == EntityContextState.Added) { // We don't need to wait for this to complete. Task.Run(() => MediaElementService.TriggerPostSaveTasks(Entity.Id)); } base.PostSave(); }
/// <summary> /// Adds the missing synced content channel items for all <see cref="MediaElement"/> /// items in the folder. /// </summary> /// <param name="mediaFolderId">The media folder identifier.</param> public static void AddMissingSyncedContentChannelItems(int mediaFolderId) { List <int> mediaElementIds; using (var rockContext = new RockContext()) { var mediaFolder = new MediaFolderService(rockContext).Get(mediaFolderId); // If the folder wasn't found or syncing isn't enabled then exit early. if (mediaFolder == null || !mediaFolder.IsContentChannelSyncEnabled) { return; } var contentChannelId = mediaFolder.ContentChannelId; var attributeId = mediaFolder.ContentChannelAttributeId; // If we don't have required information then exit early. if (!contentChannelId.HasValue || !attributeId.HasValue) { return; } // Build a query of all attribute values for the given attribute // that are not null or empty. var attributeValueQuery = new AttributeValueService(rockContext).Queryable() .Where(a => a.AttributeId == attributeId.Value && !string.IsNullOrEmpty(a.Value)); // Build a query of all content channel items for the channel. var contentChannelItemQuery = new ContentChannelItemService(rockContext).Queryable() .Where(i => i.ContentChannelId == contentChannelId.Value); // Join the two so we end up just content channel items that // have a non-null and non-empty attribute value. Finally // select just the value so we get a list of MediaElement Guid // values that have already been synced. var syncedMediaElementValues = contentChannelItemQuery.Join(attributeValueQuery, i => i.Id, v => v.EntityId, (i, v) => v.Value); // Our final query is to get all MediaElements that do not // exist in the previous query. That gives us a final list // of items that need to be synced still. mediaElementIds = new MediaElementService(rockContext).Queryable() .Where(e => e.MediaFolderId == mediaFolderId && !syncedMediaElementValues.Contains(e.Guid.ToString())) .Select(e => e.Id) .ToList(); } // Add the content channel item for each media element. foreach (var mediaElementId in mediaElementIds) { MediaElementService.AddSyncedContentChannelItem(mediaElementId); } }
/// <summary> /// Adds the synced content channel item for the media element by /// the given identifier. /// </summary> /// <param name="mediaElementId">The media element identifier.</param> public static void AddSyncedContentChannelItem(int mediaElementId) { using (var rockContext = new RockContext()) { var mediaElement = new MediaElementService(rockContext).Queryable() .Include(m => m.MediaFolder) .Include(m => m.MediaFolder.ContentChannel) .AsNoTracking() .Where(m => m.Id == mediaElementId) .SingleOrDefault(); AddSyncedContentChannelItem(mediaElement, rockContext); } }
/// <summary> /// Triggers all the post-save tasks for a <see cref="MediaElement"/> /// that need to happen in a background task. This will load the /// <see cref="MediaElement"/> from the database to determine which /// specific tasks need to be executed. /// </summary> /// <param name="mediaElementId">The media element identifier.</param> internal static void TriggerPostSaveTasks(int mediaElementId) { using (var rockContext = new RockContext()) { var mediaElement = new MediaElementService(rockContext).Queryable() .Include(m => m.MediaFolder) .Include(m => m.MediaFolder.ContentChannel) .AsNoTracking() .Where(m => m.Id == mediaElementId) .SingleOrDefault(); AddSyncedContentChannelItem(mediaElement, rockContext); if (mediaElement.MediaFolder.WorkflowTypeId.HasValue) { mediaElement.LaunchWorkflow(mediaElement.MediaFolder.WorkflowTypeId, string.Empty, null, null); } } }