Skip to content

Commit

Permalink
Optimize template addition & update by cross checking the content wit…
Browse files Browse the repository at this point in the history
…h system default templates
  • Loading branch information
darshanasbg committed Sep 2, 2024
1 parent e478992 commit 18b62f9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
public class DefaultTemplateManager implements TemplatePersistenceManager {

private final TemplatePersistenceManager templatePersistenceManager;
private final TemplatePersistenceManager inMemoryTemplateManager = new InMemoryBasedTemplateManager();
private final InMemoryBasedTemplateManager inMemoryTemplateManager = new InMemoryBasedTemplateManager();

public DefaultTemplateManager() {

Expand Down Expand Up @@ -87,7 +87,27 @@ public void deleteNotificationTemplateType(String displayName, String notificati
public void addOrUpdateNotificationTemplate(NotificationTemplate notificationTemplate, String applicationUuid,
String tenantDomain) throws NotificationTemplateManagerServerException {

templatePersistenceManager.addOrUpdateNotificationTemplate(notificationTemplate, applicationUuid, tenantDomain);
if (!inMemoryTemplateManager.hasSameTemplate(notificationTemplate)) {
templatePersistenceManager.addOrUpdateNotificationTemplate(notificationTemplate, applicationUuid,
tenantDomain);
} else {
// Template is already managed as a system default template. Handle add or update.
String displayName = notificationTemplate.getDisplayName();
String locale = notificationTemplate.getLocale();
String notificationChannel = notificationTemplate.getNotificationChannel();
boolean isExistsInStorage =
templatePersistenceManager.isNotificationTemplateExists(displayName, locale, notificationChannel,
applicationUuid, tenantDomain);
if (isExistsInStorage) {
// This request is to reset existing template to default content. Hence, delete the existing template.
templatePersistenceManager.deleteNotificationTemplate(displayName, locale, notificationChannel,
applicationUuid, tenantDomain);
} else {
// This request is to add a new template with a same content that is already managed as a system default
// template. Storing such templates is redundant. Hence, avoid storing those templates as duplicate
// contents to optimize the storage.
}
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,4 +260,16 @@ private Map<String, Map<String, NotificationTemplate>> getTemplateMap(String not
}
return defaultEmailTemplates;
}

/**
* Checks if the there is a template available as a system default template with the exact same details.
* This method is used to avoid managing duplicate templates.
*
* @param template Notification template to check.
* @return True if a template with the same details is available, false otherwise.
*/
boolean hasSameTemplate(NotificationTemplate template) {
//TODO: Check case sensitivity
return defaultEmailTemplates.containsValue(template);
}
}

0 comments on commit 18b62f9

Please sign in to comment.