Skip to content

Commit

Permalink
Adds ability for repositories to query multiple SOSL objects
Browse files Browse the repository at this point in the history
  • Loading branch information
jamessimone committed May 22, 2024
1 parent 39169a2 commit b9db66f
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 8 deletions.
2 changes: 1 addition & 1 deletion force-app/factory/RepoFactoryMock.cls
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public class RepoFactoryMock {
return this.results;
}

public override List<List<SObject>> getSosl(String searchTerm, List<Query> queries) {
public override List<List<SObject>> getSosl(String searchTerm, List<Query> queries, List<AdditionalSoslObject> additionalSoslObjects) {
QueriesMade.addAll(queries);
this.clearState();
return new List<List<SObject>>{ this.results };
Expand Down
18 changes: 18 additions & 0 deletions force-app/repository/AdditionalSoslObject.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class AdditionalSoslObject {
public final Schema.SObjectType objectType;
public final Integer queryLimit;
public final List<Query> queryFilters;
public final List<Schema.SObjectField> selectFields;

public AdditionalSoslObject(
Schema.SObjectType objectType,
List<Schema.SObjectField> selectFields,
List<Query> queryFilters,
Integer queryLimit
) {
this.objectType = objectType;
this.queryFilters = queryFilters;
this.queryLimit = queryLimit;
this.selectFields = selectFields;
}
}
5 changes: 5 additions & 0 deletions force-app/repository/AdditionalSoslObject.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>60.0</apiVersion>
<status>Active</status>
</ApexClass>
5 changes: 5 additions & 0 deletions force-app/repository/IRepository.cls
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ public interface IRepository extends IDML {

List<List<SObject>> getSosl(String searchTerm, Query query);
List<List<SObject>> getSosl(String searchTerm, List<Query> queries);
List<List<SObject>> getSosl(
String searchTerm,
List<Query> queries,
List<AdditionalSoslObject> additionalSoslObjects
);
IRepository setSearchGroup(SearchGroup searchGroup);

IRepository setAccessLevel(System.AccessLevel accessLevel);
Expand Down
41 changes: 34 additions & 7 deletions force-app/repository/Repository.cls
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,12 @@ public virtual without sharing class Repository implements IRepository {

protected virtual Set<String> addSelectFields() {
this.baseSelectUsed = true;
return this.addSelectFields(this.queryFields);
}

private Set<String> addSelectFields(List<Schema.SObjectField> fields) {
Set<String> fieldStrings = new Set<String>{ 'Id' };
for (SObjectField field : this.queryFields) {
for (SObjectField field : fields) {
fieldStrings.add(field.getDescribe().getName());
}
return fieldStrings;
Expand Down Expand Up @@ -218,19 +222,27 @@ public virtual without sharing class Repository implements IRepository {
}

public virtual List<List<SObject>> getSosl(String searchTerm, List<Query> queryFilters) {
return this.getSosl(searchTerm, queryFilters, new List<AdditionalSoslObject>());
}

public virtual List<List<SObject>> getSosl(
String searchTerm,
List<Query> queryFilters,
List<AdditionalSoslObject> additionalSoslObjects
) {
this.isSosl = true;
List<AdditionalSoslObject> orderedSearchObjects = new List<AdditionalSoslObject>{
new AdditionalSoslObject(this.repoType, this.queryFields, queryFilters, this.limitAmount)
};
orderedSearchObjects.addAll(additionalSoslObjects);
String searchQuery =
'FIND \'' +
String.escapeSingleQuotes(searchTerm) +
'\' IN ' +
this.soslSearchGroup.name().replace('_', ' ') +
' RETURNING ' +
this.repoType +
'(' +
String.join(this.addSelectFields(), ',') +
this.addWheres(queryFilters) +
this.getLimitAmount(this.limitAmount) +
')';
this.formatAdditionalSoslObjects(orderedSearchObjects);

System.debug('Search query:\n' + searchQuery);
List<List<SObject>> results = Search.query(searchQuery, this.accessLevel);
System.debug(System.LoggingLevel.FINER, 'Number of results: ' + results.size() + '\nResults: \n' + results);
Expand All @@ -244,6 +256,21 @@ public virtual without sharing class Repository implements IRepository {
return this;
}

private String formatAdditionalSoslObjects(List<AdditionalSoslObject> soslObjects) {
List<String> objectsPreJoin = new List<String>();
for (AdditionalSoslObject soslObject : soslObjects) {
objectsPreJoin.add(
soslObject.objectType +
'(' +
String.join(this.addSelectFields(soslObject.selectFields), ',') +
this.addWheres(soslObject.queryFilters) +
this.getLimitAmount(soslObject.queryLimit) +
')'
);
}
return String.join(objectsPreJoin, ',');
}

public Database.SaveResult doInsert(SObject record) {
return this.dml.doInsert(record);
}
Expand Down
25 changes: 25 additions & 0 deletions force-app/repository/RepositoryTests.cls
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,31 @@ private class RepositoryTests {
System.assertEquals(cpa.Id, results.get(0).get(0).Id);
}

@IsTest
static void it_searches_for_additional_objects() {
ContactPointPhone record = new ContactPointPhone(TelephoneNumber = 'hello universe');
insert record;

Test.setFixedSearchResults(new List<Id>{ record.Id });

List<List<SObject>> results = new ContactPointAddressRepo()
.setSearchGroup(SearchGroup.NAME_FIELDS)
.getSosl(
'hel',
new List<Query>{ Query.equals(ContactPointAddress.PreferenceRank, 1) },
new List<AdditionalSoslObject>{
new AdditionalSoslObject(
ContactPointPhone.SObjectType,
new List<Schema.SObjectField>(),
new List<Query>{ Query.equals(ContactPointPhone.TelephoneNumber, 'hello universe') },
1
)
}
);

System.assertEquals(record.Id, results.get(1).get(0).Id);
}

private class GroupMemberRepo extends Repository {
public GroupMemberRepo() {
super(GroupMember.SObjectType, new List<Schema.SObjectField>{ GroupMember.GroupId }, new RepoFactory());
Expand Down

0 comments on commit b9db66f

Please sign in to comment.