Last active
April 1, 2017 16:56
-
-
Save Walletau/3566a54f45241b1da3bb5d18e586cfc3 to your computer and use it in GitHub Desktop.
RH Snip1
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
public static Boolean GroupCalculationActive = false; | |
public static List<PublicGroupAccountWrapper> createPublicGroupSet(Set<Id> accountIdsTargeted) { | |
if (!GroupCalculationActive) { | |
GroupCalculationActive = true; | |
List<PublicGroupAccountWrapper> accountsToPopulate = new List<PublicGroupAccountWrapper>(); | |
Id organisationRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get(ConfigSupport.defaultOrganisationAccountRecordType).getRecordTypeId(); | |
//Given that the hierarchy code requires the top parent record, we first obtain the master parent records | |
Set<Id> topIds = getTopAccountIds(accountIdsTargeted); | |
List<Account> topAccounts = new List<Account>(); | |
for (Account acc: [ | |
select | |
id, | |
name, | |
PublicGroupId__c, | |
ChildrenPublicGroupId__c, | |
ParentPublicGroupId__c, | |
from Account | |
where id IN :topIds | |
]) | |
topAccounts.add(acc); | |
Map<Id, HierarchyNode> accMap = getAccountHierarchiesQuick(topAccounts); | |
for (HierarchyNode hierarchyNode : accMap.values()) { | |
Account accountToConsider = hierarchyNode.node; | |
if (accountToConsider.RecordTypeId == organisationRecordTypeId) { | |
if (accountToConsider.ChildrenPublicGroupId__c == null) | |
accountsToPopulate.add(new PublicGroupAccountWrapper(accountToConsider, null, 'ChildrenPublicGroup')); | |
if (accountToConsider.PublicGroupId__c == null) | |
accountsToPopulate.add(new PublicGroupAccountWrapper(accountToConsider, null, 'PrimaryPublicGroup')); | |
if (accountToConsider.ParentPublicGroupId__c == null) | |
accountsToPopulate.add(new PublicGroupAccountWrapper(accountToConsider, null, 'ParentPublicGroup')); | |
} | |
} | |
// This is a requirement due to test classes no allowing queuables to be nested in tests. | |
if (!Test.isRunningTest()) { | |
//If public groups don't need to be created, then a hierarchy assessment can get kicked off directly | |
if (!accountsToPopulate.isEmpty()) { | |
CreatePublicGroups publicGroupCreation = new CreatePublicGroups(accountsToPopulate); | |
ID jobID = System.enqueueJob(publicGroupCreation); | |
} else { | |
SecurityUtil.InitiateProcessAccountHierarchyMembership processHierarchy = new SecurityUtil.InitiateProcessAccountHierarchyMembership(accountIdsTargeted); | |
ID jobID = System.enqueueJob(processHierarchy); | |
} | |
} | |
} | |
} | |
//Initial method to create the public groups necessary for the account | |
//This is done through a series of queues to prevent mixed DML errors | |
public class CreatePublicGroups implements Queueable { | |
List<PublicGroupAccountWrapper> publicGroupWrapperList { | |
get; | |
set; | |
} | |
public CreatePublicGroups(List<PublicGroupAccountWrapper> publicGroupsToPopulate) { | |
this.publicGroupWrapperList = publicGroupsToPopulate; | |
} | |
public void execute(QueueableContext context) { | |
publicGroupWrapperList = SecurityUtil.insertPublicGroupsFromWrapperList(publicGroupWrapperList); | |
if (!Test.isRunningTest()) { | |
UpdateAccountsWithPublicGroupIds accountUpdate = new UpdateAccountsWithPublicGroupIds(publicGroupWrapperList); | |
ID jobID = System.enqueueJob(accountUpdate); | |
} | |
} | |
} | |
public static List<PublicGroupAccountWrapper> insertPublicGroupsFromWrapperList(List<PublicGroupAccountWrapper> publicGroupWrapperList) { | |
List<Group> publicGroupsToInsert = new List<Group>(); | |
//Dictionary For PublicGroup Creation | |
Map<String, String> groupTypeToPrefix = new Map<String, String>{ | |
'ChildrenPublicGroup' => 'ChildrenOf', 'ParentPublicGroup' => 'Parents', 'PrimaryPublicGroup' => '' | |
}; | |
for (Integer i = 0; i < publicGroupWrapperList.size(); i++) { | |
Group newPublicGroup = new Group(); | |
newPublicGroup.name = ConfigSupport.defaultGroupPrefix; | |
newPublicGroup.name += groupTypeToPrefix.get(publicGroupWrapperList[i].publicGroupType); | |
newPublicGroup.name += publicGroupWrapperList[i].account.name; | |
newPublicGroup.name = newPublicGroup.name.left(40); | |
publicGroupsToInsert.add(newPublicGroup); | |
} | |
insert publicGroupsToInsert; | |
for (Integer i = 0; i < publicGroupWrapperList.size(); i++) { | |
publicGroupWrapperList[i].publicGroup = publicGroupsToInsert[i]; | |
} | |
return publicGroupWrapperList; | |
} | |
//Update the accounts with the newly created public groups | |
public class UpdateAccountsWithPublicGroupIds implements Queueable { | |
List<PublicGroupAccountWrapper> publicGroupWrapperList { | |
get; | |
set; | |
} | |
public UpdateAccountsWithPublicGroupIds(List<PublicGroupAccountWrapper> accountsToUpdate) { | |
this.publicGroupWrapperList = accountsToUpdate; | |
} | |
public void execute(QueueableContext context) { | |
SecurityUtil.mapPublicGroupsToAccounts(publicGroupWrapperList); | |
} | |
} | |
public static void mapPublicGroupsToAccounts(List<PublicGroupAccountWrapper> publicGroupWrapperList) { | |
Map<Id, Account> accountsToUpdate = new Map<Id, Account>(); | |
for (PublicGroupAccountWrapper publicGroupWrapper : publicGroupWrapperList) { | |
accountsToUpdate.put(publicGroupWrapper.account.Id, publicGroupWrapper.account); | |
} | |
for (PublicGroupAccountWrapper publicGroupWrapper : publicGroupWrapperList) { | |
if (publicGroupWrapper.publicGroupType == 'ChildrenPublicGroup') { | |
accountsToUpdate.get(publicGroupWrapper.account.Id).OrgSec_ChildrenPublicGroupId__c = publicGroupWrapper.publicGroup.Id; | |
} else if (publicGroupWrapper.publicGroupType == 'PrimaryPublicGroup') { | |
accountsToUpdate.get(publicGroupWrapper.account.Id).OrgSec_PublicGroupId__c = publicGroupWrapper.publicGroup.Id; | |
} else if (publicGroupWrapper.publicGroupType == 'ParentPublicGroup') { | |
accountsToUpdate.get(publicGroupWrapper.account.Id).OrgSec_ParentPublicGroupId__c = publicGroupWrapper.publicGroup.Id; | |
} | |
} | |
update accountsToUpdate.values(); | |
InitiateProcessAccountHierarchyMembership processHierarchy = new SecurityUtil.InitiateProcessAccountHierarchyMembership(accountIdsTargeted); | |
ID jobID = System.enqueueJob(processHierarchy); | |
} | |
//Just storing the group, the type of group being captured | |
public class PublicGroupAccountWrapper { | |
public Group publicGroup { | |
get; | |
set; | |
} | |
public Account account { | |
get; | |
set; | |
} | |
public String publicGroupType { | |
get; | |
set; | |
} | |
public PublicGroupAccountWrapper(Account account, Group publicGroup, String publicGroupType) { | |
this.publicGroup = publicGroup; | |
this.account = account; | |
this.publicGroupType = publicGroupType; | |
} | |
} | |
//Queueable to run the hierarchy process to be initiated in case the method is required from an operation where sharing metadata is modified | |
public class InitiateProcessAccountHierarchyMembership implements Queueable { | |
Set<Id> targetedAccountId { | |
get; | |
set; | |
} | |
public InitiateProcessAccountHierarchyMembership(Set<Id> targetedAccountId) { | |
this.targetedAccountId = targetedAccountId; | |
} | |
public void execute(QueueableContext context) { | |
processAccountHierarchyMembership(targetedAccountId); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment