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