Skip to content

Commit

Permalink
Merge pull request #290 from darshanasbg/unicode-support-config
Browse files Browse the repository at this point in the history
Add support to on-demand migrate notification template content to support unicode
  • Loading branch information
darshanasbg authored Dec 3, 2024
2 parents 86690e6 + 8172453 commit 0474b45
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public class SQLConstants {
"(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 INSERT_ORG_NOTIFICATION_TEMPLATE_HYBRID_SQL =
"INSERT INTO IDN_NOTIFICATION_ORG_TEMPLATE " +
"(TEMPLATE_KEY, LOCALE, CONTENT, SUBJECT, BODY, FOOTER, CONTENT_TYPE, TYPE_ID, TENANT_ID) " +
"VALUES (:TEMPLATE_KEY;, :LOCALE;, :CONTENT;, :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 +
Expand All @@ -59,6 +64,10 @@ public class SQLConstants {
"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 GET_ORG_NOTIFICATION_TEMPLATE_HYBRID_SQL =
"SELECT CONTENT, 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;";
Expand All @@ -68,6 +77,9 @@ public class SQLConstants {
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 LIST_ORG_NOTIFICATION_TEMPLATES_BY_TYPE_HYBRID_SQL =
"SELECT CONTENT, 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; " +
Expand All @@ -78,6 +90,11 @@ public class SQLConstants {
"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 UPDATE_ORG_NOTIFICATION_TEMPLATE_HYBRID_SQL =
"UPDATE IDN_NOTIFICATION_ORG_TEMPLATE " +
"SET CONTENT = :CONTENT;, 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;";
Expand All @@ -96,6 +113,11 @@ public class SQLConstants {
"(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 INSERT_APP_NOTIFICATION_TEMPLATE_HYBRID_SQL =
"INSERT INTO IDN_NOTIFICATION_APP_TEMPLATE " +
"(TEMPLATE_KEY, LOCALE, CONTENT, SUBJECT, BODY, FOOTER, CONTENT_TYPE, TYPE_ID, APP_ID, TENANT_ID) " +
"VALUES (:TEMPLATE_KEY;, :LOCALE;, :CONTENT;, :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 +
Expand All @@ -104,6 +126,10 @@ public class SQLConstants {
"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 GET_APP_NOTIFICATION_TEMPLATE_HYBRID_SQL =
"SELECT CONTENT, 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; " +
Expand All @@ -116,6 +142,10 @@ public class SQLConstants {
"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 LIST_APP_NOTIFICATION_TEMPLATES_BY_APP_HYBRID_SQL =
"SELECT CONTENT, 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; " +
Expand All @@ -126,6 +156,11 @@ public class SQLConstants {
"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 UPDATE_APP_NOTIFICATION_TEMPLATE_HYBRID_SQL =
"UPDATE IDN_NOTIFICATION_APP_TEMPLATE " +
"SET CONTENT = :CONTENT;, 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;";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class I18nMgtDataHolder{
private List<NotificationTemplate> defaultSMSTemplates = new ArrayList<>();
private List<String> legacyTenants = new ArrayList<>();
private boolean isUnicodeSupported = false;
private boolean isUnicodeSupportedInHybridMode = false;

private static I18nMgtDataHolder instance = new I18nMgtDataHolder();

Expand Down Expand Up @@ -181,4 +182,24 @@ public boolean isUnicodeSupported() {

return isUnicodeSupported;
}

/**
* Sets whether the unicode hybrid support for template content is enabled or not.
*
* @param isUnicodeSupportedInHybridMode true to enable Unicode support in hybrid mode, false to disable.
*/
public void setHybrid(boolean isUnicodeSupportedInHybridMode) {

this.isUnicodeSupportedInHybridMode = isUnicodeSupportedInHybridMode;
}

/**
* Gets whether the unicode hybrid support for template content is enabled.
*
* @return true if the unicode hybrid support for template content is enabled.
*/
public boolean isHybrid() {

return isUnicodeSupportedInHybridMode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ protected void activate(ComponentContext context) {
List<String> 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));
String unicodeSupportType = IdentityUtil.getProperty(NOTIFICATION_TEMPLATES_ENABLE_UNICODE_SUPPORT);
I18nMgtDataHolder.getInstance().setUnicodeSupport(Boolean.parseBoolean(unicodeSupportType));
I18nMgtDataHolder.getInstance().setHybrid("hybrid".equalsIgnoreCase(unicodeSupportType));

// Register Email Mgt Service as an OSGi service.
EmailTemplateManagerImpl emailTemplateManager = new EmailTemplateManagerImpl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,18 @@
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_HYBRID_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_HYBRID_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_HYBRID_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_HYBRID_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;
Expand All @@ -65,6 +69,7 @@
public class AppNotificationTemplateDAO {

private boolean isUnicodeSupported = I18nMgtDataHolder.getInstance().isUnicodeSupported();
private boolean isHybrid = I18nMgtDataHolder.getInstance().isHybrid();

public void addNotificationTemplate(NotificationTemplate notificationTemplate, String applicationUuid, int tenantId)
throws NotificationTemplateManagerServerException {
Expand All @@ -78,12 +83,18 @@ public void addNotificationTemplate(NotificationTemplate notificationTemplate, S
int contentLength = contentByteArray.length;
try (InputStream contentStream = new ByteArrayInputStream(contentByteArray)) {
String insertAppNotificationTemplateSql = isUnicodeSupported ? INSERT_APP_NOTIFICATION_TEMPLATE_SQL :
INSERT_APP_NOTIFICATION_TEMPLATE_WITHOUT_UNICODE_SQL;
isHybrid ? INSERT_APP_NOTIFICATION_TEMPLATE_HYBRID_SQL :
INSERT_APP_NOTIFICATION_TEMPLATE_WITHOUT_UNICODE_SQL;
namedJdbcTemplate.executeInsert(insertAppNotificationTemplateSql, (preparedStatement -> {
preparedStatement.setString(TEMPLATE_KEY, locale.toLowerCase());
preparedStatement.setString(LOCALE, locale);
if (isUnicodeSupported) {
preparedStatement.setBinaryStream(CONTENT, contentStream, contentLength);
} else if (isHybrid) {
preparedStatement.setBinaryStream(CONTENT, contentStream, contentLength);
preparedStatement.setString(SUBJECT, notificationTemplate.getSubject());
preparedStatement.setString(BODY, notificationTemplate.getBody());
preparedStatement.setString(FOOTER, notificationTemplate.getFooter());
} else {
preparedStatement.setString(SUBJECT, notificationTemplate.getSubject());
preparedStatement.setString(BODY, notificationTemplate.getBody());
Expand Down Expand Up @@ -115,12 +126,22 @@ public NotificationTemplate getNotificationTemplate(String locale, String templa

try {
String getAppNotificationTemplateSql = isUnicodeSupported ? GET_APP_NOTIFICATION_TEMPLATE_SQL :
GET_APP_NOTIFICATION_TEMPLATE_WITHOUT_UNICODE_SQL;
isHybrid ? GET_APP_NOTIFICATION_TEMPLATE_HYBRID_SQL :
GET_APP_NOTIFICATION_TEMPLATE_WITHOUT_UNICODE_SQL;
notificationTemplate = namedJdbcTemplate.fetchSingleRecord(getAppNotificationTemplateSql,
(resultSet, rowNumber) -> {
NotificationTemplate notificationTemplateResult = new NotificationTemplate();
if (isUnicodeSupported) {
setContent(resultSet.getBinaryStream(CONTENT), notificationTemplateResult);
} else if (isHybrid) {
setContent(resultSet.getBinaryStream(CONTENT), notificationTemplateResult);
if (notificationTemplateResult.getSubject() == null
&& notificationTemplateResult.getBody() == null
&& notificationTemplateResult.getFooter() == null) {
notificationTemplateResult.setSubject(resultSet.getString(SUBJECT));
notificationTemplateResult.setBody(resultSet.getString(BODY));
notificationTemplateResult.setFooter(resultSet.getString(FOOTER));
}
} else {
notificationTemplateResult.setSubject(resultSet.getString(SUBJECT));
notificationTemplateResult.setBody(resultSet.getString(BODY));
Expand Down Expand Up @@ -198,12 +219,22 @@ public List<NotificationTemplate> listNotificationTemplates(String templateType,
try {
String listAppNotificationTemplatesByAppSql =
isUnicodeSupported ? LIST_APP_NOTIFICATION_TEMPLATES_BY_APP_SQL :
LIST_APP_NOTIFICATION_TEMPLATES_BY_APP_WITHOUT_UNICODE_SQL;
isHybrid ? LIST_APP_NOTIFICATION_TEMPLATES_BY_APP_HYBRID_SQL :
LIST_APP_NOTIFICATION_TEMPLATES_BY_APP_WITHOUT_UNICODE_SQL;
notificationTemplates = namedJdbcTemplate.executeQuery(listAppNotificationTemplatesByAppSql,
(resultSet, rowNumber) -> {
NotificationTemplate notificationTemplateResult = new NotificationTemplate();
if (isUnicodeSupported) {
setContent(resultSet.getBinaryStream(CONTENT), notificationTemplateResult);
} else if (isHybrid) {
setContent(resultSet.getBinaryStream(CONTENT), notificationTemplateResult);
if (notificationTemplateResult.getSubject() == null
&& notificationTemplateResult.getBody() == null
&& notificationTemplateResult.getFooter() == null) {
notificationTemplateResult.setSubject(resultSet.getString(SUBJECT));
notificationTemplateResult.setBody(resultSet.getString(BODY));
notificationTemplateResult.setFooter(resultSet.getString(FOOTER));
}
} else {
notificationTemplateResult.setSubject(resultSet.getString(SUBJECT));
notificationTemplateResult.setBody(resultSet.getString(BODY));
Expand Down Expand Up @@ -244,11 +275,17 @@ public void updateNotificationTemplate(NotificationTemplate notificationTemplate
int contentLength = contentByteArray.length;
try (InputStream contentStream = new ByteArrayInputStream(contentByteArray)) {
String updateAppNotificationTemplateSql = isUnicodeSupported ? UPDATE_APP_NOTIFICATION_TEMPLATE_SQL :
UPDATE_APP_NOTIFICATION_TEMPLATE_WITHOUT_UNICODE_SQL;
isHybrid ? UPDATE_APP_NOTIFICATION_TEMPLATE_HYBRID_SQL :
UPDATE_APP_NOTIFICATION_TEMPLATE_WITHOUT_UNICODE_SQL;
namedJdbcTemplate.executeUpdate(updateAppNotificationTemplateSql,
preparedStatement -> {
if (isUnicodeSupported) {
preparedStatement.setBinaryStream(CONTENT, contentStream, contentLength);
} else if (isHybrid) {
preparedStatement.setBinaryStream(CONTENT, contentStream, contentLength);
preparedStatement.setString(SUBJECT, notificationTemplate.getSubject());
preparedStatement.setString(BODY, notificationTemplate.getBody());
preparedStatement.setString(FOOTER, notificationTemplate.getFooter());
} else {
preparedStatement.setString(SUBJECT, notificationTemplate.getSubject());
preparedStatement.setString(BODY, notificationTemplate.getBody());
Expand Down
Loading

0 comments on commit 0474b45

Please sign in to comment.