Beispiel #1
0
        /// <summary>
        /// Posts the save changes.
        /// </summary>
        /// <param name="dbContext">The database context.</param>
        public override void PostSaveChanges(Data.DbContext dbContext)
        {
            var rockContext = dbContext as RockContext;

            if (this.PostSaveAttributeValueHistoryCurrent)
            {
                var attributeValueHistoricalService            = new AttributeValueHistoricalService(rockContext);
                var attributeValueHistoricalPreviousCurrentRow = attributeValueHistoricalService.Queryable().Where(a => a.AttributeValueId == this.Id && a.CurrentRowIndicator == true).FirstOrDefault();
                var saveChangesDateTime = RockDateTime.Now;

                if (attributeValueHistoricalPreviousCurrentRow != null)
                {
                    attributeValueHistoricalPreviousCurrentRow.CurrentRowIndicator = false;
                    attributeValueHistoricalPreviousCurrentRow.ExpireDateTime      = saveChangesDateTime;
                }

                var attributeValueHistoricalCurrent = AttributeValueHistorical.CreateCurrentRowFromAttributeValue(this, saveChangesDateTime);

                attributeValueHistoricalService.Add(attributeValueHistoricalCurrent);
                rockContext.SaveChanges();
            }

            // If this a Person Attribute, Update the ModifiedDateTime on the Person that this AttributeValue is associated with
            if (this.EntityId.HasValue && AttributeCache.Get(this.AttributeId)?.EntityTypeId == EntityTypeCache.Get <Rock.Model.Person>().Id)
            {
                var currentDateTime    = RockDateTime.Now;
                int personId           = this.EntityId.Value;
                var qryPersonsToUpdate = new PersonService(rockContext).Queryable(true, true).Where(a => a.Id == personId);
                rockContext.BulkUpdate(qryPersonsToUpdate, p => new Person {
                    ModifiedDateTime = currentDateTime, ModifiedByPersonAliasId = this.ModifiedByPersonAliasId
                });
            }

            base.PostSaveChanges(dbContext);
        }
Beispiel #2
0
        /// <summary>
        /// Posts the save changes.
        /// </summary>
        /// <param name="dbContext">The database context.</param>
        public override void PostSaveChanges(Data.DbContext dbContext)
        {
            int?historyEntityId = (HistoryEntityId.HasValue && HistoryEntityId.Value > 0) ? HistoryEntityId.Value : this.EntityId;
            var rockContext     = dbContext as RockContext;

            if (HistoryChanges != null && HistoryChanges.Any() && HistoryEntityTypeId.HasValue && historyEntityId.HasValue)
            {
                if (HistoryEntityTypeId.Value == EntityTypeCache.Get(typeof(Person)).Id)
                {
                    HistoryService.SaveChanges(rockContext, typeof(Person), Rock.SystemGuid.Category.HISTORY_PERSON_DEMOGRAPHIC_CHANGES.AsGuid(), historyEntityId.Value, HistoryChanges, string.Empty, typeof(Attribute), AttributeId, true, this.ModifiedByPersonAliasId, dbContext.SourceOfChange);
                }
                else
                {
                    HistoryService.SaveChanges(rockContext, typeof(Group), Rock.SystemGuid.Category.HISTORY_GROUP_CHANGES.AsGuid(), historyEntityId.Value, HistoryChanges, string.Empty, typeof(Attribute), AttributeId, true, this.ModifiedByPersonAliasId, dbContext.SourceOfChange);
                }
            }

            if (this.PostSaveAttributeValueHistoryCurrent)
            {
                var attributeValueHistoricalService            = new AttributeValueHistoricalService(rockContext);
                var attributeValueHistoricalPreviousCurrentRow = attributeValueHistoricalService.Queryable().Where(a => a.AttributeValueId == this.Id && a.CurrentRowIndicator == true).FirstOrDefault();
                var saveChangesDateTime = RockDateTime.Now;

                if (attributeValueHistoricalPreviousCurrentRow != null)
                {
                    attributeValueHistoricalPreviousCurrentRow.CurrentRowIndicator = false;
                    attributeValueHistoricalPreviousCurrentRow.ExpireDateTime      = saveChangesDateTime;
                }

                var attributeValueHistoricalCurrent = AttributeValueHistorical.CreateCurrentRowFromAttributeValue(this, saveChangesDateTime);

                attributeValueHistoricalService.Add(attributeValueHistoricalCurrent);
                rockContext.SaveChanges();
            }

            // If this a Person Attribute, Update the ModifiedDateTime on the Person that this AttributeValue is associated with
            if (this.EntityId.HasValue && AttributeCache.Get(this.AttributeId)?.EntityTypeId == EntityTypeCache.Get <Rock.Model.Person>().Id)
            {
                var currentDateTime    = RockDateTime.Now;
                int personId           = this.EntityId.Value;
                var qryPersonsToUpdate = new PersonService(rockContext).Queryable(true, true).Where(a => a.Id == personId);
                rockContext.BulkUpdate(qryPersonsToUpdate, p => new Person {
                    ModifiedDateTime = currentDateTime, ModifiedByPersonAliasId = this.ModifiedByPersonAliasId
                });
            }

            base.PostSaveChanges(dbContext);
        }
