Esempio n. 1
0
        /// <summary>
        /// Uses reflection to find any <see cref="FieldAttribute" /> attributes for the specified type and will create and/or update
        /// a <see cref="Rock.Model.Attribute" /> record for each attribute defined.
        /// </summary>
        /// <param name="type">The type (should be a <see cref="IHasAttributes" /> object.</param>
        /// <param name="entityTypeId">The entity type id.</param>
        /// <param name="entityQualifierColumn">The entity qualifier column.</param>
        /// <param name="entityQualifierValue">The entity qualifier value.</param>
        /// <param name="currentPersonId">The current person id.</param>
        /// <returns></returns>
        public static bool UpdateAttributes(Type type, int?entityTypeId, string entityQualifierColumn, string entityQualifierValue, int?currentPersonId)
        {
            bool attributesUpdated = false;

            List <string> existingKeys = new List <string>();

            var blockProperties = new List <FieldAttribute>();

            // If a ContextAwareAttribute exists without an EntityType defined, add a property attribute to specify the type
            int properties = 0;

            foreach (var customAttribute in type.GetCustomAttributes(typeof(ContextAwareAttribute), true))
            {
                var contextAttribute = (ContextAwareAttribute)customAttribute;
                if (contextAttribute != null && contextAttribute.EntityType == null)
                {
                    string propertyKeyName = string.Format("ContextEntityType{0}", properties > 0 ? properties.ToString() : "");
                    properties++;

                    blockProperties.Add(new EntityTypeFieldAttribute("Entity Type", false, "The type of entity that will provide context for this block", false, "Context", 0, propertyKeyName));
                }
            }

            // Add any property attributes that were defined for the block
            foreach (var customAttribute in type.GetCustomAttributes(typeof(FieldAttribute), true))
            {
                blockProperties.Add((FieldAttribute)customAttribute);
            }

            // Create any attributes that need to be created
            var attributeService = new Model.AttributeService();

            if (blockProperties.Count > 0)
            {
                var attributeQualifierService = new Model.AttributeQualifierService();
                var fieldTypeService          = new Model.FieldTypeService();
                var categoryService           = new Model.CategoryService();

                foreach (var blockProperty in blockProperties)
                {
                    attributesUpdated = UpdateAttribute(attributeService, attributeQualifierService, fieldTypeService, categoryService,
                                                        blockProperty, entityTypeId, entityQualifierColumn, entityQualifierValue, currentPersonId) || attributesUpdated;
                    existingKeys.Add(blockProperty.Key);
                }
            }

            // Remove any old attributes
            foreach (var a in attributeService.Get(entityTypeId, entityQualifierColumn, entityQualifierValue).ToList())
            {
                if (!existingKeys.Contains(a.Key))
                {
                    attributeService.Delete(a, currentPersonId);
                    attributeService.Save(a, currentPersonId);
                }
            }

            return(attributesUpdated);
        }
Esempio n. 2
0
        /// <summary>
        /// Uses reflection to find any <see cref="FieldAttribute" /> attributes for the specified type and will create and/or update
        /// a <see cref="Rock.Model.Attribute" /> record for each attribute defined.
        /// </summary>
        /// <param name="type">The type (should be a <see cref="IHasAttributes" /> object.</param>
        /// <param name="entityTypeId">The entity type id.</param>
        /// <param name="entityQualifierColumn">The entity qualifier column.</param>
        /// <param name="entityQualifierValue">The entity qualifier value.</param>
        /// <param name="currentPersonId">The current person id.</param>
        /// <returns></returns>
        public static bool UpdateAttributes(Type type, int?entityTypeId, string entityQualifierColumn, string entityQualifierValue, int?currentPersonId)
        {
            bool attributesUpdated = false;

            List <string> existingKeys = new List <string>();

            var blockProperties = new List <FieldAttribute>();

            // If a ContextAwareAttribute exists without an EntityType defined, add a property attribute to specify the type
            int properties = 0;

            foreach (var customAttribute in type.GetCustomAttributes(typeof(ContextAwareAttribute), true))
            {
                var contextAttribute = (ContextAwareAttribute)customAttribute;
                if (String.IsNullOrWhiteSpace(contextAttribute.EntityType))
                {
                    string propertyKeyName = string.Format("ContextEntityType{0}", properties > 0 ? properties.ToString() : "");
                    properties++;

                    blockProperties.Add(new TextFieldAttribute(0, "Context Entity Type", propertyKeyName, "Filter", "Context Entity Type", false, ""));
                }
            }

            // Add any property attributes that were defined for the block
            foreach (var customAttribute in type.GetCustomAttributes(typeof(FieldAttribute), true))
            {
                blockProperties.Add((FieldAttribute)customAttribute);
            }

            // Create any attributes that need to be created
            foreach (var blockProperty in blockProperties)
            {
                attributesUpdated = UpdateAttribute(blockProperty, entityTypeId, entityQualifierColumn, entityQualifierValue, currentPersonId) || attributesUpdated;
                existingKeys.Add(blockProperty.Key);
            }

            // Remove any old attributes
            Model.AttributeService attributeService = new Model.AttributeService();
            foreach (var a in attributeService.Get(entityTypeId, entityQualifierColumn, entityQualifierValue).ToList())
            {
                if (!existingKeys.Contains(a.Key))
                {
                    attributeService.Delete(a, currentPersonId);
                    attributeService.Save(a, currentPersonId);
                }
            }

            return(attributesUpdated);
        }
