Example #1
0
        /// <summary>
        /// Queues groups of this type to have their indexes updated
        /// </summary>
        /// <param name="groupTypeId">The group type identifier.</param>
        public void BulkIndexDocumentsByGroupType(int groupTypeId)
        {
            var groupIds = new GroupService(new RockContext()).Queryable()
                           .Where(i => i.GroupTypeId == groupTypeId)
                           .Select(a => a.Id).ToList();

            int groupEntityTypeId = EntityTypeCache.GetId <Rock.Model.Group>().Value;

            foreach (var groupId in groupIds)
            {
                var processEntityTypeIndexMsg = new ProcessEntityTypeIndex.Message
                {
                    EntityTypeId = groupEntityTypeId,
                    EntityId     = groupId
                };

                processEntityTypeIndexMsg.Send();
            }
        }
Example #2
0
        /// <summary>
        /// Queues ContentChannelItems of this ContentChannel to have their indexes updated
        /// </summary>
        /// <param name="contentChannelId">The content channel identifier.</param>
        public void BulkIndexDocumentsByContentChannel(int contentChannelId)
        {
            // return all approved content channel items that are in content channels that should be indexed
            var contentChannelItemIds = new ContentChannelItemService(new RockContext()).Queryable()
                                        .Where(i =>
                                               i.ContentChannelId == contentChannelId &&
                                               (i.ContentChannel.RequiresApproval == false || i.ContentChannel.ContentChannelType.DisableStatus || i.Status == ContentChannelItemStatus.Approved))
                                        .Select(a => a.Id).ToList();

            int contentChannelItemEntityTypeId = EntityTypeCache.GetId <Rock.Model.ContentChannelItem>().Value;

            foreach (var contentChannelItemId in contentChannelItemIds)
            {
                var processEntityTypeIndexMsg = new ProcessEntityTypeIndex.Message
                {
                    EntityTypeId = contentChannelItemEntityTypeId,
                    EntityId     = contentChannelItemId
                };

                processEntityTypeIndexMsg.Send();
            }
        }
