From 4471e0b6b2e197a26c74dc644f029356fd73281a Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Thu, 8 Mar 2018 13:26:26 +0100 Subject: [PATCH] Searching for Applicants should work --- Database/Database-Applicants.cs | 50 +++++++++++++++++ Database/Database-Participation.cs | 2 +- Interface/DataObjects/ListPersonDataSource.cs | 2 +- Logic/Security/Authority.cs | 53 +++++++++++++++---- Logic/Swarm/Applicant.cs | 5 ++ Logic/Swarm/Applicants.cs | 14 +++++ Logic/Swarm/Participations.cs | 4 +- Logic/Swarm/People.cs | 53 ++++++++++++++++++- .../v5/Swarm/Json-ListFindPeople.aspx.cs | 27 +++++++++- 9 files changed, 193 insertions(+), 17 deletions(-) diff --git a/Database/Database-Applicants.cs b/Database/Database-Applicants.cs index c3f153ab..bdf1b888 100644 --- a/Database/Database-Applicants.cs +++ b/Database/Database-Applicants.cs @@ -103,6 +103,56 @@ namespace Swarmops.Database } } + + public Dictionary> GetApplicantsFromPeople(int[] personIds) + { + Dictionary> result = new Dictionary>(); + + if (personIds == null || personIds.Length == 0) + { + return result; + } + + // Optimization: If more than 128 personIds, build a hash table, get _all_ memberships, and return + // the matching ones logic wise. We've had _queries_ that exceed 64k text in length... + + /* + if (personIds.Length > 128) + { + return GetParticipationsForPeopleOptimizedForLargeSets(personIds, gracePeriod); + }*/ + + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + string commandString = "SELECT " + applicantFieldSequence + " WHERE " + + "Open=1 AND PersonId in (" + JoinIds(personIds) + ")"; + + DbCommand command = GetDbCommand(commandString, connection); + command.CommandTimeout = 30; // If 30s is too short, something needs optimization. + + using (DbDataReader reader = command.ExecuteReader()) + { + while (reader.Read()) + { + BasicApplicant applicant = ReadApplicantFromDataReader(reader); + int personId = applicant.PersonId; + + if (!result.ContainsKey(personId)) + { + result[personId] = new List(); + } + + result[personId].Add(applicant); + // Add to this person's list of memberships + } + } + } + + return result; + } + #endregion #region Creation and manipulation - stored procedures diff --git a/Database/Database-Participation.cs b/Database/Database-Participation.cs index bebd3c7d..4e7042dd 100644 --- a/Database/Database-Participation.cs +++ b/Database/Database-Participation.cs @@ -306,7 +306,7 @@ namespace Swarmops.Database result[personId] = new List(); } - result[personId].Add (ReadMembershipFromDataReader (reader)); + result[personId].Add (participation); // Add to this person's list of memberships } } diff --git a/Interface/DataObjects/ListPersonDataSource.cs b/Interface/DataObjects/ListPersonDataSource.cs index c6f292bc..3c02aeec 100644 --- a/Interface/DataObjects/ListPersonDataSource.cs +++ b/Interface/DataObjects/ListPersonDataSource.cs @@ -68,7 +68,7 @@ public class ListPersonDataSource { People ppl = People.FromIdentities(this.listedPersons); Dictionary> membershipTable = - Participations.GetParticipationsForPeople(this.listedPersons, Participation.GracePeriod); + Participations.GetParticipationsForPeople(ppl, Participation.GracePeriod); Participations participationsToLoad = new Participations(); foreach (Person p in ppl) { diff --git a/Logic/Security/Authority.cs b/Logic/Security/Authority.cs index bf5db13e..048b4aa7 100644 --- a/Logic/Security/Authority.cs +++ b/Logic/Security/Authority.cs @@ -381,6 +381,7 @@ namespace Swarmops.Logic.Security orgLookup[Organization.Identity] = true; Dictionary> membershipLookup = null; + Dictionary> applicantLookup = null; if (HasSystemAccess(AccessType.Read) || HasAccess(new Access(Organization, aspect, AccessType.Read))) { @@ -388,7 +389,8 @@ namespace Swarmops.Logic.Security // or org-wide read access (at least) to participant/personal data at current Organization // Optimization: Get all memberships in advance, without instantiating logic objects - membershipLookup = Participations.GetParticipationsForPeople (rawList.Identities, 0); + membershipLookup = Participations.GetParticipationsForPeople (rawList, 0); + applicantLookup = Applicants.GetApplicantsFromPeople(rawList); foreach (Person person in rawList) { @@ -396,22 +398,40 @@ namespace Swarmops.Logic.Security // them is visible to this Authority - if it's a membership in an org at or below the // Authority object's organization - if (membershipLookup.ContainsKey (person.Identity)) + if (membershipLookup.ContainsKey(person.Identity)) { List list = membershipLookup[person.Identity]; foreach (BasicParticipation basicMembership in list) { - if (orgLookup.ContainsKey (basicMembership.OrganizationId)) + if (orgLookup.ContainsKey(basicMembership.OrganizationId)) { // hit - this person has an active membership that makes them visible to this Authority - result.Add (person); + result.Add(person); + break; + } + } + + } + + // If membership check fails, then check for membership applications + + if (applicantLookup.ContainsKey(person.Identity)) + { + List applicantList = applicantLookup[person.Identity]; + + foreach (Applicant applicant in applicantList) + { + if (orgLookup.ContainsKey(applicant.OrganizationId)) + { + // hit - this person has an active membership _application_ that makes them visible to this Authority + result.Add(person); break; } } } } - + return result; } @@ -448,7 +468,8 @@ namespace Swarmops.Logic.Security // Optimization: Get all memberships in advance, without instantiating logic objects Dictionary> personLookup = - Participations.GetParticipationsForPeople(rawList.Identities, 0); + Participations.GetParticipationsForPeople(rawList, 0); + applicantLookup = Applicants.GetApplicantsFromPeople(rawList); foreach (Person person in rawList) { @@ -462,15 +483,29 @@ namespace Swarmops.Logic.Security { // Geography hit. Test Membership / Organization. - List list = personLookup[person.Identity]; + bool added = false; - foreach (BasicParticipation basicMembership in list) + List participationList = personLookup[person.Identity]; + + foreach (BasicParticipation basicMembership in participationList) { - if (orgLookup.ContainsKey (basicMembership.OrganizationId)) + if (orgLookup.ContainsKey (basicMembership.OrganizationId) && !added) { // Organization hit - this person has an active membership that makes them visible to this Authority result.Add (person); + added = true; + } + } + + List applicantList = applicantLookup[person.Identity]; + + foreach (BasicApplicant basicApplicant in applicantList) + { + if (orgLookup.ContainsKey(basicApplicant.OrganizationId) && !added) + { + result.Add(person); + added = true; } } } diff --git a/Logic/Swarm/Applicant.cs b/Logic/Swarm/Applicant.cs index 063fbf15..5b2ae5ce 100644 --- a/Logic/Swarm/Applicant.cs +++ b/Logic/Swarm/Applicant.cs @@ -39,5 +39,10 @@ namespace Swarmops.Logic.Swarm FromIdentityAggressive(SwarmDb.GetDatabaseForWriting() .CreateApplicant(person.Identity, organization.Identity)); } + + public int ScoreTotal + { + get { return this.Score1 + this.Score2 + this.Score3; } + } } } diff --git a/Logic/Swarm/Applicants.cs b/Logic/Swarm/Applicants.cs index 682386eb..83bb90cf 100644 --- a/Logic/Swarm/Applicants.cs +++ b/Logic/Swarm/Applicants.cs @@ -4,11 +4,25 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Swarmops.Basic.Types.Swarm; +using Swarmops.Database; +using Swarmops.Logic.Structure; using Swarmops.Logic.Support; namespace Swarmops.Logic.Swarm { public class Applicants: PluralBase { + + public static Dictionary> GetApplicantsFromPeople(People people) + { + return SwarmDb.GetDatabaseForReading().GetApplicantsFromPeople(people.Identities); + } + + public static Applicants FromPeopleInOrganization(People people, Organization organization) + { + Organizations orgTree = organization.ThisAndBelow(); + + return FromArray(SwarmDb.GetDatabaseForReading().GetApplicants(people, orgTree)); + } } } diff --git a/Logic/Swarm/Participations.cs b/Logic/Swarm/Participations.cs index 8f30fc42..247ae003 100644 --- a/Logic/Swarm/Participations.cs +++ b/Logic/Swarm/Participations.cs @@ -103,9 +103,9 @@ namespace Swarmops.Logic.Swarm } - public static Dictionary> GetParticipationsForPeople (int[] personIds, int gracePeriod) + public static Dictionary> GetParticipationsForPeople (People people, int gracePeriod) { - return SwarmDb.GetDatabaseForReading().GetParticipationsForPeople (personIds, gracePeriod); + return SwarmDb.GetDatabaseForReading().GetParticipationsForPeople (people.Identities, gracePeriod); } } } \ No newline at end of file diff --git a/Logic/Swarm/People.cs b/Logic/Swarm/People.cs index 40d96ce1..6e36ab9a 100644 --- a/Logic/Swarm/People.cs +++ b/Logic/Swarm/People.cs @@ -275,7 +275,7 @@ namespace Swarmops.Logic.Swarm BasicPerson[] candidates = SwarmDb.GetDatabaseForReading().GetPeopleInGeographiesWithPattern (geoTree.Identities, pattern); - return FilterByMembership (candidates, organization); + return People.LogicalOr(FilterByMembership (candidates, organization), FilterByApplicant(candidates, organization)); } @@ -289,7 +289,7 @@ namespace Swarmops.Logic.Swarm // Filter on memberships - return FilterByMembership (people, organization); + return People.LogicalOr(FilterByMembership (people, organization), FilterByApplicant(people, organization)); } @@ -342,6 +342,55 @@ namespace Swarmops.Logic.Swarm return result; } + private static People FilterByApplicant(BasicPerson[] candidates, Organization organization) + { + // Get the organization tree + + Organizations orgTree = organization.ThisAndBelow(); + + // Build a lookup table of this organization tree. For each person in 'people' + // that has at least one membership in an organization in the lookup table, + // add that person to the final result. + + Dictionary lookup = new Dictionary(); + + foreach (Organization org in orgTree) + { + lookup[org.Identity] = org; + } + + // Get the list of all memberships + + Dictionary> applicants = + SwarmDb.GetDatabaseForReading() + .GetApplicantsFromPeople(People.FromArray(candidates).Identities); + + People result = new People(); + + foreach (BasicPerson basicPerson in candidates) + { + bool applicantToOrganization = false; + + if (applicants.ContainsKey(basicPerson.Identity)) + { + foreach (BasicApplicant applicant in applicants[basicPerson.Identity]) + { + if (lookup.ContainsKey(applicant.OrganizationId)) + { + applicantToOrganization = true; + } + } + } + + if (applicantToOrganization) + { + result.Add(Person.FromBasic(basicPerson)); + } + } + + return result; + } + public static People FromGeography (int geographyId) { Geographies geoTree = Geography.FromIdentity (geographyId).ThisAndBelow(); diff --git a/Site/Pages/v5/Swarm/Json-ListFindPeople.aspx.cs b/Site/Pages/v5/Swarm/Json-ListFindPeople.aspx.cs index f197b025..25059d47 100644 --- a/Site/Pages/v5/Swarm/Json-ListFindPeople.aspx.cs +++ b/Site/Pages/v5/Swarm/Json-ListFindPeople.aspx.cs @@ -45,11 +45,33 @@ namespace Swarmops.Frontend.Pages.Swarm string editPersonTemplate = "\"actions\":\"\""; + Dictionary applicantLookup = new Dictionary(); + + if (CurrentOrganization.Parameters.ParticipationEntry == "ApplicationApproval") + { + // There are applications possible for this org; find them and put the Score in the Notes field + + Applicants applicants = Applicants.FromPeopleInOrganization(matches, CurrentOrganization); + + foreach (Applicant applicant in applicants) + { + applicantLookup[applicant.PersonId] = applicant; + } + } + foreach (Person person in matches) { + string notes = Participant.Localized(CurrentOrganization.RegularLabel, person.Gender); + + if (applicantLookup.ContainsKey(person.Identity)) + { + // If this is an applicant, use the "Notes" field for score + notes = applicantLookup[person.Identity].ScoreTotal.ToString("N0"); + } + string onePerson = '{' + String.Format ( - "\"id\":\"{0}\",\"name\":\"{1}\",\"avatar16Url\":\"{2}\",\"geographyName\":\"{3}\",\"mail\":\"{4}\",\"phone\":\"{5}\"", + "\"id\":\"{0}\",\"name\":\"{1}\",\"avatar16Url\":\"{2}\",\"geographyName\":\"{3}\",\"mail\":\"{4}\",\"notes\":\"{6}\",\"phone\":\"{5}\"", person.Identity, JsonSanitize (person.Canonical), person.GetSecureAvatarLink (16), @@ -57,7 +79,8 @@ namespace Swarmops.Frontend.Pages.Swarm JsonSanitize (person.Mail), JsonSanitize (person.Phone)) + "," + String.Format( - editPersonTemplate, person.Identity) + editPersonTemplate, person.Identity, + JsonSanitize(notes)) + '}'; jsonPeople.Add (onePerson); }