public void GetData(string partyKey, string stateCode) { DataTable = OfficesOfficials.GetPartyIncumbentsByState(stateCode, partyKey); }
protected override bool Update(object newValue) { //Parse the value from the UI tree if (!(newValue is string valueStr)) { return(false); } var offices = valueStr.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries) .Select(officeStr => { var selected = officeStr[0] == '*'; var officeSplit = officeStr.Substring(selected ? 1 : 0).Split('='); var isRunoff = officeSplit[1].StartsWith("*", StringComparison.Ordinal); if (isRunoff) { officeSplit[1] = officeSplit[1].Substring(1); } return(new { Selected = selected, OfficeKey = officeSplit[0], IsRunoff = isRunoff, Ids = officeSplit[1].Split(',') }); }); var electionKey = Page.GetElectionKey(); var table = ElectionsPoliticians.GetWinnersData(electionKey); foreach (var o in offices) { var office = o; var politicians = table .Where(row => row.OfficeKey().IsEqIgnoreCase(office.OfficeKey)).ToList(); foreach (var politician in politicians) { if (office.IsRunoff) { var advance = office.Ids.Contains(politician.PoliticianKey, StringComparer.OrdinalIgnoreCase); if (politician.AdvanceToRunoff != advance) { LogDataChange.LogUpdate(ElectionsPoliticians.Column.AdvanceToRunoff, politician.AdvanceToRunoff, advance, DateTime.UtcNow, electionKey, politician.OfficeKey, politician.PoliticianKey); } politician.AdvanceToRunoff = advance; politician.IsWinner = false; } else // non-runoff { var isWinner = office.Ids.Contains(politician.PoliticianKey, StringComparer.OrdinalIgnoreCase); if (politician.IsWinner != isWinner) { LogDataChange.LogUpdate(ElectionsPoliticians.Column.IsWinner, politician.IsWinner, isWinner, DateTime.UtcNow, electionKey, politician.OfficeKey, politician.PoliticianKey); } politician.IsWinner = isWinner; politician.AdvanceToRunoff = false; } } if (office.Selected) { var keys = politicians.Select(p => (politicianKey: p.PoliticianKey, runningMateKey: p.RunningMateKey)).ToList(); OfficesOfficials.UpdateIncumbents(office.OfficeKey, office.Ids, keys, UserSecurityClass, UserName); //// Update incumbents //var positions = Offices.GetPositionsDataByOfficeKey(office.OfficeKey)[0]; //var incumbents = OfficesOfficials.GetData(office.OfficeKey); //var politicianKeysToAdd = // new List<string>( // office.Ids.Where(id => id != "vacant" && id != Empty)); //foreach (var incumbent in incumbents) //{ // var index = // politicianKeysToAdd.FindIndex( // key => key.IsEqIgnoreCase(incumbent.PoliticianKey)); // if (index >= 0) politicianKeysToAdd.RemoveAt(index); // // we don't remove old incumbents for offices with // // Incumbents > ElectionPositions // else if (positions.ElectionPositions == positions.Incumbents) // incumbent.Delete(); //} //foreach (var keyToAdd in politicianKeysToAdd) //{ // var politician = // politicians.FirstOrDefault( // row => row.PoliticianKey.IsEqIgnoreCase(keyToAdd)); // var runningMateKey = politician == null // ? Empty // : politician.RunningMateKey; // incumbents.AddRow(office.OfficeKey, keyToAdd, runningMateKey, // Offices.GetStateCodeFromKey(office.OfficeKey), // Offices.GetCountyCodeFromKey(office.OfficeKey), // Offices.GetLocalKeyFromKey(office.OfficeKey), Empty, DateTime.UtcNow, // UserSecurityClass, UserName); // LogDataChange.LogInsert(OfficesOfficials.TableName, DateTime.UtcNow, // office.OfficeKey, keyToAdd); //} //// Update if any changes //var incumbentsChanged = // incumbents.FirstOrDefault(row => row.RowState != DataRowState.Unchanged) != // null; //if (incumbentsChanged) OfficesOfficials.UpdateTable(incumbents); } } // Update if any changes var winnersChanged = table.FirstOrDefault(row => row.RowState != DataRowState.Unchanged) != null; if (winnersChanged) { ElectionsPoliticians.UpdateTable(table, ElectionsPoliticiansTable.ColumnSet.Winners); } LoadControl(); return(winnersChanged); }
protected override bool Update(object newValue) { //Parse the value from the UI tree var valueStr = newValue as string; if (valueStr == null) { return(false); } var offices = valueStr.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries) .Select(officeStr => { var selected = officeStr[0] == '*'; var officeSplit = officeStr.Substring(selected ? 1 : 0) .Split('='); var isRunoff = officeSplit[1].StartsWith("*", StringComparison.Ordinal); if (isRunoff) { officeSplit[1] = officeSplit[1].Substring(1); } return (new { Selected = selected, OfficeKey = officeSplit[0], IsRunoff = isRunoff, Ids = officeSplit[1].Split(',') }); }); var electionKey = Page.GetElectionKey(); var table = ElectionsPoliticians.GetWinnersData(electionKey); foreach (var o in offices) { var office = o; var politicians = table.Where(row => row.OfficeKey() .IsEqIgnoreCase(office.OfficeKey)) .ToList(); foreach (var politician in politicians) { if (office.IsRunoff) { var advance = office.Ids.Contains(politician.PoliticianKey, StringComparer.OrdinalIgnoreCase); if (politician.AdvanceToRunoff != advance) { LogDataChange.LogUpdate( ElectionsPoliticians.Column.AdvanceToRunoff, politician.AdvanceToRunoff, advance, DateTime.UtcNow, electionKey, politician.OfficeKey, politician.PoliticianKey); } politician.AdvanceToRunoff = advance; politician.IsWinner = false; } else // non-runoff { var isWinner = office.Ids.Contains(politician.PoliticianKey, StringComparer.OrdinalIgnoreCase); if (politician.IsWinner != isWinner) { LogDataChange.LogUpdate(ElectionsPoliticians.Column.IsWinner, politician.IsWinner, isWinner, DateTime.UtcNow, electionKey, politician.OfficeKey, politician.PoliticianKey); } politician.IsWinner = isWinner; politician.AdvanceToRunoff = false; } } if (office.Selected) { // Update incumbents var positions = Offices.GetPositionsDataByOfficeKey(office.OfficeKey)[0]; var incumbents = OfficesOfficials.GetData(office.OfficeKey); var politicianKeysToAdd = new List <string>(office.Ids.Where(id => id != "vacant")); foreach (var incumbent in incumbents) { var index = politicianKeysToAdd.FindIndex( key => key.IsEqIgnoreCase(incumbent.PoliticianKey)); if (index >= 0) { politicianKeysToAdd.RemoveAt(index); } // we don't remove old incumbents for offices with // Incumbents > ElectionPositions else if (positions.ElectionPositions == positions.Incumbents) { incumbent.Delete(); } } foreach (var keyToAdd in politicianKeysToAdd) { var politician = politicians.FirstOrDefault( row => row.PoliticianKey.IsEqIgnoreCase(keyToAdd)); var runningMateKey = politician == null ? string.Empty : politician.RunningMateKey; incumbents.AddRow(officeKey: office.OfficeKey, politicianKey: keyToAdd, runningMateKey: runningMateKey, stateCode: Offices.GetStateCodeFromKey(office.OfficeKey), countyCode: Offices.GetCountyCodeFromKey(office.OfficeKey), localCode: Offices.GetLocalCodeFromKey(office.OfficeKey), districtCode: string.Empty, //LDSVersion: String.Empty, LDSUpdateDate: DefaultDbDate, dataLastUpdated: DateTime.UtcNow, userSecurity: UserSecurityClass, userName: UserName); LogDataChange.LogInsert(OfficesOfficials.TableName, DateTime.UtcNow, office.OfficeKey, keyToAdd); } // Update if any changes var incumbentsChanged = incumbents.FirstOrDefault( row => row.RowState != DataRowState.Unchanged) != null; if (incumbentsChanged) { OfficesOfficials.UpdateTable(incumbents); } } } // Update if any changes var winnersChanged = table.FirstOrDefault(row => row.RowState != DataRowState.Unchanged) != null; if (winnersChanged) { ElectionsPoliticians.UpdateTable(table, ElectionsPoliticiansTable.ColumnSet.Winners); } LoadControl(); return(winnersChanged); }
private void ChangeOfficeKey() { try { var oldKeyItem = _MasterOnlyTabInfo.Single(i => i.Column == "OldKey"); var newKeyItem = _MasterOnlyTabInfo.Single(i => i.Column == "NewKey"); var jurisdictionKey = JurisdictionalKey; var success = true; success &= DataItemBase.ValidateRequired(oldKeyItem); var oldKeyOffice = oldKeyItem.DataControl.GetValue().Trim(); success &= DataItemBase.ValidateRequired(newKeyItem); var newKeyOffice = newKeyItem.DataControl.GetValue().Trim(); if (!string.IsNullOrWhiteSpace(newKeyOffice)) { // get rid of all non-alphanumerics newKeyOffice = Regex.Replace(newKeyOffice, @"[^\dA-Z]", string.Empty, RegexOptions.IgnoreCase); // get rid of leading numerics newKeyOffice = Regex.Replace(newKeyOffice, @"^\d+", string.Empty); var maxLength = Offices.OfficeKeyMaxLength - jurisdictionKey.Length; if (newKeyOffice.Length > maxLength) { newKeyItem.Feedback.PostValidationError(newKeyItem.DataControl, newKeyItem.Description + " is too long by " + (newKeyOffice.Length - maxLength) + " characters."); success = false; } if (newKeyOffice.Length == 0) { newKeyItem.Feedback.PostValidationError(newKeyItem.DataControl, newKeyItem.Description + " consists entirely of non-key characters."); success = false; } } if (success && (oldKeyOffice == newKeyOffice)) { newKeyItem.Feedback.PostValidationError(newKeyItem.DataControl, newKeyItem.Description + " is identical to the Old Office Key."); success = false; } var oldOfficeKey = jurisdictionKey + oldKeyOffice; var newOfficeKey = jurisdictionKey + newKeyOffice; var caseChangeOnly = oldOfficeKey.IsEqIgnoreCase(newOfficeKey); if (success && !caseChangeOnly) { // Make sure the new office key doesn't already exist var existsInTables = new List <string>(); if (Offices.OfficeKeyExists(newOfficeKey)) { existsInTables.Add(Offices.TableName); } if (ElectionsOffices.OfficeKeyExists(newOfficeKey)) { existsInTables.Add(ElectionsOffices.TableName); } if (ElectionsPoliticians.OfficeKeyExists(newOfficeKey)) { existsInTables.Add(ElectionsPoliticians.TableName); } if (OfficesOfficials.OfficeKeyExists(newOfficeKey)) { existsInTables.Add(OfficesOfficials.TableName); } if (ElectionsIncumbentsRemoved.OfficeKeyExists(newOfficeKey)) { existsInTables.Add(ElectionsIncumbentsRemoved.TableName); } if (Politicians.OfficeKeyExists(newOfficeKey)) { existsInTables.Add(Politicians.TableName); } if (existsInTables.Count > 0) { newKeyItem.Feedback.PostValidationError(newKeyItem.DataControl, newKeyItem.Description + " already exists in the following tables: " + string.Join(", ", existsInTables)); success = false; } } if (!success) { return; } // do the replacement var updateCount = 0; updateCount += Offices.UpdateOfficeKey(newOfficeKey, oldOfficeKey); updateCount += ElectionsOffices.UpdateOfficeKeyByOfficeKey(newOfficeKey, oldOfficeKey); updateCount += ElectionsPoliticians.UpdateOfficeKeyByOfficeKey(newOfficeKey, oldOfficeKey); updateCount += OfficesOfficials.UpdateOfficeKeyByOfficeKey(newOfficeKey, oldOfficeKey); updateCount += ElectionsIncumbentsRemoved.UpdateOfficeKeyByOfficeKey(newOfficeKey, oldOfficeKey); updateCount += Politicians.UpdateOfficeKeyByOfficeKey(newOfficeKey, oldOfficeKey); var msg = $"{updateCount} instances of the old office key {oldOfficeKey} were found."; if (updateCount > 0) { msg += $" They were all changed to the new office key {newOfficeKey}."; } FeedbackMasterOnly.AddInfo(msg); ResetMasterOnlySubTab(MasterOnlySubTab.ChangeKey); } catch (Exception ex) { FeedbackMasterOnly.PostValidationError(ControlMasterOnlyNewKey, "The office key could not be changed: " + ex.Message); } }
private void Consolidate() { try { var key1Item = _MasterOnlyTabInfo.Single(i => i.Column == "Key1"); var key2Item = _MasterOnlyTabInfo.Single(i => i.Column == "Key2"); var jurisdictionKey = JurisdictionalKey; var success = true; success &= DataItemBase.ValidateRequired(key1Item); var key1Office = key1Item.DataControl.GetValue().Trim(); success &= DataItemBase.ValidateRequired(key2Item); var key2Office = key2Item.DataControl.GetValue().Trim(); if (success && key1Office.IsEqIgnoreCase(key2Office)) { key2Item.Feedback.PostValidationError(key2Item.DataControl, key2Item.Description + " is identical to " + key1Item.Description); success = false; } var officeKey1 = jurisdictionKey + key1Office; var officeKey2 = jurisdictionKey + key2Office; if (!success) { return; } // do the consolidation var updateCount = 0; if (Offices.OfficeKeyExists(officeKey1)) { updateCount += Offices.DeleteByOfficeKey(officeKey2); } else { updateCount += Offices.UpdateOfficeKey(officeKey1, officeKey2); } foreach (var row in ElectionsOffices.GetDataByOfficeKey(officeKey2)) { if (ElectionsOffices.ElectionKeyOfficeKeyExists(row.ElectionKey, officeKey1)) { updateCount += ElectionsOffices.DeleteByElectionKeyOfficeKey(row.ElectionKey, officeKey2); } else { updateCount += ElectionsOffices.UpdateOfficeKeyByElectionKeyOfficeKey(officeKey1, row.ElectionKey, officeKey2); } } foreach (var row in ElectionsPoliticians.GetDataByOfficeKey(officeKey2)) { if (ElectionsPoliticians.ElectionKeyOfficeKeyPoliticianKeyExists(row.ElectionKey, officeKey1, row.PoliticianKey)) { updateCount += ElectionsPoliticians.DeleteByElectionKeyOfficeKeyPoliticianKey( row.ElectionKey, officeKey2, row.PoliticianKey); } else { updateCount += ElectionsPoliticians.UpdateOfficeKeyByElectionKeyOfficeKeyPoliticianKey( officeKey1, row.ElectionKey, officeKey2, row.PoliticianKey); } } foreach (var row in OfficesOfficials.GetDataByOfficeKey(officeKey2)) { if (OfficesOfficials.OfficeKeyPoliticianKeyExists(officeKey1, row.PoliticianKey)) { updateCount += OfficesOfficials.DeleteByOfficeKeyPoliticianKey(officeKey2, row.PoliticianKey); } else { updateCount += OfficesOfficials.UpdateOfficeKeyByOfficeKeyPoliticianKey(officeKey1, officeKey2, row.PoliticianKey); } } foreach (var row in ElectionsIncumbentsRemoved.GetDataByOfficeKey(officeKey2)) { if (ElectionsIncumbentsRemoved.ElectionKeyOfficeKeyPoliticianKeyExists(row.ElectionKey, officeKey1, row.PoliticianKey)) { updateCount += ElectionsIncumbentsRemoved.DeleteByElectionKeyOfficeKeyPoliticianKey( row.ElectionKey, officeKey2, row.PoliticianKey); } else { updateCount += ElectionsIncumbentsRemoved .UpdateOfficeKeyByElectionKeyOfficeKeyPoliticianKey( officeKey1, row.ElectionKey, officeKey2, row.PoliticianKey); } } updateCount += Politicians.UpdateOfficeKeyByOfficeKey(officeKey1, officeKey2); var msg = $"{updateCount} instances of the second office key {officeKey2} were found."; if (updateCount > 0) { msg += $" They were all changed to the first office key {officeKey1}."; } FeedbackMasterOnly.AddInfo(msg); ResetMasterOnlySubTab(MasterOnlySubTab.Consolidate); } catch (Exception ex) { FeedbackMasterOnly.PostValidationError(ControlMasterOnlyNewKey, "The office keys could not be consolidated: " + ex.Message); } }
protected void ButtonAddCandidates_OnClick(object sender, EventArgs e) { switch (AddCandidatesReloading.Value) { case "reloading": { DeletePoliticianOverrideContainer.AddCssClasses("hidden"); DeletePoliticianOverride.Checked = false; AddCandidatesReloading.Value = Empty; _ManagePoliticiansPanel.LoadControls(); _ManagePoliticiansPanel.ClearAddNewCandidate(true); break; } case "deleting": { AddCandidatesReloading.Value = Empty; DeletePoliticianOverrideContainer.AddCssClasses("hidden"); var keyToDelete = AddCandidatesKeyToDelete.GetValue(); // check for meaningful usages var referenceList = new List <string>(); FormatReferences(referenceList, "Answers", Answers2.CountByPoliticianKey(keyToDelete)); FormatReferences(referenceList, "ElectionsPoliticians", ElectionsPoliticians.CountByPoliticianKey(keyToDelete)); FormatReferences(referenceList, "ElectionsPoliticians:RunningMateKey", ElectionsPoliticians.CountByRunningMateKey(keyToDelete)); FormatReferences(referenceList, "OfficesOfficials", OfficesOfficials.CountByPoliticianKey(keyToDelete)); FormatReferences(referenceList, "OfficesOfficials:RunningMateKey", OfficesOfficials.CountByRunningMateKey(keyToDelete)); if (referenceList.Count > 0 && !DeletePoliticianOverride.Checked) { FeedbackAddCandidates.AddError( "Cannot delete because the PoliticianKey is referenced in the following tables. Check the box to override."); DeletePoliticianOverrideContainer.RemoveCssClass("hidden"); foreach (var @ref in referenceList) { FeedbackAddCandidates.AddError(@ref); } return; } // delete DeletePoliticianOverride.Checked = false; var name = PageCache.Politicians.GetPoliticianName(keyToDelete); if (referenceList.Count > 0) { DeleteAllPoliticianReferences(keyToDelete); } Politicians.DeleteByPoliticianKey(keyToDelete); FeedbackAddCandidates.AddInfo($"Politician {name} ({keyToDelete}) deleted."); AddCandidatesKeyToDelete.Value = Empty; //_DeleteDistrictsTabInfo.ClearValidationErrors(); //PopulateLocalDistrictDropdown(); //_DeleteDistrictsTabInfo.LoadControls(); break; } case "": { //// normal update //_ManagePoliticiansPanel.Update(); //_ManagePoliticiansPanel.ClearAddNewCandidate(); break; } default: throw new VoteException($"Unknown reloading option: '{AddCandidatesReloading.Value}'"); } }
private void DoConsolidation() { try { var selectedIndex = ConsolidateSelectedIndex.Value; string selectedKey; string unselectedKey; switch (selectedIndex) { case "1": selectedKey = ConsolidateKey1.Value; unselectedKey = ConsolidateKey2.Value; break; case "2": selectedKey = ConsolidateKey2.Value; unselectedKey = ConsolidateKey1.Value; break; default: throw new VoteException("Index not 1 or 2"); } //throw new VoteException("An error"); // Politicians var selectedPolitician = Politicians.GetData(selectedKey); var unselectedPolitician = Politicians.GetData(unselectedKey); if (selectedPolitician.Count != 1) { throw new VoteException("Politician " + selectedPolitician + " not found"); } if (unselectedPolitician.Count != 1) { throw new VoteException("Politician " + unselectedKey + " not found"); } var selectedData = UpdatePoliticians(selectedIndex, selectedPolitician, unselectedPolitician); // PoliticiansImagesData and PoliticiansImagesBlobs var selectedImagesData = PoliticiansImagesData.GetData(selectedKey); var unselectedImagesData = PoliticiansImagesData.GetData(unselectedKey); var selectedImagesBlobs = PoliticiansImagesBlobs.GetData(selectedKey); var unselectedImagesBlobs = PoliticiansImagesBlobs.GetData(unselectedKey); UpdateImages(selectedIndex, selectedData, selectedKey, selectedImagesData, selectedImagesBlobs, unselectedImagesData, unselectedImagesBlobs); // Answers var selectedAnswers = Answers.GetActiveDataByPoliticianKey(selectedKey); var unselectedAnswers = Answers.GetDataByPoliticianKey(unselectedKey); UpdateAnswers(selectedKey, selectedAnswers, unselectedAnswers); // ElectionsIncumbentsRemoved var selectedIncumbentsRemoved = ElectionsIncumbentsRemoved.GetDataByPoliticianKey(selectedKey); var unselectedIncumbentsRemoved = ElectionsIncumbentsRemoved.GetDataByPoliticianKey(unselectedKey); UpdateIncumbentsRemoved(selectedKey, unselectedIncumbentsRemoved, selectedIncumbentsRemoved); // ElectionsPoliticians var selectedElectionsPoliticians = ElectionsPoliticians.GetDataByPoliticianKey(selectedKey); var unselectedElectionsPoliticians = ElectionsPoliticians.GetDataByPoliticianKey(unselectedKey); UpdateElectionsPoliticians(selectedKey, unselectedElectionsPoliticians, selectedElectionsPoliticians); // OfficesOfficials var selectedOfficesOfficials = OfficesOfficials.GetDataByPoliticianKey(selectedKey); var unselectedOfficesOfficials = OfficesOfficials.GetDataByPoliticianKey(unselectedKey); UpdateOfficesOfficials(selectedKey, unselectedOfficesOfficials, selectedOfficesOfficials); // Update everything as one transaction, politicians last PoliticiansImagesData.UpdateTable(selectedImagesData); PoliticiansImagesData.UpdateTable(unselectedImagesData); PoliticiansImagesBlobs.UpdateTable(selectedImagesBlobs); PoliticiansImagesBlobs.UpdateTable(unselectedImagesBlobs); Answers.UpdateTable(selectedAnswers); Answers.UpdateTable(unselectedAnswers); ElectionsIncumbentsRemoved.UpdateTable(unselectedIncumbentsRemoved); ElectionsPoliticians.UpdateTable(unselectedElectionsPoliticians); OfficesOfficials.UpdateTable(unselectedOfficesOfficials); Politicians.UpdateTable(selectedPolitician); Politicians.UpdateTable(unselectedPolitician); // Log LogDataChange.LogUpdate("*ConsolidatePoliticians", "*Various", unselectedKey, selectedKey, VotePage.UserName, SecurePage.UserSecurityClass, DateTime.UtcNow, selectedKey); // After the main update, refresh the LiveOfficeKey, LiveOfficeStatus and LiveElectionKey var view = PoliticiansLiveOfficeKeyView.GetData(selectedKey); if (view.Count == 1) { var keyAndStatus = PoliticianOfficeStatus.FromLiveOfficeKeyAndStatus( view[0].LiveOfficeKeyAndStatus); selectedPolitician[0].LiveOfficeKey = keyAndStatus.OfficeKey; selectedPolitician[0].LiveOfficeStatus = keyAndStatus.PoliticianStatus.ToString(); selectedPolitician[0].LiveElectionKey = keyAndStatus.ElectionKey; Politicians.UpdateTable(selectedPolitician); } ConsolidateReloaded.Value = "ok"; } catch (Exception ex) { FeedbackConsolidate.AddError("There was an unexpected error: " + ex.Message); } }
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); } }
protected void ButtonDeleteDistricts_OnClick(object sender, EventArgs e) { switch (DeleteDistrictsReloading.Value) { case "reloading": { DeleteDistrictOverride.AddCssClasses("hidden"); ControlDeleteDistrictOverride.Checked = false; _DeleteDistrictsTabInfo.ClearValidationErrors(); DeleteDistrictsReloading.Value = Empty; PopulateLocalDistrictDropdown(); _DeleteDistrictsTabInfo.LoadControls(); FeedbackDeleteDistricts.AddInfo("Delete Districts information loaded."); } break; case "": { // normal update DeleteDistrictOverride.AddCssClasses("hidden"); _DeleteDistrictsTabInfo.ClearValidationErrors(); var localKey = ControlDeleteDistrictsLocalKey.GetValue(); var localDistrict = LocalDistricts.GetLocalDistrictByStateCodeLocalKey(StateCode, localKey); var referenceList = new List <string>(); if (!ControlDeleteDistrictOverride.Checked) { // check for references in other counties var otherCounties = new List <string>(); foreach (var oc in LocalIdsCodes.GetOtherCountyReferences(StateCode, CountyCode, localKey) .Rows.OfType <DataRow>()) { otherCounties.Add(oc.County()); } // check for meaningful usages FormatReferences(referenceList, "Elections", Elections.CountByStateCodeLocalKey(StateCode, localKey)); FormatReferences(referenceList, "ElectionsOffices", ElectionsOffices.CountByStateCodeLocalKey(StateCode, localKey)); FormatReferences(referenceList, "ElectionsPoliticians", ElectionsPoliticians.CountByStateCodeLocalKey(StateCode, localKey)); FormatReferences(referenceList, "QuestionsJurisdictions", QuestionsJurisdictions.CountByIssueLevelStateCodeCountyOrLocal( Issues.IssueLevelLocal, StateCode, localKey)); FormatReferences(referenceList, "Offices", Offices.CountByStateCodeLocalKey(StateCode, localKey)); FormatReferences(referenceList, "OfficesOfficials", OfficesOfficials.CountByStateCodeLocalKey(StateCode, localKey)); FormatReferences(referenceList, "Referendums", Referendums.CountByStateCodeLocalKey(StateCode, localKey)); if (referenceList.Count > 0 || otherCounties.Count > 0) { DeleteDistrictOverride.RemoveCssClass("hidden"); if (otherCounties.Count > 0) { FeedbackDeleteDistricts.PostValidationError(ControlDeleteDistrictsLocalKey, "The district is referenced in the following other counties. If you delete this" + " district it will be removed from the other counties also. Check the box to" + " override."); foreach (var oc in otherCounties) { FeedbackDeleteDistricts.AddError(oc); } } if (referenceList.Count > 0) { FeedbackDeleteDistricts.PostValidationError(ControlDeleteDistrictsLocalKey, "Cannot delete because the LocalKey is referenced in the following tables. Check the box to override."); foreach (var @ref in referenceList) { FeedbackDeleteDistricts.AddError(@ref); } } return; } } // delete ControlDeleteDistrictOverride.Checked = false; DeleteAllDistictReferences(StateCode, localKey); LocalDistricts.DeleteByStateCodeLocalKey(StateCode, localKey); // delete TigerPlacesCounties rows if type Vote -- these are not intrinsically tied // to county(s) var localIdsCodes = LocalIdsCodes.GetDataByStateCodeLocalKey(StateCode, localKey); Debug.Assert(localIdsCodes.Count == 1); if (localIdsCodes[0].LocalType == LocalIdsCodes.LocalTypeVote) { TigerPlacesCounties.DeleteByStateCodeTigerTypeTigerCode(StateCode, LocalIdsCodes.LocalTypeVote, localIdsCodes[0].LocalId); } LocalIdsCodes.DeleteByStateCodeLocalKey(StateCode, localKey); _DeleteDistrictsTabInfo.ClearValidationErrors(); PopulateLocalDistrictDropdown(); _DeleteDistrictsTabInfo.LoadControls(); NavigateJurisdictionUpdatePanel.Update(); NavigateJurisdiction.Initialize(); FeedbackDeleteDistricts.AddInfo( $"Local District {localDistrict} ({localKey}) deleted."); } break; default: throw new VoteException( $"Unknown reloading option: '{DeleteDistrictsReloading.Value}'"); } }
private bool UpdateManageIncumbents(object newValue) { var officeKey = _ThisControl.SafeGetOfficeKey(); var newIncumbents = UpdateParse(newValue); // Get the current slate of incumbents for this office var currentIncumbentsTable = OfficesOfficials.GetDataByOfficeKey(officeKey); // If we process a row, we delete it from this list. What's left needs // to be deleted from the DB. var rowsToDelete = Enumerable.Select(currentIncumbentsTable, row => row) .ToList(); var stateCode = Offices.GetStateCodeFromKey(officeKey); var countyCode = Offices.GetCountyCodeFromKey(officeKey); var localCode = Offices.GetLocalCodeFromKey(officeKey); foreach (var incumbent in newIncumbents) { var currentRow = currentIncumbentsTable.FirstOrDefault( row => row.PoliticianKey.IsEqIgnoreCase(incumbent.PoliticianKey)); if (currentRow == null) { // new incumbent, add LogDataChange.LogInsert(OfficesOfficials.TableName, incumbent.RunningMateKey, DateTime.UtcNow, officeKey, incumbent.PoliticianKey); currentIncumbentsTable.AddRow(officeKey, incumbent.PoliticianKey, incumbent.RunningMateKey, stateCode, countyCode, localCode, Offices.GetDistrictCode(officeKey), /*String.Empty, VotePage.DefaultDbDate,*/ DateTime.UtcNow, SecurePage.AdminSecurityClass, VotePage.UserName); } else { // existing incumbent, update if necessary if (currentRow.RunningMateKey.IsNeIgnoreCase(incumbent.RunningMateKey)) { LogDataChange.LogUpdate(OfficesOfficials.Column.RunningMateKey, currentRow.RunningMateKey, incumbent.RunningMateKey, DateTime.UtcNow, officeKey, incumbent.PoliticianKey); currentRow.RunningMateKey = incumbent.RunningMateKey; } rowsToDelete.Remove(currentRow); } } foreach (var row in rowsToDelete) { LogDataChange.LogDelete(OfficesOfficials.TableName, DateTime.UtcNow, officeKey, row.PoliticianKey); row.Delete(); } // Update if any changes var incumbentListChanged = currentIncumbentsTable.FirstOrDefault( row => row.RowState != DataRowState.Unchanged) != null; if (incumbentListChanged) { OfficesOfficials.UpdateTable(currentIncumbentsTable); } LoadControl(); return(incumbentListChanged); }
private bool UpdateManageCandidates(object newValue) { var electionKey = _ThisControl.SafeGetElectionKey(); var officeKey = _ThisControl.SafeGetOfficeKey(); var newCandidates = UpdateParse(newValue); // Get the current slate of candidate for this election/office var currentCandidatesTable = ElectionsPoliticians.GetDataByElectionKeyOfficeKey(electionKey, officeKey); // Get the incumbent(s) for this office var incumbents = Enumerable.Select(OfficesOfficials.GetPoliticianKeysData(officeKey), row => row.PoliticianKey) .ToList(); // If we process a row, we delete it from this list. What's left needs // to be deleted from the DB. var rowsToDelete = Enumerable.Select(currentCandidatesTable, row => row) .ToList(); var orderOnBallot = 0; var federalCode = Offices.GetOfficeClass(officeKey) .StateCodeProxy(); var stateCode = Elections.GetStateCodeFromKey(electionKey); if (StateCache.IsValidFederalCode(stateCode, false)) { stateCode = string.Empty; } var countyCode = Elections.GetCountyCodeFromKey(electionKey); var localCode = Elections.GetLocalCodeFromKey(electionKey); var electionKeyFederal = string.IsNullOrWhiteSpace(federalCode) ? string.Empty : Elections.GetFederalElectionKeyFromKey(electionKey, federalCode); var electionKeyState = Elections.GetStateElectionKeyFromKey(electionKey); var electionKeyCounty = Elections.GetCountyElectionKeyFromKey(electionKey); var electionKeyLocal = Elections.GetLocalElectionKeyFromKey(electionKey); foreach (var candidate in newCandidates) { orderOnBallot += 10; var currentRow = currentCandidatesTable.FirstOrDefault( row => row.PoliticianKey.IsEqIgnoreCase(candidate.PoliticianKey)); if (currentRow == null) { // new candidate, add LogDataChange.LogInsert(ElectionsPoliticians.TableName, candidate.RunningMateKey, DateTime.UtcNow, electionKey, officeKey, candidate.PoliticianKey); currentCandidatesTable.AddRow(electionKey, officeKey, candidate.PoliticianKey, candidate.RunningMateKey, electionKeyState, electionKeyFederal, electionKeyCounty, electionKeyLocal, stateCode, countyCode, localCode, string.Empty, orderOnBallot, false, incumbents.Contains(candidate.PoliticianKey), false); } else { // existing candidate, update if necessary if (currentRow.RunningMateKey.IsNeIgnoreCase(candidate.RunningMateKey)) { LogDataChange.LogUpdate(ElectionsPoliticians.Column.RunningMateKey, currentRow.RunningMateKey, candidate.RunningMateKey, DateTime.UtcNow, electionKey, officeKey, candidate.PoliticianKey); currentRow.RunningMateKey = candidate.RunningMateKey; } if (currentRow.OrderOnBallot != orderOnBallot) { LogDataChange.LogUpdate(ElectionsPoliticians.Column.OrderOnBallot, currentRow.OrderOnBallot, orderOnBallot, DateTime.UtcNow, electionKey, officeKey, candidate.PoliticianKey); currentRow.OrderOnBallot = orderOnBallot; } rowsToDelete.Remove(currentRow); } } foreach (var row in rowsToDelete) { LogDataChange.LogDelete(ElectionsPoliticians.TableName, DateTime.UtcNow, electionKey, officeKey, row.PoliticianKey); row.Delete(); } // Update if any changes var candidateListChanged = currentCandidatesTable.FirstOrDefault( row => row.RowState != DataRowState.Unchanged) != null; if (candidateListChanged) { ElectionsPoliticians.UpdateTable(currentCandidatesTable); } LoadControl(); return(candidateListChanged); }
private void LoadControlManageIncumbents() { var officeKey = _ThisControl.SafeGetOfficeKey(); var table = OfficesOfficials.GetIncumbentsForOffice(officeKey); int incumbentsAllowed; var incumbents = table.Rows.Cast <DataRow>() // these are pre-sorted .Where(row => !row.IsRunningMate()) .ToList(); _ThisControl.PageFeedback.AddInfo( $"{incumbents.Count} incumbent{incumbents.Count.Plural()} loaded."); if (incumbents.Count == 0) { _ThisControl.Message.RemoveCssClass("hidden"); _ThisControl.Message.InnerHtml = "No incumbents were found for this office."; incumbentsAllowed = Offices.GetIncumbents(officeKey, 0); } else { _ThisControl.Message.AddCssClasses("hidden"); foreach (var incumbent in incumbents) { var li = new HtmlLi { ID = "candidate-" + incumbent.PoliticianKey(), ClientIDMode = ClientIDMode.Static }.AddTo( _ThisControl.ControlAddCandidatesCandidateList); var outerDiv = new HtmlDiv().AddTo(li, "outer shadow-2"); CreateCandidateEntry(incumbent, DataMode.ManageIncumbents) .AddTo(outerDiv); var runningMateKey = incumbent.RunningMateKey(); if (!incumbent.IsRunningMateOffice()) { continue; } DataRow runningMate = null; if (!string.IsNullOrWhiteSpace(runningMateKey)) { runningMate = table.Rows.Cast <DataRow>() .FirstOrDefault(row => row.PoliticianKey() .IsEqIgnoreCase(runningMateKey)); } if (runningMate == null) { CreateNoRunningMateEntry() .AddTo(outerDiv); } else { CreateCandidateEntry(runningMate, DataMode.ManageIncumbents, incumbent.PartyCode()) .AddTo(outerDiv); } } incumbentsAllowed = incumbents.First().Incumbents(); } _ThisControl.IncumbentCountMessage.InnerText = incumbentsAllowed == 1 ? "This office can have only a single incumbent" : $"This office can have {incumbentsAllowed} incumbents"; _ThisControl.IncumbentCountMessage.Visible = true; _ThisControl.IncumbentCount.Value = incumbentsAllowed.ToString(CultureInfo.InvariantCulture); _ThisControl.IncumbentCount.Visible = true; }