Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit 0d4630c

Browse files
authored
Added support for parent field filtering and including children queries
* SObjectRepository.cls has a new constructor that will automatically add all fields to the query for the specified SObject type - essentially, it's 'select * from sobject' * SObjectRepository.cls now uses QueryFilter.cls to handle filtering on fields from the base object, parent object and grandparent objects - the constructor used for QueryFilter.cls determines the type of filter * Children queries can now be included in your queries generated by SObjectRepository.cls. * Logging is now enabled by default (via the custom setting NebulaLoggerSettings__c) * A custom app (cleverly called 'Nebula') has been added - it includes 1 tab for the NebulaLog__c object * Updated some field descriptions & help text on the various custom settings * SObjectRecordTypes.cls now uses QueryBuilder.cls for its query generation * QueryBuilder.whereField() method has been replaced by QueryBuilder.filterBy() - it expects either a single instance or a list of QueryFilter.cls * SObjectRepository.queryFactory is now SObjectRepository.Query * Miscellaneous cleanup - variable name standardisation, whitespace, etc * Removed the method ISObjectRepository.getCreatedSinceTimeValue * Delete CollectionUtils.cls * TestingUtils.setReadOnlyField() has been refactored to allow any object type as a value & to prevent duplicating fields on the returned SObject
1 parent 4d7c0bc commit 0d4630c

File tree

91 files changed

+2026
-673
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+2026
-673
lines changed

src/applications/Nebula.app

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<CustomApplication xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<defaultLandingTab>NebulaLog__c</defaultLandingTab>
4+
<formFactors>Large</formFactors>
5+
<label>Nebula</label>
6+
<tab>NebulaLog__c</tab>
7+
</CustomApplication>

src/classes/DML.cls

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*************************************************************************************************
2+
* This file is part of the Nebula Framework project, released under the MIT License. *
3+
* See LICENSE file or go to https://github.com/jongpie/NebulaFramework for full license details. *
4+
*************************************************************************************************/
5+
public abstract class DML extends NebulaCore implements IDML {
6+
7+
private Schema.SObjectType sobjectType;
8+
9+
public DML(Schema.SObjectType sobjectType) {
10+
this.sobjectType = sobjectType;
11+
}
12+
13+
public virtual void insertRecords(SObject record) {
14+
this.insertRecords(new List<SObject>{record});
15+
}
16+
17+
public virtual void insertRecords(List<SObject> records) {
18+
Database.insert(records);
19+
}
20+
21+
public virtual void updateRecords(SObject record) {
22+
this.updateRecords(new List<SObject>{record});
23+
}
24+
25+
public virtual void updateRecords(List<SObject> records) {
26+
Database.update(records);
27+
}
28+
29+
public virtual void upsertRecords(SObject record) {
30+
this.upsertRecords(this.castRecords(record));
31+
}
32+
33+
public virtual void upsertRecords(List<SObject> records) {
34+
Database.upsert(records);
35+
}
36+
37+
public virtual void undeleteRecords(SObject record) {
38+
this.undeleteRecords(new List<SObject>{record});
39+
}
40+
41+
public virtual void undeleteRecords(List<SObject> records) {
42+
Database.undelete(records);
43+
}
44+
45+
public virtual void deleteRecords(SObject record) {
46+
this.deleteRecords(new List<SObject>{record});
47+
}
48+
49+
public virtual void deleteRecords(List<SObject> records) {
50+
Database.delete(records);
51+
}
52+
53+
public virtual void hardDeleteRecords(SObject record) {
54+
this.hardDeleteRecords(new List<SObject>{record});
55+
}
56+
57+
public virtual void hardDeleteRecords(List<SObject> records) {
58+
this.deleteRecords(records);
59+
if(!records.isEmpty()) Database.emptyRecycleBin(records);
60+
}
61+
62+
// Not all objects will have external ID fields, so these methods are protected (instead of public)
63+
// Any object that needs an upsert by external ID can expose these methods in their repos
64+
protected virtual void upsertRecords(SObject record, Schema.SObjectField externalIdField) {
65+
this.upsertRecords(this.castRecords(record), externalIdField);
66+
}
67+
68+
protected virtual void upsertRecords(List<SObject> records, Schema.SObjectField externalIdField) {
69+
Database.upsert(records, externalIdField);
70+
}
71+
72+
private List<SObject> castRecords(SObject record) {
73+
// Salesforce will only allow upsert calls for SObjects if a declared-type list is passed in.
74+
// This is fine for the bulk method, where we can assume the caller is passing in an explicit list, but for a single record,
75+
// the only way to successfully perform the upsert is to dynamically spin up a list of the SObject's type
76+
77+
String listType = 'List<' + this.sobjectType + '>';
78+
List<SObject> castRecords = (List<SObject>)Type.forName(listType).newInstance();
79+
castRecords.add(record);
80+
81+
return castRecords;
82+
}
83+
84+
}

src/classes/DML.cls-meta.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>39.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>

