Esempio n. 1
0
        private void UpdateLockedClasses()
        {
            // Get all OfficesAllIdentified rows for the state and make a dictionary of
            // only the true values
            var dictionary = OfficesAllIdentified.GetDataByStateCode(StateCode)
                             .Where(row => row.IsOfficesAllIdentified)
                             .ToDictionary(row => row.OfficeLevel.ToOfficeClass(),
                                           row => null as object);

            // now get any items that don't match the dictionary
            var items = ControlMasterOnlyClassesToLock.Items.OfType <ListItem>()
                        .Where(
                item =>
                dictionary.ContainsKey(Offices.GetValidatedOfficeClass(item.Value)) !=
                item.Selected);

            foreach (var item in items)
            {
                var officeClass = Offices.GetValidatedOfficeClass(item.Value);
                OfficesAllIdentified.UpdateIsOfficesAllIdentified(item.Selected, StateCode,
                                                                  officeClass.ToInt());
            }

            FeedbackMasterOnly.AddInfo("The locked office classes were updated");
        }
Esempio n. 2
0
        private void CopyPrimaryWinners()
        {
            try
            {
                var candidatesAdded     = 0;
                var candidateDuplicates = 0;
                var generalElectionKey  = GetElectionKey();
                if (Elections.IsPrimaryElection(generalElectionKey))
                {
                    throw new VoteException("This function may not be used with primary elections.");
                }
                var generalElectionDate = Elections.GetElectionDateFromKey(generalElectionKey);
                var primaryElectionDate =
                    FeedbackMasterOnly.ValidateDate(ControlMasterOnlyPrimaryDateToCopy, out var success,
                                                    "Primary Election Date", generalElectionDate.AddYears(-1),
                                                    generalElectionDate.AddDays(-7));
                if (!success)
                {
                    return;
                }
                var runoffElectionDate =
                    FeedbackMasterOnly.ValidateDateOptional(ControlMasterOnlyRunoffDateToCopy,
                                                            out success, "Runoff Election Date", generalElectionDate.AddYears(-1),
                                                            generalElectionDate.AddDays(-7), DefaultDbDate);
                if (!success)
                {
                    return;
                }

                // for state elections we now copy for the whole ElectionKeyFamily (includes county and local)
                var family = Elections.GetVirtualElectionKeyFamily(generalElectionKey) ??
                             new List <string> {
                    generalElectionKey
                };

                foreach (var electionKey in family)
                {
                    MergePrimaryWinners(electionKey, primaryElectionDate, false,
                                        ref candidateDuplicates,
                                        ref candidatesAdded);
                    if (!runoffElectionDate.IsDefaultDate())
                    {
                        MergePrimaryWinners(electionKey, runoffElectionDate, true,
                                            ref candidateDuplicates,
                                            ref candidatesAdded);
                    }
                }

                FeedbackMasterOnly.AddInfo(
                    $"{candidatesAdded} candidates were added, {candidateDuplicates} were duplicates.");
            }
            catch (Exception ex)
            {
                FeedbackMasterOnly.PostValidationError(ControlMasterOnlyPrimaryDateToCopy,
                                                       "The candidates could not be copied: " + ex.Message);
            }
        }
Esempio n. 3
0
 private void CopyRunoffAdvancers()
 {
     try
     {
         bool success;
         var  candidatesAdded   = 0;
         var  officesAdded      = 0;
         var  runoffElectionKey = GetElectionKey();
         if (!Elections.IsRunoffElection(runoffElectionKey))
         {
             throw new VoteException("This function may only be used with runoff elections.");
         }
         var runoffElectionDate =
             Elections.GetElectionDateFromKey(runoffElectionKey);
         var previousElectionDate = FeedbackMasterOnly.ValidateDate(
             ControlMasterOnlyElectionDateToCopy, out success,
             "Previous Election Date", runoffElectionDate.AddYears(-1),
             runoffElectionDate.AddDays(-7));
         if (!success)
         {
             return;
         }
         var offices =
             ElectionsPoliticians.GetRunoffAdvancersForElection(
                 runoffElectionKey, previousElectionDate)
             .GroupBy(r => r.OfficeKey);
         ElectionsOffices.DeleteByElectionKey(runoffElectionKey);
         ElectionsPoliticians.DeleteByElectionKey(runoffElectionKey);
         foreach (var o in offices)
         {
             var office = o.First();
             InsertElectionOffice(runoffElectionKey, office.OfficeKey);
             officesAdded++;
             foreach (var c in o)
             {
                 ElectionsPoliticians.Insert(runoffElectionKey, c.OfficeKey,
                                             c.PoliticianKey, c.RunningMateKey,
                                             Elections.GetStateElectionKeyFromKey(runoffElectionKey),
                                             string.Empty,
                                             Elections.GetCountyElectionKeyFromKey(runoffElectionKey),
                                             Elections.GetLocalElectionKeyFromKey(runoffElectionKey), c.StateCode,
                                             c.CountyCode, c.LocalCode, c.DistrictCode, 0, false,
                                             OfficesOfficials.OfficeKeyPoliticianKeyExists(c.OfficeKey,
                                                                                           c.PoliticianKey), false);
                 candidatesAdded++;
             }
         }
         FeedbackMasterOnly.AddInfo(
             $"{candidatesAdded} candidates were added, {officesAdded} offices were added.");
     }
     catch (Exception ex)
     {
         FeedbackMasterOnly.PostValidationError(ControlMasterOnlyPrimaryDateToCopy,
                                                "The candidates could not be copied: " + ex.Message);
     }
 }
