private static void InsertOffice(ElectionsOfficesTable electionsOfficesTable, string electionKey, string officeKey, OfficeClass officeClass, string districtCode) { var stateCode = Elections.GetStateCodeFromKey(electionKey); electionsOfficesTable.AddRow(electionKey, officeKey, electionKey, Elections.GetFederalElectionKeyFromKey(electionKey, officeClass.StateCodeProxy()), stateCode, Empty, Empty, districtCode, officeClass.ToInt(), 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 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); } }