Beispiel #1
0
        /// <summary>
        /// Loads the kiosk locations.
        /// </summary>
        /// <param name="kioskDevice">The kiosk device.</param>
        /// <param name="location">The location.</param>
        /// <param name="campusLocations">The campus locations.</param>
        /// <param name="rockContext">The rock context.</param>
        private static void LoadKioskLocations(KioskDevice kioskDevice, Location location, Dictionary <int, int> campusLocations, RockContext rockContext)
        {
            // First check to see if this is a campus location
            int campusId = campusLocations
                           .Where(c => c.Value == location.Id)
                           .Select(c => c.Key)
                           .FirstOrDefault();

            // If location is not a campus, check the location's parent locations to see if any of them are a campus
            if (campusId == 0)
            {
                foreach (var parentLocationId in new LocationService(rockContext)
                         .GetAllAncestorIds(location.Id))
                {
                    campusId = campusLocations
                               .Where(c => c.Value == parentLocationId)
                               .Select(c => c.Key)
                               .FirstOrDefault();
                    if (campusId != 0)
                    {
                        break;
                    }
                }
            }

            LoadKioskLocations(kioskDevice, location, (campusId > 0 ? campusId : (int?)null), rockContext);
        }
Beispiel #2
0
        /// <summary>
        /// Determines whether [is temporarily closed] [the specified kiosk].
        /// </summary>
        /// <param name="kiosk">The kiosk.</param>
        /// <param name="configuredGroupTypeIds">The configured group type ids.</param>
        /// <param name="checkInState">State of the check in.</param>
        /// <returns>
        ///   <c>true</c> if [is temporarily closed] [the specified kiosk]; otherwise, <c>false</c>.
        /// </returns>
        private static bool IsTemporarilyClosed(KioskDevice kiosk, List <int> configuredGroupTypeIds, CheckInState checkInState)
        {
            bool isTemporarilyClosed = (!kiosk.HasLocations(configuredGroupTypeIds) && !checkInState.AllowCheckout) ||
                                       (checkInState.AllowCheckout && !kiosk.HasActiveCheckOutLocations(configuredGroupTypeIds));

            return(isTemporarilyClosed);
        }
