Esempio n. 1
0
        /// <summary>
        /// Returns Attribute object from cache.  If attribute does not already exist in cache, it
        /// will be read and added to cache
        /// </summary>
        /// <param name="id">The id of the Attribute to read</param>
        /// <returns></returns>
        public static AttributeCache Read(int id)
        {
            string cacheKey = AttributeCache.CacheKey(id);

            ObjectCache    cache     = MemoryCache.Default;
            AttributeCache attribute = cache[cacheKey] as AttributeCache;

            if (attribute != null)
            {
                return(attribute);
            }
            else
            {
                Rock.Model.AttributeService attributeService = new Rock.Model.AttributeService();
                Rock.Model.Attribute        attributeModel   = attributeService.Get(id);
                if (attributeModel != null)
                {
                    attribute = AttributeCache.CopyModel(attributeModel);

                    cache.Set(cacheKey, attribute, new CacheItemPolicy());

                    return(attribute);
                }
                else
                {
                    return(null);
                }
            }
        }
Esempio 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="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;
        }