Skip to content

Commit

Permalink
Merge 72aee46 into feature/integrate-npsp-tdtm
Browse files Browse the repository at this point in the history
  • Loading branch information
salesforce-org-community-ci[bot] authored Mar 28, 2022
2 parents 817c797 + 72aee46 commit 2fa53e7
Show file tree
Hide file tree
Showing 3 changed files with 856 additions and 55 deletions.
304 changes: 249 additions & 55 deletions dlrs/main/classes/RollupSummariesSelector.cls
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
* Various queries for the Lookup Rollup Summary custom object and cusotm metadata object
**/
public class RollupSummariesSelector {
@TestVisible
private CustomObjectSelector m_customObjectSelector;

@TestVisible
private CustomMetadataSelector m_customMetadataSelector;

public RollupSummariesSelector() {
Expand Down Expand Up @@ -251,7 +253,7 @@ public class RollupSummariesSelector {
/**
* Queries Custom Metadata store for rollups
**/
public class CustomMetadataSelector extends fflib_SObjectSelector {
public virtual class CustomMetadataSelector extends fflib_SObjectSelector {
private boolean forEdit = false;

public CustomMetadataSelector(boolean enforceSecurity) {
Expand Down Expand Up @@ -291,6 +293,7 @@ public class RollupSummariesSelector {
selectorFields.add(LookupRollupSummary2__mdt.TestCode2__c);
selectorFields.add(LookupRollupSummary2__mdt.TestCodeParent__c);
}

return selectorFields;
}

Expand All @@ -299,20 +302,53 @@ public class RollupSummariesSelector {
}

public List<RollupSummary> selectById(Set<String> idSet) {
return RollupSummary.toList(
Database.query(newQueryFactory().setCondition('id in :idSet').toSOQL())
);
if (this.forEdit) {
return RollupSummary.toList(
this.getLookupRollupSummariesWithParameters(
newQueryFactory().setCondition('id in :tmpVar1').toSOQL(),
new List<Object>{ idSet }
)
);
} else {
List<RollupSummary> summaries = new List<RollupSummary>();
List<SObject> records = new List<SObject>();
for (String idVal : idSet) {
SObject record = this.getLookupRollupSummaryByDeveloperNameOrId(
idVal
);
if(record != null) {
records.add(record);
}
}

return RollupSummary.toList(records);
}
}

public List<RollupSummary> selectByDeveloperName(Set<String> idSet) {
String developerName = new List<String>(idSet)[0];
return RollupSummary.toList(
Database.query(
newQueryFactory()
.setCondition('DeveloperName = :developerName')
.toSOQL()
)
);
public List<RollupSummary> selectByDeveloperName(
Set<String> developerNames
) {
String developerName = new List<String>(developerNames)[0];
if (this.forEdit) {
return RollupSummary.toList(
this.getLookupRollupSummariesWithParameters(
newQueryFactory()
.setCondition('DeveloperName = :tmpVar1')
.toSOQL(),
new List<Object>{ developerName }
)
);
} else {
List<RollupSummary> summaries = new List<RollupSummary>();
List<SObject> records = new List<SObject>();
SObject record = this.getLookupRollupSummaryByDeveloperNameOrId(developerName);
if(record != null) {
records.add(
this.getLookupRollupSummaryByDeveloperNameOrId(developerName)
);
}
return RollupSummary.toList(records);
}
}

/**
Expand All @@ -338,23 +374,51 @@ public class RollupSummariesSelector {
List<String> calculationModeNames = new List<String>();
for (RollupSummaries.CalculationMode calculationMode : calculationModes)
calculationModeNames.add(calculationMode.name());
return RollupSummary.toList(
Database.query(
newQueryFactory()
.setCondition(
'CalculationMode__c in :calculationModeNames and ChildObject__c in :childObjectNames and Active__c = true'
)
.addOrdering(
LookupRollupSummary2__mdt.ParentObject__c,
fflib_QueryFactory.SortOrder.ASCENDING
)
.addOrdering(
LookupRollupSummary2__mdt.RelationshipField__c,
fflib_QueryFactory.SortOrder.ASCENDING
)
.toSOQL()
)
);
if (this.forEdit) {
return RollupSummary.toList(
this.getLookupRollupSummariesWithParameters(
newQueryFactory()
.setCondition(
'CalculationMode__c in :tmpVar1 and ChildObject__c in :tmpVar2 and Active__c = true'
)
.addOrdering(
LookupRollupSummary2__mdt.ParentObject__c,
fflib_QueryFactory.SortOrder.ASCENDING
)
.addOrdering(
LookupRollupSummary2__mdt.RelationshipField__c,
fflib_QueryFactory.SortOrder.ASCENDING
)
.toSOQL(),
new List<Object>{ calculationModeNames, childObjectNames }
)
);
} else {
List<RollupSummary> summaries = RollupSummary.toList(
this.getLookupRollupSummaries()
);

List<RollupSummary> filteredSummaries = new List<RollupSummary>();

for (RollupSummary summary : summaries) {
if (
summary != null &&
summary.Active &&
calculationModeNames.contains(
summary.CalculationMode
) &&
childObjectNames.contains(summary.ChildObject)
) {
filteredSummaries.add(summary);
}
}

return sortSummaries(
filteredSummaries,
'ParentObject__c',
'RelationshipField__c'
);
}
}

/**
Expand All @@ -363,37 +427,167 @@ public class RollupSummariesSelector {
public List<RollupSummary> selectActiveByUniqueName(
Set<String> uniqueNames
) {
return RollupSummary.toList(
Database.query(
newQueryFactory()
.setCondition('DeveloperName in :uniqueNames and Active__c = true')
.addOrdering(
LookupRollupSummary2__mdt.ParentObject__c,
fflib_QueryFactory.SortOrder.ASCENDING
)
.addOrdering(
LookupRollupSummary2__mdt.RelationshipField__c,
fflib_QueryFactory.SortOrder.ASCENDING
)
.toSOQL()
)
);
if (this.forEdit) {
return RollupSummary.toList(
this.getLookupRollupSummariesWithParameters(
newQueryFactory()
.setCondition('DeveloperName in :tmpVar1 and Active__c = true')
.addOrdering(
LookupRollupSummary2__mdt.ParentObject__c,
fflib_QueryFactory.SortOrder.ASCENDING
)
.addOrdering(
LookupRollupSummary2__mdt.RelationshipField__c,
fflib_QueryFactory.SortOrder.ASCENDING
)
.toSOQL(),
new List<Object>{ uniqueNames }
)
);
} else {
List<RollupSummary> summaries = new List<RollupSummary>();
List<SObject> records = new List<SObject>();
for (String uniqueName : uniqueNames) {
SObject record = getLookupRollupSummaryByDeveloperNameOrId(
uniqueName
);
if (record != null && (Boolean) record.get('Active__c')) {
records.add(record);
}
}

return sortSummaries(
RollupSummary.toList(records),
'ParentObject__c',
'RelationshipField__c'
);
}
}

/**
* Select all
**/
public List<RollupSummary> selectAll() {
return RollupSummary.toList(
Database.query(
newQueryFactory()
.addOrdering(
LookupRollupSummary2__mdt.Label,
fflib_QueryFactory.SortOrder.ASCENDING
)
.toSOQL()
)
);
if (this.forEdit) {
return RollupSummary.toList(
this.getLookupRollupSummariesWithParameters(
newQueryFactory()
.addOrdering(
LookupRollupSummary2__mdt.Label,
fflib_QueryFactory.SortOrder.ASCENDING
)
.toSOQL(),
new List<Object>()
)
);
} else {
List<RollupSummary> summaries = RollupSummary.toList(
this.getLookupRollupSummaries()
);

return sortSummaries(summaries, 'Label', null);
}
}

protected virtual List<SObject> getLookupRollupSummariesWithParameters(
String queryString,
List<Object> tmpVars
) {
//Executing dynamic SOQL requires any variable binding to be in the same
//scope. We only ever use 2 variables in this class, so just hard coding
//two temporary variables for the dynamic SOQL to run
Object tmpVar1 = tmpVars.size() > 0 ? tmpVars[0] : null;
Object tmpVar2 = tmpVars.size() > 1 ? tmpVars[1] : null;

return Database.query(queryString);
}

protected virtual List<SObject> getLookupRollupSummaries() {
return LookupRollupSummary2__mdt.getAll().values();
}

protected virtual SObject getLookupRollupSummaryByDeveloperNameOrId(
String developerNameOrId
) {
return LookupRollupSummary2__mdt.getInstance(developerNameOrId);
}

private List<RollupSummary> sortSummaries(
List<RollupSummary> summaries,
String sortField,
String sortFieldSecondary
) {
List<RollupSummaryComparable> rollupSummaryComparables = new List<RollupSummaryComparable>();
for (RollupSummary summary : summaries) {
rollupSummaryComparables.add(
new RollupSummaryComparable(summary, sortField, sortFieldSecondary)
);
}

rollupSummaryComparables.sort();

List<RollupSummary> sortedSummaries = new List<RollupSummary>();

for (RollupSummaryComparable comparable : rollupSummaryComparables) {
sortedSummaries.add(comparable.summary);
}

return sortedSummaries;
}
}

private class RollupSummaryComparable implements Comparable {
private RollupSummary summary;
private String fieldToSortOn;
private String sortOrder;
private String fieldToSortOnSecondary;

private RollupSummaryComparable(
RollupSummary summary,
String fieldToSortOn,
String fieldToSortOnSecondary
) {
this.summary = summary;
this.fieldToSortOn = fieldToSortOn;
this.fieldToSortOnSecondary = fieldToSortOnSecondary;
}

public Integer compareTo(Object compareTo) {
RollupSummaryComparable compareToSummary = (RollupSummaryComparable) compareTo;

// The return value of 0 indicates that both elements are equal.
Integer returnValue = 0;
if (
String.valueOf(this.summary.record.get(this.fieldToSortOn)) >
String.valueOf(compareToSummary.summary.record.get(this.fieldToSortOn))
) {
// Set return value to a positive value.
returnValue = 1;
} else if (
String.valueOf(this.summary.record.get(this.fieldToSortOn)) <
String.valueOf(compareToSummary.summary.record.get(this.fieldToSortOn))
) {
// Set return value to a negative value.
returnValue = -1;
} else if (this.fieldToSortOnSecondary != null) {
// First value the same, consider second value
if (
String.valueOf(this.summary.record.get(this.fieldToSortOnSecondary)) >
String.valueOf(
compareToSummary.summary.record.get(this.fieldToSortOnSecondary)
)
) {
return 1;
} else if (
String.valueOf(this.summary.record.get(this.fieldToSortOnSecondary)) <
String.valueOf(
compareToSummary.summary.record.get(this.fieldToSortOnSecondary)
)
) {
return -1;
}
}
return returnValue;
}
}
}
Loading

0 comments on commit 2fa53e7

Please sign in to comment.