Пример #1
0
        private static void InsertElection(ElectionsTable electionsTable, string electionKey,
                                           [CanBeNull] string electionKeyToCopyDatesFrom = null, string electionDesc = null)
        {
            var stateCode          = Elections.GetStateCodeFromKey(electionKey);
            var electionDate       = Elections.GetElectionDateFromKey(electionKey);
            var electionDateString = Elections.GetElectionDateStringFromKey(electionKey);

            if (electionDesc == null)
            {
                electionDesc = Elections.FormatElectionDescription(electionKey);
            }
            var electionType      = Elections.GetElectionTypeFromKey(electionKey);
            var nationalPartyCode = Elections.GetNationalPartyCodeFromKey(electionKey);
            var partyCode         = Parties.FormatPartyKey(stateCode, nationalPartyCode);
            var electionOrder     = Parties.GetNationalPartyOrder(nationalPartyCode);

            // Dates
            var registrationDeadline   = DefaultDbDate;
            var earlyVotingBegin       = DefaultDbDate;
            var earlyVotingEnd         = DefaultDbDate;
            var mailBallotBegin        = DefaultDbDate;
            var mailBallotEnd          = DefaultDbDate;
            var mailBallotDeadline     = DefaultDbDate;
            var absenteeBallotBegin    = DefaultDbDate;
            var absenteeBallotEnd      = DefaultDbDate;
            var absenteeBallotDeadline = DefaultDbDate;
            var electionAdditionalInfo = Empty;
            //States.GetElectionAdditionalInfo(stateCode).SafeString();
            var ballotInstructions =
                Empty; //States.GetBallotInstructions(stateCode).SafeString();

            if (!IsNullOrWhiteSpace(electionKeyToCopyDatesFrom))
            {
                var electionToCopyDatesFrom = Elections.GetData(electionKeyToCopyDatesFrom);
                if (electionToCopyDatesFrom.Count == 1)
                {
                    var row = electionToCopyDatesFrom[0];
                    registrationDeadline   = row.RegistrationDeadline;
                    earlyVotingBegin       = row.EarlyVotingBegin;
                    earlyVotingEnd         = row.EarlyVotingEnd;
                    mailBallotBegin        = row.MailBallotBegin;
                    mailBallotEnd          = row.MailBallotEnd;
                    mailBallotDeadline     = row.MailBallotDeadline;
                    absenteeBallotBegin    = row.AbsenteeBallotBegin;
                    absenteeBallotEnd      = row.AbsenteeBallotEnd;
                    absenteeBallotDeadline = row.AbsenteeBallotDeadline;
                    electionAdditionalInfo = row.ElectionAdditionalInfo;
                    ballotInstructions     = row.BallotInstructions;
                }
            }

            electionsTable.AddRow(electionKey, stateCode, Empty, Empty,
                                  electionDate, electionDateString, electionType, nationalPartyCode, partyCode,
                                  Empty, electionDesc, electionAdditionalInfo, Empty, DefaultDbDate,
                                  ballotInstructions, false, /*0, DefaultDbDate, 0, DefaultDbDate, 0,
                                                              * DefaultDbDate, 0, DefaultDbDate, 0,*/Empty, electionOrder, false, false,
                                  registrationDeadline, earlyVotingBegin, earlyVotingEnd, mailBallotBegin,
                                  mailBallotEnd, mailBallotDeadline, absenteeBallotBegin, absenteeBallotEnd,
                                  absenteeBallotDeadline, null);
        }
Пример #2
0
        private static void WriteElectionData(XmlWriter writer, string electionKey)
        {
            writer.WriteStartElement("election");
            writer.WriteAttributeString("key", electionKey);

            ElectionsTable table = Elections.GetDataByElectionKey(electionKey);

            if (table.Count == 1)
            {
                ElectionsRow row = table[0];
                writer.WriteAttributeString("description", row.ElectionDesc);
            }

            writer.WriteEndElement();
        }
