Example #1
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 transaction = new IndexEntityTransaction {
                    EntityId = contentChannelItemId, EntityTypeId = contentChannelItemEntityTypeId
                };
                transaction.Enqueue();
            }
        }
Example #2
0
        /// <summary>
        /// Creates audit logs and/or triggers workflows for items that were changed
        /// </summary>
        /// <param name="updatedItems">The updated items.</param>
        /// <param name="personAlias">The person alias.</param>
        /// <param name="enableAuditing">if set to <c>true</c> [enable auditing].</param>
        protected virtual void RockPostSave(List <ContextItem> updatedItems, PersonAlias personAlias, bool enableAuditing = false)
        {
            if (enableAuditing)
            {
                try
                {
                    var audits = updatedItems.Select(i => i.Audit).ToList();
                    if (audits.Any(a => a.Details.Any()))
                    {
                        var transaction = new Rock.Transactions.AuditTransaction();
                        transaction.Audits = audits;
                        Rock.Transactions.RockQueue.TransactionQueue.Enqueue(transaction);
                    }
                }
                catch (SystemException ex)
                {
                    ExceptionLogService.LogException(ex, null);
                }
            }

            List <ITransaction> indexTransactions = new List <ITransaction>();

            foreach (var item in updatedItems)
            {
                if (item.State == EntityState.Detached || item.State == EntityState.Deleted)
                {
                    TriggerWorkflows(item, WorkflowTriggerType.PostDelete, personAlias);
                }
                else
                {
                    if (item.PreSaveState == EntityState.Added)
                    {
                        TriggerWorkflows(item, WorkflowTriggerType.PostAdd, personAlias);
                    }

                    TriggerWorkflows(item, WorkflowTriggerType.ImmediatePostSave, personAlias);
                    TriggerWorkflows(item, WorkflowTriggerType.PostSave, personAlias);
                }

                if (item.Entity is IModel)
                {
                    var model = item.Entity as IModel;
                    model.PostSaveChanges(this);
                }

                // check if this entity should be passed on for indexing
                if (item.Entity is IRockIndexable)
                {
                    if (item.State == EntityState.Detached || item.State == EntityState.Deleted)
                    {
                        DeleteIndexEntityTransaction transaction = new DeleteIndexEntityTransaction();
                        transaction.EntityTypeId = item.Entity.TypeId;
                        transaction.EntityId     = item.Entity.Id;

                        indexTransactions.Add(transaction);
                    }
                    else
                    {
                        IndexEntityTransaction transaction = new IndexEntityTransaction();
                        transaction.EntityTypeId = item.Entity.TypeId;
                        transaction.EntityId     = item.Entity.Id;

                        indexTransactions.Add(transaction);
                    }
                }

                if (item.Entity is ICacheable)
                {
                    (item.Entity as ICacheable).UpdateCache(item.PreSaveState, this);
                }
            }

            // check if Indexing is enabled in another thread to avoid deadlock when Snapshot Isolation is turned off when the Index components upload/load attributes
            if (indexTransactions.Any())
            {
                System.Threading.Tasks.Task.Run(() =>
                {
                    var indexingEnabled = IndexContainer.GetActiveComponent() == null ? false : true;
                    if (indexingEnabled)
                    {
                        indexTransactions.ForEach(t => RockQueue.TransactionQueue.Enqueue(t));
                    }
                });
            }
        }
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,
                        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 #4
0
        /// <summary>
        /// Pres the save changes.
        /// </summary>
        /// <param name="dbContext">The database context.</param>
        /// <param name="state">The state.</param>
        public override void PreSaveChanges(Rock.Data.DbContext dbContext, System.Data.Entity.EntityState state)
        {
            try
            {
                string verb   = string.Empty;
                string action = string.Empty;

                var   rockContext = (RockContext)dbContext;
                Group group       = null;

                if (state == System.Data.Entity.EntityState.Added)
                {
                    verb   = "ADD";
                    action = "Added to group.";
                    if (!this.DateTimeAdded.HasValue)
                    {
                        this.DateTimeAdded = RockDateTime.Now;
                    }
                }
                else if (state == System.Data.Entity.EntityState.Modified)
                {
                    verb = "UPDATE";

                    var changeEntry = dbContext.ChangeTracker.Entries <GroupMember>().Where(a => a.Entity == this).FirstOrDefault();
                    if (changeEntry != null)
                    {
                        var origGroupMemberStatus = (GroupMemberStatus)changeEntry.OriginalValues["GroupMemberStatus"];
                        if (origGroupMemberStatus != this.GroupMemberStatus)
                        {
                            action = string.Format("Group member status changed from {0} to {1}", origGroupMemberStatus.ToString(), this.GroupMemberStatus.ToString());
                        }

                        var origGroupRoleId = (int)changeEntry.OriginalValues["GroupRoleId"];
                        if (origGroupRoleId != this.GroupRoleId)
                        {
                            if (group == null)
                            {
                                group = this.Group;
                            }
                            if (group == null)
                            {
                                group = new GroupService(rockContext).Get(this.GroupId);
                            }
                            if (group != null)
                            {
                                var groupType = GroupTypeCache.Read(group.GroupTypeId);
                                if (groupType != null)
                                {
                                    var origRole = groupType.Roles.FirstOrDefault(r => r.Id == origGroupRoleId);
                                    var newRole  = groupType.Roles.FirstOrDefault(r => r.Id == this.GroupRoleId);
                                    action = string.Format("Group role changed from {0} to {1}",
                                                           (origRole != null ? origRole.Name : "??"),
                                                           (newRole != null ? newRole.Name : "??"));
                                }
                            }
                        }
                    }
                }
                else if (state == System.Data.Entity.EntityState.Deleted)
                {
                    verb   = "DELETE";
                    action = "Removed from group.";
                }

                // process universal search indexing if required
                if (group == null)
                {
                    group = this.Group;
                }
                if (group == null)
                {
                    group = new GroupService(rockContext).Get(this.GroupId);
                }
                if (group != null)
                {
                    var groupType = GroupTypeCache.Read(group.GroupTypeId);
                    if (groupType != null)
                    {
                        if (groupType.IsIndexEnabled)
                        {
                            IndexEntityTransaction transaction = new IndexEntityTransaction();
                            transaction.EntityTypeId = groupType.Id;
                            transaction.EntityId     = group.Id;

                            RockQueue.TransactionQueue.Enqueue(transaction);
                        }
                    }
                }

                if (!string.IsNullOrWhiteSpace(action))
                {
                    if (group == null)
                    {
                        group = this.Group;
                    }
                    if (group == null)
                    {
                        group = new GroupService(rockContext).Get(this.GroupId);
                    }
                    if (group != null)
                    {
                        var personEntityTypeId        = EntityTypeCache.Read("Rock.Model.Person").Id;
                        var groupEntityTypeId         = EntityTypeCache.Read("Rock.Model.Group").Id;
                        var groupMembershipCategoryId = CategoryCache.Read(Rock.SystemGuid.Category.HISTORY_PERSON_GROUP_MEMBERSHIP.AsGuid(), rockContext).Id;

                        new HistoryService(rockContext).Add(new History
                        {
                            EntityTypeId        = personEntityTypeId,
                            CategoryId          = groupMembershipCategoryId,
                            EntityId            = this.PersonId,
                            Summary             = action,
                            Caption             = group.Name,
                            RelatedEntityTypeId = groupEntityTypeId,
                            RelatedEntityId     = this.GroupId,
                            Verb = verb
                        });
                    }
                }
            }
            catch { }

            base.PreSaveChanges(dbContext, state);
        }
Example #5
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);
        }