Exemplo n.º 1
0
        /// <summary>
        /// Gets the recipient person identifier expression.
        /// </summary>
        /// <param name="dbContext">The database context.</param>
        /// <param name="entityIdProperty">The entity identifier property.</param>
        /// <param name="selection">The selection.</param>
        /// <returns></returns>
        public Expression GetRecipientPersonIdExpression(System.Data.Entity.DbContext dbContext, MemberExpression entityIdProperty, string selection)
        {
            var rockContext = dbContext as RockContext;

            if (rockContext != null)
            {
                var qryGroupService = new GroupService(rockContext).Queryable();

                Expression <Func <Rock.Model.GroupMember, bool> > memberWhereGroupType  = a => 1 == 1;
                Expression <Func <Rock.Model.GroupMember, bool> > memberWhereGroupRoles = a => 1 == 1;
                Expression <Func <Rock.Model.GroupMember, bool> > memberWhereStatus     = a => 1 == 1;

                string[] selectionValues = selection.Split('|');
                if (selectionValues.Length >= 3)
                {
                    GroupMemberService groupMemberService = new GroupMemberService(rockContext);
                    int?groupTypeId = null;

                    Guid groupTypeGuid = selectionValues[1].AsGuid();

                    var groupType = GroupTypeCache.Get(groupTypeGuid);
                    if (groupType != null)
                    {
                        groupTypeId = groupType.Id;
                    }

                    if (groupTypeId.HasValue)
                    {
                        memberWhereGroupType = xx => xx.Group.GroupTypeId == groupTypeId;
                    }

                    var        groupRoleGuids = selectionValues[2].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(n => n.AsGuid()).ToList();
                    List <int> groupRoleIds   = null;
                    if (groupRoleGuids.Count() > 0)
                    {
                        groupRoleIds          = new GroupTypeRoleService(rockContext).Queryable().Where(a => groupRoleGuids.Contains(a.Guid)).Select(a => a.Id).ToList();
                        memberWhereGroupRoles = xx => groupRoleIds.Contains(xx.GroupRoleId);
                    }

                    GroupMemberStatus?groupMemberStatus = selectionValues[3].ConvertToEnumOrNull <GroupMemberStatus>();

                    if (groupMemberStatus.HasValue)
                    {
                        memberWhereStatus = xx => xx.GroupMemberStatus == groupMemberStatus.Value;
                    }
                }

                var memberListQuery = qryGroupService.Select(p => p.Members.AsQueryable()
                                                             .Where(memberWhereGroupType)
                                                             .Where(memberWhereGroupRoles)
                                                             .Where(memberWhereStatus)
                                                             .Select(m => m.PersonId));

                var selectChildrenExpression = SelectExpressionExtractor.Extract(memberListQuery, entityIdProperty, "p");

                return(selectChildrenExpression);
            }

            return(null);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the expression.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="entityIdProperty">The entity identifier property.</param>
        /// <param name="selection">The selection.</param>
        /// <returns></returns>
        public override Expression GetExpression(RockContext context, MemberExpression entityIdProperty, string selection)
        {
            var qryGroupService = new GroupService(context).Queryable();

            Expression <Func <Rock.Model.GroupMember, bool> > memberWhereGroupType  = a => 1 == 1;
            Expression <Func <Rock.Model.GroupMember, bool> > memberWhereGroupRoles = a => 1 == 1;
            Expression <Func <Rock.Model.GroupMember, bool> > memberWhereStatus     = a => 1 == 1;

            string[] selectionValues = selection.Split('|');
            if (selectionValues.Length >= 3)
            {
                GroupMemberService groupMemberService = new GroupMemberService(context);
                int?groupTypeId = null;

                Guid groupTypeGuid = selectionValues[1].AsGuid();

                var groupType = GroupTypeCache.Get(groupTypeGuid);
                if (groupType != null)
                {
                    groupTypeId = groupType.Id;
                }

                if (groupTypeId.HasValue)
                {
                    memberWhereGroupType = xx => xx.Group.GroupTypeId == groupTypeId;
                }

                var        groupRoleGuids = selectionValues[2].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(n => n.AsGuid()).ToList();
                List <int> groupRoleIds   = null;
                if (groupRoleGuids.Count() > 0)
                {
                    groupRoleIds          = new GroupTypeRoleService(context).Queryable().Where(a => groupRoleGuids.Contains(a.Guid)).Select(a => a.Id).ToList();
                    memberWhereGroupRoles = xx => groupRoleIds.Contains(xx.GroupRoleId);
                }

                GroupMemberStatus?groupMemberStatus = selectionValues[3].ConvertToEnumOrNull <GroupMemberStatus>();

                if (groupMemberStatus.HasValue)
                {
                    memberWhereStatus = xx => xx.GroupMemberStatus == groupMemberStatus.Value;
                }
            }

            var memberListQuery = qryGroupService.Select(p => p.Members.AsQueryable()
                                                         .Where(memberWhereGroupType)
                                                         .Where(memberWhereGroupRoles)
                                                         .Where(memberWhereStatus)
                                                         .Select(m => new MemberInfo
            {
                NickName      = m.Person.NickName,
                LastName      = m.Person.LastName,
                SuffixValueId = m.Person.SuffixValueId,
                PersonId      = m.PersonId,
                GroupMemberId = m.Id
            }).OrderBy(a => a.LastName).ThenBy(a => a.NickName));

            var selectChildrenExpression = SelectExpressionExtractor.Extract(memberListQuery, entityIdProperty, "p");

            return(selectChildrenExpression);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets the expression.
        /// </summary>
        /// <param name="entityType">Type of the entity.</param>
        /// <param name="serviceInstance">The service instance.</param>
        /// <param name="parameterExpression">The parameter expression.</param>
        /// <param name="selection">The selection.</param>
        /// <returns></returns>
        public override Expression GetExpression(Type entityType, IService serviceInstance, ParameterExpression parameterExpression, string selection)
        {
            string[] selectionValues = selection.Split('|');
            if (selectionValues.Length >= 2)
            {
                GroupMemberService groupMemberService = new GroupMemberService((RockContext)serviceInstance.Context);
                int groupTypeId = 0;

                Guid groupTypeGuid = selectionValues[0].AsGuid();
                var  groupType     = GroupTypeCache.Get(groupTypeGuid);
                if (groupType != null)
                {
                    groupTypeId = groupType.Id;
                }

                var groupMemberServiceQry = groupMemberService.Queryable().Where(xx => xx.Group.GroupTypeId == groupTypeId && xx.Group.IsArchived != true);

                bool?groupStatus = null;
                if (selectionValues.Length >= 4)
                {
                    groupStatus = selectionValues[3].AsBooleanOrNull();
                }

                if (groupStatus.HasValue)
                {
                    groupMemberServiceQry = groupMemberServiceQry.Where(xx => xx.Group.IsActive == groupStatus.Value);
                }

                var groupRoleGuids = selectionValues[1].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(n => n.AsGuid()).ToList();
                if (groupRoleGuids.Count() > 0)
                {
                    var groupRoleIds = new GroupTypeRoleService((RockContext)serviceInstance.Context).Queryable().Where(a => groupRoleGuids.Contains(a.Guid)).Select(a => a.Id).ToList();
                    groupMemberServiceQry = groupMemberServiceQry.Where(xx => groupRoleIds.Contains(xx.GroupRoleId));
                }

                GroupMemberStatus?groupMemberStatus = null;
                if (selectionValues.Length >= 3)
                {
                    groupMemberStatus = selectionValues[2].ConvertToEnumOrNull <GroupMemberStatus>();
                }

                if (groupMemberStatus.HasValue)
                {
                    groupMemberServiceQry = groupMemberServiceQry.Where(xx => xx.GroupMemberStatus == groupMemberStatus.Value);
                }

                var qry = new PersonService((RockContext)serviceInstance.Context).Queryable()
                          .Where(p => groupMemberServiceQry.Any(xx => xx.PersonId == p.Id));

                Expression extractedFilterExpression = FilterExpressionExtractor.Extract <Rock.Model.Person>(qry, parameterExpression, "p");

                return(extractedFilterExpression);
            }

            return(null);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Gets the expression.
        /// </summary>
        /// <param name="entityType">Type of the entity.</param>
        /// <param name="serviceInstance">The service instance.</param>
        /// <param name="parameterExpression">The parameter expression.</param>
        /// <param name="selection">The selection.</param>
        /// <returns></returns>
        public override Expression GetExpression(Type entityType, IService serviceInstance, ParameterExpression parameterExpression, string selection)
        {
            string[] selectionValues = selection.Split('|');
            var      rockContext     = ( RockContext )serviceInstance.Context;

            if (selectionValues.Length >= 2)
            {
                GroupMemberService groupMemberService = new GroupMemberService(rockContext);
                List <Guid>        groupGuids         = selectionValues[0].Split(',').AsGuidList();
                var groupService = new GroupService(rockContext);
                var groupIds     = groupService.GetByGuids(groupGuids).Select(a => a.Id).Distinct().ToList();

                bool includeChildGroups = false;
                bool includeChildGroupsIncludeSelected = false;
                bool includeChildGroupsPlusDescendants = false;
                bool includeInactiveGroups             = false;
                if (selectionValues.Length >= 3)
                {
                    includeChildGroups = selectionValues[2].AsBooleanOrNull() ?? false;
                }

                if (selectionValues.Length >= 6)
                {
                    includeChildGroupsIncludeSelected = selectionValues[4].AsBooleanOrNull() ?? false;
                    includeChildGroupsPlusDescendants = selectionValues[5].AsBooleanOrNull() ?? false;
                }
                else if (includeChildGroups)
                {
                    // in case the selection was saved before these options where added
                    includeChildGroupsIncludeSelected = true;
                    includeChildGroupsPlusDescendants = true;
                }

                if (selectionValues.Length >= 7)
                {
                    includeInactiveGroups = selectionValues[6].AsBooleanOrNull() ?? true;
                }
                else
                {
                    // if options where saved before this option was added, set to false, even though it would have included inactive before
                    includeInactiveGroups = false;
                }

                GroupMemberStatus?groupMemberStatus = null;
                if (selectionValues.Length >= 4)
                {
                    groupMemberStatus = selectionValues[3].ConvertToEnumOrNull <GroupMemberStatus>();
                }

                var groupMemberServiceQry = groupMemberService.Queryable(true);

                List <int> childGroupIds = new List <int>();

                if (includeChildGroups)
                {
                    foreach (var groupId in groupIds)
                    {
                        if (includeChildGroupsPlusDescendants)
                        {
                            // get all children and descendants of the selected group(s)
                            var descendantGroupIds = groupService.GetAllDescendentGroupIds(groupId, includeInactiveGroups);

                            childGroupIds.AddRange(descendantGroupIds);
                        }
                        else
                        {
                            // get only immediate children of the selected group(s)
                            var childGroups = groupService.Queryable().Where(a => a.ParentGroupId == groupId);
                            if (!includeInactiveGroups)
                            {
                                childGroups = childGroups.Where(a => a.IsActive == true);
                            }

                            childGroupIds.AddRange(childGroups.Select(a => a.Id));
                        }
                    }

                    if (includeChildGroupsIncludeSelected)
                    {
                        groupMemberServiceQry = groupMemberServiceQry.Where(xx => groupIds.Contains(xx.GroupId) || childGroupIds.Contains(xx.GroupId));
                    }
                    else
                    {
                        groupMemberServiceQry = groupMemberServiceQry.Where(xx => childGroupIds.Contains(xx.GroupId));
                    }
                }
                else
                {
                    groupMemberServiceQry = groupMemberServiceQry.Where(xx => groupIds.Contains(xx.GroupId));
                }

                if (groupMemberStatus.HasValue)
                {
                    groupMemberServiceQry = groupMemberServiceQry.Where(xx => xx.GroupMemberStatus == groupMemberStatus.Value);
                }

                var groupRoleGuids = selectionValues[1].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(n => n.AsGuid()).ToList();
                if (groupRoleGuids.Count() > 0)
                {
                    var groupRoleIds = new GroupTypeRoleService(( RockContext )serviceInstance.Context).Queryable().Where(a => groupRoleGuids.Contains(a.Guid)).Select(a => a.Id).ToList();
                    groupMemberServiceQry = groupMemberServiceQry.Where(xx => groupRoleIds.Contains(xx.GroupRoleId));
                }

                if (selectionValues.Length >= 8)
                {
                    string    addedOnSlidingDelimitedValues = selectionValues[7].Replace(',', '|');
                    DateRange dateRange = SlidingDateRangePicker.CalculateDateRangeFromDelimitedValues(addedOnSlidingDelimitedValues);
                    if (dateRange.Start.HasValue)
                    {
                        groupMemberServiceQry = groupMemberServiceQry.Where(xx => xx.DateTimeAdded >= dateRange.Start.Value);
                    }

                    if (dateRange.End.HasValue)
                    {
                        groupMemberServiceQry = groupMemberServiceQry.Where(xx => xx.DateTimeAdded < dateRange.End.Value);
                    }
                }

                IQueryable <PersonIdFirstAttendance> firstAttendanceDateQry = null;
                IQueryable <PersonIdLastAttendance>  lastAttendanceDateQry  = null;

                if (selectionValues.Length >= 10)
                {
                    List <int> attendanceGroupIds = null;
                    if (includeChildGroups)
                    {
                        if (includeChildGroupsIncludeSelected)
                        {
                            attendanceGroupIds = new List <int>();
                            attendanceGroupIds.AddRange(groupIds);
                            attendanceGroupIds.AddRange(childGroupIds);
                        }
                        else
                        {
                            attendanceGroupIds = childGroupIds;
                        }
                    }
                    else
                    {
                        attendanceGroupIds = groupIds;
                    }

                    var groupAttendanceQuery = new AttendanceService(rockContext).Queryable()
                                               .Where(a =>
                                                      a.DidAttend == true &&
                                                      a.Occurrence.GroupId.HasValue &&
                                                      attendanceGroupIds.Contains(a.Occurrence.GroupId.Value));

                    string    firstAttendanceSlidingDelimitedValues = selectionValues[8].Replace(',', '|');
                    DateRange firstAttendanceDateRange = SlidingDateRangePicker.CalculateDateRangeFromDelimitedValues(firstAttendanceSlidingDelimitedValues);

                    if (firstAttendanceDateRange.Start.HasValue || firstAttendanceDateRange.End.HasValue)
                    {
                        firstAttendanceDateQry = groupAttendanceQuery
                                                 .GroupBy(xx => xx.PersonAlias.PersonId)
                                                 .Select(ss => new PersonIdFirstAttendance
                        {
                            PersonId = ss.Key,
                            FirstAttendanceSundayDate = ss.Min(a => a.Occurrence.SundayDate)
                        });

                        if (firstAttendanceDateRange.Start.HasValue)
                        {
                            firstAttendanceDateQry = firstAttendanceDateQry.Where(xx => xx.FirstAttendanceSundayDate >= firstAttendanceDateRange.Start.Value);
                        }

                        if (firstAttendanceDateRange.End.HasValue)
                        {
                            firstAttendanceDateQry = firstAttendanceDateQry.Where(xx => xx.FirstAttendanceSundayDate < firstAttendanceDateRange.End.Value);
                        }
                    }

                    string    lastAttendanceSlidingDelimitedValues = selectionValues[9].Replace(',', '|');
                    DateRange lastAttendanceDateRange = SlidingDateRangePicker.CalculateDateRangeFromDelimitedValues(lastAttendanceSlidingDelimitedValues);

                    if (lastAttendanceDateRange.Start.HasValue || lastAttendanceDateRange.End.HasValue)
                    {
                        lastAttendanceDateQry = groupAttendanceQuery
                                                .GroupBy(xx => xx.PersonAlias.PersonId)
                                                .Select(ss => new PersonIdLastAttendance
                        {
                            PersonId = ss.Key,
                            LastAttendanceSundayDate = ss.Max(a => a.Occurrence.SundayDate)
                        });

                        if (lastAttendanceDateRange.Start.HasValue)
                        {
                            lastAttendanceDateQry = lastAttendanceDateQry.Where(xx => xx.LastAttendanceSundayDate >= lastAttendanceDateRange.Start.Value);
                        }

                        if (lastAttendanceDateRange.End.HasValue)
                        {
                            lastAttendanceDateQry = lastAttendanceDateQry.Where(xx => xx.LastAttendanceSundayDate < lastAttendanceDateRange.End.Value);
                        }
                    }
                }

                IQueryable <Rock.Model.Person> qry = null;
                if (lastAttendanceDateQry == null && firstAttendanceDateQry == null)
                {
                    qry = new PersonService(( RockContext )serviceInstance.Context).Queryable()
                          .Where(p => groupMemberServiceQry.Any(xx => xx.PersonId == p.Id));
                }
                else
                {
                    if (firstAttendanceDateQry != null && lastAttendanceDateQry != null)
                    {
                        qry = new PersonService(( RockContext )serviceInstance.Context).Queryable()
                              .Where(p => groupMemberServiceQry.Any(xx => xx.PersonId == p.Id) &&
                                     firstAttendanceDateQry.Any(aa => aa.PersonId == p.Id) && lastAttendanceDateQry.Any(bb => bb.PersonId == p.Id));
                    }
                    else if (firstAttendanceDateQry != null)
                    {
                        qry = new PersonService(( RockContext )serviceInstance.Context).Queryable()
                              .Where(p => groupMemberServiceQry.Any(xx => xx.PersonId == p.Id) &&
                                     firstAttendanceDateQry.Any(aa => aa.PersonId == p.Id));
                    }
                    else if (lastAttendanceDateQry != null)
                    {
                        qry = new PersonService(( RockContext )serviceInstance.Context).Queryable()
                              .Where(p => groupMemberServiceQry.Any(xx => xx.PersonId == p.Id) &&
                                     lastAttendanceDateQry.Any(aa => aa.PersonId == p.Id));
                    }
                }

                Expression extractedFilterExpression = FilterExpressionExtractor.Extract <Rock.Model.Person>(qry, parameterExpression, "p");

                return(extractedFilterExpression);
            }

            return(null);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Gets the expression.
        /// </summary>
        /// <param name="entityType">Type of the entity.</param>
        /// <param name="serviceInstance">The service instance.</param>
        /// <param name="parameterExpression">The parameter expression.</param>
        /// <param name="selection">The selection.</param>
        /// <returns></returns>
        public override Expression GetExpression(Type entityType, IService serviceInstance, ParameterExpression parameterExpression, string selection)
        {
            string[] selectionValues = selection.Split('|');
            if (selectionValues.Length >= 2)
            {
                GroupMemberService groupMemberService = new GroupMemberService((RockContext)serviceInstance.Context);
                List <Guid>        groupGuids         = selectionValues[0].Split(',').AsGuidList();
                var groupService = new GroupService((RockContext)serviceInstance.Context);
                var groupIds     = groupService.GetByGuids(groupGuids).Select(a => a.Id).Distinct().ToList();

                bool includeChildGroups = false;
                bool includeChildGroupsIncludeSelected = false;
                bool includeChildGroupsPlusDescendants = false;
                bool includeInactiveGroups             = false;
                if (selectionValues.Length >= 3)
                {
                    includeChildGroups = selectionValues[2].AsBooleanOrNull() ?? false;
                }

                if (selectionValues.Length >= 6)
                {
                    includeChildGroupsIncludeSelected = selectionValues[4].AsBooleanOrNull() ?? false;
                    includeChildGroupsPlusDescendants = selectionValues[5].AsBooleanOrNull() ?? false;
                }
                else if (includeChildGroups)
                {
                    // in case the selection was saved before these options where added
                    includeChildGroupsIncludeSelected = true;
                    includeChildGroupsPlusDescendants = true;
                }

                if (selectionValues.Length >= 7)
                {
                    includeInactiveGroups = selectionValues[6].AsBooleanOrNull() ?? true;;
                }
                else
                {
                    // if options where saved before this option was added, set to false, even though it would have included inactive before
                    includeInactiveGroups = false;
                }

                GroupMemberStatus?groupMemberStatus = null;
                if (selectionValues.Length >= 4)
                {
                    groupMemberStatus = selectionValues[3].ConvertToEnumOrNull <GroupMemberStatus>();
                }

                var groupMemberServiceQry = groupMemberService.Queryable();

                if (includeChildGroups)
                {
                    List <int> childGroupIds = new List <int>();
                    foreach (var groupId in groupIds)
                    {
                        if (includeChildGroupsPlusDescendants)
                        {
                            // get all children and descendants of the selected group(s)
                            var descendants = groupService.GetAllDescendents(groupId);
                            if (!includeInactiveGroups)
                            {
                                descendants = descendants.Where(a => a.IsActive == true);
                            }

                            childGroupIds.AddRange(descendants.Select(a => a.Id).Distinct().ToList());
                        }
                        else
                        {
                            // get only immediate children of the selected group(s)
                            var childGroups = groupService.Queryable().Where(a => a.ParentGroupId == groupId);
                            if (!includeInactiveGroups)
                            {
                                childGroups = childGroups.Where(a => a.IsActive == true);
                            }

                            childGroupIds.AddRange(childGroups.Select(a => a.Id));
                        }
                    }

                    if (includeChildGroupsIncludeSelected)
                    {
                        groupMemberServiceQry = groupMemberServiceQry.Where(xx => groupIds.Contains(xx.GroupId) || childGroupIds.Contains(xx.GroupId));
                    }
                    else
                    {
                        groupMemberServiceQry = groupMemberServiceQry.Where(xx => childGroupIds.Contains(xx.GroupId));
                    }
                }
                else
                {
                    groupMemberServiceQry = groupMemberServiceQry.Where(xx => groupIds.Contains(xx.GroupId));
                }

                if (groupMemberStatus.HasValue)
                {
                    groupMemberServiceQry = groupMemberServiceQry.Where(xx => xx.GroupMemberStatus == groupMemberStatus.Value);
                }

                var groupRoleGuids = selectionValues[1].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(n => n.AsGuid()).ToList();
                if (groupRoleGuids.Count() > 0)
                {
                    var groupRoleIds = new GroupTypeRoleService((RockContext)serviceInstance.Context).Queryable().Where(a => groupRoleGuids.Contains(a.Guid)).Select(a => a.Id).ToList();
                    groupMemberServiceQry = groupMemberServiceQry.Where(xx => groupRoleIds.Contains(xx.GroupRoleId));
                }

                if (selectionValues.Length >= 8)
                {
                    string    slidingDelimitedValues = selectionValues[7].Replace(',', '|');
                    DateRange dateRange = SlidingDateRangePicker.CalculateDateRangeFromDelimitedValues(slidingDelimitedValues);
                    if (dateRange.Start.HasValue)
                    {
                        groupMemberServiceQry = groupMemberServiceQry.Where(xx => xx.DateTimeAdded >= dateRange.Start.Value);
                    }

                    if (dateRange.End.HasValue)
                    {
                        groupMemberServiceQry = groupMemberServiceQry.Where(xx => xx.DateTimeAdded < dateRange.End.Value);
                    }
                }

                var qry = new PersonService((RockContext)serviceInstance.Context).Queryable()
                          .Where(p => groupMemberServiceQry.Any(xx => xx.PersonId == p.Id));

                Expression extractedFilterExpression = FilterExpressionExtractor.Extract <Rock.Model.Person>(qry, parameterExpression, "p");

                return(extractedFilterExpression);
            }

            return(null);
        }