/// <summary> /// Handles the Delete event of the rGrid control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="RowEventArgs" /> instance containing the event data.</param> protected void rGrid_Delete(object sender, RowEventArgs e) { var rockContext = new RockContext(); var attributeService = new AttributeService(rockContext); var attribute = attributeService.Get(e.RowKeyId); if (attribute != null) { if ((!_entityTypeId.HasValue || _entityTypeId.Value == 0) && _entityQualifierColumn == string.Empty && _entityQualifierValue == string.Empty && (!_entityId.HasValue || _entityId.Value == 0) ) { GlobalAttributesCache.Remove(); } attributeService.Delete(attribute); rockContext.SaveChanges(); } BindGrid(); }
/// <summary> /// Updates any Cache Objects that are associated with this entity /// </summary> /// <param name="entityState">State of the entity.</param> /// <param name="dbContext">The database context.</param> public void UpdateCache(System.Data.Entity.EntityState entityState, Rock.Data.DbContext dbContext) { AttributeCache cacheAttribute = AttributeCache.Get(this.AttributeId, dbContext as RockContext); if (cacheAttribute == null) { return; } if (this.EntityId.HasValue && cacheAttribute.EntityTypeId.HasValue) { EntityTypeCache entityType = EntityTypeCache.Get(cacheAttribute.EntityTypeId.Value, dbContext as RockContext); if (entityType?.HasEntityCache() == true) { entityType.FlushCachedItem(this.EntityId.Value); } } if ((!cacheAttribute.EntityTypeId.HasValue || cacheAttribute.EntityTypeId.Value == 0) && string.IsNullOrEmpty(cacheAttribute.EntityTypeQualifierColumn) && string.IsNullOrEmpty(cacheAttribute.EntityTypeQualifierValue)) { // Update GlobalAttributes if one of the values changed GlobalAttributesCache.Remove(); } }
public BlockActionResult DeleteAttribute(Guid attributeGuid) { using (var rockContext = new RockContext()) { var attributeService = new AttributeService(rockContext); var attribute = attributeService.Get(attributeGuid); if (attribute == null) { return(ActionBadRequest("Attribute not found.")); } if (attribute.IsSystem) { return(ActionBadRequest("System attributes cannot be deleted.")); } if (!attribute.EntityTypeId.HasValue && attribute.EntityTypeQualifierColumn == string.Empty && attribute.EntityTypeQualifierValue == string.Empty && !GetEntityId().HasValue) { GlobalAttributesCache.Remove(); } attributeService.Delete(attribute); rockContext.SaveChanges(); return(ActionOk()); } }
/// <summary> /// Updates any Cache Objects that are associated with this entity /// </summary> /// <param name="entityState">State of the entity.</param> /// <param name="dbContext">The database context.</param> public void UpdateCache(EntityState entityState, Rock.Data.DbContext dbContext) { AttributeCache.UpdateCachedEntity(this.Id, entityState); AttributeCache.UpdateCacheEntityAttributes(this, entityState); int? entityTypeId; string entityTypeQualifierColumn; string entityTypeQualifierValue; if (entityState == EntityState.Deleted) { entityTypeId = originalEntityTypeId; entityTypeQualifierColumn = originalEntityTypeQualifierColumn; entityTypeQualifierValue = originalEntityTypeQualifierValue; } else { entityTypeId = this.EntityTypeId; entityTypeQualifierColumn = this.EntityTypeQualifierColumn; entityTypeQualifierValue = this.EntityTypeQualifierValue; } if ((!entityTypeId.HasValue || entityTypeId.Value == 0) && string.IsNullOrEmpty(entityTypeQualifierColumn) && string.IsNullOrEmpty(entityTypeQualifierValue)) { GlobalAttributesCache.Remove(); } if ((!entityTypeId.HasValue || entityTypeId.Value == 0) && entityTypeQualifierColumn == Attribute.SYSTEM_SETTING_QUALIFIER && string.IsNullOrEmpty(entityTypeQualifierValue)) { Rock.Web.SystemSettings.Remove(); } if (entityTypeId.HasValue) { if (entityTypeId == EntityTypeCache.GetId <Block>()) { // Update BlockTypes/Blocks that reference this attribute if (entityTypeQualifierColumn.Equals("BlockTypeId", StringComparison.OrdinalIgnoreCase)) { int?blockTypeId = entityTypeQualifierValue.AsIntegerOrNull(); if (blockTypeId.HasValue) { BlockTypeCache.FlushItem(blockTypeId.Value); foreach (var blockId in new BlockService(dbContext as RockContext).GetByBlockTypeId(blockTypeId.Value).Select(a => a.Id).ToList()) { BlockCache.FlushItem(blockId); } } } } else if (entityTypeId == EntityTypeCache.GetId <DefinedValue>()) { // Update DefinedTypes/DefinedValues that reference this attribute if (entityTypeQualifierColumn.Equals("DefinedTypeId", StringComparison.OrdinalIgnoreCase)) { int?definedTypeId = entityTypeQualifierValue.AsIntegerOrNull(); if (definedTypeId.HasValue) { DefinedTypeCache.FlushItem(definedTypeId.Value); foreach (var definedValueId in new DefinedValueService(dbContext as RockContext).GetByDefinedTypeId(definedTypeId.Value).Select(a => a.Id).ToList()) { DefinedValueCache.FlushItem(definedValueId); } } } } else if (entityTypeId == EntityTypeCache.GetId <WorkflowActivityType>()) { if (entityTypeQualifierColumn.Equals("ActivityTypeId", StringComparison.OrdinalIgnoreCase)) { int?activityTypeId = entityTypeQualifierValue.AsIntegerOrNull(); if (activityTypeId.HasValue) { WorkflowActivityTypeCache.FlushItem(activityTypeId.Value); } } } else if (entityTypeId == EntityTypeCache.GetId <GroupType>()) { if (entityTypeQualifierColumn.Equals("Id", StringComparison.OrdinalIgnoreCase)) { int?groupTypeId = entityTypeQualifierValue.AsIntegerOrNull(); if (groupTypeId.HasValue) { GroupTypeCache.FlushItem(groupTypeId.Value); } } else if (entityTypeQualifierColumn.Equals("GroupTypePurposeValueId", StringComparison.OrdinalIgnoreCase)) { int?groupTypePurposeValueId = entityTypeQualifierValue.AsIntegerOrNull(); if (groupTypePurposeValueId.HasValue) { foreach (var groupTypeId in GroupTypeCache.All().Where(a => a.GroupTypePurposeValueId == groupTypePurposeValueId.Value).Select(a => a.Id).ToList()) { GroupTypeCache.FlushItem(groupTypeId); } } } } else if (entityTypeId.HasValue) { // some other EntityType. If it the EntityType has a CacheItem associated with it, clear out all the CachedItems of that type to ensure they have a clean read of the Attributes that were Added, Changed or Removed EntityTypeCache entityType = EntityTypeCache.Get(entityTypeId.Value, dbContext as RockContext); if (entityType?.HasEntityCache() == true) { entityType.ClearCachedItems(); } } } }
public void Flush() { GlobalAttributesCache.Remove(); }