Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ComponentDescription"/> class.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="service">The service.</param>
        public ComponentDescription(int id, Rock.Attribute.IHasAttributes service)
        {
            Id = id;

            Type type = service.GetType();

            Name = type.Name;

            // Look for a DescriptionAttribute on the class and if found, use its value for the description
            // property of this class
            var descAttributes = type.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);

            if (descAttributes != null)
            {
                foreach (System.ComponentModel.DescriptionAttribute descAttribute in descAttributes)
                {
                    Description = descAttribute.Description;
                }
            }

            // If the class has an PropertyAttribute with 'Active' as the key get it's value for the property
            // otherwise default to true
            if (service.AttributeValues.ContainsKey("Active"))
            {
                Active = bool.Parse(service.AttributeValues["Active"].Value[0].Value);
            }
            else
            {
                Active = true;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Loads the <see cref="P:IHasAttributes.Attributes"/> and <see cref="P:IHasAttributes.AttributeValues"/> of any <see cref="IHasAttributes"/> object
        /// </summary>
        /// <param name="item">The item.</param>
        public static void LoadAttributes(Rock.Attribute.IHasAttributes item)
        {
            var attributes      = new SortedDictionary <string, List <Web.Cache.Attribute> >();
            var attributeValues = new Dictionary <string, KeyValuePair <string, List <Rock.Core.DTO.AttributeValue> > >();

            Dictionary <string, PropertyInfo> properties = new Dictionary <string, PropertyInfo>();

            Type entityType = item.GetType();

            if (entityType.Namespace == "System.Data.Entity.DynamicProxies")
            {
                entityType = entityType.BaseType;
            }

            foreach (PropertyInfo propertyInfo in entityType.GetProperties())
            {
                properties.Add(propertyInfo.Name.ToLower(), propertyInfo);
            }

            Rock.Core.AttributeService attributeService = new Rock.Core.AttributeService();

            foreach (Rock.Core.Attribute attribute in attributeService.GetByEntity(entityType.FullName))
            {
                if (string.IsNullOrEmpty(attribute.EntityQualifierColumn) ||
                    (properties.ContainsKey(attribute.EntityQualifierColumn.ToLower()) &&
                     (string.IsNullOrEmpty(attribute.EntityQualifierValue) ||
                      properties[attribute.EntityQualifierColumn.ToLower()].GetValue(item, null).ToString() == attribute.EntityQualifierValue)))
                {
                    Rock.Web.Cache.Attribute cachedAttribute = Rock.Web.Cache.Attribute.Read(attribute);

                    if (!attributes.ContainsKey(cachedAttribute.Category))
                    {
                        attributes.Add(cachedAttribute.Category, new List <Web.Cache.Attribute>());
                    }

                    attributes[cachedAttribute.Category].Add(cachedAttribute);
                    attributeValues.Add(attribute.Key, new KeyValuePair <string, List <Rock.Core.DTO.AttributeValue> >(attribute.Name, attribute.GetValues(item.Id)));
                }
            }

            item.Attributes      = attributes;
            item.AttributeValues = attributeValues;
        }
Exemplo n.º 3
0
        public virtual HttpResponseMessage SetAttributeValue(int id, string attributeKey, string attributeValue)
        {
            T model;

            if (!Service.TryGet(id, out model))
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            CheckCanEdit(model);

            Rock.Attribute.IHasAttributes modelWithAttributes = model as Rock.Attribute.IHasAttributes;
            if (modelWithAttributes != null)
            {
                using (var rockContext = new RockContext())
                {
                    modelWithAttributes.LoadAttributes(rockContext);
                    Rock.Web.Cache.AttributeCache attributeCache = modelWithAttributes.Attributes.ContainsKey(attributeKey) ? modelWithAttributes.Attributes[attributeKey] : null;

                    if (attributeCache != null)
                    {
                        if (!attributeCache.IsAuthorized(Rock.Security.Authorization.EDIT, this.GetPerson()))
                        {
                            throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden)
                            {
                                ReasonPhrase = string.Format("Not authorized to edit {0} on {1}", modelWithAttributes.GetType().GetFriendlyTypeName(), attributeKey)
                            });
                        }

                        Rock.Attribute.Helper.SaveAttributeValue(modelWithAttributes, attributeCache, attributeValue, rockContext);
                        var response = ControllerContext.Request.CreateResponse(HttpStatusCode.Accepted, modelWithAttributes.Id);
                        return(response);
                    }
                    else
                    {
                        throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest)
                        {
                            ReasonPhrase = string.Format("{0} does not have a {1} attribute", modelWithAttributes.GetType().GetFriendlyTypeName(), attributeKey)
                        });
                    }
                }
            }
            else
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    ReasonPhrase = "specified item does not have attributes"
                });
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Loads the <see cref="P:IHasAttributes.Attributes" /> and <see cref="P:IHasAttributes.AttributeValues" /> of any <see cref="IHasAttributes" /> object
        /// </summary>
        /// <param name="entity">The item.</param>
        /// <param name="rockContext">The rock context.</param>
        public static void LoadAttributes(Rock.Attribute.IHasAttributes entity, RockContext rockContext)
        {
            if (entity != null)
            {
                Dictionary <string, PropertyInfo> properties = new Dictionary <string, PropertyInfo>();

                Type entityType = entity.GetType();
                if (entityType.Namespace == "System.Data.Entity.DynamicProxies")
                {
                    entityType = entityType.BaseType;
                }

                rockContext = rockContext ?? new RockContext();

                // Check for group type attributes
                var groupTypeIds = new List <int>();
                if (entity is GroupMember || entity is Group || entity is GroupType)
                {
                    // Can't use GroupTypeCache here since it loads attributes and would result in a recursive stack overflow situation
                    var       groupTypeService = new GroupTypeService(rockContext);
                    GroupType groupType        = null;

                    if (entity is GroupMember)
                    {
                        var group = ((GroupMember)entity).Group ?? new GroupService(rockContext)
                                    .Queryable().AsNoTracking().FirstOrDefault(g => g.Id == ((GroupMember)entity).GroupId);
                        if (group != null)
                        {
                            groupType = group.GroupType ?? groupTypeService
                                        .Queryable().AsNoTracking().FirstOrDefault(t => t.Id == group.GroupTypeId);
                        }
                    }
                    else if (entity is Group)
                    {
                        groupType = ((Group)entity).GroupType ?? groupTypeService
                                    .Queryable().AsNoTracking().FirstOrDefault(t => t.Id == ((Group)entity).GroupTypeId);
                    }
                    else
                    {
                        groupType = ((GroupType)entity);
                    }

                    while (groupType != null)
                    {
                        groupTypeIds.Insert(0, groupType.Id);

                        // Check for inherited group type id's
                        if (groupType.InheritedGroupTypeId.HasValue)
                        {
                            groupType = groupType.InheritedGroupType ?? groupTypeService
                                        .Queryable().AsNoTracking().FirstOrDefault(t => t.Id == (groupType.InheritedGroupTypeId ?? 0));
                        }
                        else
                        {
                            groupType = null;
                        }
                    }
                }

                foreach (PropertyInfo propertyInfo in entityType.GetProperties())
                {
                    properties.Add(propertyInfo.Name.ToLower(), propertyInfo);
                }

                Rock.Model.AttributeService      attributeService      = new Rock.Model.AttributeService(rockContext);
                Rock.Model.AttributeValueService attributeValueService = new Rock.Model.AttributeValueService(rockContext);

                var inheritedAttributes = new Dictionary <int, List <Rock.Web.Cache.AttributeCache> >();
                if (groupTypeIds.Any())
                {
                    groupTypeIds.ForEach(g => inheritedAttributes.Add(g, new List <Rock.Web.Cache.AttributeCache>()));
                }
                else
                {
                    inheritedAttributes.Add(0, new List <Rock.Web.Cache.AttributeCache>());
                }

                var attributes = new List <Rock.Web.Cache.AttributeCache>();

                // Get all the attributes that apply to this entity type and this entity's properties match any attribute qualifiers
                var entityTypeCache = Rock.Web.Cache.EntityTypeCache.Read(entityType);
                if (entityTypeCache != null)
                {
                    int entityTypeId = entityTypeCache.Id;
                    foreach (var attribute in attributeService.Queryable()
                             .AsNoTracking()
                             .Where(a => a.EntityTypeId == entityTypeCache.Id)
                             .Select(a => new
                    {
                        a.Id,
                        a.EntityTypeQualifierColumn,
                        a.EntityTypeQualifierValue
                    }
                                     ))
                    {
                        // group type ids exist (entity is either GroupMember, Group, or GroupType) and qualifier is for a group type id
                        if (groupTypeIds.Any() && (
                                (entity is GroupMember && string.Compare(attribute.EntityTypeQualifierColumn, "GroupTypeId", true) == 0) ||
                                (entity is Group && string.Compare(attribute.EntityTypeQualifierColumn, "GroupTypeId", true) == 0) ||
                                (entity is GroupType && string.Compare(attribute.EntityTypeQualifierColumn, "Id", true) == 0)))
                        {
                            int groupTypeIdValue = int.MinValue;
                            if (int.TryParse(attribute.EntityTypeQualifierValue, out groupTypeIdValue) && groupTypeIds.Contains(groupTypeIdValue))
                            {
                                inheritedAttributes[groupTypeIdValue].Add(Rock.Web.Cache.AttributeCache.Read(attribute.Id));
                            }
                        }

                        else if (string.IsNullOrEmpty(attribute.EntityTypeQualifierColumn) ||
                                 (properties.ContainsKey(attribute.EntityTypeQualifierColumn.ToLower()) &&
                                  (string.IsNullOrEmpty(attribute.EntityTypeQualifierValue) ||
                                   (properties[attribute.EntityTypeQualifierColumn.ToLower()].GetValue(entity, null) ?? "").ToString() == attribute.EntityTypeQualifierValue)))
                        {
                            attributes.Add(Rock.Web.Cache.AttributeCache.Read(attribute.Id));
                        }
                    }
                }

                var allAttributes = new List <Rock.Web.Cache.AttributeCache>();

                foreach (var attributeGroup in inheritedAttributes)
                {
                    foreach (var attribute in attributeGroup.Value)
                    {
                        allAttributes.Add(attribute);
                    }
                }
                foreach (var attribute in attributes)
                {
                    allAttributes.Add(attribute);
                }

                var attributeValues = new Dictionary <string, Rock.Model.AttributeValue>();

                if (allAttributes.Any())
                {
                    foreach (var attribute in allAttributes)
                    {
                        // Add a placeholder for this item's value for each attribute
                        attributeValues.Add(attribute.Key, null);
                    }

                    // If loading attributes for a saved item, read the item's value(s) for each attribute
                    if (!entityTypeCache.IsEntity || entity.Id != 0)
                    {
                        List <int> attributeIds = allAttributes.Select(a => a.Id).ToList();
                        foreach (var attributeValue in attributeValueService.Queryable().AsNoTracking()
                                 .Where(v => v.EntityId == entity.Id && attributeIds.Contains(v.AttributeId)))
                        {
                            var attributeKey = AttributeCache.Read(attributeValue.AttributeId).Key;
                            attributeValues[attributeKey] = attributeValue.Clone(false) as Rock.Model.AttributeValue;
                        }
                    }

                    // Look for any attributes that don't have a value and create a default value entry
                    foreach (var attribute in allAttributes)
                    {
                        if (attributeValues[attribute.Key] == null)
                        {
                            var attributeValue = new Rock.Model.AttributeValue();
                            attributeValue.AttributeId = attribute.Id;
                            if (entity.AttributeValueDefaults != null && entity.AttributeValueDefaults.ContainsKey(attribute.Name))
                            {
                                attributeValue.Value = entity.AttributeValueDefaults[attribute.Name];
                            }
                            else
                            {
                                attributeValue.Value = attribute.DefaultValue;
                            }
                            attributeValues[attribute.Key] = attributeValue;
                        }
                        else
                        {
                            if (!String.IsNullOrWhiteSpace(attribute.DefaultValue) &&
                                String.IsNullOrWhiteSpace(attributeValues[attribute.Key].Value))
                            {
                                attributeValues[attribute.Key].Value = attribute.DefaultValue;
                            }
                        }
                    }
                }

                entity.Attributes = new Dictionary <string, Web.Cache.AttributeCache>();
                allAttributes.ForEach(a => entity.Attributes.Add(a.Key, a));

                entity.AttributeValues = attributeValues;
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Loads the <see cref="P:IHasAttributes.Attributes"/> and <see cref="P:IHasAttributes.AttributeValues"/> of any <see cref="IHasAttributes"/> object
        /// </summary>
        /// <param name="entity">The item.</param>
        public static void LoadAttributes(Rock.Attribute.IHasAttributes entity)
        {
            var attributes = new Dictionary <int, Rock.Web.Cache.AttributeCache>();
            Dictionary <string, PropertyInfo> properties = new Dictionary <string, PropertyInfo>();

            Type entityType = entity.GetType();

            if (entityType.Namespace == "System.Data.Entity.DynamicProxies")
            {
                entityType = entityType.BaseType;
            }

            foreach (PropertyInfo propertyInfo in entityType.GetProperties())
            {
                properties.Add(propertyInfo.Name.ToLower(), propertyInfo);
            }

            Rock.Model.AttributeService      attributeService      = new Rock.Model.AttributeService();
            Rock.Model.AttributeValueService attributeValueService = new Rock.Model.AttributeValueService();

            // Get all the attributes that apply to this entity type and this entity's properties match any attribute qualifiers
            int?entityTypeId = Rock.Web.Cache.EntityTypeCache.Read(entityType.FullName).Id;

            foreach (Rock.Model.Attribute attribute in attributeService.GetByEntityTypeId(entityTypeId))
            {
                if (string.IsNullOrEmpty(attribute.EntityTypeQualifierColumn) ||
                    (properties.ContainsKey(attribute.EntityTypeQualifierColumn.ToLower()) &&
                     (string.IsNullOrEmpty(attribute.EntityTypeQualifierValue) ||
                      properties[attribute.EntityTypeQualifierColumn.ToLower()].GetValue(entity, null).ToString() == attribute.EntityTypeQualifierValue)))
                {
                    attributes.Add(attribute.Id, Rock.Web.Cache.AttributeCache.Read(attribute));
                }
            }

            var attributeCategories = new SortedDictionary <string, List <string> >();
            var attributeValues     = new Dictionary <string, List <Rock.Model.AttributeValueDto> >();

            foreach (var attribute in attributes)
            {
                // Categorize the attributes
                if (!attributeCategories.ContainsKey(attribute.Value.Category))
                {
                    attributeCategories.Add(attribute.Value.Category, new List <string>());
                }
                attributeCategories[attribute.Value.Category].Add(attribute.Value.Key);

                // Add a placeholder for this item's value for each attribute
                attributeValues.Add(attribute.Value.Key, new List <Rock.Model.AttributeValueDto>());
            }

            // Read this item's value(s) for each attribute
            foreach (dynamic item in attributeValueService.Queryable()
                     .Where(v => v.EntityId == entity.Id && attributes.Keys.Contains(v.AttributeId))
                     .Select(v => new
            {
                Value = v,
                Key = v.Attribute.Key
            }))
            {
                attributeValues[item.Key].Add(new Rock.Model.AttributeValueDto(item.Value));
            }

            // Look for any attributes that don't have a value and create a default value entry
            foreach (var attributeEntry in attributes)
            {
                if (attributeValues[attributeEntry.Value.Key].Count == 0)
                {
                    var attributeValue = new Rock.Model.AttributeValueDto();
                    attributeValue.AttributeId = attributeEntry.Value.Id;
                    attributeValue.Value       = attributeEntry.Value.DefaultValue;
                    attributeValues[attributeEntry.Value.Key].Add(attributeValue);
                }
                else
                {
                    if (!String.IsNullOrWhiteSpace(attributeEntry.Value.DefaultValue))
                    {
                        foreach (var value in attributeValues[attributeEntry.Value.Key])
                        {
                            if (String.IsNullOrWhiteSpace(value.Value))
                            {
                                value.Value = attributeEntry.Value.DefaultValue;
                            }
                        }
                    }
                }
            }

            entity.AttributeCategories = attributeCategories;

            entity.Attributes = new Dictionary <string, Web.Cache.AttributeCache>();
            attributes.Values.ToList().ForEach(a => entity.Attributes.Add(a.Key, a));

            entity.AttributeValues = attributeValues;
        }