Skip to content

Commit

Permalink
Starting to sketch out how to persist objects that have been fabricated
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-baillie-ortoo committed Dec 2, 2021
1 parent 866bd72 commit e61067f
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 1 deletion.
7 changes: 7 additions & 0 deletions TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ Licenses that are needed with the source code and binary:

TODO:

SObject Fabricator:
* Look at subclasses for specific objects
* Setting default values
* Setting default parents
* Object register
* Persisting the result

Look at the use of 'MockDatabase' in fflib

* To finalise the core architecture:
Expand Down
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;
}
}
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>
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 );
}
}
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>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
public virtual class sfab_FabricatedSObject {

private Type sType;
@testVisible private Map<String,sfab_FabricatedSObjectNode> nodes = new Map<String,sfab_FabricatedSObjectNode>();
@testVisible protected Map<String,sfab_FabricatedSObjectNode> nodes = new Map<String,sfab_FabricatedSObjectNode>();

sfab_ObjectDescriber objectDescriber = new sfab_ObjectDescriber();

Expand Down Expand Up @@ -278,6 +278,9 @@ public virtual class sfab_FabricatedSObject {
setNode( relationshipName, new sfab_ChildRelationshipNode( relationshipName ) );
}
getChildRelationshipNode( relationshipName ).addChild( fabricatedChild );

ortoo_FabricatedSobjectRegister.registerParentOfRelationship( this, relationshipName, fabricatedChild );

}
return this;
}
Expand Down Expand Up @@ -388,6 +391,7 @@ public virtual class sfab_FabricatedSObject {
private sfab_FabricatedSObject setDirectParent( String relationshipName, sfab_FabricatedSObject fabricatedParent ) {
checkFieldIsParentRelationship( relationshipName );
checkTypeIsValidForParentRelationship( relationshipName, fabricatedParent );
ortoo_FabricatedSobjectRegister.registerChildOfRelationship( this, relationshipName, fabricatedParent );
return setNode( relationshipName, new sfab_ParentRelationshipNode(relationshipName, fabricatedParent));
}

Expand All @@ -402,6 +406,11 @@ public virtual class sfab_FabricatedSObject {
private sfab_FabricatedSObject setDirectChildren( String relationshipName, List<sfab_FabricatedSObject> fabricatedChildren ) {
childFieldIsChildRelationship( relationshipName );
checkTypeIsValidForChildRelationship( relationshipName, fabricatedChildren );

for ( sfab_FabricatedSObject thisChild : fabricatedChildren )
{
ortoo_FabricatedSobjectRegister.registerParentOfRelationship( this, relationshipName, thisChild );
}
return setNode( relationshipName, new sfab_ChildRelationshipNode( relationshipName, fabricatedChildren ) );
}

Expand All @@ -422,6 +431,7 @@ public virtual class sfab_FabricatedSObject {
private sfab_FabricatedSObject addParentChild( String fieldName, sfab_FabricatedSObject fabricatedChild ) {
sfab_FieldNameSplitter fields = defaultParentField( fieldName );
getParentRelationshipNode( fields.getParentFieldName() ).add( fields.getChildFieldName(), fabricatedChild );
// TODO: add the relatioship here too
return this;
}

Expand Down

0 comments on commit e61067f

Please sign in to comment.