Skip to content

Commit

Permalink
Issue SFDO-Community#229 - Ensure context is shared when rollups diff…
Browse files Browse the repository at this point in the history
…er by case only

Additional debug statements have been removed from tests in this commit.
  • Loading branch information
jondavis9898 committed Aug 20, 2015
1 parent 1633b43 commit 023ae41
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 36 deletions.
7 changes: 6 additions & 1 deletion rolluptool/src/classes/RollupService.cls
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,12 @@ global with sharing class RollupService
// Determine if an LREngine Context has been created for this parent child relationship, filter combination or underlying query type and sharing mode?
String rsfType = rsf.isAggregateBasedRollup() ? 'aggregate' : 'query';
String orderBy = String.isBlank(Lookup.FieldToOrderBy__c) ? '' : Lookup.FieldToOrderBy__c;
String contextKey = lookup.ParentObject__c + '#' + lookup.RelationshipField__c + '#' + lookup.RelationShipCriteria__c + '#' + rsfType + '#' + sharingMode + '#' + orderBy;
// Lowering case on Describable fields is only required for Legacy purposes since LookupRollupSummary__c records
// will be updated with describe names on insert/update moving forward.
// Ideally this would not be needed to save CPU cycles but including to ensure context is properly re-used when possible for
// rollups that have not been updated/inserted after the insert/update enhancement is applied
// Unable to lower RelationShipCriteria__c because of field value case-(in)sensitivity configuration
String contextKey = lookup.ParentObject__c.toLowerCase() + '#' + lookup.RelationshipField__c.toLowerCase() + '#' + lookup.RelationShipCriteria__c + '#' + rsfType + '#' + sharingMode + '#' + orderBy.toLowerCase();

