示例#1
0
        // --------------------------------------------
        //          WORKSPACE BELOW THIS MARK
        // --------------------------------------------


        public static Organization GetMostLocalOrganization(int geographyId, int rootOrganizationId)
        {
            // This function returns the most local organization in a tree under "rootOrganizationId".

            // First, find all possible organizations at the particular geography:

            Organizations orgList = GetOrganizationsAvailableAtGeography(geographyId);

            // Second, only note those that inherit from the supplied root:

            Dictionary <int, Organization> table = new Dictionary <int, Organization>();

            foreach (Organization org in orgList)
            {
                if (org.IsOrInherits(rootOrganizationId))
                {
                    table[org.OrganizationId] = org;
                }
            }

            // For each remaining organization, find the one with the longest organization line.
            // This is slightly inefficient as the queries for Geography.GetRootLineage() are repeated.

            if (table.Count == 0)
            {
                // No organizations!

                throw new ArgumentException(
                          string.Format(
                              "No organizations eligible under supplied organization ({0}) for supplied geography({1})",
                              rootOrganizationId, geographyId));
            }

            int          maxLength = 0;
            Organization bestFit   = null;

            foreach (Organization org in table.Values)
            {
                foreach (Geography uptakeGeo in org.UptakeGeographies)
                {
                    Geographies geoLine = Geography.FromIdentity(org.AnchorGeographyId).GetRootLineage();
                    if (geoLine.Count > maxLength && org.AutoAssignNewMembers)
                    {
                        bestFit   = org;
                        maxLength = geoLine.Count;
                    }
                    if (geoLine.Count == maxLength && org.AutoAssignNewMembers && bestFit != null)
                    {
                        if (bestFit.GetRootLineage().Count < org.GetRootLineage().Count)
                        {
                            bestFit   = org;
                            maxLength = geoLine.Count;
                        }
                    }
                }
            }

            return(bestFit);
        }
示例#2
0
        public Geographies RemoveRedundant()
        {
            Dictionary <int, Geography> remaining = new Dictionary <int, Geography>();

            foreach (Geography geo in this)
            {
                remaining[geo.Identity] = geo;
            }

            // O(n^3).

            // For each node, get the node line, and if any other org in collection
            // is a parent of the current org, the take the current org out of the collection.

            foreach (Geography currentNode in this)
            {
                Geographies currentLine = Geography.FromIdentity(currentNode.GeographyId).GetLine();

                foreach (Geography comparedNode in this)
                {
                    if (comparedNode.Identity == currentNode.Identity)
                    {
                        // Do not compare to ourselves
                        continue;
                    }

                    // When comparing to another node, check against all parents of
                    // the current node.

                    for (int index = 0; index < currentLine.Count - 1; index++)
                    {
                        if (currentLine[index].Identity == comparedNode.Identity)
                        {
                            // the compared node is a parent of the current node.
                            // Therefore, the current node is already present in the collection,
                            // as a child of the compared node. It can be safely removed.

                            remaining.Remove(currentNode.GeographyId);
                            break;
                        }
                    }
                }
            }

            // Assemble result

            Geographies result = new Geographies();

            foreach (int nodeId in remaining.Keys)
            {
                result.Add(remaining[nodeId]);
            }

            return(result);
        }
示例#3
0
        public static Geographies FromArray(BasicGeography[] basicArray)
        {
            Geographies result = new Geographies();

            result.Capacity = basicArray.Length * 11 / 10;
            foreach (BasicGeography basic in basicArray)
            {
                result.Add(Geography.FromBasic(basic));
            }

            return(result);
        }
示例#4
0
        public int GetRoleHolderCountForGeographies(Geographies geographies)
        {
            var result = new Dictionary <int, bool>();

            BasicPersonRole[] personRoles = SwarmDb.GetDatabaseForReading().GetRolesForOrganizationsGeographies(Identities,
                                                                                                                geographies.Identities);

            foreach (BasicPersonRole role in personRoles)
            {
                result[role.PersonId] = true;
            }

            return(result.Count);
        }
示例#5
0
        public bool Inherits(int prospectiveParentGeographyId)
        {
            // Returns true if prospectiveParent is a parent of ours.

            Geographies line = GetLine();

            for (int index = 0; index < line.Count - 1; index++)
            {
                if (line[index].Identity == prospectiveParentGeographyId)
                {
                    return(true);
                }
            }

            return(false);
        }
示例#6
0
        public People GetRoleHoldersForGeographies(Geographies geographies)
        {
            var result = new Dictionary <int, bool>();

            BasicPersonRole[] personRoles = SwarmDb.GetDatabaseForReading().GetRolesForOrganizationsGeographies(Identities,
                                                                                                                geographies.Identities);

            foreach (BasicPersonRole role in personRoles)
            {
                result[role.PersonId] = true;
            }

            var list = new List <int>();

            foreach (int key in result.Keys)
            {
                list.Add(key);
            }

            return(People.FromIdentities(list.ToArray()));
        }
示例#7
0
        public static Geographies LogicalOr(Geographies set1, Geographies set2)
        {
            // If either set is invalid, return the other
            // (a null is different from an empty set)

            if (set1 == null)
            {
                return(set2);
            }

            if (set2 == null)
            {
                return(set1);
            }

            // Build table, eliminating duplicates

            Dictionary <int, Geography> table = new Dictionary <int, Geography>();

            foreach (Geography geography in set1)
            {
                table[geography.Identity] = geography;
            }

            foreach (Geography geography in set2)
            {
                table[geography.Identity] = geography;
            }

            // Assemble result

            Geographies result = new Geographies();

            foreach (Geography geography in table.Values)
            {
                result.Add(geography);
            }

            return(result);
        }
