Exemplo n.º 1
0
        private void UpdateLocation(Guid?locationGuid, PersonCacher personCacher, LocationCacher locationCacher,
                                    ref bool saveNeeded)
        {
            if (locationGuid == null)
            {
                return;
            }

            var location = locationCacher.AllForThisElection.FirstOrDefault(l => l.LocationGuid == locationGuid);

            if (location == null)
            {
                return;
            }

            var oldCount = location.BallotsCollected;
            var newCount = personCacher.AllForThisElection.Count(
                p => p.VotingLocationGuid == location.LocationGuid && !String.IsNullOrEmpty(p.VotingMethod));

            if (oldCount == newCount)
            {
                return;
            }

            Db.Location.Attach(location);

            location.BallotsCollected = newCount;

            locationCacher.UpdateItemAndSaveCache(location);
            saveNeeded = true;
        }
Exemplo n.º 2
0
        public JsonResult SortLocations(List <int> idList)
        {
            //var ids = idList.Split(new[] { ',' }).AsInts().ToList();

            var locationCacher = new LocationCacher(SharedDbContext);

            var locations = locationCacher.AllForThisElection.Where(l => idList.Contains(l.Id)).ToList();

            var sortOrder = 1;

            foreach (var id in idList)
            {
                var newOrder = sortOrder++;

                var location = locations.SingleOrDefault(l => l.Id == id);

                if (location != null && location.SortOrder != newOrder)
                {
                    SharedDbContext.Location.Attach(location);
                    location.SortOrder = newOrder;

                    locationCacher.UpdateItemAndSaveCache(location);
                }
            }

            SharedDbContext.SaveChanges();

            return(new
            {
                Saved = true
            }.AsJsonResult());
        }
Exemplo n.º 3
0
        //TODO
        /// <Summary>Does this page need to show the location selector?</Summary>
        //public bool ShowLocationSelector(MenuHelper currentMenu)
        //{
        //    return currentMenu.ShowLocationSelection && HasLocations;
        //}

        public JsonResult UpdateStatus(int locationId, string status)
        {
            var locationCacher = new LocationCacher(SharedDbContext);

            var location = AllLocations.SingleOrDefault(l => l.Id == locationId);

            if (location == null)
            {
                return(new
                {
                    Saved = false
                }.AsJsonResult());
            }

            if (location.TallyStatus != status)
            {
                SharedDbContext.Location.Attach(location);
                location.TallyStatus = status;
                SharedDbContext.SaveChanges();

                locationCacher.UpdateItemAndSaveCache(location);
            }

            return(new
            {
                Saved = true,
                Location = LocationInfoForJson(location)
            }.AsJsonResult());
        }
Exemplo n.º 4
0
        protected Ballot CreateAndRegisterBallot()
        {
            var currentLocationGuid = UserSession.CurrentLocationGuid;

            if (!currentLocationGuid.HasContent())
            {
                var locationModel = new LocationModel();
                currentLocationGuid             = locationModel.GetLocations_Physical().First().LocationGuid;
                UserSession.CurrentLocationGuid = currentLocationGuid;
            }
            var computerCode = UserSession.CurrentComputerCode;

            var ballotCacher = new BallotCacher(Db);
            var firstBallot  = ballotCacher.AllForThisElection.Any();

            var ballot = new Ballot
            {
                BallotGuid          = Guid.NewGuid(),
                LocationGuid        = currentLocationGuid,
                ComputerCode        = computerCode,
                BallotNumAtComputer = NextBallotNumAtComputer(),
                StatusCode          = BallotStatusEnum.Empty,
                Teller1             = UserSession.GetCurrentTeller(1),
                Teller2             = UserSession.GetCurrentTeller(2)
            };

            Db.Ballot.Add(ballot);

            if (firstBallot)
            {
                var locationCacher = new LocationCacher(Db);
                var location       = locationCacher.AllForThisElection.FirstOrDefault(l => l.LocationGuid == currentLocationGuid);
                if (location != null && location.BallotsCollected.AsInt() == 0)
                {
                    var ballotSources = new PersonCacher(Db)
                                        .AllForThisElection
                                        .Count(p => !string.IsNullOrEmpty(p.VotingMethod) && p.VotingLocationGuid == currentLocationGuid);
                    location.BallotsCollected = ballotSources;
                    locationCacher.UpdateItemAndSaveCache(location);
                }
            }

            Db.SaveChanges();

            ballotCacher.UpdateItemAndSaveCache(ballot);

            SessionKey.CurrentBallotId.SetInSession(ballot.C_RowId);

            return(ballot); //TODO: used to be view
        }
Exemplo n.º 5
0
        public JsonResult SortLocations(List <int> idList)
        {
            //var ids = idList.Split(new[] { ',' }).AsInts().ToList();

            var locationCacher = new LocationCacher(Db);

            var locations = locationCacher.AllForThisElection
                            .Where(l => idList.Contains(l.C_RowId))
                            .ToList();

            var sortOrder = 1;

            foreach (var id in idList)
            {
                var location = locations.SingleOrDefault(l => l.C_RowId == id);
                if (location == null)
                {
                    continue;
                }

                // keep "Online" at the bottom of this list.  Use Html/css to support this rule.
                var newOrder = location.IsTheOnlineLocation ? 90 : location.IsTheImportedLocation ? 91 : sortOrder++;

                if (location.SortOrder != newOrder)
                {
                    Db.Location.Attach(location);
                    location.SortOrder = newOrder;

                    locationCacher.UpdateItemAndSaveCache(location);
                }
            }

            Db.SaveChanges();

            return(new
            {
                Saved = true
            }.AsJsonResult());
        }
