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); } }
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); }