protected override bool Update(object newValue)
            {
                var changed = false;
                //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 officeSplit = officeStr.Split(':');
                    return
                    (new
                    {
                        OfficeKey = officeSplit[0],
                        Incumbents = officeSplit[1].Split(',')
                    });
                })
                              .ToArray();

                foreach (var office in offices)
                {
                    var table         = OfficesOfficials.GetDataByOfficeKey(office.OfficeKey);
                    var officeChanged = false;
                    foreach (var row in table)
                    {
                        if (!office.Incumbents.Contains(row.PoliticianKey))
                        {
                            if (!ElectionsIncumbentsRemoved.ElectionKeyOfficeKeyPoliticianKeyExists
                                    (Page.GetElectionKey(), row.OfficeKey, row.PoliticianKey))
                            {
                                ElectionsIncumbentsRemoved.Insert(Page.GetElectionKey(), row.OfficeKey,
                                                                  row.PoliticianKey, row.RunningMateKey, row.StateCode,
                                                                  row.CountyCode, row.LocalCode, row.DistrictCode,
                                                                  //row.LDSVersion, row.LDSUpdateDate,
                                                                  row.DataLastUpdated, row.UserSecurity,
                                                                  row.UserName);
                            }
                            row.Delete();
                            officeChanged = true;
                        }
                    }
                    if (!officeChanged)
                    {
                        continue;
                    }
                    OfficesOfficials.UpdateTable(table);
                    changed = true;
                }

                //LoadControl();
                return(changed);
            }
Exemple #2
0
            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 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);
            }
        }
Exemple #4
0
            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);
            }