From ded1925a6d364500e203b5200857b9051dc3dc5c Mon Sep 17 00:00:00 2001 From: Darshana Gunawardana Date: Thu, 7 Nov 2024 10:11:54 +0530 Subject: [PATCH] Add unicode data type support for notification templates --- .../email/mgt/constants/I18nMgtConstants.java | 2 + .../email/mgt/constants/SQLConstants.java | 43 +++++++++ .../email/mgt/internal/I18nMgtDataHolder.java | 21 +++++ .../mgt/internal/I18nMgtServiceComponent.java | 8 +- .../store/dao/AppNotificationTemplateDAO.java | 91 ++++++++++++++----- .../store/dao/OrgNotificationTemplateDAO.java | 90 +++++++++++++----- pom.xml | 4 +- 7 files changed, 204 insertions(+), 55 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 e2fcd390..76b479f5 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_USE_UNICODE_DATA_TYPES = + "NotificationTemplates.UseUnicodeDataTypes"; public static final String SERVICE_PROPERTY_KEY_SERVICE_NAME = "service.name"; public static final String SERVICE_PROPERTY_VAL_EMAIL_TEMPLATE_MANAGER = "EmailTemplateManager"; 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 97852514..194aeb20 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 @@ -68,6 +68,27 @@ public class SQLConstants { "DELETE FROM IDN_NOTIFICATION_ORG_TEMPLATE WHERE TYPE_ID = (" + GET_NOTIFICATION_TYPE_ID_SQL + ") AND TENANT_ID = :TENANT_ID;"; + // sql constants for org notification template with unicode datatypes + public static final String INSERT_ORG_NOTIFICATION_TEMPLATE_SQL_UNICODE = + "INSERT INTO IDN_NOTIFICATION_ORG_TEMPLATE " + + "(TEMPLATE_KEY, LOCALE, NSUBJECT, NBODY, NFOOTER, 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_UNICODE = + "SELECT NSUBJECT AS SUBJECT, NBODY AS BODY, NFOOTER AS 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 LIST_ORG_NOTIFICATION_TEMPLATES_BY_TYPE_SQL_UNICODE = + "SELECT NSUBJECT AS SUBJECT, NBODY AS BODY, NFOOTER AS 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_UNICODE = + "UPDATE IDN_NOTIFICATION_ORG_TEMPLATE " + + "SET NSUBJECT = :SUBJECT;, NBODY = :BODY;, NFOOTER = :FOOTER;, CONTENT_TYPE = :CONTENT_TYPE; " + + "WHERE TEMPLATE_KEY = :TEMPLATE_KEY; AND TYPE_ID = (" + GET_NOTIFICATION_TYPE_ID_SQL + + ") AND TENANT_ID = :TENANT_ID;"; + // sql constants for app notification template public static final String INSERT_APP_NOTIFICATION_TEMPLATE_SQL = "INSERT INTO IDN_NOTIFICATION_APP_TEMPLATE " + @@ -100,4 +121,26 @@ public class SQLConstants { 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;"; + + // sql constants for org notification template with unicode datatypes + public static final String INSERT_APP_NOTIFICATION_TEMPLATE_SQL_UNICODE = + "INSERT INTO IDN_NOTIFICATION_APP_TEMPLATE " + + "(TEMPLATE_KEY, LOCALE, NSUBJECT, NBODY, NFOOTER, 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_UNICODE = + "SELECT NSUBJECT AS SUBJECT, NBODY AS BODY, NFOOTER AS 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 LIST_APP_NOTIFICATION_TEMPLATES_BY_APP_SQL_UNICODE = + "SELECT NSUBJECT AS SUBJECT, NBODY AS BODY, NFOOTER AS 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_UNICODE = + "UPDATE IDN_NOTIFICATION_APP_TEMPLATE " + + "SET NSUBJECT = :SUBJECT;, NBODY = :BODY;, NFOOTER = :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;"; } 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..41425101 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 useUnicodeDataTypes; private static I18nMgtDataHolder instance = new I18nMgtDataHolder(); @@ -160,4 +161,24 @@ public List getLegacyTenants() { return legacyTenants; } + + /** + * Sets whether the database should use native Unicode data types (e.g., NVARCHAR, NCHAR). + * + * @param useUnicodeDataTypes true to enable Unicode data types, false to disable. + */ + public void setUseUnicodeDataTypes(boolean useUnicodeDataTypes) { + + this.useUnicodeDataTypes = useUnicodeDataTypes; + } + + /** + * Gets whether the database uses native Unicode data types (e.g., NVARCHAR, NCHAR). + * + * @return true if the database uses Unicode data types; false otherwise. + */ + public boolean isUseUnicodeDataTypes() { + + return useUnicodeDataTypes; + } } 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 93b356bb..696112d6 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 @@ -25,25 +25,21 @@ import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceRegistration; import org.osgi.service.component.ComponentContext; -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; -import org.wso2.carbon.email.mgt.exceptions.I18nEmailMgtException; import org.wso2.carbon.email.mgt.model.SMSProviderTemplate; import org.wso2.carbon.identity.application.mgt.ApplicationManagementService; import org.wso2.carbon.identity.core.persistence.registry.RegistryResourceMgtService; -import org.wso2.carbon.identity.governance.exceptions.notiification.NotificationTemplateManagerException; import org.wso2.carbon.identity.core.util.IdentityUtil; 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.service.OrganizationManager; import org.wso2.carbon.registry.core.service.RegistryService; -import org.wso2.carbon.stratos.common.listeners.TenantMgtListener; import org.wso2.carbon.user.core.service.RealmService; import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; @@ -72,6 +68,7 @@ import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; +import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NOTIFICATION_TEMPLATES_USE_UNICODE_DATA_TYPES; 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; @@ -103,6 +100,9 @@ protected void activate(ComponentContext context) { List legacyTenants = IdentityUtil.getPropertyAsList(NOTIFICATION_TEMPLATES_LEGACY_TENANTS); I18nMgtDataHolder.getInstance().setLegacyTenants(legacyTenants); + String useUnicodeDataTypesProp = IdentityUtil.getProperty(NOTIFICATION_TEMPLATES_USE_UNICODE_DATA_TYPES); + I18nMgtDataHolder.getInstance().setUseUnicodeDataTypes(Boolean.parseBoolean(useUnicodeDataTypesProp)); + // 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 03191064..9da1db52 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; @@ -42,17 +43,23 @@ 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_SQL_UNICODE; 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_SQL_UNICODE; 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_SQL_UNICODE; 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_SQL_UNICODE; /** * This class is to perform CRUD operations for Application NotificationTemplates. */ public class AppNotificationTemplateDAO { + private boolean useUnicodeDataTypes = I18nMgtDataHolder.getInstance().isUseUnicodeDataTypes(); + public void addNotificationTemplate(NotificationTemplate notificationTemplate, String applicationUuid, int tenantId) throws NotificationTemplateManagerServerException { @@ -62,12 +69,21 @@ public void addNotificationTemplate(NotificationTemplate notificationTemplate, S NamedJdbcTemplate namedJdbcTemplate = JdbcUtils.getNewNamedJdbcTemplate(); try { - namedJdbcTemplate.executeInsert(INSERT_APP_NOTIFICATION_TEMPLATE_SQL, (preparedStatement -> { + String insertAppNotificationTemplateSql = + useUnicodeDataTypes ? INSERT_APP_NOTIFICATION_TEMPLATE_SQL_UNICODE : + INSERT_APP_NOTIFICATION_TEMPLATE_SQL; + namedJdbcTemplate.executeInsert(insertAppNotificationTemplateSql, (preparedStatement -> { preparedStatement.setString(TEMPLATE_KEY, locale.toLowerCase()); preparedStatement.setString(LOCALE, locale); - preparedStatement.setString(SUBJECT, notificationTemplate.getSubject()); - preparedStatement.setString(BODY, notificationTemplate.getBody()); - preparedStatement.setString(FOOTER, notificationTemplate.getFooter()); + if (useUnicodeDataTypes) { + preparedStatement.setNString(SUBJECT, notificationTemplate.getSubject()); + preparedStatement.setNString(BODY, notificationTemplate.getBody()); + preparedStatement.setNString(FOOTER, notificationTemplate.getFooter()); + } 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); @@ -91,12 +107,20 @@ public NotificationTemplate getNotificationTemplate(String locale, String templa NotificationTemplate notificationTemplate; try { - notificationTemplate = namedJdbcTemplate.fetchSingleRecord(GET_APP_NOTIFICATION_TEMPLATE_SQL, + String getAppNotificationTemplateSql = + useUnicodeDataTypes ? GET_APP_NOTIFICATION_TEMPLATE_SQL_UNICODE : GET_APP_NOTIFICATION_TEMPLATE_SQL; + notificationTemplate = namedJdbcTemplate.fetchSingleRecord(getAppNotificationTemplateSql, (resultSet, rowNumber) -> { NotificationTemplate notificationTemplateResult = new NotificationTemplate(); - notificationTemplateResult.setSubject(resultSet.getString(SUBJECT)); - notificationTemplateResult.setBody(resultSet.getString(BODY)); - notificationTemplateResult.setFooter(resultSet.getString(FOOTER)); + if (useUnicodeDataTypes) { + notificationTemplateResult.setSubject(resultSet.getNString(SUBJECT)); + notificationTemplateResult.setBody(resultSet.getNString(BODY)); + notificationTemplateResult.setFooter(resultSet.getNString(FOOTER)); + } 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); @@ -173,12 +197,21 @@ public List listNotificationTemplates(String templateType, List notificationTemplates; try { - notificationTemplates = namedJdbcTemplate.executeQuery(LIST_APP_NOTIFICATION_TEMPLATES_BY_APP_SQL, + String listAppNotificationTemplatesByAppSql = + useUnicodeDataTypes ? LIST_APP_NOTIFICATION_TEMPLATES_BY_APP_SQL_UNICODE : + LIST_APP_NOTIFICATION_TEMPLATES_BY_APP_SQL; + notificationTemplates = namedJdbcTemplate.executeQuery(listAppNotificationTemplatesByAppSql, (resultSet, rowNumber) -> { NotificationTemplate notificationTemplateResult = new NotificationTemplate(); - notificationTemplateResult.setSubject(resultSet.getString(SUBJECT)); - notificationTemplateResult.setBody(resultSet.getString(BODY)); - notificationTemplateResult.setFooter(resultSet.getString(FOOTER)); + if (useUnicodeDataTypes) { + notificationTemplateResult.setSubject(resultSet.getNString(SUBJECT)); + notificationTemplateResult.setBody(resultSet.getNString(BODY)); + notificationTemplateResult.setFooter(resultSet.getNString(FOOTER)); + } 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()); @@ -211,19 +244,27 @@ public void updateNotificationTemplate(NotificationTemplate notificationTemplate NamedJdbcTemplate namedJdbcTemplate = JdbcUtils.getNewNamedJdbcTemplate(); try { - namedJdbcTemplate.executeUpdate(UPDATE_APP_NOTIFICATION_TEMPLATE_SQL, - preparedStatement -> { - 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()); - preparedStatement.setString(CHANNEL, channelName); - preparedStatement.setInt(TENANT_ID, tenantId); - preparedStatement.setString(APP_ID, applicationUuid); - preparedStatement.setInt(TENANT_ID, tenantId); - }); + String updateAppNotificationTemplateSql = + useUnicodeDataTypes ? UPDATE_APP_NOTIFICATION_TEMPLATE_SQL_UNICODE : + UPDATE_APP_NOTIFICATION_TEMPLATE_SQL; + namedJdbcTemplate.executeUpdate(updateAppNotificationTemplateSql, preparedStatement -> { + if (useUnicodeDataTypes) { + preparedStatement.setNString(SUBJECT, notificationTemplate.getSubject()); + preparedStatement.setNString(BODY, notificationTemplate.getBody()); + preparedStatement.setNString(FOOTER, notificationTemplate.getFooter()); + } 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()); + preparedStatement.setString(CHANNEL, channelName); + preparedStatement.setInt(TENANT_ID, tenantId); + preparedStatement.setString(APP_ID, applicationUuid); + preparedStatement.setInt(TENANT_ID, tenantId); + }); } catch (DataAccessException e) { String error = String.format("Error while updating %s template %s of type %s from application %s in %s tenant.", 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 d14c0699..ddddb7d7 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; @@ -41,16 +42,22 @@ 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_SQL_UNICODE; 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_SQL_UNICODE; 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_SQL_UNICODE; 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_SQL_UNICODE; /** * This class is to perform CRUD operations for Org NotificationTemplates. */ public class OrgNotificationTemplateDAO { + private boolean useUnicodeDataTypes = I18nMgtDataHolder.getInstance().isUseUnicodeDataTypes(); + public void addNotificationTemplate(NotificationTemplate notificationTemplate, int tenantId) throws NotificationTemplateManagerServerException { @@ -60,12 +67,21 @@ public void addNotificationTemplate(NotificationTemplate notificationTemplate, i NamedJdbcTemplate namedJdbcTemplate = JdbcUtils.getNewNamedJdbcTemplate(); try { - namedJdbcTemplate.executeInsert(INSERT_ORG_NOTIFICATION_TEMPLATE_SQL, (preparedStatement -> { + String insertOrgNotificationTemplateSql = + useUnicodeDataTypes ? INSERT_ORG_NOTIFICATION_TEMPLATE_SQL_UNICODE : + INSERT_ORG_NOTIFICATION_TEMPLATE_SQL; + namedJdbcTemplate.executeInsert(insertOrgNotificationTemplateSql, (preparedStatement -> { preparedStatement.setString(TEMPLATE_KEY, locale.toLowerCase()); preparedStatement.setString(LOCALE, locale); - preparedStatement.setString(SUBJECT, notificationTemplate.getSubject()); - preparedStatement.setString(BODY, notificationTemplate.getBody()); - preparedStatement.setString(FOOTER, notificationTemplate.getFooter()); + if (useUnicodeDataTypes) { + preparedStatement.setNString(SUBJECT, notificationTemplate.getSubject()); + preparedStatement.setNString(BODY, notificationTemplate.getBody()); + preparedStatement.setNString(FOOTER, notificationTemplate.getFooter()); + } 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); @@ -88,12 +104,20 @@ public NotificationTemplate getNotificationTemplate(String locale, String templa NotificationTemplate notificationTemplate; try { - notificationTemplate = namedJdbcTemplate.fetchSingleRecord(GET_ORG_NOTIFICATION_TEMPLATE_SQL, + String getOrgNotificationTemplateSql = + useUnicodeDataTypes ? GET_ORG_NOTIFICATION_TEMPLATE_SQL_UNICODE : GET_ORG_NOTIFICATION_TEMPLATE_SQL; + notificationTemplate = namedJdbcTemplate.fetchSingleRecord(getOrgNotificationTemplateSql, (resultSet, rowNumber) -> { NotificationTemplate notificationTemplateResult = new NotificationTemplate(); - notificationTemplateResult.setSubject(resultSet.getString(SUBJECT)); - notificationTemplateResult.setBody(resultSet.getString(BODY)); - notificationTemplateResult.setFooter(resultSet.getString(FOOTER)); + if (useUnicodeDataTypes) { + notificationTemplateResult.setSubject(resultSet.getNString(SUBJECT)); + notificationTemplateResult.setBody(resultSet.getNString(BODY)); + notificationTemplateResult.setFooter(resultSet.getNString(FOOTER)); + } 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); @@ -166,12 +190,21 @@ public List listNotificationTemplates(String templateType, List notificationTemplates; try { - notificationTemplates = namedJdbcTemplate.executeQuery(LIST_ORG_NOTIFICATION_TEMPLATES_BY_TYPE_SQL, + String listOrgNotificationTemplatesByTypeSql = + useUnicodeDataTypes ? LIST_ORG_NOTIFICATION_TEMPLATES_BY_TYPE_SQL_UNICODE : + LIST_ORG_NOTIFICATION_TEMPLATES_BY_TYPE_SQL; + notificationTemplates = namedJdbcTemplate.executeQuery(listOrgNotificationTemplatesByTypeSql, (resultSet, rowNumber) -> { NotificationTemplate notificationTemplateResult = new NotificationTemplate(); - notificationTemplateResult.setSubject(resultSet.getString(SUBJECT)); - notificationTemplateResult.setBody(resultSet.getString(BODY)); - notificationTemplateResult.setFooter(resultSet.getString(FOOTER)); + if (useUnicodeDataTypes) { + notificationTemplateResult.setSubject(resultSet.getNString(SUBJECT)); + notificationTemplateResult.setBody(resultSet.getNString(BODY)); + notificationTemplateResult.setFooter(resultSet.getNString(FOOTER)); + } 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()); @@ -203,18 +236,27 @@ public void updateNotificationTemplate(NotificationTemplate notificationTemplate NamedJdbcTemplate namedJdbcTemplate = JdbcUtils.getNewNamedJdbcTemplate(); try { - namedJdbcTemplate.executeUpdate(UPDATE_ORG_NOTIFICATION_TEMPLATE_SQL, - preparedStatement -> { - 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()); - preparedStatement.setString(CHANNEL, channelName); - preparedStatement.setInt(TENANT_ID, tenantId); - preparedStatement.setInt(TENANT_ID, tenantId); - }); + String updateOrgNotificationTemplateSql = + useUnicodeDataTypes ? UPDATE_ORG_NOTIFICATION_TEMPLATE_SQL_UNICODE : + UPDATE_ORG_NOTIFICATION_TEMPLATE_SQL; + namedJdbcTemplate.executeUpdate(updateOrgNotificationTemplateSql, preparedStatement -> { + if (useUnicodeDataTypes) { + preparedStatement.setNString(SUBJECT, notificationTemplate.getSubject()); + preparedStatement.setNString(BODY, notificationTemplate.getBody()); + preparedStatement.setNString(FOOTER, notificationTemplate.getFooter()); + } 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()); + preparedStatement.setString(CHANNEL, channelName); + preparedStatement.setInt(TENANT_ID, tenantId); + preparedStatement.setInt(TENANT_ID, tenantId); + }); } catch (DataAccessException e) { String error = String.format("Error while updating %s template %s of type %s from %s tenant.", channelName, locale, diff --git a/pom.xml b/pom.xml index d4b72d7b..a0317615 100644 --- a/pom.xml +++ b/pom.xml @@ -518,8 +518,8 @@ [4.7.11, 5.0.0) - 2.1.0 - [2.1.0,3.0.0) + 2.2.1 + [2.2.1,3.0.0) 7.1.0