Example #1
0
        /// <summary>
        /// Reads the specified GUID.
        /// </summary>
        /// <param name="guid">The GUID.</param>
        /// <returns></returns>
        public static FieldTypeCache Read(Guid guid)
        {
            ObjectCache cache    = MemoryCache.Default;
            object      cacheObj = cache[guid.ToString()];

            if (cacheObj != null)
            {
                return(Read((int)cacheObj));
            }
            else
            {
                var fieldTypeService = new FieldTypeService();
                var fieldTypeModel   = fieldTypeService.Get(guid);
                if (fieldTypeModel != null)
                {
                    fieldTypeModel.LoadAttributes();
                    var fieldType = new FieldTypeCache(fieldTypeModel);

                    var cachePolicy = new CacheItemPolicy();
                    cache.Set(FieldTypeCache.CacheKey(fieldType.Id), fieldType, cachePolicy);
                    cache.Set(fieldType.Guid.ToString(), fieldType.Id, cachePolicy);

                    return(fieldType);
                }
                else
                {
                    return(null);
                }
            }
        }
Example #2
0
        /// <summary>
        /// Returns FieldType object from cache.  If fieldType does not already exist in cache, it
        /// will be read and added to cache
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static FieldTypeCache Read(int id)
        {
            string cacheKey = FieldTypeCache.CacheKey(id);

            ObjectCache    cache     = MemoryCache.Default;
            FieldTypeCache fieldType = cache[cacheKey] as FieldTypeCache;

            if (fieldType != null)
            {
                return(fieldType);
            }
            else
            {
                var fieldTypeService = new FieldTypeService();
                var fieldTypeModel   = fieldTypeService.Get(id);
                if (fieldTypeModel != null)
                {
                    fieldTypeModel.LoadAttributes();
                    fieldType = new FieldTypeCache(fieldTypeModel);

                    var cachePolicy = new CacheItemPolicy();
                    cache.Set(cacheKey, fieldType, cachePolicy);
                    cache.Set(fieldType.Guid.ToString(), fieldType.Id, cachePolicy);

                    return(fieldType);
                }
                else
                {
                    return(null);
                }
            }
        }
Example #3
0
        /// <summary>
        /// Returns FieldType object from cache.  If fieldType does not already exist in cache, it
        /// will be read and added to cache
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static FieldTypeCache Read(int id)
        {
            string cacheKey = FieldTypeCache.CacheKey(id);

            ObjectCache    cache     = MemoryCache.Default;
            FieldTypeCache fieldType = cache[cacheKey] as FieldTypeCache;

            if (fieldType != null)
            {
                return(fieldType);
            }
            else
            {
                Rock.Model.FieldTypeService fieldTypeService = new Rock.Model.FieldTypeService();
                Rock.Model.FieldType        fieldTypeModel   = fieldTypeService.Get(id);
                if (fieldTypeModel != null)
                {
                    fieldType = CopyModel(fieldTypeModel);

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

                    return(fieldType);
                }
                else
                {
                    return(null);
                }
            }
        }
Example #4
0
        /// <summary>
        /// Copies the model.
        /// </summary>
        /// <param name="fieldTypeModel">The field type model.</param>
        /// <returns></returns>
        public static FieldTypeCache CopyModel( Rock.Model.FieldType fieldTypeModel )
        {
            FieldTypeCache fieldType = new FieldTypeCache( fieldTypeModel );
            fieldType.Field = Rock.Field.Helper.InstantiateFieldType( fieldType.Assembly, fieldType.Class );

            return fieldType;
        }
