Skip to content

Commit

Permalink
Merge pull request #112 from LawrenceLoz/Additional_extension_support
Browse files Browse the repository at this point in the history
Dynamic component initialisation and additional extension hooks
  • Loading branch information
LawrenceLoz authored Mar 1, 2024
2 parents 3dfce96 + 9a1af24 commit 29e9ab1
Show file tree
Hide file tree
Showing 38 changed files with 441 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,14 @@ public inherited sharing class FormulaShareEntityDefinitionSelector {
return entities;
}

public List<EntityDefinition> getAllCustomMetadataTypes() {
List<EntityDefinition> entities = [SELECT QualifiedApiName, DurableId, Label, PluralLabel, DetailUrl
FROM EntityDefinition
WHERE QualifiedApiName LIKE '%__mdt'
AND (NOT QualifiedApiName LIKE 'sdfs__FormulaShare%')
WITH SECURITY_ENFORCED
ORDER BY Label];
return entities;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ public inherited sharing virtual class FormulaShareMetadataDMLRules extends Form
namespacePrefix + 'Shared_To__c',
namespacePrefix + 'Access_For_Team__c',
namespacePrefix + 'Access_For_Owner_Of_Teams_User_Is_On__c',
namespacePrefix + 'Access_For_Team_Comembers__c'
namespacePrefix + 'Access_For_Team_Comembers__c',
namespacePrefix + 'Metadata_Mapping_Type__c',
namespacePrefix + 'Metadata_Mapping_Match_Field__c',
namespacePrefix + 'Metadata_Mapping_Shared_To_Field__c'
};
this.fieldsToSetOrClear.addAll(new FormulaShareInjectionService().getExtraRelationshipFields());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ public inherited sharing class FormulaShareMetadataDMLRulesAccount extends Formu

