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); }
private void Consolidate() { try { var key1Item = _MasterOnlyTabInfo.Single(i => i.Column == "Key1"); var key2Item = _MasterOnlyTabInfo.Single(i => i.Column == "Key2"); var jurisdictionKey = JurisdictionalKey; var success = true; success &= DataItemBase.ValidateRequired(key1Item); var key1Office = key1Item.DataControl.GetValue().Trim(); success &= DataItemBase.ValidateRequired(key2Item); var key2Office = key2Item.DataControl.GetValue().Trim(); if (success && key1Office.IsEqIgnoreCase(key2Office)) { key2Item.Feedback.PostValidationError(key2Item.DataControl, key2Item.Description + " is identical to " + key1Item.Description); success = false; } var officeKey1 = jurisdictionKey + key1Office; var officeKey2 = jurisdictionKey + key2Office; if (!success) { return; } // do the consolidation var updateCount = 0; if (Offices.OfficeKeyExists(officeKey1)) { updateCount += Offices.DeleteByOfficeKey(officeKey2); } else { updateCount += Offices.UpdateOfficeKey(officeKey1, officeKey2); } foreach (var row in ElectionsOffices.GetDataByOfficeKey(officeKey2)) { if (ElectionsOffices.ElectionKeyOfficeKeyExists(row.ElectionKey, officeKey1)) { updateCount += ElectionsOffices.DeleteByElectionKeyOfficeKey(row.ElectionKey, officeKey2); } else { updateCount += ElectionsOffices.UpdateOfficeKeyByElectionKeyOfficeKey(officeKey1, row.ElectionKey, officeKey2); } } foreach (var row in ElectionsPoliticians.GetDataByOfficeKey(officeKey2)) { if (ElectionsPoliticians.ElectionKeyOfficeKeyPoliticianKeyExists(row.ElectionKey, officeKey1, row.PoliticianKey)) { updateCount += ElectionsPoliticians.DeleteByElectionKeyOfficeKeyPoliticianKey( row.ElectionKey, officeKey2, row.PoliticianKey); } else { updateCount += ElectionsPoliticians.UpdateOfficeKeyByElectionKeyOfficeKeyPoliticianKey( officeKey1, row.ElectionKey, officeKey2, row.PoliticianKey); } } foreach (var row in OfficesOfficials.GetDataByOfficeKey(officeKey2)) { if (OfficesOfficials.OfficeKeyPoliticianKeyExists(officeKey1, row.PoliticianKey)) { updateCount += OfficesOfficials.DeleteByOfficeKeyPoliticianKey(officeKey2, row.PoliticianKey); } else { updateCount += OfficesOfficials.UpdateOfficeKeyByOfficeKeyPoliticianKey(officeKey1, officeKey2, row.PoliticianKey); } } foreach (var row in ElectionsIncumbentsRemoved.GetDataByOfficeKey(officeKey2)) { if (ElectionsIncumbentsRemoved.ElectionKeyOfficeKeyPoliticianKeyExists(row.ElectionKey, officeKey1, row.PoliticianKey)) { updateCount += ElectionsIncumbentsRemoved.DeleteByElectionKeyOfficeKeyPoliticianKey( row.ElectionKey, officeKey2, row.PoliticianKey); } else { updateCount += ElectionsIncumbentsRemoved .UpdateOfficeKeyByElectionKeyOfficeKeyPoliticianKey( officeKey1, row.ElectionKey, officeKey2, row.PoliticianKey); } } updateCount += Politicians.UpdateOfficeKeyByOfficeKey(officeKey1, officeKey2); var msg = $"{updateCount} instances of the second office key {officeKey2} were found."; if (updateCount > 0) { msg += $" They were all changed to the first office key {officeKey1}."; } FeedbackMasterOnly.AddInfo(msg); ResetMasterOnlySubTab(MasterOnlySubTab.Consolidate); } catch (Exception ex) { FeedbackMasterOnly.PostValidationError(ControlMasterOnlyNewKey, "The office keys could not be consolidated: " + ex.Message); } }
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); }