Skip to content

Commit

Permalink
Add system notification template related functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
RushanNanayakkara committed Sep 26, 2024
1 parent 3e91c12 commit ffd5c3d
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand All @@ -72,7 +77,7 @@ public List<String> getAllNotificationTemplateTypes(String notificationChannel,
// Return the root organization's template types.
tenantDomain = getRootOrgTenantDomain(tenantDomain);
}
List<String> templateTypes = templatePersistenceManager
List<String> templateTypes = unifiedTemplatePersistenceManager
.listNotificationTemplateTypes(notificationChannel, tenantDomain);
return templateTypes != null ? templateTypes : new ArrayList<>();
} catch (NotificationTemplateManagerServerException ex) {
Expand All @@ -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(),
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -173,7 +188,7 @@ public List<NotificationTemplate> 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);
Expand Down Expand Up @@ -206,7 +221,7 @@ public List<NotificationTemplate> getNotificationTemplatesOfType(String notifica
tenantDomain = getRootOrgTenantDomain(tenantDomain);
}
assertTemplateTypeExists(templateDisplayName, notificationChannel, tenantDomain);
List<NotificationTemplate> notificationTemplates = templatePersistenceManager.listNotificationTemplates(
List<NotificationTemplate> notificationTemplates = userDefinedTemplatePersistenceManager.listNotificationTemplates(
templateDisplayName, notificationChannel, applicationUuid, tenantDomain);
if (notificationTemplates == null) {
notificationTemplates = new ArrayList<>();
Expand Down Expand Up @@ -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(
Expand All @@ -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}
*/
Expand All @@ -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(),
Expand All @@ -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(
Expand Down Expand Up @@ -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(),
Expand All @@ -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(
Expand Down Expand Up @@ -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,
Expand All @@ -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<NotificationTemplate> 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<NotificationTemplate> getDefaultNotificationTemplates(String notificationChannel) {

if (NotificationChannels.SMS_CHANNEL.getChannelType().equals(notificationChannel)) {
return I18nMgtDataHolder.getInstance().getDefaultSMSTemplates();
}
return I18nMgtDataHolder.getInstance().getDefaultEmailTemplates();
}

/**
* {@inheritDoc}
*/
Expand All @@ -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);
Expand Down Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

/**
Expand All @@ -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;
Expand Down
Loading

0 comments on commit ffd5c3d

Please sign in to comment.