Skip to content

Commit

Permalink
Add unicode data type support for app templates
Browse files Browse the repository at this point in the history
  • Loading branch information
darshanasbg committed Nov 7, 2024
1 parent 394e6e4 commit 2f44d8b
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,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;";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 isUnicodeDataType = I18nMgtDataHolder.getInstance().isUnicodeDataType();

public void addNotificationTemplate(NotificationTemplate notificationTemplate, String applicationUuid, int tenantId)
throws NotificationTemplateManagerServerException {

Expand All @@ -62,12 +69,20 @@ public void addNotificationTemplate(NotificationTemplate notificationTemplate, S

NamedJdbcTemplate namedJdbcTemplate = JdbcUtils.getNewNamedJdbcTemplate();
try {
namedJdbcTemplate.executeInsert(INSERT_APP_NOTIFICATION_TEMPLATE_SQL, (preparedStatement -> {
String insertAppNotificationTemplateSql = isUnicodeDataType ? 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 (isUnicodeDataType) {
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);
Expand All @@ -91,12 +106,20 @@ public NotificationTemplate getNotificationTemplate(String locale, String templa
NotificationTemplate notificationTemplate;

try {
notificationTemplate = namedJdbcTemplate.fetchSingleRecord(GET_APP_NOTIFICATION_TEMPLATE_SQL,
String getAppNotificationTemplateSql =
isUnicodeDataType ? 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 (isUnicodeDataType) {
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);
Expand Down Expand Up @@ -173,12 +196,21 @@ public List<NotificationTemplate> listNotificationTemplates(String templateType,
List<NotificationTemplate> notificationTemplates;

try {
notificationTemplates = namedJdbcTemplate.executeQuery(LIST_APP_NOTIFICATION_TEMPLATES_BY_APP_SQL,
String listAppNotificationTemplatesByAppSql =
isUnicodeDataType ? 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 (isUnicodeDataType) {
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());
Expand Down Expand Up @@ -211,19 +243,26 @@ 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 = isUnicodeDataType ? UPDATE_APP_NOTIFICATION_TEMPLATE_SQL_UNICODE :
UPDATE_APP_NOTIFICATION_TEMPLATE_SQL;
namedJdbcTemplate.executeUpdate(updateAppNotificationTemplateSql, preparedStatement -> {
if (isUnicodeDataType) {
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.",
Expand Down

0 comments on commit 2f44d8b

Please sign in to comment.