Esempio n. 3
0
        /// <summary>
        /// Uses reflection to find any <see cref="FieldAttribute" /> attributes for the specified type and will create and/or update
        /// a <see cref="Rock.Model.Attribute" /> record for each attribute defined.
        /// </summary>
        /// <param name="type">The type (should be a <see cref="IHasAttributes" /> object.</param>
        /// <param name="entityTypeId">The entity type id.</param>
        /// <param name="entityQualifierColumn">The entity qualifier column.</param>
        /// <param name="entityQualifierValue">The entity qualifier value.</param>
        /// <param name="rockContext">The rock context.</param>
        /// <returns></returns>
        /// <remarks>
        /// If a rockContext value is included, this method will save any previous changes made to the context
        /// </remarks>
        public static bool UpdateAttributes( Type type, int? entityTypeId, string entityQualifierColumn, string entityQualifierValue, RockContext rockContext = null )
        {
            bool attributesUpdated = false;

            List<string> existingKeys = new List<string>();

            var blockProperties = new List<FieldAttribute>();

            // If a ContextAwareAttribute exists without an EntityType defined, add a property attribute to specify the type
            int properties = 0;
            foreach ( var customAttribute in type.GetCustomAttributes( typeof( ContextAwareAttribute ), true ) )
            {
                var contextAttribute = (ContextAwareAttribute)customAttribute;
                if ( contextAttribute != null && contextAttribute.EntityType == null )
                {
                    if ( contextAttribute.IsConfigurable )
                    {
                        string propertyKeyName = string.Format( "ContextEntityType{0}", properties > 0 ? properties.ToString() : "" );
                        properties++;

                        blockProperties.Add( new EntityTypeFieldAttribute( "Entity Type", false, "The type of entity that will provide context for this block", false, "Context", 0, propertyKeyName ) );
                    }
                }
            }

            // Add any property attributes that were defined for the block
            foreach ( var customAttribute in type.GetCustomAttributes( typeof( FieldAttribute ), true ) )
            {
                blockProperties.Add( (FieldAttribute)customAttribute );
            }

            rockContext = rockContext ?? new RockContext();

            // Create any attributes that need to be created
            foreach ( var blockProperty in blockProperties )
            {
                try
                {
                    attributesUpdated = UpdateAttribute( blockProperty, entityTypeId, entityQualifierColumn, entityQualifierValue, rockContext ) || attributesUpdated;
                    existingKeys.Add( blockProperty.Key );
                }
                catch ( Exception ex )
                {
                    ExceptionLogService.LogException( new Exception( string.Format( "Could not update a block attribute ( Entity Type Id: {0}; Property Name: {1} ). ", entityTypeId, blockProperty.Name ), ex ), null );
                }
            }

            // Remove any old attributes
            try
            {
                var attributeService = new Model.AttributeService( rockContext );
                foreach ( var a in attributeService.Get( entityTypeId, entityQualifierColumn, entityQualifierValue ).ToList() )
                {
                    if ( !existingKeys.Contains( a.Key ) )
                    {
                        attributeService.Delete( a );
                    }
                }
                rockContext.SaveChanges();
            }
            catch ( Exception ex )
            {
                ExceptionLogService.LogException( new Exception( "Could not delete one or more old attributes.", ex ), null );
            }

            return attributesUpdated;
        }
