private static void InsertCandidate(ElectionsPoliticiansTable electionsPoliticiansTable, string electionKey, string officeKey, string politicianKey, int orderOnBallot) => electionsPoliticiansTable.AddRow(electionKey, officeKey, politicianKey, string.Empty, Elections.GetStateElectionKeyFromKey(electionKey), Elections.GetFederalElectionKeyFromKey(electionKey, OfficeClass.USPresident.StateCodeProxy()), string.Empty, string.Empty, Elections.GetStateCodeFromKey(electionKey), string.Empty, string.Empty, string.Empty, orderOnBallot, false, false, false);
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); } }
private void CopyCandidates() { var candidatesCopied = 0; var officesCopied = 0; var officesSkipped = 0; try { //if (ElectionsPoliticians.CountByElectionKey(GetElectionKey()) > 0) // throw new VoteException( // "This function can only be used if no candidates have been added for the election."); var offices = ElectionsOffices.GetDataByElectionKey(GetElectionKey()); if (offices.Count == 0) { throw new VoteException( "There are no offices for this election to copy. Use the Add/Remove Offices tab to add the offices you want to copy."); } var electionKeyToCopy = MasterOnlyElectionKeyToCopy.Value; foreach (var office in offices) { if ( ElectionsPoliticians.CountByElectionKeyOfficeKey(GetElectionKey(), office.OfficeKey) > 0) { officesSkipped++; continue; } var oldPoliticians = ElectionsPoliticians.GetDataByElectionKeyOfficeKey(electionKeyToCopy, office.OfficeKey); if (oldPoliticians.Count == 0) { continue; } officesCopied++; candidatesCopied += oldPoliticians.Count; var newPoliticians = new ElectionsPoliticiansTable(); foreach (var oldPolitician in oldPoliticians) { newPoliticians.AddRow(office.ElectionKey, office.OfficeKey, oldPolitician.PoliticianKey, oldPolitician.RunningMateKey, office.ElectionKeyState, office.ElectionKeyFederal, office.ElectionKeyCounty, office.ElectionKeyLocal, office.StateCode, office.CountyCode, office.LocalCode, office.DistrictCode, oldPolitician.OrderOnBallot, false, OfficesOfficials.OfficeKeyPoliticianKeyExists(office.OfficeKey, oldPolitician.PoliticianKey), false); } ElectionsPoliticians.UpdateTable(newPoliticians); } FeedbackMasterOnly.AddInfo( officesCopied > 0 ? $"{candidatesCopied} candidates were copied for {officesCopied} offices." : "There were no candidates that could be copied."); if (officesSkipped > 0) { FeedbackMasterOnly.AddInfo( $"{officesSkipped} offices were skipped because there were already candidates entered."); } } catch (Exception ex) { FeedbackMasterOnly.PostValidationError(ControlMasterOnlyElectionToCopy, "The candidates could not be copied: " + ex.Message); } }