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 void ChangeOfficeKey() { try { var oldKeyItem = _MasterOnlyTabInfo.Single(i => i.Column == "OldKey"); var newKeyItem = _MasterOnlyTabInfo.Single(i => i.Column == "NewKey"); var jurisdictionKey = JurisdictionalKey; var success = true; success &= DataItemBase.ValidateRequired(oldKeyItem); var oldKeyOffice = oldKeyItem.DataControl.GetValue().Trim(); success &= DataItemBase.ValidateRequired(newKeyItem); var newKeyOffice = newKeyItem.DataControl.GetValue().Trim(); if (!string.IsNullOrWhiteSpace(newKeyOffice)) { // get rid of all non-alphanumerics newKeyOffice = Regex.Replace(newKeyOffice, @"[^\dA-Z]", string.Empty, RegexOptions.IgnoreCase); // get rid of leading numerics newKeyOffice = Regex.Replace(newKeyOffice, @"^\d+", string.Empty); var maxLength = Offices.OfficeKeyMaxLength - jurisdictionKey.Length; if (newKeyOffice.Length > maxLength) { newKeyItem.Feedback.PostValidationError(newKeyItem.DataControl, newKeyItem.Description + " is too long by " + (newKeyOffice.Length - maxLength) + " characters."); success = false; } if (newKeyOffice.Length == 0) { newKeyItem.Feedback.PostValidationError(newKeyItem.DataControl, newKeyItem.Description + " consists entirely of non-key characters."); success = false; } } if (success && (oldKeyOffice == newKeyOffice)) { newKeyItem.Feedback.PostValidationError(newKeyItem.DataControl, newKeyItem.Description + " is identical to the Old Office Key."); success = false; } var oldOfficeKey = jurisdictionKey + oldKeyOffice; var newOfficeKey = jurisdictionKey + newKeyOffice; var caseChangeOnly = oldOfficeKey.IsEqIgnoreCase(newOfficeKey); if (success && !caseChangeOnly) { // Make sure the new office key doesn't already exist var existsInTables = new List <string>(); if (Offices.OfficeKeyExists(newOfficeKey)) { existsInTables.Add(Offices.TableName); } if (ElectionsOffices.OfficeKeyExists(newOfficeKey)) { existsInTables.Add(ElectionsOffices.TableName); } if (ElectionsPoliticians.OfficeKeyExists(newOfficeKey)) { existsInTables.Add(ElectionsPoliticians.TableName); } if (OfficesOfficials.OfficeKeyExists(newOfficeKey)) { existsInTables.Add(OfficesOfficials.TableName); } if (ElectionsIncumbentsRemoved.OfficeKeyExists(newOfficeKey)) { existsInTables.Add(ElectionsIncumbentsRemoved.TableName); } if (Politicians.OfficeKeyExists(newOfficeKey)) { existsInTables.Add(Politicians.TableName); } if (existsInTables.Count > 0) { newKeyItem.Feedback.PostValidationError(newKeyItem.DataControl, newKeyItem.Description + " already exists in the following tables: " + string.Join(", ", existsInTables)); success = false; } } if (!success) { return; } // do the replacement var updateCount = 0; updateCount += Offices.UpdateOfficeKey(newOfficeKey, oldOfficeKey); updateCount += ElectionsOffices.UpdateOfficeKeyByOfficeKey(newOfficeKey, oldOfficeKey); updateCount += ElectionsPoliticians.UpdateOfficeKeyByOfficeKey(newOfficeKey, oldOfficeKey); updateCount += OfficesOfficials.UpdateOfficeKeyByOfficeKey(newOfficeKey, oldOfficeKey); updateCount += ElectionsIncumbentsRemoved.UpdateOfficeKeyByOfficeKey(newOfficeKey, oldOfficeKey); updateCount += Politicians.UpdateOfficeKeyByOfficeKey(newOfficeKey, oldOfficeKey); var msg = $"{updateCount} instances of the old office key {oldOfficeKey} were found."; if (updateCount > 0) { msg += $" They were all changed to the new office key {newOfficeKey}."; } FeedbackMasterOnly.AddInfo(msg); ResetMasterOnlySubTab(MasterOnlySubTab.ChangeKey); } catch (Exception ex) { FeedbackMasterOnly.PostValidationError(ControlMasterOnlyNewKey, "The office key could not be changed: " + ex.Message); } }