forked from apex-enterprise-patterns/fflib-apex-common
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Starting to sketch out how to persist objects that have been fabricated
- Loading branch information
1 parent
866bd72
commit e61067f
Showing
6 changed files
with
155 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
framework/default/sobject-fabricator-extensions/classes/ortoo_FabricatedSObject.cls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
public virtual class ortoo_FabricatedSObject extends sfab_FabricatedSObject | ||
{ | ||
public ortoo_FabricatedSObject( Type sType ) | ||
{ | ||
super( sType ); | ||
ortoo_FabricatedSObjectRegister.registerObject( this ); | ||
} | ||
|
||
public List<sfab_ParentRelationshipNode> getParentRelationshipNodes() | ||
{ | ||
List<sfab_ParentRelationshipNode> parentNodes = new List<sfab_ParentRelationshipNode>(); | ||
for ( sfab_FabricatedSObjectNode thisNode : nodes.values() ) | ||
{ | ||
if ( thisNode instanceOf sfab_ParentRelationshipNode ) | ||
{ | ||
parentNodes.add( (sfab_ParentRelationshipNode)thisNode ); | ||
} | ||
} | ||
return parentNodes; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
framework/default/sobject-fabricator-extensions/classes/ortoo_FabricatedSObject.cls-meta.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>53.0</apiVersion> | ||
<status>Active</status> | ||
</ApexClass> |
106 changes: 106 additions & 0 deletions
106
framework/default/sobject-fabricator-extensions/classes/ortoo_FabricatedSObjectRegister.cls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
public class ortoo_FabricatedSObjectRegister { | ||
|
||
private static List<sfab_FabricatedSObject> objectRegister = new List<sfab_FabricatedSObject>(); | ||
|
||
private static Map<sfab_FabricatedSObject,Sobject> sobjectsByFabricated; | ||
|
||
private static List<Relationship> relationships = new List<Relationship>(); | ||
|
||
|
||
public static void registerObject( sfab_FabricatedSObject objectToRegister ) | ||
{ | ||
objectRegister.add( objectToRegister ); | ||
} | ||
|
||
public static void registerChildOfRelationship( sfab_FabricatedSObject child, String relationship, sfab_FabricatedSObject parent ) | ||
{ | ||
relationships.add( | ||
buildChildOfRelationship( child, relationship, parent ) | ||
); | ||
} | ||
|
||
public static void registerParentOfRelationship( sfab_FabricatedSObject parent, String relationship, sfab_FabricatedSObject child ) | ||
{ | ||
relationships.add( | ||
buildParentOfRelationship( parent, relationship, child ) | ||
); | ||
} | ||
|
||
public static void persist() | ||
{ | ||
// build all the sobjects, and key them on the fabricated version | ||
sobjectsByFabricated = new Map<sfab_FabricatedSObject,Sobject>(); | ||
for ( sfab_FabricatedSObject thisFabricatedObject : objectRegister ) | ||
{ | ||
|
||
Sobject objectToStore = thisFabricatedObject.toSObject(); | ||
// TODO: strip any lookups or similar | ||
if (objectToStore instanceOf Contact ) | ||
{ | ||
((Contact)objectToStore).Account = null; | ||
} | ||
|
||
sobjectsByFabricated.put( thisFabricatedObject, objectToStore ); | ||
} | ||
system.debug( sobjectsByFabricated.values() ); | ||
|
||
// work out the order to do things in, using the directed graph | ||
// TODO: work out the order | ||
List<SobjectType> sobjectTypes = new List<SobjectType> | ||
{ | ||
Account.sobjectType, | ||
Contact.sobjectType | ||
}; | ||
|
||
// register all the inserts | ||
ortoo_SobjectUnitOfWork uow = new ortoo_SobjectUnitOfWork( sobjectTypes ); | ||
for ( sfab_FabricatedSObject thisFabricatedObject : objectRegister ) | ||
{ | ||
uow.registerNew( sobjectsByFabricated.get( thisFabricatedObject ) ); | ||
} | ||
|
||
// register all the relationships | ||
for ( Relationship thisRelationship : relationships ) | ||
{ | ||
thisRelationship.register( uow ); | ||
} | ||
|
||
uow.commitWork(); | ||
} | ||
|
||
private inherited sharing class Relationship | ||
{ | ||
sfab_FabricatedSObject child; | ||
sfab_FabricatedSObject parent; | ||
SobjectField relationship; | ||
|
||
public Relationship( sfab_FabricatedSObject child, SobjectField relationship, sfab_FabricatedSObject parent ) | ||
{ | ||
this.parent = parent; | ||
this.relationship = relationship; | ||
this.child = child; | ||
} | ||
|
||
public void register( ortoo_SobjectUnitOfWork uow ) | ||
{ | ||
Sobject parentSobject = sobjectsByFabricated.get( parent ); | ||
Sobject childSobject = sobjectsByFabricated.get( child ); | ||
|
||
system.debug( 'registering the relationship: ' + childSobject + ' : ' + relationship + ' : ' + parentSobject ); | ||
|
||
uow.registerRelationship( childSobject, relationship, parentSobject ); | ||
} | ||
} | ||
|
||
public static Relationship buildChildOfRelationship( sfab_FabricatedSObject child, String relationship, sfab_FabricatedSObject parent ) | ||
{ | ||
// TODO: resolve the passed in relationship | ||
return new Relationship( child, Contact.AccountId, parent ); | ||
} | ||
|
||
public static Relationship buildParentOfRelationship( sfab_FabricatedSObject parent, String relationship, sfab_FabricatedSObject child ) | ||
{ | ||
// TODO: resolve the passed in relationship | ||
return new Relationship( child, Contact.AccountId, parent ); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...efault/sobject-fabricator-extensions/classes/ortoo_FabricatedSObjectRegister.cls-meta.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>53.0</apiVersion> | ||
<status>Active</status> | ||
</ApexClass> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters