Exemplo n.º 1
0
        /// <summary>
        /// Calculates (or re-calculates) the requirements, then updates the results on the UI
        /// </summary>
        private void CalculateRequirements()
        {
            var rockContext = new RockContext();
            var groupMember = new GroupMemberService(rockContext).Get(hfGroupMemberId.Value.AsInteger());

            if (groupMember != null && !groupMember.IsNewOrChangedGroupMember(rockContext))
            {
                groupMember.CalculateRequirements(rockContext, true);
            }

            ShowGroupRequirementsStatuses();
        }
        /// <summary>
        /// Execute method to check for any workflows to launch and will also recalculate group member requirements when a member is added
        /// </summary>
        public void Execute()
        {
            // if a GroupMember is getting added, call CalculateRequirements to make sure that group member requirements are calculated (if the group has requirements)
            if (State == EntityState.Added || (PreviousIsArchived && IsArchived != PreviousIsArchived))
            {
                if (GroupMemberGuid.HasValue)
                {
                    using (var rockContext = new RockContext())
                    {
                        var groupMember = new GroupMemberService(rockContext).Get(GroupMemberGuid.Value);
                        if (groupMember != null)
                        {
                            groupMember.CalculateRequirements(rockContext, true);
                        }
                    }
                }
            }

            GroupMemberWorkflowTriggerType[] groupMemberWorkflowChangeTriggers = new GroupMemberWorkflowTriggerType[] { GroupMemberWorkflowTriggerType.MemberAddedToGroup, GroupMemberWorkflowTriggerType.MemberRemovedFromGroup, GroupMemberWorkflowTriggerType.MemberStatusChanged, GroupMemberWorkflowTriggerType.MemberRoleChanged };

            // Verify that valid ids were saved
            if (GroupId.HasValue && PersonId.HasValue)
            {
                // Get all the triggers from cache
                var cachedTriggers = GroupMemberWorkflowTriggerService.GetCachedTriggers();

                // If any triggers exist
                if (cachedTriggers != null && cachedTriggers.Any())
                {
                    // Get the ACTIVE group member triggers associated to the group
                    var groupTriggers = cachedTriggers
                                        .Where(w =>
                                               groupMemberWorkflowChangeTriggers.Contains(w.TriggerType) &&
                                               w.IsActive &&
                                               w.GroupId.HasValue &&
                                               w.GroupId.Value == GroupId.Value)
                                        .OrderBy(w => w.Order)
                                        .ToList();

                    // Get any ACTIVE triggers associated to a group type ( if any are found, will then filter by group type )
                    var groupTypeTriggers = cachedTriggers
                                            .Where(w =>
                                                   groupMemberWorkflowChangeTriggers.Contains(w.TriggerType) &&
                                                   w.IsActive &&
                                                   w.GroupTypeId.HasValue)
                                            .OrderBy(w => w.Order)
                                            .ToList();

                    if (groupTriggers.Any() || groupTypeTriggers.Any())
                    {
                        using (var rockContext = new RockContext())
                        {
                            // If there were any group type triggers, will now need to read the group's group type id
                            // and then further filter these triggers by the current transaction's group type
                            if (groupTypeTriggers.Any())
                            {
                                // Get the current transaction's group type id
                                if (!GroupTypeId.HasValue)
                                {
                                    GroupTypeId = new GroupService(rockContext)
                                                  .Queryable().AsNoTracking()
                                                  .Where(g => g.Id == GroupId.Value)
                                                  .Select(g => g.GroupTypeId)
                                                  .FirstOrDefault();
                                }

                                // Further filter the group type triggers by the group type id
                                groupTypeTriggers = groupTypeTriggers
                                                    .Where(t =>
                                                           t.GroupTypeId.HasValue &&
                                                           t.GroupTypeId.Equals(GroupTypeId))
                                                    .OrderBy(t => t.Order)
                                                    .ToList();
                            }

                            // Combine group and group type triggers
                            var triggers = groupTriggers.Union(groupTypeTriggers).ToList();

                            // If any triggers were found
                            if (triggers.Any())
                            {
                                // Loop through triggers and launch appropriate workflow
                                foreach (var trigger in triggers)
                                {
                                    switch (trigger.TriggerType)
                                    {
                                    case GroupMemberWorkflowTriggerType.MemberAddedToGroup:
                                    {
                                        if (State == EntityState.Added && QualifiersMatch(rockContext, trigger, GroupMemberStatus, GroupMemberStatus, GroupMemberRoleId, GroupMemberRoleId))
                                        {
                                            LaunchWorkflow(rockContext, trigger.WorkflowTypeId, trigger.Name);
                                        }
                                        break;
                                    }

                                    case GroupMemberWorkflowTriggerType.MemberRemovedFromGroup:
                                    {
                                        if (State == EntityState.Deleted && QualifiersMatch(rockContext, trigger, PreviousGroupMemberStatus, PreviousGroupMemberStatus, PreviousGroupMemberRoleId, PreviousGroupMemberRoleId))
                                        {
                                            LaunchWorkflow(rockContext, trigger.WorkflowTypeId, trigger.Name);
                                        }
                                        break;
                                    }

                                    case GroupMemberWorkflowTriggerType.MemberRoleChanged:
                                    {
                                        if (State == EntityState.Modified && PreviousGroupMemberRoleId != GroupMemberRoleId && QualifiersMatch(rockContext, trigger, PreviousGroupMemberRoleId, GroupMemberRoleId))
                                        {
                                            LaunchWorkflow(rockContext, trigger.WorkflowTypeId, trigger.Name);
                                        }
                                        break;
                                    }

                                    case GroupMemberWorkflowTriggerType.MemberStatusChanged:
                                    {
                                        if (State == EntityState.Modified && PreviousGroupMemberStatus != GroupMemberStatus && QualifiersMatch(rockContext, trigger, PreviousGroupMemberStatus, GroupMemberStatus))
                                        {
                                            LaunchWorkflow(rockContext, trigger.WorkflowTypeId, trigger.Name);
                                        }
                                        break;
                                    }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }