-
Notifications
You must be signed in to change notification settings - Fork 21
/
Repository.cls
363 lines (314 loc) · 12.1 KB
/
Repository.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
public virtual without sharing class Repository implements IRepository {
private final Map<Schema.SObjectField, String> childToRelationshipNames;
private final IDML dml;
@TestVisible
private final List<Schema.SObjectField> queryFields;
private final Set<String> selectFields = new Set<String>();
protected final Schema.SObjectType repoType;
protected final Map<String, Object> bindVars = new Map<String, Object>();
protected System.AccessLevel accessLevel = System.AccessLevel.SYSTEM_MODE;
protected final Map<String, RepositorySortOrder> fieldToSortOrder = new Map<String, RepositorySortOrder>();
private Boolean baseSelectUsed = false;
private Boolean isSosl = false;
private Boolean shouldAddChildFields = true;
private Integer limitAmount;
private SearchGroup soslSearchGroup = SearchGroup.ALL_FIELDS;
public Repository(Schema.SObjectType repoType, List<Schema.SObjectField> queryFields, RepoFactory repoFactory) {
this.dml = repoFactory.getDml();
this.queryFields = queryFields;
this.repoType = repoType;
this.childToRelationshipNames = this.getChildRelationshipNames(repoType);
}
// SOQL
public Database.QueryLocator getQueryLocator(List<Query> queries) {
return this.getQueryLocator(queries, this.shouldAddChildFields);
}
public Database.QueryLocator getQueryLocator(List<Query> queries, Boolean shouldAddChildFields) {
Boolean originalValue = this.shouldAddChildFields;
this.shouldAddChildFields = shouldAddChildFields;
Database.QueryLocator locator = Database.getQueryLocatorWithBinds(
this.getFinalQuery(queries),
this.bindVars,
this.accessLevel
);
this.bindVars.clear();
this.shouldAddChildFields = originalValue;
return locator;
}
public virtual List<SObject> get(Query query) {
return this.get(new List<Query>{ query });
}
public virtual List<SObject> get(List<Query> queries) {
String finalQuery = this.getFinalQuery(queries);
return this.performQuery(finalQuery);
}
public virtual List<SObject> getAll() {
return this.get(new List<Query>());
}
public Repository setLimit(Integer limitAmount) {
this.limitAmount = limitAmount;
return this;
}
public Repository addSortOrder(Schema.SObjectField fieldToken, RepositorySortOrder sortOrder) {
this.fieldToSortOrder.put(fieldToken.getDescribe().getName(), sortOrder);
return this;
}
public Repository addSortOrder(List<Schema.SObjectField> parentFieldChain, RepositorySortOrder sortOrder) {
this.fieldToSortOrder.put(Query.getBuiltUpParentFieldName(parentFieldChain), sortOrder);
return this;
}
public Repository addBaseFields(List<Schema.SObjectField> fields) {
Set<Schema.SObjectField> uniqueFields = new Set<Schema.SObjectField>(this.queryFields);
uniqueFields.addAll(fields);
this.queryFields.clear();
this.queryFields.addAll(uniqueFields);
return this;
}
public Repository addParentFields(List<Schema.SObjectField> parentTypes, List<Schema.SObjectField> parentFields) {
this.selectFields.addAll(this.getParentFields(parentTypes, parentFields));
return this;
}
public Repository addChildFields(Schema.SObjectField childFieldToken, List<Schema.SObjectField> childFields) {
return this.addChildFields(
childFieldToken,
childFields,
new List<Query>(),
new Map<String, RepositorySortOrder>(),
null
);
}
public Repository addChildFields(
Schema.SObjectField childFieldToken,
List<Schema.SObjectField> childFields,
List<Query> optionalWhereFilters,
Map<String, RepositorySortOrder> fieldToSortOrder,
Integer limitBy
) {
return this.addChildFields(
childFieldToken,
new List<QueryField>{ new QueryField(childFields) },
optionalWhereFilters,
fieldToSortOrder,
limitBy
);
}
public Repository addChildFields(
Schema.SObjectField childFieldToken,
List<QueryField> childFields,
List<Query> optionalWhereFilters,
Map<String, RepositorySortOrder> fieldToSortOrder,
Integer limitBy
) {
if (this.childToRelationshipNames.containsKey(childFieldToken) == false || this.shouldAddChildFields == false) {
return this;
}
String baseSubselect =
'(SELECT {0} FROM {1}' +
this.addWheres(optionalWhereFilters) +
this.getOrderBys(fieldToSortOrder) +
this.getLimitAmount(limitBy) +
')';
Set<String> childFieldNames = new Set<String>{ 'Id' };
for (QueryField childField : childFields) {
childFieldNames.add(childField.toString());
}
this.selectFields.add(
String.format(
baseSubselect,
new List<String>{ String.join(childFieldNames, ','), this.childToRelationshipNames.get(childFieldToken) }
)
);
return this;
}
public Repository setAccessLevel(System.AccessLevel accessLevel) {
this.setOptions(null, accessLevel);
return this;
}
public Repository clearBindVars() {
this.bindVars.clear();
return this;
}
protected virtual Set<String> addSelectFields() {
this.baseSelectUsed = true;
return this.addSelectFields(this.queryFields);
}
protected virtual String getFinalQuery(List<Query> queries) {
return this.getSelectAndFrom() +
this.addWheres(queries) +
this.getOrderBys(this.fieldToSortOrder) +
this.getLimitAmount(this.limitAmount);
}
protected virtual void clearState() {
this.fieldToSortOrder.clear();
this.limitAmount = null;
}
private List<String> getParentFields(
List<Schema.SObjectField> parentTypes,
List<Schema.SObjectField> parentFieldTokens
) {
List<String> parentFields = new List<String>();
String parentBase = '';
for (Schema.SObjectField parentToken : parentTypes) {
String parentName = parentToken.getDescribe().getRelationshipName() ?? parentToken.toString().replace('__c', '__r');
parentBase += parentName + '.';
}
for (Schema.SObjectField parentField : parentFieldTokens) {
parentFields.add(parentBase + parentField.getDescribe().getName());
}
return parentFields;
}
private Map<Schema.SObjectField, String> getChildRelationshipNames(Schema.SObjectType repoType) {
Map<Schema.SObjectField, String> localChildToRelationshipNames = new Map<Schema.SObjectField, String>();
for (Schema.ChildRelationship childRelationship : repoType.getDescribe().getChildRelationships()) {
localChildToRelationshipNames.put(childRelationship.getField(), childRelationship.getRelationshipName());
}
return localChildToRelationshipNames;
}
private String getSelectAndFrom() {
Set<String> localSelectFields = this.addSelectFields();
if (this.baseSelectUsed) {
localSelectFields.addAll(this.selectFields);
this.baseSelectUsed = false;
}
return 'SELECT ' + String.join(localSelectFields, ', ') + '\nFROM ' + this.repoType;
}
private Set<String> addSelectFields(List<Schema.SObjectField> fields) {
Set<String> fieldStrings = new Set<String>{ 'Id' };
for (SObjectField field : fields) {
fieldStrings.add(field.getDescribe().getName());
}
return fieldStrings;
}
private String addWheres(List<Query> queries) {
List<String> wheres = new List<String>();
for (Query qry : queries) {
String possibleClauseToAdd = this.isSosl ? qry.toSoslString() : qry.toString();
if (qry.isSoslEmpty() == false) {
wheres.add(possibleClauseToAdd);
this.bindVars.putAll(qry.getBindVars());
}
}
return wheres.isEmpty() ? '' : '\nWHERE ' + String.join(wheres, '\nAND ');
}
private List<SObject> performQuery(String finalQuery) {
System.debug(finalQuery);
List<SObject> results = Database.queryWithBinds(finalQuery, this.bindVars, this.accessLevel);
this.clearState();
System.debug(System.LoggingLevel.FINER, 'Number of results: ' + results.size() + '\nResults: \n' + results);
return results;
}
private String getOrderBys(Map<String, RepositorySortOrder> sortOrders) {
String orderByString = '';
if (sortOrders.isEmpty() == false) {
orderByString += ' \nORDER BY ';
String separator = ', ';
for (String field : sortOrders.keySet()) {
orderByString += field + ' ' + sortOrders.get(field).toString() + separator;
}
orderByString = orderByString.removeEnd(separator);
}
return orderByString;
}
private String getLimitAmount(Integer limitAmount) {
return (limitAmount != null ? '\nLIMIT ' + limitAmount : '');
}
// SOSL
public List<List<SObject>> getSosl(String searchTerm, Query queryFilter) {
return this.getSosl(searchTerm, new List<Query>{ queryFilter });
}
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.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);
this.clearState();
this.isSosl = false;
return results;
}
public Repository setSearchGroup(SearchGroup searchGroup) {
this.soslSearchGroup = searchGroup;
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, ',');
}
// DML
public Database.SaveResult doInsert(SObject record) {
return this.dml.doInsert(record);
}
public List<Database.SaveResult> doInsert(List<SObject> records) {
return this.dml.doInsert(records);
}
public Database.SaveResult doUpdate(SObject record) {
return this.dml.doUpdate(record);
}
public List<Database.SaveResult> doUpdate(List<SObject> records) {
return this.dml.doUpdate(records);
}
public Database.UpsertResult doUpsert(SObject record) {
return this.dml.doUpsert(record);
}
public List<Database.UpsertResult> doUpsert(List<SObject> records) {
return this.dml.doUpsert(records);
}
public List<Database.UpsertResult> doUpsert(List<SObject> records, Schema.SObjectField field) {
return this.dml.doUpsert(records, field);
}
public Database.UndeleteResult doUndelete(SObject record) {
return this.dml.doUnDelete(record);
}
public List<Database.UndeleteResult> doUndelete(List<SObject> records) {
return this.dml.doUndelete(records);
}
public Database.DeleteResult doDelete(SObject record) {
return this.dml.doDelete(record);
}
public List<Database.DeleteResult> doDelete(List<SObject> records) {
return this.dml.doDelete(records);
}
public Database.DeleteResult doHardDelete(SObject record) {
return this.dml.doHardDelete(record);
}
public List<Database.DeleteResult> doHardDelete(List<SObject> records) {
return this.dml.doHardDelete(records);
}
public Database.SaveResult publish(SObject event) {
return this.dml.publish(event);
}
public List<Database.SaveResult> publish(List<SObject> events) {
return this.dml.publish(events);
}
public IDML setOptions(Database.DMLOptions options, System.AccessLevel accessLevel) {
this.accessLevel = accessLevel;
return this.dml.setOptions(options, accessLevel);
}
}