Esempio n. 1
0
        private static int GetCountyElections(Control container, string stateElectionKey,
                                              ReportLevel reportLevel)
        {
            var stateCode = Elections.GetStateCodeFromKey(stateElectionKey);

            // We get a dictionary of counties with elections that match the stateElectionKey
            // Key: countyCode; Value: countyElectionKey
            // Counties without an election will not be in the dictionary
            // We include local elections too, to account for situations where there is no county
            // election but there are local elections.
            var countyElectionDictionary =
                ElectionsOffices.GetCountyAndLocalElections(stateElectionKey);
            // We can't forget the Ballot Measures...
            var countyReferendumDictionary =
                Referendums.GetCountyAndLocalElections(stateElectionKey);

            // merge them into the first dictionary
            foreach (var kvp in countyReferendumDictionary)
            {
                if (!countyElectionDictionary.ContainsKey(kvp.Key))
                {
                    countyElectionDictionary.Add(kvp.Key, kvp.Value);
                }
            }
            if (countyElectionDictionary.Count == 0)
            {
                return(0);
            }

            // For reporting we start with all counties for the state (it will be in
            // order by county name) and select only those in the election dictionary.
            var counties = CountyCache.GetCountiesByState(stateCode)
                           .Where(countyElectionDictionary.ContainsKey).ToList();

            var multiCountySection = new PlaceHolder();

            multiCountySection.AddTo(container);

            var winnersNotIdentified = 0;

            // each county report is in its own accordion
            foreach (var countyCode in counties)
            {
                var countyName        = CountyCache.GetCountyName(stateCode, countyCode);
                var countyElectionKey = countyElectionDictionary[countyCode];

                var electionReport = new AdminElectionReport();
                var countyReport   = electionReport.GenerateReport(countyElectionKey,
                                                                   reportLevel, multiCountySection);

                for (var inx = 0; inx < countyReport.Controls.Count; inx += 2)
                {
                    // this will always be a state report (for county reports, GenerateReport
                    // is called directly. Inject the county name into the office title header for context
                    var header = countyReport.Controls[inx] as HtmlContainerControl;
                    header?.Controls.Add(
                        new HtmlP {
                        InnerText = countyName
                    }.AddCssClasses("county-message"));
                }

                // move them to the content
                while (countyReport.Controls.Count > 0)
                {
                    container.Controls.Add(countyReport.Controls[0]);
                }

                winnersNotIdentified += electionReport.WinnersNotIdentified;
            }

            return(winnersNotIdentified);
        }