Пример #3
0
        protected void ButtonAddElection_OnClick(object sender, EventArgs e)
        {
            try
            {
                _AddElectionTabInfo.ClearValidationErrors();
                var electionType      = ControlAddElectionElectionType.SelectedValue;
                var nationalPartyCode = ControlAddElectionNationalParty.SelectedValue;

                var electionDate = ValidateElectionDate(ControlAddElectionElectionDate,
                                                        FeedbackAddElection, ControlAddElectionPastElection.Checked, out var success);
                if (!success)
                {
                    return;
                }

                // if election type is not primary, the nationalPartyCode is always
                // NationalPartyAll
                if (Elections.IsPrimaryElectionType(electionType))
                {
                    FeedbackAddElection.ValidateRequired(ControlAddElectionNationalParty, "Party",
                                                         out success);
                    if (!success)
                    {
                        return;
                    }
                }
                else
                {
                    nationalPartyCode = Parties.NationalPartyAll;
                }

                // special for Presidential candidates
                if (StateCode == "US" && electionType == "A" && nationalPartyCode == "A")
                {
                    electionType = "G";
                }

                // This tab is now for state elections only
                var newElectionKey = Elections.FormatElectionKey(electionDate, electionType,
                                                                 nationalPartyCode, StateCode);

                // Make sure the election isn't a duplicate
                if (Elections.ElectionKeyExists(newElectionKey))
                {
                    PostAddElectionValidationError("This election already exists.");
                    return;
                }

                // for ElectionTypeStatePrimary, we need to check party conflicts:
                //   ● if it's NationalPartyNonPartisan, there can't be other party primaries
                //     on the same day
                //   ● if it's not NationalPartyNonPartisan, there can't be a NationalPartyNonPartisan
                //     on the same day
                // It is necessary to allow this sometimes, so this has been replaced by a client-side
                // warning/override
                var alreadyHasPartyPrimary = false;
                if (electionType == Elections.ElectionTypeStatePrimary)
                {
                    alreadyHasPartyPrimary = Elections.GetPartyPrimaryExists(StateCode, electionDate);
                }

                bool addPresident;
                switch (electionType)
                {
                case Elections.ElectionTypeStatePresidentialPrimary:
                case Elections.ElectionTypeUSPresidentialPrimary:
                    addPresident = true;
                    break;

                case Elections.ElectionTypeStatePrimary:
                    addPresident = ControlAddElectionIncludePresident.Checked;
                    break;

                case Elections.ElectionTypeGeneralElection:
                    addPresident = StateCode == "US" && nationalPartyCode == "A";
                    break;

                default:
                    addPresident = false;
                    break;
                }

                var includePresidentialCandidates = false;
                if (addPresident && electionType != Elections.ElectionTypeUSPresidentialPrimary)
                {
                    includePresidentialCandidates = ControlAddElectionIncludePresidentCandidates
                                                    .Checked;
                }

                var electionKeyToCopyOfficesAndDatesFrom = Empty;
                if (electionType == Elections.ElectionTypeStatePrimary)
                {
                    electionKeyToCopyOfficesAndDatesFrom = AddElectionCopyOfficesHidden.Value.Trim();
                }

                var copyCandidates = ControlAddElectionCopyCandidates.Checked;

                // Build the tables to add

                var electionsTable            = new ElectionsTable();
                var electionsOfficesTable     = new ElectionsOfficesTable();
                var electionsPoliticiansTable = new ElectionsPoliticiansTable();

                LogElectionsInsert(newElectionKey);

                // The election
                InsertElection(electionsTable, newElectionKey,
                               electionKeyToCopyOfficesAndDatesFrom);

                // Offices
                if (addPresident)
                {
                    InsertOffice(electionsOfficesTable, newElectionKey, Offices.USPresident,
                                 OfficeClass.USPresident, Empty);
                }

                if (!IsNullOrWhiteSpace(electionKeyToCopyOfficesAndDatesFrom))
                {
                    var copyTable =
                        ElectionsOffices.GetOfficeKeysData(electionKeyToCopyOfficesAndDatesFrom);
                    foreach (var row in copyTable.Where(row => row.OfficeKey != Offices.USPresident))
                    {
                        InsertOffice(electionsOfficesTable, newElectionKey, row.OfficeKey,
                                     row.OfficeLevel.ToOfficeClass(), row.DistrictCode);
                    }
                }

                // Candidates
                if (includePresidentialCandidates)
                {
                    var candidateTable =
                        ElectionsPoliticians.GetPresidentialCandidatesFromTemplate(electionDate,
                                                                                   nationalPartyCode);
                    if (candidateTable == null)
                    {
                        throw new ApplicationException($"Presidential candidate template for national party {nationalPartyCode} with date >= {electionDate} not found");
                    }
                    foreach (var row in candidateTable.Rows.OfType <DataRow>())
                    {
                        InsertCandidate(electionsPoliticiansTable, newElectionKey, row.OfficeKey(),
                                        row.PoliticianKey(), row.OrderOnBallot());
                    }
                }

                if (!IsNullOrWhiteSpace(electionKeyToCopyOfficesAndDatesFrom) &&
                    copyCandidates)
                {
                    var candidateTable =
                        ElectionsPoliticians.GetPrimaryCandidatesToCopy(
                            electionKeyToCopyOfficesAndDatesFrom);
                    foreach (var row in candidateTable.Rows.OfType <DataRow>())
                    {
                        InsertCandidate(electionsPoliticiansTable, newElectionKey, row.OfficeKey(),
                                        row.PoliticianKey(), row.OrderOnBallot());
                    }
                }

                Elections.UpdateElectionsAndOffices(electionsTable, electionsOfficesTable,
                                                    electionsPoliticiansTable);

                ReloadElectionControl(newElectionKey);
                _AddElectionTabInfo.Reset();
                FeedbackAddElection.AddInfo(
                    $"Election added: {Elections.FormatElectionDescription(newElectionKey)}");

                if (electionType == Elections.ElectionTypeStatePrimary && !alreadyHasPartyPrimary)
                {
                    FeedbackAddElection.AddInfo(
                        "Your next step is to use the Add/Remove Offices tab to add office contests for" +
                        " this party primary. You can then copy these office for the remaining party primaries.");
                }

                if (!StateCache.IsValidStateCode(StateCode)) // Presidential Primary
                {
                    FeedbackAddElection.AddInfo(
                        "Use the Setup Candidates for Office tab to identify the presidential candidates.");
                }
            }
            catch (Exception ex)
            {
                FeedbackAddElection.HandleException(ex);
            }
        }