Example #3
0
            /// <summary>
            /// Called before the save operation is executed.
            /// </summary>
            protected override void PreSave()
            {
                var    rockContext = ( RockContext )this.RockContext;
                string errorMessage;

                if (State != EntityContextState.Deleted &&
                    Entity.IsArchived == false &&
                    Entity.GroupMemberStatus != GroupMemberStatus.Inactive)
                {
                    if (!Entity.ValidateGroupMembership(rockContext, out errorMessage))
                    {
                        var ex = new GroupMemberValidationException(errorMessage);
                        ExceptionLogService.LogException(ex);
                        throw ex;
                    }
                }

                var updateGroupMemberMsg = new UpdateGroupMember.Message
                {
                    State             = State,
                    GroupId           = Entity.GroupId,
                    PersonId          = Entity.PersonId,
                    GroupMemberStatus = Entity.GroupMemberStatus,
                    GroupMemberRoleId = Entity.GroupRoleId,
                    IsArchived        = Entity.IsArchived
                };

                if (Entity.Group != null)
                {
                    updateGroupMemberMsg.GroupTypeId = Entity.Group.GroupTypeId;
                }

                // If this isn't a new group member, get the previous status and role values
                if (State == EntityContextState.Modified)
                {
                    updateGroupMemberMsg.PreviousGroupMemberStatus = ( GroupMemberStatus )OriginalValues[nameof(GroupMember.GroupMemberStatus)].ToStringSafe().ConvertToEnum <GroupMemberStatus>();
                    updateGroupMemberMsg.PreviousGroupMemberRoleId = OriginalValues[nameof(GroupMember.GroupRoleId)].ToStringSafe().AsInteger();
                    updateGroupMemberMsg.PreviousIsArchived        = OriginalValues[nameof(GroupMember.IsArchived)].ToStringSafe().AsBoolean();
                }

                // If this isn't a deleted group member, get the group member guid
                if (State != EntityContextState.Deleted)
                {
                    updateGroupMemberMsg.GroupMemberGuid = Entity.Guid;
                }

                updateGroupMemberMsg.Send();

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

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

                switch (State)
                {
                case EntityContextState.Added:
                {
                    oldPersonId = null;
                    newPersonId = Entity.PersonId;

                    oldGroupId = null;
                    newGroupId = Entity.GroupId;

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

                    // if this is a new record, but is saved with IsActive=False, set the InactiveDateTime if it isn't set already
                    if (Entity.GroupMemberStatus == GroupMemberStatus.Inactive)
                    {
                        Entity.InactiveDateTime = Entity.InactiveDateTime ?? RockDateTime.Now;
                    }

                    break;
                }

                case EntityContextState.Modified:
                {
                    oldPersonId = OriginalValues[nameof(GroupMember.PersonId)].ToStringSafe().AsIntegerOrNull();
                    newPersonId = Entity.PersonId;

                    oldGroupId = OriginalValues[nameof(GroupMember.GroupId)].ToStringSafe().AsIntegerOrNull();
                    newGroupId = Entity.GroupId;

                    var originalStatus = OriginalValues[nameof(GroupMember.GroupMemberStatus)].ToStringSafe().ConvertToEnum <GroupMemberStatus>();

                    // IsActive was modified, set the InactiveDateTime if it changed to Inactive, or set it to NULL if it changed to Active
                    if (originalStatus != Entity.GroupMemberStatus)
                    {
                        if (Entity.GroupMemberStatus == GroupMemberStatus.Inactive)
                        {
                            // if the caller didn't already set InactiveDateTime, set it to now
                            Entity.InactiveDateTime = Entity.InactiveDateTime ?? RockDateTime.Now;
                        }
                        else
                        {
                            Entity.InactiveDateTime = null;
                        }
                    }

                    break;
                }

                case EntityContextState.Deleted:
                {
                    oldPersonId = Entity.PersonId;
                    newPersonId = null;

                    oldGroupId = Entity.GroupId;
                    newGroupId = null;

                    break;
                }
                }

                Group group = Entity.Group;

                if (group == null)
                {
                    group = new GroupService(rockContext).Get(Entity.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, Entity.GroupRole, Entity.GroupRoleId, rockContext);
                        History.EvaluateChange(historyItem.PersonHistoryChangeList, $"{historyItem.Caption} Note", string.Empty, Entity.Note);
                        History.EvaluateChange(historyItem.PersonHistoryChangeList, $"{historyItem.Caption} Status", null, Entity.GroupMemberStatus);
                        History.EvaluateChange(historyItem.PersonHistoryChangeList, $"{historyItem.Caption} Communication Preference", null, Entity.CommunicationPreference);
                        History.EvaluateChange(historyItem.PersonHistoryChangeList, $"{historyItem.Caption} Guest Count", ( int? )null, Entity.GuestCount);

                        var addedMemberPerson = Entity.Person ?? new PersonService(rockContext).Get(Entity.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, Entity.GroupRole, Entity.GroupRoleId, rockContext);
                        History.EvaluateChange(historyItem.GroupMemberHistoryChangeList, $"Note", string.Empty, Entity.Note);
                        History.EvaluateChange(historyItem.GroupMemberHistoryChangeList, $"Status", null, Entity.GroupMemberStatus);
                        History.EvaluateChange(historyItem.GroupMemberHistoryChangeList, $"Communication Preference", null, Entity.CommunicationPreference);
                        History.EvaluateChange(historyItem.GroupMemberHistoryChangeList, $"Guest Count", ( int? )null, Entity.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", OriginalValues["GroupRoleId"].ToStringSafe().AsIntegerOrNull(), Entity.GroupRole, Entity.GroupRoleId, rockContext);
                        History.EvaluateChange(historyItem.PersonHistoryChangeList, $"{historyItem.Caption} Note", OriginalValues["Note"].ToStringSafe(), Entity.Note);
                        History.EvaluateChange(historyItem.PersonHistoryChangeList, $"{historyItem.Caption} Status", OriginalValues["GroupMemberStatus"].ToStringSafe().ConvertToEnum <GroupMemberStatus>(), Entity.GroupMemberStatus);
                        History.EvaluateChange(historyItem.PersonHistoryChangeList, $"{historyItem.Caption} Communication Preference", OriginalValues["CommunicationPreference"].ToStringSafe().ConvertToEnum <CommunicationType>(), Entity.CommunicationPreference);
                        History.EvaluateChange(historyItem.PersonHistoryChangeList, $"{historyItem.Caption} Guest Count", OriginalValues["GuestCount"].ToStringSafe().AsIntegerOrNull(), Entity.GuestCount);
                        History.EvaluateChange(historyItem.PersonHistoryChangeList, $"{historyItem.Caption} Archived", OriginalValues["IsArchived"].ToStringSafe().AsBoolean(), Entity.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 = OriginalValues[nameof(GroupMember.IsArchived)].ToStringSafe().AsBoolean();

                        if (origIsArchived != Entity.IsArchived)
                        {
                            var memberPerson = Entity.Person ?? new PersonService(rockContext).Get(Entity.PersonId);
                            if (Entity.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", OriginalValues[nameof(GroupMember.GroupRoleId)].ToStringSafe().AsIntegerOrNull(), Entity.GroupRole, Entity.GroupRoleId, rockContext);
                        History.EvaluateChange(historyItem.GroupMemberHistoryChangeList, $"Note", OriginalValues[nameof(GroupMember.Note)].ToStringSafe(), Entity.Note);
                        History.EvaluateChange(historyItem.GroupMemberHistoryChangeList, $"Status", OriginalValues[nameof(GroupMember.GroupMemberStatus)].ToStringSafe().ConvertToEnum <GroupMemberStatus>(), Entity.GroupMemberStatus);
                        History.EvaluateChange(historyItem.GroupMemberHistoryChangeList, $"Communication Preference", OriginalValues[nameof(GroupMember.CommunicationPreference)].ToStringSafe().ConvertToEnum <CommunicationType>(), Entity.CommunicationPreference);
                        History.EvaluateChange(historyItem.GroupMemberHistoryChangeList, $"Guest Count", OriginalValues[nameof(GroupMember.GuestCount)].ToStringSafe().AsIntegerOrNull(), Entity.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 = Entity.Person ?? new PersonService(rockContext).Get(Entity.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)
                    {
                        var processEntityTypeIndexMsg = new ProcessEntityTypeIndex.Message
                        {
                            EntityTypeId = groupType.Id,
                            EntityId     = group.Id
                        };

                        processEntityTypeIndexMsg.Send();
                    }
                }

                _preSaveChangesOldGroupId = oldGroupId;

                base.PreSave();
            }