Skip to content

Commit

Permalink
Cleaned up some code & added a guard clause to avoid running a query …
Browse files Browse the repository at this point in the history
…when it's guaranteed to be empty
  • Loading branch information
jongpie committed Sep 1, 2022
1 parent 09c87b8 commit 127894a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
42 changes: 22 additions & 20 deletions nebula-logger/core/main/log-management/classes/LogHandler.cls
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public without sharing class LogHandler extends LoggerSObjectHandler {

protected override void executeBeforeInsert(List<SObject> triggerNew) {
this.logs = (List<Log__c>) triggerNew;
this.logScenariosById = this.queryLogScenarios();
this.logScenariosById = queryLogScenarios(this.logs);

this.setClosedStatusFields();
// The log OwnerId field should support being manually changed, so only auto-set it on insert
Expand All @@ -53,23 +53,6 @@ public without sharing class LogHandler extends LoggerSObjectHandler {
this.shareLogsWithLoggingUsers();
}

private Map<Id, LogScenario__c> queryLogScenarios() {
Map<Id, LogScenario__c> logScenariosById = new Map<Id, LogScenario__c>();
List<Id> logScenarioIds = new List<Id>();
for (Log__c log : this.logs) {
if (log.LogScenario__c != null) {
logScenarioIds.add(log.LogScenario__c);
}
}

List<LogScenario__c> matchingLogScenarios = LogManagementDataSelector.getInstance().getLogScenariosById(logScenarioIds);
if (matchingLogScenarios != null && matchingLogScenarios.isEmpty() == false) {
logScenariosById = new Map<Id, LogScenario__c>(matchingLogScenarios);
}

return logScenariosById;
}

private void setClosedStatusFields() {
Map<String, LogStatus__mdt> logStatusNameToStatus = loadActiveLogStatuses();
for (Log__c log : this.logs) {
Expand Down Expand Up @@ -164,7 +147,6 @@ public without sharing class LogHandler extends LoggerSObjectHandler {
LogScenarioRule__mdt matchingScenarioRule;
if (log.LogScenario__c != null) {
LogScenario__c logScenario = this.logScenariosById.get(log.LogScenario__c);

matchingScenarioRule = scenarioToScenarioRule.get(logScenario.UniqueId__c);
}

Expand Down Expand Up @@ -250,6 +232,27 @@ public without sharing class LogHandler extends LoggerSObjectHandler {
return logStatusNameToStatus;
}

private Map<Id, LogScenario__c> queryLogScenarios(List<Log__c> logs) {
List<Id> logScenarioIds = new List<Id>();
for (Log__c log : logs) {
if (log.LogScenario__c != null) {
logScenarioIds.add(log.LogScenario__c);
}
}

Map<Id, LogScenario__c> logScenariosById = new Map<Id, LogScenario__c>();

if (logScenarioIds.isEmpty() == true) {
return logScenariosById;
}

List<LogScenario__c> matchingLogScenarios = LogManagementDataSelector.getInstance().getLogScenariosById(logScenarioIds);
if (matchingLogScenarios != null && matchingLogScenarios.isEmpty() == false) {
logScenariosById = new Map<Id, LogScenario__c>(matchingLogScenarios);
}
return logScenariosById;
}

private static Map<String, Id> queryQueues(List<String> possibleQueueNames) {
Map<String, Id> queuesByDeveloperName = new Map<String, Id>();
for (Group queue : LogManagementDataSelector.getInstance().getQueuesByDeveloperName(possibleQueueNames)) {
Expand All @@ -273,7 +276,6 @@ public without sharing class LogHandler extends LoggerSObjectHandler {
scenarios.add(logScenariosById.get(log.LogScenario__c).UniqueId__c);
}
}
// System.assert(false, 'queried rule scenarios: ' + json.serialize(scenarios) + '\nalso the logScenariosById: ' + json.serialize(logScenariosById));

Map<String, LogScenarioRule__mdt> scenarioToScenarioRule = new Map<String, LogScenarioRule__mdt>();
for (LogScenarioRule__mdt scenarioRule : LogScenarioRule__mdt.getAll().values()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public without sharing class LogManagementDataSelector {
}

/**
* @description Returns a `List<LogScenario__c>` of records with the specified unique IDs
* @description Returns a `List<LogScenario__c>` of records with the specified log scenario IDs
* @param logScenarioIds The list of `ID` of the `Log__c` records to query
* @return The list of matching `LogScenario__c` records
*/
Expand Down

0 comments on commit 127894a

Please sign in to comment.