Launches a group member change workflow. Will also recalculate group member requirements when a member is added
Inheritance: ITransaction
Example #1
0
        /// <summary>
        /// Pres the save changes.
        /// </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 transaction = new Rock.Transactions.GroupMemberChangeTransaction(entry);

            Rock.Transactions.RockQueue.TransactionQueue.Enqueue(transaction);

            base.PreSaveChanges(dbContext, entry);
        }
Example #2
0
        /// <summary>
        /// Pres the save changes.
        /// </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 changeTransaction = new Rock.Transactions.GroupMemberChangeTransaction(entry);

            Rock.Transactions.RockQueue.TransactionQueue.Enqueue(changeTransaction);

            int?oldPersonId = null;
            int?newPersonId = null;

            int?oldGroupId = null;
            int?newGroupId = null;

            switch (entry.State)
            {
            case System.Data.Entity.EntityState.Added:
            {
                oldPersonId = null;
                newPersonId = PersonId;

                oldGroupId = null;
                newGroupId = GroupId;

                if (!this.DateTimeAdded.HasValue)
                {
                    this.DateTimeAdded = RockDateTime.Now;
                }

                break;
            }

            case System.Data.Entity.EntityState.Modified:
            {
                oldPersonId = entry.OriginalValues["PersonId"].ToStringSafe().AsIntegerOrNull();
                newPersonId = PersonId;

                oldGroupId = entry.OriginalValues["GroupId"].ToStringSafe().AsIntegerOrNull();
                newGroupId = GroupId;

                break;
            }

            case System.Data.Entity.EntityState.Deleted:
            {
                oldPersonId = entry.OriginalValues["PersonId"].ToStringSafe().AsIntegerOrNull();
                newPersonId = null;

                oldGroupId = entry.OriginalValues["GroupId"].ToStringSafe().AsIntegerOrNull();
                newGroupId = null;

                break;
            }
            }

            var rockContext = (RockContext)dbContext;

            Group group = this.Group;

            if (group == null)
            {
                group = new GroupService(rockContext).Get(this.GroupId);
            }

            if (group != null)
            {
                string oldGroupName = group.Name;
                if (oldGroupId.HasValue && oldGroupId.Value != (group.Id))
                {
                    var oldGroup = new GroupService(rockContext).Get(oldGroupId.Value);
                    if (oldGroup != null)
                    {
                        oldGroupName = oldGroup.Name;
                    }
                }

                HistoryChanges = new List <HistoryItem>();
                if (newPersonId.HasValue)
                {
                    HistoryChanges.Add(new HistoryItem()
                    {
                        PersonId = newPersonId.Value,
                        Caption  = group.Name,
                        GroupId  = group.Id,
                        Group    = group
                    });
                }

                if (oldPersonId.HasValue)
                {
                    HistoryChanges.Add(new HistoryItem()
                    {
                        PersonId = oldPersonId.Value,
                        Caption  = oldGroupName,
                        GroupId  = oldGroupId
                    });
                }

                if (newPersonId.HasValue && newGroupId.HasValue &&
                    (!oldPersonId.HasValue || oldPersonId.Value != newPersonId.Value || !oldGroupId.HasValue || oldGroupId.Value != newGroupId.Value))
                {
                    // New Person in group
                    var historyItem = HistoryChanges.First(h => h.PersonId == newPersonId.Value && h.GroupId == newGroupId.Value);

                    historyItem.PersonHistoryChangeList.AddChange(History.HistoryVerb.AddedToGroup, History.HistoryChangeType.Record, $"'{group.Name}' Group");
                    History.EvaluateChange(historyItem.PersonHistoryChangeList, $"{historyItem.Caption} Role", (int?)null, GroupRole, GroupRoleId, rockContext);
                    History.EvaluateChange(historyItem.PersonHistoryChangeList, $"{historyItem.Caption} Note", string.Empty, Note);
                    History.EvaluateChange(historyItem.PersonHistoryChangeList, $"{historyItem.Caption} Status", null, GroupMemberStatus);
                    History.EvaluateChange(historyItem.PersonHistoryChangeList, $"{historyItem.Caption} Guest Count", (int?)null, GuestCount);

                    var addedMemberPerson = this.Person ?? new PersonService(rockContext).Get(this.PersonId);

                    // add the Person's Name as ValueName and Caption (just in case the groupmember record is deleted later)
                    historyItem.GroupMemberHistoryChangeList.AddChange(History.HistoryVerb.AddedToGroup, History.HistoryChangeType.Record, $"{addedMemberPerson?.FullName}").SetCaption($"{addedMemberPerson?.FullName}");
                    History.EvaluateChange(historyItem.GroupMemberHistoryChangeList, $"Role", ( int? )null, GroupRole, GroupRoleId, rockContext);
                    History.EvaluateChange(historyItem.GroupMemberHistoryChangeList, $"Note", string.Empty, Note);
                    History.EvaluateChange(historyItem.GroupMemberHistoryChangeList, $"Status", null, GroupMemberStatus);
                    History.EvaluateChange(historyItem.GroupMemberHistoryChangeList, $"Guest Count", ( int? )null, GuestCount);
                }

                if (newPersonId.HasValue && oldPersonId.HasValue && oldPersonId.Value == newPersonId.Value &&
                    newGroupId.HasValue && oldGroupId.HasValue && oldGroupId.Value == newGroupId.Value)
                {
                    // Updated same person in group
                    var historyItem = HistoryChanges.First(h => h.PersonId == newPersonId.Value && h.GroupId == newGroupId.Value);
                    History.EvaluateChange(historyItem.PersonHistoryChangeList, $"{historyItem.Caption} Role", entry.OriginalValues["GroupRoleId"].ToStringSafe().AsIntegerOrNull(), GroupRole, GroupRoleId, rockContext);
                    History.EvaluateChange(historyItem.PersonHistoryChangeList, $"{historyItem.Caption} Note", entry.OriginalValues["Note"].ToStringSafe(), Note);
                    History.EvaluateChange(historyItem.PersonHistoryChangeList, $"{historyItem.Caption} Status", entry.OriginalValues["GroupMemberStatus"].ToStringSafe().ConvertToEnum <GroupMemberStatus>(), GroupMemberStatus);
                    History.EvaluateChange(historyItem.PersonHistoryChangeList, $"{historyItem.Caption} Guest Count", entry.OriginalValues["GuestCount"].ToStringSafe().AsIntegerOrNull(), GuestCount);
                    History.EvaluateChange(historyItem.PersonHistoryChangeList, $"{historyItem.Caption} Archived", entry.OriginalValues["IsArchived"].ToStringSafe().AsBoolean(), IsArchived);

                    // If the groupmember was Archived, make sure it is the first GroupMember History change (since they get summarized when doing a HistoryLog and Timeline
                    bool origIsArchived = entry.OriginalValues["IsArchived"].ToStringSafe().AsBoolean();

                    if (origIsArchived != IsArchived)
                    {
                        var memberPerson = this.Person ?? new PersonService(rockContext).Get(this.PersonId);
                        if (IsArchived == true)
                        {
                            // GroupMember changed to Archived
                            historyItem.GroupMemberHistoryChangeList.AddChange(History.HistoryVerb.RemovedFromGroup, History.HistoryChangeType.Record, $"{memberPerson?.FullName}").SetCaption($"{memberPerson?.FullName}");
                        }
                        else
                        {
                            // GroupMember changed to Not archived
                            historyItem.GroupMemberHistoryChangeList.AddChange(History.HistoryVerb.AddedToGroup, History.HistoryChangeType.Record, $"{memberPerson?.FullName}").SetCaption($"{memberPerson?.FullName}");
                        }
                    }

                    History.EvaluateChange(historyItem.GroupMemberHistoryChangeList, $"Role", entry.OriginalValues["GroupRoleId"].ToStringSafe().AsIntegerOrNull(), GroupRole, GroupRoleId, rockContext);
                    History.EvaluateChange(historyItem.GroupMemberHistoryChangeList, $"Note", entry.OriginalValues["Note"].ToStringSafe(), Note);
                    History.EvaluateChange(historyItem.GroupMemberHistoryChangeList, $"Status", entry.OriginalValues["GroupMemberStatus"].ToStringSafe().ConvertToEnum <GroupMemberStatus>(), GroupMemberStatus);
                    History.EvaluateChange(historyItem.GroupMemberHistoryChangeList, $"Guest Count", entry.OriginalValues["GuestCount"].ToStringSafe().AsIntegerOrNull(), GuestCount);
                }

                if (oldPersonId.HasValue && oldGroupId.HasValue &&
                    (!newPersonId.HasValue || newPersonId.Value != oldPersonId.Value || !newGroupId.HasValue || newGroupId.Value != oldGroupId.Value))
                {
                    // Removed a person/groupmember in group
                    var historyItem = HistoryChanges.First(h => h.PersonId == oldPersonId.Value && h.GroupId == oldGroupId.Value);

                    historyItem.PersonHistoryChangeList.AddChange(History.HistoryVerb.RemovedFromGroup, History.HistoryChangeType.Record, $"{oldGroupName} Group");

                    var deletedMemberPerson = this.Person ?? new PersonService(rockContext).Get(this.PersonId);

                    historyItem.GroupMemberHistoryChangeList.AddChange(History.HistoryVerb.RemovedFromGroup, History.HistoryChangeType.Record, $"{deletedMemberPerson?.FullName}").SetCaption($"{deletedMemberPerson?.FullName}");
                }

                // process universal search indexing if required
                var groupType = GroupTypeCache.Get(group.GroupTypeId);
                if (groupType != null && groupType.IsIndexEnabled)
                {
                    IndexEntityTransaction transaction = new IndexEntityTransaction();
                    transaction.EntityTypeId = groupType.Id;
                    transaction.EntityId     = group.Id;

                    RockQueue.TransactionQueue.Enqueue(transaction);
                }
            }

            base.PreSaveChanges(dbContext, entry);
        }