Beispiel #3
0
        /// <summary>
        /// Gets the checkin status.
        /// </summary>
        /// <param name="checkInState">State of the check in.</param>
        /// <returns></returns>
        public static CheckinStatus GetCheckinStatus(CheckInState checkInState)
        {
            KioskDevice kiosk = checkInState.Kiosk;
            List <int>  configuredGroupTypeIds = checkInState.ConfiguredGroupTypes;

            bool hasGroupTypes = kiosk.FilteredGroupTypes(configuredGroupTypeIds).Any();

            if (!hasGroupTypes)
            {
                return(CheckinStatus.Inactive);
            }

            if (IsTemporarilyClosed(kiosk, configuredGroupTypeIds, checkInState))
            {
                return(CheckinStatus.TemporarilyClosed);
            }
            else if (IsClosed(kiosk, configuredGroupTypeIds, checkInState))
            {
                return(CheckinStatus.Closed);
            }
            else
            {
                return(CheckinStatus.Active);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Reads the device by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="configuredGroupTypes">The configured group types.</param>
        /// <returns></returns>
        public static KioskDevice Read(int id, List <int> configuredGroupTypes)
        {
            string cacheKey = KioskDevice.CacheKey(id);

            ObjectCache cache  = Rock.Web.Cache.RockMemoryCache.Default;
            KioskDevice device = cache[cacheKey] as KioskDevice;

            // If the kioskdevice is currently inactive, but has a next active time prior to now, force a refresh
            if (device != null && device.FilteredGroupTypes(configuredGroupTypes).Count > 0 && !device.HasLocations(configuredGroupTypes))
            {
                if (device.KioskGroupTypes.Select(g => g.NextActiveTime).Min().CompareTo(RockDateTime.Now) < 0)
                {
                    device = null;
                }
            }

            if (device != null)
            {
                return(device);
            }
            else
            {
                using (var rockContext = new RockContext())
                {
                    var campusLocations = new Dictionary <int, int>();
                    Rock.Web.Cache.CampusCache.All()
                    .Where(c => c.LocationId.HasValue)
                    .Select(c => new
                    {
                        CampusId   = c.Id,
                        LocationId = c.LocationId.Value
                    })
                    .ToList()
                    .ForEach(c => campusLocations.Add(c.CampusId, c.LocationId));

                    var deviceModel = new DeviceService(rockContext)
                                      .Queryable("Locations")
                                      .Where(d => d.Id == id)
                                      .FirstOrDefault();

                    if (deviceModel != null)
                    {
                        device = new KioskDevice(deviceModel);
                        foreach (Location location in deviceModel.Locations)
                        {
                            LoadKioskLocations(device, location, campusLocations, rockContext);
                        }

                        var cachePolicy = new CacheItemPolicy();
                        cachePolicy.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(60);
                        cache.Set(cacheKey, device, cachePolicy);

                        return(device);
                    }
                }
            }

            return(null);
        }
Beispiel #5
0
        /// <summary>
        /// Reads the device by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="configuredGroupTypes">The configured group types.</param>
        /// <returns></returns>
        public static KioskDevice Read(int id, List <int> configuredGroupTypes)
        {
            object obj = new object();

            obj = _locks.GetOrAdd(id, obj);

            lock ( obj )
            {
                string cacheKey = KioskDevice.CacheKey(id);

                ObjectCache cache  = Rock.Web.Cache.RockMemoryCache.Default;
                KioskDevice device = cache[cacheKey] as KioskDevice;

                if (device != null)
                {
                    return(device);
                }
                else
                {
                    using (var rockContext = new RockContext())
                    {
                        var campusLocations = new Dictionary <int, int>();
                        Rock.Web.Cache.CampusCache.All()
                        .Where(c => c.LocationId.HasValue)
                        .Select(c => new
                        {
                            CampusId   = c.Id,
                            LocationId = c.LocationId.Value
                        })
                        .ToList()
                        .ForEach(c => campusLocations.Add(c.CampusId, c.LocationId));

                        var deviceModel = new DeviceService(rockContext)
                                          .Queryable().AsNoTracking()
                                          .Where(d => d.Id == id)
                                          .FirstOrDefault();

                        if (deviceModel != null)
                        {
                            device = new KioskDevice(deviceModel);
                            foreach (Location location in deviceModel.Locations)
                            {
                                LoadKioskLocations(device, location, campusLocations, rockContext);
                            }

                            cache.Set(cacheKey, device, new CacheItemPolicy {
                                AbsoluteExpiration = DateTimeOffset.Now.Date.AddDays(1)
                            });

                            return(device);
                        }
                    }
                }
            }

            return(null);
        }
Beispiel #6
0
        /// <summary>
        /// Determines whether the specified kiosk is closed.
        /// </summary>
        /// <param name="kiosk">The kiosk.</param>
        /// <param name="configuredGroupTypeIds">The configured group type ids.</param>
        /// <param name="checkInState">State of the check in.</param>
        /// <returns>
        ///   <c>true</c> if the specified kiosk is closed; otherwise, <c>false</c>.
        /// </returns>
        private static bool IsClosed(KioskDevice kiosk, List <int> configuredGroupTypeIds, CheckInState checkInState)
        {
            // Closed if there are no active locations and check-out is not allowed, or if check -out is allowed but there
            // are no active check-out locations.
            bool isClosed = (!kiosk.HasActiveLocations(configuredGroupTypeIds) && !checkInState.AllowCheckout) ||
                            (checkInState.AllowCheckout && !kiosk.HasActiveCheckOutLocations(configuredGroupTypeIds));

            return(isClosed);
        }
Beispiel #7
0
        /// <summary>
        /// Loads the kiosk locations.
        /// </summary>
        /// <param name="kioskDevice">The kiosk device.</param>
        /// <param name="location">The location.</param>
        /// <param name="campusId">The campus identifier.</param>
        /// <param name="rockContext">The rock context.</param>
        private static void LoadKioskLocations(KioskDevice kioskDevice, Location location, int?campusId, RockContext rockContext)
        {
            // Get the child locations and the selected location
            var allLocations = new LocationService(rockContext).GetAllDescendentIds(location.Id).ToList();

            allLocations.Add(location.Id);

            var activeSchedules = new Dictionary <int, KioskSchedule>();

            foreach (var groupLocation in new GroupLocationService(rockContext).GetActiveByLocations(allLocations).OrderBy(l => l.Order).AsNoTracking())
            {
                var kioskLocation = new KioskLocation(groupLocation.Location);
                kioskLocation.CampusId = campusId;
                kioskLocation.Order    = groupLocation.Order;

                // Populate each kioskLocation with its schedules (kioskSchedules)
                foreach (var schedule in groupLocation.Schedules.Where(s => s.CheckInStartOffsetMinutes.HasValue))
                {
                    if (activeSchedules.Keys.Contains(schedule.Id))
                    {
                        kioskLocation.KioskSchedules.Add(activeSchedules[schedule.Id]);
                    }
                    else
                    {
                        var kioskSchedule = new KioskSchedule(schedule);
                        kioskSchedule.CheckInTimes = schedule.GetCheckInTimes(RockDateTime.Now);
                        if (kioskSchedule.IsCheckInActive || kioskSchedule.NextActiveDateTime.HasValue)
                        {
                            kioskLocation.KioskSchedules.Add(kioskSchedule);
                            activeSchedules.Add(schedule.Id, kioskSchedule);
                        }
                    }
                }

                // If the group location has any active OR future schedules, add the group's group type to the kiosk's
                // list of group types
                if (kioskLocation.KioskSchedules.Count > 0)
                {
                    KioskGroupType kioskGroupType = kioskDevice.KioskGroupTypes.Where(g => g.GroupType.Id == groupLocation.Group.GroupTypeId).FirstOrDefault();
                    if (kioskGroupType == null)
                    {
                        kioskGroupType = new KioskGroupType(groupLocation.Group.GroupTypeId);
                        kioskDevice.KioskGroupTypes.Add(kioskGroupType);
                    }

                    KioskGroup kioskGroup = kioskGroupType.KioskGroups.Where(g => g.Group.Id == groupLocation.GroupId).FirstOrDefault();
                    if (kioskGroup == null)
                    {
                        kioskGroup = new KioskGroup(groupLocation.Group);
                        kioskGroup.Group.LoadAttributes(rockContext);
                        kioskGroupType.KioskGroups.Add(kioskGroup);
                    }

                    kioskGroup.KioskLocations.Add(kioskLocation);
                }
            }
        }
Beispiel #8
0
        /// <summary>
        /// Loads the kiosk locations.
        /// </summary>
        /// <param name="kioskDevice">The kiosk device.</param>
        /// <param name="location">The location.</param>
        /// <param name="campusId">The campus identifier.</param>
        /// <param name="rockContext">The rock context.</param>
        private static void LoadKioskLocations(KioskDevice kioskDevice, Location location, int?campusId, RockContext rockContext)
        {
            // Get all the child locations also
            var allLocations = new List <int> {
                location.Id
            };

            new LocationService(rockContext)
            .GetAllDescendents(location.Id)
            .Select(l => l.Id)
            .ToList()
            .ForEach(l => allLocations.Add(l));

            foreach (var groupLocation in new GroupLocationService(rockContext).GetActiveByLocations(allLocations))
            {
                var kioskLocation = new KioskLocation(groupLocation.Location);
                kioskLocation.CampusId = campusId;

                // Populate each kioskLocation with its schedules (kioskSchedules)
                foreach (var schedule in groupLocation.Schedules.Where(s => s.CheckInStartOffsetMinutes.HasValue))
                {
                    var nextActiveDateTime = schedule.GetNextCheckInStartTime(RockDateTime.Now);
                    if (nextActiveDateTime.HasValue)
                    {
                        var kioskSchedule = new KioskSchedule(schedule);
                        kioskSchedule.NextActiveDateTime = nextActiveDateTime;
                        kioskLocation.KioskSchedules.Add(kioskSchedule);
                    }
                }

                // If the group location has any active OR future schedules, add the group's group type to the kiosk's
                // list of group types
                if (kioskLocation.KioskSchedules.Count > 0)
                {
                    KioskGroupType kioskGroupType = kioskDevice.KioskGroupTypes.Where(g => g.GroupType.Id == groupLocation.Group.GroupTypeId).FirstOrDefault();
                    if (kioskGroupType == null)
                    {
                        kioskGroupType = new KioskGroupType(groupLocation.Group.GroupTypeId);
                        kioskDevice.KioskGroupTypes.Add(kioskGroupType);
                    }

                    KioskGroup kioskGroup = kioskGroupType.KioskGroups.Where(g => g.Group.Id == groupLocation.GroupId).FirstOrDefault();
                    if (kioskGroup == null)
                    {
                        kioskGroup = new KioskGroup(groupLocation.Group);
                        kioskGroup.Group.LoadAttributes(rockContext);
                        kioskGroupType.KioskGroups.Add(kioskGroup);
                    }

                    kioskGroup.KioskLocations.Add(kioskLocation);
                }
            }
        }
        /// <summary>
        /// Gets the local device configuration status.
        /// </summary>
        /// <param name="localDeviceConfiguration">The local device configuration.</param>
        /// <param name="httpRequest">The HTTP request.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">LocalDeviceConfiguration with a valid KioskId and Checkin Type  is required</exception>
        public static LocalDeviceConfigurationStatus GetLocalDeviceConfigurationStatus(LocalDeviceConfiguration localDeviceConfiguration, HttpRequest httpRequest)
        {
            if (localDeviceConfiguration?.CurrentKioskId == null || localDeviceConfiguration?.CurrentCheckinTypeId == null)
            {
                throw new ArgumentNullException("LocalDeviceConfiguration with a valid KioskId and Checkin Type is required");
            }

            var kiosk = KioskDevice.Get(localDeviceConfiguration.CurrentKioskId.Value, localDeviceConfiguration.CurrentGroupTypeIds);

            DateTime nextActiveDateTime = kiosk.FilteredGroupTypes(localDeviceConfiguration.CurrentGroupTypeIds).Min(g => ( DateTime? )g.NextActiveTime) ?? DateTime.MaxValue;

            nextActiveDateTime = DateTime.SpecifyKind(nextActiveDateTime, DateTimeKind.Unspecified);

            bool isMobileAndExpired = CheckinConfigurationHelper.IsMobileAndExpiredDevice(httpRequest);

            CheckInState checkInState = new CheckInState(localDeviceConfiguration);

            CheckinConfigurationHelper.CheckinStatus checkinStatus = CheckinConfigurationHelper.GetCheckinStatus(checkInState);

            var rockVersion = Rock.VersionInfo.VersionInfo.GetRockProductVersionFullName();

            CheckIn.CheckinType checkinType = new Rock.CheckIn.CheckinType(localDeviceConfiguration.CurrentCheckinTypeId.Value);

            var configurationData = new
            {
                CheckinType        = checkinType,
                IsMobileAndExpired = isMobileAndExpired,
                Kiosk              = kiosk,
                CheckinStatus      = checkinStatus,
                NextActiveDateTime = nextActiveDateTime,
                RockVersion        = rockVersion
            };

            var configurationString = configurationData.ToJson();

            DateTime campusCurrentDateTime = RockDateTime.Now;

            if (kiosk.CampusId.HasValue)
            {
                campusCurrentDateTime = CampusCache.Get(kiosk.CampusId.Value)?.CurrentDateTime ?? RockDateTime.Now;
            }

            LocalDeviceConfigurationStatus localDeviceConfigurationStatus = new LocalDeviceConfigurationStatus();

            localDeviceConfigurationStatus.ConfigurationHash     = Rock.Security.Encryption.GetSHA1Hash(configurationString);
            localDeviceConfigurationStatus.ServerCurrentDateTime = RockDateTime.Now;
            localDeviceConfigurationStatus.CampusCurrentDateTime = campusCurrentDateTime;
            localDeviceConfigurationStatus.NextActiveDateTime    = nextActiveDateTime;
            return(localDeviceConfigurationStatus);
        }
Beispiel #10
0
        /// <summary>
        /// Loads the kiosk locations.
        /// </summary>
        /// <param name="kioskDevice">The kiosk device.</param>
        /// <param name="location">The location.</param>
        /// <param name="campusLocations">The campus locations.</param>
        /// <param name="rockContext">The rock context.</param>
        private static void LoadKioskLocations(KioskDevice kioskDevice, Location location, Dictionary <int, int> campusLocations, RockContext rockContext)
        {
            int campusId       = 0;
            var parentLocation = location;

            while (campusId == 0 && parentLocation != null)
            {
                campusId = campusLocations
                           .Where(c => c.Value == parentLocation.Id)
                           .Select(c => c.Key)
                           .FirstOrDefault();
                parentLocation = parentLocation.ParentLocation;
            }

            LoadKioskLocations(kioskDevice, location, (campusId > 0 ? campusId : (int?)null), rockContext);
        }
Beispiel #11
0
        /// <summary>
        /// Reads the device by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="configuredGroupTypes">The configured group types.</param>
        /// <returns></returns>
        public static KioskDevice Read(int id, List <int> configuredGroupTypes)
        {
            string cacheKey = KioskDevice.CacheKey(id);

            ObjectCache cache  = MemoryCache.Default;
            KioskDevice device = cache[cacheKey] as KioskDevice;

            // If the kioskdevice is currently inactive, but has a next active time prior to now, force a refresh
            if (device != null && device.FilteredGroupTypes(configuredGroupTypes).Count > 0 && !device.HasLocations(configuredGroupTypes))
            {
                if (device.KioskGroupTypes.Select(g => g.NextActiveTime).Min().CompareTo(DateTimeOffset.Now) < 0)
                {
                    device = null;
                }
            }

            if (device != null)
            {
                return(device);
            }
            else
            {
                var deviceModel = new DeviceService().Get(id);

                if (deviceModel != null)
                {
                    device = new KioskDevice(deviceModel);

                    using (new Rock.Data.UnitOfWorkScope())
                    {
                        foreach (Location location in deviceModel.Locations)
                        {
                            LoadKioskLocations(device, location);
                        }
                    }

                    var cachePolicy = new CacheItemPolicy();
                    cachePolicy.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(60);
                    cache.Set(cacheKey, device, cachePolicy);

                    return(device);
                }
            }

            return(null);
        }
Beispiel #12
0
        /// <summary>
        /// Reads the device by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="configuredGroupTypes">The configured group types.</param>
        /// <returns></returns>
        public static KioskDevice Read( int id, List<int> configuredGroupTypes )
        {
            object obj = new object();
            obj =_locks.GetOrAdd( id, obj );

            lock ( obj )
            {
                string cacheKey = KioskDevice.CacheKey( id );

                ObjectCache cache = Rock.Web.Cache.RockMemoryCache.Default;
                KioskDevice device = cache[cacheKey] as KioskDevice;

                // If the kioskdevice is currently inactive, but has a next active time prior to now, force a refresh
                if ( device != null && device.FilteredGroupTypes(configuredGroupTypes).Count > 0 && !device.HasLocations( configuredGroupTypes ) )
                {
                    if ( device.KioskGroupTypes.Select( g => g.NextActiveTime ).Min().CompareTo( RockDateTime.Now ) < 0 )
                    {
                        device = null;
                    }
                }

                if ( device != null )
                {
                    return device;
                }
                else
                {
                    using ( var rockContext = new RockContext() )
                    {
                        var campusLocations = new Dictionary<int, int>();
                        Rock.Web.Cache.CampusCache.All()
                            .Where( c => c.LocationId.HasValue )
                            .Select( c => new
                            {
                                CampusId = c.Id,
                                LocationId = c.LocationId.Value
                            } )
                            .ToList()
                            .ForEach( c => campusLocations.Add( c.CampusId, c.LocationId ) );

                        var deviceModel = new DeviceService( rockContext )
                            .Queryable( "Locations" ).AsNoTracking()
                            .Where( d => d.Id == id )
                            .FirstOrDefault();

                        if ( deviceModel != null )
                        {
                            device = new KioskDevice( deviceModel );
                            foreach ( Location location in deviceModel.Locations )
                            {
                                LoadKioskLocations( device, location, campusLocations, rockContext );
                            }

                            var cachePolicy = new CacheItemPolicy();
                            cachePolicy.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds( 60 );
                            cache.Set( cacheKey, device, cachePolicy );

                            return device;
                        }
                    }
                }
            }

            return null;
        }
Beispiel #13
0
        /// <summary>
        /// Loads the kiosk locations.
        /// </summary>
        /// <param name="kioskDevice">The kiosk device.</param>
        /// <param name="location">The location.</param>
        /// <param name="campusId">The campus identifier.</param>
        /// <param name="rockContext">The rock context.</param>
        private static void LoadKioskLocations( KioskDevice kioskDevice, Location location, int? campusId, RockContext rockContext )
        {
            // Get the child locations and the selected location
            var allLocations = new LocationService( rockContext ).GetAllDescendentIds( location.Id ).ToList();
            allLocations.Add( location.Id );

            var activeSchedules = new Dictionary<int, KioskSchedule>();

            foreach ( var groupLocation in new GroupLocationService( rockContext ).GetActiveByLocations( allLocations ).OrderBy( l => l.Order ).AsNoTracking() )
            {
                var kioskLocation = new KioskLocation( groupLocation.Location );
                kioskLocation.CampusId = campusId;
                kioskLocation.Order = groupLocation.Order;

                // Populate each kioskLocation with its schedules (kioskSchedules)
                foreach ( var schedule in groupLocation.Schedules.Where( s => s.CheckInStartOffsetMinutes.HasValue ) )
                {
                    if ( activeSchedules.Keys.Contains( schedule.Id ) )
                    {
                        kioskLocation.KioskSchedules.Add( activeSchedules[schedule.Id] );
                    }
                    else
                    {
                        var kioskSchedule = new KioskSchedule( schedule );
                        kioskSchedule.CheckInTimes = schedule.GetCheckInTimes( RockDateTime.Now );
                        if ( kioskSchedule.IsCheckInActive || kioskSchedule.NextActiveDateTime.HasValue )
                        {
                            kioskLocation.KioskSchedules.Add( kioskSchedule );
                            activeSchedules.Add( schedule.Id, kioskSchedule );
                        }
                    }
                }

                // If the group location has any active OR future schedules, add the group's group type to the kiosk's
                // list of group types
                if ( kioskLocation.KioskSchedules.Count > 0 )
                {
                    KioskGroupType kioskGroupType = kioskDevice.KioskGroupTypes.Where( g => g.GroupType.Id == groupLocation.Group.GroupTypeId ).FirstOrDefault();
                    if ( kioskGroupType == null )
                    {
                        kioskGroupType = new KioskGroupType( groupLocation.Group.GroupTypeId );
                        kioskDevice.KioskGroupTypes.Add( kioskGroupType );
                    }

                    KioskGroup kioskGroup = kioskGroupType.KioskGroups.Where( g => g.Group.Id == groupLocation.GroupId ).FirstOrDefault();
                    if ( kioskGroup == null )
                    {
                        kioskGroup = new KioskGroup( groupLocation.Group );
                        kioskGroup.Group.LoadAttributes( rockContext );
                        kioskGroupType.KioskGroups.Add( kioskGroup );
                    }

                    kioskGroup.KioskLocations.Add( kioskLocation );
                }
            }
        }
Beispiel #14
0
        /// <summary>
        /// Reads the device by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="configuredGroupTypes">The configured group types.</param>
        /// <returns></returns>
        public static KioskDevice Read( int id, List<int> configuredGroupTypes )
        {
            object obj = new object();
            obj =_locks.GetOrAdd( id, obj );

            lock ( obj )
            {
                string cacheKey = KioskDevice.CacheKey( id );

                ObjectCache cache = Rock.Web.Cache.RockMemoryCache.Default;
                KioskDevice device = cache[cacheKey] as KioskDevice;

                if ( device != null )
                {
                    return device;
                }
                else
                {
                    using ( var rockContext = new RockContext() )
                    {
                        var campusLocations = new Dictionary<int, int>();
                        Rock.Web.Cache.CampusCache.All()
                            .Where( c => c.LocationId.HasValue )
                            .Select( c => new
                            {
                                CampusId = c.Id,
                                LocationId = c.LocationId.Value
                            } )
                            .ToList()
                            .ForEach( c => campusLocations.Add( c.CampusId, c.LocationId ) );

                        var deviceModel = new DeviceService( rockContext )
                            .Queryable().AsNoTracking()
                            .Where( d => d.Id == id )
                            .FirstOrDefault();

                        if ( deviceModel != null )
                        {
                            device = new KioskDevice( deviceModel );
                            foreach ( Location location in deviceModel.Locations )
                            {
                                LoadKioskLocations( device, location, campusLocations, rockContext );
                            }

                            cache.Set( cacheKey, device, new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.Date.AddDays( 1 ) } );

                            return device;
                        }
                    }
                }
            }

            return null;
        }
Beispiel #15
0
        /// <summary>
        /// Loads the kiosk locations.
        /// </summary>
        /// <param name="kioskDevice">The kiosk device.</param>
        /// <param name="location">The location.</param>
        private static void LoadKioskLocations(KioskDevice kioskDevice, Location location)
        {
            var groupLocationService = new GroupLocationService();

            foreach (var groupLocation in groupLocationService.GetActiveByLocation(location.Id))
            {
                DateTimeOffset nextGroupActiveTime = DateTimeOffset.MaxValue;

                var kioskLocation = new KioskLocation(groupLocation.Location);

                // Populate each kioskLocation with it's schedules (kioskSchedules)
                foreach (var schedule in groupLocation.Schedules.Where(s => s.CheckInStartOffsetMinutes.HasValue))
                {
                    var nextScheduleActiveTime = schedule.GetNextCheckInStartTime(DateTimeOffset.Now);
                    if (nextScheduleActiveTime.HasValue && nextScheduleActiveTime.Value.CompareTo(nextGroupActiveTime.DateTime) < 0)
                    {
                        nextGroupActiveTime = nextScheduleActiveTime.Value;
                    }

                    if (schedule.IsCheckInActive && kioskLocation != null)
                    {
                        kioskLocation.KioskSchedules.Add(new KioskSchedule(schedule));
                    }
                }

                // If the group location has any active OR future schedules, add the group's group type to the kiosk's
                // list of group types
                if (kioskLocation.KioskSchedules.Count > 0 || nextGroupActiveTime < DateTimeOffset.MaxValue)
                {
                    KioskGroupType kioskGroupType = kioskDevice.KioskGroupTypes.Where(g => g.GroupType.Id == groupLocation.Group.GroupTypeId).FirstOrDefault();
                    if (kioskGroupType == null)
                    {
                        kioskGroupType = new KioskGroupType(groupLocation.Group.GroupType);
                        kioskGroupType.GroupType.LoadAttributes();
                        kioskGroupType.NextActiveTime = DateTimeOffset.MaxValue;
                        kioskDevice.KioskGroupTypes.Add(kioskGroupType);
                    }

                    if (nextGroupActiveTime.CompareTo(kioskGroupType.NextActiveTime) < 0)
                    {
                        kioskGroupType.NextActiveTime = nextGroupActiveTime;
                    }

                    // If there are active schedules, add the group to the group type groups
                    if (kioskLocation.KioskSchedules.Count > 0)
                    {
                        KioskGroup kioskGroup = kioskGroupType.KioskGroups.Where(g => g.Group.Id == groupLocation.GroupId).FirstOrDefault();
                        if (kioskGroup == null)
                        {
                            kioskGroup = new KioskGroup(groupLocation.Group);
                            kioskGroup.Group.LoadAttributes();
                            kioskGroupType.KioskGroups.Add(kioskGroup);
                        }

                        kioskLocation.Location.LoadAttributes();
                        kioskGroup.KioskLocations.Add(kioskLocation);
                    }
                }
            }

            foreach (var childLocation in location.ChildLocations)
            {
                LoadKioskLocations(kioskDevice, childLocation);
            }
        }
Beispiel #16
0
        /// <summary>
        /// Flushes the specified id.
        /// </summary>
        /// <param name="id">The id.</param>
        public static void Flush(int id)
        {
            ObjectCache cache = MemoryCache.Default;

            cache.Remove(KioskDevice.CacheKey(id));
        }
Beispiel #17
0
        /// <summary>
        /// Reads the device by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="configuredGroupTypes">The configured group types.</param>
        /// <returns></returns>
        public static KioskDevice Read( int id, List<int> configuredGroupTypes )
        {
            string cacheKey = KioskDevice.CacheKey( id );

            ObjectCache cache = MemoryCache.Default;
            KioskDevice device = cache[cacheKey] as KioskDevice;

            // If the kioskdevice is currently inactive, but has a next active time prior to now, force a refresh
            if ( device != null && device.FilteredGroupTypes(configuredGroupTypes).Count > 0 && !device.HasLocations( configuredGroupTypes ) )
            {
                if ( device.KioskGroupTypes.Select( g => g.NextActiveTime ).Min().CompareTo( DateTimeOffset.Now ) < 0 )
                {
                    device = null;
                }
            }

            if (device != null)
            {
                return device;
            }
            else
            {
                var rockContext = new RockContext();

                var deviceModel = new DeviceService( rockContext )
                    .Queryable( "Locations" )
                    .Where( d => d.Id == id )
                    .FirstOrDefault();

                if ( deviceModel != null )
                {
                    device = new KioskDevice( deviceModel );
                    foreach ( Location location in deviceModel.Locations )
                    {
                        LoadKioskLocations( device, location, rockContext );
                    }

                    var cachePolicy = new CacheItemPolicy();
                    cachePolicy.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds( 60 );
                    cache.Set( cacheKey, device, cachePolicy );

                    return device;
                }
            }

            return null;
        }
Beispiel #18
0
        /// <summary>
        /// Loads the kiosk locations.
        /// </summary>
        /// <param name="kioskDevice">The kiosk device.</param>
        /// <param name="location">The location.</param>
        /// <param name="campusLocations">The campus locations.</param>
        /// <param name="rockContext">The rock context.</param>
        private static void LoadKioskLocations( KioskDevice kioskDevice, Location location, Dictionary<int, int> campusLocations, RockContext rockContext )
        {
            // First check to see if this is a campus location
            int campusId = campusLocations
                .Where( c => c.Value == location.Id )
                .Select( c => c.Key )
                .FirstOrDefault();

            // If location is not a campus, check the location's parent locations to see if any of them are a campus
            if ( campusId == 0 )
            {
                foreach ( var parentLocationId in new LocationService( rockContext )
                    .GetAllAncestors( location.Id )
                    .Select( l => l.Id ) )
                {
                    campusId = campusLocations
                        .Where( c => c.Value == parentLocationId )
                        .Select( c => c.Key )
                        .FirstOrDefault();
                    if ( campusId != 0 )
                    {
                        break;
                    }
                }
            }

            LoadKioskLocations( kioskDevice, location, ( campusId > 0 ? campusId : (int?)null ), rockContext );
        }
Beispiel #19
0
        /// <summary>
        /// Loads the kiosk locations.
        /// </summary>
        /// <param name="kioskDevice">The kiosk device.</param>
        /// <param name="location">The location.</param>
        /// <param name="campusId">The campus identifier.</param>
        /// <param name="rockContext">The rock context.</param>
        private static void LoadKioskLocations( KioskDevice kioskDevice, Location location, int? campusId, RockContext rockContext )
        {
            // Get all the child locations also
            var allLocations = new List<int> { location.Id };
            new LocationService( rockContext )
                .GetAllDescendents( location.Id )
                .Select( l => l.Id )
                .ToList()
                .ForEach( l => allLocations.Add( l ) );

            foreach ( var groupLocation in new GroupLocationService( rockContext ).GetActiveByLocations( allLocations ) )
            {
                DateTime nextGroupActiveTime = DateTime.MaxValue;

                var kioskLocation = new KioskLocation( groupLocation.Location );
                kioskLocation.CampusId = campusId;

                // Populate each kioskLocation with its schedules (kioskSchedules)
                foreach ( var schedule in groupLocation.Schedules.Where( s => s.CheckInStartOffsetMinutes.HasValue ) )
                {
                    var nextScheduleActiveTime = schedule.GetNextCheckInStartTime( RockDateTime.Now );
                    if ( nextScheduleActiveTime.HasValue &&
                        ( schedule.IsCheckInActive || nextScheduleActiveTime.Value.CompareTo( RockDateTime.Now ) >= 0 ) &&
                        nextScheduleActiveTime.Value.CompareTo( nextGroupActiveTime ) < 0 )
                    {
                        nextGroupActiveTime = nextScheduleActiveTime.Value;
                    }

                    if ( schedule.IsCheckInActive && kioskLocation != null )
                    {
                        kioskLocation.KioskSchedules.Add( new KioskSchedule( schedule ) );
                    }
                }

                // If the group location has any active OR future schedules, add the group's group type to the kiosk's
                // list of group types
                if ( kioskLocation.KioskSchedules.Count > 0 || nextGroupActiveTime < DateTime.MaxValue )
                {
                    KioskGroupType kioskGroupType = kioskDevice.KioskGroupTypes.Where( g => g.GroupType.Id == groupLocation.Group.GroupTypeId ).FirstOrDefault();
                    if ( kioskGroupType == null )
                    {
                        kioskGroupType = new KioskGroupType( groupLocation.Group.GroupTypeId );
                        kioskGroupType.NextActiveTime = DateTime.MaxValue;
                        kioskDevice.KioskGroupTypes.Add( kioskGroupType );
                    }

                    if ( nextGroupActiveTime.CompareTo( kioskGroupType.NextActiveTime ) < 0 )
                    {
                        kioskGroupType.NextActiveTime = nextGroupActiveTime;
                    }

                    // If there are active schedules, add the group to the group type groups
                    if ( kioskLocation.KioskSchedules.Count > 0 )
                    {
                        KioskGroup kioskGroup = kioskGroupType.KioskGroups.Where( g => g.Group.Id == groupLocation.GroupId ).FirstOrDefault();
                        if ( kioskGroup == null )
                        {
                            kioskGroup = new KioskGroup( groupLocation.Group );
                            kioskGroup.Group.LoadAttributes( rockContext );
                            kioskGroupType.KioskGroups.Add( kioskGroup );
                        }

                        //kioskLocation.Location.LoadAttributes( rockContext ); // Locations don't have UI for attributes
                        kioskGroup.KioskLocations.Add( kioskLocation );
                    }
                }
            }
        }
Beispiel #20
0
        public static CheckinStatus GetCheckinStatus(KioskDevice kiosk, List <int> configuredGroupTypeIds, CheckinType checkInType)
        {
            var checkinState = new CheckInState(kiosk.Device?.Id ?? 0, checkInType.Id, configuredGroupTypeIds);

            return(GetCheckinStatus(checkinState));
        }
Beispiel #21
0
        /// <summary>
        /// Loads the kiosk locations.
        /// </summary>
        /// <param name="kioskDevice">The kiosk device.</param>
        /// <param name="location">The location.</param>
        /// <param name="rockContext">The rock context.</param>
        private static void LoadKioskLocations( KioskDevice kioskDevice, Location location, RockContext rockContext )
        {
            var groupLocationService = new GroupLocationService( rockContext );
            foreach ( var groupLocation in groupLocationService.GetActiveByLocation( location.Id ) )
            {
                DateTimeOffset nextGroupActiveTime = DateTimeOffset.MaxValue;

                var kioskLocation = new KioskLocation( location );

                // Populate each kioskLocation with it's schedules (kioskSchedules)
                foreach ( var schedule in groupLocation.Schedules.Where( s => s.CheckInStartOffsetMinutes.HasValue ) )
                {
                    var nextScheduleActiveTime = schedule.GetNextCheckInStartTime( DateTimeOffset.Now );
                    if ( nextScheduleActiveTime.HasValue && nextScheduleActiveTime.Value.CompareTo( nextGroupActiveTime.DateTime ) < 0 )
                    {
                        nextGroupActiveTime = nextScheduleActiveTime.Value;
                    }

                    if ( schedule.IsCheckInActive && kioskLocation != null )
                    {
                        kioskLocation.KioskSchedules.Add( new KioskSchedule( schedule ) );
                    }
                }

                // If the group location has any active OR future schedules, add the group's group type to the kiosk's 
                // list of group types
                if ( kioskLocation.KioskSchedules.Count > 0 || nextGroupActiveTime < DateTimeOffset.MaxValue )
                {
                    KioskGroupType kioskGroupType = kioskDevice.KioskGroupTypes.Where( g => g.GroupType.Id == groupLocation.Group.GroupTypeId ).FirstOrDefault();
                    if ( kioskGroupType == null )
                    {
                        kioskGroupType = new KioskGroupType( groupLocation.Group.GroupType );
                        kioskGroupType.GroupType.LoadAttributes();
                        kioskGroupType.NextActiveTime = DateTimeOffset.MaxValue;
                        kioskDevice.KioskGroupTypes.Add( kioskGroupType );
                    }

                    if ( nextGroupActiveTime.CompareTo( kioskGroupType.NextActiveTime ) < 0 )
                    {
                        kioskGroupType.NextActiveTime = nextGroupActiveTime;
                    }

                    // If there are active schedules, add the group to the group type groups
                    if ( kioskLocation.KioskSchedules.Count > 0 )
                    {
                        KioskGroup kioskGroup = kioskGroupType.KioskGroups.Where( g => g.Group.Id == groupLocation.GroupId ).FirstOrDefault();
                        if ( kioskGroup == null )
                        {
                            kioskGroup = new KioskGroup( groupLocation.Group );
                            kioskGroup.Group.LoadAttributes();
                            kioskGroupType.KioskGroups.Add( kioskGroup );
                        }

                        kioskLocation.Location.LoadAttributes();
                        kioskGroup.KioskLocations.Add( kioskLocation );
                    }
                }
            }

            foreach ( var childLocation in location.ChildLocations )
            {
                LoadKioskLocations( kioskDevice, childLocation, rockContext );
            }
        }
        /// <summary>
        /// Loads the kiosk locations.
        /// </summary>
        /// <param name="kioskDevice">The kiosk device.</param>
        /// <param name="location">The location.</param>
        /// <param name="campusLocations">The campus locations.</param>
        /// <param name="rockContext">The rock context.</param>
        private static void LoadKioskLocations( KioskDevice kioskDevice, Location location, Dictionary<int, int> campusLocations, RockContext rockContext )
        {
            int campusId = 0;
            var parentLocation = location;
            while ( campusId == 0 && parentLocation != null )
            {
                campusId = campusLocations
                    .Where( c => c.Value == parentLocation.Id )
                    .Select( c => c.Key )
                    .FirstOrDefault();
                parentLocation = parentLocation.ParentLocation;
            }

            LoadKioskLocations( kioskDevice, location, ( campusId > 0 ? campusId : (int?)null ), rockContext );
        }