示例#1
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
        }
示例#2
0
        public Computer GetComputerForMe(Guid oldComputerGuid)
        {
            Computer computer;

            var computerCacher = new ComputerCacher();

            var locationGuid  = UserSession.CurrentLocationGuid;
            var locationModel = new LocationModel();
            var hasMultiplePhysicalLocations = locationModel.HasMultiplePhysicalLocations;

            if (locationGuid == Guid.Empty && !hasMultiplePhysicalLocations)
            {
                // if only one location, learn what it is
                var locations = locationModel.GetLocations_Physical();
                // var locations = new LocationCacher(Db)
                //   .AllForThisElection
                //   .Where(l => l.Name != LocationModel.OnlineLocationName)
                //   .Where(l => l.Name != LocationModel.ImportedLocationName)
                //   .OrderBy(l => l.SortOrder).ToList();
                if (locations.Count == 0)
                {
                    // missing location?  fix it
                    var location = new Location
                    {
                        ElectionGuid = UserSession.CurrentElectionGuid,
                        Name         = "Main Location",
                        LocationGuid = Guid.NewGuid()
                    };
                    Db.Location.Add(location);
                    Db.SaveChanges();
                    locationGuid = location.LocationGuid;
                }
                else
                {
                    locationGuid = locations.First().LocationGuid;
                }

                UserSession.CurrentLocationGuid = locationGuid;
            }

            lock (ComputerModelLock)
            {
                var allComputersInThisElection = computerCacher.AllForThisElection;

                computer = allComputersInThisElection.FirstOrDefault(c => c.ComputerGuid == oldComputerGuid && c.ElectionGuid == UserSession.CurrentElectionGuid);
                if (computer == null)
                {
                    computer = new Computer
                    {
                        ComputerGuid = Guid.NewGuid(),
                        ComputerCode = DetermineNextFreeComputerCode(allComputersInThisElection.Select(c => c.ComputerCode).Distinct().OrderBy(s => s)),
                        ElectionGuid = UserSession.CurrentElectionGuid
                    };
                    computerCacher.UpdateComputer(computer);
                }

                computer.LastContact  = DateTime.Now;
                computer.LocationGuid = locationGuid;
                computer.AuthLevel    = UserSession.AuthLevel;
                computer.SessionId    = HttpContext.Current.Session.SessionID;
            }

            UserSession.CurrentComputer = computer;

            return(computer);
        }