protected override bool Update(object newValue) { var electionKey = Page.GetElectionKey(); var changed = false; var stringValue = newValue.ToString(); // get the list of officeKeys var officeKeys = stringValue.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries).ToList(); // actualize any virtuals foreach (var officeKey in officeKeys) { Offices.ActualizeOffice(officeKey, Page.CountyCode, Page.LocalCode); } // create a dictionary of office keys from the updated tree var officeKeyDictionary = officeKeys .ToDictionary(ActualizeOfficeKey, key => false, StringComparer.OrdinalIgnoreCase); // get the current list of office keys for the election var offices = ElectionsOffices.GetOfficeKeysData(electionKey); // delete any that aren't in the dictionary (and from // ElectionsPoliticians too), and mark those that are there // so we know what to add foreach (var office in offices) { var officeKey = office.OfficeKey; if (officeKeyDictionary.ContainsKey(officeKey)) { officeKeyDictionary[officeKey] = true; } else { LogElectionsOfficesDelete(electionKey, officeKey); ElectionsOffices.DeleteByElectionKeyOfficeKey(electionKey, officeKey); ElectionsPoliticians.DeleteByElectionKeyOfficeKey(electionKey, officeKey); changed = true; } } // add offices from the dictionary that aren't marked foreach (var officeKey in officeKeyDictionary.Where(kvp => !kvp.Value) .Select(kvp => kvp.Key)) { LogElectionsOfficesInsert(electionKey, officeKey); InsertElectionOffice(electionKey, officeKey); changed = true; } LoadControl(); return(changed); }
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); } }