Example #1
0
        /// <summary>
        /// Adds the synced content channel item for the media element by
        /// the given identifier.
        /// </summary>
        /// <param name="mediaElement">The media element.</param>
        /// <param name="rockContext">The <see cref="RockContext"/> to perform the save in.</param>
        /// <remarks>Must be called on a MediaElement that has a valid <see cref="MediaElement.MediaFolder"/> property.</remarks>
        private static void AddSyncedContentChannelItem(MediaElement mediaElement, RockContext rockContext)
        {
            try
            {
                // Freak accident, either we are in a transaction or the
                // media element got deleted before we started executing.
                if (mediaElement == null || mediaElement.MediaFolder == null)
                {
                    return;
                }

                // No sync, just exit.
                if (!mediaElement.MediaFolder.IsContentChannelSyncEnabled)
                {
                    return;
                }

                var contentChannelTypeId = mediaElement.MediaFolder.ContentChannel?.ContentChannelTypeId;
                var contentChannelId     = mediaElement.MediaFolder.ContentChannelId;
                var attributeId          = mediaElement.MediaFolder.ContentChannelAttributeId;
                var status = mediaElement.MediaFolder.ContentChannelItemStatus;

                // Missing required information.
                if (!contentChannelTypeId.HasValue || !contentChannelId.HasValue || !attributeId.HasValue || !status.HasValue)
                {
                    return;
                }

                var attribute = AttributeCache.Get(attributeId.Value);

                // Shouldn't happen, but make sure we don't hit a NRE later.
                if (attribute == null)
                {
                    return;
                }

                var contentChannelItemService = new ContentChannelItemService(rockContext);
                var contentChannelItem        = new ContentChannelItem();
                contentChannelItemService.Add(contentChannelItem);

                contentChannelItem.ContentChannelId     = contentChannelId.Value;
                contentChannelItem.ContentChannelTypeId = contentChannelTypeId.Value;
                contentChannelItem.Title         = mediaElement.Name;
                contentChannelItem.Content       = mediaElement.Description;
                contentChannelItem.StartDateTime = mediaElement.SourceCreatedDateTime ?? mediaElement.CreatedDateTime ?? RockDateTime.Now;
                contentChannelItem.Status        = status.Value;

                // We want both the content channel item and it's attributes
                // to be saved either together or not at all.
                rockContext.WrapTransaction(() =>
                {
                    rockContext.SaveChanges();

                    // 2.5x faster than LoadAttributes/SaveAttributeValues.
                    Rock.Attribute.Helper.SaveAttributeValue(contentChannelItem, attribute, mediaElement.Guid.ToString(), rockContext);
                });
            }
            catch (Exception ex)
            {
                var exception = new Exception("Error while creating synced content channel item for media element.", ex);
                ExceptionLogService.LogException(exception);
            }
        }