diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/EmailTemplateManager.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/EmailTemplateManager.java index 798164b7..c150c8da 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/EmailTemplateManager.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/EmailTemplateManager.java @@ -77,6 +77,29 @@ void deleteEmailTemplate(String templateTypeName, String localeCode, String tenantDomain) throws I18nEmailMgtException; + /** + * Delete all email templates from the tenant registry. Email templates are identified with the templateTypeName and + * localeCode. + * + * @param templateTypeName Email template type name. + * @param tenantDomain Tenant domain. + * @throws I18nEmailMgtException If an error occurred while deleting the email templates. + */ + void deleteEmailTemplates(String templateTypeName, String tenantDomain) throws I18nEmailMgtException; + + /** + * Delete all email templates from the tenant registry. Email templates are identified with the templateTypeName, + * localeCode and application UUID. + * + * @param templateTypeName Email template type name. + * @param tenantDomain Tenant domain. + * @param applicationUuid Application UUID. + * @throws I18nEmailMgtException If an error occurred while deleting the email templates. + */ + void deleteEmailTemplates(String templateTypeName, + String tenantDomain, + String applicationUuid) throws I18nEmailMgtException; + /** * Get an email template from tenant registry. diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/EmailTemplateManagerImpl.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/EmailTemplateManagerImpl.java index b1235428..f6e9f6e4 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/EmailTemplateManagerImpl.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/EmailTemplateManagerImpl.java @@ -561,6 +561,52 @@ public void deleteEmailTemplate(String templateTypeName, String localeCode, Stri deleteEmailTemplate(templateTypeName, localeCode, tenantDomain, null); } + @Override + public void deleteEmailTemplates(String templateTypeName, String tenantDomain) throws I18nEmailMgtException { + + validateTemplateType(templateTypeName, tenantDomain); + + String templateType = I18nEmailUtil.getNormalizedName(templateTypeName); + String path = EMAIL_TEMPLATE_PATH + PATH_SEPARATOR + templateType; + + try { + Collection templates = (Collection) resourceMgtService.getIdentityResource(path, tenantDomain); + for (String subPath : templates.getChildren()) { + // Exclude the app templates. + if (!subPath.contains(APP_TEMPLATE_PATH)) { + resourceMgtService.deleteIdentityResource(subPath, tenantDomain); + } + } + } catch (IdentityRuntimeException | RegistryException ex) { + String errorMsg = String.format + ("Error deleting email template type %s from %s tenant.", templateType, tenantDomain); + handleServerException(errorMsg, ex); + } + } + + @Override + public void deleteEmailTemplates(String templateTypeName, String tenantDomain, String applicationUuid) + throws I18nEmailMgtException { + + validateTemplateType(templateTypeName, tenantDomain); + + String templateType = I18nEmailUtil.getNormalizedName(templateTypeName); + String path = EMAIL_TEMPLATE_PATH + PATH_SEPARATOR + templateType + APP_TEMPLATE_PATH + + PATH_SEPARATOR + applicationUuid; + + try { + if (!resourceMgtService.isResourceExists(path, tenantDomain)) { + // No templates found for the given application UUID. + return; + } + resourceMgtService.deleteIdentityResource(path, tenantDomain); + } catch (IdentityRuntimeException ex) { + String errorMsg = String.format("Error deleting email template type %s from %s tenant for application %s.", + templateType, tenantDomain, applicationUuid); + handleServerException(errorMsg, ex); + } + } + @Override public void addDefaultEmailTemplates(String tenantDomain) throws I18nEmailMgtException {