Ejemplo n.º 1
0
        /// <summary>
        ///   Leave this election... remove computer record, close election
        /// </summary>
        /// <param name="movingToOtherElection"></param>
        public static void LeaveElection(bool movingToOtherElection)
        {
            var computer = CurrentComputer;

            if (computer != null && computer.AuthLevel == "Known")
            {
                computer.AuthLevel = "Left";
                var computerCacher = new ComputerCacher();
                computerCacher.UpdateComputer(computer);

                var numKnownTellers = computerCacher.ElectionGuidsOfActiveComputers.Count;
                if (numKnownTellers == 0)
                {
                    //TODO
                    //new ElectionModel().CloseElection();
                }
                else
                {
                    Startup.GetService <IPublicHubHelper>().TellPublicAboutVisibleElections();

                    //new PublicHub().TellPublicAboutVisibleElections(); // in case the name, or ListForPublic, etc. has changed
                }
            }


            if (movingToOtherElection)
            {
                ResetWhenSwitchingElections();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Is this election Id in the list of publically visible ids?
        /// </summary>
        /// <param name="electionGuid"></param>
        /// <returns></returns>
        public string GetPasscodeIfAvailable(Guid electionGuid)
        {
            var activeElectionGuids = new ComputerCacher().ElectionGuidsOfActiveComputers.Where(g => g == electionGuid).ToList();

            if (activeElectionGuids.Count == 0)
            {
                return(null);
            }

            var election = GetNewDbContext().Election
                           .FirstOrDefault(e => e.ElectionGuid == electionGuid &&
                                           e.ListForPublic.HasValue &&
                                           e.ListForPublic.Value);

            return(election == null ? null : election.ElectionPasscode);
        }
Ejemplo n.º 3
0
        public Computer GetComputerForMe(Guid oldComputerGuid)
        {
            Computer computer;

            var computerCacher = new ComputerCacher();

            var locationGuid = UserSession.CurrentLocationGuid;
            var hasLocations = false; //TODO  new LocationModel().HasLocations;

            if (locationGuid == Guid.Empty && !hasLocations)
            {
                // if only one location, learn what it is
                //TODO   UserSession.CurrentLocationGuid = locationGuid = new LocationCacher(Db).AllForThisElection.OrderBy(l => l.SortOrder).First().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    = UserSession.CurrentContext.Session.Id;
            }

            UserSession.CurrentComputer = computer;

            return(computer);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Refresh the list and return it.
        /// </summary>
        /// <returns></returns>
        public string RefreshAndGetListOfAvailableElections()
        {
            const string template = "<option value=\"{0}\">{1} {2}</option>";

            var activeElectionGuids = new ComputerCacher().ElectionGuidsOfActiveComputers;

            if (activeElectionGuids.Count == 0)
            {
                return(template.FilledWith(0, "(No elections are active right now.)", ""));
            }

            var elections = GetNewDbContext().Election
                            .Where(e => activeElectionGuids.Contains(e.ElectionGuid) &&
                                   e.ListForPublic.HasValue &&
                                   e.ListForPublic.Value &&
                                   e.ElectionPasscode != null)
                            .Select(e => new { e.Name, e.ElectionGuid, e.Convenor })
                            .ToList();

            return(elections
                   .OrderBy(e => e.Name)
                   .Select(e => template.FilledWith(e.ElectionGuid, e.Name, e.Convenor.SurroundContentWith("(", ")")))
                   .JoinedAsString());
        }
Ejemplo n.º 5
0
        public object ChooseTeller(int num, int tellerId, string newName)
        {
            var helper = new TellerHelper();

            var tellerCacher   = new TellerCacher(Db);
            var computerCacher = new ComputerCacher();

            var currentComputer = UserSession.CurrentComputer;

            if (tellerId == 0)
            {
                UserSession.SetCurrentTeller(num, null);

                switch (num)
                {
                case 1:
                    currentComputer.Teller1 = null;
                    break;

                case 2:
                    currentComputer.Teller2 = null;
                    break;
                }

                computerCacher.UpdateComputer(currentComputer);

                return(new { Saved = true });
            }

            Teller teller;

            if (tellerId == -1)
            {
                // add new
                // check for existing
                teller =
                    tellerCacher.AllForThisElection.FirstOrDefault(t => t.Name.Equals(newName, StringComparison.OrdinalIgnoreCase));
                if (teller == null)
                {
                    // add the new one
                    teller = new Teller
                    {
                        ElectionGuid      = UserSession.CurrentElectionGuid,
                        Name              = newName,
                        UsingComputerCode = UserSession.CurrentComputerCode,
                    };
                    Db.Teller.Add(teller);
                    Db.SaveChanges();
                    tellerCacher.UpdateItemAndSaveCache(teller);
                }
            }
            else
            {
                // using existing
                teller = tellerCacher.GetById(tellerId);
                if (teller == null)
                {
                    return(new { Saved = false });
                }
            }

            switch (num)
            {
            case 1:
                currentComputer.Teller1 = teller.Name;
                break;

            case 2:
                currentComputer.Teller2 = teller.Name;
                break;
            }
            Db.SaveChanges();
            computerCacher.UpdateComputer(currentComputer);

            UserSession.SetCurrentTeller(num, teller.Name);

            return(new
            {
                Saved = true,
                Selected = teller.C_RowId,
                TellerList = helper.GetTellerOptions(num)
            });
        }
Ejemplo n.º 6
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);
        }