Exemplo n.º 6
0
        public JsonResult UpdateStatus(int locationId, string status)
        {
            var locationCacher = new LocationCacher(Db);

            var location = GetLocations_Physical().SingleOrDefault(l => l.C_RowId == locationId);

            if (location == null)
            {
                return(new
                {
                    Saved = false
                }.AsJsonResult());
            }

            if (location.IsVirtual)
            {
                return(new
                {
                    Saved = false
                }.AsJsonResult());
            }

            if (location.TallyStatus != status)
            {
                Db.Location.Attach(location);
                location.TallyStatus = status;
                Db.SaveChanges();

                locationCacher.UpdateItemAndSaveCache(location);
            }

            return(new
            {
                Saved = true,
                Location = LocationInfoForJson(location)
            }.AsJsonResult());
        }
Exemplo n.º 7
0
        public JsonResult EditLocation(int id, string text, bool allowModifyOnline = false)
        {
            if (text == OnlineLocationName && !allowModifyOnline)
            {
                return(new
                {
                    Success = false,
                    Status = $"Cannot name a location as \"{OnlineLocationName}\""
                }.AsJsonResult());
            }

            if (text == ImportedLocationName)
            {
                return(new
                {
                    Success = false,
                    Status = $"Cannot name a location as \"{ImportedLocationName}\""
                }.AsJsonResult());
            }

            var locationCacher = new LocationCacher(Db);

            var location = locationCacher.AllForThisElection.SingleOrDefault(l => l.C_RowId == id);
            var changed  = false;

            if (location == null)
            {
                location = new Location
                {
                    ElectionGuid = UserSession.CurrentElectionGuid,
                    LocationGuid = Guid.NewGuid()
                };
                Db.Location.Add(location);
                changed = true;
            }
            else
            {
                if (location.IsVirtual && !allowModifyOnline)
                {
                    return(new
                    {
                        Success = false,
                        Status = "Cannot edit Online location"
                    }.AsJsonResult());
                }

                Db.Location.Attach(location);
            }

            int    locationId;
            string locationText;
            string status;
            var    success = false;

            if (text.HasNoContent() && location.C_RowId > 0)
            {
                // deleting this location

                // don't delete last location
                if (location.IsTheOnlineLocation && allowModifyOnline ||
                    GetLocations_Physical().Count > 1)
                {
                    // delete existing if we can
                    if (!IsLocationInUse(location.LocationGuid))
                    {
                        Db.Location.Remove(location);
                        Db.SaveChanges();
                        locationCacher.RemoveItemAndSaveCache(location);

                        status       = "Deleted";
                        success      = true;
                        locationId   = 0;
                        locationText = "";
                    }
                    else
                    {
                        status       = "Cannot deleted this location because it has Ballots recorded in it";
                        locationId   = location.C_RowId;
                        locationText = location.Name;
                    }
                }
                else
                {
                    // only one
                    status       = "At least one location is required";
                    locationId   = location.C_RowId;
                    locationText = location.Name;
                }
            }
            else if (text.HasContent())
            {
                locationText = location.Name = text;
                locationId   = location.C_RowId; // may be 0 if new

                changed = true;
                status  = "Saved";
            }
            else
            {
                status       = "Nothing to save";
                locationId   = 0;
                locationText = "";
                success      = true;
                changed      = false;
            }

            if (changed)
            {
                Db.SaveChanges();

                locationId = location.C_RowId;
                locationCacher.UpdateItemAndSaveCache(location);
                success = true;
            }

            return(new
            {
                // returns 0 if deleted or not created
                Id = locationId,
                Text = locationText,
                Success = success,
                Status = status
            }.AsJsonResult());
        }
Exemplo n.º 8
0
        public JsonResult EditLocation(int id, string text)
        {
            var locationCacher = new LocationCacher(SharedDbContext);

            var location = locationCacher.AllForThisElection.SingleOrDefault(l => l.Id == id);
            var changed  = false;

            if (location == null)
            {
                location = new Location
                {
                    ElectionGuid = UserSession.CurrentElectionGuid,
                    LocationGuid = Guid.NewGuid()
                };
                SharedDbContext.Location.Add(location);
                changed = true;
            }
            else
            {
                SharedDbContext.Location.Attach(location);
            }

            int    locationId;
            string locationText;
            string status;
            var    success = false;

            if (text.HasNoContent() && location.Id > 0)
            {
                // don't delete last location
                if (AllLocations.Count() > 1)
                {
                    // delete existing if we can
                    var used = new BallotCacher(SharedDbContext).AllForThisElection.Any(b => b.LocationGuid == location.LocationGuid);
                    if (!used)
                    {
                        SharedDbContext.Location.Remove(location);
                        SharedDbContext.SaveChanges();
                        locationCacher.RemoveItemAndSaveCache(location);

                        status       = "Deleted";
                        success      = true;
                        locationId   = 0;
                        locationText = "";
                    }
                    else
                    {
                        status       = "Cannot deleted this location because it has Ballots recorded in it";
                        locationId   = location.Id;
                        locationText = location.Name;
                    }
                }
                else
                {
                    // only one
                    status       = "At least one location is required";
                    locationId   = location.Id;
                    locationText = location.Name;
                }
            }
            else if (text.HasContent())
            {
                locationText = location.Name = text;
                locationId   = location.Id; // may be 0 if new

                changed = true;
                status  = "Saved";
            }
            else
            {
                status       = "Nothing to save";
                locationId   = 0;
                locationText = "";
                success      = true;
                changed      = false;
            }

            if (changed)
            {
                SharedDbContext.SaveChanges();

                locationId = location.Id;
                locationCacher.UpdateItemAndSaveCache(location);
                success = true;
            }

            return(new
            {
                // returns 0 if deleted or not created
                Id = locationId,
                Text = locationText,
                Success = success,
                Status = status
            }.AsJsonResult());
        }