Beispiel #1
0
        /// <summary>
        /// Delete the old binary file for UPDATE and DELETE and make sure new binary files are not temporary
        /// </summary>
        /// <param name="dbContext">The database context.</param>
        /// <param name="entry">The entry.</param>
        protected void PreSaveBinaryFile(Rock.Data.DbContext dbContext, DbEntityEntry entry)
        {
            Guid?newBinaryFileGuid = null;
            Guid?oldBinaryFileGuid = null;

            if (entry.State == EntityState.Added || entry.State == EntityState.Modified)
            {
                newBinaryFileGuid = Value.AsGuidOrNull();
            }

            if (entry.State == EntityState.Modified || entry.State == EntityState.Deleted)
            {
                oldBinaryFileGuid = entry.OriginalValues["Value"]?.ToString().AsGuidOrNull();
            }

            if (oldBinaryFileGuid.HasValue)
            {
                if (!newBinaryFileGuid.HasValue || !newBinaryFileGuid.Value.Equals(oldBinaryFileGuid.Value))
                {
                    var transaction = new Rock.Transactions.DeleteAttributeBinaryFile(oldBinaryFileGuid.Value);
                    Rock.Transactions.RockQueue.TransactionQueue.Enqueue(transaction);
                }
            }

            if (newBinaryFileGuid.HasValue)
            {
                BinaryFileService binaryFileService = new BinaryFileService(( RockContext )dbContext);
                var binaryFile = binaryFileService.Get(newBinaryFileGuid.Value);
                if (binaryFile != null && binaryFile.IsTemporary)
                {
                    binaryFile.IsTemporary = false;
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Pres the save.
        /// </summary>
        /// <param name="dbContext">The database context.</param>
        /// <param name="entry">The entry.</param>
        public override void PreSaveChanges(Rock.Data.DbContext dbContext, System.Data.Entity.Infrastructure.DbEntityEntry entry)
        {
            var attributeCache = AttributeCache.Read(this.AttributeId);

            if (attributeCache != null)
            {
                // Check to see if this attribute value if for a Field or Image field type
                // ( we don't want BinaryFileFieldType as that type of attribute's file can be used by more than one attribute )
                var field = attributeCache.FieldType.Field;
                if (field != null && (
                        field is Rock.Field.Types.FileFieldType ||
                        field is Rock.Field.Types.ImageFieldType))
                {
                    Guid?newBinaryFileGuid = null;
                    Guid?oldBinaryFileGuid = null;

                    if (entry.State == System.Data.Entity.EntityState.Added ||
                        entry.State == System.Data.Entity.EntityState.Modified)
                    {
                        newBinaryFileGuid = Value.AsGuidOrNull();
                    }

                    if (entry.State == System.Data.Entity.EntityState.Modified ||
                        entry.State == System.Data.Entity.EntityState.Deleted)
                    {
                        if (entry.OriginalValues["Value"] != null)
                        {
                            oldBinaryFileGuid = entry.OriginalValues["Value"].ToString().AsGuidOrNull();
                        }
                    }

                    if (oldBinaryFileGuid.HasValue)
                    {
                        if (!newBinaryFileGuid.HasValue || !newBinaryFileGuid.Value.Equals(oldBinaryFileGuid.Value))
                        {
                            var transaction = new Rock.Transactions.DeleteAttributeBinaryFile(oldBinaryFileGuid.Value);
                            Rock.Transactions.RockQueue.TransactionQueue.Enqueue(transaction);
                        }
                    }

                    if (newBinaryFileGuid.HasValue)
                    {
                        BinaryFileService binaryFileService = new BinaryFileService((RockContext)dbContext);
                        var binaryFile = binaryFileService.Get(newBinaryFileGuid.Value);
                        if (binaryFile != null && binaryFile.IsTemporary)
                        {
                            binaryFile.IsTemporary = false;
                        }
                    }
                }
            }

            base.PreSaveChanges(dbContext, entry);
        }
Beispiel #3
0
        /// <summary>
        /// Pres the save.
        /// </summary>
        /// <param name="dbContext">The database context.</param>
        /// <param name="entry">The entry.</param>
        public override void PreSaveChanges(Rock.Data.DbContext dbContext, System.Data.Entity.Infrastructure.DbEntityEntry entry)
        {
            var attributeCache = AttributeCache.Get(this.AttributeId);

            if (attributeCache != null)
            {
                // Check to see if this attribute value if for a Field or Image field type
                // ( we don't want BinaryFileFieldType as that type of attribute's file can be used by more than one attribute )
                var field = attributeCache.FieldType.Field;
                if (field != null && (
                        field is Rock.Field.Types.FileFieldType ||
                        field is Rock.Field.Types.ImageFieldType))
                {
                    Guid?newBinaryFileGuid = null;
                    Guid?oldBinaryFileGuid = null;

                    if (entry.State == System.Data.Entity.EntityState.Added ||
                        entry.State == System.Data.Entity.EntityState.Modified)
                    {
                        newBinaryFileGuid = Value.AsGuidOrNull();
                    }

                    if (entry.State == System.Data.Entity.EntityState.Modified ||
                        entry.State == System.Data.Entity.EntityState.Deleted)
                    {
                        if (entry.OriginalValues["Value"] != null)
                        {
                            oldBinaryFileGuid = entry.OriginalValues["Value"].ToString().AsGuidOrNull();
                        }
                    }

                    if (oldBinaryFileGuid.HasValue)
                    {
                        if (!newBinaryFileGuid.HasValue || !newBinaryFileGuid.Value.Equals(oldBinaryFileGuid.Value))
                        {
                            var transaction = new Rock.Transactions.DeleteAttributeBinaryFile(oldBinaryFileGuid.Value);
                            Rock.Transactions.RockQueue.TransactionQueue.Enqueue(transaction);
                        }
                    }

                    if (newBinaryFileGuid.HasValue)
                    {
                        BinaryFileService binaryFileService = new BinaryFileService(( RockContext )dbContext);
                        var binaryFile = binaryFileService.Get(newBinaryFileGuid.Value);
                        if (binaryFile != null && binaryFile.IsTemporary)
                        {
                            binaryFile.IsTemporary = false;
                        }
                    }
                }

                // Check to see if this attribute is for a person or group, and if so, save to history table
                bool saveToHistoryTable = attributeCache.EntityTypeId.HasValue &&
                                          (attributeCache.EntityTypeId.Value == EntityTypeCache.Get(typeof(Person)).Id ||
                                           attributeCache.EntityTypeId.Value == EntityTypeCache.Get(typeof(Group)).Id);

                if (saveToHistoryTable || attributeCache.EnableHistory)
                {
                    string oldValue = string.Empty;
                    string newValue = string.Empty;

                    HistoryEntityTypeId = attributeCache.EntityTypeId.Value;
                    HistoryEntityId     = EntityId;

                    switch (entry.State)
                    {
                    case System.Data.Entity.EntityState.Added:
                    {
                        newValue = Value;
                        break;
                    }

                    case System.Data.Entity.EntityState.Modified:
                    {
                        oldValue = entry.OriginalValues["Value"].ToStringSafe();
                        newValue = Value;
                        break;
                    }

                    case System.Data.Entity.EntityState.Deleted:
                    {
                        HistoryEntityId = entry.OriginalValues["EntityId"].ToStringSafe().AsIntegerOrNull();
                        oldValue        = entry.OriginalValues["Value"].ToStringSafe();
                        return;
                    }
                    }

                    this.PostSaveAttributeValueHistoryCurrent = false;

                    if (oldValue != newValue)
                    {
                        var formattedOldValue = oldValue.IsNotNullOrWhiteSpace() ? attributeCache.FieldType.Field.FormatValue(null, oldValue, attributeCache.QualifierValues, true) : string.Empty;
                        var formattedNewValue = newValue.IsNotNullOrWhiteSpace() ? attributeCache.FieldType.Field.FormatValue(null, newValue, attributeCache.QualifierValues, true) : string.Empty;

                        if (saveToHistoryTable)
                        {
                            HistoryChanges = new History.HistoryChangeList();
                            History.EvaluateChange(HistoryChanges, attributeCache.Name, formattedOldValue, formattedNewValue);
                        }

                        if (attributeCache.EnableHistory)
                        {
                            // value changed and attribute.EnableHistory = true, so flag PostSaveAttributeValueHistoryCurrent
                            this.PostSaveAttributeValueHistoryCurrent = true;

                            var attributeValueHistoricalService = new AttributeValueHistoricalService(dbContext as RockContext);

                            if (this.Id > 0)
                            {
                                // this is an existing AttributeValue, so fetch the AttributeValue that is currently marked as CurrentRow for this attribute value (if it exists)
                                bool hasAttributeValueHistoricalCurrentRow = attributeValueHistoricalService.Queryable().Where(a => a.AttributeValueId == this.Id && a.CurrentRowIndicator == true).Any();

                                if (!hasAttributeValueHistoricalCurrentRow)
                                {
                                    // this is an existing AttributeValue but there isn't a CurrentRow AttributeValueHistorical for this AttributeValue yet, so create it off of the OriginalValues
                                    AttributeValueHistorical attributeValueHistoricalPreviousCurrentRow = new AttributeValueHistorical
                                    {
                                        AttributeValueId    = this.Id,
                                        Value               = oldValue,
                                        ValueFormatted      = formattedOldValue,
                                        ValueAsNumeric      = entry.OriginalValues["ValueAsNumeric"] as decimal?,
                                        ValueAsDateTime     = entry.OriginalValues["ValueAsDateTime"] as DateTime?,
                                        ValueAsBoolean      = entry.OriginalValues["ValueAsBoolean"] as bool?,
                                        ValueAsPersonId     = entry.OriginalValues["ValueAsPersonId"] as int?,
                                        EffectiveDateTime   = entry.OriginalValues["ModifiedDateTime"] as DateTime? ?? RockDateTime.Now,
                                        CurrentRowIndicator = true,
                                        ExpireDateTime      = HistoricalTracking.MaxExpireDateTime
                                    };

                                    attributeValueHistoricalService.Add(attributeValueHistoricalPreviousCurrentRow);
                                }
                            }
                        }
                    }
                }
            }

            base.PreSaveChanges(dbContext, entry);
        }
Beispiel #4
0
        /// <summary>
        /// Pres the save.
        /// </summary>
        /// <param name="dbContext">The database context.</param>
        /// <param name="entry">The entry.</param>
        public override void PreSaveChanges(Rock.Data.DbContext dbContext, System.Data.Entity.Infrastructure.DbEntityEntry entry)
        {
            var attributeCache = AttributeCache.Read(this.AttributeId);

            if (attributeCache != null)
            {
                // Check to see if this attribute value if for a Field or Image field type
                // ( we don't want BinaryFileFieldType as that type of attribute's file can be used by more than one attribute )
                var field = attributeCache.FieldType.Field;
                if (field != null && (
                        field is Rock.Field.Types.FileFieldType ||
                        field is Rock.Field.Types.ImageFieldType))
                {
                    Guid?newBinaryFileGuid = null;
                    Guid?oldBinaryFileGuid = null;

                    if (entry.State == System.Data.Entity.EntityState.Added ||
                        entry.State == System.Data.Entity.EntityState.Modified)
                    {
                        newBinaryFileGuid = Value.AsGuidOrNull();
                    }

                    if (entry.State == System.Data.Entity.EntityState.Modified ||
                        entry.State == System.Data.Entity.EntityState.Deleted)
                    {
                        if (entry.OriginalValues["Value"] != null)
                        {
                            oldBinaryFileGuid = entry.OriginalValues["Value"].ToString().AsGuidOrNull();
                        }
                    }

                    if (oldBinaryFileGuid.HasValue)
                    {
                        if (!newBinaryFileGuid.HasValue || !newBinaryFileGuid.Value.Equals(oldBinaryFileGuid.Value))
                        {
                            var transaction = new Rock.Transactions.DeleteAttributeBinaryFile(oldBinaryFileGuid.Value);
                            Rock.Transactions.RockQueue.TransactionQueue.Enqueue(transaction);
                        }
                    }

                    if (newBinaryFileGuid.HasValue)
                    {
                        BinaryFileService binaryFileService = new BinaryFileService((RockContext)dbContext);
                        var binaryFile = binaryFileService.Get(newBinaryFileGuid.Value);
                        if (binaryFile != null && binaryFile.IsTemporary)
                        {
                            binaryFile.IsTemporary = false;
                        }
                    }
                }

                // Check to see if this attribute is for a person or group, and if so, save history
                if (attributeCache.EntityTypeId.HasValue &&
                    (attributeCache.EntityTypeId.Value == EntityTypeCache.Read(typeof(Person)).Id ||
                     attributeCache.EntityTypeId.Value == EntityTypeCache.Read(typeof(Group)).Id))
                {
                    string oldValue = string.Empty;
                    string newValue = string.Empty;

                    HistoryEntityTypeId = attributeCache.EntityTypeId.Value;
                    HistoryEntityId     = EntityId;

                    switch (entry.State)
                    {
                    case System.Data.Entity.EntityState.Added:
                    {
                        newValue = Value;
                        break;
                    }

                    case System.Data.Entity.EntityState.Modified:
                    {
                        oldValue = entry.OriginalValues["Value"].ToStringSafe();
                        newValue = Value;
                        break;
                    }

                    case System.Data.Entity.EntityState.Deleted:
                    {
                        HistoryEntityId = entry.OriginalValues["EntityId"].ToStringSafe().AsIntegerOrNull();
                        oldValue        = entry.OriginalValues["Value"].ToStringSafe();
                        return;
                    }
                    }

                    if (oldValue != newValue)
                    {
                        oldValue = oldValue.IsNotNullOrWhitespace() ? attributeCache.FieldType.Field.FormatValue(null, oldValue, attributeCache.QualifierValues, true) : string.Empty;
                        newValue = newValue.IsNotNullOrWhitespace() ? attributeCache.FieldType.Field.FormatValue(null, newValue, attributeCache.QualifierValues, true) : string.Empty;

                        HistoryChanges = new List <string>();
                        History.EvaluateChange(HistoryChanges, attributeCache.Name, oldValue, newValue);
                    }
                }
            }

            base.PreSaveChanges(dbContext, entry);
        }