LREngine.Context lreContext = engineCtxByParentRelationship.get(contextKey);
if(lreContext==null)
Expand Down
35 changes: 0 additions & 35 deletions rolluptool/src/classes/RollupServiceTest4.cls
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,6 @@ private class RollupServiceTest4 {
insert children;

// Assert limits
// TODO: Remove debug statements - in-place to get full picture when test fails
System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% Queries - before:{0} after:{1}', new List<String> { String.valueOf(beforeQueries), String.valueOf(Limits.getQueries()) }));
System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% QueryRows - before:{0} after:{1}', new List<String> { String.valueOf(beforeRows), String.valueOf(Limits.getQueryRows()) }));
System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% DMLRows - before:{0} after:{1}', new List<String> { String.valueOf(beforeDMLRows), String.valueOf(Limits.getDMLRows()) }));

// + One query on Rollup object
// + One query on LookupChild__c for rollup
System.assertEquals(beforeQueries + 2, Limits.getQueries());
Expand Down Expand Up @@ -363,11 +358,6 @@ private class RollupServiceTest4 {
insert children;

// Assert limits
// TODO: Remove debug statements - in-place to get full picture when test fails
System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% Queries - before:{0} after:{1}', new List<String> { String.valueOf(beforeQueries), String.valueOf(Limits.getQueries()) }));
System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% QueryRows - before:{0} after:{1}', new List<String> { String.valueOf(beforeRows), String.valueOf(Limits.getQueryRows()) }));
System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% DMLRows - before:{0} after:{1}', new List<String> { String.valueOf(beforeDMLRows), String.valueOf(Limits.getDMLRows()) }));

// + One query on Rollup object
// + One query on LookupChild__c for rollup A
// + One query on LookupChild__c for rollup B
Expand Down Expand Up @@ -492,11 +482,6 @@ private class RollupServiceTest4 {
insert children;

// Assert limits
// TODO: Remove debug statements - in-place to get full picture when test fails
System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% Queries - before:{0} after:{1}', new List<String> { String.valueOf(beforeQueries), String.valueOf(Limits.getQueries()) }));
System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% QueryRows - before:{0} after:{1}', new List<String> { String.valueOf(beforeRows), String.valueOf(Limits.getQueryRows()) }));
System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% DMLRows - before:{0} after:{1}', new List<String> { String.valueOf(beforeDMLRows), String.valueOf(Limits.getDMLRows()) }));

// + One query on Rollup object
// + One query on LookupChild__c for rollup
System.assertEquals(beforeQueries + 2, Limits.getQueries());
Expand Down Expand Up @@ -619,11 +604,6 @@ private class RollupServiceTest4 {
insert children;

// Assert limits
// TODO: Remove debug statements - in-place to get full picture when test fails
System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% Queries - before:{0} after:{1}', new List<String> { String.valueOf(beforeQueries), String.valueOf(Limits.getQueries()) }));
System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% QueryRows - before:{0} after:{1}', new List<String> { String.valueOf(beforeRows), String.valueOf(Limits.getQueryRows()) }));
System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% DMLRows - before:{0} after:{1}', new List<String> { String.valueOf(beforeDMLRows), String.valueOf(Limits.getDMLRows()) }));

// + One query on Rollup object
// + One query on LookupChild__c for rollup A
// + One query on LookupChild__c for rollup B
Expand Down Expand Up @@ -753,11 +733,6 @@ private class RollupServiceTest4 {
insert children;

// Assert limits
// TODO: Remove debug statements - in-place to get full picture when test fails
System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% Queries - before:{0} after:{1}', new List<String> { String.valueOf(beforeQueries), String.valueOf(Limits.getQueries()) }));
System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% QueryRows - before:{0} after:{1}', new List<String> { String.valueOf(beforeRows), String.valueOf(Limits.getQueryRows()) }));
System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% DMLRows - before:{0} after:{1}', new List<String> { String.valueOf(beforeDMLRows), String.valueOf(Limits.getDMLRows()) }));

// + One query on Rollup object
// + One query on LookupChild__c for rollup
System.assertEquals(beforeQueries + 2, Limits.getQueries());
Expand Down Expand Up @@ -886,11 +861,6 @@ private class RollupServiceTest4 {
insert children;

// Assert limits
// TODO: Remove debug statements - in-place to get full picture when test fails
System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% Queries - before:{0} after:{1}', new List<String> { String.valueOf(beforeQueries), String.valueOf(Limits.getQueries()) }));
System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% QueryRows - before:{0} after:{1}', new List<String> { String.valueOf(beforeRows), String.valueOf(Limits.getQueryRows()) }));
System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% DMLRows - before:{0} after:{1}', new List<String> { String.valueOf(beforeDMLRows), String.valueOf(Limits.getDMLRows()) }));

// + One query on Rollup object
// + One query on LookupChild__c for rollup A
// + One query on LookupChild__c for rollup B
Expand Down Expand Up @@ -1020,11 +990,6 @@ private class RollupServiceTest4 {
insert children;

// Assert limits
// TODO: Remove debug statements - in-place to get full picture when test fails
System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% Queries - before:{0} after:{1}', new List<String> { String.valueOf(beforeQueries), String.valueOf(Limits.getQueries()) }));
System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% QueryRows - before:{0} after:{1}', new List<String> { String.valueOf(beforeRows), String.valueOf(Limits.getQueryRows()) }));
System.debug(LoggingLevel.ERROR, String.format('%%LIMITS%% DMLRows - before:{0} after:{1}', new List<String> { String.valueOf(beforeDMLRows), String.valueOf(Limits.getDMLRows()) }));

// + One query on Rollup object
// + One query on LookupChild__c for rollup A
// + One query on LookupChild__c for rollup B
Expand Down
91 changes: 91 additions & 0 deletions rolluptool/src/classes/RollupSummaries.cls
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,97 @@ public class RollupSummaries extends fflib_SObjectDomain
{
super(records);
}

/**
* Before Insert processing
**/
public override void onBeforeInsert()
{
updateDescribableFieldNames();
}

/**
* Before Update processing
**/
public override void onBeforeUpdate(Map<Id,SObject> existingRecords)
{
updateDescribableFieldNames();
}

/**
* update field names using describe info
**/
private void updateDescribableFieldNames()
{
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
Map<SObjectType, Map<String, Schema.SObjectField>> gdFields = new Map<SObjectType, Map<String, Schema.SObjectField>>();
for(LookupRollupSummary__c lookupRollupSummary : (List<LookupRollupSummary__c>) Records)
{
SObjectType parentObjectType = gd.get(lookupRollupSummary.ParentObject__c);
SObjectType childObjectType = gd.get(lookupRollupSummary.ChildObject__c);
if(parentObjectType!=null && !gdFields.containsKey(parentObjectType))
gdFields.put(parentObjectType, parentObjectType.getDescribe().fields.getMap());
if(childObjectType!=null && !gdFields.containsKey(childObjectType))
gdFields.put(childObjectType, childObjectType.getDescribe().fields.getMap());
}

for(LookupRollupSummary__c lookupRollupSummary : (List<LookupRollupSummary__c>) Records)
{
// Parent Object
SObjectType parentObjectType = gd.get(lookupRollupSummary.ParentObject__c);
if(parentObjectType!=null)
lookupRollupSummary.ParentObject__c = parentObjectType.getDescribe().getName();

// Child Object
SObjectType childObjectType = gd.get(lookupRollupSummary.ChildObject__c);
if(childObjectType!=null)
lookupRollupSummary.ChildObject__c = childObjectType.getDescribe().getName();

// Child Object fields
SObjectField relationshipField = null;
SObjectField fieldToAggregate = null;
SObjectField fieldToOrderBy = null;
Map<String, Schema.SObjectField> childObjectFields = gdFields.get(childObjectType);
if(childObjectFields!=null)
{
// Relationship field
relationshipField = childObjectFields.get(lookupRollupSummary.RelationshipField__c);
if(relationshipField!=null)
lookupRollupSummary.RelationshipField__c = relationshipField.getDescribe().getName();
// Field to Aggregate
fieldToAggregate = childObjectFields.get(lookupRollupSummary.FieldToAggregate__c);
if(fieldToAggregate!=null)
lookupRollupSummary.FieldToAggregate__c = fieldToAggregate.getDescribe().getName();
// Field to Order By
if(lookupRollupSummary.FieldToOrderBy__c!=null) {
fieldToOrderBy = childObjectFields.get(lookupRollupSummary.FieldToOrderBy__c);
if(fieldToOrderBy!=null)
lookupRollupSummary.FieldToOrderBy__c = fieldToOrderBy.getDescribe().getName();
}
}
// Parent Object fields
SObjectField aggregateResultField = null;
Map<String, Schema.SObjectField> parentObjectFields = gdFields.get(parentObjectType);
if(parentObjectFields!=null)
{
// Aggregate Result field
aggregateResultField = parentObjectFields.get(lookupRollupSummary.AggregateResultField__c);
if(aggregateResultField!=null)
lookupRollupSummary.AggregateResultField__c = aggregateResultField.getDescribe().getName();
}
// Check the list of fields expressed in the relationship critiera fields
if(childObjectFields!=null && lookupRollupSummary.RelationshipCriteriaFields__c!=null)
{
List<String> relationshipCriteriaFields = new List<String>();
String[] fieldList = lookupRollupSummary.RelationshipCriteriaFields__c.split('\r\n');
for(String field : fieldList) {
SObjectField relationshipCriteriaField = childObjectFields.get(field);
relationshipCriteriaFields.add(relationshipCriteriaField !=null ? relationshipCriteriaField.getDescribe().getName() : field);
}
lookupRollupSummary.RelationshipCriteriaFields__c = String.join(relationshipCriteriaFields, '\r\n');
}
}
}

/**
* Validations for inserts and updates of records
Expand Down

0 comments on commit 023ae41

Please sign in to comment.