/// <summary>
        /// Executes the specified workflow.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="action">The workflow action.</param>
        /// <param name="entity">The entity.</param>
        /// <param name="errorMessages">The error messages.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public override bool Execute(RockContext rockContext, WorkflowAction action, object entity, out List <string> errorMessages)
        {
            var filterAttendanceSchedules = GetActionAttributeValue(action, "FilterAttendanceSchedules").AsBoolean();

            var checkInState = GetCheckInState(entity, out errorMessages);

            if (checkInState != null)
            {
                var family = checkInState.CheckIn.Families.Where(f => f.Selected).FirstOrDefault();
                if (family != null)
                {
                    KioskCountUtility kioskCountUtility = new KioskCountUtility(checkInState.ConfiguredGroupTypes);
                    List <int>        volunteerGroupIds = kioskCountUtility.VolunteerGroupIds;

                    var locationService   = new LocationService(rockContext);
                    var attendanceService = new AttendanceService(rockContext).Queryable();
                    foreach (var person in family.People)
                    {
                        List <int> filteredScheduleIds = new List <int>();
                        foreach (var groupType in person.GroupTypes)
                        {
                            foreach (var group in groupType.Groups)
                            {
                                foreach (var location in group.Locations)
                                {
                                    //Get a new copy of the entity because otherwise the threshold data may be stale
                                    var locationEntity = locationService.Get(location.Location.Id);

                                    foreach (var schedule in location.Schedules.ToList())
                                    {
                                        if (filterAttendanceSchedules && filteredScheduleIds.Contains(schedule.Schedule.Id))
                                        {
                                            location.Schedules.Remove(schedule);
                                            continue;
                                        }

                                        var attendanceQry = attendanceService.Where(a =>
                                                                                    a.EndDateTime == null &&
                                                                                    a.Occurrence.ScheduleId == schedule.Schedule.Id &&
                                                                                    a.StartDateTime >= Rock.RockDateTime.Today);


                                        if (filterAttendanceSchedules && attendanceQry.Where(a => a.PersonAlias.PersonId == person.Person.Id).Any())
                                        {
                                            filteredScheduleIds.Add(schedule.Schedule.Id);
                                            location.Schedules.Remove(schedule);
                                            continue;
                                        }

                                        var threshold = locationEntity.FirmRoomThreshold ?? 0;

                                        LocationScheduleCount locationScheduleCount = kioskCountUtility.GetLocationScheduleCount(location.Location.Id, schedule.Schedule.Id);
                                        if (locationScheduleCount.TotalCount >= threshold)
                                        {
                                            location.Schedules.Remove(schedule);
                                            continue;
                                        }

                                        if (!volunteerGroupIds.Contains(group.Group.Id))
                                        {
                                            threshold = Math.Min(locationEntity.FirmRoomThreshold ?? 0, locationEntity.SoftRoomThreshold ?? 0);

                                            if (locationScheduleCount.ChildCount >= threshold)
                                            {
                                                location.Schedules.Remove(schedule);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                    return(true);
                }
                else
                {
                    errorMessages.Add("There is not a family that is selected");
                }

                return(false);
            }

            return(false);
        }
Esempio n. 2
0
        /// <summary>
        /// Executes the specified workflow.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="action">The workflow action.</param>
        /// <param name="entity">The entity.</param>
        /// <param name="errorMessages">The error messages.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public override bool Execute(RockContext rockContext, WorkflowAction action, object entity, out List <string> errorMessages)
        {
            var filterAttendanceSchedules = GetActionAttributeValue(action, "FilterAttendanceSchedules").AsBoolean();

            var checkInState = GetCheckInState(entity, out errorMessages);

            if (checkInState != null)
            {
                var family = checkInState.CheckIn.Families.Where(f => f.Selected).FirstOrDefault();
                if (family != null)
                {
                    var volAttributeGuid = Constants.VOLUNTEER_ATTRIBUTE_GUID;

                    KioskCountUtility kioskCountUtility = new KioskCountUtility(checkInState.ConfiguredGroupTypes);

                    ObjectCache cache             = Rock.Web.Cache.RockMemoryCache.Default;
                    List <int>  volunteerGroupIds = cache[volAttributeGuid + "CachedVolunteerGroups"] as List <int>;
                    if (volunteerGroupIds == null)   //cache miss load from DB
                    {
                        var volAttribute = AttributeCache.Read(volAttributeGuid.AsGuid());
                        AttributeValueService attributeValueService = new AttributeValueService(rockContext);
                        volunteerGroupIds = attributeValueService.Queryable().Where(av => av.AttributeId == volAttribute.Id && av.Value == "True").Select(av => av.EntityId.Value).ToList();
                        var cachePolicy = new CacheItemPolicy();
                        cachePolicy.AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(10);
                        cache.Set(volAttributeGuid + "CachedVolunteerGroups", volAttributeGuid + "CachedVolunteerGroups", cachePolicy);
                    }

                    var locationService   = new LocationService(rockContext);
                    var attendanceService = new AttendanceService(rockContext).Queryable();
                    foreach (var person in family.People)
                    {
                        List <int> filteredScheduleIds = new List <int>();
                        foreach (var groupType in person.GroupTypes)
                        {
                            foreach (var group in groupType.Groups)
                            {
                                foreach (var location in group.Locations)
                                {
                                    //Get a new copy of the entity because otherwise the threshold data may be stale
                                    var locationEntity = locationService.Get(location.Location.Id);

                                    foreach (var schedule in location.Schedules.ToList())
                                    {
                                        if (filterAttendanceSchedules && filteredScheduleIds.Contains(schedule.Schedule.Id))
                                        {
                                            location.Schedules.Remove(schedule);
                                            continue;
                                        }

                                        var attendanceQry = attendanceService.Where(a =>
                                                                                    a.EndDateTime == null &&
                                                                                    a.ScheduleId == schedule.Schedule.Id &&
                                                                                    a.StartDateTime >= Rock.RockDateTime.Today);


                                        if (filterAttendanceSchedules && attendanceQry.Where(a => a.PersonAlias.PersonId == person.Person.Id).Any())
                                        {
                                            filteredScheduleIds.Add(schedule.Schedule.Id);
                                            location.Schedules.Remove(schedule);
                                            continue;
                                        }

                                        var threshold = locationEntity.FirmRoomThreshold ?? 0;

                                        LocationScheduleCount locationScheduleCount = kioskCountUtility.GetLocationScheduleCount(location.Location.Id, schedule.Schedule.Id);
                                        if (locationScheduleCount.TotalCount >= threshold)
                                        {
                                            location.Schedules.Remove(schedule);
                                            continue;
                                        }

                                        if (!volunteerGroupIds.Contains(group.Group.Id))
                                        {
                                            threshold = Math.Min(locationEntity.FirmRoomThreshold ?? 0, locationEntity.SoftRoomThreshold ?? 0);

                                            if (locationScheduleCount.ChildCount >= threshold)
                                            {
                                                location.Schedules.Remove(schedule);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                    return(true);
                }
                else
                {
                    errorMessages.Add("There is not a family that is selected");
                }

                return(false);
            }

            return(false);
        }