示例#8
0
 public Geographies GetLine()
 {
     return(Geographies.FromArray(GeographyCache.GetGeographyLine(Identity)));
     //return Geographies.FromArray(SwarmDb.GetDatabaseForReading().GetGeographyLine(Identity));
 }
示例#9
0
 public int GetMemberCountForGeographies(Geographies geographies)
 {
     return(SwarmDb.GetDatabaseForReading().GetMemberCountForOrganizationsAndGeographies(Identities,
                                                                                         geographies.Identities));
 }
示例#10
0
 public Geographies LogicalOr(Geographies set2)
 {
     return(LogicalOr(this, set2));
 }
示例#11
0
        public static GeographyStatistics GeneratePresent(int[] memberCountForOrganizations)
        {
            Dictionary <int, int> voterCounts = SwarmDb.GetDatabaseForReading().GetGeographyVoterCounts();
            var result = new GeographyStatistics();

            // Step 1 - tally the leaf nodes

            foreach (int geographyId in voterCounts.Keys)
            {
                var dataPoint = new GeographyDataPoint
                {
                    GeographyName = Geography.FromIdentity(geographyId).Name,
                    GeographyId   = geographyId,
                    VoterCount    = voterCounts[geographyId]
                };

                result[geographyId] = dataPoint;
            }


            // Step 2 - add the member counts to the individual requested geo nodes

            foreach (int orgId in memberCountForOrganizations)
            {
                People members =
                    People.FromMemberships(
                        Memberships.ForOrganizations(Organization.FromIdentity(orgId).GetTree()));

                foreach (Person person in members)
                {
                    Geography geography = person.Geography;

                    // If we don't have this key, it's because it's too far down. Move up the tree until we're at least
                    // at municipal level.

                    while (geography.Identity != 1 && !result.ContainsKey(geography.Identity))
                    {
                        geography = geography.Parent;
                    }

                    // Add the data, unless we hit the roof in the last op.

                    if (geography.Identity != 1)
                    {
                        int birthYearBracket = (person.Birthdate.Year - 1900) / 5;

                        if (birthYearBracket >= 0 && birthYearBracket < 30)
                        {
                            result[geography.Identity].OrganizationData[orgId - 1].BirthYearBracketMemberCounts[
                                birthYearBracket]++;
                        }

                        if (person.IsFemale)
                        {
                            result[geography.Identity].OrganizationData[orgId - 1].FemaleMemberCount++;
                        }
                        else if (person.IsMale)
                        {
                            result[geography.Identity].OrganizationData[orgId - 1].MaleMemberCount++;
                        }
                    }
                }
            }

            // TODO: Activist count as a new step


            // Step 3 - add up the totals for every intermediate node (expensive!)

            Geographies allGeographies = Geography.Root.GetTree();

            foreach (Geography geography in allGeographies)
            {
                Geographies localTree   = geography.GetTree();
                int         voterCount  = 0;
                var         tempOrgData = new GeographyOrganizationDataPoint[2]; // HACK
                tempOrgData[0] = new GeographyOrganizationDataPoint();
                tempOrgData[1] = new GeographyOrganizationDataPoint();

                foreach (Geography localNode in localTree)
                {
                    // Increment our temp values for every geo node below the one we're currently processing.

                    if (!result.ContainsKey(localNode.Identity))
                    {
                        continue;
                    }

                    voterCount += result[localNode.Identity].VoterCount;

                    for (int orgIndex = 0; orgIndex < 2; orgIndex++)
                    {
                        for (int ageBracketIndex = 0; ageBracketIndex < 30; ageBracketIndex++)
                        {
                            tempOrgData[orgIndex].BirthYearBracketMemberCounts[ageBracketIndex] +=
                                result[localNode.Identity].OrganizationData[orgIndex].BirthYearBracketMemberCounts[
                                    ageBracketIndex];
                        }

                        tempOrgData[orgIndex].ActivistCount +=
                            result[localNode.Identity].OrganizationData[orgIndex].ActivistCount;
                        tempOrgData[orgIndex].FemaleMemberCount +=
                            result[localNode.Identity].OrganizationData[orgIndex].FemaleMemberCount;
                        tempOrgData[orgIndex].MaleMemberCount +=
                            result[localNode.Identity].OrganizationData[orgIndex].MaleMemberCount;
                    }
                }

                if (!result.ContainsKey(geography.Identity))
                {
                    result[geography.Identity] = new GeographyDataPoint
                    {
                        GeographyId   = geography.Identity,
                        GeographyName = geography.Name
                    };
                }

                // Save our temp values to the processed node.

                result[geography.Identity].VoterCount = voterCount;

                for (int orgIndex = 0; orgIndex < 2; orgIndex++)
                {
                    for (int ageBracketIndex = 0; ageBracketIndex < 30; ageBracketIndex++)
                    {
                        result[geography.Identity].OrganizationData[orgIndex].BirthYearBracketMemberCounts[
                            ageBracketIndex] =
                            tempOrgData[orgIndex].BirthYearBracketMemberCounts[ageBracketIndex];
                    }

                    result[geography.Identity].OrganizationData[orgIndex].ActivistCount =
                        tempOrgData[orgIndex].ActivistCount;
                    result[geography.Identity].OrganizationData[orgIndex].FemaleMemberCount =
                        tempOrgData[orgIndex].FemaleMemberCount;
                    result[geography.Identity].OrganizationData[orgIndex].MaleMemberCount =
                        tempOrgData[orgIndex].MaleMemberCount;
                }
            }


            // Step 4 - collect

            return(result);
        }
示例#12
0
 public Geographies ThisAndBelow()
 {
     return(Geographies.FromArray(GeographyCache.GetGeographyTree(Identity)));
     // return Geographies.FromArray(SwarmDb.GetDatabaseForReading().GetGeographyTree(Identity));
 }