Esempio n. 4
0
        protected void ButtonMasterOnly_OnClick(object sender, EventArgs e)
        {
            switch (MasterOnlyReloading.Value)
            {
            case "reloading":
            {
                MasterOnlyReloading.Value = string.Empty;
                //      ControlMasterOnlyElectionDesc.Enabled = false;
                //      MasterOnlyDateWasChanged.Value = String.Empty;
                _MasterOnlyTabInfo.LoadControls();
                //      SetElectionHeading(HeadingMasterOnly);
                FeedbackMasterOnly.AddInfo("Master-only data loaded.");
            }
            break;

            case "":
            {
                // normal update
                int subTabIndex;
                _MasterOnlyTabInfo.ClearValidationErrors();
                if (int.TryParse(ContainerMasterOnlySubTabIndex.Value, out subTabIndex))
                {
                    switch (_MasterOnlySubTabs[subTabIndex])
                    {
                    case MasterOnlySubTab.LockClasses:
                        UpdateLockedClasses();
                        break;

                    case MasterOnlySubTab.ChangeKey:
                        ChangeOfficeKey();
                        break;

                    case MasterOnlySubTab.Consolidate:
                        Consolidate();
                        break;
                    }
                }
            }
            break;

            default:
                throw new VoteException("Unknown reloading option");
            }
        }
Esempio n. 5
0
 private void ChangeSelectedElectionDate(DateTime newElectionDate)
 {
     // Only for state elections -- changes county and local elections too
     try
     {
         var newElectionKey = Elections.ChangeElectionDate(GetElectionKey(),
                                                           newElectionDate, 0);
         FeedbackMasterOnly.AddInfo("The election date was changed");
         SelectedElectionKey.Value = newElectionKey;
         SetElectionHeading(HeadingMasterOnly);
         ReloadElectionControl();
         ControlMasterOnlyElectionDesc.Enabled = true;
         MasterOnlyDateWasChanged.Value        = "true";
     }
     catch (Exception ex)
     {
         FeedbackMasterOnly.PostValidationError(ControlMasterOnlyElectionDate,
                                                "The election date could not be changed: " + ex.Message);
     }
 }
Esempio n. 6
0
        protected void ButtonMasterOnly_OnClick(object sender, EventArgs e)
        {
            switch (MasterOnlyReloading.Value)
            {
            case "reloading":
            {
                MasterOnlyReloading.Value = Empty;
                _MasterOnlyTabInfo.LoadControls();
                FeedbackMasterOnly.AddInfo("Master-only data loaded.");
            }
            break;

            case "":
            {
                // normal update
                _MasterOnlyTabInfo.ClearValidationErrors();
                if (int.TryParse(ContainerMasterOnlySubTabIndex.Value, out var subTabIndex))
                {
                    switch (_MasterOnlySubTabs[subTabIndex])
                    {
                    case MasterOnlySubTab.LockClasses:
                        // obsolete
                        break;

                    case MasterOnlySubTab.ChangeKey:
                        ChangeOfficeKey();
                        break;

                    case MasterOnlySubTab.Consolidate:
                        Consolidate();
                        break;
                    }
                }
            }
            break;

            default:
                throw new VoteException($"Unknown reloading option: '{MasterOnlyReloading.Value}'");
            }
        }
