Exemple #1
0
            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);
            }