Пример #4
0
        private void CreateGeneralElection()
        {
            try
            {
                bool success;
                var  electionDate =
                    ValidateElectionDate(ControlMasterOnlyGeneralElectionDate,
                                         FeedbackMasterOnly, ControlMasterOnlyGeneralPastElection.Checked,
                                         out success);
                if (!success)
                {
                    return;
                }

                var existingElections =
                    Elections.GetStateGeneralElectionsByDate(electionDate);
                var existingStates =
                    existingElections.Select(Elections.GetStateCodeFromKey)
                    .OrderBy(code => code)
                    .ToList();
                var includePresident     = ControlMasterOnlyGeneralIncludePresident.Checked;
                var electionDescTemplate = ControlMasterOnlyGeneralElectionDesc.GetValue()
                                           .Trim();
                if (string.IsNullOrWhiteSpace(electionDescTemplate))
                {
                    electionDescTemplate =
                        Elections.GetGeneralElectionDescriptionTemplate(electionDate);
                }

                var electionsTable         = new ElectionsTable();
                var electionsOfficesTable  = new ElectionsOfficesTable();
                var generalElectionOffices = Offices.GetGeneralElectionOffices();

                var statesCreated = 0;

                // create the elections and (optionally) the presidential contests
                foreach (
                    var code in StateCache.All51StateCodes.Union(StateCache.AllFederalCodes))
                {
                    if (!existingStates.Contains(code))
                    {
                        var electionKey = Elections.FormatElectionKey(electionDate,
                                                                      Elections.ElectionTypeGeneralElection, Parties.NationalPartyAll, code);
                        var electionDesc =
                            GetElectionDescriptionFromTemplate(electionDescTemplate, code,
                                                               electionDate);

                        if (includePresident || !StateCache.IsUSPresident(code))
                        {
                            InsertElection(electionsTable, electionKey, null, electionDesc);
                            statesCreated++;
                        }

                        if (includePresident && StateCache.IsValidStateCode(code))
                        {
                            InsertOffice(electionsOfficesTable, electionKey, Offices.USPresident,
                                         OfficeClass.USPresident, string.Empty);
                        }
                    }
                }

                // add all the offices
                foreach (var row in generalElectionOffices)
                {
                    var stateCode = Offices.GetStateCodeFromKey(row.OfficeKey);
                    if (StateCache.IsValidStateCode(stateCode) && !existingStates.Contains(stateCode))
                    {
                        InsertOffice(electionsOfficesTable,
                                     Elections.FormatElectionKey(electionDate,
                                                                 Elections.ElectionTypeGeneralElection, Parties.NationalPartyAll,
                                                                 stateCode), row.OfficeKey, row.OfficeLevel.ToOfficeClass(),
                                     row.DistrictCode);
                    }
                }

                Elections.UpdateElectionsAndOffices(electionsTable, electionsOfficesTable,
                                                    null);

                ReloadElectionControl();
                var msg =
                    $"General elections were created for {statesCreated} states and pseudo-states.";
                if (existingStates.Count > 0)
                {
                    msg +=
                        $" There were already general elections on this date for the following states: {string.Join(", ", existingStates)}.";
                }
                FeedbackMasterOnly.AddInfo(msg);
                ResetMasterOnlySubTab(MasterOnlySubTab.CreateGeneral);
            }
            catch (Exception ex)
            {
                FeedbackMasterOnly.PostValidationError(
                    ControlMasterOnlyGeneralElectionDate,
                    "The general election could not be created: " + ex.Message);
            }
        }