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