Esempio n. 1
0
        /// <summary>
        /// Gets the component.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="channel">The channel.</param>
        /// <param name="entityId">The entity identifier.</param>
        /// <param name="identifier">The identifier.</param>
        /// <returns></returns>
        private InteractionComponentCache GetComponent(RockContext rockContext, InteractionChannelCache channel, int?entityId, string identifier)
        {
            if (channel != null)
            {
                if (entityId.HasValue)
                {
                    // Find by the Entity Id
                    var component = channel.InteractionComponents.FirstOrDefault(c => c.EntityId.HasValue && c.EntityId.Value == entityId.Value);
                    if (component != null)
                    {
                        return(component);
                    }
                }

                if (identifier.IsNotNullOrWhitespace())
                {
                    // Find by Id
                    int?id = identifier.AsIntegerOrNull();
                    if (id.HasValue)
                    {
                        var component = InteractionComponentCache.Read(id.Value);
                        if (component != null && component.ChannelId == channel.Id)
                        {
                            return(component);
                        }
                    }

                    // Find by Guid
                    Guid?guid = identifier.AsGuidOrNull();
                    if (guid.HasValue)
                    {
                        var component = InteractionComponentCache.Read(guid.Value);
                        if (component != null && component.ChannelId == channel.Id)
                        {
                            return(component);
                        }
                    }

                    if (!id.HasValue && !guid.HasValue)
                    {
                        // Find by Name
                        var component = channel.InteractionComponents.FirstOrDefault(c => c.Name.Equals(identifier, StringComparison.OrdinalIgnoreCase));
                        if (component != null)
                        {
                            return(component);
                        }

                        // If still no match, and we have a name, create a new channel
                        using (var newRockContext = new RockContext())
                        {
                            var interactionComponent = new InteractionComponent();
                            interactionComponent.Name      = identifier;
                            interactionComponent.ChannelId = channel.Id;
                            new InteractionComponentService(newRockContext).Add(interactionComponent);
                            newRockContext.SaveChanges();

                            return(InteractionComponentCache.Read(interactionComponent.Id));
                        }
                    }
                }
            }

            return(null);
        }