Esempio n. 7
0
 private void DeleteSelectedElection()
 {
     try
     {
         var electionKey = GetElectionKey();
         if (Elections.IsStateElection(electionKey))
         {
             Elections.DeleteElectionFamily(electionKey);
         }
         else
         {
             Elections.DeleteElection(electionKey);
         }
         FeedbackMasterOnly.AddInfo("The election was deleted");
         ControlMasterOnlyDeleteElection.Checked = false;
         SelectedElectionKey.Value = string.Empty;
         ReloadElectionControl();
     }
     catch (Exception ex)
     {
         FeedbackMasterOnly.PostValidationError(ControlMasterOnlyDeleteElection,
                                                "The election could not be deleted: " + ex.Message);
     }
 }
Esempio n. 8
0
        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);
            }
        }
Esempio n. 9
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);
            }
        }
Esempio n. 10
0
        protected void ButtonMasterOnly_OnClick(object sender, EventArgs e)
        {
            switch (MasterOnlyReloading.Value)
            {
            case "reloading":
            {
                MasterOnlyReloading.Value             = string.Empty;
                ControlMasterOnlyElectionDesc.Enabled = false;
                MasterOnlyDateWasChanged.Value        = string.Empty;
                var electionKey = GetElectionKey();
                ControlMasterOnlyElectionKeyToInclude.Items.Clear();
                ControlMasterOnlyElectionKeyToInclude.AddItem("<No election included>", " ");
                if (!string.IsNullOrWhiteSpace(electionKey))
                {
                    var table = Elections.GetElectionsOnSameDate(electionKey)
                                .Where(r => r.ElectionKey != electionKey)
                                .OrderBy(r => r.ElectionDesc);
                    foreach (var row in table)
                    {
                        ControlMasterOnlyElectionKeyToInclude.AddItem(row.ElectionDesc,
                                                                      row.ElectionKey);
                    }
                }
                if (!Elections.IsPrimaryElection(electionKey))
                {
                    // set the date to the most recent primary
                    var date = Elections.GetPrimaryDateForGeneralElection(electionKey);
                    if (date != DefaultDbDate)
                    {
                        HiddenMasterOnlyPrimaryDateToCopy.Value = date.ToString("MM/dd/yyyy");
                    }
                }
                if (Elections.IsRunoffElection(electionKey))
                {
                    // set the date to the most recent matching election on type and party
                    var date = Elections.GetElectionDateForRunoffElection(electionKey);
                    if (date != DefaultDbDate)
                    {
                        HiddenMasterOnlyElectionDateToCopy.Value = date.ToString("MM/dd/yyyy");
                    }
                }
                SetElectionHeading(HeadingMasterOnly);
                _MasterOnlyTabInfo.LoadControls();
                FeedbackMasterOnly.AddInfo("Master-only data loaded.");
            }
            break;

            case "":
            {
                // normal update
                int subTabIndex;
                _MasterOnlyTabInfo.ClearValidationErrors();
                if (int.TryParse(ContainerMasterOnlySubTabIndex.Value, out subTabIndex))
                {
                    switch (_MasterOnlySubTabs[subTabIndex])
                    {
                    case MasterOnlySubTab.ChangeDate:
                        bool success;
                        var  newElectionDate =
                            FeedbackMasterOnly.ValidateDate(ControlMasterOnlyElectionDate,
                                                            out success, "Election Date");
                        if (!success)
                        {
                            return;
                        }
                        var oldElectionDate =
                            Elections.GetElectionDateFromKey(GetElectionKey());
                        if (oldElectionDate != newElectionDate)
                        {
                            ChangeSelectedElectionDate(newElectionDate);
                            _MasterOnlyTabInfo.LoadControls();
                        }
                        else
                        {
                            _MasterOnlyTabInfo.Update(FeedbackMasterOnly);
                            SetElectionHeading(HeadingMasterOnly);
                            ReloadElectionControl();
                        }
                        break;

                    case MasterOnlySubTab.CopyCandidates:
                        CopyCandidates();
                        break;

                    case MasterOnlySubTab.CopyPrimaryWinners:
                        CopyPrimaryWinners();
                        break;

                    case MasterOnlySubTab.CopyRunoffAdvancers:
                        CopyRunoffAdvancers();
                        break;

                    case MasterOnlySubTab.DeleteElection:
                        DeleteSelectedElection();
                        break;

                    case MasterOnlySubTab.CreateGeneral:
                        CreateGeneralElection();
                        break;

                    case MasterOnlySubTab.IncludeElection:
                    case MasterOnlySubTab.StatusNotes:
                        _MasterOnlyTabInfo.Where(
                            tab => tab.SubTab == _MasterOnlySubTabs[subTabIndex])
                        .Update(FeedbackMasterOnly);
                        break;
                    }
                }
            }
            break;

            default:
                throw new VoteException("Unknown reloading option");
            }
        }