Esempio n. 2
0
        private static int GetLocalElections(Control container, string countyElectionKey,
                                             ReportLevel reportLevel, Control multiCountySection = null)
        {
            var stateCode        = Elections.GetStateCodeFromKey(countyElectionKey);
            var countyCode       = Elections.GetCountyCodeFromKey(countyElectionKey);
            var stateElectionKey = Elections.GetStateElectionKeyFromKey(countyElectionKey);

            // We get a dictionary of locals with elections that match the stateElectionKey
            // Key: localKey; Value: local electionKey
            // Locals without an election will not be in the dictionary
            var localElectionDictionary =
                ElectionsOffices.GetLocalElections(stateElectionKey, countyCode);
            // We can't forget the Ballot Measures...
            var localReferendumDictionary =
                Referendums.GetLocalElections(stateElectionKey, countyCode);

            // merge them into the first dictionary
            foreach (var kvp in localReferendumDictionary)
            {
                if (!localElectionDictionary.ContainsKey(kvp.Key))
                {
                    localElectionDictionary.Add(kvp.Key, kvp.Value);
                }
            }
            if (localElectionDictionary.Count == 0)
            {
                return(0);
            }

            // We also get a dictionary of all local names for the county
            var localNamesDictionary = LocalDistricts.GetNamesDictionary(stateCode, countyCode);

            // For reporting we filter only locals with elections and sort by name,
            var locals = localNamesDictionary
                         .Where(kvp => localElectionDictionary.ContainsKey(kvp.Key))
                         .OrderBy(kvp => kvp.Value, new AlphanumericComparer()).ToList();

            // Get all counties for the locals NB: the counties are pre-sorted by county name
            var countiesForLocals =
                LocalIdsCodes.FindCountiesWithNames(stateCode, locals.Select(kvp => kvp.Key));

            var winnersNotIdentified = 0;

            foreach (var kvp in locals)
            {
                var localKey         = kvp.Key;
                var localName        = kvp.Value;
                var localElectionKey = localElectionDictionary[localKey];
                var countiesForLocal = countiesForLocals[localKey];

                var electionReport = new AdminElectionReport();
                var localReport    = electionReport.GenerateReport(localElectionKey);

                // this will be either a state or county report (for local reports, GenerateReport
                // is called directly)

                // for either county or state, inject the name of the local district into the office
                for (var inx = 0; inx < localReport.Controls.Count; inx += 2)
                {
                    var header = localReport.Controls[inx] as HtmlContainerControl;
                    header?.Controls.AddAt(0,
                                           new HtmlSpan {
                        InnerText = $"{localName}, "
                    }.AddCssClasses("local-name"));
                }

                switch (reportLevel)
                {
                case ReportLevel.StateLevel:
                {
                    while (localReport.Controls.Count > 0)
                    {
                        if (countiesForLocal.Length > 1)
                        {
                            // if the county is the first in the counties, inject a multi-county message
                            // and move the pair (header and content) to the multi-counties section.
                            // Otherwise, discard it (it's a duplicate)
                            if (countyCode == countiesForLocal[0].Value)
                            {
                                var countyMessage = "Parts of this district are in" +
                                                    $" {LocalIdsCodes.FormatCountyNames(countiesForLocal, true)}";
                                var header = localReport.Controls[0] as HtmlContainerControl;
                                header?.Controls.Add(
                                    new HtmlP {
                                        InnerText = countyMessage
                                    }.AddCssClasses("county-message"));
                                Debug.Assert(multiCountySection != null, "multiCountySection != null");
                                multiCountySection.Controls.Add(localReport.Controls[0]);
                                multiCountySection.Controls.Add(localReport.Controls[0]);
                            }
                            else
                            {
                                localReport.Controls.RemoveAt(0);
                                localReport.Controls.RemoveAt(0);
                                // avoid double counting
                                winnersNotIdentified -= electionReport.WinnersNotIdentified;
                            }
                        }
                        else
                        {
                            // move the pair (header and content) to the outer content
                            container.Controls.Add(localReport.Controls[0]);
                            container.Controls.Add(localReport.Controls[0]);
                        }
                    }
                }
                break;

                case ReportLevel.CountyLevel:
                {
                    var first = true;
                    while (localReport.Controls.Count > 0)
                    {
                        // for multi-county districts, inject an "other counties" message into the
                        // first office header if multi county
                        var countyMessage = Empty;
                        if (first && countiesForLocal.Length > 1)
                        {
                            countyMessage = "Parts of this local district are also in" +
                                            $" {LocalIdsCodes.FormatCountyNames(countiesForLocal, true, countyCode)}";
                        }
                        var header = localReport.Controls[0] as HtmlContainerControl;
                        if (!IsNullOrWhiteSpace(countyMessage))
                        {
                            header?.Controls.Add(
                                new HtmlP {
                                    InnerText = countyMessage
                                }.AddCssClasses("county-message"));
                        }
                        first = false;
                        // move the pair (header and content) to the outer content
                        container.Controls.Add(localReport.Controls[0]);
                        container.Controls.Add(localReport.Controls[0]);
                    }
                }
                break;
                }

                winnersNotIdentified += electionReport.WinnersNotIdentified;
            }

            return(winnersNotIdentified);
        }