Example #5
0
        /// <summary>
        /// Copies the model.
        /// </summary>
        /// <param name="fieldTypeModel">The field type model.</param>
        /// <returns></returns>
        public static FieldTypeCache CopyModel(Rock.Model.FieldType fieldTypeModel)
        {
            FieldTypeCache fieldType = new FieldTypeCache(fieldTypeModel);

            fieldType.Field = Rock.Field.Helper.InstantiateFieldType(fieldType.Assembly, fieldType.Class);

            return(fieldType);
        }
        /// <summary>
        /// Sets the value.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        /// <param name="currentPersonId">The current person id.</param>
        /// <param name="saveValue">if set to <c>true</c> [save value].</param>
        public void SetValue(string key, string value, int?currentPersonId, bool saveValue)
        {
            if (saveValue)
            {
                using (new Rock.Data.UnitOfWorkScope())
                {
                    // Save new value
                    var attributeValueService = new AttributeValueService();
                    var attributeValue        = attributeValueService.GetGlobalAttributeValue(key);

                    if (attributeValue == null)
                    {
                        var attributeService = new AttributeService();
                        var attribute        = attributeService.GetGlobalAttribute(key);
                        if (attribute == null)
                        {
                            attribute             = new Rock.Model.Attribute();
                            attribute.FieldTypeId = FieldTypeCache.Read(new Guid(SystemGuid.FieldType.TEXT)).Id;
                            attribute.EntityTypeQualifierColumn = string.Empty;
                            attribute.EntityTypeQualifierValue  = string.Empty;
                            attribute.Key  = key;
                            attribute.Name = key.SplitCase();
                            attributeService.Add(attribute, currentPersonId);
                            attributeService.Save(attribute, currentPersonId);

                            Attributes.Add(AttributeCache.Read(attribute.Id));
                        }

                        attributeValue = new AttributeValue();
                        attributeValueService.Add(attributeValue, currentPersonId);
                        attributeValue.IsSystem    = false;
                        attributeValue.AttributeId = attribute.Id;

                        if (!AttributeValues.Keys.Contains(key))
                        {
                            AttributeValues.Add(key, new KeyValuePair <string, string>(attribute.Name, value));
                        }
                    }

                    attributeValue.Value = value;
                    attributeValueService.Save(attributeValue, currentPersonId);
                }
            }

            var attributeCache = Attributes.FirstOrDefault(a => a.Key.Equals(key, StringComparison.OrdinalIgnoreCase));

            if (attributeCache != null)   // (Should never be null)
            {
                if (AttributeValues.Keys.Contains(key))
                {
                    AttributeValues[key] = new KeyValuePair <string, string>(attributeCache.Name, value);
                }
                else
                {
                    AttributeValues.Add(key, new KeyValuePair <string, string>(attributeCache.Name, value));
                }
            }
        }
Example #7
0
        /// <summary>
        /// Sets the value.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        /// <param name="saveValue">if set to <c>true</c> [save value].</param>
        /// <param name="rockContext">The rock context.</param>
        public void SetValue(string key, string value, bool saveValue, RockContext rockContext)
        {
            if (saveValue)
            {
                // Save new value
                rockContext = rockContext ?? new RockContext();
                var attributeValueService = new AttributeValueService(rockContext);
                var attributeValue        = attributeValueService.GetGlobalAttributeValue(key);

                if (attributeValue == null)
                {
                    var attributeService = new AttributeService(rockContext);
                    var attribute        = attributeService.GetGlobalAttribute(key);
                    if (attribute == null)
                    {
                        attribute = new Model.Attribute
                        {
                            FieldTypeId = FieldTypeCache.Get(new Guid(SystemGuid.FieldType.TEXT)).Id,
                            EntityTypeQualifierColumn = string.Empty,
                            EntityTypeQualifierValue  = string.Empty,
                            Key  = key,
                            Name = key.SplitCase()
                        };
                        attributeService.Add(attribute);
                        rockContext.SaveChanges();
                    }

                    attributeValue = new AttributeValue
                    {
                        IsSystem    = false,
                        AttributeId = attribute.Id
                    };
                    attributeValueService.Add(attributeValue);
                }

                attributeValue.Value = value;
                rockContext.SaveChanges();
            }

            lock ( _obj )
            {
                _attributeIds = null;
            }

            AttributeValues.AddOrUpdate(key, value, (k, v) => value);

            var attributeCache = Attributes.FirstOrDefault(a => a.Key.Equals(key, StringComparison.OrdinalIgnoreCase));

            if (attributeCache != null)
            {
                value = attributeCache.FieldType.Field.FormatValue(null, value, attributeCache.QualifierValues, false);
            }
            AttributeValuesFormatted.AddOrUpdate(key, value, (k, v) => value);
        }
Example #8
0
        /// <summary>
        /// All the field types
        /// </summary>
        /// <returns></returns>
        public static List <FieldTypeCache> All()
        {
            var allfieldTypes = new List <FieldTypeCache>();
            var fieldTypeIds  = GetOrAddExisting("Rock:FieldType:All", () => LoadAll());

            if (fieldTypeIds != null)
            {
                foreach (int fieldTypeId in fieldTypeIds)
                {
                    allfieldTypes.Add(FieldTypeCache.Read(fieldTypeId));
                }
            }
            return(allfieldTypes);
        }
Example #9
0
        /// <summary>
        /// Reads the specified field type model.
        /// </summary>
        /// <param name="fieldTypeModel">The field type model.</param>
        /// <returns></returns>
        public static FieldTypeCache Read(Rock.Model.FieldType fieldTypeModel)
        {
            string cacheKey = FieldTypeCache.CacheKey(fieldTypeModel.Id);

            ObjectCache    cache     = MemoryCache.Default;
            FieldTypeCache fieldType = cache[cacheKey] as FieldTypeCache;

            if (fieldType != null)
            {
                return(fieldType);
            }
            else
            {
                fieldType = FieldTypeCache.CopyModel(fieldTypeModel);
                cache.Set(cacheKey, fieldType, new CacheItemPolicy());

                return(fieldType);
            }
        }