this.fieldsToSetOrClear = new Set<String> {
namespacePrefix + 'Shared_To_Field_Type__c',
namespacePrefix + 'Shared_To__c'
namespacePrefix + 'Shared_To__c',
namespacePrefix + 'Metadata_Mapping_Type__c',
namespacePrefix + 'Metadata_Mapping_Match_Field__c',
namespacePrefix + 'Metadata_Mapping_Shared_To_Field__c'
};
this.fieldsToSetOrClear.addAll(new FormulaShareInjectionService().getExtraRelationshipFields());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,21 @@ public inherited sharing class FormulaShareRetrievedRecordDetails {
public FormulaShareRetrievedRecordDetails() {
controllingRecordsForRulesMap = new Map<String,List<SObject>>();
}

public List<SObject> getControllingObjectsForRule(String ruleDeveloperName) {
List<SObject> controllingObjectsForRule;

// If controlling records map doesn't include this rule (e.g. standard rule)
// then controlling records are the shared records
if(!controllingRecordsForRulesMap.containsKey(ruleDeveloperName)) {
controllingObjectsForRule = new List<SObject>{sharedRecord};
}

// Otherwise, controlling records should be set in map
else {
controllingObjectsForRule = controllingRecordsForRulesMap.get(ruleDeveloperName);
}

return controllingObjectsForRule;
}
}
15 changes: 15 additions & 0 deletions fs-core/main/default/classes/FormulaShareRule.cls
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public virtual inherited sharing class FormulaShareRule {
@AuraEnabled public String controllingObjectSharedToFieldToken;
@AuraEnabled public String controllingObjectSharedToFieldType;

@AuraEnabled public String mdMappingType;
@AuraEnabled public String mdMappingMatchField;
@AuraEnabled public String mdMappingSharedToField;

@AuraEnabled public String accessForTeam;
@AuraEnabled public String accessForOwnerOfTeamsUserIsOn;
@AuraEnabled public String accessForTeamComembers;
Expand Down Expand Up @@ -118,6 +122,11 @@ public virtual inherited sharing class FormulaShareRule {
caseAccess = rule.Case_Access__c;
opportunityAccess = rule.Opportunity_Access__c;

// Metadata mapping
mdMappingType = rule.Metadata_Mapping_Type__c;
mdMappingMatchField = rule.Metadata_Mapping_Match_Field__c;
mdMappingSharedToField = rule.Metadata_Mapping_Shared_To_Field__c;

// Access levels for default team sharing
accessForTeam = rule.Access_For_Team__c;
accessForOwnerOfTeamsUserIsOn = rule.Access_For_Owner_Of_Teams_User_Is_On__c;
Expand Down Expand Up @@ -348,4 +357,10 @@ public virtual inherited sharing class FormulaShareRule {
}
}

public Boolean isMappedToMetadata {
get {
return mdMappingType != null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public abstract inherited sharing class FormulaShareRuleConverterBase {
mdRule.Opportunity_Access__c = rule.opportunityAccess;
mdRule.Sharing_Reason__c = rule.sharingReason;

mdRule.Metadata_Mapping_Type__c = rule.mdMappingType;
mdRule.Metadata_Mapping_Match_Field__c = rule.mdMappingMatchField;
mdRule.Metadata_Mapping_Shared_To_Field__c = rule.mdMappingSharedToField;

mdRule.Access_For_Team__c = rule.accessForTeam;
mdRule.Access_For_Owner_Of_Teams_User_Is_On__c = rule.accessForOwnerOfTeamsUserIsOn;
mdRule.Access_For_Team_Comembers__c = rule.accessForTeamComembers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public inherited sharing class FormulaShareRuleConverterStandard extends Formula
rule.controllingObjectApiName = mdRule.Object_Shared__r.QualifiedApiName;
rule.controllingObjectLabel = mdRule.Object_Shared__r.MasterLabel;
rule.controllingObjectSharedToFieldAPIName = mdRule.Shared_To__r.QualifiedApiName;
rule.controllingObjectSharedToFieldLabel = mdRule.Shared_To__r.QualifiedApiName;
rule.controllingObjectSharedToFieldLabel = mdRule.Shared_To__r.MasterLabel;
rule.controllingObjectSharedToFieldToken = mdRule.Shared_To__c;
rule.controllingObjectSharedToFieldType = mdrule.Shared_To_Field_Type__c;

Expand Down
21 changes: 21 additions & 0 deletions fs-core/main/default/classes/FormulaShareRuleDetailController.cls
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,27 @@ public with sharing class FormulaShareRuleDetailController {
return FormulaShareUtilities.getParentRelationships(childObjectAPIName);
}

@AuraEnabled(cacheable=true)
public static Map<String,String> getAllCustomMetadataTypes() {
List<EntityDefinition> eds = FormulaShareEntityDefinitionSelector.construct().getAllCustomMetadataTypes();
Map<String,String> edsMap = new Map<String,String>();
for(EntityDefinition ed : eds) {
edsMap.put(ed.QualifiedApiName, ed.Label);
}
return edsMap;
}

@AuraEnabled(cacheable=true)
public static Map<String,String> getTextFields(String cmdtApiName) {
List<Schema.SObjectField> fields = FormulaShareUtilities.getTextFields(cmdtApiName);
Map<String,String> fieldsMap = new Map<String,String>();
for(Schema.SObjectField field : fields) {
Schema.DescribeFieldResult dfr = field.getDescribe();
fieldsMap.put(dfr.getName(), dfr.getLabel());
}
return fieldsMap;
}


@AuraEnabled(cacheable=true)
public static List<ShareFieldOption> getShareFieldOptions(String objectApiName){
Expand Down
2 changes: 1 addition & 1 deletion fs-core/main/default/classes/FormulaShareRuleStandard.cls
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**/

public inherited sharing class FormulaShareRuleStandard extends FormulaShareRule {
public virtual inherited sharing class FormulaShareRuleStandard extends FormulaShareRule {

public FormulaShareRuleStandard() {
type = 'standard';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ public with sharing class FormulaShareRulesListViewController {
rw.tableLabel = rule.label;
rw.label = rule.label;
rw.objectLabel = rule.objectSharedLabel;
rw.controllingObject = rule.controllingObjectLabel;
rw.shareWith = rule.shareWith;
rw.sharingReason = rule.sharingReason;
rw.accessLevel = rule.accessLevel;
Expand Down Expand Up @@ -125,7 +124,8 @@ public with sharing class FormulaShareRulesListViewController {

// Set URL for shared to field if populated
// System.debug('this field detail. Label: '+rule.controllingObjectSharedToFieldLabel+' | token: '+rule.controllingObjectSharedToFieldToken);
if(!String.isBlank(rule.controllingObjectSharedToFieldToken) && rule.controllingObjectSharedToFieldToken.contains('.')) {
if(!rule.isMappedToMetadata && !String.isBlank(rule.controllingObjectSharedToFieldToken) && rule.controllingObjectSharedToFieldToken.contains('.')) {
rw.controllingObject = rule.controllingObjectLabel;

// Build URL to field from token (includes object and field id, separated by full stop)
Integer sharedToPoint = rule.controllingObjectSharedToFieldToken.indexOf('.');
Expand All @@ -140,6 +140,19 @@ public with sharing class FormulaShareRulesListViewController {
rw.sharedToLinkLabel = rule.controllingObjectSharedToFieldLabel;
}

else if(rule.isMappedToMetadata) {
try {
Schema.SObjectType mdMappingType = FormulaShareUtilities.getSObjectTypeFromName(rule.mdMappingType);
rw.controllingObject = mdMappingType.getDescribe().getLabel();
Schema.SObjectField mdMappingSharedToFieldType = FormulaShareUtilities.getSObjectFieldFromName(rule.mdMappingSharedToField, mdMappingType);
rw.sharedToLinkLabel = mdMappingSharedToFieldType.getDescribe().getLabel();
rw.sharedToLink = FormulaShareLWCUtilities.getLightningDomain() + '/lightning/setup/CustomMetadata/home';
}
catch(Exception e) {
System.debug('Unable to resolve metadata type or field name');
}
}

// Set calculation status depending on most recent logs
if(!ruleRunDetailMap.containsKey(rw.developerName)) {
rw.lastCalcStatus = 'Pending';
Expand Down
4 changes: 4 additions & 0 deletions fs-core/main/default/classes/FormulaShareRulesSelector.cls
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public virtual inherited sharing class FormulaShareRulesSelector {
'Sharing_Reason__c',
'Shared_To_Field_Type__c',

'Metadata_Mapping_Type__c',
'Metadata_Mapping_Match_Field__c',
'Metadata_Mapping_Shared_To_Field__c',

'Object_Shared__c',
'Object_Shared__r.QualifiedApiName',
'Object_Shared__r.MasterLabel',
Expand Down
35 changes: 21 additions & 14 deletions fs-core/main/default/classes/FormulaShareService.cls
Original file line number Diff line number Diff line change
Expand Up @@ -395,32 +395,38 @@ public inherited sharing class FormulaShareService {


private List<RecordRule> buildRecordRules() {

FormulaShareSharedValueMapper valueMapper = new FormulaShareInjectionService().getSharedValueMapper();
valueMapper.setRules(rules, recordDetails);

List<RecordRule> allRecordRules = new List<RecordRule>();

for(FormulaShareRule rule : rules) {
for(FormulaShareRetrievedRecordDetails details : recordDetails) {

List<SObject> controllingObjectsForRule = new List<SObject>();

// If controlling records map doesn't include this rule (e.g. standard rule)
// then controlling records are the shared records
if(!details.controllingRecordsForRulesMap.containsKey(rule.developerName)) {
controllingObjectsForRule.add(details.sharedRecord);
}
for(FormulaShareRetrievedRecordDetails details : recordDetails) {

// Otherwise, controlling records should be set in map
else {
controllingObjectsForRule = details.controllingRecordsForRulesMap.get(rule.developerName);
}
List<SObject> controllingObjectsForRule = details.getControllingObjectsForRule(rule.developerName);

for(SOBject controllingObj : controllingObjectsForRule) {
for(SObject controllingObj : controllingObjectsForRule) {
System.debug('controllingObj: '+controllingObj);
System.debug('Rule: '+rule.developerName);
RecordRule recRule = new RecordRule();
recRule.recordToShare = details.sharedRecord;
recRule.recordWithSharedToDetails = controllingObj;
recRule.sharedToString = rule.getSharedToValueFromRecord(controllingObj);
recRule.rule = rule;

try {
recRule.sharedToString = valueMapper.getSharedToValue(rule, controllingObj);
}
// FormulaShareException suggests share value can't be found for this record
catch(FormulaShareException e) {
captureEntityNotFound(recRule.recordToShare, recRule.rule, e.getMessage());
}
// Other exceptions are likely to be query exceptions, so log and don't add this record rule
catch(Exception e) {
logExceptionAllRecords(e.getMessage());
break;
}

allRecordRules.add(recRule);
}
Expand All @@ -431,6 +437,7 @@ public inherited sharing class FormulaShareService {
}



public void updateShareMaps(Id recordId, Id sharedTo, String targetAccessLevel, FormulaShareRule rule) {

// Check whether we have any shares for this record already
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
public virtual inherited sharing class FormulaShareSharedValueMapper {

public virtual void setRules(List<FormulaShareRule> rules, List<FormulaShareRetrievedRecordDetails> recordDetails) {}

public virtual String getSharedToValue(FormulaShareRule rule, SObject controllingObj) {
return rule.getSharedToValueFromRecord(controllingObj);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>59.0</apiVersion>
<status>Active</status>
</ApexClass>
19 changes: 19 additions & 0 deletions fs-core/main/default/classes/FormulaShareUtilities.cls
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,25 @@ public inherited sharing class FormulaShareUtilities {
}


public static List<Schema.SObjectField> getTextFields(String objectApiName) {
List<Schema.SObjectField> textFields = new List<Schema.SObjectField>();

Map<String,Schema.SObjectField> allFieldsMap = getFieldMap(objectAPIName);
for(Schema.SObjectField field : allFieldsMap.values()) {
Schema.DescribeFieldResult fieldDesc = field.getDescribe();
Schema.DisplayType fieldType = fieldDesc.getType();

// Check whether field could contain information for sharing
if(fieldType == Schema.DisplayType.String ||
fieldType == Schema.DisplayType.Picklist) {
textFields.add(field);
}
}

return textFields;
}


// Checks and excludes compound fields
private static Boolean isSupportedFieldForCustomMetadata(String objectApiName, Schema.DescribeFieldResult fieldDesc) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@
<behavior>Edit</behavior>
<field>Description__c</field>
</layoutItems>
<layoutItems>
<behavior>Edit</behavior>
<field>Metadata_Mapping_Type__c</field>
</layoutItems>
<layoutItems>
<behavior>Edit</behavior>
<field>Metadata_Mapping_Match_Field__c</field>
</layoutItems>
<layoutItems>
<behavior>Edit</behavior>
<field>Metadata_Mapping_Shared_To_Field__c</field>
</layoutItems>
</layoutColumns>
<layoutColumns>
<layoutItems>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { createElement } from 'lwc';
import FormulaShareBrowseFieldContents from 'c/formulaShareBrowseFieldContents';

describe('c-formula-share-browse-field-contents', () => {
afterEach(() => {
// The jsdom instance is shared across test cases in a single file so reset the DOM
while (document.body.firstChild) {
document.body.removeChild(document.body.firstChild);
}
});

it('TODO: test case generated by CLI command, please fill in test logic', () => {
// Arrange
const element = createElement('c-formula-share-browse-field-contents', {
is: FormulaShareBrowseFieldContents
});

// Act
document.body.appendChild(element);

// Assert
// const div = element.shadowRoot.querySelector('div');
expect(1).toBe(1);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<template>
<div class="slds-box slds-theme_shade">

<div class="slds-form slds-form_stacked">
<div class="slds-form-element">
<div class="slds-form-element__control">

<template if:true={fieldFormula}>
<lightning-textarea
id="fieldFormula"
label="Formula"
value={fieldFormula}
read-only>
</lightning-textarea>
</template>

<lightning-textarea
id="sample"
label="Sample data (from first 100 records)"
value={fieldSample}
read-only>
</lightning-textarea>
<template if:true={loadingSample}>
<lightning-spinner alternative-text="Loading" size="small"></lightning-spinner>
</template>

</div>
</div>
</div>
</div>
</template>
Loading

0 comments on commit 29e9ab1

Please sign in to comment.