Esempio n. 11
0
        private void CreateGeneralElection()
        {
            try
            {
                bool success;
                var  electionDate =
                    ValidateElectionDate(ControlMasterOnlyGeneralElectionDate,
                                         FeedbackMasterOnly, ControlMasterOnlyGeneralPastElection.Checked,
                                         out success);
                if (!success)
                {
                    return;
                }

                var existingElections =
                    Elections.GetStateGeneralElectionsByDate(electionDate);
                var existingStates =
                    existingElections.Select(Elections.GetStateCodeFromKey)
                    .OrderBy(code => code)
                    .ToList();
                var includePresident     = ControlMasterOnlyGeneralIncludePresident.Checked;
                var electionDescTemplate = ControlMasterOnlyGeneralElectionDesc.GetValue()
                                           .Trim();
                if (string.IsNullOrWhiteSpace(electionDescTemplate))
                {
                    electionDescTemplate =
                        Elections.GetGeneralElectionDescriptionTemplate(electionDate);
                }

                var electionsTable         = new ElectionsTable();
                var electionsOfficesTable  = new ElectionsOfficesTable();
                var generalElectionOffices = Offices.GetGeneralElectionOffices();

                var statesCreated = 0;

                // create the elections and (optionally) the presidential contests
                foreach (
                    var code in StateCache.All51StateCodes.Union(StateCache.AllFederalCodes))
                {
                    if (!existingStates.Contains(code))
                    {
                        var electionKey = Elections.FormatElectionKey(electionDate,
                                                                      Elections.ElectionTypeGeneralElection, Parties.NationalPartyAll, code);
                        var electionDesc =
                            GetElectionDescriptionFromTemplate(electionDescTemplate, code,
                                                               electionDate);

                        if (includePresident || !StateCache.IsUSPresident(code))
                        {
                            InsertElection(electionsTable, electionKey, null, electionDesc);
                            statesCreated++;
                        }

                        if (includePresident && StateCache.IsValidStateCode(code))
                        {
                            InsertOffice(electionsOfficesTable, electionKey, Offices.USPresident,
                                         OfficeClass.USPresident, string.Empty);
                        }
                    }
                }

                // add all the offices
                foreach (var row in generalElectionOffices)
                {
                    var stateCode = Offices.GetStateCodeFromKey(row.OfficeKey);
                    if (StateCache.IsValidStateCode(stateCode) && !existingStates.Contains(stateCode))
                    {
                        InsertOffice(electionsOfficesTable,
                                     Elections.FormatElectionKey(electionDate,
                                                                 Elections.ElectionTypeGeneralElection, Parties.NationalPartyAll,
                                                                 stateCode), row.OfficeKey, row.OfficeLevel.ToOfficeClass(),
                                     row.DistrictCode);
                    }
                }

                Elections.UpdateElectionsAndOffices(electionsTable, electionsOfficesTable,
                                                    null);

                ReloadElectionControl();
                var msg =
                    $"General elections were created for {statesCreated} states and pseudo-states.";
                if (existingStates.Count > 0)
                {
                    msg +=
                        $" There were already general elections on this date for the following states: {string.Join(", ", existingStates)}.";
                }
                FeedbackMasterOnly.AddInfo(msg);
                ResetMasterOnlySubTab(MasterOnlySubTab.CreateGeneral);
            }
            catch (Exception ex)
            {
                FeedbackMasterOnly.PostValidationError(
                    ControlMasterOnlyGeneralElectionDate,
                    "The general election could not be created: " + ex.Message);
            }
        }