Example #3
0
        /// <summary>
        /// Pres the save changes.
        /// </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 changeTransaction = new Rock.Transactions.GroupMemberChangeTransaction(entry);

            Rock.Transactions.RockQueue.TransactionQueue.Enqueue(changeTransaction);

            int?oldPersonId = null;
            int?newPersonId = null;

            int?oldGroupId = null;
            int?newGroupId = null;

            switch (entry.State)
            {
            case System.Data.Entity.EntityState.Added:
            {
                oldPersonId = null;
                newPersonId = PersonId;

                oldGroupId = null;
                newGroupId = GroupId;

                if (!this.DateTimeAdded.HasValue)
                {
                    this.DateTimeAdded = RockDateTime.Now;
                }

                break;
            }

            case System.Data.Entity.EntityState.Modified:
            {
                oldPersonId = entry.OriginalValues["PersonId"].ToStringSafe().AsIntegerOrNull();
                newPersonId = PersonId;

                oldGroupId = entry.OriginalValues["GroupId"].ToStringSafe().AsIntegerOrNull();
                newGroupId = GroupId;

                break;
            }

            case System.Data.Entity.EntityState.Deleted:
            {
                oldPersonId = entry.OriginalValues["PersonId"].ToStringSafe().AsIntegerOrNull();
                newPersonId = null;

                oldGroupId = entry.OriginalValues["GroupId"].ToStringSafe().AsIntegerOrNull();
                newGroupId = null;

                break;
            }
            }

            var rockContext = (RockContext)dbContext;

            Group group = this.Group;

            if (group == null)
            {
                group = new GroupService(rockContext).Get(this.GroupId);
            }
            if (group != null)
            {
                string oldGroupName = group.Name;
                if (oldGroupId.HasValue && oldGroupId.Value != (group.Id))
                {
                    var oldGroup = new GroupService(rockContext).Get(oldGroupId.Value);
                    if (oldGroup != null)
                    {
                        oldGroupName = oldGroup.Name;
                    }
                }

                HistoryChanges = new List <HistoryItem>();
                if (newPersonId.HasValue)
                {
                    HistoryChanges.Add(new HistoryItem()
                    {
                        PersonId = newPersonId.Value,
                        Changes  = new List <string>(),
                        Caption  = group.Name,
                        GroupId  = group.Id
                    });
                }

                if (oldPersonId.HasValue)
                {
                    HistoryChanges.Add(new HistoryItem()
                    {
                        PersonId = oldPersonId.Value,
                        Changes  = new List <string>(),
                        Caption  = oldGroupName,
                        GroupId  = oldGroupId
                    });
                }

                if (newPersonId.HasValue && newGroupId.HasValue &&
                    (!oldPersonId.HasValue || oldPersonId.Value != newPersonId.Value || !oldGroupId.HasValue || oldGroupId.Value != newGroupId.Value))
                {
                    // New Person in group
                    var historyItem = HistoryChanges.First(h => h.PersonId == newPersonId.Value && h.GroupId == newGroupId.Value);
                    historyItem.Changes.Add($"Added to '{group.Name}' Group");
                    History.EvaluateChange(historyItem.Changes, $"{historyItem.Caption} Role", (int?)null, GroupRole, GroupRoleId, rockContext);
                    History.EvaluateChange(historyItem.Changes, $"{historyItem.Caption} Note", string.Empty, Note);
                    History.EvaluateChange(historyItem.Changes, $"{historyItem.Caption} Status", null, GroupMemberStatus);
                    History.EvaluateChange(historyItem.Changes, $"{historyItem.Caption} Guest Count", (int?)null, GuestCount);
                }

                if (newPersonId.HasValue && oldPersonId.HasValue && oldPersonId.Value == newPersonId.Value &&
                    newGroupId.HasValue && oldGroupId.HasValue && oldGroupId.Value == newGroupId.Value)
                {
                    // Updated same person in group
                    var historyItem = HistoryChanges.First(h => h.PersonId == newPersonId.Value && h.GroupId == newGroupId.Value);
                    History.EvaluateChange(historyItem.Changes, $"{historyItem.Caption} Role", entry.OriginalValues["GroupRoleId"].ToStringSafe().AsIntegerOrNull(), GroupRole, GroupRoleId, rockContext);
                    History.EvaluateChange(historyItem.Changes, $"{historyItem.Caption} Note", entry.OriginalValues["Note"].ToStringSafe(), Note);
                    History.EvaluateChange(historyItem.Changes, $"{historyItem.Caption} Status", entry.OriginalValues["GroupMemberStatus"].ToStringSafe().ConvertToEnum <GroupMemberStatus>(), GroupMemberStatus);
                    History.EvaluateChange(historyItem.Changes, $"{historyItem.Caption} Guest Count", entry.OriginalValues["GuestCount"].ToStringSafe().AsIntegerOrNull(), GuestCount);
                }

                if (oldPersonId.HasValue && oldGroupId.HasValue &&
                    (!newPersonId.HasValue || newPersonId.Value != oldPersonId.Value || !newGroupId.HasValue || newGroupId.Value != oldGroupId.Value))
                {
                    // Removed a person in group
                    var historyItem = HistoryChanges.First(h => h.PersonId == oldPersonId.Value && h.GroupId == oldGroupId.Value);
                    historyItem.Changes.Add($"Removed from '{oldGroupName}' Group");
                }

                // process universal search indexing if required
                var groupType = GroupTypeCache.Read(group.GroupTypeId);
                if (groupType != null && groupType.IsIndexEnabled)
                {
                    IndexEntityTransaction transaction = new IndexEntityTransaction();
                    transaction.EntityTypeId = groupType.Id;
                    transaction.EntityId     = group.Id;

                    RockQueue.TransactionQueue.Enqueue(transaction);
                }
            }

            base.PreSaveChanges(dbContext, entry);
        }