Esempio n. 4
0
        /// <summary>
        /// Uses reflection to find any <see cref="FieldAttribute" /> attributes for the specified type and will create and/or update
        /// a <see cref="Rock.Model.Attribute" /> record for each attribute defined.
        /// </summary>
        /// <param name="type">The type (should be a <see cref="IHasAttributes" /> object.</param>
        /// <param name="entityTypeId">The entity type id.</param>
        /// <param name="entityQualifierColumn">The entity qualifier column.</param>
        /// <param name="entityQualifierValue">The entity qualifier value.</param>
        /// <param name="rockContext">The rock context.</param>
        /// <returns></returns>
        /// <remarks>
        /// If a rockContext value is included, this method will save any previous changes made to the context
        /// </remarks>
        public static bool UpdateAttributes(Type type, int?entityTypeId, string entityQualifierColumn, string entityQualifierValue, RockContext rockContext = null)
        {
            bool attributesUpdated = false;

            List <string> existingKeys = new List <string>();

            var blockProperties = new List <FieldAttribute>();

            // If a ContextAwareAttribute exists without an EntityType defined, add a property attribute to specify the type
            int properties = 0;

            foreach (var customAttribute in type.GetCustomAttributes(typeof(ContextAwareAttribute), true))
            {
                var contextAttribute = (ContextAwareAttribute)customAttribute;
                if (contextAttribute != null && contextAttribute.EntityType == null)
                {
                    if (contextAttribute.IsConfigurable)
                    {
                        string propertyKeyName = string.Format("ContextEntityType{0}", properties > 0 ? properties.ToString() : "");
                        properties++;

                        blockProperties.Add(new EntityTypeFieldAttribute("Entity Type", false, "The type of entity that will provide context for this block", false, "Context", 0, propertyKeyName));
                    }
                }
            }

            // Add any property attributes that were defined for the block
            foreach (var customAttribute in type.GetCustomAttributes(typeof(FieldAttribute), true))
            {
                blockProperties.Add((FieldAttribute)customAttribute);
            }

            rockContext = rockContext ?? new RockContext();

            // Create any attributes that need to be created
            foreach (var blockProperty in blockProperties)
            {
                try
                {
                    attributesUpdated = UpdateAttribute(blockProperty, entityTypeId, entityQualifierColumn, entityQualifierValue, rockContext) || attributesUpdated;
                    existingKeys.Add(blockProperty.Key);
                }
                catch (Exception ex)
                {
                    ExceptionLogService.LogException(new Exception(string.Format("Could not update a block attribute ( Entity Type Id: {0}; Property Name: {1} ). ", entityTypeId, blockProperty.Name), ex), null);
                }
            }

            // Remove any old attributes
            try
            {
                var attributeService = new Model.AttributeService(rockContext);
                foreach (var a in attributeService.Get(entityTypeId, entityQualifierColumn, entityQualifierValue).ToList())
                {
                    if (!existingKeys.Contains(a.Key))
                    {
                        attributeService.Delete(a);
                    }
                }
                rockContext.SaveChanges();
            }
            catch (Exception ex)
            {
                ExceptionLogService.LogException(new Exception("Could not delete one or more old attributes.", ex), null);
            }

            return(attributesUpdated);
        }
Esempio n. 5
0
        /// <summary>
        /// Deletes a <see cref="Rock.Model.Person">Person's</see> user preference setting by key and SavesChanges()
        /// </summary>
        /// <param name="person">The <see cref="Rock.Model.Person"/> who the preference value belongs to.</param>
        /// <param name="key">A <see cref="System.String"/> representing the key (name) of the preference setting.</param>
        public static void DeleteUserPreference( Person person, string key )
        {
            int? personEntityTypeId = Rock.Web.Cache.EntityTypeCache.Read( Person.USER_VALUE_ENTITY ).Id;

            using ( var rockContext = new RockContext() )
            {
                var attributeService = new Model.AttributeService( rockContext );
                var attribute = attributeService.Get( personEntityTypeId, string.Empty, string.Empty, key );

                if ( attribute != null )
                {
                    var attributeValueService = new Model.AttributeValueService( rockContext );
                    var attributeValue = attributeValueService.GetByAttributeIdAndEntityId( attribute.Id, person.Id );
                    if ( attributeValue != null )
                    {
                        attributeValueService.Delete( attributeValue );
                    }

                    attributeService.Delete( attribute );
                    rockContext.SaveChanges();
                }
            }
        }