private string GetValue( string key, AttributeCache attributeCache, RockContext rockContext ) { var attributeValue = new AttributeValueService( rockContext ).GetByAttributeIdAndEntityId( attributeCache.Id, null ); var value = ( !string.IsNullOrEmpty( attributeValue?.Value ) ) ? attributeValue.Value : attributeCache.DefaultValue; AttributeValues.AddOrUpdate( key, value, ( k, v ) => value ); return value; }
/// <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); }