src/classes/DMLMock.cls

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*************************************************************************************************
2+
* This file is part of the Nebula Framework project, released under the MIT License. *
3+
* See LICENSE file or go to https://github.com/jongpie/NebulaFramework for full license details. *
4+
*************************************************************************************************/
5+
@isTest
6+
public class DMLMock {
7+
8+
public virtual class Base implements IDML {
9+
10+
public void insertRecords(SObject record) {
11+
this.insertRecords(new List<SObject>{record});
12+
}
13+
14+
public void insertRecords(List<SObject> recordList) {
15+
TestingUtils.generateIds(recordList);
16+
TestingUtils.insertedRecords.addAll(recordList);
17+
}
18+
19+
public void updateRecords(SObject record) {
20+
this.updateRecords(new List<SObject>{record});
21+
}
22+
23+
public void updateRecords(List<SObject> recordList) {
24+
TestingUtils.updatedRecords.addAll(recordList);
25+
}
26+
27+
public void upsertRecords(SObject record) {
28+
this.upsertRecords(new List<SObject>{record});
29+
}
30+
31+
public void upsertRecords(List<SObject> recordList) {
32+
TestingUtils.generateIds(recordList);
33+
TestingUtils.upsertedRecords.addAll(recordList);
34+
}
35+
36+
public void undeleteRecords(SObject record) {
37+
this.undeleteRecords(new List<SObject>{record});
38+
}
39+
40+
public void undeleteRecords(List<SObject> recordList) {
41+
TestingUtils.undeletedRecords.addAll(recordList);
42+
}
43+
44+
public void deleteRecords(SObject record) {
45+
this.deleteRecords(new List<SObject>{record});
46+
}
47+
48+
public void deleteRecords(List<SObject> recordList) {
49+
if(recordList != null) TestingUtils.deletedRecords.addAll(recordList);
50+
}
51+
52+
public void hardDeleteRecords(SObject record) {
53+
this.hardDeleteRecords(new List<SObject>{record});
54+
}
55+
56+
public void hardDeleteRecords(List<SObject> recordList) {
57+
this.deleteRecords(recordList);
58+
}
59+
60+
}
61+
62+
}

src/classes/DMLMock.cls-meta.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>39.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>

src/classes/DMLMock_Tests.cls

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*************************************************************************************************
2+
* This file is part of the Nebula Framework project, released under the MIT License. *
3+
* See LICENSE file or go to https://github.com/jongpie/NebulaFramework for full license details. *
4+
*************************************************************************************************/
5+
@isTest
6+
private class DMLMock_Tests {
7+
8+
static IDML dmlRepo = new DMLMock.Base();
9+
static Schema.Contact con = createContact();
10+
11+
@isTest
12+
static void it_should_fake_dml_insert() {
13+
Test.startTest();
14+
dmlRepo.insertRecords(con);
15+
Test.stopTest();
16+
17+
System.assert(TestingUtils.insertedRecords.size() > 0);
18+
}
19+
20+
@isTest
21+
static void it_should_fake_dml_update() {
22+
Test.startTest();
23+
dmlRepo.updateRecords(con);
24+
Test.stopTest();
25+
26+
System.assert(!TestingUtils.updatedRecords.isEmpty());
27+
}
28+
29+
@isTest
30+
static void it_should_fake_dml_upsert() {
31+
Test.startTest();
32+
dmlRepo.upsertRecords(con);
33+
Test.stopTest();
34+
35+
System.assert(!TestingUtils.upsertedRecords.isEmpty());
36+
}
37+
38+
@isTest
39+
static void it_should_fake_dml_delete() {
40+
Test.startTest();
41+
dmlRepo.deleteRecords(con);
42+
Test.stopTest();
43+
44+
System.assert(!TestingUtils.deletedRecords.isEmpty());
45+
}
46+
47+
@isTest
48+
static void it_should_fake_dml_hard_delete() {
49+
Test.startTest();
50+
dmlRepo.hardDeleteRecords(con);
51+
Test.stopTest();
52+
53+
System.assert(!TestingUtils.deletedRecords.isEmpty());
54+
}
55+
56+
@isTest
57+
static void it_should_fake_dml_undelete() {
58+
Test.startTest();
59+
dmlRepo.undeleteRecords(con);
60+
Test.stopTest();
61+
62+
System.assert(!TestingUtils.undeletedRecords.isEmpty());
63+
}
64+
65+
@isTest
66+
static void it_should_mock_updating_read_only_fields_when_updating_data() {
67+
Schema.Lead l = new Schema.Lead();
68+
l = (Lead)TestingUtils.setReadOnlyField(l, Schema.Lead.IsConverted, true);
69+
70+
Test.startTest();
71+
dmlRepo.updateRecords(l);
72+
Test.stoptest();
73+
74+
SObject record = TestingUtils.updatedRecords[0];
75+
System.assert(record instanceof Schema.Lead);
76+
System.assert(record.get('IsConverted') != null);
77+
}
78+
79+
private static Contact createContact() {
80+
con = new Contact();
81+
con.Email = 'rightHandMan@hamilton.com';
82+
con.FirstName = 'George';
83+
con.LastName = 'Washington';
84+
con.LeadSource = 'Web';
85+
86+
return con;
87+
}
88+
89+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>39.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3-
<apiVersion>38.0</apiVersion>
3+
<apiVersion>39.0</apiVersion>
44
<status>Active</status>
55
</ApexClass>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3-
<apiVersion>38.0</apiVersion>
3+
<apiVersion>39.0</apiVersion>
44
<status>Active</status>
55
</ApexClass>

src/classes/Exceptions.cls

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*************************************************************************************************
2+
* This file is part of the Nebula Framework project, released under the MIT License. *
3+
* See LICENSE file or go to https://github.com/jongpie/NebulaFramework for full license details. *
4+
*************************************************************************************************/
5+
public without sharing class Exceptions {
6+
7+
public class InvalidOperationException extends Exception {}
8+
public class RecordTypeException extends Exception {}
9+
public class SObjectTriggerHandlerException extends Exception {}
10+
11+
}

0 commit comments

Comments
 (0)