From 6401241f2f4004ff9d1812550a40b13010517c4a Mon Sep 17 00:00:00 2001 From: rushannanayakkara Date: Tue, 2 Jul 2024 16:55:14 +0530 Subject: [PATCH 01/13] Add Notification Template API SMS template functions --- .../mgt/NotificationTemplateManagerImpl.java | 663 ++++++++++++++++++ .../mgt/constants/TemplateMgtConstants.java | 102 +++ .../mgt/internal/I18nMgtServiceComponent.java | 16 + .../mgt/store/DBBasedTemplateManager.java | 55 ++ .../mgt/store/TemplatePersistenceManager.java | 26 + 5 files changed, 862 insertions(+) create mode 100644 components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java create mode 100644 components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/TemplateMgtConstants.java diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java new file mode 100644 index 00000000..6733a577 --- /dev/null +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java @@ -0,0 +1,663 @@ +/* + * Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.email.mgt; + +import org.apache.commons.lang.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.email.mgt.constants.TemplateMgtConstants; +import org.wso2.carbon.email.mgt.internal.I18nMgtDataHolder; +import org.wso2.carbon.email.mgt.store.DBBasedTemplateManager; +import org.wso2.carbon.email.mgt.store.TemplatePersistenceManager; +import org.wso2.carbon.email.mgt.util.I18nEmailUtil; +import org.wso2.carbon.identity.governance.exceptions.notiification.NotificationTemplateManagerClientException; +import org.wso2.carbon.identity.governance.exceptions.notiification.NotificationTemplateManagerException; +import org.wso2.carbon.identity.governance.exceptions.notiification.NotificationTemplateManagerInternalException; +import org.wso2.carbon.identity.governance.exceptions.notiification.NotificationTemplateManagerServerException; +import org.wso2.carbon.identity.governance.model.NotificationTemplate; +import org.wso2.carbon.identity.governance.service.notification.NotificationChannels; +import org.wso2.carbon.identity.governance.service.notification.NotificationTemplateManager; +import org.wso2.carbon.identity.organization.management.application.OrgApplicationManager; +import org.wso2.carbon.identity.organization.management.service.OrganizationManager; +import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementException; +import org.wso2.carbon.identity.organization.management.service.util.OrganizationManagementUtil; + +import java.util.ArrayList; +import java.util.List; + +import static org.wso2.carbon.email.mgt.constants.TemplateMgtConstants.DEFAULT_EMAIL_NOTIFICATION_LOCALE; +import static org.wso2.carbon.email.mgt.constants.TemplateMgtConstants.DEFAULT_SMS_NOTIFICATION_LOCALE; +import static org.wso2.carbon.email.mgt.util.I18nEmailUtil.normalizeLocaleFormat; + +/** + * Provides functionality to manage notification templates. + */ +public class NotificationTemplateManagerImpl implements NotificationTemplateManager { + + private static final Log log = LogFactory.getLog(NotificationTemplateManagerImpl.class); + + private final TemplatePersistenceManager templatePersistenceManager; + + public NotificationTemplateManagerImpl() { + + templatePersistenceManager = new DBBasedTemplateManager(); + } + + /** + * {@inheritDoc} + */ + @Override + public List getAllNotificationTemplateTypes(String notificationChannel, String tenantDomain) + throws NotificationTemplateManagerException { + + try { + if (OrganizationManagementUtil.isOrganization(tenantDomain)) { + // Return the root organization's template types. + tenantDomain = getRootOrgTenantDomain(tenantDomain); + } + List templateTypes = templatePersistenceManager + .listNotificationTemplateTypes(notificationChannel, tenantDomain); + return templateTypes != null ? templateTypes : new ArrayList<>(); + } catch (NotificationTemplateManagerServerException ex) { + String errorMsg = String.format("Error when retrieving %s template types of %s tenant.", + notificationChannel.toLowerCase(), tenantDomain); + throw new NotificationTemplateManagerServerException(errorMsg, ex); + } catch (OrganizationManagementException e) { + throw new NotificationTemplateManagerServerException(e.getMessage(), e); + } + } + + /** + * {@inheritDoc} + */ + @Override + public void addNotificationTemplateType(String notificationChannel, String displayName, String tenantDomain) + throws NotificationTemplateManagerException { + + validateDisplayNameOfTemplateType(displayName); + try { + if (templatePersistenceManager + .isNotificationTemplateTypeExists(displayName, notificationChannel, tenantDomain)) { + // This error is caught in the catch block below to generate the + // NotificationTemplateManagerServerException. + throw new NotificationTemplateManagerInternalException( + TemplateMgtConstants.ErrorCodes.TEMPLATE_TYPE_ALREADY_EXISTS, StringUtils.EMPTY); + } + templatePersistenceManager.addNotificationTemplateType(displayName, notificationChannel, tenantDomain); + } catch (NotificationTemplateManagerServerException e) { + String code = I18nEmailUtil.prependOperationScenarioToErrorCode( + TemplateMgtConstants.ErrorMessages.ERROR_CODE_ERROR_ADDING_TEMPLATE.getCode(), + TemplateMgtConstants.ErrorScenarios.NOTIFICATION_TEMPLATE_MANAGER); + String message = String.format( + TemplateMgtConstants.ErrorMessages.ERROR_CODE_ERROR_ADDING_TEMPLATE.getMessage(), displayName, + tenantDomain); + throw new NotificationTemplateManagerException(code, message, e); + } catch (NotificationTemplateManagerInternalException e) { + if (TemplateMgtConstants.ErrorCodes.TEMPLATE_TYPE_ALREADY_EXISTS.equals(e.getErrorCode())) { + String code = I18nEmailUtil.prependOperationScenarioToErrorCode( + TemplateMgtConstants.ErrorMessages.ERROR_CODE_TEMPLATE_TYPE_ALREADY_EXISTS.getCode(), + TemplateMgtConstants.ErrorScenarios.NOTIFICATION_TEMPLATE_MANAGER); + String message = String.format( + TemplateMgtConstants.ErrorMessages.ERROR_CODE_TEMPLATE_TYPE_ALREADY_EXISTS.getMessage(), + displayName, tenantDomain); + throw new NotificationTemplateManagerServerException(code, message, e); + } + if (log.isDebugEnabled()) { + log.debug("Error when adding template type : " + displayName + " to tenant : " + tenantDomain, e); + } + } + } + + /** + * {@inheritDoc} + */ + @Override + public void deleteNotificationTemplateType(String notificationChannel, String templateDisplayName, + String tenantDomain) + throws NotificationTemplateManagerException { + + validateDisplayNameOfTemplateType(templateDisplayName); + try { + templatePersistenceManager.deleteNotificationTemplateType(templateDisplayName, notificationChannel, + tenantDomain); + } catch (NotificationTemplateManagerException ex) { + String errorMsg = String.format + ("Error deleting template type %s from %s tenant.", templateDisplayName, tenantDomain); + throw handleServerException(errorMsg, ex); + } + } + + /** + * {@inheritDoc} + */ + @Override + public boolean isNotificationTemplateTypeExists(String notificationChannel, String templateTypeDisplayName, + String tenantDomain) + throws NotificationTemplateManagerException { + + try { + return templatePersistenceManager.isNotificationTemplateTypeExists(templateTypeDisplayName, + notificationChannel, tenantDomain); + } catch (NotificationTemplateManagerServerException e) { + String error = String.format("Error when retrieving templates of %s tenant.", tenantDomain); + throw handleServerException(error, e); + } + } + + /** + * {@inheritDoc} + */ + @Override + public List getAllNotificationTemplates(String notificationChannel, String tenantDomain) + throws NotificationTemplateManagerException { + + try { + if (OrganizationManagementUtil.isOrganization(tenantDomain)) { + // Return the root organization's email templates. + tenantDomain = getRootOrgTenantDomain(tenantDomain); + } + return templatePersistenceManager.listAllNotificationTemplates(notificationChannel, tenantDomain); + } catch (NotificationTemplateManagerServerException e) { + String error = String.format("Error when retrieving templates of %s tenant.", tenantDomain); + throw handleServerException(error, e); + } catch (OrganizationManagementException e) { + throw handleServerException(e.getMessage(), e); + } + } + + /** + * {@inheritDoc} + */ + @Override + public List getNotificationTemplatesOfType(String notificationChannel, + String templateDisplayName, String tenantDomain) + throws NotificationTemplateManagerException { + return getNotificationTemplatesOfType(notificationChannel, templateDisplayName, tenantDomain, null); + } + + /** + * {@inheritDoc} + */ + @Override + public List getNotificationTemplatesOfType(String notificationChannel, + String templateDisplayName, String tenantDomain, String applicationUuid) + throws NotificationTemplateManagerException { + + try { + if (OrganizationManagementUtil.isOrganization(tenantDomain)) { + // Return the root organization's email templates. + tenantDomain = getRootOrgTenantDomain(tenantDomain); + } + assertTemplateTypeExists(templateDisplayName, notificationChannel, tenantDomain); + List notificationTemplates = templatePersistenceManager.listNotificationTemplates( + templateDisplayName, notificationChannel, applicationUuid, tenantDomain); + if (notificationTemplates == null) { + notificationTemplates = new ArrayList<>(); + } + return notificationTemplates; + } catch (NotificationTemplateManagerServerException e) { + String error = String.format("Error when retrieving email templates of %s tenant.", tenantDomain); + throw handleServerException(error, e); + } catch (OrganizationManagementException e) { + throw handleServerException(e.getMessage(), e); + } + } + /** + * {@inheritDoc} + */ + @Override + public NotificationTemplate getNotificationTemplate(String notificationChannel, String templateType, String locale, + String tenantDomain) + throws NotificationTemplateManagerException { + + return getNotificationTemplate(notificationChannel, templateType, locale, tenantDomain, null); + } + + /** + * {@inheritDoc} + */ + @Override + public NotificationTemplate getNotificationTemplate(String notificationChannel, String templateType, String locale, + String tenantDomain, String applicationUuid) + throws NotificationTemplateManagerException { + + try { + if (OrganizationManagementUtil.isOrganization(tenantDomain)) { + // Return the root organization's notification template. + tenantDomain = getRootOrgTenantDomain(tenantDomain); + // If it's application specific template is required, get the root organization's application. + if (StringUtils.isNotBlank(applicationUuid)) { + applicationUuid = getMainApplicationIdForGivenSharedApp(applicationUuid, tenantDomain); + } + } + } catch (OrganizationManagementException e) { + throw new NotificationTemplateManagerException(e.getMessage(), e); + } + validateTemplateLocale(locale); + locale = normalizeLocaleFormat(locale); + validateDisplayNameOfTemplateType(templateType); + assertTemplateTypeExists(templateType, notificationChannel, tenantDomain); + NotificationTemplate notificationTemplate = templatePersistenceManager.getNotificationTemplate(templateType, + locale, notificationChannel, applicationUuid, tenantDomain); + + String defaultLocale = getDefaultNotificationLocale(notificationChannel); + if (notificationTemplate == null) { + if (StringUtils.equalsIgnoreCase(DEFAULT_SMS_NOTIFICATION_LOCALE, defaultLocale)) { + // Template is not available in the default locale. Therefore, breaking the flow at the consuming side + // to avoid NPE. + String code = I18nEmailUtil.prependOperationScenarioToErrorCode( + TemplateMgtConstants.ErrorMessages.ERROR_CODE_TEMPLATE_NOT_FOUND.getCode(), + TemplateMgtConstants.ErrorScenarios.NOTIFICATION_TEMPLATE_MANAGER); + String errorMessage = String + .format(TemplateMgtConstants.ErrorMessages.ERROR_CODE_TEMPLATE_NOT_FOUND.getMessage(), + templateType, tenantDomain); + throw new NotificationTemplateManagerServerException(code, errorMessage); + } else { + if (log.isDebugEnabled()) { + String message = String + .format("'%s' template in '%s' locale was not found in '%s' tenant. Trying to return the " + + "template in default locale : '%s'", templateType, locale, tenantDomain, + defaultLocale); + log.debug(message); + } + // Try to get the template type in default locale. + return getNotificationTemplate(notificationChannel, templateType, defaultLocale, tenantDomain); + } + } + return notificationTemplate; + } + + /** + * {@inheritDoc} + */ + @Override + public void addNotificationTemplate(NotificationTemplate notificationTemplate, String tenantDomain) + throws NotificationTemplateManagerException { + + addNotificationTemplate(notificationTemplate, tenantDomain, null); + } + + /** + * {@inheritDoc} + */ + @Override + public void addNotificationTemplate(NotificationTemplate notificationTemplate, String tenantDomain, + String applicationUuid) throws NotificationTemplateManagerException { + + validateNotificationTemplate(notificationTemplate); + String displayName = notificationTemplate.getDisplayName(); + String locale = notificationTemplate.getLocale(); + String notificationChannel = notificationTemplate.getNotificationChannel(); + locale = normalizeLocaleFormat(locale); + if (notificationTemplate.getLocale() != null && !notificationTemplate.getLocale().equals(locale)) { + notificationTemplate.setLocale(locale); + } + assertTemplateTypeExists(displayName, notificationChannel, tenantDomain); + if (templatePersistenceManager.isNotificationTemplateExists(displayName, locale, notificationChannel, + applicationUuid, tenantDomain)) { + String code = I18nEmailUtil.prependOperationScenarioToErrorCode( + TemplateMgtConstants.ErrorMessages.ERROR_CODE_TEMPLATE_ALREADY_EXISTS.getCode(), + TemplateMgtConstants.ErrorScenarios.NOTIFICATION_TEMPLATE_MANAGER); + String message = String.format( + TemplateMgtConstants.ErrorMessages.ERROR_CODE_TEMPLATE_ALREADY_EXISTS.getMessage(), displayName, + tenantDomain); + throw new NotificationTemplateManagerServerException(code, message); + } + try { + templatePersistenceManager.addNotificationTemplate(notificationTemplate, applicationUuid, + tenantDomain); + } catch (NotificationTemplateManagerServerException e) { + String code = I18nEmailUtil.prependOperationScenarioToErrorCode( + TemplateMgtConstants.ErrorMessages.ERROR_CODE_ERROR_ADDING_TEMPLATE.getCode(), + TemplateMgtConstants.ErrorScenarios.NOTIFICATION_TEMPLATE_MANAGER); + String message = + String.format(TemplateMgtConstants.ErrorMessages.ERROR_CODE_ERROR_ADDING_TEMPLATE.getMessage(), + displayName, tenantDomain); + throw new NotificationTemplateManagerServerException(code, message, e); + } + } + + /** + * {@inheritDoc} + */ + @Override + public void updateNotificationTemplate(NotificationTemplate notificationTemplate, String tenantDomain) + throws NotificationTemplateManagerException { + + updateNotificationTemplate(notificationTemplate, tenantDomain, null); + } + + /** + * {@inheritDoc} + */ + @Override + public void updateNotificationTemplate(NotificationTemplate notificationTemplate, String tenantDomain, + String applicationUuid) throws NotificationTemplateManagerException { + + validateNotificationTemplate(notificationTemplate); + String displayName = notificationTemplate.getDisplayName(); + String locale = notificationTemplate.getLocale(); + String notificationChannel = notificationTemplate.getNotificationChannel(); + locale = normalizeLocaleFormat(locale); + if (notificationTemplate.getLocale() != null && !notificationTemplate.getLocale().equals(locale)) { + notificationTemplate.setLocale(locale); + } + assertTemplateTypeExists(displayName, notificationChannel, tenantDomain); + if (!templatePersistenceManager.isNotificationTemplateExists( + displayName, locale, notificationChannel, applicationUuid, tenantDomain)) { + String code = I18nEmailUtil.prependOperationScenarioToErrorCode( + TemplateMgtConstants.ErrorMessages.ERROR_CODE_TEMPLATE_NOT_FOUND.getCode(), + TemplateMgtConstants.ErrorScenarios.NOTIFICATION_TEMPLATE_MANAGER); + String message = String.format( + TemplateMgtConstants.ErrorMessages.ERROR_CODE_TEMPLATE_NOT_FOUND.getMessage(), displayName, + tenantDomain); + throw new NotificationTemplateManagerServerException(code, message); + } + try { + templatePersistenceManager.updateNotificationTemplate(notificationTemplate, applicationUuid, + tenantDomain); + } catch (NotificationTemplateManagerServerException e) { + String code = I18nEmailUtil.prependOperationScenarioToErrorCode( + TemplateMgtConstants.ErrorMessages.ERROR_CODE_ERROR_UPDATING_TEMPLATE.getCode(), + TemplateMgtConstants.ErrorScenarios.NOTIFICATION_TEMPLATE_MANAGER); + String message = + String.format(TemplateMgtConstants.ErrorMessages.ERROR_CODE_ERROR_UPDATING_TEMPLATE.getMessage(), + displayName, locale, tenantDomain); + throw new NotificationTemplateManagerServerException(code, message, e); + } + } + + /** + * {@inheritDoc} + */ + @Override + public void deleteNotificationTemplate(String notificationChannel, String templateDisplayName, String locale, + String tenantDomain) + throws NotificationTemplateManagerException { + + deleteNotificationTemplate(notificationChannel, templateDisplayName, locale, tenantDomain, null); + } + + /** + * {@inheritDoc} + */ + @Override + public void deleteNotificationTemplate(String notificationChannel, String templateDisplayName, String locale, + String tenantDomain, String applicationUuid) + throws NotificationTemplateManagerException { + + // Validate the name and locale code. + if (StringUtils.isBlank(templateDisplayName)) { + String errorCode = + I18nEmailUtil.prependOperationScenarioToErrorCode( + TemplateMgtConstants.ErrorMessages.ERROR_CODE_INVALID_TEMPLATE_DISPLAY_NAME.getCode(), + TemplateMgtConstants.ErrorScenarios.NOTIFICATION_TEMPLATE_MANAGER); + throw new NotificationTemplateManagerClientException( errorCode, + TemplateMgtConstants.ErrorMessages.ERROR_CODE_INVALID_TEMPLATE_DISPLAY_NAME.getMessage()); + } + if (StringUtils.isBlank(locale)) { + String errorCode = + I18nEmailUtil.prependOperationScenarioToErrorCode( + TemplateMgtConstants.ErrorMessages.ERROR_CODE_INVALID_LOCALE.getCode(), + TemplateMgtConstants.ErrorScenarios.NOTIFICATION_TEMPLATE_MANAGER); + throw new NotificationTemplateManagerClientException(errorCode, + TemplateMgtConstants.ErrorMessages.ERROR_CODE_INVALID_LOCALE.getMessage()); + } + locale = normalizeLocaleFormat(locale); + try { + templatePersistenceManager.deleteNotificationTemplate(templateDisplayName, locale, + notificationChannel, applicationUuid, tenantDomain); + } catch (NotificationTemplateManagerServerException ex) { + String msg = String.format("Error deleting %s:%s template from %s tenant registry.", templateDisplayName, + locale, tenantDomain); + throw handleServerException(msg, ex); + } + } + + /** + * {@inheritDoc} + */ + @Override + public void addDefaultNotificationTemplates(String notificationChannel, String tenantDomain) + throws NotificationTemplateManagerException { + + // Get the list of Default notification templates. + List notificationTemplates = + getDefaultNotificationTemplates(notificationChannel); + int numberOfAddedTemplates = 0; + try { + for (NotificationTemplate template : notificationTemplates) { + String displayName = template.getDisplayName(); + String locale = template.getLocale(); + + /*Check for existence of each category, since some template may have migrated from earlier version + This will also add new template types provided from file, but won't update any existing template*/ + if (!templatePersistenceManager.isNotificationTemplateExists(displayName, locale, notificationChannel, + null, tenantDomain)) { + try { + addNotificationTemplate(template, tenantDomain); + if (log.isDebugEnabled()) { + String msg = "Default template added to %s tenant registry : %n%s"; + log.debug(String.format(msg, tenantDomain, template)); + } + numberOfAddedTemplates++; + } catch (NotificationTemplateManagerInternalException e) { + log.warn("Template : " + displayName + "already exists in the registry. Hence " + + "ignoring addition"); + } + } + } + if (log.isDebugEnabled()) { + log.debug(String.format("Added %d default %s templates to the tenant registry : %s", + numberOfAddedTemplates, notificationChannel, tenantDomain)); + } + } catch (NotificationTemplateManagerServerException ex) { + String error = "Error when tried to check for default email templates in tenant registry : %s"; + log.error(String.format(error, tenantDomain), ex); + } + } + + /** + * {@inheritDoc} + */ + @Override + public List getDefaultNotificationTemplates(String notificationChannel) { + + if (NotificationChannels.SMS_CHANNEL.getChannelType().equals(notificationChannel)) { + return I18nMgtDataHolder.getInstance().getDefaultSMSTemplates(); + } + return I18nMgtDataHolder.getInstance().getDefaultEmailTemplates(); + } + + /** + * {@inheritDoc} + */ + @Override + public boolean isNotificationTemplateExists(String notificationChannel, String templateDisplayName, String locale, + String tenantDomain) + throws NotificationTemplateManagerException { + + return isNotificationTemplateExists(notificationChannel, templateDisplayName, locale, tenantDomain, null); + } + + /** + * {@inheritDoc} + */ + @Override + public boolean isNotificationTemplateExists(String notificationChannel, String templateDisplayName, String locale, + String tenantDomain, String applicationUuid) + throws NotificationTemplateManagerException { + + try { + locale = normalizeLocaleFormat(locale); + return templatePersistenceManager.isNotificationTemplateExists(templateDisplayName, locale, + notificationChannel, applicationUuid, tenantDomain); + } catch (NotificationTemplateManagerServerException e) { + String error = String.format("Error when retrieving notification templates of %s tenant.", tenantDomain); + throw new NotificationTemplateManagerServerException(error, e); + } + } + + private void validateDisplayNameOfTemplateType(String templateDisplayName) + throws NotificationTemplateManagerClientException { + + if (StringUtils.isBlank(templateDisplayName)) { + String errorCode = + I18nEmailUtil.prependOperationScenarioToErrorCode( + TemplateMgtConstants.ErrorMessages.ERROR_CODE_EMPTY_TEMPLATE_NAME.getCode(), + TemplateMgtConstants.ErrorScenarios.NOTIFICATION_TEMPLATE_MANAGER); + throw new NotificationTemplateManagerClientException(errorCode, + TemplateMgtConstants.ErrorMessages.ERROR_CODE_EMPTY_TEMPLATE_NAME.getMessage()); + } + } + + private String getRootOrgTenantDomain(String tenantDomain) throws OrganizationManagementException { + + OrganizationManager organizationManager = I18nMgtDataHolder.getInstance().getOrganizationManager(); + String orgId = organizationManager.resolveOrganizationId(tenantDomain); + String primaryOrgId = organizationManager.getPrimaryOrganizationId(orgId); + return organizationManager.resolveTenantDomain(primaryOrgId); + } + + private NotificationTemplateManagerServerException handleServerException(String errorMsg, Throwable ex) + throws NotificationTemplateManagerServerException { + + log.error(errorMsg); + return new NotificationTemplateManagerServerException(errorMsg, ex); + } + + private String getMainApplicationIdForGivenSharedApp(String applicationUuid, String tenantDomain) + throws OrganizationManagementException { + + OrganizationManager organizationManager = I18nMgtDataHolder.getInstance().getOrganizationManager(); + String sharedOrgId = organizationManager.resolveOrganizationId(tenantDomain); + OrgApplicationManager sharedAppManager = I18nMgtDataHolder.getInstance().getSharedAppManager(); + return sharedAppManager.getMainApplicationIdForGivenSharedApp(applicationUuid, sharedOrgId); + } + + private void validateTemplateLocale(String locale) throws NotificationTemplateManagerClientException { + + if (StringUtils.isBlank(locale)) { + String errorCode = + I18nEmailUtil.prependOperationScenarioToErrorCode( + TemplateMgtConstants.ErrorMessages.ERROR_CODE_EMPTY_LOCALE.getCode(), + TemplateMgtConstants.ErrorScenarios.NOTIFICATION_TEMPLATE_MANAGER); + throw new NotificationTemplateManagerClientException(errorCode, + TemplateMgtConstants.ErrorMessages.ERROR_CODE_EMPTY_LOCALE.getMessage()); + } + final String LOCAL_REGEX = "^[a-z]{2}_[A-Z]{2}$"; + if (!locale.matches(LOCAL_REGEX)) { + String errorCode = + I18nEmailUtil.prependOperationScenarioToErrorCode( + TemplateMgtConstants.ErrorMessages.ERROR_CODE_INVALID_LOCALE.getCode(), + TemplateMgtConstants.ErrorScenarios.NOTIFICATION_TEMPLATE_MANAGER); + throw new NotificationTemplateManagerClientException(errorCode, + TemplateMgtConstants.ErrorMessages.ERROR_CODE_INVALID_LOCALE.getMessage()); + } + } + + private void validateNotificationTemplate(NotificationTemplate notificationTemplate) + throws NotificationTemplateManagerClientException { + + if (notificationTemplate == null) { + String errorCode = + I18nEmailUtil.prependOperationScenarioToErrorCode( + TemplateMgtConstants.ErrorMessages.ERROR_CODE_NULL_TEMPLATE_OBJECT.getCode(), + TemplateMgtConstants.ErrorScenarios.NOTIFICATION_TEMPLATE_MANAGER); + throw new NotificationTemplateManagerClientException(errorCode, + TemplateMgtConstants.ErrorMessages.ERROR_CODE_NULL_TEMPLATE_OBJECT.getMessage()); + } + String displayName = notificationTemplate.getDisplayName(); + validateDisplayNameOfTemplateType(displayName); + String normalizedDisplayName = I18nEmailUtil.getNormalizedName(displayName); + if (!StringUtils.equalsIgnoreCase(normalizedDisplayName, notificationTemplate.getType())) { + if (log.isDebugEnabled()) { + String message = String.format("In the template normalizedDisplayName : %s is not equal to the " + + "template type : %s. Therefore template type is sent to : %s", normalizedDisplayName, + notificationTemplate.getType(), normalizedDisplayName); + log.debug(message); + } + notificationTemplate.setType(normalizedDisplayName); + } + validateTemplateLocale(notificationTemplate.getLocale()); + String body = notificationTemplate.getBody(); + String subject = notificationTemplate.getSubject(); + String footer = notificationTemplate.getFooter(); + if (StringUtils.isBlank(notificationTemplate.getNotificationChannel())) { + String errorCode = + I18nEmailUtil.prependOperationScenarioToErrorCode( + TemplateMgtConstants.ErrorMessages.ERROR_CODE_EMPTY_TEMPLATE_CHANNEL.getCode(), + TemplateMgtConstants.ErrorScenarios.NOTIFICATION_TEMPLATE_MANAGER); + throw new NotificationTemplateManagerClientException(errorCode, + TemplateMgtConstants.ErrorMessages.ERROR_CODE_EMPTY_TEMPLATE_CHANNEL.getMessage()); + } + if (NotificationChannels.SMS_CHANNEL.getChannelType() + .equalsIgnoreCase(notificationTemplate.getNotificationChannel())) { + if (StringUtils.isBlank(body)) { + String errorCode = + I18nEmailUtil.prependOperationScenarioToErrorCode( + TemplateMgtConstants.ErrorMessages.ERROR_CODE_INVALID_SMS_TEMPLATE.getCode(), + TemplateMgtConstants.ErrorScenarios.NOTIFICATION_TEMPLATE_MANAGER); + throw new NotificationTemplateManagerClientException(errorCode, + TemplateMgtConstants.ErrorMessages.ERROR_CODE_INVALID_SMS_TEMPLATE.getMessage()); + } + if (StringUtils.isNotBlank(subject) || StringUtils.isNotBlank(footer)) { + String errorCode = + I18nEmailUtil.prependOperationScenarioToErrorCode( + TemplateMgtConstants.ErrorMessages.ERROR_CODE_INVALID_SMS_TEMPLATE_CONTENT.getCode(), + TemplateMgtConstants.ErrorScenarios.NOTIFICATION_TEMPLATE_MANAGER); + throw new NotificationTemplateManagerClientException(errorCode, + TemplateMgtConstants.ErrorMessages.ERROR_CODE_INVALID_SMS_TEMPLATE_CONTENT.getMessage()); + } + } else { + if (StringUtils.isBlank(subject) || StringUtils.isBlank(body)) { + String errorCode = + I18nEmailUtil.prependOperationScenarioToErrorCode( + TemplateMgtConstants.ErrorMessages.ERROR_CODE_INVALID_EMAIL_TEMPLATE.getCode(), + TemplateMgtConstants.ErrorScenarios.NOTIFICATION_TEMPLATE_MANAGER); + throw new NotificationTemplateManagerClientException(errorCode, + TemplateMgtConstants.ErrorMessages.ERROR_CODE_INVALID_EMAIL_TEMPLATE.getMessage()); + } + } + } + + private String getDefaultNotificationLocale(String notificationChannel) { + + if (NotificationChannels.SMS_CHANNEL.getChannelType().equals(notificationChannel)) { + return DEFAULT_SMS_NOTIFICATION_LOCALE; + } else { + return DEFAULT_EMAIL_NOTIFICATION_LOCALE; + } + } + + private void assertTemplateTypeExists(String templateType, String notificationChannel, String tenantDomain) + throws NotificationTemplateManagerServerException { + + if (!templatePersistenceManager.isNotificationTemplateTypeExists(templateType, notificationChannel, + tenantDomain)) { + String code = I18nEmailUtil.prependOperationScenarioToErrorCode( + TemplateMgtConstants.ErrorMessages.ERROR_CODE_TEMPLATE_TYPE_NOT_FOUND.getCode(), + TemplateMgtConstants.ErrorScenarios.NOTIFICATION_TEMPLATE_MANAGER); + String message = String.format( + TemplateMgtConstants.ErrorMessages.ERROR_CODE_TEMPLATE_TYPE_NOT_FOUND.getMessage(), + templateType, tenantDomain); + throw new NotificationTemplateManagerServerException(code, message); + } + } +} diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/TemplateMgtConstants.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/TemplateMgtConstants.java new file mode 100644 index 00000000..b48f7f47 --- /dev/null +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/TemplateMgtConstants.java @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.email.mgt.constants; + +public class TemplateMgtConstants { + + private TemplateMgtConstants() {} + + public static final String DEFAULT_EMAIL_NOTIFICATION_LOCALE = "en_us"; + public static final String DEFAULT_SMS_NOTIFICATION_LOCALE = "en_us"; + + /** + * Class which contains the error scenarios. + */ + public static class ErrorScenarios { + + // NTM - NOTIFICATION TEMPLATE MANAGER + public static final String NOTIFICATION_TEMPLATE_MANAGER = "NTM"; + } + + /** + * Class which contains the error codes for template management. + */ + public static class ErrorCodes { + public static final String TEMPLATE_TYPE_ALREADY_EXISTS = "65001"; + public static final String TEMPLATE_TYPE_NOT_FOUND = "65002"; + public static final String TEMPLATE_ALREADY_EXISTS = "65003"; + public static final String TEMPLATE_NOT_FOUND = "65004"; + public static final String ERROR_ADDING_TEMPLATE = "65005"; + public static final String ERROR_UPDATING_TEMPLATE = "65006"; + } + + /** + * Enum which contains error codes and corresponding error messages. + */ + public enum ErrorMessages { + + ERROR_CODE_EMPTY_TEMPLATE_NAME("60001", "Notification template name cannot be empty."), + ERROR_CODE_EMPTY_LOCALE("60002", "Locale code cannot be empty"), + ERROR_CODE_INVALID_LOCALE("60003", "Locale code is invalid."), + ERROR_CODE_EMPTY_TEMPLATE_CHANNEL("60004", "Notification template channel cannot be empty"), + ERROR_CODE_INVALID_SMS_TEMPLATE("60005", "Body of a SMS template cannot be empty."), + ERROR_CODE_INVALID_SMS_TEMPLATE_CONTENT("60006", "SMS template cannot have a subject or footer"), + ERROR_CODE_INVALID_EMAIL_TEMPLATE("60007", "Subject/Body/Footer sections of an email template " + + "cannot be empty."), + ERROR_CODE_INVALID_TEMPLATE_DISPLAY_NAME("60008", "Invalid template display name."), + ERROR_CODE_NULL_TEMPLATE_OBJECT("60009", "Notification template is not provided."), + ERROR_CODE_TEMPLATE_TYPE_ALREADY_EXISTS(ErrorCodes.TEMPLATE_TYPE_ALREADY_EXISTS, + "Notification template type : %s already exists in tenant : %s"), + ERROR_CODE_TEMPLATE_TYPE_NOT_FOUND(ErrorCodes.TEMPLATE_TYPE_NOT_FOUND, "Notification template type :" + + " %s doesn't exist in tenant : %s"), + ERROR_CODE_TEMPLATE_ALREADY_EXISTS(ErrorCodes.TEMPLATE_ALREADY_EXISTS, "Notification template : %s" + + " already exists in tenant : %s"), + ERROR_CODE_TEMPLATE_NOT_FOUND(ErrorCodes.TEMPLATE_NOT_FOUND, "Notification template : %s " + + "doesn't exist in tenant : %s"), + ERROR_CODE_ERROR_ADDING_TEMPLATE(ErrorCodes.ERROR_ADDING_TEMPLATE, "Error when adding template : %s" + + " to tenant : %s"), + ERROR_CODE_ERROR_UPDATING_TEMPLATE(ErrorCodes.ERROR_UPDATING_TEMPLATE, "Error when updating " + + "template : %s on tenant : %s"); + + private final String code; + private final String message; + + ErrorMessages(String code, String message) { + + this.code = code; + this.message = message; + } + + public String getCode() { + + return code; + } + + public String getMessage() { + + return message; + } + + @Override + public String toString() { + + return code + " - " + message; + } + } +} diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/internal/I18nMgtServiceComponent.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/internal/I18nMgtServiceComponent.java index 9946ba51..d837f261 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/internal/I18nMgtServiceComponent.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/internal/I18nMgtServiceComponent.java @@ -28,6 +28,7 @@ import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.email.mgt.EmailTemplateManager; import org.wso2.carbon.email.mgt.EmailTemplateManagerImpl; +import org.wso2.carbon.email.mgt.NotificationTemplateManagerImpl; import org.wso2.carbon.email.mgt.SMSProviderPayloadTemplateManager; import org.wso2.carbon.email.mgt.SMSProviderPayloadTemplateManagerImpl; import org.wso2.carbon.email.mgt.constants.I18nMgtConstants; @@ -60,6 +61,7 @@ import java.nio.file.Path; import java.util.ArrayList; import java.util.HashMap; +import java.util.Hashtable; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -99,6 +101,20 @@ protected void activate(ComponentContext context) { log.error("Error registering Email Template Mgt Service."); } + // Register Notification Template Mgt Service as an OSGi service. + NotificationTemplateManagerImpl notificationTemplateManager = new NotificationTemplateManagerImpl(); + Hashtable serviceProperties = new Hashtable<>(); + serviceProperties.put("service.name", "NotificationTemplateManager"); + ServiceRegistration notificationTemplateSR = bundleCtx.registerService( + NotificationTemplateManager.class.getName(), notificationTemplateManager, serviceProperties); + if (notificationTemplateSR != null) { + if (log.isDebugEnabled()) { + log.debug("Notification Template Mgt Service registered."); + } + } else { + log.error("Error registering Notification Template Mgt Service."); + } + // Register EmailTemplateManagerImpl. ServiceRegistration notificationManagerSR = bundleCtx .registerService(NotificationTemplateManager.class.getName(), emailTemplateManager, null); diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/DBBasedTemplateManager.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/DBBasedTemplateManager.java index df6eaa65..b7cb8479 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/DBBasedTemplateManager.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/DBBasedTemplateManager.java @@ -21,6 +21,7 @@ import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.email.mgt.constants.TemplateMgtConstants; import org.wso2.carbon.email.mgt.internal.I18nMgtDataHolder; import org.wso2.carbon.email.mgt.store.dao.AppNotificationTemplateDAO; import org.wso2.carbon.email.mgt.store.dao.cache.CacheBackedAppNotificationTemplateDAO; @@ -164,6 +165,60 @@ public void addOrUpdateNotificationTemplate(NotificationTemplate notificationTem } } + @Override + public void addNotificationTemplate(NotificationTemplate notificationTemplate, String applicationUuid, + String tenantDomain) throws NotificationTemplateManagerServerException { + + String displayName = notificationTemplate.getDisplayName(); + String notificationChannel = notificationTemplate.getNotificationChannel(); + String locale = notificationTemplate.getLocale(); + int tenantId = getTenantId(tenantDomain); + + if (StringUtils.isBlank(applicationUuid)) { + orgNotificationTemplateDAO.addNotificationTemplate(notificationTemplate, tenantId); + if (log.isDebugEnabled()) { + log.debug(String.format( + "Org %s template with locale: %s for type: %s for tenant: %s successfully added.", + notificationChannel, locale, displayName, tenantDomain)); + } + } else { + appNotificationTemplateDAO.addNotificationTemplate(notificationTemplate, applicationUuid, tenantId); + if (log.isDebugEnabled()) { + log.debug(String.format( + "App %s template with locale: %s for type: %s for application: %s for tenant: %s " + + "successfully added.", + notificationChannel, locale, displayName, applicationUuid, tenantDomain)); + } + } + } + + @Override + public void updateNotificationTemplate(NotificationTemplate notificationTemplate, String applicationUuid, + String tenantDomain) throws NotificationTemplateManagerServerException { + + String displayName = notificationTemplate.getDisplayName(); + String notificationChannel = notificationTemplate.getNotificationChannel(); + String locale = notificationTemplate.getLocale(); + int tenantId = getTenantId(tenantDomain); + + if (StringUtils.isBlank(applicationUuid)) { + orgNotificationTemplateDAO.updateNotificationTemplate(notificationTemplate, tenantId); + if (log.isDebugEnabled()) { + log.debug(String.format( + "Org %s template with locale: %s for type: %s for tenant: %s successfully added.", + notificationChannel, locale, displayName, tenantDomain)); + } + } else { + appNotificationTemplateDAO.updateNotificationTemplate(notificationTemplate, applicationUuid, tenantId); + if (log.isDebugEnabled()) { + log.debug(String.format( + "App %s template with locale: %s for type: %s for application: %s for tenant: %s " + + "successfully added.", + notificationChannel, locale, displayName, applicationUuid, tenantDomain)); + } + } + } + @Override public boolean isNotificationTemplateExists(String displayName, String locale, String notificationChannel, String applicationUuid, String tenantDomain) diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/TemplatePersistenceManager.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/TemplatePersistenceManager.java index 69321c11..decfbfdc 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/TemplatePersistenceManager.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/TemplatePersistenceManager.java @@ -84,6 +84,32 @@ void deleteNotificationTemplateType(String displayName, String notificationChann void addOrUpdateNotificationTemplate(NotificationTemplate notificationTemplate, String applicationUuid, String tenantDomain) throws NotificationTemplateManagerServerException; + /** + * Add a new template if not exists. Only to be used with DB only use cases. + * + * @param notificationTemplate Notification template. + * @param applicationUuid Application UUID. + * @param tenantDomain Tenant domain. + * @throws NotificationTemplateManagerServerException If an error occurred while adding the template. + */ + default void addNotificationTemplate(NotificationTemplate notificationTemplate, String applicationUuid, + String tenantDomain) throws NotificationTemplateManagerServerException { + // not implemented + } + + /** + * Update a template if exists. Only to be used with DB only use cases. + * + * @param notificationTemplate Notification template. + * @param applicationUuid Application UUID. + * @param tenantDomain Tenant domain. + * @throws NotificationTemplateManagerServerException If an error occurred while updating the template. + */ + default void updateNotificationTemplate(NotificationTemplate notificationTemplate, String applicationUuid, + String tenantDomain) throws NotificationTemplateManagerServerException { + // not implemented + } + /** * Check whether the specified notification template exists. * From 3e91c124d7c816b56ae51bd5443d8b5948fb210c Mon Sep 17 00:00:00 2001 From: rushannanayakkara Date: Tue, 24 Sep 2024 14:48:28 +0530 Subject: [PATCH 02/13] Add Notification Template API SMS template functions --- .../mgt/NotificationTemplateManagerImpl.java | 11 ++-- .../mgt/store/DBBasedTemplateManager.java | 55 ----------------- .../mgt/store/TemplatePersistenceManager.java | 26 -------- .../TemplatePersistenceManagerFactory.java | 59 ++++++++++++++++++- 4 files changed, 64 insertions(+), 87 deletions(-) diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java index 6733a577..cc5c1af9 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java @@ -23,8 +23,8 @@ import org.apache.commons.logging.LogFactory; import org.wso2.carbon.email.mgt.constants.TemplateMgtConstants; import org.wso2.carbon.email.mgt.internal.I18nMgtDataHolder; -import org.wso2.carbon.email.mgt.store.DBBasedTemplateManager; import org.wso2.carbon.email.mgt.store.TemplatePersistenceManager; +import org.wso2.carbon.email.mgt.store.TemplatePersistenceManagerFactory; import org.wso2.carbon.email.mgt.util.I18nEmailUtil; import org.wso2.carbon.identity.governance.exceptions.notiification.NotificationTemplateManagerClientException; import org.wso2.carbon.identity.governance.exceptions.notiification.NotificationTemplateManagerException; @@ -56,7 +56,8 @@ public class NotificationTemplateManagerImpl implements NotificationTemplateMana public NotificationTemplateManagerImpl() { - templatePersistenceManager = new DBBasedTemplateManager(); + TemplatePersistenceManagerFactory templatePersistenceManagerFactory = new TemplatePersistenceManagerFactory(); + this.templatePersistenceManager = templatePersistenceManagerFactory.getUserDefinedTemplatePersistenceManager(); } /** @@ -320,7 +321,7 @@ public void addNotificationTemplate(NotificationTemplate notificationTemplate, S throw new NotificationTemplateManagerServerException(code, message); } try { - templatePersistenceManager.addNotificationTemplate(notificationTemplate, applicationUuid, + templatePersistenceManager.addOrUpdateNotificationTemplate(notificationTemplate, applicationUuid, tenantDomain); } catch (NotificationTemplateManagerServerException e) { String code = I18nEmailUtil.prependOperationScenarioToErrorCode( @@ -370,7 +371,7 @@ public void updateNotificationTemplate(NotificationTemplate notificationTemplate throw new NotificationTemplateManagerServerException(code, message); } try { - templatePersistenceManager.updateNotificationTemplate(notificationTemplate, applicationUuid, + templatePersistenceManager.addOrUpdateNotificationTemplate(notificationTemplate, applicationUuid, tenantDomain); } catch (NotificationTemplateManagerServerException e) { String code = I18nEmailUtil.prependOperationScenarioToErrorCode( @@ -431,7 +432,7 @@ public void deleteNotificationTemplate(String notificationChannel, String templa } /** - * {@inheritDoc} + * {@inheritDoc} todo : remove this function. */ @Override public void addDefaultNotificationTemplates(String notificationChannel, String tenantDomain) diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/DBBasedTemplateManager.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/DBBasedTemplateManager.java index b7cb8479..df6eaa65 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/DBBasedTemplateManager.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/DBBasedTemplateManager.java @@ -21,7 +21,6 @@ import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.email.mgt.constants.TemplateMgtConstants; import org.wso2.carbon.email.mgt.internal.I18nMgtDataHolder; import org.wso2.carbon.email.mgt.store.dao.AppNotificationTemplateDAO; import org.wso2.carbon.email.mgt.store.dao.cache.CacheBackedAppNotificationTemplateDAO; @@ -165,60 +164,6 @@ public void addOrUpdateNotificationTemplate(NotificationTemplate notificationTem } } - @Override - public void addNotificationTemplate(NotificationTemplate notificationTemplate, String applicationUuid, - String tenantDomain) throws NotificationTemplateManagerServerException { - - String displayName = notificationTemplate.getDisplayName(); - String notificationChannel = notificationTemplate.getNotificationChannel(); - String locale = notificationTemplate.getLocale(); - int tenantId = getTenantId(tenantDomain); - - if (StringUtils.isBlank(applicationUuid)) { - orgNotificationTemplateDAO.addNotificationTemplate(notificationTemplate, tenantId); - if (log.isDebugEnabled()) { - log.debug(String.format( - "Org %s template with locale: %s for type: %s for tenant: %s successfully added.", - notificationChannel, locale, displayName, tenantDomain)); - } - } else { - appNotificationTemplateDAO.addNotificationTemplate(notificationTemplate, applicationUuid, tenantId); - if (log.isDebugEnabled()) { - log.debug(String.format( - "App %s template with locale: %s for type: %s for application: %s for tenant: %s " + - "successfully added.", - notificationChannel, locale, displayName, applicationUuid, tenantDomain)); - } - } - } - - @Override - public void updateNotificationTemplate(NotificationTemplate notificationTemplate, String applicationUuid, - String tenantDomain) throws NotificationTemplateManagerServerException { - - String displayName = notificationTemplate.getDisplayName(); - String notificationChannel = notificationTemplate.getNotificationChannel(); - String locale = notificationTemplate.getLocale(); - int tenantId = getTenantId(tenantDomain); - - if (StringUtils.isBlank(applicationUuid)) { - orgNotificationTemplateDAO.updateNotificationTemplate(notificationTemplate, tenantId); - if (log.isDebugEnabled()) { - log.debug(String.format( - "Org %s template with locale: %s for type: %s for tenant: %s successfully added.", - notificationChannel, locale, displayName, tenantDomain)); - } - } else { - appNotificationTemplateDAO.updateNotificationTemplate(notificationTemplate, applicationUuid, tenantId); - if (log.isDebugEnabled()) { - log.debug(String.format( - "App %s template with locale: %s for type: %s for application: %s for tenant: %s " + - "successfully added.", - notificationChannel, locale, displayName, applicationUuid, tenantDomain)); - } - } - } - @Override public boolean isNotificationTemplateExists(String displayName, String locale, String notificationChannel, String applicationUuid, String tenantDomain) diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/TemplatePersistenceManager.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/TemplatePersistenceManager.java index decfbfdc..69321c11 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/TemplatePersistenceManager.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/TemplatePersistenceManager.java @@ -84,32 +84,6 @@ void deleteNotificationTemplateType(String displayName, String notificationChann void addOrUpdateNotificationTemplate(NotificationTemplate notificationTemplate, String applicationUuid, String tenantDomain) throws NotificationTemplateManagerServerException; - /** - * Add a new template if not exists. Only to be used with DB only use cases. - * - * @param notificationTemplate Notification template. - * @param applicationUuid Application UUID. - * @param tenantDomain Tenant domain. - * @throws NotificationTemplateManagerServerException If an error occurred while adding the template. - */ - default void addNotificationTemplate(NotificationTemplate notificationTemplate, String applicationUuid, - String tenantDomain) throws NotificationTemplateManagerServerException { - // not implemented - } - - /** - * Update a template if exists. Only to be used with DB only use cases. - * - * @param notificationTemplate Notification template. - * @param applicationUuid Application UUID. - * @param tenantDomain Tenant domain. - * @throws NotificationTemplateManagerServerException If an error occurred while updating the template. - */ - default void updateNotificationTemplate(NotificationTemplate notificationTemplate, String applicationUuid, - String tenantDomain) throws NotificationTemplateManagerServerException { - // not implemented - } - /** * Check whether the specified notification template exists. * diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/TemplatePersistenceManagerFactory.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/TemplatePersistenceManagerFactory.java index 022e4274..00e919cc 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/TemplatePersistenceManagerFactory.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/TemplatePersistenceManagerFactory.java @@ -33,7 +33,8 @@ public class TemplatePersistenceManagerFactory { private static final Log log = LogFactory.getLog(TemplatePersistenceManagerFactory.class); /** - * Returns a {@link TemplatePersistenceManager} implementation based on the configuration. + * Returns a {@link TemplatePersistenceManager} implementation based on the configuration that handles both system + * templates and user defined templates. * * If the storage type is configured as database, an instance of {@link DBBasedTemplateManager} will be returned. * If the storage type is configured as hybrid, an instance of {@link HybridTemplateManager} will be returned. @@ -66,4 +67,60 @@ public TemplatePersistenceManager getTemplatePersistenceManager() { } return new UnifiedTemplateManager(persistenceManager); } + + /** + * Returns a {@link TemplatePersistenceManager} implementation based on the configuration that handles only user + * defined templates. + * + * If the storage type is configured as database, an instance of {@link DBBasedTemplateManager} will be returned. + * If the storage type is configured as hybrid, an instance of {@link HybridTemplateManager} will be returned. + * If the storage type is configured as registry, an instance of {@link RegistryBasedTemplateManager} will be returned. + * For any other case, an instance of {@link DBBasedTemplateManager} will be returned. + * + * @return an implementation of {@link TemplatePersistenceManager}. + */ + public TemplatePersistenceManager getUserDefinedTemplatePersistenceManager() { + + String notificationTemplatesStorageType = IdentityUtil.getProperty(NOTIFICATION_TEMPLATES_STORAGE_CONFIG); + + TemplatePersistenceManager persistenceManager = new DBBasedTemplateManager(); + + if (StringUtils.isNotBlank(notificationTemplatesStorageType)) { + switch (notificationTemplatesStorageType) { + case "hybrid": + persistenceManager = new HybridTemplateManager(); + log.info("Hybrid template persistent manager initialized."); + break; + case "registry": + persistenceManager = new RegistryBasedTemplateManager(); + log.warn("Registry based template persistent manager initialized."); + break; + } + } + + if (log.isDebugEnabled()) { + log.debug("Template persistent manager initialized with the type: " + persistenceManager.getClass()); + } + return persistenceManager; + } + + /** + * Returns a {@link TemplatePersistenceManager} implementation based on the configuration that handles only + * system templates. + * + * If the storage type is configured as database, an instance of {@link DBBasedTemplateManager} will be returned. + * If the storage type is configured as hybrid, an instance of {@link HybridTemplateManager} will be returned. + * If the storage type is configured as registry, an instance of {@link RegistryBasedTemplateManager} will be returned. + * For any other case, an instance of {@link DBBasedTemplateManager} will be returned. + * + * @return an implementation of {@link TemplatePersistenceManager}. + */ + public TemplatePersistenceManager getSystemTemplatePersistenceManager() { + + SystemDefaultTemplateManager inMemoryTemplateManager = new SystemDefaultTemplateManager(); + if (log.isDebugEnabled()) { + log.debug("Template persistent manager initialized with the type: " + inMemoryTemplateManager.getClass()); + } + return inMemoryTemplateManager; + } } From ffd5c3d4318dc05620a68aaed7ca008c61e4ec56 Mon Sep 17 00:00:00 2001 From: rushannanayakkara Date: Thu, 26 Sep 2024 22:42:57 +0530 Subject: [PATCH 03/13] Add system notification template related functions. --- .../mgt/NotificationTemplateManagerImpl.java | 164 ++++++++++-------- .../mgt/constants/TemplateMgtConstants.java | 9 +- pom.xml | 2 +- 3 files changed, 100 insertions(+), 75 deletions(-) diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java index cc5c1af9..ff32de9b 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java @@ -52,12 +52,17 @@ public class NotificationTemplateManagerImpl implements NotificationTemplateMana private static final Log log = LogFactory.getLog(NotificationTemplateManagerImpl.class); - private final TemplatePersistenceManager templatePersistenceManager; + private final TemplatePersistenceManager userDefinedTemplatePersistenceManager; + private final TemplatePersistenceManager systemTemplatePersistenceManager; + private final TemplatePersistenceManager unifiedTemplatePersistenceManager; public NotificationTemplateManagerImpl() { TemplatePersistenceManagerFactory templatePersistenceManagerFactory = new TemplatePersistenceManagerFactory(); - this.templatePersistenceManager = templatePersistenceManagerFactory.getUserDefinedTemplatePersistenceManager(); + this.userDefinedTemplatePersistenceManager = + templatePersistenceManagerFactory.getUserDefinedTemplatePersistenceManager(); + this.systemTemplatePersistenceManager = templatePersistenceManagerFactory.getSystemTemplatePersistenceManager(); + this.unifiedTemplatePersistenceManager = templatePersistenceManagerFactory.getTemplatePersistenceManager(); } /** @@ -72,7 +77,7 @@ public List getAllNotificationTemplateTypes(String notificationChannel, // Return the root organization's template types. tenantDomain = getRootOrgTenantDomain(tenantDomain); } - List templateTypes = templatePersistenceManager + List templateTypes = unifiedTemplatePersistenceManager .listNotificationTemplateTypes(notificationChannel, tenantDomain); return templateTypes != null ? templateTypes : new ArrayList<>(); } catch (NotificationTemplateManagerServerException ex) { @@ -93,14 +98,14 @@ public void addNotificationTemplateType(String notificationChannel, String displ validateDisplayNameOfTemplateType(displayName); try { - if (templatePersistenceManager + if (userDefinedTemplatePersistenceManager .isNotificationTemplateTypeExists(displayName, notificationChannel, tenantDomain)) { // This error is caught in the catch block below to generate the // NotificationTemplateManagerServerException. throw new NotificationTemplateManagerInternalException( TemplateMgtConstants.ErrorCodes.TEMPLATE_TYPE_ALREADY_EXISTS, StringUtils.EMPTY); } - templatePersistenceManager.addNotificationTemplateType(displayName, notificationChannel, tenantDomain); + userDefinedTemplatePersistenceManager.addNotificationTemplateType(displayName, notificationChannel, tenantDomain); } catch (NotificationTemplateManagerServerException e) { String code = I18nEmailUtil.prependOperationScenarioToErrorCode( TemplateMgtConstants.ErrorMessages.ERROR_CODE_ERROR_ADDING_TEMPLATE.getCode(), @@ -134,8 +139,18 @@ public void deleteNotificationTemplateType(String notificationChannel, String te throws NotificationTemplateManagerException { validateDisplayNameOfTemplateType(templateDisplayName); + if (systemTemplatePersistenceManager.isNotificationTemplateTypeExists(templateDisplayName, notificationChannel, + null)) { + String code = I18nEmailUtil.prependOperationScenarioToErrorCode( + TemplateMgtConstants.ErrorMessages.ERROR_CODE_SYSTEM_RESOURCE_DELETION_NOT_ALLOWED.getCode(), + TemplateMgtConstants.ErrorScenarios.NOTIFICATION_TEMPLATE_MANAGER); + String message = String.format( + TemplateMgtConstants.ErrorMessages.ERROR_CODE_SYSTEM_RESOURCE_DELETION_NOT_ALLOWED.getMessage(), + "System notification template types are not eligible for deletion."); + throw new NotificationTemplateManagerServerException(code, message); + } try { - templatePersistenceManager.deleteNotificationTemplateType(templateDisplayName, notificationChannel, + userDefinedTemplatePersistenceManager.deleteNotificationTemplateType(templateDisplayName, notificationChannel, tenantDomain); } catch (NotificationTemplateManagerException ex) { String errorMsg = String.format @@ -153,7 +168,7 @@ public boolean isNotificationTemplateTypeExists(String notificationChannel, Stri throws NotificationTemplateManagerException { try { - return templatePersistenceManager.isNotificationTemplateTypeExists(templateTypeDisplayName, + return unifiedTemplatePersistenceManager.isNotificationTemplateTypeExists(templateTypeDisplayName, notificationChannel, tenantDomain); } catch (NotificationTemplateManagerServerException e) { String error = String.format("Error when retrieving templates of %s tenant.", tenantDomain); @@ -173,7 +188,7 @@ public List getAllNotificationTemplates(String notificatio // Return the root organization's email templates. tenantDomain = getRootOrgTenantDomain(tenantDomain); } - return templatePersistenceManager.listAllNotificationTemplates(notificationChannel, tenantDomain); + return userDefinedTemplatePersistenceManager.listAllNotificationTemplates(notificationChannel, tenantDomain); } catch (NotificationTemplateManagerServerException e) { String error = String.format("Error when retrieving templates of %s tenant.", tenantDomain); throw handleServerException(error, e); @@ -206,7 +221,7 @@ public List getNotificationTemplatesOfType(String notifica tenantDomain = getRootOrgTenantDomain(tenantDomain); } assertTemplateTypeExists(templateDisplayName, notificationChannel, tenantDomain); - List notificationTemplates = templatePersistenceManager.listNotificationTemplates( + List notificationTemplates = userDefinedTemplatePersistenceManager.listNotificationTemplates( templateDisplayName, notificationChannel, applicationUuid, tenantDomain); if (notificationTemplates == null) { notificationTemplates = new ArrayList<>(); @@ -254,12 +269,12 @@ public NotificationTemplate getNotificationTemplate(String notificationChannel, locale = normalizeLocaleFormat(locale); validateDisplayNameOfTemplateType(templateType); assertTemplateTypeExists(templateType, notificationChannel, tenantDomain); - NotificationTemplate notificationTemplate = templatePersistenceManager.getNotificationTemplate(templateType, + NotificationTemplate notificationTemplate = userDefinedTemplatePersistenceManager.getNotificationTemplate(templateType, locale, notificationChannel, applicationUuid, tenantDomain); String defaultLocale = getDefaultNotificationLocale(notificationChannel); if (notificationTemplate == null) { - if (StringUtils.equalsIgnoreCase(DEFAULT_SMS_NOTIFICATION_LOCALE, defaultLocale)) { + if (StringUtils.equalsIgnoreCase(locale, defaultLocale)) { // Template is not available in the default locale. Therefore, breaking the flow at the consuming side // to avoid NPE. String code = I18nEmailUtil.prependOperationScenarioToErrorCode( @@ -284,6 +299,49 @@ public NotificationTemplate getNotificationTemplate(String notificationChannel, return notificationTemplate; } + /** + * {@inheritDoc} + */ + @Override + public NotificationTemplate getSystemNotificationTemplate(String notificationChannel, String templateType, + String locale) + throws NotificationTemplateManagerException { + + validateTemplateLocale(locale); + locale = normalizeLocaleFormat(locale); + validateDisplayNameOfTemplateType(templateType); + assertSystemTemplateTypeExists(templateType, notificationChannel); + NotificationTemplate notificationTemplate = + systemTemplatePersistenceManager.getNotificationTemplate(templateType,locale, notificationChannel, + null, null); + + String defaultLocale = getDefaultNotificationLocale(notificationChannel); + if (notificationTemplate == null) { + if (StringUtils.equalsIgnoreCase(locale, defaultLocale)) { + // Template is not available in the default locale. Therefore, breaking the flow at the consuming side + // to avoid NPE. + String code = I18nEmailUtil.prependOperationScenarioToErrorCode( + TemplateMgtConstants.ErrorMessages.ERROR_CODE_TEMPLATE_NOT_FOUND.getCode(), + TemplateMgtConstants.ErrorScenarios.NOTIFICATION_TEMPLATE_MANAGER); + String errorMessage = String + .format(TemplateMgtConstants.ErrorMessages.ERROR_CODE_SYSTEM_TEMPLATE_NOT_FOUND.getMessage(), + templateType); + throw new NotificationTemplateManagerServerException(code, errorMessage); + } else { + if (log.isDebugEnabled()) { + String message = String + .format("'%s' system template in '%s' locale was not found. Trying to return the " + + "template in default locale : '%s'", templateType, locale, + defaultLocale); + log.debug(message); + } + // Try to get the template type in default locale. + return getSystemNotificationTemplate(notificationChannel, templateType, defaultLocale); + } + } + return notificationTemplate; + } + /** * {@inheritDoc} */ @@ -310,7 +368,7 @@ public void addNotificationTemplate(NotificationTemplate notificationTemplate, S notificationTemplate.setLocale(locale); } assertTemplateTypeExists(displayName, notificationChannel, tenantDomain); - if (templatePersistenceManager.isNotificationTemplateExists(displayName, locale, notificationChannel, + if (userDefinedTemplatePersistenceManager.isNotificationTemplateExists(displayName, locale, notificationChannel, applicationUuid, tenantDomain)) { String code = I18nEmailUtil.prependOperationScenarioToErrorCode( TemplateMgtConstants.ErrorMessages.ERROR_CODE_TEMPLATE_ALREADY_EXISTS.getCode(), @@ -321,7 +379,7 @@ public void addNotificationTemplate(NotificationTemplate notificationTemplate, S throw new NotificationTemplateManagerServerException(code, message); } try { - templatePersistenceManager.addOrUpdateNotificationTemplate(notificationTemplate, applicationUuid, + userDefinedTemplatePersistenceManager.addOrUpdateNotificationTemplate(notificationTemplate, applicationUuid, tenantDomain); } catch (NotificationTemplateManagerServerException e) { String code = I18nEmailUtil.prependOperationScenarioToErrorCode( @@ -360,7 +418,7 @@ public void updateNotificationTemplate(NotificationTemplate notificationTemplate notificationTemplate.setLocale(locale); } assertTemplateTypeExists(displayName, notificationChannel, tenantDomain); - if (!templatePersistenceManager.isNotificationTemplateExists( + if (!userDefinedTemplatePersistenceManager.isNotificationTemplateExists( displayName, locale, notificationChannel, applicationUuid, tenantDomain)) { String code = I18nEmailUtil.prependOperationScenarioToErrorCode( TemplateMgtConstants.ErrorMessages.ERROR_CODE_TEMPLATE_NOT_FOUND.getCode(), @@ -371,7 +429,7 @@ public void updateNotificationTemplate(NotificationTemplate notificationTemplate throw new NotificationTemplateManagerServerException(code, message); } try { - templatePersistenceManager.addOrUpdateNotificationTemplate(notificationTemplate, applicationUuid, + userDefinedTemplatePersistenceManager.addOrUpdateNotificationTemplate(notificationTemplate, applicationUuid, tenantDomain); } catch (NotificationTemplateManagerServerException e) { String code = I18nEmailUtil.prependOperationScenarioToErrorCode( @@ -422,7 +480,7 @@ public void deleteNotificationTemplate(String notificationChannel, String templa } locale = normalizeLocaleFormat(locale); try { - templatePersistenceManager.deleteNotificationTemplate(templateDisplayName, locale, + userDefinedTemplatePersistenceManager.deleteNotificationTemplate(templateDisplayName, locale, notificationChannel, applicationUuid, tenantDomain); } catch (NotificationTemplateManagerServerException ex) { String msg = String.format("Error deleting %s:%s template from %s tenant registry.", templateDisplayName, @@ -431,61 +489,6 @@ public void deleteNotificationTemplate(String notificationChannel, String templa } } - /** - * {@inheritDoc} todo : remove this function. - */ - @Override - public void addDefaultNotificationTemplates(String notificationChannel, String tenantDomain) - throws NotificationTemplateManagerException { - - // Get the list of Default notification templates. - List notificationTemplates = - getDefaultNotificationTemplates(notificationChannel); - int numberOfAddedTemplates = 0; - try { - for (NotificationTemplate template : notificationTemplates) { - String displayName = template.getDisplayName(); - String locale = template.getLocale(); - - /*Check for existence of each category, since some template may have migrated from earlier version - This will also add new template types provided from file, but won't update any existing template*/ - if (!templatePersistenceManager.isNotificationTemplateExists(displayName, locale, notificationChannel, - null, tenantDomain)) { - try { - addNotificationTemplate(template, tenantDomain); - if (log.isDebugEnabled()) { - String msg = "Default template added to %s tenant registry : %n%s"; - log.debug(String.format(msg, tenantDomain, template)); - } - numberOfAddedTemplates++; - } catch (NotificationTemplateManagerInternalException e) { - log.warn("Template : " + displayName + "already exists in the registry. Hence " + - "ignoring addition"); - } - } - } - if (log.isDebugEnabled()) { - log.debug(String.format("Added %d default %s templates to the tenant registry : %s", - numberOfAddedTemplates, notificationChannel, tenantDomain)); - } - } catch (NotificationTemplateManagerServerException ex) { - String error = "Error when tried to check for default email templates in tenant registry : %s"; - log.error(String.format(error, tenantDomain), ex); - } - } - - /** - * {@inheritDoc} - */ - @Override - public List getDefaultNotificationTemplates(String notificationChannel) { - - if (NotificationChannels.SMS_CHANNEL.getChannelType().equals(notificationChannel)) { - return I18nMgtDataHolder.getInstance().getDefaultSMSTemplates(); - } - return I18nMgtDataHolder.getInstance().getDefaultEmailTemplates(); - } - /** * {@inheritDoc} */ @@ -507,7 +510,7 @@ public boolean isNotificationTemplateExists(String notificationChannel, String t try { locale = normalizeLocaleFormat(locale); - return templatePersistenceManager.isNotificationTemplateExists(templateDisplayName, locale, + return userDefinedTemplatePersistenceManager.isNotificationTemplateExists(templateDisplayName, locale, notificationChannel, applicationUuid, tenantDomain); } catch (NotificationTemplateManagerServerException e) { String error = String.format("Error when retrieving notification templates of %s tenant.", tenantDomain); @@ -647,10 +650,25 @@ private String getDefaultNotificationLocale(String notificationChannel) { } } + private void assertSystemTemplateTypeExists(String templateType, String notificationChannel) + throws NotificationTemplateManagerServerException { + + if (!systemTemplatePersistenceManager.isNotificationTemplateTypeExists(templateType, notificationChannel, + null)) { + String code = I18nEmailUtil.prependOperationScenarioToErrorCode( + TemplateMgtConstants.ErrorMessages.ERROR_CODE_SYSTEM_TEMPLATE_TYPE_NOT_FOUND.getCode(), + TemplateMgtConstants.ErrorScenarios.NOTIFICATION_TEMPLATE_MANAGER); + String message = String.format( + TemplateMgtConstants.ErrorMessages.ERROR_CODE_SYSTEM_TEMPLATE_TYPE_NOT_FOUND.getMessage(), + templateType); + throw new NotificationTemplateManagerServerException(code, message); + } + } + private void assertTemplateTypeExists(String templateType, String notificationChannel, String tenantDomain) throws NotificationTemplateManagerServerException { - if (!templatePersistenceManager.isNotificationTemplateTypeExists(templateType, notificationChannel, + if (!unifiedTemplatePersistenceManager.isNotificationTemplateTypeExists(templateType, notificationChannel, tenantDomain)) { String code = I18nEmailUtil.prependOperationScenarioToErrorCode( TemplateMgtConstants.ErrorMessages.ERROR_CODE_TEMPLATE_TYPE_NOT_FOUND.getCode(), diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/TemplateMgtConstants.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/TemplateMgtConstants.java index b48f7f47..1772fe17 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/TemplateMgtConstants.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/TemplateMgtConstants.java @@ -44,6 +44,7 @@ public static class ErrorCodes { public static final String TEMPLATE_NOT_FOUND = "65004"; public static final String ERROR_ADDING_TEMPLATE = "65005"; public static final String ERROR_UPDATING_TEMPLATE = "65006"; + public static final String ERROR_SYSTEM_RESOURCE_DELETION_NOT_ALLOWED = "65007"; } /** @@ -69,10 +70,16 @@ public enum ErrorMessages { " already exists in tenant : %s"), ERROR_CODE_TEMPLATE_NOT_FOUND(ErrorCodes.TEMPLATE_NOT_FOUND, "Notification template : %s " + "doesn't exist in tenant : %s"), + ERROR_CODE_SYSTEM_TEMPLATE_TYPE_NOT_FOUND(ErrorCodes.TEMPLATE_TYPE_NOT_FOUND, + "System notification template type : %s doesn't exist."), + ERROR_CODE_SYSTEM_TEMPLATE_NOT_FOUND(ErrorCodes.TEMPLATE_NOT_FOUND, "System notification " + + "template : %s doesn't exist"), ERROR_CODE_ERROR_ADDING_TEMPLATE(ErrorCodes.ERROR_ADDING_TEMPLATE, "Error when adding template : %s" + " to tenant : %s"), ERROR_CODE_ERROR_UPDATING_TEMPLATE(ErrorCodes.ERROR_UPDATING_TEMPLATE, "Error when updating " + - "template : %s on tenant : %s"); + "template : %s on tenant : %s"), + ERROR_CODE_SYSTEM_RESOURCE_DELETION_NOT_ALLOWED(ErrorCodes.ERROR_SYSTEM_RESOURCE_DELETION_NOT_ALLOWED, + "System resource deletion not allowed. %S"); private final String code; private final String message; diff --git a/pom.xml b/pom.xml index 43841725..b0f2e7c2 100644 --- a/pom.xml +++ b/pom.xml @@ -477,7 +477,7 @@ - 1.10.0 + 1.11.4-SNAPSHOT [1.0.0, 3.0.0) From f12d380681b4392d6a8864f7eab03e2479ca05e0 Mon Sep 17 00:00:00 2001 From: rushannanayakkara Date: Sun, 29 Sep 2024 19:01:30 +0530 Subject: [PATCH 04/13] Allow partial deletion for system template types --- .../mgt/NotificationTemplateManagerImpl.java | 49 +++++++++++++------ .../mgt/constants/TemplateMgtConstants.java | 4 +- 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java index ff32de9b..3b717ff2 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java @@ -139,6 +139,17 @@ public void deleteNotificationTemplateType(String notificationChannel, String te throws NotificationTemplateManagerException { validateDisplayNameOfTemplateType(templateDisplayName); + assertTemplateTypeExists(templateDisplayName, notificationChannel, tenantDomain); + try { + userDefinedTemplatePersistenceManager.deleteNotificationTemplateType( + templateDisplayName, notificationChannel, tenantDomain); + } catch (NotificationTemplateManagerException ex) { + String errorMsg = String.format + ("Error deleting template type %s from %s tenant.", templateDisplayName, tenantDomain); + throw handleServerException(errorMsg, ex); + } + // If delete was executed on a system template type, the type will not be deleted, but the user define + // templates will be deleted. Therefore, this needs to be informed. if (systemTemplatePersistenceManager.isNotificationTemplateTypeExists(templateDisplayName, notificationChannel, null)) { String code = I18nEmailUtil.prependOperationScenarioToErrorCode( @@ -146,17 +157,9 @@ public void deleteNotificationTemplateType(String notificationChannel, String te TemplateMgtConstants.ErrorScenarios.NOTIFICATION_TEMPLATE_MANAGER); String message = String.format( TemplateMgtConstants.ErrorMessages.ERROR_CODE_SYSTEM_RESOURCE_DELETION_NOT_ALLOWED.getMessage(), - "System notification template types are not eligible for deletion."); + "System template type not deleted. User defined templates deleted."); throw new NotificationTemplateManagerServerException(code, message); } - try { - userDefinedTemplatePersistenceManager.deleteNotificationTemplateType(templateDisplayName, notificationChannel, - tenantDomain); - } catch (NotificationTemplateManagerException ex) { - String errorMsg = String.format - ("Error deleting template type %s from %s tenant.", templateDisplayName, tenantDomain); - throw handleServerException(errorMsg, ex); - } } /** @@ -188,7 +191,8 @@ public List getAllNotificationTemplates(String notificatio // Return the root organization's email templates. tenantDomain = getRootOrgTenantDomain(tenantDomain); } - return userDefinedTemplatePersistenceManager.listAllNotificationTemplates(notificationChannel, tenantDomain); + return userDefinedTemplatePersistenceManager.listAllNotificationTemplates( + notificationChannel, tenantDomain); } catch (NotificationTemplateManagerServerException e) { String error = String.format("Error when retrieving templates of %s tenant.", tenantDomain); throw handleServerException(error, e); @@ -221,15 +225,13 @@ public List getNotificationTemplatesOfType(String notifica tenantDomain = getRootOrgTenantDomain(tenantDomain); } assertTemplateTypeExists(templateDisplayName, notificationChannel, tenantDomain); - List notificationTemplates = userDefinedTemplatePersistenceManager.listNotificationTemplates( - templateDisplayName, notificationChannel, applicationUuid, tenantDomain); + List notificationTemplates = + userDefinedTemplatePersistenceManager.listNotificationTemplates(templateDisplayName, + notificationChannel, applicationUuid, tenantDomain); if (notificationTemplates == null) { notificationTemplates = new ArrayList<>(); } return notificationTemplates; - } catch (NotificationTemplateManagerServerException e) { - String error = String.format("Error when retrieving email templates of %s tenant.", tenantDomain); - throw handleServerException(error, e); } catch (OrganizationManagementException e) { throw handleServerException(e.getMessage(), e); } @@ -299,6 +301,23 @@ public NotificationTemplate getNotificationTemplate(String notificationChannel, return notificationTemplate; } + /** + * {@inheritDoc} + */ + @Override + public List getAllSystemNotificationTemplatesOfType(String notificationChannel, + String templateDisplayName) throws NotificationTemplateManagerException { + + assertSystemTemplateTypeExists(templateDisplayName, notificationChannel); + List notificationTemplates = + systemTemplatePersistenceManager.listNotificationTemplates(templateDisplayName, + notificationChannel, null, null); + if (notificationTemplates == null) { + notificationTemplates = new ArrayList<>(); + } + return notificationTemplates; + } + /** * {@inheritDoc} */ diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/TemplateMgtConstants.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/TemplateMgtConstants.java index 1772fe17..69216c67 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/TemplateMgtConstants.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/TemplateMgtConstants.java @@ -22,8 +22,8 @@ public class TemplateMgtConstants { private TemplateMgtConstants() {} - public static final String DEFAULT_EMAIL_NOTIFICATION_LOCALE = "en_us"; - public static final String DEFAULT_SMS_NOTIFICATION_LOCALE = "en_us"; + public static final String DEFAULT_EMAIL_NOTIFICATION_LOCALE = "en_US"; + public static final String DEFAULT_SMS_NOTIFICATION_LOCALE = "en_US"; /** * Class which contains the error scenarios. From 9d494c328c09f5639413c313a750b497f103947c Mon Sep 17 00:00:00 2001 From: rushannanayakkara Date: Sat, 5 Oct 2024 09:29:22 +0530 Subject: [PATCH 05/13] Remove partial deletion for system template types and add deleteCustomizedNotificationTemplates function. --- .../mgt/NotificationTemplateManagerImpl.java | 39 +++++++++++++------ 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java index 3b717ff2..9fb03487 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java @@ -140,16 +140,8 @@ public void deleteNotificationTemplateType(String notificationChannel, String te validateDisplayNameOfTemplateType(templateDisplayName); assertTemplateTypeExists(templateDisplayName, notificationChannel, tenantDomain); - try { - userDefinedTemplatePersistenceManager.deleteNotificationTemplateType( - templateDisplayName, notificationChannel, tenantDomain); - } catch (NotificationTemplateManagerException ex) { - String errorMsg = String.format - ("Error deleting template type %s from %s tenant.", templateDisplayName, tenantDomain); - throw handleServerException(errorMsg, ex); - } - // If delete was executed on a system template type, the type will not be deleted, but the user define - // templates will be deleted. Therefore, this needs to be informed. + + // System template types are cannot be deleted since these are hard coded values. if (systemTemplatePersistenceManager.isNotificationTemplateTypeExists(templateDisplayName, notificationChannel, null)) { String code = I18nEmailUtil.prependOperationScenarioToErrorCode( @@ -157,9 +149,18 @@ public void deleteNotificationTemplateType(String notificationChannel, String te TemplateMgtConstants.ErrorScenarios.NOTIFICATION_TEMPLATE_MANAGER); String message = String.format( TemplateMgtConstants.ErrorMessages.ERROR_CODE_SYSTEM_RESOURCE_DELETION_NOT_ALLOWED.getMessage(), - "System template type not deleted. User defined templates deleted."); + "System template types are not eligible for deletion."); throw new NotificationTemplateManagerServerException(code, message); } + + try { + userDefinedTemplatePersistenceManager.deleteNotificationTemplateType( + templateDisplayName, notificationChannel, tenantDomain); + } catch (NotificationTemplateManagerException ex) { + String errorMsg = String.format + ("Error deleting template type %s from %s tenant.", templateDisplayName, tenantDomain); + throw handleServerException(errorMsg, ex); + } } /** @@ -508,6 +509,22 @@ public void deleteNotificationTemplate(String notificationChannel, String templa } } + /** + * {@inheritDoc} + */ + @Override + public void deleteCustomizedNotificationTemplates(String notificationChannel, String templateType, + String tenantDomain) throws NotificationTemplateManagerException { + try { + NotificationTemplateManager.super.deleteCustomizedNotificationTemplates(notificationChannel, + templateType, tenantDomain); + } catch (NotificationTemplateManagerException e) { + String msg = String.format("Error deleting custom templates for %s template type %s from %s .", + notificationChannel, templateType, tenantDomain); + throw handleServerException(msg, e); + } + } + /** * {@inheritDoc} */ From bceca94fc66e43b751db3c6f4d69573e6cc12d2c Mon Sep 17 00:00:00 2001 From: rushannanayakkara Date: Wed, 16 Oct 2024 20:03:46 +0530 Subject: [PATCH 06/13] Get main app through application management service --- .../mgt/NotificationTemplateManagerImpl.java | 46 ++++++++++--------- .../mgt/constants/TemplateMgtConstants.java | 5 +- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java index 9fb03487..3eaf2948 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java @@ -26,6 +26,7 @@ import org.wso2.carbon.email.mgt.store.TemplatePersistenceManager; import org.wso2.carbon.email.mgt.store.TemplatePersistenceManagerFactory; import org.wso2.carbon.email.mgt.util.I18nEmailUtil; +import org.wso2.carbon.identity.application.common.IdentityApplicationManagementServerException; import org.wso2.carbon.identity.governance.exceptions.notiification.NotificationTemplateManagerClientException; import org.wso2.carbon.identity.governance.exceptions.notiification.NotificationTemplateManagerException; import org.wso2.carbon.identity.governance.exceptions.notiification.NotificationTemplateManagerInternalException; @@ -33,7 +34,6 @@ import org.wso2.carbon.identity.governance.model.NotificationTemplate; import org.wso2.carbon.identity.governance.service.notification.NotificationChannels; import org.wso2.carbon.identity.governance.service.notification.NotificationTemplateManager; -import org.wso2.carbon.identity.organization.management.application.OrgApplicationManager; import org.wso2.carbon.identity.organization.management.service.OrganizationManager; import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementException; import org.wso2.carbon.identity.organization.management.service.util.OrganizationManagementUtil; @@ -98,14 +98,15 @@ public void addNotificationTemplateType(String notificationChannel, String displ validateDisplayNameOfTemplateType(displayName); try { - if (userDefinedTemplatePersistenceManager + if (unifiedTemplatePersistenceManager .isNotificationTemplateTypeExists(displayName, notificationChannel, tenantDomain)) { // This error is caught in the catch block below to generate the // NotificationTemplateManagerServerException. throw new NotificationTemplateManagerInternalException( TemplateMgtConstants.ErrorCodes.TEMPLATE_TYPE_ALREADY_EXISTS, StringUtils.EMPTY); } - userDefinedTemplatePersistenceManager.addNotificationTemplateType(displayName, notificationChannel, tenantDomain); + unifiedTemplatePersistenceManager.addNotificationTemplateType(displayName, notificationChannel, + tenantDomain); } catch (NotificationTemplateManagerServerException e) { String code = I18nEmailUtil.prependOperationScenarioToErrorCode( TemplateMgtConstants.ErrorMessages.ERROR_CODE_ERROR_ADDING_TEMPLATE.getCode(), @@ -154,8 +155,8 @@ public void deleteNotificationTemplateType(String notificationChannel, String te } try { - userDefinedTemplatePersistenceManager.deleteNotificationTemplateType( - templateDisplayName, notificationChannel, tenantDomain); + unifiedTemplatePersistenceManager.deleteNotificationTemplateType(templateDisplayName, + notificationChannel, tenantDomain); } catch (NotificationTemplateManagerException ex) { String errorMsg = String.format ("Error deleting template type %s from %s tenant.", templateDisplayName, tenantDomain); @@ -209,6 +210,7 @@ public List getAllNotificationTemplates(String notificatio public List getNotificationTemplatesOfType(String notificationChannel, String templateDisplayName, String tenantDomain) throws NotificationTemplateManagerException { + return getNotificationTemplatesOfType(notificationChannel, templateDisplayName, tenantDomain, null); } @@ -258,22 +260,31 @@ public NotificationTemplate getNotificationTemplate(String notificationChannel, try { if (OrganizationManagementUtil.isOrganization(tenantDomain)) { - // Return the root organization's notification template. + // To return the root organization's notification template. tenantDomain = getRootOrgTenantDomain(tenantDomain); // If it's application specific template is required, get the root organization's application. if (StringUtils.isNotBlank(applicationUuid)) { - applicationUuid = getMainApplicationIdForGivenSharedApp(applicationUuid, tenantDomain); + applicationUuid = I18nMgtDataHolder.getInstance().getApplicationManagementService() + .getMainAppId(applicationUuid); } } } catch (OrganizationManagementException e) { throw new NotificationTemplateManagerException(e.getMessage(), e); + } catch (IdentityApplicationManagementServerException e) { + String code = I18nEmailUtil.prependOperationScenarioToErrorCode( + TemplateMgtConstants.ErrorMessages.ERROR_CODE_ERROR_RESOLVING_MAIN_APPLICATION.getCode(), + TemplateMgtConstants.ErrorScenarios.NOTIFICATION_TEMPLATE_MANAGER); + String message = String.format( + TemplateMgtConstants.ErrorMessages.ERROR_CODE_ERROR_RESOLVING_MAIN_APPLICATION.getMessage(), + applicationUuid, tenantDomain); + throw new NotificationTemplateManagerException(code, message, e); } validateTemplateLocale(locale); locale = normalizeLocaleFormat(locale); validateDisplayNameOfTemplateType(templateType); assertTemplateTypeExists(templateType, notificationChannel, tenantDomain); - NotificationTemplate notificationTemplate = userDefinedTemplatePersistenceManager.getNotificationTemplate(templateType, - locale, notificationChannel, applicationUuid, tenantDomain); + NotificationTemplate notificationTemplate = userDefinedTemplatePersistenceManager.getNotificationTemplate( + templateType, locale, notificationChannel, applicationUuid, tenantDomain); String defaultLocale = getDefaultNotificationLocale(notificationChannel); if (notificationTemplate == null) { @@ -513,11 +524,13 @@ public void deleteNotificationTemplate(String notificationChannel, String templa * {@inheritDoc} */ @Override - public void deleteCustomizedNotificationTemplates(String notificationChannel, String templateType, + public void resetNotificationTemplateType(String notificationChannel, String templateType, String tenantDomain) throws NotificationTemplateManagerException { try { - NotificationTemplateManager.super.deleteCustomizedNotificationTemplates(notificationChannel, - templateType, tenantDomain); + unifiedTemplatePersistenceManager.deleteNotificationTemplateType(templateType, notificationChannel, + tenantDomain); + unifiedTemplatePersistenceManager.addNotificationTemplateType(templateType, notificationChannel, + tenantDomain); } catch (NotificationTemplateManagerException e) { String msg = String.format("Error deleting custom templates for %s template type %s from %s .", notificationChannel, templateType, tenantDomain); @@ -582,15 +595,6 @@ private NotificationTemplateManagerServerException handleServerException(String return new NotificationTemplateManagerServerException(errorMsg, ex); } - private String getMainApplicationIdForGivenSharedApp(String applicationUuid, String tenantDomain) - throws OrganizationManagementException { - - OrganizationManager organizationManager = I18nMgtDataHolder.getInstance().getOrganizationManager(); - String sharedOrgId = organizationManager.resolveOrganizationId(tenantDomain); - OrgApplicationManager sharedAppManager = I18nMgtDataHolder.getInstance().getSharedAppManager(); - return sharedAppManager.getMainApplicationIdForGivenSharedApp(applicationUuid, sharedOrgId); - } - private void validateTemplateLocale(String locale) throws NotificationTemplateManagerClientException { if (StringUtils.isBlank(locale)) { diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/TemplateMgtConstants.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/TemplateMgtConstants.java index 69216c67..c1a2396c 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/TemplateMgtConstants.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/TemplateMgtConstants.java @@ -45,6 +45,7 @@ public static class ErrorCodes { public static final String ERROR_ADDING_TEMPLATE = "65005"; public static final String ERROR_UPDATING_TEMPLATE = "65006"; public static final String ERROR_SYSTEM_RESOURCE_DELETION_NOT_ALLOWED = "65007"; + public static final String ERROR_RESOLVING_MAIN_APPLICATION = "65008"; } /** @@ -79,7 +80,9 @@ public enum ErrorMessages { ERROR_CODE_ERROR_UPDATING_TEMPLATE(ErrorCodes.ERROR_UPDATING_TEMPLATE, "Error when updating " + "template : %s on tenant : %s"), ERROR_CODE_SYSTEM_RESOURCE_DELETION_NOT_ALLOWED(ErrorCodes.ERROR_SYSTEM_RESOURCE_DELETION_NOT_ALLOWED, - "System resource deletion not allowed. %S"); + "System resource deletion not allowed. %S"), + ERROR_CODE_ERROR_RESOLVING_MAIN_APPLICATION(ErrorCodes.ERROR_RESOLVING_MAIN_APPLICATION, + "Error when resolving main application : %s"); private final String code; private final String message; From bdbae4c9ea99d36ec6d2d7f76dca8788e3dd6003 Mon Sep 17 00:00:00 2001 From: rushannanayakkara Date: Thu, 17 Oct 2024 17:46:06 +0530 Subject: [PATCH 07/13] Add deleteAllNotificationTemplates function --- .../mgt/NotificationTemplateManagerImpl.java | 4 +--- .../email/mgt/constants/SQLConstants.java | 3 +++ .../mgt/store/DBBasedTemplateManager.java | 19 +++++++++++++++++ .../mgt/store/HybridTemplateManager.java | 14 +++++++++++++ .../store/RegistryBasedTemplateManager.java | 17 +++++++++++++++ .../store/SystemDefaultTemplateManager.java | 7 +++++++ .../mgt/store/TemplatePersistenceManager.java | 13 ++++++++++++ .../mgt/store/UnifiedTemplateManager.java | 10 +++++++++ .../store/dao/AppNotificationTemplateDAO.java | 21 +++++++++++++++++++ 9 files changed, 105 insertions(+), 3 deletions(-) diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java index 3eaf2948..a9a7c7e5 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java @@ -527,9 +527,7 @@ public void deleteNotificationTemplate(String notificationChannel, String templa public void resetNotificationTemplateType(String notificationChannel, String templateType, String tenantDomain) throws NotificationTemplateManagerException { try { - unifiedTemplatePersistenceManager.deleteNotificationTemplateType(templateType, notificationChannel, - tenantDomain); - unifiedTemplatePersistenceManager.addNotificationTemplateType(templateType, notificationChannel, + unifiedTemplatePersistenceManager.deleteAllNotificationTemplates(templateType, notificationChannel, tenantDomain); } catch (NotificationTemplateManagerException e) { String msg = String.format("Error deleting custom templates for %s template type %s from %s .", diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/SQLConstants.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/SQLConstants.java index 61bfaf6e..2df488c7 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/SQLConstants.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/SQLConstants.java @@ -97,4 +97,7 @@ public class SQLConstants { public static final String DELETE_APP_NOTIFICATION_TEMPLATES_BY_TYPE_SQL = "DELETE FROM IDN_NOTIFICATION_ORG_TEMPLATE WHERE TYPE_ID = (" + GET_NOTIFICATION_TYPE_ID_SQL + ") AND APP_ID = :APP_ID; AND TENANT_ID = :TENANT_ID;"; + public static final String DELETE_ALL_APP_NOTIFICATION_TEMPLATES_BY_TYPE_SQL = + "DELETE FROM IDN_NOTIFICATION_APP_TEMPLATE WHERE TYPE_ID = (" + GET_NOTIFICATION_TYPE_ID_SQL + + ") AND TENANT_ID = :TENANT_ID;"; } diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/DBBasedTemplateManager.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/DBBasedTemplateManager.java index df6eaa65..a1155bbe 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/DBBasedTemplateManager.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/DBBasedTemplateManager.java @@ -328,6 +328,25 @@ public void deleteNotificationTemplates(String displayName, String notificationC } } + @Override + public void deleteAllNotificationTemplates(String displayName, String notificationChannel, String tenantDomain) + throws NotificationTemplateManagerServerException { + + String templateTypeKey = displayName.toLowerCase(); + int tenantId = getTenantId(tenantDomain); + + orgNotificationTemplateDAO.removeNotificationTemplates(templateTypeKey, notificationChannel, tenantId); + if (log.isDebugEnabled()) { + log.debug(String.format("Org %s templates for type: %s for tenant: %s successfully deleted.", + notificationChannel, displayName, tenantDomain)); + } + appNotificationTemplateDAO.removeAllNotificationTemplates(templateTypeKey, notificationChannel, tenantId); + if (log.isDebugEnabled()) { + log.debug(String.format("App %s templates for type: %s for all applications for tenant: %s " + + "successfully deleted.", notificationChannel, displayName, tenantDomain)); + } + } + /** * Get the tenant id of the given tenant domain. * @param tenantDomain diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/HybridTemplateManager.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/HybridTemplateManager.java index 0f0a5b18..513bb5a5 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/HybridTemplateManager.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/HybridTemplateManager.java @@ -102,6 +102,20 @@ public void deleteNotificationTemplateType(String displayName, String notificati } } + @Override + public void deleteAllNotificationTemplates(String displayName, String notificationChannel, String tenantDomain) + throws NotificationTemplateManagerServerException { + + if (dbBasedTemplateManager.isNotificationTemplateTypeExists(displayName, notificationChannel, tenantDomain)) { + dbBasedTemplateManager.deleteAllNotificationTemplates(displayName, notificationChannel, tenantDomain); + } + + if (registryBasedTemplateManager.isNotificationTemplateTypeExists(displayName, notificationChannel, + tenantDomain)) { + registryBasedTemplateManager.deleteAllNotificationTemplates(displayName, notificationChannel, tenantDomain); + } + } + @Override public void addOrUpdateNotificationTemplate(NotificationTemplate notificationTemplate, String applicationUuid, String tenantDomain) throws NotificationTemplateManagerServerException { diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/RegistryBasedTemplateManager.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/RegistryBasedTemplateManager.java index f8910253..10ca2d3e 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/RegistryBasedTemplateManager.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/RegistryBasedTemplateManager.java @@ -297,6 +297,23 @@ public void deleteNotificationTemplates(String displayName, String notificationC } } + @Override + public void deleteAllNotificationTemplates(String displayName, String notificationChannel, String tenantDomain) + throws NotificationTemplateManagerServerException { + + String templateType = I18nEmailUtil.getNormalizedName(displayName); + String path = buildTemplateRootDirectoryPath(templateType, notificationChannel, null); + + try { + Collection templates = (Collection) resourceMgtService.getIdentityResource(path, tenantDomain); + for (String subPath : templates.getChildren()) { + resourceMgtService.deleteIdentityResource(subPath, tenantDomain); + } + } catch (IdentityRuntimeException | RegistryException e) { + throw new NotificationTemplateManagerServerException("Error while deleting notification templates.", e); + } + } + /** * Get the notification template from resource. * diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/SystemDefaultTemplateManager.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/SystemDefaultTemplateManager.java index 75ca2331..d340a305 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/SystemDefaultTemplateManager.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/SystemDefaultTemplateManager.java @@ -112,6 +112,13 @@ public void deleteNotificationTemplateType(String displayName, String notificati throw new UnsupportedOperationException(); } + @Override + public void deleteAllNotificationTemplates(String displayName, String notificationChannel, String tenantDomain) + throws NotificationTemplateManagerServerException { + + throw new UnsupportedOperationException(); + } + @Override public void addOrUpdateNotificationTemplate(NotificationTemplate notificationTemplate, String applicationUuid, String tenantDomain) throws NotificationTemplateManagerServerException { diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/TemplatePersistenceManager.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/TemplatePersistenceManager.java index 69321c11..a058f1eb 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/TemplatePersistenceManager.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/TemplatePersistenceManager.java @@ -73,6 +73,19 @@ List listNotificationTemplateTypes(String notificationChannel, String te void deleteNotificationTemplateType(String displayName, String notificationChannel, String tenantDomain) throws NotificationTemplateManagerServerException; + /** + * Delete both organization level and application level notification templates under a notification template type. + * + * @param displayName Display Name. + * @param notificationChannel Notification channel. + * @param tenantDomain Tenant domain. + * @throws NotificationTemplateManagerServerException If an error occurred while resetting the template type. + */ + default void deleteAllNotificationTemplates(String displayName, String notificationChannel, String tenantDomain) + throws NotificationTemplateManagerServerException { + // not implemented + } + /** * Update the notification template if exists or add a new template if not exists. * diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/UnifiedTemplateManager.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/UnifiedTemplateManager.java index 9ba6f3c4..52ba3e0a 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/UnifiedTemplateManager.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/UnifiedTemplateManager.java @@ -82,6 +82,16 @@ public void deleteNotificationTemplateType(String displayName, String notificati } } + @Override + public void deleteAllNotificationTemplates(String displayName, String notificationChannel, String tenantDomain) + throws NotificationTemplateManagerServerException { + + if (templatePersistenceManager.isNotificationTemplateTypeExists(displayName, notificationChannel, + tenantDomain)) { + templatePersistenceManager.deleteAllNotificationTemplates(displayName, notificationChannel, tenantDomain); + } + } + @Override public void addOrUpdateNotificationTemplate(NotificationTemplate notificationTemplate, String applicationUuid, String tenantDomain) throws NotificationTemplateManagerServerException { diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/AppNotificationTemplateDAO.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/AppNotificationTemplateDAO.java index 53dc2568..03191064 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/AppNotificationTemplateDAO.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/AppNotificationTemplateDAO.java @@ -38,6 +38,7 @@ import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.TENANT_ID; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.TYPE_ID; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.TYPE_KEY; +import static org.wso2.carbon.email.mgt.constants.SQLConstants.DELETE_ALL_APP_NOTIFICATION_TEMPLATES_BY_TYPE_SQL; import static org.wso2.carbon.email.mgt.constants.SQLConstants.DELETE_APP_NOTIFICATION_TEMPLATES_BY_TYPE_SQL; import static org.wso2.carbon.email.mgt.constants.SQLConstants.DELETE_APP_NOTIFICATION_TEMPLATE_SQL; import static org.wso2.carbon.email.mgt.constants.SQLConstants.GET_APP_NOTIFICATION_TEMPLATE_SQL; @@ -275,4 +276,24 @@ public void removeNotificationTemplates(String templateType, String channelName, throw new NotificationTemplateManagerServerException(error, e); } } + + public void removeAllNotificationTemplates(String templateType, String channelName, int tenantId) + throws NotificationTemplateManagerServerException { + + NamedJdbcTemplate namedJdbcTemplate = JdbcUtils.getNewNamedJdbcTemplate(); + try { + namedJdbcTemplate.executeUpdate(DELETE_ALL_APP_NOTIFICATION_TEMPLATES_BY_TYPE_SQL, + preparedStatement -> { + preparedStatement.setString(TYPE_KEY, templateType.toLowerCase()); + preparedStatement.setString(CHANNEL, channelName); + preparedStatement.setInt(TENANT_ID, tenantId); + preparedStatement.setInt(TENANT_ID, tenantId); + }); + } catch (DataAccessException e) { + String error = + String.format("Error while deleting %s templates of type %s from all applications in %s tenant.", + channelName, templateType, tenantId); + throw new NotificationTemplateManagerServerException(error, e); + } + } } From f3b34a6a0a4458bbaf9c0ad4222ec9f70db11706 Mon Sep 17 00:00:00 2001 From: rushannanayakkara Date: Fri, 18 Oct 2024 07:13:11 +0530 Subject: [PATCH 08/13] Clear cache when all templates or a type are removed. --- .../mgt/NotificationTemplateManagerImpl.java | 4 ++-- ...CacheBackedAppNotificationTemplateDAO.java | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java index a9a7c7e5..70aaf6c7 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java @@ -524,8 +524,8 @@ public void deleteNotificationTemplate(String notificationChannel, String templa * {@inheritDoc} */ @Override - public void resetNotificationTemplateType(String notificationChannel, String templateType, - String tenantDomain) throws NotificationTemplateManagerException { + public void resetNotificationTemplateType(String notificationChannel, String templateType, String tenantDomain) + throws NotificationTemplateManagerException { try { unifiedTemplatePersistenceManager.deleteAllNotificationTemplates(templateType, notificationChannel, tenantDomain); diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/cache/CacheBackedAppNotificationTemplateDAO.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/cache/CacheBackedAppNotificationTemplateDAO.java index 8717c7ee..4c3c0bee 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/cache/CacheBackedAppNotificationTemplateDAO.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/cache/CacheBackedAppNotificationTemplateDAO.java @@ -20,17 +20,26 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.database.utils.jdbc.NamedJdbcTemplate; +import org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException; import org.wso2.carbon.email.mgt.cache.AppNotificationTemplateCache; import org.wso2.carbon.email.mgt.cache.AppNotificationTemplateCacheKey; import org.wso2.carbon.email.mgt.cache.AppNotificationTemplateListCache; import org.wso2.carbon.email.mgt.cache.AppNotificationTemplateListCacheKey; +import org.wso2.carbon.email.mgt.cache.OrgNotificationTemplateListCacheKey; import org.wso2.carbon.email.mgt.store.dao.AppNotificationTemplateDAO; +import org.wso2.carbon.identity.core.util.JdbcUtils; import org.wso2.carbon.identity.governance.exceptions.notiification.NotificationTemplateManagerServerException; import org.wso2.carbon.identity.governance.model.NotificationTemplate; import java.util.ArrayList; import java.util.List; +import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.CHANNEL; +import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.TENANT_ID; +import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.TYPE_KEY; +import static org.wso2.carbon.email.mgt.constants.SQLConstants.DELETE_ALL_APP_NOTIFICATION_TEMPLATES_BY_TYPE_SQL; + /** * This class provides the cache backed implementation for {@link AppNotificationTemplateDAO}. */ @@ -186,4 +195,15 @@ public void removeNotificationTemplates(String templateType, String channelName, new AppNotificationTemplateListCacheKey(templateType, channelName, applicationUuid); templateListCache.clearCacheEntry(listCacheKey, tenantId); } + + public void removeAllNotificationTemplates(String templateType, String channelName, int tenantId) + throws NotificationTemplateManagerServerException { + + super.removeAllNotificationTemplates(templateType, channelName, tenantId); + + appNotificationTemplateCache.clear(tenantId); + // Clearing full template list cache for tenant since it's not possible to remove all entries for a template + // type at once. + templateListCache.clear(tenantId); + } } From 4252c141fcc27c4a071bcb913aa758e65812c44d Mon Sep 17 00:00:00 2001 From: rushannanayakkara Date: Sun, 20 Oct 2024 07:54:46 +0530 Subject: [PATCH 09/13] Refactor code --- .../mgt/internal/I18nMgtServiceComponent.java | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/internal/I18nMgtServiceComponent.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/internal/I18nMgtServiceComponent.java index 4c368bee..ece2a0d1 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/internal/I18nMgtServiceComponent.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/internal/I18nMgtServiceComponent.java @@ -112,13 +112,13 @@ protected void activate(ComponentContext context) { log.error("Error registering Email Template Mgt Service."); } - // Register Notification Template Mgt Service as an OSGi service. - NotificationTemplateManagerImpl notificationTemplateManager = new NotificationTemplateManagerImpl(); - Hashtable serviceProperties = new Hashtable<>(); - serviceProperties.put("service.name", "NotificationTemplateManager"); - ServiceRegistration notificationTemplateSR = bundleCtx.registerService( - NotificationTemplateManager.class.getName(), notificationTemplateManager, serviceProperties); - if (notificationTemplateSR != null) { + // Register EmailTemplateManagerImpl. + Hashtable emailTemplateManagerServiceProperties = new Hashtable<>(); + emailTemplateManagerServiceProperties.put("service.name", "EmailTemplateManager"); + ServiceRegistration notificationManagerSR = bundleCtx + .registerService(NotificationTemplateManager.class.getName(), emailTemplateManager, + emailTemplateManagerServiceProperties); + if (notificationManagerSR != null) { if (log.isDebugEnabled()) { log.debug("Notification Template Mgt Service registered."); } @@ -126,10 +126,14 @@ protected void activate(ComponentContext context) { log.error("Error registering Notification Template Mgt Service."); } - // Register EmailTemplateManagerImpl. - ServiceRegistration notificationManagerSR = bundleCtx - .registerService(NotificationTemplateManager.class.getName(), emailTemplateManager, null); - if (notificationManagerSR != null) { + // Register Notification Template Mgt Service as an OSGi service. + NotificationTemplateManagerImpl notificationTemplateManager = new NotificationTemplateManagerImpl(); + Hashtable notificationTemplateManagerServiceProperties = new Hashtable<>(); + notificationTemplateManagerServiceProperties.put("service.name", "NotificationTemplateManager"); + ServiceRegistration notificationTemplateSR = bundleCtx + .registerService(NotificationTemplateManager.class.getName(), notificationTemplateManager, + notificationTemplateManagerServiceProperties); + if (notificationTemplateSR != null) { if (log.isDebugEnabled()) { log.debug("Notification Template Mgt Service registered."); } From b828356ab48d2c21dbdf01a3949e3ec86b98ce14 Mon Sep 17 00:00:00 2001 From: rushannanayakkara Date: Mon, 21 Oct 2024 11:22:43 +0530 Subject: [PATCH 10/13] Addressing comments --- .../email/mgt/constants/I18nMgtConstants.java | 4 ++++ .../mgt/internal/I18nMgtServiceComponent.java | 17 +++++++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/I18nMgtConstants.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/I18nMgtConstants.java index 5215ec2c..0a4b7a47 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/I18nMgtConstants.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/I18nMgtConstants.java @@ -65,6 +65,10 @@ private I18nMgtConstants() {} public static final String NOTIFICATION_TEMPLATES_STORAGE_CONFIG = "DataStorageType.NotificationTemplates"; public static final String NOTIFICATION_TEMPLATES_LEGACY_TENANTS = "NotificationTemplates.LegacyTenants.Tenant"; + public static final String SERVICE_PROPERTY_KEY_SERVICE_NAME = "service.name"; + public static final String SERVICE_PROPERTY_VAL_EMAIL_TEMPLATE_MANAGER = "EmailTemplateManager"; + public static final String SERVICE_PROPERTY_VAL_NOTIFICATION_TEMPLATE_MANAGER = "NotificationTemplateManager"; + public static class ErrorMsg { private ErrorMsg() { diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/internal/I18nMgtServiceComponent.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/internal/I18nMgtServiceComponent.java index ece2a0d1..93b356bb 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/internal/I18nMgtServiceComponent.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/internal/I18nMgtServiceComponent.java @@ -73,6 +73,9 @@ import javax.xml.stream.XMLStreamReader; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NOTIFICATION_TEMPLATES_LEGACY_TENANTS; +import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.SERVICE_PROPERTY_KEY_SERVICE_NAME; +import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.SERVICE_PROPERTY_VAL_EMAIL_TEMPLATE_MANAGER; +import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.SERVICE_PROPERTY_VAL_NOTIFICATION_TEMPLATE_MANAGER; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.SMS_PROVIDER; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.SMS_PROVIDER_POST_BODY_TEMPLATES_DIR_PATH; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.TEMPLATE_BODY; @@ -114,14 +117,13 @@ protected void activate(ComponentContext context) { // Register EmailTemplateManagerImpl. Hashtable emailTemplateManagerServiceProperties = new Hashtable<>(); - emailTemplateManagerServiceProperties.put("service.name", "EmailTemplateManager"); + emailTemplateManagerServiceProperties.put(SERVICE_PROPERTY_KEY_SERVICE_NAME, + SERVICE_PROPERTY_VAL_EMAIL_TEMPLATE_MANAGER); ServiceRegistration notificationManagerSR = bundleCtx .registerService(NotificationTemplateManager.class.getName(), emailTemplateManager, emailTemplateManagerServiceProperties); if (notificationManagerSR != null) { - if (log.isDebugEnabled()) { - log.debug("Notification Template Mgt Service registered."); - } + log.debug("Notification Template Mgt Service registered."); } else { log.error("Error registering Notification Template Mgt Service."); } @@ -129,14 +131,13 @@ protected void activate(ComponentContext context) { // Register Notification Template Mgt Service as an OSGi service. NotificationTemplateManagerImpl notificationTemplateManager = new NotificationTemplateManagerImpl(); Hashtable notificationTemplateManagerServiceProperties = new Hashtable<>(); - notificationTemplateManagerServiceProperties.put("service.name", "NotificationTemplateManager"); + notificationTemplateManagerServiceProperties.put(SERVICE_PROPERTY_KEY_SERVICE_NAME, + SERVICE_PROPERTY_VAL_NOTIFICATION_TEMPLATE_MANAGER); ServiceRegistration notificationTemplateSR = bundleCtx .registerService(NotificationTemplateManager.class.getName(), notificationTemplateManager, notificationTemplateManagerServiceProperties); if (notificationTemplateSR != null) { - if (log.isDebugEnabled()) { - log.debug("Notification Template Mgt Service registered."); - } + log.debug("Notification Template Mgt Service registered."); } else { log.error("Error registering Notification Template Mgt Service."); } From 26a332fc3c1197a7e9e19d14b53548153b250c2a Mon Sep 17 00:00:00 2001 From: rushannanayakkara Date: Mon, 21 Oct 2024 12:06:40 +0530 Subject: [PATCH 11/13] Code refactoring --- .../wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java index 70aaf6c7..83041b45 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java @@ -106,7 +106,7 @@ public void addNotificationTemplateType(String notificationChannel, String displ TemplateMgtConstants.ErrorCodes.TEMPLATE_TYPE_ALREADY_EXISTS, StringUtils.EMPTY); } unifiedTemplatePersistenceManager.addNotificationTemplateType(displayName, notificationChannel, - tenantDomain); + tenantDomain); } catch (NotificationTemplateManagerServerException e) { String code = I18nEmailUtil.prependOperationScenarioToErrorCode( TemplateMgtConstants.ErrorMessages.ERROR_CODE_ERROR_ADDING_TEMPLATE.getCode(), From cef6060109d91a4f2185e52720aeebcbb8b5ce92 Mon Sep 17 00:00:00 2001 From: rushannanayakkara Date: Tue, 22 Oct 2024 15:52:17 +0530 Subject: [PATCH 12/13] addressing comments --- .../mgt/NotificationTemplateManagerImpl.java | 62 +++++++++---------- .../mgt/constants/TemplateMgtConstants.java | 7 ++- .../store/RegistryBasedTemplateManager.java | 4 +- 3 files changed, 35 insertions(+), 38 deletions(-) diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java index 83041b45..f24acf81 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/NotificationTemplateManagerImpl.java @@ -54,7 +54,7 @@ public class NotificationTemplateManagerImpl implements NotificationTemplateMana private final TemplatePersistenceManager userDefinedTemplatePersistenceManager; private final TemplatePersistenceManager systemTemplatePersistenceManager; - private final TemplatePersistenceManager unifiedTemplatePersistenceManager; + private final TemplatePersistenceManager templatePersistenceManager; public NotificationTemplateManagerImpl() { @@ -62,7 +62,7 @@ public NotificationTemplateManagerImpl() { this.userDefinedTemplatePersistenceManager = templatePersistenceManagerFactory.getUserDefinedTemplatePersistenceManager(); this.systemTemplatePersistenceManager = templatePersistenceManagerFactory.getSystemTemplatePersistenceManager(); - this.unifiedTemplatePersistenceManager = templatePersistenceManagerFactory.getTemplatePersistenceManager(); + this.templatePersistenceManager = templatePersistenceManagerFactory.getTemplatePersistenceManager(); } /** @@ -77,7 +77,7 @@ public List getAllNotificationTemplateTypes(String notificationChannel, // Return the root organization's template types. tenantDomain = getRootOrgTenantDomain(tenantDomain); } - List templateTypes = unifiedTemplatePersistenceManager + List templateTypes = templatePersistenceManager .listNotificationTemplateTypes(notificationChannel, tenantDomain); return templateTypes != null ? templateTypes : new ArrayList<>(); } catch (NotificationTemplateManagerServerException ex) { @@ -96,17 +96,20 @@ public List getAllNotificationTemplateTypes(String notificationChannel, public void addNotificationTemplateType(String notificationChannel, String displayName, String tenantDomain) throws NotificationTemplateManagerException { + // For new additions display name is trimmed to avoid allowing surrounding white spaces. + // This is not done for existing display names for backward compatibility. + displayName = displayName.trim(); validateDisplayNameOfTemplateType(displayName); try { - if (unifiedTemplatePersistenceManager + if (templatePersistenceManager .isNotificationTemplateTypeExists(displayName, notificationChannel, tenantDomain)) { // This error is caught in the catch block below to generate the // NotificationTemplateManagerServerException. throw new NotificationTemplateManagerInternalException( TemplateMgtConstants.ErrorCodes.TEMPLATE_TYPE_ALREADY_EXISTS, StringUtils.EMPTY); } - unifiedTemplatePersistenceManager.addNotificationTemplateType(displayName, notificationChannel, - tenantDomain); + templatePersistenceManager.addNotificationTemplateType(displayName, notificationChannel, + tenantDomain); } catch (NotificationTemplateManagerServerException e) { String code = I18nEmailUtil.prependOperationScenarioToErrorCode( TemplateMgtConstants.ErrorMessages.ERROR_CODE_ERROR_ADDING_TEMPLATE.getCode(), @@ -140,7 +143,7 @@ public void deleteNotificationTemplateType(String notificationChannel, String te throws NotificationTemplateManagerException { validateDisplayNameOfTemplateType(templateDisplayName); - assertTemplateTypeExists(templateDisplayName, notificationChannel, tenantDomain); + verifyTemplateTypeExists(templateDisplayName, notificationChannel, tenantDomain); // System template types are cannot be deleted since these are hard coded values. if (systemTemplatePersistenceManager.isNotificationTemplateTypeExists(templateDisplayName, notificationChannel, @@ -155,7 +158,7 @@ public void deleteNotificationTemplateType(String notificationChannel, String te } try { - unifiedTemplatePersistenceManager.deleteNotificationTemplateType(templateDisplayName, + templatePersistenceManager.deleteNotificationTemplateType(templateDisplayName, notificationChannel, tenantDomain); } catch (NotificationTemplateManagerException ex) { String errorMsg = String.format @@ -173,7 +176,7 @@ public boolean isNotificationTemplateTypeExists(String notificationChannel, Stri throws NotificationTemplateManagerException { try { - return unifiedTemplatePersistenceManager.isNotificationTemplateTypeExists(templateTypeDisplayName, + return templatePersistenceManager.isNotificationTemplateTypeExists(templateTypeDisplayName, notificationChannel, tenantDomain); } catch (NotificationTemplateManagerServerException e) { String error = String.format("Error when retrieving templates of %s tenant.", tenantDomain); @@ -227,14 +230,9 @@ public List getNotificationTemplatesOfType(String notifica // Return the root organization's email templates. tenantDomain = getRootOrgTenantDomain(tenantDomain); } - assertTemplateTypeExists(templateDisplayName, notificationChannel, tenantDomain); - List notificationTemplates = - userDefinedTemplatePersistenceManager.listNotificationTemplates(templateDisplayName, + verifyTemplateTypeExists(templateDisplayName, notificationChannel, tenantDomain); + return userDefinedTemplatePersistenceManager.listNotificationTemplates(templateDisplayName, notificationChannel, applicationUuid, tenantDomain); - if (notificationTemplates == null) { - notificationTemplates = new ArrayList<>(); - } - return notificationTemplates; } catch (OrganizationManagementException e) { throw handleServerException(e.getMessage(), e); } @@ -282,7 +280,7 @@ public NotificationTemplate getNotificationTemplate(String notificationChannel, validateTemplateLocale(locale); locale = normalizeLocaleFormat(locale); validateDisplayNameOfTemplateType(templateType); - assertTemplateTypeExists(templateType, notificationChannel, tenantDomain); + verifyTemplateTypeExists(templateType, notificationChannel, tenantDomain); NotificationTemplate notificationTemplate = userDefinedTemplatePersistenceManager.getNotificationTemplate( templateType, locale, notificationChannel, applicationUuid, tenantDomain); @@ -320,14 +318,9 @@ public NotificationTemplate getNotificationTemplate(String notificationChannel, public List getAllSystemNotificationTemplatesOfType(String notificationChannel, String templateDisplayName) throws NotificationTemplateManagerException { - assertSystemTemplateTypeExists(templateDisplayName, notificationChannel); - List notificationTemplates = - systemTemplatePersistenceManager.listNotificationTemplates(templateDisplayName, + verifySystemTemplateTypeExists(templateDisplayName, notificationChannel); + return systemTemplatePersistenceManager.listNotificationTemplates(templateDisplayName, notificationChannel, null, null); - if (notificationTemplates == null) { - notificationTemplates = new ArrayList<>(); - } - return notificationTemplates; } /** @@ -341,7 +334,7 @@ public NotificationTemplate getSystemNotificationTemplate(String notificationCha validateTemplateLocale(locale); locale = normalizeLocaleFormat(locale); validateDisplayNameOfTemplateType(templateType); - assertSystemTemplateTypeExists(templateType, notificationChannel); + verifySystemTemplateTypeExists(templateType, notificationChannel); NotificationTemplate notificationTemplate = systemTemplatePersistenceManager.getNotificationTemplate(templateType,locale, notificationChannel, null, null); @@ -390,6 +383,9 @@ public void addNotificationTemplate(NotificationTemplate notificationTemplate, S public void addNotificationTemplate(NotificationTemplate notificationTemplate, String tenantDomain, String applicationUuid) throws NotificationTemplateManagerException { + // For new additions display name is trimmed to avoid allowing surrounding white spaces. + // This is not done for existing display names for backward compatibility. + notificationTemplate.setDisplayName(notificationTemplate.getDisplayName().trim()); validateNotificationTemplate(notificationTemplate); String displayName = notificationTemplate.getDisplayName(); String locale = notificationTemplate.getLocale(); @@ -398,7 +394,7 @@ public void addNotificationTemplate(NotificationTemplate notificationTemplate, S if (notificationTemplate.getLocale() != null && !notificationTemplate.getLocale().equals(locale)) { notificationTemplate.setLocale(locale); } - assertTemplateTypeExists(displayName, notificationChannel, tenantDomain); + verifyTemplateTypeExists(displayName, notificationChannel, tenantDomain); if (userDefinedTemplatePersistenceManager.isNotificationTemplateExists(displayName, locale, notificationChannel, applicationUuid, tenantDomain)) { String code = I18nEmailUtil.prependOperationScenarioToErrorCode( @@ -448,7 +444,7 @@ public void updateNotificationTemplate(NotificationTemplate notificationTemplate if (notificationTemplate.getLocale() != null && !notificationTemplate.getLocale().equals(locale)) { notificationTemplate.setLocale(locale); } - assertTemplateTypeExists(displayName, notificationChannel, tenantDomain); + verifyTemplateTypeExists(displayName, notificationChannel, tenantDomain); if (!userDefinedTemplatePersistenceManager.isNotificationTemplateExists( displayName, locale, notificationChannel, applicationUuid, tenantDomain)) { String code = I18nEmailUtil.prependOperationScenarioToErrorCode( @@ -526,8 +522,9 @@ public void deleteNotificationTemplate(String notificationChannel, String templa @Override public void resetNotificationTemplateType(String notificationChannel, String templateType, String tenantDomain) throws NotificationTemplateManagerException { + try { - unifiedTemplatePersistenceManager.deleteAllNotificationTemplates(templateType, notificationChannel, + templatePersistenceManager.deleteAllNotificationTemplates(templateType, notificationChannel, tenantDomain); } catch (NotificationTemplateManagerException e) { String msg = String.format("Error deleting custom templates for %s template type %s from %s .", @@ -603,8 +600,7 @@ private void validateTemplateLocale(String locale) throws NotificationTemplateMa throw new NotificationTemplateManagerClientException(errorCode, TemplateMgtConstants.ErrorMessages.ERROR_CODE_EMPTY_LOCALE.getMessage()); } - final String LOCAL_REGEX = "^[a-z]{2}_[A-Z]{2}$"; - if (!locale.matches(LOCAL_REGEX)) { + if (!locale.matches("^[a-z]{2}_[A-Z]{2}$")) { String errorCode = I18nEmailUtil.prependOperationScenarioToErrorCode( TemplateMgtConstants.ErrorMessages.ERROR_CODE_INVALID_LOCALE.getCode(), @@ -688,7 +684,7 @@ private String getDefaultNotificationLocale(String notificationChannel) { } } - private void assertSystemTemplateTypeExists(String templateType, String notificationChannel) + private void verifySystemTemplateTypeExists(String templateType, String notificationChannel) throws NotificationTemplateManagerServerException { if (!systemTemplatePersistenceManager.isNotificationTemplateTypeExists(templateType, notificationChannel, @@ -703,10 +699,10 @@ private void assertSystemTemplateTypeExists(String templateType, String notifica } } - private void assertTemplateTypeExists(String templateType, String notificationChannel, String tenantDomain) + private void verifyTemplateTypeExists(String templateType, String notificationChannel, String tenantDomain) throws NotificationTemplateManagerServerException { - if (!unifiedTemplatePersistenceManager.isNotificationTemplateTypeExists(templateType, notificationChannel, + if (!templatePersistenceManager.isNotificationTemplateTypeExists(templateType, notificationChannel, tenantDomain)) { String code = I18nEmailUtil.prependOperationScenarioToErrorCode( TemplateMgtConstants.ErrorMessages.ERROR_CODE_TEMPLATE_TYPE_NOT_FOUND.getCode(), diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/TemplateMgtConstants.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/TemplateMgtConstants.java index c1a2396c..aea6a620 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/TemplateMgtConstants.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/constants/TemplateMgtConstants.java @@ -1,8 +1,8 @@ /* - * Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) 2023-2024, WSO2 LLC. (http://www.wso2.com). * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except * in compliance with the License. * You may obtain a copy of the License at * @@ -38,6 +38,7 @@ public static class ErrorScenarios { * Class which contains the error codes for template management. */ public static class ErrorCodes { + public static final String TEMPLATE_TYPE_ALREADY_EXISTS = "65001"; public static final String TEMPLATE_TYPE_NOT_FOUND = "65002"; public static final String TEMPLATE_ALREADY_EXISTS = "65003"; diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/RegistryBasedTemplateManager.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/RegistryBasedTemplateManager.java index 10ca2d3e..e1beb019 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/RegistryBasedTemplateManager.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/RegistryBasedTemplateManager.java @@ -301,8 +301,8 @@ public void deleteNotificationTemplates(String displayName, String notificationC public void deleteAllNotificationTemplates(String displayName, String notificationChannel, String tenantDomain) throws NotificationTemplateManagerServerException { - String templateType = I18nEmailUtil.getNormalizedName(displayName); - String path = buildTemplateRootDirectoryPath(templateType, notificationChannel, null); + String path = buildTemplateRootDirectoryPath(I18nEmailUtil.getNormalizedName(displayName), + notificationChannel, null); try { Collection templates = (Collection) resourceMgtService.getIdentityResource(path, tenantDomain); From e012ab93b24ba772fd29dd2c2f1ccfd7d40b6222 Mon Sep 17 00:00:00 2001 From: rushannanayakkara Date: Wed, 23 Oct 2024 18:27:30 +0530 Subject: [PATCH 13/13] Bump governance version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 175e8d8d..296ed608 100644 --- a/pom.xml +++ b/pom.xml @@ -477,7 +477,7 @@ - 1.11.4-SNAPSHOT + 1.11.11 [1.0.0, 3.0.0)