Esempio n. 12
0
        private void CopyPrimaryWinners()
        {
            try
            {
                bool success;
                var  candidatesAdded = 0;
                //var candidatesSkippedOfficeMissing = 0;
                var candidateDuplicates = 0;
                var generalElectionKey  = GetElectionKey();
                if (Elections.IsPrimaryElection(generalElectionKey))
                {
                    throw new VoteException("This function may not be used with primary elections.");
                }
                var generalElectionDate =
                    Elections.GetElectionDateFromKey(generalElectionKey);
                var primaryElectionDate = FeedbackMasterOnly.ValidateDate(
                    ControlMasterOnlyPrimaryDateToCopy, out success,
                    "Primary Election Date", generalElectionDate.AddYears(-1),
                    generalElectionDate.AddDays(-7));
                if (!success)
                {
                    return;
                }
                var runoffElectionDate = FeedbackMasterOnly.ValidateDateOptional(
                    ControlMasterOnlyRunoffDateToCopy, out success,
                    "Runoff Election Date", generalElectionDate.AddYears(-1),
                    generalElectionDate.AddDays(-7), DefaultDbDate);
                if (!success)
                {
                    return;
                }

                // for state elections we now copy for the whole ElectionKeyFamily (includes county and local)
                //var family = Elections.GetElectionKeyFamily(generalElectionKey) ??
                //  new List<string> { generalElectionKey };
                var family = Elections.GetVirtualElectionKeyFamily(generalElectionKey) ??
                             new List <string> {
                    generalElectionKey
                };

                foreach (var electionKey in family)
                {
                    MergePrimaryWinners(electionKey, primaryElectionDate, false,
                                        ref candidateDuplicates, ref candidatesAdded /*, ref candidatesSkippedOfficeMissing*/);
                    if (runoffElectionDate != DefaultDbDate)
                    {
                        MergePrimaryWinners(electionKey, runoffElectionDate, true,
                                            ref candidateDuplicates, ref candidatesAdded /*, ref candidatesSkippedOfficeMissing*/);
                    }
                }

                //MergePrimaryWinners(generalElectionKey, primaryElectionDate, false,
                //  ref candidateDuplicates, ref candidatesAdded/*, ref candidatesSkippedOfficeMissing*/);
                //if (runoffElectionDate != DefaultDbDate)
                //  MergePrimaryWinners(generalElectionKey, runoffElectionDate, true,
                //    ref candidateDuplicates, ref candidatesAdded/*, ref candidatesSkippedOfficeMissing*/);

                FeedbackMasterOnly.AddInfo(
                    //String.Format("{0} candidates were added, {1} were duplicates, {2} were skipped because the office was not found.",
                    $"{candidatesAdded} candidates were added, {candidateDuplicates} were duplicates.");
            }
            catch (Exception ex)
            {
                FeedbackMasterOnly.PostValidationError(ControlMasterOnlyPrimaryDateToCopy,
                                                       "The candidates could not be copied: " + ex.Message);
            }
        }
Esempio n. 13
0
        private void CopyCandidates()
        {
            var candidatesCopied = 0;
            var officesCopied    = 0;
            var officesSkipped   = 0;

            try
            {
                //if (ElectionsPoliticians.CountByElectionKey(GetElectionKey()) > 0)
                //  throw new VoteException(
                //    "This function can only be used if no candidates have been added for the election.");
                var offices = ElectionsOffices.GetDataByElectionKey(GetElectionKey());
                if (offices.Count == 0)
                {
                    throw new VoteException(
                              "There are no offices for this election to copy. Use the Add/Remove Offices tab to add the offices you want to copy.");
                }
                var electionKeyToCopy = MasterOnlyElectionKeyToCopy.Value;
                foreach (var office in offices)
                {
                    if (
                        ElectionsPoliticians.CountByElectionKeyOfficeKey(GetElectionKey(),
                                                                         office.OfficeKey) > 0)
                    {
                        officesSkipped++;
                        continue;
                    }
                    var oldPoliticians =
                        ElectionsPoliticians.GetDataByElectionKeyOfficeKey(electionKeyToCopy,
                                                                           office.OfficeKey);
                    if (oldPoliticians.Count == 0)
                    {
                        continue;
                    }
                    officesCopied++;
                    candidatesCopied += oldPoliticians.Count;
                    var newPoliticians = new ElectionsPoliticiansTable();
                    foreach (var oldPolitician in oldPoliticians)
                    {
                        newPoliticians.AddRow(office.ElectionKey, office.OfficeKey,
                                              oldPolitician.PoliticianKey, oldPolitician.RunningMateKey,
                                              office.ElectionKeyState, office.ElectionKeyFederal,
                                              office.ElectionKeyCounty, office.ElectionKeyLocal, office.StateCode,
                                              office.CountyCode, office.LocalCode, office.DistrictCode,
                                              oldPolitician.OrderOnBallot, false,
                                              OfficesOfficials.OfficeKeyPoliticianKeyExists(office.OfficeKey,
                                                                                            oldPolitician.PoliticianKey), false);
                    }
                    ElectionsPoliticians.UpdateTable(newPoliticians);
                }
                FeedbackMasterOnly.AddInfo(
                    officesCopied > 0
            ? $"{candidatesCopied} candidates were copied for {officesCopied} offices."
            : "There were no candidates that could be copied.");
                if (officesSkipped > 0)
                {
                    FeedbackMasterOnly.AddInfo(
                        $"{officesSkipped} offices were skipped because there were already candidates entered.");
                }
            }
            catch (Exception ex)
            {
                FeedbackMasterOnly.PostValidationError(ControlMasterOnlyElectionToCopy,
                                                       "The candidates could not be copied: " + ex.Message);
            }
        }