Skip to content

Instantly share code, notes, and snippets.

@Walletau
Created March 29, 2017 15:52
Show Gist options
  • Save Walletau/f629fe3296b3481ce4fab1c770657544 to your computer and use it in GitHub Desktop.
Save Walletau/f629fe3296b3481ce4fab1c770657544 to your computer and use it in GitHub Desktop.
/**
* Created by Nick Likane on 30/12/2016.
*/
public without sharing class SecurityUtil {
// Default up down map
public static Boolean GroupCalculationActive = false;
//////////
//Managing of specific share records
//////////
//Method takes a sObject Type and an sObject List, creates the Share record for the sObject as required
public static void manageSharing(String objectName, List<sObject> objectList, TriggerContext context) {
System.debug('xxxManageSharing');
//From individual Handler classes, this method is hit with the trigger objects as an sObject list
//This calls the utility method specific to the object type
if (objectName == 'Member') {
manageSharingMember(objectList);
} else if (objectName == 'MemberType') {
manageSharingMemberType(objectList);
} else if (objectName == 'AgeGroup') {
manageSharingAgeGroup(objectList);
} else if (objectName == 'Account') {
manageSharingAccount(objectList);
} else if (objectName == 'Season') {
manageSharingSeason(objectList);
} else if (objectName == 'Product') {
manageSharingProduct(objectList);
} else if (objectName == 'Competition') {
manageSharingCompetition(objectList);
} else if (objectName == 'CompetitionGrade') {
manageSharingCompetitionGrade(objectList);
} else if (objectName == 'CompetitionType') {
manageSharingCompetitionType(objectList);
} else if (objectName == 'Location') {
manageSharingLocation(objectList);
} else if (objectName == 'Team') {
manageSharingTeam(objectList);
} else if (objectName == 'OrganisationConfigurations') {
manageSharingOrganisationConfigurations(objectList);
} else if (objectName == 'MemberSecurityPermission') {
manageSharingMemberSecurityPermission(objectList);
} else if (objectName == 'HierarchyAllocationRule') {
manageSharingHierarchyAllocationRule(objectList, context);
} else if (objectName == 'Form') {
manageSharingForm(objectList);
} else if (objectName == 'AwardTemplate') {
manageSharingAwardTemplate(objectList);
} else if (objectName == 'ClearanceApplication') {
manageSharingClearanceApplication(objectList);
} else if (objectName == 'ClearanceSetting') {
manageSharingClearanceSetting(objectList);
} else if (objectName == 'EmailLetterhead') {
manageSharingEmailLetterhead(objectList);
} else if (objectName == 'EmailTemplate') {
manageSharingEmailTemplate(objectList);
} else if (objectName == 'MediaReleaseSchedule') {
manageSharingMediaReleaseSchedule(objectList);
} else if (objectName == 'Misconduct') {
manageSharingMisconduct(objectList);
} else if (objectName == 'SMSTemplate') {
manageSharingSMSTemplate(objectList);
} else if (objectName == 'SplitRule') {
manageSharingSplitRule(objectList);
} else if (objectName == 'StatisticsSettingTemplate') {
manageSharingStatisticsSettingTemplate(objectList);
}
}
//On provision of an sObjectList calculate the Sharing records that need to be present against the target records
//Modify sharing as necessary based on the Security Custom Setting, and any discrepancy deleting and creating
//the sharing records as necessary
public static void manageSharingMember(List<sObject> objectList) {
System.debug('xxxManageSharingMember');
//Obtain the security configuration map
Map<String, HierarchySecurity__c> hierarchySecurityMap = HierarchySecurity__c.getAll();
Set<Id> memberIdsTargetedToReview = new Set<Id>();
List<Member__Share> memberShareToInsert = new List<Member__Share>();
List<Member__Share> memberShareToDelete = new List<Member__Share>();
for (sObject memberObject : objectList) {
Member__c memberToReview = (Member__c) memberObject;
memberIdsTargetedToReview.add(memberToReview.Id);
}
//query member with organisation details
Map<Id, Member__c> membersToReview = new Map<Id, Member__c>([
SELECT
Id,
Organisation__c,
Organisation__r.OrgSec_ChildrenUserGroupId__c,
Organisation__r.OrgSec_ChildrenIncludedUserGroupId__c,
Organisation__r.OrgSec_UserGroupId__c,
Organisation__r.OrgSec_ParentUserGroupId__c,
Organisation__r.OrgSec_DirectChildrenUserGroupId__c
FROM Member__c
WHERE Id IN :memberIdsTargetedToReview
]);
Map<Id, List<Member__Share>> memberIdToMemberShare = new Map<Id, List<Member__Share>>();
List<Member__Share> memberSharesToReview = [
SELECT Id,
UserOrGroupId,
ParentId,
AccessLevel
FROM
Member__Share
WHERE RowCause = :Schema.Member__Share.rowCause.DefaultExternalSharingConfiguration__c
AND ParentId IN :memberIdsTargetedToReview
];
for (Member__Share memberShare : memberSharesToReview) {
if (!memberIdToMemberShare.containsKey(memberShare.ParentId)) {
memberIdToMemberShare.put(memberShare.ParentId, new List<Member__Share>{
memberShare
});
} else {
memberIdToMemberShare.get(memberShare.ParentId).add(memberShare);
}
}
for (Id memberId : membersToReview.keySet()) {
Member__c memberToReview = membersToReview.get(memberId);
List<Member__Share> relatedMemberShareRecords = new List<Member__Share>();
if (memberIdToMemberShare.containsKey(memberId))
relatedMemberShareRecords = memberIdToMemberShare.get(memberId);
Map<String, Member__Share> groupsPresent = new Map<String, Member__Share>();
for (Member__Share memberSharePresent: relatedMemberShareRecords) {
groupsPresent.put(memberSharePresent.UserOrGroupId + memberSharePresent.AccessLevel, memberSharePresent);
}
Set<Id> memberShareSuitable = new Set<Id>();
if (memberToReview.Organisation__c != null && hierarchySecurityMap.containsKey('Member')) {
if (memberToReview.Organisation__r.OrgSec_ParentUserGroupId__c != null &&
memberToReview.Organisation__r.OrgSec_ChildrenUserGroupId__c != null &&
memberToReview.Organisation__r.OrgSec_UserGroupId__c != null) {
String accessLevelParentUserGroup = hierarchySecurityMap.get('Member').ParentAccess__c;
if (groupsPresent.containsKey(memberToReview.Organisation__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup)) {
memberShareSuitable.add(groupsPresent.get(memberToReview.Organisation__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup).Id);
} else if (accessLevelParentUserGroup != 'None') {
memberShareToInsert.add(new Member__Share(ParentId = memberId, UserOrGroupId = memberToReview.Organisation__r.OrgSec_ParentUserGroupId__c, RowCause = Schema.Member__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelParentUserGroup));
}
String accessLevelChildUserGroup = hierarchySecurityMap.get('Member').ChildAccess__c;
if (groupsPresent.containsKey(memberToReview.Organisation__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup)) {
memberShareSuitable.add(groupsPresent.get(memberToReview.Organisation__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup).Id);
} else if (accessLevelChildUserGroup != 'None') {
memberShareToInsert.add(new Member__Share(ParentId = memberId, UserOrGroupId = memberToReview.Organisation__r.OrgSec_ChildrenUserGroupId__c, RowCause = Schema.Member__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelChildUserGroup));
}
String accessLevelPrimaryUserGroup = hierarchySecurityMap.get('Member').PrimaryAccess__c;
if (groupsPresent.containsKey(memberToReview.Organisation__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup)) {
memberShareSuitable.add(groupsPresent.get(memberToReview.Organisation__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup).Id);
} else if (accessLevelPrimaryUserGroup != 'None') {
memberShareToInsert.add(new Member__Share(ParentId = memberId, UserOrGroupId = memberToReview.Organisation__r.OrgSec_UserGroupId__c, RowCause = Schema.Member__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelPrimaryUserGroup));
}
}
}
for (Member__Share memberSharePresent: relatedMemberShareRecords) {
if (!memberShareSuitable.contains(memberSharePresent.Id)) {
memberShareToDelete.add(memberSharePresent);
}
}
}
delete memberShareToDelete;
System.debug('xxx mmm' + memberShareToInsert);
insert memberShareToInsert;
}
public static void manageSharingAgeGroup(List<sObject> objectList) {
System.debug('xxxManageSharingAgeGroup');
Map<String, HierarchySecurity__c> hierarchySecurityMap = HierarchySecurity__c.getAll();
Set<Id> ageGroupIdsTargetedToReview = new Set<Id>();
List<AgeGroup__Share> ageGroupShareToInsert = new List<AgeGroup__Share>();
List<AgeGroup__Share> ageGroupShareToDelete = new List<AgeGroup__Share>();
for (sObject ageGroupObject : objectList) {
AgeGroup__c ageGroupToReview = (AgeGroup__c) ageGroupObject;
ageGroupIdsTargetedToReview.add(ageGroupToReview.Id);
}
//query ageGroup with organisation details
Map<Id, AgeGroup__c> ageGroupsToReview = new Map<Id, AgeGroup__c>([
SELECT
Id,
DefinedBy__c,
DefinedBy__r.OrgSec_ChildrenUserGroupId__c,
DefinedBy__r.OrgSec_ChildrenIncludedUserGroupId__c,
DefinedBy__r.OrgSec_UserGroupId__c,
DefinedBy__r.OrgSec_ParentUserGroupId__c,
DefinedBy__r.OrgSec_DirectChildrenUserGroupId__c
FROM AgeGroup__c
WHERE Id IN :ageGroupIdsTargetedToReview
]);
Map<Id, List<AgeGroup__Share>> ageGroupIdToAgeGroupShare = new Map<Id, List<AgeGroup__Share>>();
List<AgeGroup__Share> ageGroupSharesToReview = [
SELECT Id,
UserOrGroupId,
ParentId,
AccessLevel
FROM
AgeGroup__Share
WHERE RowCause = :Schema.AgeGroup__Share.rowCause.DefaultExternalSharingConfiguration__c
AND ParentId IN :ageGroupIdsTargetedToReview
];
for (AgeGroup__Share ageGroupShare : ageGroupSharesToReview) {
if (!ageGroupIdToAgeGroupShare.containsKey(ageGroupShare.ParentId)) {
ageGroupIdToAgeGroupShare.put(ageGroupShare.ParentId, new List<AgeGroup__Share>{
ageGroupShare
});
} else {
ageGroupIdToAgeGroupShare.get(ageGroupShare.ParentId).add(ageGroupShare);
}
}
for (Id ageGroupId : ageGroupsToReview.keySet()) {
AgeGroup__c ageGroupToReview = ageGroupsToReview.get(ageGroupId);
List<AgeGroup__Share> relatedAgeGroupShareRecords = new List<AgeGroup__Share>();
if (ageGroupIdToAgeGroupShare.containsKey(ageGroupId))
relatedAgeGroupShareRecords = ageGroupIdToAgeGroupShare.get(ageGroupId);
Map<String, AgeGroup__Share> groupsPresent = new Map<String, AgeGroup__Share>();
for (AgeGroup__Share ageGroupSharePresent: relatedAgeGroupShareRecords) {
groupsPresent.put(ageGroupSharePresent.UserOrGroupId + ageGroupSharePresent.AccessLevel, ageGroupSharePresent);
}
Set<Id> ageGroupShareSuitable = new Set<Id>();
if (ageGroupToReview.DefinedBy__c != null && hierarchySecurityMap.containsKey('AgeGroup')) {
if (ageGroupToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c != null &&
ageGroupToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c != null &&
ageGroupToReview.DefinedBy__r.OrgSec_UserGroupId__c != null) {
String accessLevelParentUserGroup = hierarchySecurityMap.get('AgeGroup').ParentAccess__c;
if (groupsPresent.containsKey(ageGroupToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup)) {
ageGroupShareSuitable.add(groupsPresent.get(ageGroupToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup).Id);
} else if (accessLevelParentUserGroup != 'None') {
ageGroupShareToInsert.add(new AgeGroup__Share(ParentId = ageGroupId, UserOrGroupId = ageGroupToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c, RowCause = Schema.AgeGroup__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelParentUserGroup));
}
String accessLevelChildUserGroup = hierarchySecurityMap.get('AgeGroup').ChildAccess__c;
if (groupsPresent.containsKey(ageGroupToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup)) {
ageGroupShareSuitable.add(groupsPresent.get(ageGroupToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup).Id);
} else if (accessLevelChildUserGroup != 'None') {
ageGroupShareToInsert.add(new AgeGroup__Share(ParentId = ageGroupId, UserOrGroupId = ageGroupToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c, RowCause = Schema.AgeGroup__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelChildUserGroup));
}
String accessLevelPrimaryUserGroup = hierarchySecurityMap.get('AgeGroup').PrimaryAccess__c;
if (groupsPresent.containsKey(ageGroupToReview.DefinedBy__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup)) {
ageGroupShareSuitable.add(groupsPresent.get(ageGroupToReview.DefinedBy__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup).Id);
} else if (accessLevelPrimaryUserGroup != 'None') {
ageGroupShareToInsert.add(new AgeGroup__Share(ParentId = ageGroupId, UserOrGroupId = ageGroupToReview.DefinedBy__r.OrgSec_UserGroupId__c, RowCause = Schema.AgeGroup__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelPrimaryUserGroup));
}
}
}
for (AgeGroup__Share ageGroupSharePresent: relatedAgeGroupShareRecords) {
if (!ageGroupShareSuitable.contains(ageGroupSharePresent.Id)) {
ageGroupShareToDelete.add(ageGroupSharePresent);
}
}
}
delete ageGroupShareToDelete;
insert ageGroupShareToInsert;
}
public static void manageSharingMemberType(List<sObject> objectList) {
System.debug('xxxManageSharingMemberType');
Map<String, HierarchySecurity__c> hierarchySecurityMap = HierarchySecurity__c.getAll();
Set<Id> memberTypeIdsTargetedToReview = new Set<Id>();
List<MemberType__Share> memberTypeShareToInsert = new List<MemberType__Share>();
List<MemberType__Share> memberTypeShareToDelete = new List<MemberType__Share>();
for (sObject memberTypeObject : objectList) {
MemberType__c memberTypeToReview = (MemberType__c) memberTypeObject;
memberTypeIdsTargetedToReview.add(memberTypeToReview.Id);
}
//query memberType with organisation details
Map<Id, MemberType__c> memberTypesToReview = new Map<Id, MemberType__c>([
SELECT
Id,
DefinedBy__c,
DefinedBy__r.OrgSec_ChildrenUserGroupId__c,
DefinedBy__r.OrgSec_ChildrenIncludedUserGroupId__c,
DefinedBy__r.OrgSec_UserGroupId__c,
DefinedBy__r.OrgSec_ParentUserGroupId__c,
DefinedBy__r.OrgSec_DirectChildrenUserGroupId__c
FROM MemberType__c
WHERE Id IN :memberTypeIdsTargetedToReview
]);
Map<Id, List<MemberType__Share>> memberTypeIdToMemberTypeShare = new Map<Id, List<MemberType__Share>>();
List<MemberType__Share> memberTypeSharesToReview = [
SELECT Id,
UserOrGroupId,
ParentId,
AccessLevel
FROM
MemberType__Share
WHERE RowCause = :Schema.MemberType__Share.rowCause.DefaultExternalSharingConfiguration__c
AND ParentId IN :memberTypeIdsTargetedToReview
];
for (MemberType__Share memberTypeShare : memberTypeSharesToReview) {
if (!memberTypeIdToMemberTypeShare.containsKey(memberTypeShare.ParentId)) {
memberTypeIdToMemberTypeShare.put(memberTypeShare.ParentId, new List<MemberType__Share>{
memberTypeShare
});
} else {
memberTypeIdToMemberTypeShare.get(memberTypeShare.ParentId).add(memberTypeShare);
}
}
for (Id memberTypeId : memberTypesToReview.keySet()) {
MemberType__c memberTypeToReview = memberTypesToReview.get(memberTypeId);
List<MemberType__Share> relatedMemberTypeShareRecords = new List<MemberType__Share>();
if (memberTypeIdToMemberTypeShare.containsKey(memberTypeId))
relatedMemberTypeShareRecords = memberTypeIdToMemberTypeShare.get(memberTypeId);
Map<String, MemberType__Share> groupsPresent = new Map<String, MemberType__Share>();
for (MemberType__Share memberTypeSharePresent: relatedMemberTypeShareRecords) {
groupsPresent.put(memberTypeSharePresent.UserOrGroupId + memberTypeSharePresent.AccessLevel, memberTypeSharePresent);
}
Set<Id> memberTypeShareSuitable = new Set<Id>();
if (memberTypeToReview.DefinedBy__c != null && hierarchySecurityMap.containsKey('MemberType')) {
if (memberTypeToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c != null &&
memberTypeToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c != null &&
memberTypeToReview.DefinedBy__r.OrgSec_UserGroupId__c != null) {
String accessLevelParentUserGroup = hierarchySecurityMap.get('MemberType').ParentAccess__c;
if (groupsPresent.containsKey(memberTypeToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup)) {
memberTypeShareSuitable.add(groupsPresent.get(memberTypeToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup).Id);
} else if (accessLevelParentUserGroup != 'None') {
memberTypeShareToInsert.add(new MemberType__Share(ParentId = memberTypeId, UserOrGroupId = memberTypeToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c, RowCause = Schema.MemberType__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelParentUserGroup));
}
String accessLevelChildUserGroup = hierarchySecurityMap.get('MemberType').ChildAccess__c;
if (groupsPresent.containsKey(memberTypeToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup)) {
memberTypeShareSuitable.add(groupsPresent.get(memberTypeToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup).Id);
} else if (accessLevelChildUserGroup != 'None') {
memberTypeShareToInsert.add(new MemberType__Share(ParentId = memberTypeId, UserOrGroupId = memberTypeToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c, RowCause = Schema.MemberType__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelChildUserGroup));
}
String accessLevelPrimaryUserGroup = hierarchySecurityMap.get('MemberType').PrimaryAccess__c;
if (groupsPresent.containsKey(memberTypeToReview.DefinedBy__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup)) {
memberTypeShareSuitable.add(groupsPresent.get(memberTypeToReview.DefinedBy__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup).Id);
} else if (accessLevelPrimaryUserGroup != 'None') {
memberTypeShareToInsert.add(new MemberType__Share(ParentId = memberTypeId, UserOrGroupId = memberTypeToReview.DefinedBy__r.OrgSec_UserGroupId__c, RowCause = Schema.MemberType__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelPrimaryUserGroup));
}
}
}
for (MemberType__Share memberTypeSharePresent: relatedMemberTypeShareRecords) {
if (!memberTypeShareSuitable.contains(memberTypeSharePresent.Id)) {
memberTypeShareToDelete.add(memberTypeSharePresent);
}
}
}
delete memberTypeShareToDelete;
insert memberTypeShareToInsert;
}
public static void manageSharingOrganisationConfigurations(List<sObject> objectList) {
System.debug('xxx manageSharingOrganisationConfigurations');
Map<String, HierarchySecurity__c> hierarchySecurityMap = HierarchySecurity__c.getAll();
Set<Id> organisationConfigurationsIdsTargetedToReview = new Set<Id>();
List<OrganisationConfigurations__Share> organisationConfigurationsShareToInsert = new List<OrganisationConfigurations__Share>();
List<OrganisationConfigurations__Share> organisationConfigurationsShareToDelete = new List<OrganisationConfigurations__Share>();
for (sObject organisationConfigurationsObject : objectList) {
OrganisationConfigurations__c organisationConfigurationsToReview = (OrganisationConfigurations__c) organisationConfigurationsObject;
organisationConfigurationsIdsTargetedToReview.add(organisationConfigurationsToReview.Id);
}
//query organisationConfigurations with organisation details
Map<Id, OrganisationConfigurations__c> organisationConfigurationssToReview = new Map<Id, OrganisationConfigurations__c>([
SELECT
Id,
Organisation__c,
Organisation__r.OrgSec_ChildrenUserGroupId__c,
Organisation__r.OrgSec_ChildrenIncludedUserGroupId__c,
Organisation__r.OrgSec_UserGroupId__c,
Organisation__r.OrgSec_ParentUserGroupId__c,
Organisation__r.OrgSec_DirectChildrenUserGroupId__c
FROM OrganisationConfigurations__c
WHERE Id IN :organisationConfigurationsIdsTargetedToReview
]);
Map<Id, List<OrganisationConfigurations__Share>> organisationConfigurationsIdToOrganisationConfigurationsShare = new Map<Id, List<OrganisationConfigurations__Share>>();
List<OrganisationConfigurations__Share> organisationConfigurationsSharesToReview = [
SELECT Id,
UserOrGroupId,
ParentId,
AccessLevel
FROM
OrganisationConfigurations__Share
WHERE RowCause = :Schema.OrganisationConfigurations__Share.rowCause.DefaultExternalSharingConfiguration__c
AND ParentId IN :organisationConfigurationsIdsTargetedToReview
];
for (OrganisationConfigurations__Share organisationConfigurationsShare : organisationConfigurationsSharesToReview) {
if (!organisationConfigurationsIdToOrganisationConfigurationsShare.containsKey(organisationConfigurationsShare.ParentId)) {
organisationConfigurationsIdToOrganisationConfigurationsShare.put(organisationConfigurationsShare.ParentId, new List<OrganisationConfigurations__Share>{
organisationConfigurationsShare
});
} else {
organisationConfigurationsIdToOrganisationConfigurationsShare.get(organisationConfigurationsShare.ParentId).add(organisationConfigurationsShare);
}
}
for (Id organisationConfigurationsId : organisationConfigurationssToReview.keySet()) {
OrganisationConfigurations__c organisationConfigurationsToReview = organisationConfigurationssToReview.get(organisationConfigurationsId);
List<OrganisationConfigurations__Share> relatedOrganisationConfigurationsShareRecords = new List<OrganisationConfigurations__Share>();
if (organisationConfigurationsIdToOrganisationConfigurationsShare.containsKey(organisationConfigurationsId))
relatedOrganisationConfigurationsShareRecords = organisationConfigurationsIdToOrganisationConfigurationsShare.get(organisationConfigurationsId);
Map<String, OrganisationConfigurations__Share> groupsPresent = new Map<String, OrganisationConfigurations__Share>();
for (OrganisationConfigurations__Share organisationConfigurationsSharePresent: relatedOrganisationConfigurationsShareRecords) {
groupsPresent.put(organisationConfigurationsSharePresent.UserOrGroupId + organisationConfigurationsSharePresent.AccessLevel, organisationConfigurationsSharePresent);
}
Set<Id> organisationConfigurationsShareSuitable = new Set<Id>();
if (organisationConfigurationsToReview.Organisation__c != null && hierarchySecurityMap.containsKey('OrganisationConfigurations')) {
if (organisationConfigurationsToReview.Organisation__r.OrgSec_ParentUserGroupId__c != null &&
organisationConfigurationsToReview.Organisation__r.OrgSec_ChildrenUserGroupId__c != null &&
organisationConfigurationsToReview.Organisation__r.OrgSec_UserGroupId__c != null) {
String accessLevelParentUserGroup = hierarchySecurityMap.get('OrganisationConfigurations').ParentAccess__c;
if (groupsPresent.containsKey(organisationConfigurationsToReview.Organisation__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup)) {
organisationConfigurationsShareSuitable.add(groupsPresent.get(organisationConfigurationsToReview.Organisation__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup).Id);
} else if (accessLevelParentUserGroup != 'None') {
organisationConfigurationsShareToInsert.add(new OrganisationConfigurations__Share(ParentId = organisationConfigurationsId, UserOrGroupId = organisationConfigurationsToReview.Organisation__r.OrgSec_ParentUserGroupId__c, RowCause = Schema.OrganisationConfigurations__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelParentUserGroup));
}
String accessLevelChildUserGroup = hierarchySecurityMap.get('OrganisationConfigurations').ChildAccess__c;
if (groupsPresent.containsKey(organisationConfigurationsToReview.Organisation__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup)) {
organisationConfigurationsShareSuitable.add(groupsPresent.get(organisationConfigurationsToReview.Organisation__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup).Id);
} else if (accessLevelChildUserGroup != 'None') {
organisationConfigurationsShareToInsert.add(new OrganisationConfigurations__Share(ParentId = organisationConfigurationsId, UserOrGroupId = organisationConfigurationsToReview.Organisation__r.OrgSec_ChildrenUserGroupId__c, RowCause = Schema.OrganisationConfigurations__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelChildUserGroup));
}
String accessLevelPrimaryUserGroup = hierarchySecurityMap.get('OrganisationConfigurations').PrimaryAccess__c;
if (groupsPresent.containsKey(organisationConfigurationsToReview.Organisation__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup)) {
organisationConfigurationsShareSuitable.add(groupsPresent.get(organisationConfigurationsToReview.Organisation__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup).Id);
} else if (accessLevelPrimaryUserGroup != 'None') {
organisationConfigurationsShareToInsert.add(new OrganisationConfigurations__Share(ParentId = organisationConfigurationsId, UserOrGroupId = organisationConfigurationsToReview.Organisation__r.OrgSec_UserGroupId__c, RowCause = Schema.OrganisationConfigurations__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelPrimaryUserGroup));
}
}
}
for (OrganisationConfigurations__Share organisationConfigurationsSharePresent: relatedOrganisationConfigurationsShareRecords) {
if (!organisationConfigurationsShareSuitable.contains(organisationConfigurationsSharePresent.Id)) {
organisationConfigurationsShareToDelete.add(organisationConfigurationsSharePresent);
}
}
}
delete organisationConfigurationsShareToDelete;
insert organisationConfigurationsShareToInsert;
}
public static void manageSharingSeason(List<sObject> objectList) {
System.debug('xxx manageSharingSeason');
Map<String, HierarchySecurity__c> hierarchySecurityMap = HierarchySecurity__c.getAll();
Set<Id> seasonIdsTargetedToReview = new Set<Id>();
List<Season__Share> seasonShareToInsert = new List<Season__Share>();
List<Season__Share> seasonShareToDelete = new List<Season__Share>();
for (sObject seasonObject : objectList) {
Season__c seasonToReview = (Season__c) seasonObject;
seasonIdsTargetedToReview.add(seasonToReview.Id);
}
//query season with organisation details
Map<Id, Season__c> seasonsToReview = new Map<Id, Season__c>([
SELECT
Id,
DefinedBy__c,
DefinedBy__r.OrgSec_ChildrenUserGroupId__c,
DefinedBy__r.OrgSec_ChildrenIncludedUserGroupId__c,
DefinedBy__r.OrgSec_UserGroupId__c,
DefinedBy__r.OrgSec_ParentUserGroupId__c,
DefinedBy__r.OrgSec_DirectChildrenUserGroupId__c
FROM Season__c
WHERE Id IN :seasonIdsTargetedToReview
]);
Map<Id, List<Season__Share>> seasonIdToSeasonShare = new Map<Id, List<Season__Share>>();
List<Season__Share> seasonSharesToReview = [
SELECT Id,
UserOrGroupId,
ParentId,
AccessLevel
FROM
Season__Share
WHERE RowCause = :Schema.Season__Share.rowCause.DefaultExternalSharingConfiguration__c
AND ParentId IN :seasonIdsTargetedToReview
];
for (Season__Share seasonShare : seasonSharesToReview) {
if (!seasonIdToSeasonShare.containsKey(seasonShare.ParentId)) {
seasonIdToSeasonShare.put(seasonShare.ParentId, new List<Season__Share>{
seasonShare
});
} else {
seasonIdToSeasonShare.get(seasonShare.ParentId).add(seasonShare);
}
}
for (Id seasonId : seasonsToReview.keySet()) {
Season__c seasonToReview = seasonsToReview.get(seasonId);
List<Season__Share> relatedSeasonShareRecords = new List<Season__Share>();
if (seasonIdToSeasonShare.containsKey(seasonId))
relatedSeasonShareRecords = seasonIdToSeasonShare.get(seasonId);
Map<String, Season__Share> groupsPresent = new Map<String, Season__Share>();
for (Season__Share seasonSharePresent: relatedSeasonShareRecords) {
groupsPresent.put(seasonSharePresent.UserOrGroupId + seasonSharePresent.AccessLevel, seasonSharePresent);
}
Set<Id> seasonShareSuitable = new Set<Id>();
if (seasonToReview.DefinedBy__c != null && hierarchySecurityMap.containsKey('Season')) {
if (seasonToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c != null &&
seasonToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c != null &&
seasonToReview.DefinedBy__r.OrgSec_UserGroupId__c != null) {
String accessLevelParentUserGroup = hierarchySecurityMap.get('Season').ParentAccess__c;
if (groupsPresent.containsKey(seasonToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup)) {
seasonShareSuitable.add(groupsPresent.get(seasonToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup).Id);
} else if (accessLevelParentUserGroup != 'None') {
seasonShareToInsert.add(new Season__Share(ParentId = seasonId, UserOrGroupId = seasonToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c, RowCause = Schema.Season__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelParentUserGroup));
}
String accessLevelChildUserGroup = hierarchySecurityMap.get('Season').ChildAccess__c;
if (groupsPresent.containsKey(seasonToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup)) {
seasonShareSuitable.add(groupsPresent.get(seasonToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup).Id);
} else if (accessLevelChildUserGroup != 'None') {
seasonShareToInsert.add(new Season__Share(ParentId = seasonId, UserOrGroupId = seasonToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c, RowCause = Schema.Season__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelChildUserGroup));
}
String accessLevelPrimaryUserGroup = hierarchySecurityMap.get('Season').PrimaryAccess__c;
if (groupsPresent.containsKey(seasonToReview.DefinedBy__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup)) {
seasonShareSuitable.add(groupsPresent.get(seasonToReview.DefinedBy__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup).Id);
} else if (accessLevelPrimaryUserGroup != 'None') {
seasonShareToInsert.add(new Season__Share(ParentId = seasonId, UserOrGroupId = seasonToReview.DefinedBy__r.OrgSec_UserGroupId__c, RowCause = Schema.Season__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelPrimaryUserGroup));
}
}
}
for (Season__Share seasonSharePresent: relatedSeasonShareRecords) {
if (!seasonShareSuitable.contains(seasonSharePresent.Id)) {
seasonShareToDelete.add(seasonSharePresent);
}
}
}
delete seasonShareToDelete;
insert seasonShareToInsert;
}
public static void manageSharingAccount(List<sObject> objectList) {
System.debug('xxx manageSharingAccount');
Map<String, HierarchySecurity__c> hierarchySecurityMap = HierarchySecurity__c.getAll();
Set<Id> accountIdsTargetedToReview = new Set<Id>();
List<AccountShare> accountShareToInsert = new List<AccountShare>();
List<AccountShare> accountShareToDelete = new List<AccountShare>();
for (sObject accountObject : objectList) {
Account accountToReview = (Account) accountObject;
accountIdsTargetedToReview.add(accountToReview.Id);
}
//query account with organisation details
Map<Id, Account> accountsToReview = new Map<Id, Account>([
SELECT
Id,
ParentId,
Parent.OrgSec_ChildrenUserGroupId__c,
Parent.OrgSec_ChildrenIncludedUserGroupId__c,
Parent.OrgSec_UserGroupId__c,
Parent.OrgSec_ParentUserGroupId__c,
Parent.OrgSec_DirectChildrenUserGroupId__c
FROM Account
WHERE Id IN :accountIdsTargetedToReview
]);
Map<Id, List<AccountShare>> accountIdToAccountShare = new Map<Id, List<AccountShare>>();
List<AccountShare> accountSharesToReview = [
SELECT Id,
UserOrGroupId,
AccountId,
AccountAccessLevel
FROM
AccountShare
WHERE RowCause = 'Manual'
AND AccountId IN :accountIdsTargetedToReview
];
for (AccountShare accountShare : accountSharesToReview) {
if (!accountIdToAccountShare.containsKey(accountShare.AccountId)) {
accountIdToAccountShare.put(accountShare.AccountId, new List<AccountShare>{
accountShare
});
} else {
accountIdToAccountShare.get(accountShare.AccountId).add(accountShare);
}
}
for (Id accountId : accountsToReview.keySet()) {
Account accountToReview = accountsToReview.get(accountId);
List<AccountShare> relatedAccountShareRecords = new List<AccountShare>();
if (accountIdToAccountShare.containsKey(accountId))
relatedAccountShareRecords = accountIdToAccountShare.get(accountId);
Map<String, AccountShare> groupsPresent = new Map<String, AccountShare>();
for (AccountShare accountSharePresent: relatedAccountShareRecords) {
groupsPresent.put(accountSharePresent.UserOrGroupId + accountSharePresent.AccountAccessLevel, accountSharePresent);
}
Set<Id> accountShareSuitable = new Set<Id>();
if (accountToReview.ParentId != null && hierarchySecurityMap.containsKey('Account')) {
if (accountToReview.Parent.OrgSec_ParentUserGroupId__c != null &&
accountToReview.Parent.OrgSec_ChildrenUserGroupId__c != null &&
accountToReview.Parent.OrgSec_UserGroupId__c != null) {
String accessLevelParentUserGroup = hierarchySecurityMap.get('Account').ParentAccess__c;
if (groupsPresent.containsKey(accountToReview.Parent.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup)) {
accountShareSuitable.add(groupsPresent.get(accountToReview.Parent.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup).Id);
} else if (accessLevelParentUserGroup != 'None') {
accountShareToInsert.add(new AccountShare(AccountId = accountId, UserOrGroupId = accountToReview.Parent.OrgSec_ParentUserGroupId__c, RowCause = 'Manual', AccountAccessLevel = accessLevelParentUserGroup, OpportunityAccessLevel = 'Read'));
}
String accessLevelChildUserGroup = hierarchySecurityMap.get('Account').ChildAccess__c;
if (groupsPresent.containsKey(accountToReview.Parent.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup)) {
accountShareSuitable.add(groupsPresent.get(accountToReview.Parent.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup).Id);
} else if (accessLevelChildUserGroup != 'None') {
accountShareToInsert.add(new AccountShare(AccountId = accountId, UserOrGroupId = accountToReview.Parent.OrgSec_ChildrenUserGroupId__c, RowCause = 'Manual', AccountAccessLevel = accessLevelChildUserGroup, OpportunityAccessLevel = 'Read'));
}
String accessLevelPrimaryUserGroup = hierarchySecurityMap.get('Account').PrimaryAccess__c;
if (groupsPresent.containsKey(accountToReview.Parent.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup)) {
accountShareSuitable.add(groupsPresent.get(accountToReview.Parent.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup).Id);
} else if (accessLevelPrimaryUserGroup != 'None') {
accountShareToInsert.add(new AccountShare(AccountId = accountId, UserOrGroupId = accountToReview.Parent.OrgSec_UserGroupId__c, RowCause = 'Manual', AccountAccessLevel = accessLevelPrimaryUserGroup, OpportunityAccessLevel = 'Read'));
}
}
}
for (AccountShare accountSharePresent: relatedAccountShareRecords) {
if (!accountShareSuitable.contains(accountSharePresent.Id)) {
accountShareToDelete.add(accountSharePresent);
}
}
}
delete accountShareToDelete;
insert accountShareToInsert;
}
public static void manageSharingProduct(List<sObject> objectList) {
System.debug('xxx manageSharingProduct');
Map<String, HierarchySecurity__c> hierarchySecurityMap = HierarchySecurity__c.getAll();
Set<Id> productIdsTargetedToReview = new Set<Id>();
List<Product__Share> productShareToInsert = new List<Product__Share>();
List<Product__Share> productShareToDelete = new List<Product__Share>();
for (sObject productObject : objectList) {
Product__c productToReview = (Product__c) productObject;
productIdsTargetedToReview.add(productToReview.Id);
}
//query product with organisation details
Map<Id, Product__c> productsToReview = new Map<Id, Product__c>([
SELECT
Id,
DefinedBy__c,
DefinedBy__r.OrgSec_ChildrenUserGroupId__c,
DefinedBy__r.OrgSec_ChildrenIncludedUserGroupId__c,
DefinedBy__r.OrgSec_UserGroupId__c,
DefinedBy__r.OrgSec_ParentUserGroupId__c,
DefinedBy__r.OrgSec_DirectChildrenUserGroupId__c
FROM Product__c
WHERE Id IN :productIdsTargetedToReview
]);
Map<Id, List<Product__Share>> productIdToProductShare = new Map<Id, List<Product__Share>>();
List<Product__Share> productSharesToReview = [
SELECT Id,
UserOrGroupId,
ParentId,
AccessLevel
FROM
Product__Share
WHERE RowCause = :Schema.Product__Share.rowCause.DefaultExternalSharingConfiguration__c
AND ParentId IN :productIdsTargetedToReview
];
for (Product__Share productShare : productSharesToReview) {
if (!productIdToProductShare.containsKey(productShare.ParentId)) {
productIdToProductShare.put(productShare.ParentId, new List<Product__Share>{
productShare
});
} else {
productIdToProductShare.get(productShare.ParentId).add(productShare);
}
}
for (Id productId : productsToReview.keySet()) {
Product__c productToReview = productsToReview.get(productId);
List<Product__Share> relatedProductShareRecords = new List<Product__Share>();
if (productIdToProductShare.containsKey(productId))
relatedProductShareRecords = productIdToProductShare.get(productId);
Map<String, Product__Share> groupsPresent = new Map<String, Product__Share>();
for (Product__Share productSharePresent: relatedProductShareRecords) {
groupsPresent.put(productSharePresent.UserOrGroupId + productSharePresent.AccessLevel, productSharePresent);
}
Set<Id> productShareSuitable = new Set<Id>();
if (productToReview.DefinedBy__c != null && hierarchySecurityMap.containsKey('Product')) {
if (productToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c != null &&
productToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c != null &&
productToReview.DefinedBy__r.OrgSec_UserGroupId__c != null) {
String accessLevelParentUserGroup = hierarchySecurityMap.get('Product').ParentAccess__c;
if (groupsPresent.containsKey(productToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup)) {
productShareSuitable.add(groupsPresent.get(productToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup).Id);
} else if (accessLevelParentUserGroup != 'None') {
productShareToInsert.add(new Product__Share(ParentId = productId, UserOrGroupId = productToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c, RowCause = Schema.Product__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelParentUserGroup));
}
String accessLevelChildUserGroup = hierarchySecurityMap.get('Product').ChildAccess__c;
if (groupsPresent.containsKey(productToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup)) {
productShareSuitable.add(groupsPresent.get(productToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup).Id);
} else if (accessLevelChildUserGroup != 'None') {
productShareToInsert.add(new Product__Share(ParentId = productId, UserOrGroupId = productToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c, RowCause = Schema.Product__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelChildUserGroup));
}
String accessLevelPrimaryUserGroup = hierarchySecurityMap.get('Product').PrimaryAccess__c;
if (groupsPresent.containsKey(productToReview.DefinedBy__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup)) {
productShareSuitable.add(groupsPresent.get(productToReview.DefinedBy__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup).Id);
} else if (accessLevelPrimaryUserGroup != 'None') {
productShareToInsert.add(new Product__Share(ParentId = productId, UserOrGroupId = productToReview.DefinedBy__r.OrgSec_UserGroupId__c, RowCause = Schema.Product__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelPrimaryUserGroup));
}
}
}
for (Product__Share productSharePresent: relatedProductShareRecords) {
if (!productShareSuitable.contains(productSharePresent.Id)) {
productShareToDelete.add(productSharePresent);
}
}
}
delete productShareToDelete;
insert productShareToInsert;
}
public static void manageSharingCompetition(List<sObject> objectList) {
System.debug('xxx manageSharingCompetition');
Map<String, HierarchySecurity__c> hierarchySecurityMap = HierarchySecurity__c.getAll();
Set<Id> competitionIdsTargetedToReview = new Set<Id>();
List<Competition__Share> competitionShareToInsert = new List<Competition__Share>();
List<Competition__Share> competitionShareToDelete = new List<Competition__Share>();
for (sObject competitionObject : objectList) {
Competition__c competitionToReview = (Competition__c) competitionObject;
competitionIdsTargetedToReview.add(competitionToReview.Id);
}
//query competition with organisation details
Map<Id, Competition__c> competitionsToReview = new Map<Id, Competition__c>([
SELECT
Id,
OrganisationOwner__c,
OrganisationOwner__r.OrgSec_ChildrenUserGroupId__c,
OrganisationOwner__r.OrgSec_ChildrenIncludedUserGroupId__c,
OrganisationOwner__r.OrgSec_UserGroupId__c,
OrganisationOwner__r.OrgSec_ParentUserGroupId__c,
OrganisationOwner__r.OrgSec_DirectChildrenUserGroupId__c
FROM Competition__c
WHERE Id IN :competitionIdsTargetedToReview
]);
Map<Id, List<Competition__Share>> competitionIdToCompetitionShare = new Map<Id, List<Competition__Share>>();
List<Competition__Share> competitionSharesToReview = [
SELECT Id,
UserOrGroupId,
ParentId,
AccessLevel
FROM
Competition__Share
WHERE RowCause = :Schema.Competition__Share.rowCause.DefaultExternalSharingConfiguration__c
AND ParentId IN :competitionIdsTargetedToReview
];
for (Competition__Share competitionShare : competitionSharesToReview) {
if (!competitionIdToCompetitionShare.containsKey(competitionShare.ParentId)) {
competitionIdToCompetitionShare.put(competitionShare.ParentId, new List<Competition__Share>{
competitionShare
});
} else {
competitionIdToCompetitionShare.get(competitionShare.ParentId).add(competitionShare);
}
}
for (Id competitionId : competitionsToReview.keySet()) {
Competition__c competitionToReview = competitionsToReview.get(competitionId);
List<Competition__Share> relatedCompetitionShareRecords = new List<Competition__Share>();
if (competitionIdToCompetitionShare.containsKey(competitionId))
relatedCompetitionShareRecords = competitionIdToCompetitionShare.get(competitionId);
Map<String, Competition__Share> groupsPresent = new Map<String, Competition__Share>();
for (Competition__Share competitionSharePresent: relatedCompetitionShareRecords) {
groupsPresent.put(competitionSharePresent.UserOrGroupId + competitionSharePresent.AccessLevel, competitionSharePresent);
}
Set<Id> competitionShareSuitable = new Set<Id>();
if (competitionToReview.OrganisationOwner__c != null && hierarchySecurityMap.containsKey('Competition')) {
if (competitionToReview.OrganisationOwner__r.OrgSec_ParentUserGroupId__c != null &&
competitionToReview.OrganisationOwner__r.OrgSec_ChildrenUserGroupId__c != null &&
competitionToReview.OrganisationOwner__r.OrgSec_UserGroupId__c != null) {
String accessLevelParentUserGroup = hierarchySecurityMap.get('Competition').ParentAccess__c;
if (groupsPresent.containsKey(competitionToReview.OrganisationOwner__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup)) {
competitionShareSuitable.add(groupsPresent.get(competitionToReview.OrganisationOwner__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup).Id);
} else if (accessLevelParentUserGroup != 'None') {
competitionShareToInsert.add(new Competition__Share(ParentId = competitionId, UserOrGroupId = competitionToReview.OrganisationOwner__r.OrgSec_ParentUserGroupId__c, RowCause = Schema.Competition__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelParentUserGroup));
}
String accessLevelChildUserGroup = hierarchySecurityMap.get('Competition').ChildAccess__c;
if (groupsPresent.containsKey(competitionToReview.OrganisationOwner__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup)) {
competitionShareSuitable.add(groupsPresent.get(competitionToReview.OrganisationOwner__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup).Id);
} else if (accessLevelChildUserGroup != 'None') {
competitionShareToInsert.add(new Competition__Share(ParentId = competitionId, UserOrGroupId = competitionToReview.OrganisationOwner__r.OrgSec_ChildrenUserGroupId__c, RowCause = Schema.Competition__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelChildUserGroup));
}
String accessLevelPrimaryUserGroup = hierarchySecurityMap.get('Competition').PrimaryAccess__c;
if (groupsPresent.containsKey(competitionToReview.OrganisationOwner__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup)) {
competitionShareSuitable.add(groupsPresent.get(competitionToReview.OrganisationOwner__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup).Id);
} else if (accessLevelPrimaryUserGroup != 'None') {
competitionShareToInsert.add(new Competition__Share(ParentId = competitionId, UserOrGroupId = competitionToReview.OrganisationOwner__r.OrgSec_UserGroupId__c, RowCause = Schema.Competition__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelPrimaryUserGroup));
}
}
}
for (Competition__Share competitionSharePresent: relatedCompetitionShareRecords) {
if (!competitionShareSuitable.contains(competitionSharePresent.Id)) {
competitionShareToDelete.add(competitionSharePresent);
}
}
}
delete competitionShareToDelete;
insert competitionShareToInsert;
}
public static void manageSharingCompetitionType(List<sObject> objectList) {
System.debug('xxx manageSharingCompetitionType');
Map<String, HierarchySecurity__c> hierarchySecurityMap = HierarchySecurity__c.getAll();
Set<Id> competitionTypeIdsTargetedToReview = new Set<Id>();
List<CompetitionType__Share> competitionTypeShareToInsert = new List<CompetitionType__Share>();
List<CompetitionType__Share> competitionTypeShareToDelete = new List<CompetitionType__Share>();
for (sObject competitionTypeObject : objectList) {
CompetitionType__c competitionTypeToReview = (CompetitionType__c) competitionTypeObject;
competitionTypeIdsTargetedToReview.add(competitionTypeToReview.Id);
}
//query competitionType with organisation details
Map<Id, CompetitionType__c> competitionTypesToReview = new Map<Id, CompetitionType__c>([
SELECT
Id,
OrganisationOwner__c,
OrganisationOwner__r.OrgSec_ChildrenUserGroupId__c,
OrganisationOwner__r.OrgSec_ChildrenIncludedUserGroupId__c,
OrganisationOwner__r.OrgSec_UserGroupId__c,
OrganisationOwner__r.OrgSec_ParentUserGroupId__c,
OrganisationOwner__r.OrgSec_DirectChildrenUserGroupId__c
FROM CompetitionType__c
WHERE Id IN :competitionTypeIdsTargetedToReview
]);
Map<Id, List<CompetitionType__Share>> competitionTypeIdToCompetitionTypeShare = new Map<Id, List<CompetitionType__Share>>();
List<CompetitionType__Share> competitionTypeSharesToReview = [
SELECT Id,
UserOrGroupId,
ParentId,
AccessLevel
FROM
CompetitionType__Share
WHERE RowCause = :Schema.CompetitionType__Share.rowCause.DefaultExternalSharingConfiguration__c
AND ParentId IN :competitionTypeIdsTargetedToReview
];
for (CompetitionType__Share competitionTypeShare : competitionTypeSharesToReview) {
if (!competitionTypeIdToCompetitionTypeShare.containsKey(competitionTypeShare.ParentId)) {
competitionTypeIdToCompetitionTypeShare.put(competitionTypeShare.ParentId, new List<CompetitionType__Share>{
competitionTypeShare
});
} else {
competitionTypeIdToCompetitionTypeShare.get(competitionTypeShare.ParentId).add(competitionTypeShare);
}
}
for (Id competitionTypeId : competitionTypesToReview.keySet()) {
CompetitionType__c competitionTypeToReview = competitionTypesToReview.get(competitionTypeId);
List<CompetitionType__Share> relatedCompetitionTypeShareRecords = new List<CompetitionType__Share>();
if (competitionTypeIdToCompetitionTypeShare.containsKey(competitionTypeId))
relatedCompetitionTypeShareRecords = competitionTypeIdToCompetitionTypeShare.get(competitionTypeId);
Map<String, CompetitionType__Share> groupsPresent = new Map<String, CompetitionType__Share>();
for (CompetitionType__Share competitionTypeSharePresent: relatedCompetitionTypeShareRecords) {
groupsPresent.put(competitionTypeSharePresent.UserOrGroupId + competitionTypeSharePresent.AccessLevel, competitionTypeSharePresent);
}
Set<Id> competitionTypeShareSuitable = new Set<Id>();
if (competitionTypeToReview.OrganisationOwner__c != null && hierarchySecurityMap.containsKey('CompetitionType')) {
if (competitionTypeToReview.OrganisationOwner__r.OrgSec_ParentUserGroupId__c != null &&
competitionTypeToReview.OrganisationOwner__r.OrgSec_ChildrenUserGroupId__c != null &&
competitionTypeToReview.OrganisationOwner__r.OrgSec_UserGroupId__c != null) {
String accessLevelParentUserGroup = hierarchySecurityMap.get('CompetitionType').ParentAccess__c;
if (groupsPresent.containsKey(competitionTypeToReview.OrganisationOwner__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup)) {
competitionTypeShareSuitable.add(groupsPresent.get(competitionTypeToReview.OrganisationOwner__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup).Id);
} else if (accessLevelParentUserGroup != 'None') {
competitionTypeShareToInsert.add(new CompetitionType__Share(ParentId = competitionTypeId, UserOrGroupId = competitionTypeToReview.OrganisationOwner__r.OrgSec_ParentUserGroupId__c, RowCause = Schema.CompetitionType__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelParentUserGroup));
}
String accessLevelChildUserGroup = hierarchySecurityMap.get('CompetitionType').ChildAccess__c;
if (groupsPresent.containsKey(competitionTypeToReview.OrganisationOwner__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup)) {
competitionTypeShareSuitable.add(groupsPresent.get(competitionTypeToReview.OrganisationOwner__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup).Id);
} else if (accessLevelChildUserGroup != 'None') {
competitionTypeShareToInsert.add(new CompetitionType__Share(ParentId = competitionTypeId, UserOrGroupId = competitionTypeToReview.OrganisationOwner__r.OrgSec_ChildrenUserGroupId__c, RowCause = Schema.CompetitionType__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelChildUserGroup));
}
String accessLevelPrimaryUserGroup = hierarchySecurityMap.get('CompetitionType').PrimaryAccess__c;
if (groupsPresent.containsKey(competitionTypeToReview.OrganisationOwner__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup)) {
competitionTypeShareSuitable.add(groupsPresent.get(competitionTypeToReview.OrganisationOwner__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup).Id);
} else if (accessLevelPrimaryUserGroup != 'None') {
competitionTypeShareToInsert.add(new CompetitionType__Share(ParentId = competitionTypeId, UserOrGroupId = competitionTypeToReview.OrganisationOwner__r.OrgSec_UserGroupId__c, RowCause = Schema.CompetitionType__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelPrimaryUserGroup));
}
}
}
for (CompetitionType__Share competitionTypeSharePresent: relatedCompetitionTypeShareRecords) {
if (!competitionTypeShareSuitable.contains(competitionTypeSharePresent.Id)) {
competitionTypeShareToDelete.add(competitionTypeSharePresent);
}
}
}
delete competitionTypeShareToDelete;
insert competitionTypeShareToInsert;
}
public static void manageSharingCompetitionGrade(List<sObject> objectList) {
System.debug('xxx manageSharingCompetitionGrade');
Map<String, HierarchySecurity__c> hierarchySecurityMap = HierarchySecurity__c.getAll();
Set<Id> competitionGradeIdsTargetedToReview = new Set<Id>();
List<CompetitionGrade__Share> competitionGradeShareToInsert = new List<CompetitionGrade__Share>();
List<CompetitionGrade__Share> competitionGradeShareToDelete = new List<CompetitionGrade__Share>();
for (sObject competitionGradeObject : objectList) {
CompetitionGrade__c competitionGradeToReview = (CompetitionGrade__c) competitionGradeObject;
competitionGradeIdsTargetedToReview.add(competitionGradeToReview.Id);
}
//query competitionGrade with organisation details
Map<Id, CompetitionGrade__c> competitionGradesToReview = new Map<Id, CompetitionGrade__c>([
SELECT
Id,
OrganisationOwner__c,
OrganisationOwner__r.OrgSec_ChildrenUserGroupId__c,
OrganisationOwner__r.OrgSec_ChildrenIncludedUserGroupId__c,
OrganisationOwner__r.OrgSec_UserGroupId__c,
OrganisationOwner__r.OrgSec_ParentUserGroupId__c,
OrganisationOwner__r.OrgSec_DirectChildrenUserGroupId__c
FROM CompetitionGrade__c
WHERE Id IN :competitionGradeIdsTargetedToReview
]);
Map<Id, List<CompetitionGrade__Share>> competitionGradeIdToCompetitionGradeShare = new Map<Id, List<CompetitionGrade__Share>>();
List<CompetitionGrade__Share> competitionGradeSharesToReview = [
SELECT Id,
UserOrGroupId,
ParentId,
AccessLevel
FROM
CompetitionGrade__Share
WHERE RowCause = :Schema.CompetitionGrade__Share.rowCause.DefaultExternalSharingConfiguration__c
AND ParentId IN :competitionGradeIdsTargetedToReview
];
for (CompetitionGrade__Share competitionGradeShare : competitionGradeSharesToReview) {
if (!competitionGradeIdToCompetitionGradeShare.containsKey(competitionGradeShare.ParentId)) {
competitionGradeIdToCompetitionGradeShare.put(competitionGradeShare.ParentId, new List<CompetitionGrade__Share>{
competitionGradeShare
});
} else {
competitionGradeIdToCompetitionGradeShare.get(competitionGradeShare.ParentId).add(competitionGradeShare);
}
}
for (Id competitionGradeId : competitionGradesToReview.keySet()) {
CompetitionGrade__c competitionGradeToReview = competitionGradesToReview.get(competitionGradeId);
List<CompetitionGrade__Share> relatedCompetitionGradeShareRecords = new List<CompetitionGrade__Share>();
if (competitionGradeIdToCompetitionGradeShare.containsKey(competitionGradeId))
relatedCompetitionGradeShareRecords = competitionGradeIdToCompetitionGradeShare.get(competitionGradeId);
Map<String, CompetitionGrade__Share> groupsPresent = new Map<String, CompetitionGrade__Share>();
for (CompetitionGrade__Share competitionGradeSharePresent: relatedCompetitionGradeShareRecords) {
groupsPresent.put(competitionGradeSharePresent.UserOrGroupId + competitionGradeSharePresent.AccessLevel, competitionGradeSharePresent);
}
Set<Id> competitionGradeShareSuitable = new Set<Id>();
if (competitionGradeToReview.OrganisationOwner__c != null && hierarchySecurityMap.containsKey('CompetitionGrade')) {
if (competitionGradeToReview.OrganisationOwner__r.OrgSec_ParentUserGroupId__c != null &&
competitionGradeToReview.OrganisationOwner__r.OrgSec_ChildrenUserGroupId__c != null &&
competitionGradeToReview.OrganisationOwner__r.OrgSec_UserGroupId__c != null) {
String accessLevelParentUserGroup = hierarchySecurityMap.get('CompetitionGrade').ParentAccess__c;
if (groupsPresent.containsKey(competitionGradeToReview.OrganisationOwner__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup)) {
competitionGradeShareSuitable.add(groupsPresent.get(competitionGradeToReview.OrganisationOwner__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup).Id);
} else if (accessLevelParentUserGroup != 'None') {
competitionGradeShareToInsert.add(new CompetitionGrade__Share(ParentId = competitionGradeId, UserOrGroupId = competitionGradeToReview.OrganisationOwner__r.OrgSec_ParentUserGroupId__c, RowCause = Schema.CompetitionGrade__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelParentUserGroup));
}
String accessLevelChildUserGroup = hierarchySecurityMap.get('CompetitionGrade').ChildAccess__c;
if (groupsPresent.containsKey(competitionGradeToReview.OrganisationOwner__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup)) {
competitionGradeShareSuitable.add(groupsPresent.get(competitionGradeToReview.OrganisationOwner__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup).Id);
} else if (accessLevelChildUserGroup != 'None') {
competitionGradeShareToInsert.add(new CompetitionGrade__Share(ParentId = competitionGradeId, UserOrGroupId = competitionGradeToReview.OrganisationOwner__r.OrgSec_ChildrenUserGroupId__c, RowCause = Schema.CompetitionGrade__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelChildUserGroup));
}
String accessLevelPrimaryUserGroup = hierarchySecurityMap.get('CompetitionGrade').PrimaryAccess__c;
if (groupsPresent.containsKey(competitionGradeToReview.OrganisationOwner__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup)) {
competitionGradeShareSuitable.add(groupsPresent.get(competitionGradeToReview.OrganisationOwner__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup).Id);
} else if (accessLevelChildUserGroup != 'None') {
competitionGradeShareToInsert.add(new CompetitionGrade__Share(ParentId = competitionGradeId, UserOrGroupId = competitionGradeToReview.OrganisationOwner__r.OrgSec_UserGroupId__c, RowCause = Schema.CompetitionGrade__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelPrimaryUserGroup));
}
}
}
for (CompetitionGrade__Share competitionGradeSharePresent: relatedCompetitionGradeShareRecords) {
if (!competitionGradeShareSuitable.contains(competitionGradeSharePresent.Id)) {
competitionGradeShareToDelete.add(competitionGradeSharePresent);
}
}
}
delete competitionGradeShareToDelete;
insert competitionGradeShareToInsert;
}
public static void manageSharingLocation(List<sObject> objectList) {
System.debug('xxx manageSharingLocation');
Map<String, HierarchySecurity__c> hierarchySecurityMap = HierarchySecurity__c.getAll();
Set<Id> locationIdsTargetedToReview = new Set<Id>();
List<Location__Share> locationShareToInsert = new List<Location__Share>();
List<Location__Share> locationShareToDelete = new List<Location__Share>();
for (sObject locationObject : objectList) {
Location__c locationToReview = (Location__c) locationObject;
locationIdsTargetedToReview.add(locationToReview.Id);
}
//query location with organisation details
Map<Id, Location__c> locationsToReview = new Map<Id, Location__c>([
SELECT
Id,
OrganisationOwner__c,
OrganisationOwner__r.OrgSec_ChildrenUserGroupId__c,
OrganisationOwner__r.OrgSec_ChildrenIncludedUserGroupId__c,
OrganisationOwner__r.OrgSec_UserGroupId__c,
OrganisationOwner__r.OrgSec_ParentUserGroupId__c,
OrganisationOwner__r.OrgSec_DirectChildrenUserGroupId__c
FROM Location__c
WHERE Id IN :locationIdsTargetedToReview
]);
Map<Id, List<Location__Share>> locationIdToLocationShare = new Map<Id, List<Location__Share>>();
List<Location__Share> locationSharesToReview = [
SELECT Id,
UserOrGroupId,
ParentId,
AccessLevel
FROM
Location__Share
WHERE RowCause = :Schema.Location__Share.rowCause.DefaultExternalSharingConfiguration__c
AND ParentId IN :locationIdsTargetedToReview
];
for (Location__Share locationShare : locationSharesToReview) {
if (!locationIdToLocationShare.containsKey(locationShare.ParentId)) {
locationIdToLocationShare.put(locationShare.ParentId, new List<Location__Share>{
locationShare
});
} else {
locationIdToLocationShare.get(locationShare.ParentId).add(locationShare);
}
}
for (Id locationId : locationsToReview.keySet()) {
Location__c locationToReview = locationsToReview.get(locationId);
List<Location__Share> relatedLocationShareRecords = new List<Location__Share>();
if (locationIdToLocationShare.containsKey(locationId))
relatedLocationShareRecords = locationIdToLocationShare.get(locationId);
Map<String, Location__Share> groupsPresent = new Map<String, Location__Share>();
for (Location__Share locationSharePresent: relatedLocationShareRecords) {
groupsPresent.put(locationSharePresent.UserOrGroupId + locationSharePresent.AccessLevel, locationSharePresent);
}
Set<Id> locationShareSuitable = new Set<Id>();
if (locationToReview.OrganisationOwner__c != null && hierarchySecurityMap.containsKey('Location')) {
if (locationToReview.OrganisationOwner__r.OrgSec_ParentUserGroupId__c != null &&
locationToReview.OrganisationOwner__r.OrgSec_ChildrenUserGroupId__c != null &&
locationToReview.OrganisationOwner__r.OrgSec_UserGroupId__c != null) {
String accessLevelParentUserGroup = hierarchySecurityMap.get('Location').ParentAccess__c;
if (groupsPresent.containsKey(locationToReview.OrganisationOwner__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup)) {
locationShareSuitable.add(groupsPresent.get(locationToReview.OrganisationOwner__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup).Id);
} else if (accessLevelParentUserGroup != 'None') {
locationShareToInsert.add(new Location__Share(ParentId = locationId, UserOrGroupId = locationToReview.OrganisationOwner__r.OrgSec_ParentUserGroupId__c, RowCause = Schema.Location__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelParentUserGroup));
}
String accessLevelChildUserGroup = hierarchySecurityMap.get('Location').ChildAccess__c;
if (groupsPresent.containsKey(locationToReview.OrganisationOwner__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup)) {
locationShareSuitable.add(groupsPresent.get(locationToReview.OrganisationOwner__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup).Id);
} else if (accessLevelChildUserGroup != 'None') {
locationShareToInsert.add(new Location__Share(ParentId = locationId, UserOrGroupId = locationToReview.OrganisationOwner__r.OrgSec_ChildrenUserGroupId__c, RowCause = Schema.Location__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelChildUserGroup));
}
String accessLevelPrimaryUserGroup = hierarchySecurityMap.get('Location').PrimaryAccess__c;
if (groupsPresent.containsKey(locationToReview.OrganisationOwner__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup)) {
locationShareSuitable.add(groupsPresent.get(locationToReview.OrganisationOwner__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup).Id);
} else if (accessLevelPrimaryUserGroup != 'None') {
locationShareToInsert.add(new Location__Share(ParentId = locationId, UserOrGroupId = locationToReview.OrganisationOwner__r.OrgSec_UserGroupId__c, RowCause = Schema.Location__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelPrimaryUserGroup));
}
}
}
for (Location__Share locationSharePresent: relatedLocationShareRecords) {
if (!locationShareSuitable.contains(locationSharePresent.Id)) {
locationShareToDelete.add(locationSharePresent);
}
}
}
delete locationShareToDelete;
insert locationShareToInsert;
}
public static void manageSharingTeam(List<sObject> objectList) {
System.debug('xxx manageSharingTeam');
Map<String, HierarchySecurity__c> hierarchySecurityMap = HierarchySecurity__c.getAll();
Set<Id> teamIdsTargetedToReview = new Set<Id>();
List<Team__Share> teamShareToInsert = new List<Team__Share>();
List<Team__Share> teamShareToDelete = new List<Team__Share>();
for (sObject teamObject : objectList) {
Team__c teamToReview = (Team__c) teamObject;
teamIdsTargetedToReview.add(teamToReview.Id);
}
//query team with organisation details
Map<Id, Team__c> teamsToReview = new Map<Id, Team__c>([
SELECT
Id,
TeamOwnerLegalEntity__c,
TeamOwnerLegalEntity__r.OrgSec_ChildrenUserGroupId__c,
TeamOwnerLegalEntity__r.OrgSec_ChildrenIncludedUserGroupId__c,
TeamOwnerLegalEntity__r.OrgSec_UserGroupId__c,
TeamOwnerLegalEntity__r.OrgSec_ParentUserGroupId__c,
TeamOwnerLegalEntity__r.OrgSec_DirectChildrenUserGroupId__c
FROM Team__c
WHERE Id IN :teamIdsTargetedToReview
]);
Map<Id, List<Team__Share>> teamIdToTeamShare = new Map<Id, List<Team__Share>>();
List<Team__Share> teamSharesToReview = [
SELECT Id,
UserOrGroupId,
ParentId,
AccessLevel
FROM
Team__Share
WHERE RowCause = :Schema.Team__Share.rowCause.DefaultExternalSharingConfiguration__c
AND ParentId IN :teamIdsTargetedToReview
];
for (Team__Share teamShare : teamSharesToReview) {
if (!teamIdToTeamShare.containsKey(teamShare.ParentId)) {
teamIdToTeamShare.put(teamShare.ParentId, new List<Team__Share>{
teamShare
});
} else {
teamIdToTeamShare.get(teamShare.ParentId).add(teamShare);
}
}
for (Id teamId : teamsToReview.keySet()) {
Team__c teamToReview = teamsToReview.get(teamId);
List<Team__Share> relatedTeamShareRecords = new List<Team__Share>();
if (teamIdToTeamShare.containsKey(teamId))
relatedTeamShareRecords = teamIdToTeamShare.get(teamId);
Map<String, Team__Share> groupsPresent = new Map<String, Team__Share>();
for (Team__Share teamSharePresent: relatedTeamShareRecords) {
groupsPresent.put(teamSharePresent.UserOrGroupId + teamSharePresent.AccessLevel, teamSharePresent);
}
Set<Id> teamShareSuitable = new Set<Id>();
if (teamToReview.TeamOwnerLegalEntity__c != null && hierarchySecurityMap.containsKey('Team')) {
if (teamToReview.TeamOwnerLegalEntity__r.OrgSec_ParentUserGroupId__c != null &&
teamToReview.TeamOwnerLegalEntity__r.OrgSec_ChildrenUserGroupId__c != null &&
teamToReview.TeamOwnerLegalEntity__r.OrgSec_UserGroupId__c != null) {
String accessLevelParentUserGroup = hierarchySecurityMap.get('Team').ParentAccess__c;
if (groupsPresent.containsKey(teamToReview.TeamOwnerLegalEntity__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup)) {
teamShareSuitable.add(groupsPresent.get(teamToReview.TeamOwnerLegalEntity__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup).Id);
} else if (accessLevelParentUserGroup != 'None') {
teamShareToInsert.add(new Team__Share(ParentId = teamId, UserOrGroupId = teamToReview.TeamOwnerLegalEntity__r.OrgSec_ParentUserGroupId__c, RowCause = Schema.Team__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelParentUserGroup));
}
String accessLevelChildUserGroup = hierarchySecurityMap.get('Team').ChildAccess__c;
if (groupsPresent.containsKey(teamToReview.TeamOwnerLegalEntity__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup)) {
teamShareSuitable.add(groupsPresent.get(teamToReview.TeamOwnerLegalEntity__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup).Id);
} else if (accessLevelChildUserGroup != 'None') {
teamShareToInsert.add(new Team__Share(ParentId = teamId, UserOrGroupId = teamToReview.TeamOwnerLegalEntity__r.OrgSec_ChildrenUserGroupId__c, RowCause = Schema.Team__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelChildUserGroup));
}
String accessLevelPrimaryUserGroup = hierarchySecurityMap.get('Team').PrimaryAccess__c;
if (groupsPresent.containsKey(teamToReview.TeamOwnerLegalEntity__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup)) {
teamShareSuitable.add(groupsPresent.get(teamToReview.TeamOwnerLegalEntity__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup).Id);
} else if (accessLevelPrimaryUserGroup != 'None') {
teamShareToInsert.add(new Team__Share(ParentId = teamId, UserOrGroupId = teamToReview.TeamOwnerLegalEntity__r.OrgSec_UserGroupId__c, RowCause = Schema.Team__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelPrimaryUserGroup));
}
}
}
for (Team__Share teamSharePresent: relatedTeamShareRecords) {
if (!teamShareSuitable.contains(teamSharePresent.Id)) {
teamShareToDelete.add(teamSharePresent);
}
}
}
delete teamShareToDelete;
insert teamShareToInsert;
}
public static void manageSharingMemberSecurityPermission(List<sObject> objectList) {
System.debug('xxx manageSharingMemberSecurityPermission');
Map<String, HierarchySecurity__c> hierarchySecurityMap = HierarchySecurity__c.getAll();
Set<Id> memberSecurityPermissionIdsTargetedToReview = new Set<Id>();
List<MemberSecurityPermission__Share> memberSecurityPermissionShareToInsert = new List<MemberSecurityPermission__Share>();
List<MemberSecurityPermission__Share> memberSecurityPermissionShareToDelete = new List<MemberSecurityPermission__Share>();
for (sObject memberSecurityPermissionObject : objectList) {
MemberSecurityPermission__c memberSecurityPermissionToReview = (MemberSecurityPermission__c) memberSecurityPermissionObject;
memberSecurityPermissionIdsTargetedToReview.add(memberSecurityPermissionToReview.Id);
}
//query memberSecurityPermission with organisation details
Map<Id, MemberSecurityPermission__c> memberSecurityPermissionsToReview = new Map<Id, MemberSecurityPermission__c>([
SELECT
Id,
Related3rdParty__c,
Related3rdParty__r.OrgSec_ChildrenUserGroupId__c,
Related3rdParty__r.OrgSec_ChildrenIncludedUserGroupId__c,
Related3rdParty__r.OrgSec_UserGroupId__c,
Related3rdParty__r.OrgSec_ParentUserGroupId__c,
Related3rdParty__r.OrgSec_DirectChildrenUserGroupId__c
FROM MemberSecurityPermission__c
WHERE Id IN :memberSecurityPermissionIdsTargetedToReview
]);
Map<Id, List<MemberSecurityPermission__Share>> memberSecurityPermissionIdToMemberSecurityPermissionShare = new Map<Id, List<MemberSecurityPermission__Share>>();
List<MemberSecurityPermission__Share> memberSecurityPermissionSharesToReview = [
SELECT Id,
UserOrGroupId,
ParentId,
AccessLevel
FROM
MemberSecurityPermission__Share
WHERE RowCause = :Schema.MemberSecurityPermission__Share.rowCause.DefaultExternalSharingConfiguration__c
AND ParentId IN :memberSecurityPermissionIdsTargetedToReview
];
for (MemberSecurityPermission__Share memberSecurityPermissionShare : memberSecurityPermissionSharesToReview) {
if (!memberSecurityPermissionIdToMemberSecurityPermissionShare.containsKey(memberSecurityPermissionShare.ParentId)) {
memberSecurityPermissionIdToMemberSecurityPermissionShare.put(memberSecurityPermissionShare.ParentId, new List<MemberSecurityPermission__Share>{
memberSecurityPermissionShare
});
} else {
memberSecurityPermissionIdToMemberSecurityPermissionShare.get(memberSecurityPermissionShare.ParentId).add(memberSecurityPermissionShare);
}
}
for (Id memberSecurityPermissionId : memberSecurityPermissionsToReview.keySet()) {
MemberSecurityPermission__c memberSecurityPermissionToReview = memberSecurityPermissionsToReview.get(memberSecurityPermissionId);
List<MemberSecurityPermission__Share> relatedMemberSecurityPermissionShareRecords = new List<MemberSecurityPermission__Share>();
if (memberSecurityPermissionIdToMemberSecurityPermissionShare.containsKey(memberSecurityPermissionId))
relatedMemberSecurityPermissionShareRecords = memberSecurityPermissionIdToMemberSecurityPermissionShare.get(memberSecurityPermissionId);
Map<String, MemberSecurityPermission__Share> groupsPresent = new Map<String, MemberSecurityPermission__Share>();
for (MemberSecurityPermission__Share memberSecurityPermissionSharePresent: relatedMemberSecurityPermissionShareRecords) {
groupsPresent.put(memberSecurityPermissionSharePresent.UserOrGroupId + memberSecurityPermissionSharePresent.AccessLevel, memberSecurityPermissionSharePresent);
}
Set<Id> memberSecurityPermissionShareSuitable = new Set<Id>();
if (memberSecurityPermissionToReview.Related3rdParty__c != null && hierarchySecurityMap.containsKey('MemberSecurityPermission')) {
if (memberSecurityPermissionToReview.Related3rdParty__r.OrgSec_ParentUserGroupId__c != null &&
memberSecurityPermissionToReview.Related3rdParty__r.OrgSec_ChildrenUserGroupId__c != null &&
memberSecurityPermissionToReview.Related3rdParty__r.OrgSec_UserGroupId__c != null) {
String accessLevelParentUserGroup = hierarchySecurityMap.get('MemberSecurityPermission').ParentAccess__c;
if (groupsPresent.containsKey(memberSecurityPermissionToReview.Related3rdParty__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup)) {
memberSecurityPermissionShareSuitable.add(groupsPresent.get(memberSecurityPermissionToReview.Related3rdParty__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup).Id);
} else if (accessLevelParentUserGroup != 'None') {
memberSecurityPermissionShareToInsert.add(new MemberSecurityPermission__Share(ParentId = memberSecurityPermissionId, UserOrGroupId = memberSecurityPermissionToReview.Related3rdParty__r.OrgSec_ParentUserGroupId__c, RowCause = Schema.MemberSecurityPermission__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelParentUserGroup));
}
String accessLevelChildUserGroup = hierarchySecurityMap.get('MemberSecurityPermission').ChildAccess__c;
if (groupsPresent.containsKey(memberSecurityPermissionToReview.Related3rdParty__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup)) {
memberSecurityPermissionShareSuitable.add(groupsPresent.get(memberSecurityPermissionToReview.Related3rdParty__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup).Id);
} else if (accessLevelChildUserGroup != 'None') {
memberSecurityPermissionShareToInsert.add(new MemberSecurityPermission__Share(ParentId = memberSecurityPermissionId, UserOrGroupId = memberSecurityPermissionToReview.Related3rdParty__r.OrgSec_ChildrenUserGroupId__c, RowCause = Schema.MemberSecurityPermission__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelChildUserGroup));
}
String accessLevelPrimaryUserGroup = hierarchySecurityMap.get('MemberSecurityPermission').PrimaryAccess__c;
if (groupsPresent.containsKey(memberSecurityPermissionToReview.Related3rdParty__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup)) {
memberSecurityPermissionShareSuitable.add(groupsPresent.get(memberSecurityPermissionToReview.Related3rdParty__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup).Id);
} else if (accessLevelPrimaryUserGroup != 'None') {
memberSecurityPermissionShareToInsert.add(new MemberSecurityPermission__Share(ParentId = memberSecurityPermissionId, UserOrGroupId = memberSecurityPermissionToReview.Related3rdParty__r.OrgSec_UserGroupId__c, RowCause = Schema.MemberSecurityPermission__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelPrimaryUserGroup));
}
}
}
for (MemberSecurityPermission__Share memberSecurityPermissionSharePresent: relatedMemberSecurityPermissionShareRecords) {
if (!memberSecurityPermissionShareSuitable.contains(memberSecurityPermissionSharePresent.Id)) {
memberSecurityPermissionShareToDelete.add(memberSecurityPermissionSharePresent);
}
}
}
delete memberSecurityPermissionShareToDelete;
insert memberSecurityPermissionShareToInsert;
}
public static void manageSharingHierarchyAllocationRule(List<sObject> objectList, TriggerContext context) {
System.debug('xxx manageSharingHierarchyAllocationRule');
Map<String, HierarchySecurity__c> hierarchySecurityMap = HierarchySecurity__c.getAll();
Set<Id> hierarchyAllocationRuleIdsTargetedToReview = new Set<Id>();
Set<Id> productIdsTargeted = new Set<Id>();
Set<Id> formIdsTargeted = new Set<Id>();
List<HierarchyAllocationRule__Share> hierarchyAllocationRuleShareToInsert = new List<HierarchyAllocationRule__Share>();
List<HierarchyAllocationRule__Share> hierarchyAllocationRuleShareToDelete = new List<HierarchyAllocationRule__Share>();
List<Product__Share> productShareToInsert = new List<Product__Share>();
List<Product__Share> productShareToDelete = new List<Product__Share>();
List<Form__Share> formShareToInsert = new List<Form__Share>();
List<Form__Share> formShareToDelete = new List<Form__Share>();
for (sObject hierarchyAllocationRuleObject : objectList) {
HierarchyAllocationRule__c hierarchyAllocationRuleToReview = (HierarchyAllocationRule__c) hierarchyAllocationRuleObject;
hierarchyAllocationRuleIdsTargetedToReview.add(hierarchyAllocationRuleToReview.Id);
if (hierarchyAllocationRuleToReview.RelatedProduct__c != null) {
productIdsTargeted.add(hierarchyAllocationRuleToReview.RelatedProduct__c);
}
if (hierarchyAllocationRuleToReview.RelatedForm__c != null) {
formIdsTargeted.add(hierarchyAllocationRuleToReview.RelatedForm__c);
}
if (context != null) {
if (context.OldMap != null && context.OldMap.containsKey(hierarchyAllocationRuleToReview.Id)) {
HierarchyAllocationRule__c oldHierarchyAllocationRuleToReview = (HierarchyAllocationRule__c) context.OldMap.get(hierarchyAllocationRuleToReview.Id);
if (oldHierarchyAllocationRuleToReview.RelatedProduct__c != null) {
System.debug('xxx old related product');
productIdsTargeted.add(oldHierarchyAllocationRuleToReview.RelatedProduct__c);
}
if (oldHierarchyAllocationRuleToReview.RelatedForm__c != null)
productIdsTargeted.add(oldHierarchyAllocationRuleToReview.RelatedForm__c);
}
}
}
//query hierarchyAllocationRules with organisation details
Map<Id, HierarchyAllocationRule__c> hierarchyAllocationRulesToReview = new Map<Id, HierarchyAllocationRule__c>([
SELECT
Id,
AllocatedTo__c,
AllocatedTo__r.OrgSec_UserGroupId__c,
AllocationToType__c,
RelatedProduct__c,
RelatedForm__c,
RelatedProduct__r.DefinedBy__r.OrgSec_ChildrenUserGroupId__c,
RelatedProduct__r.DefinedBy__r.OrgSec_ChildrenIncludedUserGroupId__c,
RelatedProduct__r.DefinedBy__r.OrgSec_UserGroupId__c,
RelatedProduct__r.DefinedBy__r.OrgSec_ParentUserGroupId__c,
RelatedProduct__r.DefinedBy__r.OrgSec_DirectChildrenUserGroupId__c,
RelatedForm__r.DefinedBy__r.OrgSec_ChildrenUserGroupId__c,
RelatedForm__r.DefinedBy__r.OrgSec_ChildrenIncludedUserGroupId__c,
RelatedForm__r.DefinedBy__r.OrgSec_UserGroupId__c,
RelatedForm__r.DefinedBy__r.OrgSec_ParentUserGroupId__c,
RelatedForm__r.DefinedBy__r.OrgSec_DirectChildrenUserGroupId__c
FROM HierarchyAllocationRule__c
WHERE Id IN :hierarchyAllocationRuleIdsTargetedToReview
OR RelatedForm__c IN :formIdsTargeted
OR RelatedProduct__c IN :productIdsTargeted
]);
//Get all custom shares associated to the hierarchy rule and attach it to hierarchy id
Map<Id, List<HierarchyAllocationRule__Share>> hierarchyAllocationRuleIdToHierarchyAllocationRuleShare = new Map<Id, List<HierarchyAllocationRule__Share>>();
List<HierarchyAllocationRule__Share> hierarchyAllocationRuleSharesToReview = [
SELECT Id,
UserOrGroupId,
ParentId,
AccessLevel
FROM
HierarchyAllocationRule__Share
WHERE RowCause = :Schema.HierarchyAllocationRule__Share.rowCause.DefaultExternalSharingConfiguration__c
AND ParentId IN :hierarchyAllocationRuleIdsTargetedToReview
];
for (HierarchyAllocationRule__Share hierarchyAllocationRuleShare : hierarchyAllocationRuleSharesToReview) {
if (!hierarchyAllocationRuleIdToHierarchyAllocationRuleShare.containsKey(hierarchyAllocationRuleShare.ParentId)) {
hierarchyAllocationRuleIdToHierarchyAllocationRuleShare.put(hierarchyAllocationRuleShare.ParentId, new List<HierarchyAllocationRule__Share>{
hierarchyAllocationRuleShare
});
} else {
hierarchyAllocationRuleIdToHierarchyAllocationRuleShare.get(hierarchyAllocationRuleShare.ParentId).add(hierarchyAllocationRuleShare);
}
}
//Get all hierarchy shares associated to targeted Products
Map<Id, List<Product__Share>> productIdToProductShare = new Map<Id, List<Product__Share>>();
List<Product__Share> productSharesToReview = [
SELECT Id,
UserOrGroupId,
ParentId,
AccessLevel
FROM
Product__Share
WHERE RowCause = :Schema.Product__Share.rowCause.HierarchyExternalSharingConfiguration__c
AND ParentId IN :productIdsTargeted
];
for (Product__Share productShare : productSharesToReview) {
if (!productIdToProductShare.containsKey(productShare.ParentId)) {
productIdToProductShare.put(productShare.ParentId, new List<Product__Share>{
productShare
});
} else {
productIdToProductShare.get(productShare.ParentId).add(productShare);
}
}
//Get all hierarchy shares associated to targeted Forms
Map<Id, List<Form__Share>> formIdToFormShare = new Map<Id, List<Form__Share>>();
List<Form__Share> formSharesToReview = [
SELECT Id,
UserOrGroupId,
ParentId,
AccessLevel
FROM
Form__Share
WHERE RowCause = :Schema.Form__Share.rowCause.HierarchyExternalSharingConfiguration__c
AND ParentId IN :formIdsTargeted
];
for (Form__Share formShare : formSharesToReview) {
if (!formIdToFormShare.containsKey(formShare.ParentId)) {
formIdToFormShare.put(formShare.ParentId, new List<Form__Share>{
formShare
});
} else {
formIdToFormShare.get(formShare.ParentId).add(formShare);
}
}
Set<Id> hierarchyAllocationRuleShareSuitable = new Set<Id>();
Set<Id> productShareSuitable = new Set<Id>();
Set<Id> formShareSuitable = new Set<Id>();
for (Id hierarchyAllocationRuleId : hierarchyAllocationRulesToReview.keySet()) {
HierarchyAllocationRule__c hierarchyAllocationRuleToReview = hierarchyAllocationRulesToReview.get(hierarchyAllocationRuleId);
List<HierarchyAllocationRule__Share> relatedHierarchyAllocationRuleShareRecords = new List<HierarchyAllocationRule__Share>();
List<Product__Share> relatedProductShareRecords = new List<Product__Share>();
List<Form__Share> relatedFormShareRecords = new List<Form__Share>();
if (hierarchyAllocationRuleIdToHierarchyAllocationRuleShare.containsKey(hierarchyAllocationRuleId)) {
relatedHierarchyAllocationRuleShareRecords = hierarchyAllocationRuleIdToHierarchyAllocationRuleShare.get(hierarchyAllocationRuleId);
}
if (productIdToProductShare.containsKey(hierarchyAllocationRuleToReview.RelatedProduct__c)) {
relatedProductShareRecords = productIdToProductShare.get(hierarchyAllocationRuleToReview.RelatedProduct__c);
}
if (formIdToFormShare.containsKey(hierarchyAllocationRuleToReview.RelatedForm__c)) {
relatedFormShareRecords = formIdToFormShare.get(hierarchyAllocationRuleToReview.RelatedForm__c);
}
Map<String, HierarchyAllocationRule__Share> groupsPresent = new Map<String, HierarchyAllocationRule__Share>();
Map<String, Product__Share> groupsPresentForProduct = new Map<String, Product__Share>();
Map<String, Form__Share> groupsPresentForForms = new Map<String, Form__Share>();
for (HierarchyAllocationRule__Share hierarchyAllocationRuleSharePresent: relatedHierarchyAllocationRuleShareRecords) {
groupsPresent.put(hierarchyAllocationRuleSharePresent.UserOrGroupId + hierarchyAllocationRuleSharePresent.AccessLevel, hierarchyAllocationRuleSharePresent);
}
for (Product__Share productSharePresent: relatedProductShareRecords) {
groupsPresentForProduct.put(productSharePresent.UserOrGroupId + productSharePresent.AccessLevel, productSharePresent);
}
for (Form__Share formSharePresent: relatedFormShareRecords) {
groupsPresentForForms.put(formSharePresent.UserOrGroupId + formSharePresent.AccessLevel, formSharePresent);
}
Id parentUserGroupId;
Id childrenUserGroupId;
Id userGroupId;
Id directChildrenOfGroupId;
if (hierarchyAllocationRuleToReview.RelatedProduct__c != null) {
parentUserGroupId = hierarchyAllocationRuleToReview.RelatedProduct__r.DefinedBy__r.OrgSec_ParentUserGroupId__c;
childrenUserGroupId = hierarchyAllocationRuleToReview.RelatedProduct__r.DefinedBy__r.OrgSec_ChildrenUserGroupId__c;
userGroupId = hierarchyAllocationRuleToReview.RelatedProduct__r.DefinedBy__r.OrgSec_UserGroupId__c;
directChildrenOfGroupId = hierarchyAllocationRuleToReview.RelatedProduct__r.DefinedBy__r.OrgSec_DirectChildrenUserGroupId__c;
} else if (hierarchyAllocationRuleToReview.RelatedForm__c != null) {
parentUserGroupId = hierarchyAllocationRuleToReview.RelatedForm__r.DefinedBy__r.OrgSec_ParentUserGroupId__c;
childrenUserGroupId = hierarchyAllocationRuleToReview.RelatedForm__r.DefinedBy__r.OrgSec_ChildrenUserGroupId__c;
userGroupId = hierarchyAllocationRuleToReview.RelatedForm__r.DefinedBy__r.OrgSec_UserGroupId__c;
directChildrenOfGroupId = hierarchyAllocationRuleToReview.RelatedForm__r.DefinedBy__r.OrgSec_DirectChildrenUserGroupId__c;
}
if (parentUserGroupId != null &&
childrenUserGroupId != null &&
userGroupId != null &&
hierarchySecurityMap.containsKey('HierarchyAllocationRule') &&
(hierarchyAllocationRuleToReview.AllocationToType__c != 'Specific organisation' || hierarchyAllocationRuleToReview.AllocatedTo__r.OrgSec_UserGroupId__c != null)) {
String accessLevelParentUserGroup = hierarchySecurityMap.get('HierarchyAllocationRule').ParentAccess__c;
if (groupsPresent.containsKey(parentUserGroupId + accessLevelParentUserGroup)) {
hierarchyAllocationRuleShareSuitable.add(groupsPresent.get(parentUserGroupId + accessLevelParentUserGroup).Id);
} else if (accessLevelParentUserGroup != 'None') {
hierarchyAllocationRuleShareToInsert.add(new HierarchyAllocationRule__Share(ParentId = hierarchyAllocationRuleId, UserOrGroupId = parentUserGroupId, RowCause = Schema.HierarchyAllocationRule__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelParentUserGroup));
}
String accessLevelChildUserGroup = hierarchySecurityMap.get('HierarchyAllocationRule').ChildAccess__c;
if (groupsPresent.containsKey(childrenUserGroupId + accessLevelChildUserGroup)) {
hierarchyAllocationRuleShareSuitable.add(groupsPresent.get(childrenUserGroupId + accessLevelChildUserGroup).Id);
} else if (accessLevelChildUserGroup != 'None') {
hierarchyAllocationRuleShareToInsert.add(new HierarchyAllocationRule__Share(ParentId = hierarchyAllocationRuleId, UserOrGroupId = childrenUserGroupId, RowCause = Schema.HierarchyAllocationRule__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelChildUserGroup));
}
String accessLevelPrimaryUserGroup = hierarchySecurityMap.get('HierarchyAllocationRule').PrimaryAccess__c;
if (groupsPresent.containsKey(userGroupId + accessLevelPrimaryUserGroup)) {
hierarchyAllocationRuleShareSuitable.add(groupsPresent.get(userGroupId + accessLevelPrimaryUserGroup).Id);
} else if (accessLevelPrimaryUserGroup != 'None') {
hierarchyAllocationRuleShareToInsert.add(new HierarchyAllocationRule__Share(ParentId = hierarchyAllocationRuleId, UserOrGroupId = userGroupId, RowCause = Schema.HierarchyAllocationRule__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelPrimaryUserGroup));
}
//unlike other object the specific check has to occur additionally if Allocation is to children of or a specific organisation
if (hierarchyAllocationRuleToReview.AllocationToType__c == 'Specific organisation' && hierarchyAllocationRuleToReview.AllocatedTo__r.OrgSec_UserGroupId__c != null) {
if (groupsPresent.containsKey(hierarchyAllocationRuleToReview.AllocatedTo__r.OrgSec_UserGroupId__c + 'Read')) {
hierarchyAllocationRuleShareSuitable.add(groupsPresent.get(hierarchyAllocationRuleToReview.AllocatedTo__r.OrgSec_UserGroupId__c + 'Read').Id);
} else {
hierarchyAllocationRuleShareToInsert.add(new HierarchyAllocationRule__Share(ParentId = hierarchyAllocationRuleId, UserOrGroupId = hierarchyAllocationRuleToReview.AllocatedTo__r.OrgSec_UserGroupId__c, RowCause = Schema.HierarchyAllocationRule__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = 'Read'));
}
if (groupsPresentForProduct.containsKey(hierarchyAllocationRuleToReview.AllocatedTo__r.OrgSec_UserGroupId__c + 'Read')) {
productShareSuitable.add(groupsPresentForProduct.get(hierarchyAllocationRuleToReview.AllocatedTo__r.OrgSec_UserGroupId__c + 'Read').Id);
} else if (hierarchyAllocationRuleToReview.RelatedProduct__c != null) {
productShareToInsert.add(new Product__Share(ParentId = hierarchyAllocationRuleToReview.RelatedProduct__c, UserOrGroupId = hierarchyAllocationRuleToReview.AllocatedTo__r.OrgSec_UserGroupId__c, RowCause = Schema.Product__Share.rowCause.HierarchyExternalSharingConfiguration__c, AccessLevel = 'Read'));
}
if (groupsPresentForForms.containsKey(hierarchyAllocationRuleToReview.AllocatedTo__r.OrgSec_UserGroupId__c + 'Read')) {
formShareSuitable.add(groupsPresentForForms.get(hierarchyAllocationRuleToReview.AllocatedTo__r.OrgSec_UserGroupId__c + 'Read').Id);
} else if (hierarchyAllocationRuleToReview.RelatedForm__c != null) {
formShareToInsert.add(new Form__Share(ParentId = hierarchyAllocationRuleToReview.RelatedForm__c, UserOrGroupId = hierarchyAllocationRuleToReview.AllocatedTo__r.OrgSec_UserGroupId__c, RowCause = Schema.Form__Share.rowCause.HierarchyExternalSharingConfiguration__c, AccessLevel = 'Read'));
}
} else if (hierarchyAllocationRuleToReview.AllocationToType__c == 'One level below' && directChildrenOfGroupId != null) {
if (groupsPresent.containsKey(directChildrenOfGroupId + 'Read')) {
hierarchyAllocationRuleShareSuitable.add(groupsPresent.get(directChildrenOfGroupId + 'Read').Id);
} else {
hierarchyAllocationRuleShareToInsert.add(new HierarchyAllocationRule__Share(ParentId = hierarchyAllocationRuleId, UserOrGroupId = directChildrenOfGroupId, RowCause = Schema.Product__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = 'Read'));
}
if (groupsPresentForProduct.containsKey(directChildrenOfGroupId + 'Read')) {
productShareSuitable.add(groupsPresentForProduct.get(directChildrenOfGroupId + 'Read').Id);
} else if (hierarchyAllocationRuleToReview.RelatedProduct__c != null) {
productShareToInsert.add(new Product__Share(ParentId = hierarchyAllocationRuleToReview.RelatedProduct__c, UserOrGroupId = directChildrenOfGroupId, RowCause = Schema.Product__Share.rowCause.HierarchyExternalSharingConfiguration__c, AccessLevel = 'Read'));
}
if (groupsPresentForForms.containsKey(directChildrenOfGroupId + 'Read')) {
formShareSuitable.add(groupsPresentForForms.get(directChildrenOfGroupId + 'Read').Id);
} else if (hierarchyAllocationRuleToReview.RelatedForm__c != null) {
formShareToInsert.add(new Form__Share(ParentId = hierarchyAllocationRuleToReview.RelatedForm__c, UserOrGroupId = directChildrenOfGroupId, RowCause = Schema.Form__Share.rowCause.HierarchyExternalSharingConfiguration__c, AccessLevel = 'Read'));
}
} else if (hierarchyAllocationRuleToReview.AllocationToType__c == 'All levels below') {
if (groupsPresent.containsKey(childrenUserGroupId + 'Read')) {
hierarchyAllocationRuleShareSuitable.add(groupsPresent.get(childrenUserGroupId + 'Read').Id);
} else {
hierarchyAllocationRuleShareToInsert.add(new HierarchyAllocationRule__Share(ParentId = hierarchyAllocationRuleId, UserOrGroupId = childrenUserGroupId, RowCause = Schema.Product__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = 'Read'));
}
if (groupsPresentForProduct.containsKey(childrenUserGroupId + 'Read')) {
productShareSuitable.add(groupsPresentForProduct.get(childrenUserGroupId + 'Read').Id);
} else if (hierarchyAllocationRuleToReview.RelatedProduct__c != null) {
productShareToInsert.add(new Product__Share(ParentId = hierarchyAllocationRuleToReview.RelatedProduct__c, UserOrGroupId = childrenUserGroupId, RowCause = Schema.Product__Share.rowCause.HierarchyExternalSharingConfiguration__c, AccessLevel = 'Read'));
}
if (groupsPresentForForms.containsKey(childrenUserGroupId + 'Read')) {
formShareSuitable.add(groupsPresentForForms.get(childrenUserGroupId + 'Read').Id);
} else if (hierarchyAllocationRuleToReview.RelatedForm__c != null) {
formShareToInsert.add(new Form__Share(ParentId = hierarchyAllocationRuleToReview.RelatedForm__c, UserOrGroupId = childrenUserGroupId, RowCause = Schema.Form__Share.rowCause.HierarchyExternalSharingConfiguration__c, AccessLevel = 'Read'));
}
}
}
}
//Loop through for deletion assessment
for (Id hierarchyAllocationRuleId : hierarchyAllocationRulesToReview.keySet()) {
HierarchyAllocationRule__c hierarchyAllocationRuleToReview = hierarchyAllocationRulesToReview.get(hierarchyAllocationRuleId);
List<HierarchyAllocationRule__Share> relatedHierarchyAllocationRuleShareRecords = new List<HierarchyAllocationRule__Share>();
if (hierarchyAllocationRuleIdToHierarchyAllocationRuleShare.containsKey(hierarchyAllocationRuleId)) {
relatedHierarchyAllocationRuleShareRecords = hierarchyAllocationRuleIdToHierarchyAllocationRuleShare.get(hierarchyAllocationRuleId);
}
for (HierarchyAllocationRule__Share hierarchyAllocationRuleSharePresent : relatedHierarchyAllocationRuleShareRecords) {
if (!hierarchyAllocationRuleShareSuitable.contains(hierarchyAllocationRuleSharePresent.Id)) {
hierarchyAllocationRuleShareToDelete.add(hierarchyAllocationRuleSharePresent);
}
}
}
for (Product__Share productSharePresent : productSharesToReview) {
if (!productShareSuitable.contains(productSharePresent.Id)) {
productShareToDelete.add(productSharePresent);
}
}
for (Form__Share formSharePresent : formSharesToReview) {
if (!formShareSuitable.contains(formSharePresent.Id)) {
formShareToDelete.add(formSharePresent);
}
}
delete hierarchyAllocationRuleShareToDelete;
insert hierarchyAllocationRuleShareToInsert;
delete productShareToDelete;
insert productShareToInsert;
delete formShareToDelete;
insert formShareToInsert;
}
public static void manageSharingForm(List<sObject> objectList) {
System.debug('xxx manageSharingForm');
Map<String, HierarchySecurity__c> hierarchySecurityMap = HierarchySecurity__c.getAll();
Set<Id> formIdsTargetedToReview = new Set<Id>();
List<Form__Share> formShareToInsert = new List<Form__Share>();
List<Form__Share> formShareToDelete = new List<Form__Share>();
for (sObject formObject : objectList) {
Form__c formToReview = (Form__c) formObject;
formIdsTargetedToReview.add(formToReview.Id);
}
//query form with organisation details
Map<Id, Form__c> formsToReview = new Map<Id, Form__c>([
SELECT
Id,
DefinedBy__c,
DefinedBy__r.OrgSec_ChildrenUserGroupId__c,
DefinedBy__r.OrgSec_ChildrenIncludedUserGroupId__c,
DefinedBy__r.OrgSec_UserGroupId__c,
DefinedBy__r.OrgSec_ParentUserGroupId__c,
DefinedBy__r.OrgSec_DirectChildrenUserGroupId__c
FROM Form__c
WHERE Id IN :formIdsTargetedToReview
]);
Map<Id, List<Form__Share>> formIdToFormShare = new Map<Id, List<Form__Share>>();
List<Form__Share> formSharesToReview = [
SELECT Id,
UserOrGroupId,
ParentId,
AccessLevel
FROM
Form__Share
WHERE RowCause = :Schema.Form__Share.rowCause.DefaultExternalSharingConfiguration__c
AND ParentId IN :formIdsTargetedToReview
];
for (Form__Share formShare : formSharesToReview) {
if (!formIdToFormShare.containsKey(formShare.ParentId)) {
formIdToFormShare.put(formShare.ParentId, new List<Form__Share>{
formShare
});
} else {
formIdToFormShare.get(formShare.ParentId).add(formShare);
}
}
for (Id formId : formsToReview.keySet()) {
Form__c formToReview = formsToReview.get(formId);
List<Form__Share> relatedFormShareRecords = new List<Form__Share>();
if (formIdToFormShare.containsKey(formId))
relatedFormShareRecords = formIdToFormShare.get(formId);
Map<String, Form__Share> groupsPresent = new Map<String, Form__Share>();
for (Form__Share formSharePresent: relatedFormShareRecords) {
groupsPresent.put(formSharePresent.UserOrGroupId + formSharePresent.AccessLevel, formSharePresent);
}
Set<Id> formShareSuitable = new Set<Id>();
if (formToReview.DefinedBy__c != null && hierarchySecurityMap.containsKey('Form')) {
if (formToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c != null &&
formToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c != null &&
formToReview.DefinedBy__r.OrgSec_UserGroupId__c != null) {
String accessLevelParentUserGroup = hierarchySecurityMap.get('Form').ParentAccess__c;
if (groupsPresent.containsKey(formToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup)) {
formShareSuitable.add(groupsPresent.get(formToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup).Id);
} else if (accessLevelParentUserGroup != 'None') {
formShareToInsert.add(new Form__Share(ParentId = formId, UserOrGroupId = formToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c, RowCause = Schema.Form__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelParentUserGroup));
}
String accessLevelChildUserGroup = hierarchySecurityMap.get('Form').ChildAccess__c;
if (groupsPresent.containsKey(formToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup)) {
formShareSuitable.add(groupsPresent.get(formToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup).Id);
} else if (accessLevelChildUserGroup != 'None') {
formShareToInsert.add(new Form__Share(ParentId = formId, UserOrGroupId = formToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c, RowCause = Schema.Form__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelChildUserGroup));
}
String accessLevelPrimaryUserGroup = hierarchySecurityMap.get('Form').PrimaryAccess__c;
if (groupsPresent.containsKey(formToReview.DefinedBy__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup)) {
formShareSuitable.add(groupsPresent.get(formToReview.DefinedBy__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup).Id);
} else if (accessLevelPrimaryUserGroup != 'None') {
formShareToInsert.add(new Form__Share(ParentId = formId, UserOrGroupId = formToReview.DefinedBy__r.OrgSec_UserGroupId__c, RowCause = Schema.Form__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelPrimaryUserGroup));
}
}
}
for (Form__Share formSharePresent: relatedFormShareRecords) {
if (!formShareSuitable.contains(formSharePresent.Id)) {
formShareToDelete.add(formSharePresent);
}
}
}
delete formShareToDelete;
insert formShareToInsert;
}
public static void manageSharingAwardTemplate(List<sObject> objectList) {
System.debug('xxx manageSharingAwardTemplate');
Map<String, HierarchySecurity__c> hierarchySecurityMap = HierarchySecurity__c.getAll();
Set<Id> awardTemplateIdsTargetedToReview = new Set<Id>();
List<AwardTemplate__Share> awardTemplateShareToInsert = new List<AwardTemplate__Share>();
List<AwardTemplate__Share> awardTemplateShareToDelete = new List<AwardTemplate__Share>();
for (sObject awardTemplateObject : objectList) {
AwardTemplate__c awardTemplateToReview = (AwardTemplate__c) awardTemplateObject;
awardTemplateIdsTargetedToReview.add(awardTemplateToReview.Id);
}
//query awardTemplate with organisation details
Map<Id, AwardTemplate__c> awardTemplatesToReview = new Map<Id, AwardTemplate__c>([
SELECT
Id,
DefinedBy__c,
DefinedBy__r.OrgSec_ChildrenUserGroupId__c,
DefinedBy__r.OrgSec_ChildrenIncludedUserGroupId__c,
DefinedBy__r.OrgSec_UserGroupId__c,
DefinedBy__r.OrgSec_ParentUserGroupId__c,
DefinedBy__r.OrgSec_DirectChildrenUserGroupId__c
FROM AwardTemplate__c
WHERE Id IN :awardTemplateIdsTargetedToReview
]);
Map<Id, List<AwardTemplate__Share>> awardTemplateIdToAwardTemplateShare = new Map<Id, List<AwardTemplate__Share>>();
List<AwardTemplate__Share> awardTemplateSharesToReview = [
SELECT Id,
UserOrGroupId,
ParentId,
AccessLevel
FROM
AwardTemplate__Share
WHERE RowCause = :Schema.AwardTemplate__Share.rowCause.DefaultExternalSharingConfiguration__c
AND ParentId IN :awardTemplateIdsTargetedToReview
];
for (AwardTemplate__Share awardTemplateShare : awardTemplateSharesToReview) {
if (!awardTemplateIdToAwardTemplateShare.containsKey(awardTemplateShare.ParentId)) {
awardTemplateIdToAwardTemplateShare.put(awardTemplateShare.ParentId, new List<AwardTemplate__Share>{
awardTemplateShare
});
} else {
awardTemplateIdToAwardTemplateShare.get(awardTemplateShare.ParentId).add(awardTemplateShare);
}
}
for (Id awardTemplateId : awardTemplatesToReview.keySet()) {
AwardTemplate__c awardTemplateToReview = awardTemplatesToReview.get(awardTemplateId);
List<AwardTemplate__Share> relatedAwardTemplateShareRecords = new List<AwardTemplate__Share>();
if (awardTemplateIdToAwardTemplateShare.containsKey(awardTemplateId))
relatedAwardTemplateShareRecords = awardTemplateIdToAwardTemplateShare.get(awardTemplateId);
Map<String, AwardTemplate__Share> groupsPresent = new Map<String, AwardTemplate__Share>();
for (AwardTemplate__Share awardTemplateSharePresent: relatedAwardTemplateShareRecords) {
groupsPresent.put(awardTemplateSharePresent.UserOrGroupId + awardTemplateSharePresent.AccessLevel, awardTemplateSharePresent);
}
Set<Id> awardTemplateShareSuitable = new Set<Id>();
if (awardTemplateToReview.DefinedBy__c != null && hierarchySecurityMap.containsKey('AwardTemplate')) {
if (awardTemplateToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c != null &&
awardTemplateToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c != null &&
awardTemplateToReview.DefinedBy__r.OrgSec_UserGroupId__c != null) {
String accessLevelParentUserGroup = hierarchySecurityMap.get('AwardTemplate').ParentAccess__c;
if (groupsPresent.containsKey(awardTemplateToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup)) {
awardTemplateShareSuitable.add(groupsPresent.get(awardTemplateToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup).Id);
} else if (accessLevelParentUserGroup != 'None') {
awardTemplateShareToInsert.add(new AwardTemplate__Share(ParentId = awardTemplateId, UserOrGroupId = awardTemplateToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c, RowCause = Schema.AwardTemplate__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelParentUserGroup));
}
String accessLevelChildUserGroup = hierarchySecurityMap.get('AwardTemplate').ChildAccess__c;
if (groupsPresent.containsKey(awardTemplateToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup)) {
awardTemplateShareSuitable.add(groupsPresent.get(awardTemplateToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup).Id);
} else if (accessLevelChildUserGroup != 'None') {
awardTemplateShareToInsert.add(new AwardTemplate__Share(ParentId = awardTemplateId, UserOrGroupId = awardTemplateToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c, RowCause = Schema.AwardTemplate__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelChildUserGroup));
}
String accessLevelPrimaryUserGroup = hierarchySecurityMap.get('AwardTemplate').PrimaryAccess__c;
if (groupsPresent.containsKey(awardTemplateToReview.DefinedBy__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup)) {
awardTemplateShareSuitable.add(groupsPresent.get(awardTemplateToReview.DefinedBy__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup).Id);
} else if (accessLevelPrimaryUserGroup != 'None') {
awardTemplateShareToInsert.add(new AwardTemplate__Share(ParentId = awardTemplateId, UserOrGroupId = awardTemplateToReview.DefinedBy__r.OrgSec_UserGroupId__c, RowCause = Schema.AwardTemplate__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelPrimaryUserGroup));
}
}
}
for (AwardTemplate__Share awardTemplateSharePresent: relatedAwardTemplateShareRecords) {
if (!awardTemplateShareSuitable.contains(awardTemplateSharePresent.Id)) {
awardTemplateShareToDelete.add(awardTemplateSharePresent);
}
}
}
delete awardTemplateShareToDelete;
insert awardTemplateShareToInsert;
}
public static void manageSharingClearanceApplication(List<sObject> objectList) {
System.debug('xxx manageSharingClearanceApplication');
Map<String, HierarchySecurity__c> hierarchySecurityMap = HierarchySecurity__c.getAll();
Set<Id> clearanceApplicationIdsTargetedToReview = new Set<Id>();
List<ClearanceApplication__Share> clearanceApplicationShareToInsert = new List<ClearanceApplication__Share>();
List<ClearanceApplication__Share> clearanceApplicationShareToDelete = new List<ClearanceApplication__Share>();
for (sObject clearanceApplicationObject : objectList) {
ClearanceApplication__c clearanceApplicationToReview = (ClearanceApplication__c) clearanceApplicationObject;
clearanceApplicationIdsTargetedToReview.add(clearanceApplicationToReview.Id);
}
//query clearanceApplication with organisation details
Map<Id, ClearanceApplication__c> clearanceApplicationsToReview = new Map<Id, ClearanceApplication__c>([
SELECT
Id,
LeavingOrganisation__c,
LeavingOrganisation__r.OrgSec_ChildrenUserGroupId__c,
LeavingOrganisation__r.OrgSec_ChildrenIncludedUserGroupId__c,
LeavingOrganisation__r.OrgSec_UserGroupId__c,
LeavingOrganisation__r.OrgSec_ParentUserGroupId__c,
LeavingOrganisation__r.OrgSec_DirectChildrenUserGroupId__c
FROM ClearanceApplication__c
WHERE Id IN :clearanceApplicationIdsTargetedToReview
]);
Map<Id, List<ClearanceApplication__Share>> clearanceApplicationIdToClearanceApplicationShare = new Map<Id, List<ClearanceApplication__Share>>();
List<ClearanceApplication__Share> clearanceApplicationSharesToReview = [
SELECT Id,
UserOrGroupId,
ParentId,
AccessLevel
FROM
ClearanceApplication__Share
WHERE RowCause = :Schema.ClearanceApplication__Share.rowCause.DefaultExternalSharingConfiguration__c
AND ParentId IN :clearanceApplicationIdsTargetedToReview
];
for (ClearanceApplication__Share clearanceApplicationShare : clearanceApplicationSharesToReview) {
if (!clearanceApplicationIdToClearanceApplicationShare.containsKey(clearanceApplicationShare.ParentId)) {
clearanceApplicationIdToClearanceApplicationShare.put(clearanceApplicationShare.ParentId, new List<ClearanceApplication__Share>{
clearanceApplicationShare
});
} else {
clearanceApplicationIdToClearanceApplicationShare.get(clearanceApplicationShare.ParentId).add(clearanceApplicationShare);
}
}
for (Id clearanceApplicationId : clearanceApplicationsToReview.keySet()) {
ClearanceApplication__c clearanceApplicationToReview = clearanceApplicationsToReview.get(clearanceApplicationId);
List<ClearanceApplication__Share> relatedClearanceApplicationShareRecords = new List<ClearanceApplication__Share>();
if (clearanceApplicationIdToClearanceApplicationShare.containsKey(clearanceApplicationId))
relatedClearanceApplicationShareRecords = clearanceApplicationIdToClearanceApplicationShare.get(clearanceApplicationId);
Map<String, ClearanceApplication__Share> groupsPresent = new Map<String, ClearanceApplication__Share>();
for (ClearanceApplication__Share clearanceApplicationSharePresent: relatedClearanceApplicationShareRecords) {
groupsPresent.put(clearanceApplicationSharePresent.UserOrGroupId + clearanceApplicationSharePresent.AccessLevel, clearanceApplicationSharePresent);
}
Set<Id> clearanceApplicationShareSuitable = new Set<Id>();
if (clearanceApplicationToReview.LeavingOrganisation__c != null && hierarchySecurityMap.containsKey('ClearanceApplication')) {
if (clearanceApplicationToReview.LeavingOrganisation__r.OrgSec_ParentUserGroupId__c != null &&
clearanceApplicationToReview.LeavingOrganisation__r.OrgSec_ChildrenUserGroupId__c != null &&
clearanceApplicationToReview.LeavingOrganisation__r.OrgSec_UserGroupId__c != null) {
String accessLevelParentUserGroup = hierarchySecurityMap.get('ClearanceApplication').ParentAccess__c;
if (groupsPresent.containsKey(clearanceApplicationToReview.LeavingOrganisation__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup)) {
clearanceApplicationShareSuitable.add(groupsPresent.get(clearanceApplicationToReview.LeavingOrganisation__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup).Id);
} else if (accessLevelParentUserGroup != 'None') {
clearanceApplicationShareToInsert.add(new ClearanceApplication__Share(ParentId = clearanceApplicationId, UserOrGroupId = clearanceApplicationToReview.LeavingOrganisation__r.OrgSec_ParentUserGroupId__c, RowCause = Schema.ClearanceApplication__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelParentUserGroup));
}
String accessLevelChildUserGroup = hierarchySecurityMap.get('ClearanceApplication').ChildAccess__c;
if (groupsPresent.containsKey(clearanceApplicationToReview.LeavingOrganisation__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup)) {
clearanceApplicationShareSuitable.add(groupsPresent.get(clearanceApplicationToReview.LeavingOrganisation__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup).Id);
} else if (accessLevelChildUserGroup != 'None') {
clearanceApplicationShareToInsert.add(new ClearanceApplication__Share(ParentId = clearanceApplicationId, UserOrGroupId = clearanceApplicationToReview.LeavingOrganisation__r.OrgSec_ChildrenUserGroupId__c, RowCause = Schema.ClearanceApplication__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelChildUserGroup));
}
String accessLevelPrimaryUserGroup = hierarchySecurityMap.get('ClearanceApplication').PrimaryAccess__c;
if (groupsPresent.containsKey(clearanceApplicationToReview.LeavingOrganisation__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup)) {
clearanceApplicationShareSuitable.add(groupsPresent.get(clearanceApplicationToReview.LeavingOrganisation__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup).Id);
} else if (accessLevelPrimaryUserGroup != 'None') {
clearanceApplicationShareToInsert.add(new ClearanceApplication__Share(ParentId = clearanceApplicationId, UserOrGroupId = clearanceApplicationToReview.LeavingOrganisation__r.OrgSec_UserGroupId__c, RowCause = Schema.ClearanceApplication__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelPrimaryUserGroup));
}
}
}
for (ClearanceApplication__Share clearanceApplicationSharePresent: relatedClearanceApplicationShareRecords) {
if (!clearanceApplicationShareSuitable.contains(clearanceApplicationSharePresent.Id)) {
clearanceApplicationShareToDelete.add(clearanceApplicationSharePresent);
}
}
}
delete clearanceApplicationShareToDelete;
insert clearanceApplicationShareToInsert;
}
public static void manageSharingClearanceSetting(List<sObject> objectList) {
System.debug('xxx manageSharingClearanceSetting');
Map<String, HierarchySecurity__c> hierarchySecurityMap = HierarchySecurity__c.getAll();
Set<Id> clearanceSettingIdsTargetedToReview = new Set<Id>();
List<ClearanceSetting__Share> clearanceSettingShareToInsert = new List<ClearanceSetting__Share>();
List<ClearanceSetting__Share> clearanceSettingShareToDelete = new List<ClearanceSetting__Share>();
for (sObject clearanceSettingObject : objectList) {
ClearanceSetting__c clearanceSettingToReview = (ClearanceSetting__c) clearanceSettingObject;
clearanceSettingIdsTargetedToReview.add(clearanceSettingToReview.Id);
}
//query clearanceSetting with organisation details
Map<Id, ClearanceSetting__c> clearanceSettingsToReview = new Map<Id, ClearanceSetting__c>([
SELECT
Id,
Organisation__c,
Organisation__r.OrgSec_ChildrenUserGroupId__c,
Organisation__r.OrgSec_ChildrenIncludedUserGroupId__c,
Organisation__r.OrgSec_UserGroupId__c,
Organisation__r.OrgSec_ParentUserGroupId__c,
Organisation__r.OrgSec_DirectChildrenUserGroupId__c
FROM ClearanceSetting__c
WHERE Id IN :clearanceSettingIdsTargetedToReview
]);
Map<Id, List<ClearanceSetting__Share>> clearanceSettingIdToClearanceSettingShare = new Map<Id, List<ClearanceSetting__Share>>();
List<ClearanceSetting__Share> clearanceSettingSharesToReview = [
SELECT Id,
UserOrGroupId,
ParentId,
AccessLevel
FROM
ClearanceSetting__Share
WHERE RowCause = :Schema.ClearanceSetting__Share.rowCause.DefaultExternalSharingConfiguration__c
AND ParentId IN :clearanceSettingIdsTargetedToReview
];
for (ClearanceSetting__Share clearanceSettingShare : clearanceSettingSharesToReview) {
if (!clearanceSettingIdToClearanceSettingShare.containsKey(clearanceSettingShare.ParentId)) {
clearanceSettingIdToClearanceSettingShare.put(clearanceSettingShare.ParentId, new List<ClearanceSetting__Share>{
clearanceSettingShare
});
} else {
clearanceSettingIdToClearanceSettingShare.get(clearanceSettingShare.ParentId).add(clearanceSettingShare);
}
}
for (Id clearanceSettingId : clearanceSettingsToReview.keySet()) {
ClearanceSetting__c clearanceSettingToReview = clearanceSettingsToReview.get(clearanceSettingId);
List<ClearanceSetting__Share> relatedClearanceSettingShareRecords = new List<ClearanceSetting__Share>();
if (clearanceSettingIdToClearanceSettingShare.containsKey(clearanceSettingId))
relatedClearanceSettingShareRecords = clearanceSettingIdToClearanceSettingShare.get(clearanceSettingId);
Map<String, ClearanceSetting__Share> groupsPresent = new Map<String, ClearanceSetting__Share>();
for (ClearanceSetting__Share clearanceSettingSharePresent: relatedClearanceSettingShareRecords) {
groupsPresent.put(clearanceSettingSharePresent.UserOrGroupId + clearanceSettingSharePresent.AccessLevel, clearanceSettingSharePresent);
}
Set<Id> clearanceSettingShareSuitable = new Set<Id>();
if (clearanceSettingToReview.Organisation__c != null && hierarchySecurityMap.containsKey('ClearanceSetting')) {
if (clearanceSettingToReview.Organisation__r.OrgSec_ParentUserGroupId__c != null &&
clearanceSettingToReview.Organisation__r.OrgSec_ChildrenUserGroupId__c != null &&
clearanceSettingToReview.Organisation__r.OrgSec_UserGroupId__c != null) {
String accessLevelParentUserGroup = hierarchySecurityMap.get('ClearanceSetting').ParentAccess__c;
if (groupsPresent.containsKey(clearanceSettingToReview.Organisation__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup)) {
clearanceSettingShareSuitable.add(groupsPresent.get(clearanceSettingToReview.Organisation__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup).Id);
} else if (accessLevelParentUserGroup != 'None') {
clearanceSettingShareToInsert.add(new ClearanceSetting__Share(ParentId = clearanceSettingId, UserOrGroupId = clearanceSettingToReview.Organisation__r.OrgSec_ParentUserGroupId__c, RowCause = Schema.ClearanceSetting__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelParentUserGroup));
}
String accessLevelChildUserGroup = hierarchySecurityMap.get('ClearanceSetting').ChildAccess__c;
if (groupsPresent.containsKey(clearanceSettingToReview.Organisation__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup)) {
clearanceSettingShareSuitable.add(groupsPresent.get(clearanceSettingToReview.Organisation__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup).Id);
} else if (accessLevelChildUserGroup != 'None') {
clearanceSettingShareToInsert.add(new ClearanceSetting__Share(ParentId = clearanceSettingId, UserOrGroupId = clearanceSettingToReview.Organisation__r.OrgSec_ChildrenUserGroupId__c, RowCause = Schema.ClearanceSetting__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelChildUserGroup));
}
String accessLevelPrimaryUserGroup = hierarchySecurityMap.get('ClearanceSetting').PrimaryAccess__c;
if (groupsPresent.containsKey(clearanceSettingToReview.Organisation__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup)) {
clearanceSettingShareSuitable.add(groupsPresent.get(clearanceSettingToReview.Organisation__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup).Id);
} else if (accessLevelPrimaryUserGroup != 'None') {
clearanceSettingShareToInsert.add(new ClearanceSetting__Share(ParentId = clearanceSettingId, UserOrGroupId = clearanceSettingToReview.Organisation__r.OrgSec_UserGroupId__c, RowCause = Schema.ClearanceSetting__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelPrimaryUserGroup));
}
}
}
for (ClearanceSetting__Share clearanceSettingSharePresent: relatedClearanceSettingShareRecords) {
if (!clearanceSettingShareSuitable.contains(clearanceSettingSharePresent.Id)) {
clearanceSettingShareToDelete.add(clearanceSettingSharePresent);
}
}
}
delete clearanceSettingShareToDelete;
insert clearanceSettingShareToInsert;
}
public static void manageSharingEmailLetterhead(List<sObject> objectList) {
System.debug('xxx manageSharingEmailLetterhead');
Map<String, HierarchySecurity__c> hierarchySecurityMap = HierarchySecurity__c.getAll();
Set<Id> emailLetterheadIdsTargetedToReview = new Set<Id>();
List<EmailLetterhead__Share> emailLetterheadShareToInsert = new List<EmailLetterhead__Share>();
List<EmailLetterhead__Share> emailLetterheadShareToDelete = new List<EmailLetterhead__Share>();
for (sObject emailLetterheadObject : objectList) {
EmailLetterhead__c emailLetterheadToReview = (EmailLetterhead__c) emailLetterheadObject;
emailLetterheadIdsTargetedToReview.add(emailLetterheadToReview.Id);
}
//query emailLetterhead with organisation details
Map<Id, EmailLetterhead__c> emailLetterheadsToReview = new Map<Id, EmailLetterhead__c>([
SELECT
Id,
DefinedBy__c,
DefinedBy__r.OrgSec_ChildrenUserGroupId__c,
DefinedBy__r.OrgSec_ChildrenIncludedUserGroupId__c,
DefinedBy__r.OrgSec_UserGroupId__c,
DefinedBy__r.OrgSec_ParentUserGroupId__c,
DefinedBy__r.OrgSec_DirectChildrenUserGroupId__c
FROM EmailLetterhead__c
WHERE Id IN :emailLetterheadIdsTargetedToReview
]);
Map<Id, List<EmailLetterhead__Share>> emailLetterheadIdToEmailLetterheadShare = new Map<Id, List<EmailLetterhead__Share>>();
List<EmailLetterhead__Share> emailLetterheadSharesToReview = [
SELECT Id,
UserOrGroupId,
ParentId,
AccessLevel
FROM
EmailLetterhead__Share
WHERE RowCause = :Schema.EmailLetterhead__Share.rowCause.DefaultExternalSharingConfiguration__c
AND ParentId IN :emailLetterheadIdsTargetedToReview
];
for (EmailLetterhead__Share emailLetterheadShare : emailLetterheadSharesToReview) {
if (!emailLetterheadIdToEmailLetterheadShare.containsKey(emailLetterheadShare.ParentId)) {
emailLetterheadIdToEmailLetterheadShare.put(emailLetterheadShare.ParentId, new List<EmailLetterhead__Share>{
emailLetterheadShare
});
} else {
emailLetterheadIdToEmailLetterheadShare.get(emailLetterheadShare.ParentId).add(emailLetterheadShare);
}
}
for (Id emailLetterheadId : emailLetterheadsToReview.keySet()) {
EmailLetterhead__c emailLetterheadToReview = emailLetterheadsToReview.get(emailLetterheadId);
List<EmailLetterhead__Share> relatedEmailLetterheadShareRecords = new List<EmailLetterhead__Share>();
if (emailLetterheadIdToEmailLetterheadShare.containsKey(emailLetterheadId))
relatedEmailLetterheadShareRecords = emailLetterheadIdToEmailLetterheadShare.get(emailLetterheadId);
Map<String, EmailLetterhead__Share> groupsPresent = new Map<String, EmailLetterhead__Share>();
for (EmailLetterhead__Share emailLetterheadSharePresent: relatedEmailLetterheadShareRecords) {
groupsPresent.put(emailLetterheadSharePresent.UserOrGroupId + emailLetterheadSharePresent.AccessLevel, emailLetterheadSharePresent);
}
Set<Id> emailLetterheadShareSuitable = new Set<Id>();
if (emailLetterheadToReview.DefinedBy__c != null && hierarchySecurityMap.containsKey('EmailLetterhead')) {
if (emailLetterheadToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c != null &&
emailLetterheadToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c != null &&
emailLetterheadToReview.DefinedBy__r.OrgSec_UserGroupId__c != null) {
String accessLevelParentUserGroup = hierarchySecurityMap.get('EmailLetterhead').ParentAccess__c;
if (groupsPresent.containsKey(emailLetterheadToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup)) {
emailLetterheadShareSuitable.add(groupsPresent.get(emailLetterheadToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup).Id);
} else if (accessLevelParentUserGroup != 'None') {
emailLetterheadShareToInsert.add(new EmailLetterhead__Share(ParentId = emailLetterheadId, UserOrGroupId = emailLetterheadToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c, RowCause = Schema.EmailLetterhead__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelParentUserGroup));
}
String accessLevelChildUserGroup = hierarchySecurityMap.get('EmailLetterhead').ChildAccess__c;
if (groupsPresent.containsKey(emailLetterheadToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup)) {
emailLetterheadShareSuitable.add(groupsPresent.get(emailLetterheadToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup).Id);
} else if (accessLevelChildUserGroup != 'None') {
emailLetterheadShareToInsert.add(new EmailLetterhead__Share(ParentId = emailLetterheadId, UserOrGroupId = emailLetterheadToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c, RowCause = Schema.EmailLetterhead__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelChildUserGroup));
}
String accessLevelPrimaryUserGroup = hierarchySecurityMap.get('EmailLetterhead').PrimaryAccess__c;
if (groupsPresent.containsKey(emailLetterheadToReview.DefinedBy__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup)) {
emailLetterheadShareSuitable.add(groupsPresent.get(emailLetterheadToReview.DefinedBy__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup).Id);
} else if (accessLevelPrimaryUserGroup != 'None') {
emailLetterheadShareToInsert.add(new EmailLetterhead__Share(ParentId = emailLetterheadId, UserOrGroupId = emailLetterheadToReview.DefinedBy__r.OrgSec_UserGroupId__c, RowCause = Schema.EmailLetterhead__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelPrimaryUserGroup));
}
}
}
for (EmailLetterhead__Share emailLetterheadSharePresent: relatedEmailLetterheadShareRecords) {
if (!emailLetterheadShareSuitable.contains(emailLetterheadSharePresent.Id)) {
emailLetterheadShareToDelete.add(emailLetterheadSharePresent);
}
}
}
delete emailLetterheadShareToDelete;
insert emailLetterheadShareToInsert;
}
public static void manageSharingEmailTemplate(List<sObject> objectList) {
System.debug('xxx manageSharingEmailTemplate');
Map<String, HierarchySecurity__c> hierarchySecurityMap = HierarchySecurity__c.getAll();
Set<Id> emailTemplateIdsTargetedToReview = new Set<Id>();
List<EmailTemplate__Share> emailTemplateShareToInsert = new List<EmailTemplate__Share>();
List<EmailTemplate__Share> emailTemplateShareToDelete = new List<EmailTemplate__Share>();
for (sObject emailTemplateObject : objectList) {
EmailTemplate__c emailTemplateToReview = (EmailTemplate__c) emailTemplateObject;
emailTemplateIdsTargetedToReview.add(emailTemplateToReview.Id);
}
//query emailTemplate with organisation details
Map<Id, EmailTemplate__c> emailTemplatesToReview = new Map<Id, EmailTemplate__c>([
SELECT
Id,
DefinedBy__c,
DefinedBy__r.OrgSec_ChildrenUserGroupId__c,
DefinedBy__r.OrgSec_ChildrenIncludedUserGroupId__c,
DefinedBy__r.OrgSec_UserGroupId__c,
DefinedBy__r.OrgSec_ParentUserGroupId__c,
DefinedBy__r.OrgSec_DirectChildrenUserGroupId__c
FROM EmailTemplate__c
WHERE Id IN :emailTemplateIdsTargetedToReview
]);
Map<Id, List<EmailTemplate__Share>> emailTemplateIdToEmailTemplateShare = new Map<Id, List<EmailTemplate__Share>>();
List<EmailTemplate__Share> emailTemplateSharesToReview = [
SELECT Id,
UserOrGroupId,
ParentId,
AccessLevel
FROM
EmailTemplate__Share
WHERE RowCause = :Schema.EmailTemplate__Share.rowCause.DefaultExternalSharingConfiguration__c
AND ParentId IN :emailTemplateIdsTargetedToReview
];
for (EmailTemplate__Share emailTemplateShare : emailTemplateSharesToReview) {
if (!emailTemplateIdToEmailTemplateShare.containsKey(emailTemplateShare.ParentId)) {
emailTemplateIdToEmailTemplateShare.put(emailTemplateShare.ParentId, new List<EmailTemplate__Share>{
emailTemplateShare
});
} else {
emailTemplateIdToEmailTemplateShare.get(emailTemplateShare.ParentId).add(emailTemplateShare);
}
}
for (Id emailTemplateId : emailTemplatesToReview.keySet()) {
EmailTemplate__c emailTemplateToReview = emailTemplatesToReview.get(emailTemplateId);
List<EmailTemplate__Share> relatedEmailTemplateShareRecords = new List<EmailTemplate__Share>();
if (emailTemplateIdToEmailTemplateShare.containsKey(emailTemplateId))
relatedEmailTemplateShareRecords = emailTemplateIdToEmailTemplateShare.get(emailTemplateId);
Map<String, EmailTemplate__Share> groupsPresent = new Map<String, EmailTemplate__Share>();
for (EmailTemplate__Share emailTemplateSharePresent: relatedEmailTemplateShareRecords) {
groupsPresent.put(emailTemplateSharePresent.UserOrGroupId + emailTemplateSharePresent.AccessLevel, emailTemplateSharePresent);
}
Set<Id> emailTemplateShareSuitable = new Set<Id>();
if (emailTemplateToReview.DefinedBy__c != null && hierarchySecurityMap.containsKey('EmailTemplate')) {
if (emailTemplateToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c != null &&
emailTemplateToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c != null &&
emailTemplateToReview.DefinedBy__r.OrgSec_UserGroupId__c != null) {
String accessLevelParentUserGroup = hierarchySecurityMap.get('EmailTemplate').ParentAccess__c;
if (groupsPresent.containsKey(emailTemplateToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup)) {
emailTemplateShareSuitable.add(groupsPresent.get(emailTemplateToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup).Id);
} else if (accessLevelParentUserGroup != 'None') {
emailTemplateShareToInsert.add(new EmailTemplate__Share(ParentId = emailTemplateId, UserOrGroupId = emailTemplateToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c, RowCause = Schema.EmailTemplate__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelParentUserGroup));
}
String accessLevelChildUserGroup = hierarchySecurityMap.get('EmailTemplate').ChildAccess__c;
if (groupsPresent.containsKey(emailTemplateToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup)) {
emailTemplateShareSuitable.add(groupsPresent.get(emailTemplateToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup).Id);
} else if (accessLevelChildUserGroup != 'None') {
emailTemplateShareToInsert.add(new EmailTemplate__Share(ParentId = emailTemplateId, UserOrGroupId = emailTemplateToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c, RowCause = Schema.EmailTemplate__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelChildUserGroup));
}
String accessLevelPrimaryUserGroup = hierarchySecurityMap.get('EmailTemplate').PrimaryAccess__c;
if (groupsPresent.containsKey(emailTemplateToReview.DefinedBy__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup)) {
emailTemplateShareSuitable.add(groupsPresent.get(emailTemplateToReview.DefinedBy__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup).Id);
} else if (accessLevelPrimaryUserGroup != 'None') {
emailTemplateShareToInsert.add(new EmailTemplate__Share(ParentId = emailTemplateId, UserOrGroupId = emailTemplateToReview.DefinedBy__r.OrgSec_UserGroupId__c, RowCause = Schema.EmailTemplate__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelPrimaryUserGroup));
}
}
}
for (EmailTemplate__Share emailTemplateSharePresent: relatedEmailTemplateShareRecords) {
if (!emailTemplateShareSuitable.contains(emailTemplateSharePresent.Id)) {
emailTemplateShareToDelete.add(emailTemplateSharePresent);
}
}
}
delete emailTemplateShareToDelete;
insert emailTemplateShareToInsert;
}
public static void manageSharingMediaReleaseSchedule(List<sObject> objectList) {
System.debug('xxx manageSharingMediaReleaseSchedule');
Map<String, HierarchySecurity__c> hierarchySecurityMap = HierarchySecurity__c.getAll();
Set<Id> mediaReleaseScheduleIdsTargetedToReview = new Set<Id>();
List<MediaReleaseSchedule__Share> mediaReleaseScheduleShareToInsert = new List<MediaReleaseSchedule__Share>();
List<MediaReleaseSchedule__Share> mediaReleaseScheduleShareToDelete = new List<MediaReleaseSchedule__Share>();
for (sObject mediaReleaseScheduleObject : objectList) {
MediaReleaseSchedule__c mediaReleaseScheduleToReview = (MediaReleaseSchedule__c) mediaReleaseScheduleObject;
mediaReleaseScheduleIdsTargetedToReview.add(mediaReleaseScheduleToReview.Id);
}
//query mediaReleaseSchedule with organisation details
Map<Id, MediaReleaseSchedule__c> mediaReleaseSchedulesToReview = new Map<Id, MediaReleaseSchedule__c>([
SELECT
Id,
DefinedBy__c,
DefinedBy__r.OrgSec_ChildrenUserGroupId__c,
DefinedBy__r.OrgSec_ChildrenIncludedUserGroupId__c,
DefinedBy__r.OrgSec_UserGroupId__c,
DefinedBy__r.OrgSec_ParentUserGroupId__c,
DefinedBy__r.OrgSec_DirectChildrenUserGroupId__c
FROM MediaReleaseSchedule__c
WHERE Id IN :mediaReleaseScheduleIdsTargetedToReview
]);
Map<Id, List<MediaReleaseSchedule__Share>> mediaReleaseScheduleIdToMediaReleaseScheduleShare = new Map<Id, List<MediaReleaseSchedule__Share>>();
List<MediaReleaseSchedule__Share> mediaReleaseScheduleSharesToReview = [
SELECT Id,
UserOrGroupId,
ParentId,
AccessLevel
FROM
MediaReleaseSchedule__Share
WHERE RowCause = :Schema.MediaReleaseSchedule__Share.rowCause.DefaultExternalSharingConfiguration__c
AND ParentId IN :mediaReleaseScheduleIdsTargetedToReview
];
for (MediaReleaseSchedule__Share mediaReleaseScheduleShare : mediaReleaseScheduleSharesToReview) {
if (!mediaReleaseScheduleIdToMediaReleaseScheduleShare.containsKey(mediaReleaseScheduleShare.ParentId)) {
mediaReleaseScheduleIdToMediaReleaseScheduleShare.put(mediaReleaseScheduleShare.ParentId, new List<MediaReleaseSchedule__Share>{
mediaReleaseScheduleShare
});
} else {
mediaReleaseScheduleIdToMediaReleaseScheduleShare.get(mediaReleaseScheduleShare.ParentId).add(mediaReleaseScheduleShare);
}
}
for (Id mediaReleaseScheduleId : mediaReleaseSchedulesToReview.keySet()) {
MediaReleaseSchedule__c mediaReleaseScheduleToReview = mediaReleaseSchedulesToReview.get(mediaReleaseScheduleId);
List<MediaReleaseSchedule__Share> relatedMediaReleaseScheduleShareRecords = new List<MediaReleaseSchedule__Share>();
if (mediaReleaseScheduleIdToMediaReleaseScheduleShare.containsKey(mediaReleaseScheduleId))
relatedMediaReleaseScheduleShareRecords = mediaReleaseScheduleIdToMediaReleaseScheduleShare.get(mediaReleaseScheduleId);
Map<String, MediaReleaseSchedule__Share> groupsPresent = new Map<String, MediaReleaseSchedule__Share>();
for (MediaReleaseSchedule__Share mediaReleaseScheduleSharePresent: relatedMediaReleaseScheduleShareRecords) {
groupsPresent.put(mediaReleaseScheduleSharePresent.UserOrGroupId + mediaReleaseScheduleSharePresent.AccessLevel, mediaReleaseScheduleSharePresent);
}
Set<Id> mediaReleaseScheduleShareSuitable = new Set<Id>();
if (mediaReleaseScheduleToReview.DefinedBy__c != null && hierarchySecurityMap.containsKey('MediaReleaseSchedule')) {
if (mediaReleaseScheduleToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c != null &&
mediaReleaseScheduleToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c != null &&
mediaReleaseScheduleToReview.DefinedBy__r.OrgSec_UserGroupId__c != null) {
String accessLevelParentUserGroup = hierarchySecurityMap.get('MediaReleaseSchedule').ParentAccess__c;
if (groupsPresent.containsKey(mediaReleaseScheduleToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup)) {
mediaReleaseScheduleShareSuitable.add(groupsPresent.get(mediaReleaseScheduleToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup).Id);
} else if (accessLevelParentUserGroup != 'None') {
mediaReleaseScheduleShareToInsert.add(new MediaReleaseSchedule__Share(ParentId = mediaReleaseScheduleId, UserOrGroupId = mediaReleaseScheduleToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c, RowCause = Schema.MediaReleaseSchedule__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelParentUserGroup));
}
String accessLevelChildUserGroup = hierarchySecurityMap.get('MediaReleaseSchedule').ChildAccess__c;
if (groupsPresent.containsKey(mediaReleaseScheduleToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup)) {
mediaReleaseScheduleShareSuitable.add(groupsPresent.get(mediaReleaseScheduleToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup).Id);
} else if (accessLevelChildUserGroup != 'None') {
mediaReleaseScheduleShareToInsert.add(new MediaReleaseSchedule__Share(ParentId = mediaReleaseScheduleId, UserOrGroupId = mediaReleaseScheduleToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c, RowCause = Schema.MediaReleaseSchedule__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelChildUserGroup));
}
String accessLevelPrimaryUserGroup = hierarchySecurityMap.get('MediaReleaseSchedule').PrimaryAccess__c;
if (groupsPresent.containsKey(mediaReleaseScheduleToReview.DefinedBy__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup)) {
mediaReleaseScheduleShareSuitable.add(groupsPresent.get(mediaReleaseScheduleToReview.DefinedBy__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup).Id);
} else if (accessLevelPrimaryUserGroup != 'None') {
mediaReleaseScheduleShareToInsert.add(new MediaReleaseSchedule__Share(ParentId = mediaReleaseScheduleId, UserOrGroupId = mediaReleaseScheduleToReview.DefinedBy__r.OrgSec_UserGroupId__c, RowCause = Schema.MediaReleaseSchedule__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelPrimaryUserGroup));
}
}
}
for (MediaReleaseSchedule__Share mediaReleaseScheduleSharePresent: relatedMediaReleaseScheduleShareRecords) {
if (!mediaReleaseScheduleShareSuitable.contains(mediaReleaseScheduleSharePresent.Id)) {
mediaReleaseScheduleShareToDelete.add(mediaReleaseScheduleSharePresent);
}
}
}
delete mediaReleaseScheduleShareToDelete;
insert mediaReleaseScheduleShareToInsert;
}
public static void manageSharingMisconduct(List<sObject> objectList) {
System.debug('xxx manageSharingMisconduct');
Map<String, HierarchySecurity__c> hierarchySecurityMap = HierarchySecurity__c.getAll();
Set<Id> misconductIdsTargetedToReview = new Set<Id>();
List<Misconduct__Share> misconductShareToInsert = new List<Misconduct__Share>();
List<Misconduct__Share> misconductShareToDelete = new List<Misconduct__Share>();
for (sObject misconductObject : objectList) {
Misconduct__c misconductToReview = (Misconduct__c) misconductObject;
misconductIdsTargetedToReview.add(misconductToReview.Id);
}
//query misconduct with organisation details
Map<Id, Misconduct__c> misconductsToReview = new Map<Id, Misconduct__c>([
SELECT
Id,
InfringingClub__c,
InfringingClub__r.OrgSec_ChildrenUserGroupId__c,
InfringingClub__r.OrgSec_ChildrenIncludedUserGroupId__c,
InfringingClub__r.OrgSec_UserGroupId__c,
InfringingClub__r.OrgSec_ParentUserGroupId__c,
InfringingClub__r.OrgSec_DirectChildrenUserGroupId__c
FROM Misconduct__c
WHERE Id IN :misconductIdsTargetedToReview
]);
Map<Id, List<Misconduct__Share>> misconductIdToMisconductShare = new Map<Id, List<Misconduct__Share>>();
List<Misconduct__Share> misconductSharesToReview = [
SELECT Id,
UserOrGroupId,
ParentId,
AccessLevel
FROM
Misconduct__Share
WHERE RowCause = :Schema.Misconduct__Share.rowCause.DefaultExternalSharingConfiguration__c
AND ParentId IN :misconductIdsTargetedToReview
];
for (Misconduct__Share misconductShare : misconductSharesToReview) {
if (!misconductIdToMisconductShare.containsKey(misconductShare.ParentId)) {
misconductIdToMisconductShare.put(misconductShare.ParentId, new List<Misconduct__Share>{
misconductShare
});
} else {
misconductIdToMisconductShare.get(misconductShare.ParentId).add(misconductShare);
}
}
for (Id misconductId : misconductsToReview.keySet()) {
Misconduct__c misconductToReview = misconductsToReview.get(misconductId);
List<Misconduct__Share> relatedMisconductShareRecords = new List<Misconduct__Share>();
if (misconductIdToMisconductShare.containsKey(misconductId))
relatedMisconductShareRecords = misconductIdToMisconductShare.get(misconductId);
Map<String, Misconduct__Share> groupsPresent = new Map<String, Misconduct__Share>();
for (Misconduct__Share misconductSharePresent: relatedMisconductShareRecords) {
groupsPresent.put(misconductSharePresent.UserOrGroupId + misconductSharePresent.AccessLevel, misconductSharePresent);
}
Set<Id> misconductShareSuitable = new Set<Id>();
if (misconductToReview.InfringingClub__c != null && hierarchySecurityMap.containsKey('Misconduct')) {
if (misconductToReview.InfringingClub__r.OrgSec_ParentUserGroupId__c != null &&
misconductToReview.InfringingClub__r.OrgSec_ChildrenUserGroupId__c != null &&
misconductToReview.InfringingClub__r.OrgSec_UserGroupId__c != null) {
String accessLevelParentUserGroup = hierarchySecurityMap.get('Misconduct').ParentAccess__c;
if (groupsPresent.containsKey(misconductToReview.InfringingClub__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup)) {
misconductShareSuitable.add(groupsPresent.get(misconductToReview.InfringingClub__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup).Id);
} else if (accessLevelParentUserGroup != 'None') {
misconductShareToInsert.add(new Misconduct__Share(ParentId = misconductId, UserOrGroupId = misconductToReview.InfringingClub__r.OrgSec_ParentUserGroupId__c, RowCause = Schema.Misconduct__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelParentUserGroup));
}
String accessLevelChildUserGroup = hierarchySecurityMap.get('Misconduct').ChildAccess__c;
if (groupsPresent.containsKey(misconductToReview.InfringingClub__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup)) {
misconductShareSuitable.add(groupsPresent.get(misconductToReview.InfringingClub__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup).Id);
} else if (accessLevelChildUserGroup != 'None') {
misconductShareToInsert.add(new Misconduct__Share(ParentId = misconductId, UserOrGroupId = misconductToReview.InfringingClub__r.OrgSec_ChildrenUserGroupId__c, RowCause = Schema.Misconduct__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelChildUserGroup));
}
String accessLevelPrimaryUserGroup = hierarchySecurityMap.get('Misconduct').PrimaryAccess__c;
if (groupsPresent.containsKey(misconductToReview.InfringingClub__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup)) {
misconductShareSuitable.add(groupsPresent.get(misconductToReview.InfringingClub__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup).Id);
} else if (accessLevelPrimaryUserGroup != 'None') {
misconductShareToInsert.add(new Misconduct__Share(ParentId = misconductId, UserOrGroupId = misconductToReview.InfringingClub__r.OrgSec_UserGroupId__c, RowCause = Schema.Misconduct__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelPrimaryUserGroup));
}
}
}
for (Misconduct__Share misconductSharePresent: relatedMisconductShareRecords) {
if (!misconductShareSuitable.contains(misconductSharePresent.Id)) {
misconductShareToDelete.add(misconductSharePresent);
}
}
}
delete misconductShareToDelete;
insert misconductShareToInsert;
}
public static void manageSharingSMSTemplate(List<sObject> objectList) {
System.debug('xxx manageSharingSMSTemplate');
Map<String, HierarchySecurity__c> hierarchySecurityMap = HierarchySecurity__c.getAll();
Set<Id> sMSTemplateIdsTargetedToReview = new Set<Id>();
List<SMSTemplate__Share> sMSTemplateShareToInsert = new List<SMSTemplate__Share>();
List<SMSTemplate__Share> sMSTemplateShareToDelete = new List<SMSTemplate__Share>();
for (sObject sMSTemplateObject : objectList) {
SMSTemplate__c sMSTemplateToReview = (SMSTemplate__c) sMSTemplateObject;
sMSTemplateIdsTargetedToReview.add(sMSTemplateToReview.Id);
}
//query sMSTemplate with organisation details
Map<Id, SMSTemplate__c> sMSTemplatesToReview = new Map<Id, SMSTemplate__c>([
SELECT
Id,
DefinedBy__c,
DefinedBy__r.OrgSec_ChildrenUserGroupId__c,
DefinedBy__r.OrgSec_ChildrenIncludedUserGroupId__c,
DefinedBy__r.OrgSec_UserGroupId__c,
DefinedBy__r.OrgSec_ParentUserGroupId__c,
DefinedBy__r.OrgSec_DirectChildrenUserGroupId__c
FROM SMSTemplate__c
WHERE Id IN :sMSTemplateIdsTargetedToReview
]);
Map<Id, List<SMSTemplate__Share>> sMSTemplateIdToSMSTemplateShare = new Map<Id, List<SMSTemplate__Share>>();
List<SMSTemplate__Share> sMSTemplateSharesToReview = [
SELECT Id,
UserOrGroupId,
ParentId,
AccessLevel
FROM
SMSTemplate__Share
WHERE RowCause = :Schema.SMSTemplate__Share.rowCause.DefaultExternalSharingConfiguration__c
AND ParentId IN :sMSTemplateIdsTargetedToReview
];
for (SMSTemplate__Share sMSTemplateShare : sMSTemplateSharesToReview) {
if (!sMSTemplateIdToSMSTemplateShare.containsKey(sMSTemplateShare.ParentId)) {
sMSTemplateIdToSMSTemplateShare.put(sMSTemplateShare.ParentId, new List<SMSTemplate__Share>{
sMSTemplateShare
});
} else {
sMSTemplateIdToSMSTemplateShare.get(sMSTemplateShare.ParentId).add(sMSTemplateShare);
}
}
for (Id sMSTemplateId : sMSTemplatesToReview.keySet()) {
SMSTemplate__c sMSTemplateToReview = sMSTemplatesToReview.get(sMSTemplateId);
List<SMSTemplate__Share> relatedSMSTemplateShareRecords = new List<SMSTemplate__Share>();
if (sMSTemplateIdToSMSTemplateShare.containsKey(sMSTemplateId))
relatedSMSTemplateShareRecords = sMSTemplateIdToSMSTemplateShare.get(sMSTemplateId);
Map<String, SMSTemplate__Share> groupsPresent = new Map<String, SMSTemplate__Share>();
for (SMSTemplate__Share sMSTemplateSharePresent: relatedSMSTemplateShareRecords) {
groupsPresent.put(sMSTemplateSharePresent.UserOrGroupId + sMSTemplateSharePresent.AccessLevel, sMSTemplateSharePresent);
}
Set<Id> sMSTemplateShareSuitable = new Set<Id>();
if (sMSTemplateToReview.DefinedBy__c != null && hierarchySecurityMap.containsKey('SMSTemplate')) {
if (sMSTemplateToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c != null &&
sMSTemplateToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c != null &&
sMSTemplateToReview.DefinedBy__r.OrgSec_UserGroupId__c != null) {
String accessLevelParentUserGroup = hierarchySecurityMap.get('SMSTemplate').ParentAccess__c;
if (groupsPresent.containsKey(sMSTemplateToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup)) {
sMSTemplateShareSuitable.add(groupsPresent.get(sMSTemplateToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup).Id);
} else if (accessLevelParentUserGroup != 'None') {
sMSTemplateShareToInsert.add(new SMSTemplate__Share(ParentId = sMSTemplateId, UserOrGroupId = sMSTemplateToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c, RowCause = Schema.SMSTemplate__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelParentUserGroup));
}
String accessLevelChildUserGroup = hierarchySecurityMap.get('SMSTemplate').ChildAccess__c;
if (groupsPresent.containsKey(sMSTemplateToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup)) {
sMSTemplateShareSuitable.add(groupsPresent.get(sMSTemplateToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup).Id);
} else if (accessLevelChildUserGroup != 'None') {
sMSTemplateShareToInsert.add(new SMSTemplate__Share(ParentId = sMSTemplateId, UserOrGroupId = sMSTemplateToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c, RowCause = Schema.SMSTemplate__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelChildUserGroup));
}
String accessLevelPrimaryUserGroup = hierarchySecurityMap.get('SMSTemplate').PrimaryAccess__c;
if (groupsPresent.containsKey(sMSTemplateToReview.DefinedBy__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup)) {
sMSTemplateShareSuitable.add(groupsPresent.get(sMSTemplateToReview.DefinedBy__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup).Id);
} else if (accessLevelPrimaryUserGroup != 'None') {
sMSTemplateShareToInsert.add(new SMSTemplate__Share(ParentId = sMSTemplateId, UserOrGroupId = sMSTemplateToReview.DefinedBy__r.OrgSec_UserGroupId__c, RowCause = Schema.SMSTemplate__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelPrimaryUserGroup));
}
}
}
for (SMSTemplate__Share sMSTemplateSharePresent: relatedSMSTemplateShareRecords) {
if (!sMSTemplateShareSuitable.contains(sMSTemplateSharePresent.Id)) {
sMSTemplateShareToDelete.add(sMSTemplateSharePresent);
}
}
}
delete sMSTemplateShareToDelete;
insert sMSTemplateShareToInsert;
}
public static void manageSharingSplitRule(List<sObject> objectList) {
System.debug('xxx manageSharingSplitRule');
Map<String, HierarchySecurity__c> hierarchySecurityMap = HierarchySecurity__c.getAll();
Set<Id> splitRuleIdsTargetedToReview = new Set<Id>();
List<SplitRule__Share> splitRuleShareToInsert = new List<SplitRule__Share>();
List<SplitRule__Share> splitRuleShareToDelete = new List<SplitRule__Share>();
for (sObject splitRuleObject : objectList) {
SplitRule__c splitRuleToReview = (SplitRule__c) splitRuleObject;
splitRuleIdsTargetedToReview.add(splitRuleToReview.Id);
}
//query splitRule with organisation details
Map<Id, SplitRule__c> splitRulesToReview = new Map<Id, SplitRule__c>([
SELECT
Id,
DefinedBy__c,
DefinedBy__r.OrgSec_ChildrenUserGroupId__c,
DefinedBy__r.OrgSec_ChildrenIncludedUserGroupId__c,
DefinedBy__r.OrgSec_UserGroupId__c,
DefinedBy__r.OrgSec_ParentUserGroupId__c,
DefinedBy__r.OrgSec_DirectChildrenUserGroupId__c
FROM SplitRule__c
WHERE Id IN :splitRuleIdsTargetedToReview
]);
Map<Id, List<SplitRule__Share>> splitRuleIdToSplitRuleShare = new Map<Id, List<SplitRule__Share>>();
List<SplitRule__Share> splitRuleSharesToReview = [
SELECT Id,
UserOrGroupId,
ParentId,
AccessLevel
FROM
SplitRule__Share
WHERE RowCause = :Schema.SplitRule__Share.rowCause.DefaultExternalSharingConfiguration__c
AND ParentId IN :splitRuleIdsTargetedToReview
];
for (SplitRule__Share splitRuleShare : splitRuleSharesToReview) {
if (!splitRuleIdToSplitRuleShare.containsKey(splitRuleShare.ParentId)) {
splitRuleIdToSplitRuleShare.put(splitRuleShare.ParentId, new List<SplitRule__Share>{
splitRuleShare
});
} else {
splitRuleIdToSplitRuleShare.get(splitRuleShare.ParentId).add(splitRuleShare);
}
}
for (Id splitRuleId : splitRulesToReview.keySet()) {
SplitRule__c splitRuleToReview = splitRulesToReview.get(splitRuleId);
List<SplitRule__Share> relatedSplitRuleShareRecords = new List<SplitRule__Share>();
if (splitRuleIdToSplitRuleShare.containsKey(splitRuleId))
relatedSplitRuleShareRecords = splitRuleIdToSplitRuleShare.get(splitRuleId);
Map<String, SplitRule__Share> groupsPresent = new Map<String, SplitRule__Share>();
for (SplitRule__Share splitRuleSharePresent: relatedSplitRuleShareRecords) {
groupsPresent.put(splitRuleSharePresent.UserOrGroupId + splitRuleSharePresent.AccessLevel, splitRuleSharePresent);
}
Set<Id> splitRuleShareSuitable = new Set<Id>();
if (splitRuleToReview.DefinedBy__c != null && hierarchySecurityMap.containsKey('SplitRule')) {
if (splitRuleToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c != null &&
splitRuleToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c != null &&
splitRuleToReview.DefinedBy__r.OrgSec_UserGroupId__c != null) {
String accessLevelParentUserGroup = hierarchySecurityMap.get('SplitRule').ParentAccess__c;
if (groupsPresent.containsKey(splitRuleToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup)) {
splitRuleShareSuitable.add(groupsPresent.get(splitRuleToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup).Id);
} else if (accessLevelParentUserGroup != 'None') {
splitRuleShareToInsert.add(new SplitRule__Share(ParentId = splitRuleId, UserOrGroupId = splitRuleToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c, RowCause = Schema.SplitRule__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelParentUserGroup));
}
String accessLevelChildUserGroup = hierarchySecurityMap.get('SplitRule').ChildAccess__c;
if (groupsPresent.containsKey(splitRuleToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup)) {
splitRuleShareSuitable.add(groupsPresent.get(splitRuleToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup).Id);
} else if (accessLevelChildUserGroup != 'None') {
splitRuleShareToInsert.add(new SplitRule__Share(ParentId = splitRuleId, UserOrGroupId = splitRuleToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c, RowCause = Schema.SplitRule__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelChildUserGroup));
}
String accessLevelPrimaryUserGroup = hierarchySecurityMap.get('SplitRule').PrimaryAccess__c;
if (groupsPresent.containsKey(splitRuleToReview.DefinedBy__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup)) {
splitRuleShareSuitable.add(groupsPresent.get(splitRuleToReview.DefinedBy__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup).Id);
} else if (accessLevelPrimaryUserGroup != 'None') {
splitRuleShareToInsert.add(new SplitRule__Share(ParentId = splitRuleId, UserOrGroupId = splitRuleToReview.DefinedBy__r.OrgSec_UserGroupId__c, RowCause = Schema.SplitRule__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelPrimaryUserGroup));
}
}
}
for (SplitRule__Share splitRuleSharePresent: relatedSplitRuleShareRecords) {
if (!splitRuleShareSuitable.contains(splitRuleSharePresent.Id)) {
splitRuleShareToDelete.add(splitRuleSharePresent);
}
}
}
delete splitRuleShareToDelete;
insert splitRuleShareToInsert;
}
public static void manageSharingStatisticsSettingTemplate(List<sObject> objectList) {
System.debug('xxx manageSharingStatisticsSettingTemplate');
Map<String, HierarchySecurity__c> hierarchySecurityMap = HierarchySecurity__c.getAll();
Set<Id> statisticsSettingTemplateIdsTargetedToReview = new Set<Id>();
List<StatisticsSettingTemplate__Share> statisticsSettingTemplateShareToInsert = new List<StatisticsSettingTemplate__Share>();
List<StatisticsSettingTemplate__Share> statisticsSettingTemplateShareToDelete = new List<StatisticsSettingTemplate__Share>();
for (sObject statisticsSettingTemplateObject : objectList) {
StatisticsSettingTemplate__c statisticsSettingTemplateToReview = (StatisticsSettingTemplate__c) statisticsSettingTemplateObject;
statisticsSettingTemplateIdsTargetedToReview.add(statisticsSettingTemplateToReview.Id);
}
//query statisticsSettingTemplate with organisation details
Map<Id, StatisticsSettingTemplate__c> statisticsSettingTemplatesToReview = new Map<Id, StatisticsSettingTemplate__c>([
SELECT
Id,
DefinedBy__c,
DefinedBy__r.OrgSec_ChildrenUserGroupId__c,
DefinedBy__r.OrgSec_ChildrenIncludedUserGroupId__c,
DefinedBy__r.OrgSec_UserGroupId__c,
DefinedBy__r.OrgSec_ParentUserGroupId__c,
DefinedBy__r.OrgSec_DirectChildrenUserGroupId__c
FROM StatisticsSettingTemplate__c
WHERE Id IN :statisticsSettingTemplateIdsTargetedToReview
]);
Map<Id, List<StatisticsSettingTemplate__Share>> statisticsSettingTemplateIdToStatisticsSettingTemplateShare = new Map<Id, List<StatisticsSettingTemplate__Share>>();
List<StatisticsSettingTemplate__Share> statisticsSettingTemplateSharesToReview = [
SELECT Id,
UserOrGroupId,
ParentId,
AccessLevel
FROM
StatisticsSettingTemplate__Share
WHERE RowCause = :Schema.StatisticsSettingTemplate__Share.rowCause.DefaultExternalSharingConfiguration__c
AND ParentId IN :statisticsSettingTemplateIdsTargetedToReview
];
for (StatisticsSettingTemplate__Share statisticsSettingTemplateShare : statisticsSettingTemplateSharesToReview) {
if (!statisticsSettingTemplateIdToStatisticsSettingTemplateShare.containsKey(statisticsSettingTemplateShare.ParentId)) {
statisticsSettingTemplateIdToStatisticsSettingTemplateShare.put(statisticsSettingTemplateShare.ParentId, new List<StatisticsSettingTemplate__Share>{
statisticsSettingTemplateShare
});
} else {
statisticsSettingTemplateIdToStatisticsSettingTemplateShare.get(statisticsSettingTemplateShare.ParentId).add(statisticsSettingTemplateShare);
}
}
for (Id statisticsSettingTemplateId : statisticsSettingTemplatesToReview.keySet()) {
StatisticsSettingTemplate__c statisticsSettingTemplateToReview = statisticsSettingTemplatesToReview.get(statisticsSettingTemplateId);
List<StatisticsSettingTemplate__Share> relatedStatisticsSettingTemplateShareRecords = new List<StatisticsSettingTemplate__Share>();
if (statisticsSettingTemplateIdToStatisticsSettingTemplateShare.containsKey(statisticsSettingTemplateId))
relatedStatisticsSettingTemplateShareRecords = statisticsSettingTemplateIdToStatisticsSettingTemplateShare.get(statisticsSettingTemplateId);
Map<String, StatisticsSettingTemplate__Share> groupsPresent = new Map<String, StatisticsSettingTemplate__Share>();
for (StatisticsSettingTemplate__Share statisticsSettingTemplateSharePresent: relatedStatisticsSettingTemplateShareRecords) {
groupsPresent.put(statisticsSettingTemplateSharePresent.UserOrGroupId + statisticsSettingTemplateSharePresent.AccessLevel, statisticsSettingTemplateSharePresent);
}
Set<Id> statisticsSettingTemplateShareSuitable = new Set<Id>();
if (statisticsSettingTemplateToReview.DefinedBy__c != null && hierarchySecurityMap.containsKey('StatisticsSettingTemplate')) {
if (statisticsSettingTemplateToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c != null &&
statisticsSettingTemplateToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c != null &&
statisticsSettingTemplateToReview.DefinedBy__r.OrgSec_UserGroupId__c != null) {
String accessLevelParentUserGroup = hierarchySecurityMap.get('StatisticsSettingTemplate').ParentAccess__c;
if (groupsPresent.containsKey(statisticsSettingTemplateToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup)) {
statisticsSettingTemplateShareSuitable.add(groupsPresent.get(statisticsSettingTemplateToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c + accessLevelParentUserGroup).Id);
} else if (accessLevelParentUserGroup != 'None') {
statisticsSettingTemplateShareToInsert.add(new StatisticsSettingTemplate__Share(ParentId = statisticsSettingTemplateId, UserOrGroupId = statisticsSettingTemplateToReview.DefinedBy__r.OrgSec_ParentUserGroupId__c, RowCause = Schema.StatisticsSettingTemplate__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelParentUserGroup));
}
String accessLevelChildUserGroup = hierarchySecurityMap.get('StatisticsSettingTemplate').ChildAccess__c;
if (groupsPresent.containsKey(statisticsSettingTemplateToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup)) {
statisticsSettingTemplateShareSuitable.add(groupsPresent.get(statisticsSettingTemplateToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c + accessLevelChildUserGroup).Id);
} else if (accessLevelChildUserGroup != 'None') {
statisticsSettingTemplateShareToInsert.add(new StatisticsSettingTemplate__Share(ParentId = statisticsSettingTemplateId, UserOrGroupId = statisticsSettingTemplateToReview.DefinedBy__r.OrgSec_ChildrenUserGroupId__c, RowCause = Schema.StatisticsSettingTemplate__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelChildUserGroup));
}
String accessLevelPrimaryUserGroup = hierarchySecurityMap.get('StatisticsSettingTemplate').PrimaryAccess__c;
if (groupsPresent.containsKey(statisticsSettingTemplateToReview.DefinedBy__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup)) {
statisticsSettingTemplateShareSuitable.add(groupsPresent.get(statisticsSettingTemplateToReview.DefinedBy__r.OrgSec_UserGroupId__c + accessLevelPrimaryUserGroup).Id);
} else if (accessLevelPrimaryUserGroup != 'None') {
statisticsSettingTemplateShareToInsert.add(new StatisticsSettingTemplate__Share(ParentId = statisticsSettingTemplateId, UserOrGroupId = statisticsSettingTemplateToReview.DefinedBy__r.OrgSec_UserGroupId__c, RowCause = Schema.StatisticsSettingTemplate__Share.rowCause.DefaultExternalSharingConfiguration__c, AccessLevel = accessLevelPrimaryUserGroup));
}
}
}
for (StatisticsSettingTemplate__Share statisticsSettingTemplateSharePresent: relatedStatisticsSettingTemplateShareRecords) {
if (!statisticsSettingTemplateShareSuitable.contains(statisticsSettingTemplateSharePresent.Id)) {
statisticsSettingTemplateShareToDelete.add(statisticsSettingTemplateSharePresent);
}
}
}
delete statisticsSettingTemplateShareToDelete;
insert statisticsSettingTemplateShareToInsert;
}
//////////
//Creation and cleanup of Public Groups for Accounts
//////////
//Assess a set of Account Id and kick off queuable for the creation of the public groups if necessary
public static List<PublicGroupAccountWrapper> createUserGroupSet(Set<Id> accountIdsTargeted) {
if (!SecurityUtil.GroupCalculationActive) {
SecurityUtil.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,
OrgSec_UserGroupId__c,
OrgSec_ChildrenUserGroupId__c,
OrgSec_ChildrenIncludedUserGroupId__c,
OrgSec_ParentUserGroupId__c,
OrgSec_DirectChildrenUserGroupId__c,
RecordTypeId
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.OrgSec_ChildrenUserGroupId__c == null)
accountsToPopulate.add(new PublicGroupAccountWrapper(accountToConsider, null, 'ChildrenUserGroup'));
if (accountToConsider.OrgSec_ChildrenIncludedUserGroupId__c == null)
accountsToPopulate.add(new PublicGroupAccountWrapper(accountToConsider, null, 'ChildrenIncludedUserGroup'));
if (accountToConsider.OrgSec_UserGroupId__c == null)
accountsToPopulate.add(new PublicGroupAccountWrapper(accountToConsider, null, 'PrimaryUserGroup'));
if (accountToConsider.OrgSec_ParentUserGroupId__c == null)
accountsToPopulate.add(new PublicGroupAccountWrapper(accountToConsider, null, 'ParentUserGroup'));
if (accountToConsider.OrgSec_DirectChildrenUserGroupId__c == null)
accountsToPopulate.add(new PublicGroupAccountWrapper(accountToConsider, null, 'DirectChildrenUserGroup'));
}
}
// If Accounts required public groups to be created, enque the queueable for the creation process
if (!Test.isRunningTest()) {
if (!accountsToPopulate.isEmpty()) {
CreateUserGroups publicGroupCreation = new CreateUserGroups(accountsToPopulate);
ID jobID = System.enqueueJob(publicGroupCreation);
} else {
SecurityUtil.InitiateProcessAccountHierarchyMembership processHierarchy = new SecurityUtil.InitiateProcessAccountHierarchyMembership(accountIdsTargeted);
ID jobID = System.enqueueJob(processHierarchy);
}
}
return accountsToPopulate;
}
return null;
}
//method to kick off batch, which will scan all Groups and remove those not referenced in Accounts.
public static void runCleanupPublicGroups() {
System.debug('xxx runCleanupPublicGroups');
CleanupPublicGroupsBatch cleanupBatchJob = new CleanupPublicGroupsBatch();
Id batchId = Database.executeBatch(cleanupBatchJob);
}
public static void deleteAllShares() {
List<AccountShare> accSharesToDelete = [SELECT ID FROM AccountShare WHERE RowCause = 'Manual'];
delete accSharesToDelete;
List<Member__Share> memberSharesToDelete = [
SELECT Id
FROM
Member__Share
WHERE RowCause = :Schema.Member__Share.rowCause.DefaultExternalSharingConfiguration__c
];
delete memberSharesToDelete;
List<AgeGroup__Share> ageGroupSharesToDelete = [
SELECT Id
FROM
AgeGroup__Share
WHERE RowCause = :Schema.AgeGroup__Share.rowCause.DefaultExternalSharingConfiguration__c
];
delete ageGroupSharesToDelete;
List<MemberType__Share> memberTypeSharesToDelete = [
SELECT Id
FROM
MemberType__Share
WHERE RowCause = :Schema.MemberType__Share.rowCause.DefaultExternalSharingConfiguration__c
];
delete memberTypeSharesToDelete;
List<OrganisationConfigurations__Share> organisationConfigurationsSharesToDelete = [
SELECT Id
FROM
OrganisationConfigurations__Share
WHERE RowCause = :Schema.OrganisationConfigurations__Share.rowCause.DefaultExternalSharingConfiguration__c
];
delete organisationConfigurationsSharesToDelete;
List<Season__Share> seasonSharesToDelete = [
SELECT Id
FROM
Season__Share
WHERE RowCause = :Schema.Season__Share.rowCause.DefaultExternalSharingConfiguration__c
];
delete seasonSharesToDelete;
List<Product__Share> productSharesToDelete = [
SELECT Id
FROM
Product__Share
WHERE RowCause = :Schema.Product__Share.rowCause.DefaultExternalSharingConfiguration__c
];
delete productSharesToDelete;
List<Competition__Share> competitionSharesToDelete = [
SELECT Id
FROM
Competition__Share
WHERE RowCause = :Schema.Competition__Share.rowCause.DefaultExternalSharingConfiguration__c
];
delete competitionSharesToDelete;
List<CompetitionType__Share> competitionTypeSharesToDelete = [
SELECT Id
FROM
CompetitionType__Share
WHERE RowCause = :Schema.CompetitionType__Share.rowCause.DefaultExternalSharingConfiguration__c
];
delete competitionTypeSharesToDelete;
List<CompetitionGrade__Share> competitionGradeSharesToDelete = [
SELECT Id
FROM
CompetitionGrade__Share
WHERE RowCause = :Schema.CompetitionGrade__Share.rowCause.DefaultExternalSharingConfiguration__c
];
delete competitionGradeSharesToDelete;
List<Location__Share> locationSharesToDelete = [
SELECT Id
FROM
Location__Share
WHERE RowCause = :Schema.Location__Share.rowCause.DefaultExternalSharingConfiguration__c
];
delete locationSharesToDelete;
List<Team__Share> teamSharesToDelete = [
SELECT Id
FROM
Team__Share
WHERE RowCause = :Schema.Team__Share.rowCause.DefaultExternalSharingConfiguration__c
];
delete teamSharesToDelete;
}
//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 CreateUserGroups implements Queueable {
List<PublicGroupAccountWrapper> publicGroupWrapperList {
get;
set;
}
public CreateUserGroups(List<PublicGroupAccountWrapper> publicGroupsToPopulate) {
this.publicGroupWrapperList = publicGroupsToPopulate;
}
public void execute(QueueableContext context) {
System.debug('xxx CreateUserGroups Execute');
publicGroupWrapperList = SecurityUtil.insertPublicGroupsFromWrapperList(publicGroupWrapperList);
if (!Test.isRunningTest()) {
UpdateAccountsWithUserGroupIds accountUpdate = new UpdateAccountsWithUserGroupIds(publicGroupWrapperList);
ID jobID = System.enqueueJob(accountUpdate);
}
}
}
public static List<PublicGroupAccountWrapper> insertPublicGroupsFromWrapperList(List<PublicGroupAccountWrapper> publicGroupWrapperList) {
System.debug('xxx CreateUserGroups Execute');
List<Group> publicGroupsToInsert = new List<Group>();
//Abreviation dictionary for Public Group Name to improve readibility
Map<String, String> groupTypeToPrefix = new Map<String, String>{
'ChildrenUserGroup' => 'ChildrenOf', 'ChildrenIncludedUserGroup' => 'ChildrenInclusive', 'ParentUserGroup' => 'Parents', 'PrimaryUserGroup' => '', 'DirectChildrenUserGroup' => 'DirectChildren'
};
for (Integer i = 0; i < publicGroupWrapperList.size(); i++) {
Group newUserGroup = new Group();
newUserGroup.name = ConfigSupport.defaultGroupPrefix;
newUserGroup.name += groupTypeToPrefix.get(publicGroupWrapperList[i].publicGroupType);
newUserGroup.name += publicGroupWrapperList[i].account.name;
newUserGroup.name = newUserGroup.name.left(40);
publicGroupsToInsert.add(newUserGroup);
}
System.debug('xxxinsertingpublicgroups ' + publicGroupsToInsert);
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 UpdateAccountsWithUserGroupIds implements Queueable {
List<PublicGroupAccountWrapper> publicGroupWrapperList {
get;
set;
}
public UpdateAccountsWithUserGroupIds(List<PublicGroupAccountWrapper> accountsToUpdate) {
System.debug('xxx UpdateAccountsWithUserGroupIds');
this.publicGroupWrapperList = accountsToUpdate;
}
public void execute(QueueableContext context) {
System.debug('xxx executeUpdateAccountsWithUserGroupIds');
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 == 'ChildrenUserGroup') {
accountsToUpdate.get(publicGroupWrapper.account.Id).OrgSec_ChildrenUserGroupId__c = publicGroupWrapper.publicGroup.Id;
} else if (publicGroupWrapper.publicGroupType == 'ChildrenIncludedUserGroup') {
accountsToUpdate.get(publicGroupWrapper.account.Id).OrgSec_ChildrenIncludedUserGroupId__c = publicGroupWrapper.publicGroup.Id;
} else if (publicGroupWrapper.publicGroupType == 'PrimaryUserGroup') {
accountsToUpdate.get(publicGroupWrapper.account.Id).OrgSec_UserGroupId__c = publicGroupWrapper.publicGroup.Id;
} else if (publicGroupWrapper.publicGroupType == 'ParentUserGroup') {
accountsToUpdate.get(publicGroupWrapper.account.Id).OrgSec_ParentUserGroupId__c = publicGroupWrapper.publicGroup.Id;
} else if (publicGroupWrapper.publicGroupType == 'DirectChildrenUserGroup') {
accountsToUpdate.get(publicGroupWrapper.account.Id).OrgSec_DirectChildrenUserGroupId__c = publicGroupWrapper.publicGroup.Id;
}
}
update accountsToUpdate.values();
}
//Wrapper to assist with public group creation process
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) {
System.debug('xxx executePublicGroupAccountWrapper');
processAccountHierarchyMembership(targetedAccountId);
}
}
//Method consumes two sets of Ids and returns a map of sets
//Primarily used for comparison of existing and new memberships to see what existing records have to be removed
public static Map<String, Set<Id>> compareSets(Set<Id> setOne, Set<Id> setTwo) {
System.debug('xxx compareSets');
Map<String, Set<Id>> comparisonMap = new Map<String, Set<Id>>{
'In_First_Set' => new Set<Id>(),
'Present_In_Both' => new Set<Id>(),
'In_Second_Set' => new Set<Id>()
};
for (String setOneString : setOne) {
if (setTwo.contains(setOneString))
comparisonMap.get('Present_In_Both').add(setOneString); else
comparisonMap.get('In_First_Set').add(setOneString);
}
for (String setTwoString : setTwo) {
if (!setOne.contains(setTwoString))
comparisonMap.get('In_Second_Set').add(setTwoString);
}
return comparisonMap;
}
///////////////
//Hierarchy tree sorter
///////////////
//Initialising process to recalculate hierarchy for given accounts
//Any member of the account hierarchy can be identified in order to intiate a complete recalculation
public static void processAccountHierarchyMembership(Set<Id> targetedAccountsIds) {
System.debug('xxx processAccountHierarchyMembership');
//Given that the hierarchy code requires the top parent record, we first obtain the master parent records
Set<Id> topIds = getTopAccountIds(targetedAccountsIds);
List<Account> topAccounts = new List<Account>();
for (Account acc: [
select id,
name,
OrgSec_UserGroupId__c,
OrgSec_ChildrenUserGroupId__c,
OrgSec_ChildrenIncludedUserGroupId__c,
OrgSec_ParentUserGroupId__c,
OrgSec_DirectChildrenUserGroupId__c
from Account
where id IN :topIds
])
topAccounts.add(acc);
Map<Id, HierarchyNode> accMap = getAccountHierarchiesQuick(topAccounts);
memberManagement(accMap);
}
//Recursive method loop to return a Set of Accounts identifying all children accounts for a given set
private static Set<Id> traverseChildren(List<HierarchyNode> children, Map<Id, HierarchyNode> nodes) {
System.debug('xxx traverseChildren');
Set<Id> acc_ids = new Set<Id>();
for (HierarchyNode referenceNode : children) {
acc_ids.add(referenceNode.node.id);
acc_ids.addAll(traverseChildren(referenceNode.children, nodes));
}
return acc_ids;
}
//Given a list of accounts and their direct parent Ids, create new nodes for the children accounts and add them to the
//children of the parent accounts
public static void addChildren(List<Account> accs, Map<Id, HierarchyNode> nodes, Set<Id> parentAccountIds) {
System.debug('xxx addChildren');
for (Account acc : accs) {
HierarchyNode referenceNode = nodes.get(acc.ParentId);
HierarchyNode newNode = new HierarchyNode(acc, referenceNode);
nodes.put(acc.id, newNode);
if (referenceNode != null) {
referenceNode.children.add(newNode);
}
if (parentAccountIds != null) {
parentAccountIds.add(acc.id);
}
}
}
//Initialisation of the Account Hierarchy generator, when the source is an account list
//Will only return a hierarchy of the accounts at the level or below of specified list
public static Map<Id, HierarchyNode> getAccountHierarchiesQuick(List<Account> top_accts) {
Set<Id> parentAccountIds = new Set<Id>();
for (Account a : top_accts) {
parentAccountIds.add(a.Id);
}
return getAccountHierarchiesQuick(parentAccountIds, top_accts);
}
//Recursive method loop to find the master parent accounts for any accounts submitted to the testMethod
public static Set<Id> getTopAccountIds(Set<Id> accountsTargeted) {
System.debug('xxx getTopAccountIds');
Set<Id> topAccountIds = new Set<Id>();
Set<Id> recursivelyTargetedAccountIds = new Set<Id>();
for (Account targetedAccount : [
SELECT Id,
Name,
OrgSec_ChildrenUserGroupId__c,
OrgSec_ChildrenIncludedUserGroupId__c,
OrgSec_UserGroupId__c,
OrgSec_ParentUserGroupId__c,
OrgSec_DirectChildrenUserGroupId__c,
ParentID,
Parent.ParentID,
Parent.Parent.ParentID,
Parent.Parent.Parent.ParentID,
RecordTypeId
FROM Account
WHERE Id IN :accountsTargeted
]) {
if (targetedAccount.Parent.Parent.Parent.ParentID != null) {
recursivelyTargetedAccountIds.add(targetedAccount.Parent.Parent.Parent.ParentID);
} else if (targetedAccount.Parent.Parent.ParentId != null) {
topAccountIds.add(targetedAccount.Parent.Parent.ParentId);
} else if (targetedAccount.Parent.ParentId != null) {
topAccountIds.add(targetedAccount.Parent.ParentId);
} else if (targetedAccount.ParentId != null) {
topAccountIds.add(targetedAccount.ParentId);
} else {
topAccountIds.add(targetedAccount.Id);
}
}
if (!recursivelyTargetedAccountIds.isEmpty()) {
topAccountIds.addAll(getTopAccountIds(recursivelyTargetedAccountIds));
}
return topAccountIds;
}
public static Map<Id, HierarchyNode> getAccountHierarchiesQuick(Set<Id> top_acct_ids, List<Account> top_accs) {
System.debug('xxx getAccountHierarchiesQuick');
Map<Id, HierarchyNode> nodes = new Map<Id, HierarchyNode>();
Set<Id> parentAccountIds = top_acct_ids;
for (Account top_acc : top_accs) {
HierarchyNode newNode = new HierarchyNode(top_acc);
nodes.put(top_acc.id, newNode);
}
while (parentAccountIds.size() > 0) {
Map<Id, Account> subordinate_accounts =
new Map<Id, Account>([
SELECT Id,
Name,
OrgSec_ChildrenUserGroupId__c,
OrgSec_ChildrenIncludedUserGroupId__c,
OrgSec_UserGroupId__c,
OrgSec_ParentUserGroupId__c,
OrgSec_DirectChildrenUserGroupId__c,
Parent.ParentID,
Parent.Parent.ParentID,
Parent.Parent.Parent.ParentID,
RecordTypeId, (
SELECT Id
FROM Contacts
)
FROM Account
WHERE (ParentId IN :parentAccountIds)
OR
(Parent.ParentId IN :parentAccountIds)
OR
(Parent.Parent.ParentId IN :parentAccountIds)
OR
(Parent.Parent.Parent.ParentId IN :parentAccountIds)
]);
List<Account> level1_accs = new List<Account>();
List<Account> level2_accs = new List<Account>();
List<Account> level3_accs = new List<Account>();
List<Account> level4_accs = new List<Account>();
// Split accounts in levels
for (Account acc : subordinate_accounts.values()) {
if (acc.ParentId != null) {
if (parentAccountIds.contains(acc.ParentId)) {
level1_accs.add(acc);
} else if (acc.Parent.ParentId != null) {
if (parentAccountIds.contains(acc.Parent.ParentId)) {
level2_accs.add(acc);
} else if (acc.Parent.Parent.ParentId != null) {
if (parentAccountIds.contains(acc.Parent.Parent.ParentId)) {
level3_accs.add(acc);
} else if (acc.Parent.Parent.Parent.ParentId != null) {
if (parentAccountIds.contains(acc.Parent.Parent.Parent.ParentId)) {
level4_accs.add(acc);
}
}
}
}
}
}
Set<Id> next_parentAccountIds = new Set<Id>();
// Add children on all levels found, one level at a time
addChildren(level1_accs, nodes, null);
addChildren(level2_accs, nodes, null);
addChildren(level3_accs, nodes, null);
addChildren(level4_accs, nodes, next_parentAccountIds);
// Use lowest level of account ids for next SOQL query
parentAccountIds = next_parentAccountIds;
}
return nodes;
}
//process individual membership per public group of each account
public static void memberManagement(Map<Id, HierarchyNode> nodes) {
System.debug('xxx memberManagement');
Map<Id, Id> childUserGroupIdToAccountId = new Map<Id, Id>();
Map<Id, Id> childrenIncludedUserGroupIdToAccountId = new Map<Id, Id>();
Map<Id, Id> primaryUserGroupIdToAccountId = new Map<Id, Id>();
Map<Id, Id> parentUserGroupIdToAccountId = new Map<Id, Id>();
Map<Id, Id> directChildrenUserGroupIdToAccountId = new Map<Id, Id>();
List<GroupMember> groupMembersToDelete = new List<GroupMember>();
List<GroupMember> groupMembersToInsert = new List<GroupMember>();
//obtain complete list of public groups to process
for (HierarchyNode hierarchyNode : nodes.values()) {
childUserGroupIdToAccountId.put(hierarchyNode.node.OrgSec_ChildrenUserGroupId__c, hierarchyNode.node.Id);
childrenIncludedUserGroupIdToAccountId.put(hierarchyNode.node.OrgSec_ChildrenIncludedUserGroupId__c, hierarchyNode.node.Id);
primaryUserGroupIdToAccountId.put(hierarchyNode.node.OrgSec_UserGroupId__c, hierarchyNode.node.Id);
parentUserGroupIdToAccountId.put(hierarchyNode.node.OrgSec_ParentUserGroupId__c, hierarchyNode.node.Id);
directChildrenUserGroupIdToAccountId.put(hierarchyNode.node.OrgSec_DirectChildrenUserGroupId__c, hierarchyNode.node.Id);
}
Set<Id> targetedPublicGroupIds = new Set<Id>();
targetedPublicGroupIds.addAll(childUserGroupIdToAccountId.keySet());
targetedPublicGroupIds.addAll(childrenIncludedUserGroupIdToAccountId.keySet());
targetedPublicGroupIds.addAll(primaryUserGroupIdToAccountId.keySet());
targetedPublicGroupIds.addAll(parentUserGroupIdToAccountId.keySet());
targetedPublicGroupIds.addAll(directChildrenUserGroupIdToAccountId.keySet());
//search all account related roles for role mapping
Map<Id, UserRole> accountRelatedRoles = new Map<Id, UserRole>([
SELECT Id,
PortalAccountId
FROM UserRole
WHERE PortalAccountId IN :primaryUserGroupIdToAccountId.values()
]);
Map<Id, Id> accountIdToPrimaryRoleGroupId = new Map<Id, Id>();
//
Map<Id, Group> groupIdToGroup = new map<Id, Group>([
SELECT Id,
Type,
RelatedId, (
SELECT Id,
UserOrGroupId,
GroupId
FROM GroupMembers
)
FROM Group
WHERE Id IN :targetedPublicGroupIds
OR RelatedId IN :accountRelatedRoles.keySet()
]);
//
for (Id groupId : groupIdToGroup.keySet()) {
Group targetedGroup = groupIdToGroup.get(groupId);
if (targetedGroup.Type == 'Role') {
if (accountRelatedRoles.containsKey(targetedGroup.RelatedId))
accountIdToPrimaryRoleGroupId.put(accountRelatedRoles.get(targetedGroup.RelatedId).PortalAccountId, groupId);
}
}
//
Set<Id> groupIdsForFinalMembershipRecalc = new Set<Id>();
for (Id groupId : groupIdToGroup.keySet()) {
Group targetedGroup = groupIdToGroup.get(groupId);
if (childUserGroupIdToAccountId.containsKey(groupId)) {
//should be all children group and the associated primary group
HierarchyNode targetedNode = nodes.get(childUserGroupIdToAccountId.get(groupId));
Set<Id> allChildNodes = traverseChildren(targetedNode.children, nodes);
Set<Id> childUserGroupsToBePresent = new Set<Id>();
for (Id childNodeId : allChildNodes)
childUserGroupsToBePresent.add(nodes.get(childNodeId).node.OrgSec_UserGroupId__c);
Map<Id, Id> memberDetailIdToGroupMemberId = new Map<Id, Id>();
for (GroupMember groupMember : groupIdToGroup.get(groupId).GroupMembers)
memberDetailIdToGroupMemberId.put(groupMember.UserOrGroupId, groupMember.Id);
Map<String, Set<Id>> comparedSet = compareSets(childUserGroupsToBePresent, memberDetailIdToGroupMemberId.keySet());
if (comparedSet.containsKey('In_First_Set')) {
for (Id groupMemberUserGroup: comparedSet.get('In_First_Set'))
groupMembersToInsert.add(new GroupMember(GroupId = groupId, UserOrGroupId = groupMemberUserGroup));
}
if (comparedSet.containsKey('In_Second_Set')) {
for (Id groupMemberUserGroupId: comparedSet.get('In_Second_Set'))
groupMembersToDelete.add(new GroupMember(id = memberDetailIdToGroupMemberId.get(groupMemberUserGroupId)));
}
} else if (childrenIncludedUserGroupIdToAccountId.containsKey(groupId)) {
//should be all children group and the associated primary group
Set<Id> childUserGroupsToBePresent = new Set<Id>();
HierarchyNode targetedNode = nodes.get(childrenIncludedUserGroupIdToAccountId.get(groupId));
if (targetedNode.node.OrgSec_ChildrenUserGroupId__c != null)
childUserGroupsToBePresent.add(targetedNode.node.OrgSec_ChildrenUserGroupId__c);
if (targetedNode.node.OrgSec_UserGroupId__c != null)
childUserGroupsToBePresent.add(targetedNode.node.OrgSec_UserGroupId__c);
Map<Id, Id> memberDetailIdToGroupMemberId = new Map<Id, Id>();
for (GroupMember groupMember : groupIdToGroup.get(groupId).GroupMembers)
memberDetailIdToGroupMemberId.put(groupMember.UserOrGroupId, groupMember.Id);
Map<String, Set<Id>> comparedSet = compareSets(childUserGroupsToBePresent, memberDetailIdToGroupMemberId.keySet());
if (comparedSet.containsKey('In_First_Set')) {
for (Id groupMemberUserGroup: comparedSet.get('In_First_Set'))
groupMembersToInsert.add(new GroupMember(GroupId = groupId, UserOrGroupId = groupMemberUserGroup));
}
if (comparedSet.containsKey('In_Second_Set')) {
for (Id groupMemberUserGroup: comparedSet.get('In_Second_Set'))
groupMembersToDelete.add(new GroupMember(Id = memberDetailIdToGroupMemberId.get(groupMemberUserGroup)));
}
} else if (parentUserGroupIdToAccountId.containsKey(groupId)) {
//primary user group of all parents
HierarchyNode targetedNode = nodes.get(parentUserGroupIdToAccountId.get(groupId));
Set<Id> parentUserGroupsToBePresent = new Set<Id>();
for (HierarchyNode parentNode : targetedNode.parents)
parentUserGroupsToBePresent.add(parentNode.node.OrgSec_UserGroupId__c);
Map<Id, Id> memberDetailIdToGroupMemberId = new Map<Id, Id>();
for (GroupMember groupMember : groupIdToGroup.get(groupId).GroupMembers)
memberDetailIdToGroupMemberId.put(groupMember.UserOrGroupId, groupMember.Id);
Map<String, Set<Id>> comparedSet = compareSets(parentUserGroupsToBePresent, memberDetailIdToGroupMemberId.keySet());
if (comparedSet.containsKey('In_First_Set')) {
for (Id groupMemberUserGroup: comparedSet.get('In_First_Set'))
groupMembersToInsert.add(new GroupMember(
GroupId = groupId,
UserOrGroupId = groupMemberUserGroup));
}
if (comparedSet.containsKey('In_Second_Set')) {
for (Id groupMemberUserGroup: comparedSet.get('In_Second_Set'))
groupMembersToDelete.add(new GroupMember(Id = memberDetailIdToGroupMemberId.get(groupMemberUserGroup)));
}
} else if (primaryUserGroupIdToAccountId.containsKey(groupId)) {
HierarchyNode targetedNode = nodes.get(primaryUserGroupIdToAccountId.get(groupId));
Set<Id> roleGroupsToBePresent = new Set<Id>();
if (accountIdToPrimaryRoleGroupId.containsKey(targetedNode.node.Id)) {
roleGroupsToBePresent.add(accountIdToPrimaryRoleGroupId.get(targetedNode.node.Id));
}
Map<Id, Id> memberDetailIdToGroupMemberId = new Map<Id, Id>();
for (GroupMember groupMember : groupIdToGroup.get(groupId).GroupMembers) {
memberDetailIdToGroupMemberId.put(groupMember.UserOrGroupId, groupMember.Id);
}
Map<String, Set<Id>> comparedSet = compareSets(roleGroupsToBePresent, memberDetailIdToGroupMemberId.keySet());
if (comparedSet.containsKey('In_First_Set')) {
for (Id groupMemberUserGroup: comparedSet.get('In_First_Set')) {
groupMembersToInsert.add(new GroupMember(GroupId = groupId, UserOrGroupId = groupMemberUserGroup));
}
}
if (comparedSet.containsKey('In_Second_Set')) {
for (Id groupMemberUserGroup: comparedSet.get('In_Second_Set')) {
groupMembersToDelete.add(new GroupMember(Id = memberDetailIdToGroupMemberId.get(groupMemberUserGroup)));
}
}
} else if (directChildrenUserGroupIdToAccountId.containsKey(groupId)) {
//should be all children group and the associated primary group
HierarchyNode targetedNode = nodes.get(directChildrenUserGroupIdToAccountId.get(groupId));
Set<Id> childUserGroupsToBePresent = new Set<Id>();
for (HierarchyNode childNode : targetedNode.children)
childUserGroupsToBePresent.add(childNode.node.OrgSec_UserGroupId__c);
Map<Id, Id> memberDetailIdToGroupMemberId = new Map<Id, Id>();
for (GroupMember groupMember : groupIdToGroup.get(groupId).GroupMembers)
memberDetailIdToGroupMemberId.put(groupMember.UserOrGroupId, groupMember.Id);
Map<String, Set<Id>> comparedSet = compareSets(childUserGroupsToBePresent, memberDetailIdToGroupMemberId.keySet());
if (comparedSet.containsKey('In_First_Set')) {
for (Id groupMemberUserGroup: comparedSet.get('In_First_Set'))
groupMembersToInsert.add(new GroupMember(GroupId = groupId, UserOrGroupId = groupMemberUserGroup));
}
if (comparedSet.containsKey('In_Second_Set')) {
for (Id groupMemberUserGroupId: comparedSet.get('In_Second_Set'))
groupMembersToDelete.add(new GroupMember(id = memberDetailIdToGroupMemberId.get(groupMemberUserGroupId)));
}
}
}
delete groupMembersToDelete;
insert groupMembersToInsert;
}
//Wrapper Class that is used as nodes in the account hierarchy tree built
public class HierarchyNode {
public HierarchyNode parent;
public Account node;
public List<HierarchyNode> children;
public List<HierarchyNode> parents;
HierarchyNode(Account acc, HierarchyNode parent) {
this.parent = parent;
this.node = acc;
this.children = new List<HierarchyNode>();
this.parents = new List<HierarchyNode>();
this.parents.addAll(parent.parents);
this.parents.add(parent);
}
HierarchyNode(Account acc) {
this.parent = null;
this.node = acc;
this.children = new List<HierarchyNode>();
this.parents = new List<HierarchyNode>();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment