/// <summary>
/// Saves any attribute edits made to an attribute
/// </summary>
/// <param name="newAttribute">The new attribute.</param>
/// <param name="entityTypeId">The entity type identifier.</param>
/// <param name="entityTypeQualifierColumn">The entity type qualifier column.</param>
/// <param name="entityTypeQualifierValue">The entity type 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 Rock.Model.Attribute SaveAttributeEdits( Rock.Model.Attribute newAttribute, int? entityTypeId, string entityTypeQualifierColumn, string entityTypeQualifierValue, RockContext rockContext = null )
{
rockContext = rockContext ?? new RockContext();
var internalAttributeService = new AttributeService( rockContext );
var attributeQualifierService = new AttributeQualifierService( rockContext );
var categoryService = new CategoryService( rockContext );
// If attribute is not valid, return null
if (!newAttribute.IsValid)
{
return null;
}
// Create a attribute model that will be saved
Rock.Model.Attribute attribute = null;
// Check to see if this was an existing or new attribute
if (newAttribute.Id > 0)
{
// If editing an existing attribute, remove all the old qualifiers in case they were changed
foreach ( var oldQualifier in attributeQualifierService.GetByAttributeId( newAttribute.Id ).ToList() )
{
attributeQualifierService.Delete( oldQualifier );
}
rockContext.SaveChanges();
// Then re-load the existing attribute
attribute = internalAttributeService.Get( newAttribute.Id );
}
if ( attribute == null )
{
// If the attribute didn't exist, create it
attribute = new Rock.Model.Attribute();
internalAttributeService.Add( attribute );
}
else
{
// If it did exist, set the new attribute ID and GUID since we're copying all properties in the next step
newAttribute.Id = attribute.Id;
newAttribute.Guid = attribute.Guid;
}
// Copy all the properties from the new attribute to the attribute model
attribute.CopyPropertiesFrom( newAttribute );
// Add any qualifiers
foreach ( var qualifier in newAttribute.AttributeQualifiers )
{
attribute.AttributeQualifiers.Add( new AttributeQualifier { Key = qualifier.Key, Value = qualifier.Value, IsSystem = qualifier.IsSystem } );
}
// Add any categories
attribute.Categories.Clear();
foreach ( var category in newAttribute.Categories )
{
attribute.Categories.Add( categoryService.Get( category.Id ) );
}
attribute.EntityTypeId = entityTypeId;
attribute.EntityTypeQualifierColumn = entityTypeQualifierColumn;
attribute.EntityTypeQualifierValue = entityTypeQualifierValue;
rockContext.SaveChanges();
if ( attribute != null )
{
Rock.Web.Cache.AttributeCache.Flush( attribute.Id );
// If this is a global attribute, flush all global attributes
if ( !entityTypeId.HasValue && entityTypeQualifierColumn == string.Empty && entityTypeQualifierValue == string.Empty )
{
Rock.Web.Cache.GlobalAttributesCache.Flush();
}
}
return attribute;
}