protected static Organization FromOptionalDataString(ObjectOptionalDataType dataType, string data) { int[] organizationIds = SwarmDb.GetDatabaseForReading().GetObjectsByOptionalData(ObjectType.Organization, dataType, data.Trim()); if (organizationIds.Length < 1) { return(null); } else if (organizationIds.Length == 1) { return(Organization.FromIdentity(organizationIds[0])); } else { throw new InvalidOperationException("Invalid state: multiple organizations with same " + dataType.ToString()); } }
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); }