Skip to content

Commit

Permalink
Move query & search execution back to SObjectRepository (#19)
Browse files Browse the repository at this point in the history
* Created SOQLUtils class to handle parsing a List<Object> into the formatted expected by SOQL for each data type
* Moved query & search execution back to SObjectRepository
* Deleted some old classes
  • Loading branch information
jongpie authored Feb 23, 2017
1 parent c40f63f commit 073c2b7
Show file tree
Hide file tree
Showing 14 changed files with 179 additions and 336 deletions.
34 changes: 0 additions & 34 deletions src/classes/CollectionUtils.cls

This file was deleted.

71 changes: 0 additions & 71 deletions src/classes/CollectionUtils_Test.cls

This file was deleted.

5 changes: 0 additions & 5 deletions src/classes/CollectionUtils_Test.cls-meta.xml

This file was deleted.

96 changes: 0 additions & 96 deletions src/classes/LeadQueryRepositoryTest.cls

This file was deleted.

5 changes: 0 additions & 5 deletions src/classes/LeadQueryRepositoryTest.cls-meta.xml

This file was deleted.

52 changes: 22 additions & 30 deletions src/classes/LeadRepository.cls
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ public without sharing class LeadRepository extends SObjectRepository {

public LeadRepository() {
super(LeadRepository.DEFAULT_FIELD_SET);
// Any conditions added in the constructor will apply to all queries in this class
this.whereIsConverted(false);
}

// Overload the constructor if you want to allow other code to specify the field set used
Expand All @@ -13,65 +15,55 @@ public without sharing class LeadRepository extends SObjectRepository {

// ISObjectRepository requires at least 2 methods, getRecord & getList
public Lead getRecord(Id leadId) {
String query = this
.addConditionIdEquals(leadId)
return (Lead)this
.whereIdEquals(leadId)
.setAsUpdate()
.getQuery();

return (Lead)Database.query(query)[0];
.getFirstQueryResult();
}

public List<Lead> getList(List<Id> leadIdList) {
String query = this
.addConditionIdIn(leadIdList)
return (List<Lead>)this
.whereIdIn(leadIdList)
.setAsUpdate()
.getQuery();

return (List<Lead>)Database.query(query);
.getQueryResults();
}

// Add public methods needed that return the query results
// Only methods that return an SObject or collection of SObjects should be made public
public List<Lead> getListForSources(List<String> leadSourceList) {
String query = this
.addCondition(Schema.Lead.LeadSource + ' IN ' + CollectionUtils.toString(leadSourceList))
return (List<Lead>)this
.whereFieldIn(Schema.Lead.LeadSource, leadSourceList)
.orderBy(Schema.Lead.CreatedDate)
.getQuery();

return (List<Lead>)Database.query(query);
.getQueryResults();
}

public List<Lead> getListForStatus(String status, Integer limitCount) {
String query = this
.addConditionIsConverted(false)
.addConditionStatusEquals(status)
return (List<Lead>)this
.whereIsConverted(false)
.whereStatusEquals(status)
.limitCount(limitCount)
.orderBy(Schema.Lead.LastModifiedDate, SObjectRepository.SortOrder.DESCENDING)
.setAsUpdate()
.getQuery();

return (List<Lead>)Database.query(query);
.getQueryResults();
}

public List<Lead> searchInAllFields(String searchTerm) {
String query = this
.addConditionIsConverted(false)
return (List<Lead>)this
.whereIsConverted(false)
.orderBy(Schema.Lead.CreatedDate, SObjectRepository.SortOrder.DESCENDING)
.limitCount(10)
.setAsUpdate() // SOSL cannot use FOR UPDATE. This will execute, but a warning debug statement will indicate that it is ignored
.getSearchQuery(searchTerm, SObjectRepository.SearchGroup.ALL_FIELDS);

return (List<Lead>)Search.query(query)[0];
.getSearchResults(searchTerm, SObjectRepository.SearchGroup.ALL_FIELDS);
}

// You can add additional builder methods for any commonly used filters for this SObject
// All builder methods should be kept as private or protected
private LeadRepository addConditionIsConverted(Boolean bool) {
return (LeadRepository)this.addCondition(Schema.Lead.IsConverted + ' = ' + bool);
private LeadRepository whereIsConverted(Boolean bool) {
return (LeadRepository)this.whereFieldEquals(Schema.Lead.IsConverted, bool);
}

private LeadRepository addConditionStatusEquals(String status) {
return (LeadRepository)this.addCondition(Schema.Lead.Status + ' = ' + StringUtils.wrapInSingleQuotes(status));
private LeadRepository whereStatusEquals(String status) {
return (LeadRepository)this.whereFieldEquals(Schema.Lead.Status, status);
}

}
35 changes: 35 additions & 0 deletions src/classes/SOQLUtils.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
public without sharing class SOQLUtils {

public static String toSOQLString(List<Object> valueList) {
List<String> parsedValueList = new List<String>();
for(Object value : valueList) parsedValueList.add(toSOQLString(value));
return '(' + String.join(parsedValueList, ',') + ')';
}

public static String toSOQLString(Object value) {
if(value == null) return null;
else if(value instanceof Boolean) return String.valueOf((Boolean)value);
else if(value instanceof Date) return String.valueOf((Date)value);
else if(value instanceof Datetime) {
Datetime datetimeValue = (Datetime)value;
return datetimeValue.format('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'', 'Greenwich Mean Time');
}
else if(value instanceof Decimal) return String.valueOf((Decimal) value);
else if(value instanceof Double) return String.valueOf((Double) value);
else if(value instanceof Integer) return String.valueOf((Integer) value);
else if(value instanceof Long) return String.valueOf((Long) value);
else if(value instanceof SObject) {
SObject record = (SObject)value;
return wrapInSingleQuotes(record.Id);
}
else if(value instanceof String) return wrapInSingleQuotes((String)value);
else return String.valueOf(value);
}

public static String wrapInSingleQuotes(String input) {
if(input.left(1) != '\'') input = '\'' + input;
if(input.right(1) != '\'') input = input + '\'';
return input;
}

}
File renamed without changes.
Loading

0 comments on commit 073c2b7

Please sign in to comment.