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); }
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); }
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 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 officeSplit = officeStr.Split('='); var isRunoff = officeSplit[1].StartsWith("*", StringComparison.Ordinal); if (isRunoff) { officeSplit[1] = officeSplit[1].Substring(1); } return(new { 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; } } } // 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 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); }