From 18b62f9f73f43f5c18d53ba063f10f9dfaaad5e9 Mon Sep 17 00:00:00 2001 From: Darshana Gunawardana Date: Mon, 2 Sep 2024 09:12:34 +0530 Subject: [PATCH] Optimize template addition & update by cross checking the content with system default templates --- .../mgt/store/DefaultTemplateManager.java | 24 +++++++++++++++++-- .../store/InMemoryBasedTemplateManager.java | 12 ++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/DefaultTemplateManager.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/DefaultTemplateManager.java index 65fc6da1..72e6b516 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/DefaultTemplateManager.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/DefaultTemplateManager.java @@ -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() { @@ -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 diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/InMemoryBasedTemplateManager.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/InMemoryBasedTemplateManager.java index 4d33cbbb..016f58be 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/InMemoryBasedTemplateManager.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/InMemoryBasedTemplateManager.java @@ -260,4 +260,16 @@ private Map> 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); + } }