Beispiel #3
0
        /// <summary>
        /// Posts the save changes.
        /// </summary>
        /// <param name="dbContext">The database context.</param>
        public override void PostSaveChanges(Data.DbContext dbContext)
        {
            var rockContext = dbContext as RockContext;

            if (this.PostSaveAttributeValueHistoryCurrent)
            {
                var attributeValueHistoricalService            = new AttributeValueHistoricalService(rockContext);
                var attributeValueHistoricalPreviousCurrentRow = attributeValueHistoricalService.Queryable().Where(a => a.AttributeValueId == this.Id && a.CurrentRowIndicator == true).FirstOrDefault();
                var saveChangesDateTime = RockDateTime.Now;

                if (attributeValueHistoricalPreviousCurrentRow != null)
                {
                    attributeValueHistoricalPreviousCurrentRow.CurrentRowIndicator = false;
                    attributeValueHistoricalPreviousCurrentRow.ExpireDateTime      = saveChangesDateTime;
                }

                var attributeValueHistoricalCurrent = AttributeValueHistorical.CreateCurrentRowFromAttributeValue(this, saveChangesDateTime);

                attributeValueHistoricalService.Add(attributeValueHistoricalCurrent);
                rockContext.SaveChanges();
            }

            // If this a Person Attribute, Update the ModifiedDateTime on the Person that this AttributeValue is associated with.
            // For example, if the FavoriteColor attribute of Ted Decker is changed from Red to Blue, we'll update Ted's Person.ModifiedDateTime.
            if (this.EntityId.HasValue && AttributeCache.Get(this.AttributeId)?.EntityTypeId == EntityTypeCache.Get <Rock.Model.Person>().Id)
            {
                // since this could get called several times (one for each of changed Attributes on a person), do a direct SQL to minimize overhead
                var currentDateTime = RockDateTime.Now;
                int personId        = this.EntityId.Value;
                if (this.ModifiedByPersonAliasId.HasValue)
                {
                    rockContext.Database.ExecuteSqlCommand(
                        $"UPDATE [Person] SET [ModifiedDateTime] = @modifiedDateTime, [ModifiedByPersonAliasId] = @modifiedByPersonAliasId WHERE [Id] = @personId",
                        new System.Data.SqlClient.SqlParameter("@modifiedDateTime", currentDateTime),
                        new System.Data.SqlClient.SqlParameter("@modifiedByPersonAliasId", this.ModifiedByPersonAliasId.Value),
                        new System.Data.SqlClient.SqlParameter("@personId", personId));
                }
                else
                {
                    rockContext.Database.ExecuteSqlCommand(
                        $"UPDATE [Person] SET [ModifiedDateTime] = @modifiedDateTime, [ModifiedByPersonAliasId] = NULL WHERE [Id] = @personId",
                        new System.Data.SqlClient.SqlParameter("@modifiedDateTime", currentDateTime),
                        new System.Data.SqlClient.SqlParameter("@personId", personId));
                }
            }

            base.PostSaveChanges(dbContext);
        }