Example #10
0
        /// <summary>
        /// Reads the specified field type model.
        /// </summary>
        /// <param name="fieldTypeModel">The field type model.</param>
        /// <returns></returns>
        public static FieldTypeCache Read(FieldType fieldTypeModel)
        {
            string cacheKey = FieldTypeCache.CacheKey(fieldTypeModel.Id);

            ObjectCache    cache     = MemoryCache.Default;
            FieldTypeCache fieldType = cache[cacheKey] as FieldTypeCache;

            if (fieldType != null)
            {
                return(fieldType);
            }
            else
            {
                fieldType = new FieldTypeCache(fieldTypeModel);

                var cachePolicy = new CacheItemPolicy();
                cache.Set(cacheKey, fieldType, cachePolicy);
                cache.Set(fieldType.Guid.ToString(), fieldType.Id, cachePolicy);

                return(fieldType);
            }
        }
Example #11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EntityField" /> class.
 /// </summary>
 /// <param name="name">The name.</param>
 /// <param name="fieldKind">Kind of the field.</param>
 /// <param name="propertyType">Type of the property.</param>
 /// <param name="attributeGuid">The attribute unique identifier.</param>
 /// <param name="fieldType">Type of the field.</param>
 public EntityField( string name, FieldKind fieldKind, Type propertyType, Guid attributeGuid, FieldTypeCache fieldType )
     : this(name, fieldKind, propertyType, null, attributeGuid)
 {
     this.FieldType = fieldType;
 }
        private static void SaveAttributeValue( Rock.Model.Workflow workflow, string key, string value, 
            FieldTypeCache fieldType, RockContext rockContext, Dictionary<string, string> qualifiers = null )
        {
            if (workflow.Attributes.ContainsKey(key))
            {
                workflow.SetAttributeValue( key, value );
            }
            else
            {
                // If workflow attribute doesn't exist, create it
                // ( should only happen first time a background check is processed for given workflow type)
                var attribute = new Rock.Model.Attribute();
                attribute.EntityTypeId = workflow.WorkflowType.TypeId;
                attribute.EntityTypeQualifierColumn = "WorkflowTypeId";
                attribute.EntityTypeQualifierValue = workflow.WorkflowTypeId.ToString();
                attribute.Name = key.SplitCase();
                attribute.Key = key;
                attribute.FieldTypeId = fieldType.Id;

                if ( qualifiers != null )
                {
                    foreach( var keyVal in qualifiers )
                    {
                        var qualifier = new Rock.Model.AttributeQualifier();
                        qualifier.Key = keyVal.Key;
                        qualifier.Value = keyVal.Value;
                        attribute.AttributeQualifiers.Add( qualifier );
                    }
                }

                // Set the value for this action's instance to the current time
                var attributeValue = new Rock.Model.AttributeValue();
                attributeValue.Attribute = attribute;
                attributeValue.EntityId = workflow.Id;
                attributeValue.Value = value;
                new AttributeValueService( rockContext ).Add( attributeValue );
            }
        }
Example #13
0
        /// <summary>
        /// Removes fieldType from cache
        /// </summary>
        /// <param name="id"></param>
        public static void Flush(int id)
        {
            ObjectCache cache = MemoryCache.Default;

            cache.Remove(FieldTypeCache.CacheKey(id));
        }
Example #14
0
 /// <summary>
 /// Reads this instance.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 public static FieldTypeCache Read <T>() where T : Rock.Field.IFieldType
 {
     return(FieldTypeCache.All().FirstOrDefault(a => a.Class == typeof(T).FullName));
 }
Example #15
0
 /// <summary>
 /// Returns FieldType object from cache.  If fieldType does not already exist in cache, it
 /// will be read and added to cache
 /// </summary>
 /// <param name="id">The identifier.</param>
 /// <param name="rockContext">The rock context.</param>
 /// <returns></returns>
 public static FieldTypeCache Read(int id, RockContext rockContext = null)
 {
     return(GetOrAddExisting(FieldTypeCache.CacheKey(id),
                             () => LoadById(id, rockContext)));
 }
Example #16
0
 /// <summary>
 /// Removes fieldType from cache
 /// </summary>
 /// <param name="id"></param>
 public static void Flush(int id)
 {
     FlushCache(FieldTypeCache.CacheKey(id));
     FlushCache("Rock:FieldType:All");
 }
Example #17
0
 /// <summary>
 /// Reads the specified field type model.
 /// </summary>
 /// <param name="fieldTypeModel">The field type model.</param>
 /// <returns></returns>
 public static FieldTypeCache Read(FieldType fieldTypeModel)
 {
     return(GetOrAddExisting(FieldTypeCache.CacheKey(fieldTypeModel.Id),
                             () => LoadByModel(fieldTypeModel)));
 }