From cc954ab76a5f7e0437b8ec65d9d67735ac10f416 Mon Sep 17 00:00:00 2001 From: Darshana Gunawardana Date: Wed, 20 Nov 2024 21:38:07 +0530 Subject: [PATCH] Store content based on the unicode support flag --- .../email/mgt/constants/I18nMgtConstants.java | 5 ++ .../email/mgt/constants/SQLConstants.java | 35 +++++++++++ .../email/mgt/internal/I18nMgtDataHolder.java | 21 +++++++ .../mgt/internal/I18nMgtServiceComponent.java | 4 ++ .../store/dao/AppNotificationTemplateDAO.java | 59 ++++++++++++++++--- .../store/dao/OrgNotificationTemplateDAO.java | 59 ++++++++++++++++--- 6 files changed, 167 insertions(+), 16 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 3ea9acaa..e0020665 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 @@ -64,6 +64,8 @@ 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 NOTIFICATION_TEMPLATES_ENABLE_UNICODE_SUPPORT = + "NotificationTemplates.EnableUnicodeSupport"; public static final String SERVICE_PROPERTY_KEY_SERVICE_NAME = "service.name"; public static final String SERVICE_PROPERTY_VAL_EMAIL_TEMPLATE_MANAGER = "EmailTemplateManager"; @@ -157,6 +159,9 @@ public static class NotificationTableColumns { public static final String TENANT_ID = "TENANT_ID"; public static final String TEMPLATE_KEY = "TEMPLATE_KEY"; public static final String LOCALE = "LOCALE"; + public static final String SUBJECT = "SUBJECT"; + public static final String BODY = "BODY"; + public static final String FOOTER = "FOOTER"; public static final String CONTENT = "CONTENT"; public static final String CONTENT_TYPE = "CONTENT_TYPE"; public static final String TYPE_ID = "TYPE_ID"; 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 2fb08ba2..4e4fccd2 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 @@ -46,21 +46,38 @@ public class SQLConstants { "(TEMPLATE_KEY, LOCALE, CONTENT, CONTENT_TYPE, TYPE_ID, TENANT_ID) " + "VALUES (:TEMPLATE_KEY;, :LOCALE;, :CONTENT;, :CONTENT_TYPE;, (" + GET_NOTIFICATION_TYPE_ID_SQL + "), :TENANT_ID;)"; + public static final String INSERT_ORG_NOTIFICATION_TEMPLATE_WITHOUT_UNICODE_SQL = + "INSERT INTO IDN_NOTIFICATION_ORG_TEMPLATE " + + "(TEMPLATE_KEY, LOCALE, SUBJECT, BODY, FOOTER, CONTENT_TYPE, TYPE_ID, TENANT_ID) " + + "VALUES (:TEMPLATE_KEY;, :LOCALE;, :SUBJECT;, :BODY;, :FOOTER;, :CONTENT_TYPE;, (" + + GET_NOTIFICATION_TYPE_ID_SQL + "), :TENANT_ID;)"; public static final String GET_ORG_NOTIFICATION_TEMPLATE_SQL = "SELECT CONTENT, CONTENT_TYPE FROM IDN_NOTIFICATION_ORG_TEMPLATE " + "WHERE TEMPLATE_KEY = :TEMPLATE_KEY; AND TYPE_ID = (" + GET_NOTIFICATION_TYPE_ID_SQL + ") AND TENANT_ID = :TENANT_ID;"; + public static final String GET_ORG_NOTIFICATION_TEMPLATE_WITHOUT_UNICODE_SQL = + "SELECT SUBJECT, BODY, FOOTER, CONTENT_TYPE FROM IDN_NOTIFICATION_ORG_TEMPLATE " + + "WHERE TEMPLATE_KEY = :TEMPLATE_KEY; AND TYPE_ID = (" + GET_NOTIFICATION_TYPE_ID_SQL + + ") AND TENANT_ID = :TENANT_ID;"; public static final String IS_ORG_NOTIFICATION_TEMPLATE_EXISTS_SQL = "SELECT ID FROM IDN_NOTIFICATION_ORG_TEMPLATE " + "WHERE TEMPLATE_KEY = :TEMPLATE_KEY; AND TYPE_ID = :TYPE_ID; AND TENANT_ID = :TENANT_ID;"; public static final String LIST_ORG_NOTIFICATION_TEMPLATES_BY_TYPE_SQL = "SELECT CONTENT, CONTENT_TYPE, LOCALE FROM IDN_NOTIFICATION_ORG_TEMPLATE " + "WHERE TYPE_ID = (" + GET_NOTIFICATION_TYPE_ID_SQL + ") AND TENANT_ID = :TENANT_ID;"; + public static final String LIST_ORG_NOTIFICATION_TEMPLATES_BY_TYPE_WITHOUT_UNICODE_SQL = + "SELECT SUBJECT, BODY, FOOTER, CONTENT_TYPE, LOCALE FROM IDN_NOTIFICATION_ORG_TEMPLATE " + + "WHERE TYPE_ID = (" + GET_NOTIFICATION_TYPE_ID_SQL + ") AND TENANT_ID = :TENANT_ID;"; public static final String UPDATE_ORG_NOTIFICATION_TEMPLATE_SQL = "UPDATE IDN_NOTIFICATION_ORG_TEMPLATE " + "SET CONTENT = :CONTENT;, CONTENT_TYPE = :CONTENT_TYPE; " + "WHERE TEMPLATE_KEY = :TEMPLATE_KEY; AND TYPE_ID = (" + GET_NOTIFICATION_TYPE_ID_SQL + ") AND TENANT_ID = :TENANT_ID;"; + public static final String UPDATE_ORG_NOTIFICATION_TEMPLATE_WITHOUT_UNICODE_SQL = + "UPDATE IDN_NOTIFICATION_ORG_TEMPLATE " + + "SET SUBJECT = :SUBJECT;, BODY = :BODY;, FOOTER = :FOOTER;, CONTENT_TYPE = :CONTENT_TYPE; " + + "WHERE TEMPLATE_KEY = :TEMPLATE_KEY; AND TYPE_ID = (" + GET_NOTIFICATION_TYPE_ID_SQL + + ") AND TENANT_ID = :TENANT_ID;"; public static final String DELETE_ORG_NOTIFICATION_TEMPLATE_SQL = "DELETE FROM IDN_NOTIFICATION_ORG_TEMPLATE WHERE TEMPLATE_KEY = :TEMPLATE_KEY; AND TYPE_ID = (" + GET_NOTIFICATION_TYPE_ID_SQL + ") AND TENANT_ID = :TENANT_ID;"; @@ -74,10 +91,19 @@ public class SQLConstants { "(TEMPLATE_KEY, LOCALE, CONTENT, CONTENT_TYPE, TYPE_ID, APP_ID, TENANT_ID) " + "VALUES (:TEMPLATE_KEY;, :LOCALE;, :CONTENT;, :CONTENT_TYPE;, (" + GET_NOTIFICATION_TYPE_ID_SQL + "), :APP_ID;, :TENANT_ID;)"; + public static final String INSERT_APP_NOTIFICATION_TEMPLATE_WITHOUT_UNICODE_SQL = + "INSERT INTO IDN_NOTIFICATION_APP_TEMPLATE " + + "(TEMPLATE_KEY, LOCALE, SUBJECT, BODY, FOOTER, CONTENT_TYPE, TYPE_ID, APP_ID, TENANT_ID) " + + "VALUES (:TEMPLATE_KEY;, :LOCALE;, :SUBJECT;, :BODY;, :FOOTER;, :CONTENT_TYPE;, (" + + GET_NOTIFICATION_TYPE_ID_SQL + "), :APP_ID;, :TENANT_ID;)"; public static final String GET_APP_NOTIFICATION_TEMPLATE_SQL = "SELECT CONTENT, CONTENT_TYPE FROM IDN_NOTIFICATION_APP_TEMPLATE " + "WHERE TEMPLATE_KEY = :TEMPLATE_KEY; AND TYPE_ID = (" + GET_NOTIFICATION_TYPE_ID_SQL + ") AND APP_ID = :APP_ID; AND TENANT_ID = :TENANT_ID;"; + public static final String GET_APP_NOTIFICATION_TEMPLATE_WITHOUT_UNICODE_SQL = + "SELECT SUBJECT, BODY, FOOTER, CONTENT_TYPE FROM IDN_NOTIFICATION_APP_TEMPLATE " + + "WHERE TEMPLATE_KEY = :TEMPLATE_KEY; AND TYPE_ID = (" + GET_NOTIFICATION_TYPE_ID_SQL + + ") AND APP_ID = :APP_ID; AND TENANT_ID = :TENANT_ID;"; public static final String IS_APP_NOTIFICATION_TEMPLATE_EXISTS_SQL = "SELECT ID FROM IDN_NOTIFICATION_APP_TEMPLATE " + "WHERE TEMPLATE_KEY = :TEMPLATE_KEY; AND TYPE_ID = :TYPE_ID; AND APP_ID = :APP_ID; " + @@ -86,11 +112,20 @@ public class SQLConstants { "SELECT CONTENT, CONTENT_TYPE, LOCALE FROM IDN_NOTIFICATION_APP_TEMPLATE " + "WHERE TYPE_ID = (" + GET_NOTIFICATION_TYPE_ID_SQL + ") AND APP_ID = :APP_ID; AND TENANT_ID = :TENANT_ID;"; + public static final String LIST_APP_NOTIFICATION_TEMPLATES_BY_APP_WITHOUT_UNICODE_SQL = + "SELECT SUBJECT, BODY, FOOTER, CONTENT_TYPE, LOCALE FROM IDN_NOTIFICATION_APP_TEMPLATE " + + "WHERE TYPE_ID = (" + GET_NOTIFICATION_TYPE_ID_SQL + + ") AND APP_ID = :APP_ID; AND TENANT_ID = :TENANT_ID;"; public static final String UPDATE_APP_NOTIFICATION_TEMPLATE_SQL = "UPDATE IDN_NOTIFICATION_APP_TEMPLATE " + "SET CONTENT = :CONTENT;, CONTENT_TYPE = :CONTENT_TYPE; " + "WHERE TEMPLATE_KEY = :TEMPLATE_KEY; AND TYPE_ID = (" + GET_NOTIFICATION_TYPE_ID_SQL + ") AND APP_ID = :APP_ID; AND TENANT_ID = :TENANT_ID;"; + public static final String UPDATE_APP_NOTIFICATION_TEMPLATE_WITHOUT_UNICODE_SQL = + "UPDATE IDN_NOTIFICATION_APP_TEMPLATE " + + "SET SUBJECT = :SUBJECT;, BODY = :BODY;, FOOTER = :FOOTER;, CONTENT_TYPE = :CONTENT_TYPE; " + + "WHERE TEMPLATE_KEY = :TEMPLATE_KEY; AND TYPE_ID = (" + GET_NOTIFICATION_TYPE_ID_SQL + + ") AND APP_ID = :APP_ID; AND TENANT_ID = :TENANT_ID;"; public static final String DELETE_APP_NOTIFICATION_TEMPLATE_SQL = "DELETE FROM IDN_NOTIFICATION_APP_TEMPLATE WHERE TEMPLATE_KEY = :TEMPLATE_KEY; AND TYPE_ID = (" + GET_NOTIFICATION_TYPE_ID_SQL + ") AND APP_ID = :APP_ID; AND TENANT_ID = :TENANT_ID;"; diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/internal/I18nMgtDataHolder.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/internal/I18nMgtDataHolder.java index 1b845fdb..39c2886f 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/internal/I18nMgtDataHolder.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/internal/I18nMgtDataHolder.java @@ -37,6 +37,7 @@ public class I18nMgtDataHolder{ private List defaultEmailTemplates = new ArrayList<>(); private List defaultSMSTemplates = new ArrayList<>(); private List legacyTenants = new ArrayList<>(); + private boolean isUnicodeSupported = false; private static I18nMgtDataHolder instance = new I18nMgtDataHolder(); @@ -160,4 +161,24 @@ public List getLegacyTenants() { return legacyTenants; } + + /** + * Sets whether the unicode support for template content is enabled or not. + * + * @param isUnicodeSupported true to enable Unicode support, false to disable. + */ + public void setUnicodeSupport(boolean isUnicodeSupported) { + + this.isUnicodeSupported = isUnicodeSupported; + } + + /** + * Gets whether the unicode support for template content is enabled. + * + * @return true if the unicode support for template content is enabled. + */ + public boolean isUnicodeSupported() { + + return isUnicodeSupported; + } } 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 1644bc4a..a095312c 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 @@ -69,6 +69,7 @@ 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.NOTIFICATION_TEMPLATES_ENABLE_UNICODE_SUPPORT; 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; @@ -99,6 +100,9 @@ protected void activate(ComponentContext context) { List legacyTenants = IdentityUtil.getPropertyAsList(NOTIFICATION_TEMPLATES_LEGACY_TENANTS); I18nMgtDataHolder.getInstance().setLegacyTenants(legacyTenants); + String enableUnicodeSupport = IdentityUtil.getProperty(NOTIFICATION_TEMPLATES_ENABLE_UNICODE_SUPPORT); + I18nMgtDataHolder.getInstance().setUnicodeSupport(Boolean.parseBoolean(enableUnicodeSupport)); + // Register Email Mgt Service as an OSGi service. EmailTemplateManagerImpl emailTemplateManager = new EmailTemplateManagerImpl(); ServiceRegistration emailTemplateSR = bundleCtx.registerService(EmailTemplateManager.class.getName(), 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 6df89ced..465d11bc 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 @@ -20,6 +20,7 @@ import org.wso2.carbon.database.utils.jdbc.NamedJdbcTemplate; import org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException; +import org.wso2.carbon.email.mgt.internal.I18nMgtDataHolder; 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; @@ -30,11 +31,14 @@ import java.util.List; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.APP_ID; +import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.BODY; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.CHANNEL; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.CONTENT; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.CONTENT_TYPE; +import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.FOOTER; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.ID; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.LOCALE; +import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.SUBJECT; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.TEMPLATE_KEY; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.TENANT_ID; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.TYPE_ID; @@ -43,11 +47,15 @@ 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; +import static org.wso2.carbon.email.mgt.constants.SQLConstants.GET_APP_NOTIFICATION_TEMPLATE_WITHOUT_UNICODE_SQL; import static org.wso2.carbon.email.mgt.constants.SQLConstants.GET_NOTIFICATION_TYPE_ID_SQL; import static org.wso2.carbon.email.mgt.constants.SQLConstants.INSERT_APP_NOTIFICATION_TEMPLATE_SQL; +import static org.wso2.carbon.email.mgt.constants.SQLConstants.INSERT_APP_NOTIFICATION_TEMPLATE_WITHOUT_UNICODE_SQL; import static org.wso2.carbon.email.mgt.constants.SQLConstants.IS_APP_NOTIFICATION_TEMPLATE_EXISTS_SQL; import static org.wso2.carbon.email.mgt.constants.SQLConstants.LIST_APP_NOTIFICATION_TEMPLATES_BY_APP_SQL; +import static org.wso2.carbon.email.mgt.constants.SQLConstants.LIST_APP_NOTIFICATION_TEMPLATES_BY_APP_WITHOUT_UNICODE_SQL; import static org.wso2.carbon.email.mgt.constants.SQLConstants.UPDATE_APP_NOTIFICATION_TEMPLATE_SQL; +import static org.wso2.carbon.email.mgt.constants.SQLConstants.UPDATE_APP_NOTIFICATION_TEMPLATE_WITHOUT_UNICODE_SQL; import static org.wso2.carbon.email.mgt.util.I18nEmailUtil.getContentByteArray; import static org.wso2.carbon.email.mgt.util.I18nEmailUtil.setContent; @@ -56,6 +64,8 @@ */ public class AppNotificationTemplateDAO { + private boolean isUnicodeSupported = I18nMgtDataHolder.getInstance().isUnicodeSupported(); + public void addNotificationTemplate(NotificationTemplate notificationTemplate, String applicationUuid, int tenantId) throws NotificationTemplateManagerServerException { @@ -67,10 +77,18 @@ public void addNotificationTemplate(NotificationTemplate notificationTemplate, S byte[] contentByteArray = getContentByteArray(notificationTemplate); int contentLength = contentByteArray.length; try (InputStream contentStream = new ByteArrayInputStream(contentByteArray)) { - namedJdbcTemplate.executeInsert(INSERT_APP_NOTIFICATION_TEMPLATE_SQL, (preparedStatement -> { + String insertAppNotificationTemplateSql = isUnicodeSupported ? INSERT_APP_NOTIFICATION_TEMPLATE_SQL : + INSERT_APP_NOTIFICATION_TEMPLATE_WITHOUT_UNICODE_SQL; + namedJdbcTemplate.executeInsert(insertAppNotificationTemplateSql, (preparedStatement -> { preparedStatement.setString(TEMPLATE_KEY, locale.toLowerCase()); preparedStatement.setString(LOCALE, locale); - preparedStatement.setBinaryStream(CONTENT, contentStream, contentLength); + if (isUnicodeSupported) { + preparedStatement.setBinaryStream(CONTENT, contentStream, contentLength); + } else { + preparedStatement.setString(SUBJECT, notificationTemplate.getSubject()); + preparedStatement.setString(BODY, notificationTemplate.getBody()); + preparedStatement.setString(FOOTER, notificationTemplate.getFooter()); + } preparedStatement.setString(CONTENT_TYPE, notificationTemplate.getContentType()); preparedStatement.setString(TYPE_KEY, displayName.toLowerCase()); preparedStatement.setString(CHANNEL, channelName); @@ -96,10 +114,18 @@ public NotificationTemplate getNotificationTemplate(String locale, String templa NotificationTemplate notificationTemplate; try { - notificationTemplate = namedJdbcTemplate.fetchSingleRecord(GET_APP_NOTIFICATION_TEMPLATE_SQL, + String getAppNotificationTemplateSql = isUnicodeSupported ? GET_APP_NOTIFICATION_TEMPLATE_SQL : + GET_APP_NOTIFICATION_TEMPLATE_WITHOUT_UNICODE_SQL; + notificationTemplate = namedJdbcTemplate.fetchSingleRecord(getAppNotificationTemplateSql, (resultSet, rowNumber) -> { NotificationTemplate notificationTemplateResult = new NotificationTemplate(); - setContent(resultSet.getBinaryStream(CONTENT), notificationTemplateResult); + if (isUnicodeSupported) { + setContent(resultSet.getBinaryStream(CONTENT), notificationTemplateResult); + } else { + notificationTemplateResult.setSubject(resultSet.getString(SUBJECT)); + notificationTemplateResult.setBody(resultSet.getString(BODY)); + notificationTemplateResult.setFooter(resultSet.getString(FOOTER)); + } notificationTemplateResult.setContentType(resultSet.getString(CONTENT_TYPE)); notificationTemplateResult.setLocale(locale); notificationTemplateResult.setType(templateType); @@ -170,10 +196,19 @@ public List listNotificationTemplates(String templateType, List notificationTemplates; try { - notificationTemplates = namedJdbcTemplate.executeQuery(LIST_APP_NOTIFICATION_TEMPLATES_BY_APP_SQL, + String listAppNotificationTemplatesByAppSql = + isUnicodeSupported ? LIST_APP_NOTIFICATION_TEMPLATES_BY_APP_SQL : + LIST_APP_NOTIFICATION_TEMPLATES_BY_APP_WITHOUT_UNICODE_SQL; + notificationTemplates = namedJdbcTemplate.executeQuery(listAppNotificationTemplatesByAppSql, (resultSet, rowNumber) -> { NotificationTemplate notificationTemplateResult = new NotificationTemplate(); - setContent(resultSet.getBinaryStream(CONTENT), notificationTemplateResult); + if (isUnicodeSupported) { + setContent(resultSet.getBinaryStream(CONTENT), notificationTemplateResult); + } else { + notificationTemplateResult.setSubject(resultSet.getString(SUBJECT)); + notificationTemplateResult.setBody(resultSet.getString(BODY)); + notificationTemplateResult.setFooter(resultSet.getString(FOOTER)); + } notificationTemplateResult.setContentType(resultSet.getString(CONTENT_TYPE)); notificationTemplateResult.setLocale(resultSet.getString(LOCALE)); notificationTemplateResult.setType(templateType.toLowerCase()); @@ -208,9 +243,17 @@ public void updateNotificationTemplate(NotificationTemplate notificationTemplate byte[] contentByteArray = getContentByteArray(notificationTemplate); int contentLength = contentByteArray.length; try (InputStream contentStream = new ByteArrayInputStream(contentByteArray)) { - namedJdbcTemplate.executeUpdate(UPDATE_APP_NOTIFICATION_TEMPLATE_SQL, + String updateAppNotificationTemplateSql = isUnicodeSupported ? UPDATE_APP_NOTIFICATION_TEMPLATE_SQL : + UPDATE_APP_NOTIFICATION_TEMPLATE_WITHOUT_UNICODE_SQL; + namedJdbcTemplate.executeUpdate(updateAppNotificationTemplateSql, preparedStatement -> { - preparedStatement.setBinaryStream(CONTENT, contentStream, contentLength); + if (isUnicodeSupported) { + preparedStatement.setBinaryStream(CONTENT, contentStream, contentLength); + } else { + preparedStatement.setString(SUBJECT, notificationTemplate.getSubject()); + preparedStatement.setString(BODY, notificationTemplate.getBody()); + preparedStatement.setString(FOOTER, notificationTemplate.getFooter()); + } preparedStatement.setString(CONTENT_TYPE, notificationTemplate.getContentType()); preparedStatement.setString(TEMPLATE_KEY, locale.toLowerCase()); preparedStatement.setString(TYPE_KEY, displayName.toLowerCase()); diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/OrgNotificationTemplateDAO.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/OrgNotificationTemplateDAO.java index fa8d0d01..4d808b65 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/OrgNotificationTemplateDAO.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/OrgNotificationTemplateDAO.java @@ -20,6 +20,7 @@ import org.wso2.carbon.database.utils.jdbc.NamedJdbcTemplate; import org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException; +import org.wso2.carbon.email.mgt.internal.I18nMgtDataHolder; 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; @@ -29,11 +30,14 @@ import java.io.InputStream; import java.util.List; +import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.BODY; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.CHANNEL; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.CONTENT; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.CONTENT_TYPE; +import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.FOOTER; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.ID; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.LOCALE; +import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.SUBJECT; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.TEMPLATE_KEY; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.TENANT_ID; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.TYPE_ID; @@ -42,10 +46,14 @@ import static org.wso2.carbon.email.mgt.constants.SQLConstants.DELETE_ORG_NOTIFICATION_TEMPLATE_SQL; import static org.wso2.carbon.email.mgt.constants.SQLConstants.GET_NOTIFICATION_TYPE_ID_SQL; import static org.wso2.carbon.email.mgt.constants.SQLConstants.GET_ORG_NOTIFICATION_TEMPLATE_SQL; +import static org.wso2.carbon.email.mgt.constants.SQLConstants.GET_ORG_NOTIFICATION_TEMPLATE_WITHOUT_UNICODE_SQL; import static org.wso2.carbon.email.mgt.constants.SQLConstants.INSERT_ORG_NOTIFICATION_TEMPLATE_SQL; +import static org.wso2.carbon.email.mgt.constants.SQLConstants.INSERT_ORG_NOTIFICATION_TEMPLATE_WITHOUT_UNICODE_SQL; import static org.wso2.carbon.email.mgt.constants.SQLConstants.IS_ORG_NOTIFICATION_TEMPLATE_EXISTS_SQL; import static org.wso2.carbon.email.mgt.constants.SQLConstants.LIST_ORG_NOTIFICATION_TEMPLATES_BY_TYPE_SQL; +import static org.wso2.carbon.email.mgt.constants.SQLConstants.LIST_ORG_NOTIFICATION_TEMPLATES_BY_TYPE_WITHOUT_UNICODE_SQL; import static org.wso2.carbon.email.mgt.constants.SQLConstants.UPDATE_ORG_NOTIFICATION_TEMPLATE_SQL; +import static org.wso2.carbon.email.mgt.constants.SQLConstants.UPDATE_ORG_NOTIFICATION_TEMPLATE_WITHOUT_UNICODE_SQL; import static org.wso2.carbon.email.mgt.util.I18nEmailUtil.getContentByteArray; import static org.wso2.carbon.email.mgt.util.I18nEmailUtil.setContent; @@ -54,6 +62,8 @@ */ public class OrgNotificationTemplateDAO { + private boolean isUnicodeSupported = I18nMgtDataHolder.getInstance().isUnicodeSupported(); + public void addNotificationTemplate(NotificationTemplate notificationTemplate, int tenantId) throws NotificationTemplateManagerServerException { @@ -65,10 +75,18 @@ public void addNotificationTemplate(NotificationTemplate notificationTemplate, i byte[] contentByteArray = getContentByteArray(notificationTemplate); int contentLength = contentByteArray.length; try (InputStream contentStream = new ByteArrayInputStream(contentByteArray)) { - namedJdbcTemplate.executeInsert(INSERT_ORG_NOTIFICATION_TEMPLATE_SQL, (preparedStatement -> { + String insertOrgNotificationTemplateSql = isUnicodeSupported ? INSERT_ORG_NOTIFICATION_TEMPLATE_SQL: + INSERT_ORG_NOTIFICATION_TEMPLATE_WITHOUT_UNICODE_SQL; + namedJdbcTemplate.executeInsert(insertOrgNotificationTemplateSql, (preparedStatement -> { preparedStatement.setString(TEMPLATE_KEY, locale.toLowerCase()); preparedStatement.setString(LOCALE, locale); - preparedStatement.setBinaryStream(CONTENT, contentStream, contentLength); + if (isUnicodeSupported) { + preparedStatement.setBinaryStream(CONTENT, contentStream, contentLength); + } else { + preparedStatement.setString(SUBJECT, notificationTemplate.getSubject()); + preparedStatement.setString(BODY, notificationTemplate.getBody()); + preparedStatement.setString(FOOTER, notificationTemplate.getFooter()); + } preparedStatement.setString(CONTENT_TYPE, notificationTemplate.getContentType()); preparedStatement.setString(TYPE_KEY, displayName.toLowerCase()); preparedStatement.setString(CHANNEL, channelName); @@ -93,10 +111,18 @@ public NotificationTemplate getNotificationTemplate(String locale, String templa NotificationTemplate notificationTemplate; try { - notificationTemplate = namedJdbcTemplate.fetchSingleRecord(GET_ORG_NOTIFICATION_TEMPLATE_SQL, + String getOrgNotificationTemplateSql = isUnicodeSupported ? GET_ORG_NOTIFICATION_TEMPLATE_SQL : + GET_ORG_NOTIFICATION_TEMPLATE_WITHOUT_UNICODE_SQL; + notificationTemplate = namedJdbcTemplate.fetchSingleRecord(getOrgNotificationTemplateSql, (resultSet, rowNumber) -> { NotificationTemplate notificationTemplateResult = new NotificationTemplate(); - setContent(resultSet.getBinaryStream(CONTENT), notificationTemplateResult); + if (isUnicodeSupported) { + setContent(resultSet.getBinaryStream(CONTENT), notificationTemplateResult); + } else { + notificationTemplateResult.setSubject(resultSet.getString(SUBJECT)); + notificationTemplateResult.setBody(resultSet.getString(BODY)); + notificationTemplateResult.setFooter(resultSet.getString(FOOTER)); + } notificationTemplateResult.setContentType(resultSet.getString(CONTENT_TYPE)); notificationTemplateResult.setLocale(locale); notificationTemplateResult.setType(templateType); @@ -163,10 +189,19 @@ public List listNotificationTemplates(String templateType, List notificationTemplates; try { - notificationTemplates = namedJdbcTemplate.executeQuery(LIST_ORG_NOTIFICATION_TEMPLATES_BY_TYPE_SQL, + String listOrgNotificationTemplatesByTypeSql = + isUnicodeSupported ? LIST_ORG_NOTIFICATION_TEMPLATES_BY_TYPE_SQL : + LIST_ORG_NOTIFICATION_TEMPLATES_BY_TYPE_WITHOUT_UNICODE_SQL; + notificationTemplates = namedJdbcTemplate.executeQuery(listOrgNotificationTemplatesByTypeSql, (resultSet, rowNumber) -> { NotificationTemplate notificationTemplateResult = new NotificationTemplate(); - setContent(resultSet.getBinaryStream(CONTENT), notificationTemplateResult); + if (isUnicodeSupported) { + setContent(resultSet.getBinaryStream(CONTENT), notificationTemplateResult); + } else { + notificationTemplateResult.setSubject(resultSet.getString(SUBJECT)); + notificationTemplateResult.setBody(resultSet.getString(BODY)); + notificationTemplateResult.setFooter(resultSet.getString(FOOTER)); + } notificationTemplateResult.setContentType(resultSet.getString(CONTENT_TYPE)); notificationTemplateResult.setLocale(resultSet.getString(LOCALE)); notificationTemplateResult.setType(templateType.toLowerCase()); @@ -200,9 +235,17 @@ public void updateNotificationTemplate(NotificationTemplate notificationTemplate byte[] contentByteArray = getContentByteArray(notificationTemplate); int contentLength = contentByteArray.length; try (InputStream contentStream = new ByteArrayInputStream(contentByteArray)) { - namedJdbcTemplate.executeUpdate(UPDATE_ORG_NOTIFICATION_TEMPLATE_SQL, + String updateOrgNotificationTemplateSql = isUnicodeSupported ? UPDATE_ORG_NOTIFICATION_TEMPLATE_SQL : + UPDATE_ORG_NOTIFICATION_TEMPLATE_WITHOUT_UNICODE_SQL; + namedJdbcTemplate.executeUpdate(updateOrgNotificationTemplateSql, preparedStatement -> { - preparedStatement.setBinaryStream(CONTENT, contentStream, contentLength); + if (isUnicodeSupported) { + preparedStatement.setBinaryStream(CONTENT, contentStream, contentLength); + } else { + preparedStatement.setString(SUBJECT, notificationTemplate.getSubject()); + preparedStatement.setString(BODY, notificationTemplate.getBody()); + preparedStatement.setString(FOOTER, notificationTemplate.getFooter()); + } preparedStatement.setString(CONTENT_TYPE, notificationTemplate.getContentType()); preparedStatement.setString(TEMPLATE_KEY, locale.toLowerCase()); preparedStatement.setString(TYPE_KEY, displayName.toLowerCase());