Skip to content

Commit

Permalink
Add unicode data type support for org templates
Browse files Browse the repository at this point in the history
  • Loading branch information
darshanasbg committed Nov 7, 2024
1 parent a00b682 commit 394e6e4
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ 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_IS_UNICODE_DATA_TYPE = "NotificationTemplates.IsUnicodeDataType";

public static final String SERVICE_PROPERTY_KEY_SERVICE_NAME = "service.name";
public static final String SERVICE_PROPERTY_VAL_EMAIL_TEMPLATE_MANAGER = "EmailTemplateManager";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class I18nMgtDataHolder{
private List<NotificationTemplate> defaultEmailTemplates = new ArrayList<>();
private List<NotificationTemplate> defaultSMSTemplates = new ArrayList<>();
private List<String> legacyTenants = new ArrayList<>();
private boolean isUnicodeDataType;

private static I18nMgtDataHolder instance = new I18nMgtDataHolder();

Expand Down Expand Up @@ -160,4 +161,24 @@ public List<String> getLegacyTenants() {

return legacyTenants;
}

/**
* Set the unicode data type status.
*
* @param isUnicodeDataType Unicode data type status.
*/
public void setUnicodeDataType(boolean isUnicodeDataType) {

this.isUnicodeDataType = isUnicodeDataType;
}

/**
* Get the unicode data type status.
*
* @return Unicode data type status.
*/
public boolean isUnicodeDataType() {

return isUnicodeDataType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NOTIFICATION_TEMPLATES_IS_UNICODE_DATA_TYPE;
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;
Expand Down Expand Up @@ -103,6 +104,9 @@ protected void activate(ComponentContext context) {
List<String> legacyTenants = IdentityUtil.getPropertyAsList(NOTIFICATION_TEMPLATES_LEGACY_TENANTS);
I18nMgtDataHolder.getInstance().setLegacyTenants(legacyTenants);

String isUnicodeDataTypeProp = IdentityUtil.getProperty(NOTIFICATION_TEMPLATES_IS_UNICODE_DATA_TYPE);
I18nMgtDataHolder.getInstance().setUnicodeDataType(Boolean.parseBoolean(isUnicodeDataTypeProp));

// Register Email Mgt Service as an OSGi service.
EmailTemplateManagerImpl emailTemplateManager = new EmailTemplateManagerImpl();
ServiceRegistration emailTemplateSR = bundleCtx.registerService(EmailTemplateManager.class.getName(),
Expand Down
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 @@ -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 isUnicodeDataType = I18nMgtDataHolder.getInstance().isUnicodeDataType();

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

Expand All @@ -60,12 +67,20 @@ public void addNotificationTemplate(NotificationTemplate notificationTemplate, i

NamedJdbcTemplate namedJdbcTemplate = JdbcUtils.getNewNamedJdbcTemplate();
try {
namedJdbcTemplate.executeInsert(INSERT_ORG_NOTIFICATION_TEMPLATE_SQL, (preparedStatement -> {
String insertOrgNotificationTemplateSql = isUnicodeDataType ? INSERT_ORG_NOTIFICATION_TEMPLATE_SQL_UNICODE :
INSERT_ORG_NOTIFICATION_TEMPLATE_SQL;
namedJdbcTemplate.executeInsert(insertOrgNotificationTemplateSql, (preparedStatement -> {

Check warning on line 72 in components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/OrgNotificationTemplateDAO.java

View check run for this annotation

Codecov / codecov/patch

components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/OrgNotificationTemplateDAO.java#L71-L72

Added lines #L71 - L72 were not covered by tests
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());

Check warning on line 78 in components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/OrgNotificationTemplateDAO.java

View check run for this annotation

Codecov / codecov/patch

components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/OrgNotificationTemplateDAO.java#L76-L78

Added lines #L76 - L78 were not covered by tests
} else {
preparedStatement.setString(SUBJECT, notificationTemplate.getSubject());
preparedStatement.setString(BODY, notificationTemplate.getBody());
preparedStatement.setString(FOOTER, notificationTemplate.getFooter());

Check warning on line 82 in components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/OrgNotificationTemplateDAO.java

View check run for this annotation

Codecov / codecov/patch

components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/OrgNotificationTemplateDAO.java#L80-L82

Added lines #L80 - L82 were not covered by tests
}
preparedStatement.setString(CONTENT_TYPE, notificationTemplate.getContentType());
preparedStatement.setString(TYPE_KEY, displayName.toLowerCase());
preparedStatement.setString(CHANNEL, channelName);
Expand All @@ -88,12 +103,20 @@ public NotificationTemplate getNotificationTemplate(String locale, String templa
NotificationTemplate notificationTemplate;

try {
notificationTemplate = namedJdbcTemplate.fetchSingleRecord(GET_ORG_NOTIFICATION_TEMPLATE_SQL,
String getOrgNotificationTemplateSql =
isUnicodeDataType ? GET_ORG_NOTIFICATION_TEMPLATE_SQL_UNICODE : GET_ORG_NOTIFICATION_TEMPLATE_SQL;
notificationTemplate = namedJdbcTemplate.fetchSingleRecord(getOrgNotificationTemplateSql,

Check warning on line 108 in components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/OrgNotificationTemplateDAO.java

View check run for this annotation

Codecov / codecov/patch

components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/OrgNotificationTemplateDAO.java#L108

Added line #L108 was not covered by tests
(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));

Check warning on line 114 in components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/OrgNotificationTemplateDAO.java

View check run for this annotation

Codecov / codecov/patch

components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/OrgNotificationTemplateDAO.java#L112-L114

Added lines #L112 - L114 were not covered by tests
} else {
notificationTemplateResult.setSubject(resultSet.getString(SUBJECT));
notificationTemplateResult.setBody(resultSet.getString(BODY));
notificationTemplateResult.setFooter(resultSet.getString(FOOTER));

Check warning on line 118 in components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/OrgNotificationTemplateDAO.java

View check run for this annotation

Codecov / codecov/patch

components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/OrgNotificationTemplateDAO.java#L116-L118

Added lines #L116 - L118 were not covered by tests
}
notificationTemplateResult.setContentType(resultSet.getString(CONTENT_TYPE));
notificationTemplateResult.setLocale(locale);
notificationTemplateResult.setType(templateType);
Expand Down Expand Up @@ -166,12 +189,21 @@ public List<NotificationTemplate> listNotificationTemplates(String templateType,
List<NotificationTemplate> notificationTemplates;

try {
notificationTemplates = namedJdbcTemplate.executeQuery(LIST_ORG_NOTIFICATION_TEMPLATES_BY_TYPE_SQL,
String listOrgNotificationTemplatesByTypeSql =
isUnicodeDataType ? LIST_ORG_NOTIFICATION_TEMPLATES_BY_TYPE_SQL_UNICODE :
LIST_ORG_NOTIFICATION_TEMPLATES_BY_TYPE_SQL;
notificationTemplates = namedJdbcTemplate.executeQuery(listOrgNotificationTemplatesByTypeSql,

Check warning on line 195 in components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/OrgNotificationTemplateDAO.java

View check run for this annotation

Codecov / codecov/patch

components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/OrgNotificationTemplateDAO.java#L194-L195

Added lines #L194 - L195 were not covered by tests
(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));

Check warning on line 201 in components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/OrgNotificationTemplateDAO.java

View check run for this annotation

Codecov / codecov/patch

components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/OrgNotificationTemplateDAO.java#L199-L201

Added lines #L199 - L201 were not covered by tests
} else {
notificationTemplateResult.setSubject(resultSet.getString(SUBJECT));
notificationTemplateResult.setBody(resultSet.getString(BODY));
notificationTemplateResult.setFooter(resultSet.getString(FOOTER));

Check warning on line 205 in components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/OrgNotificationTemplateDAO.java

View check run for this annotation

Codecov / codecov/patch

components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/OrgNotificationTemplateDAO.java#L203-L205

Added lines #L203 - L205 were not covered by tests
}
notificationTemplateResult.setContentType(resultSet.getString(CONTENT_TYPE));
notificationTemplateResult.setLocale(resultSet.getString(LOCALE));
notificationTemplateResult.setType(templateType.toLowerCase());
Expand Down Expand Up @@ -203,18 +235,26 @@ 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 = isUnicodeDataType ? UPDATE_ORG_NOTIFICATION_TEMPLATE_SQL_UNICODE :
UPDATE_ORG_NOTIFICATION_TEMPLATE_SQL;
namedJdbcTemplate.executeUpdate(updateOrgNotificationTemplateSql, preparedStatement -> {

Check warning on line 240 in components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/OrgNotificationTemplateDAO.java

View check run for this annotation

Codecov / codecov/patch

components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/OrgNotificationTemplateDAO.java#L239-L240

Added lines #L239 - L240 were not covered by tests
if (isUnicodeDataType) {
preparedStatement.setNString(SUBJECT, notificationTemplate.getSubject());
preparedStatement.setNString(BODY, notificationTemplate.getBody());
preparedStatement.setNString(FOOTER, notificationTemplate.getFooter());

Check warning on line 244 in components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/OrgNotificationTemplateDAO.java

View check run for this annotation

Codecov / codecov/patch

components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/OrgNotificationTemplateDAO.java#L242-L244

Added lines #L242 - L244 were not covered by tests
} else {
preparedStatement.setString(SUBJECT, notificationTemplate.getSubject());
preparedStatement.setString(BODY, notificationTemplate.getBody());
preparedStatement.setString(FOOTER, notificationTemplate.getFooter());

Check warning on line 248 in components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/OrgNotificationTemplateDAO.java

View check run for this annotation

Codecov / codecov/patch

components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/OrgNotificationTemplateDAO.java#L246-L248

Added lines #L246 - L248 were not covered by tests

}
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);
});

Check warning on line 257 in components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/OrgNotificationTemplateDAO.java

View check run for this annotation

Codecov / codecov/patch

components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/OrgNotificationTemplateDAO.java#L251-L257

Added lines #L251 - L257 were not covered by tests
} catch (DataAccessException e) {
String error =
String.format("Error while updating %s template %s of type %s from %s tenant.", channelName, locale,
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@
<carbon.commons.imp.pkg.version>[4.7.11, 5.0.0)</carbon.commons.imp.pkg.version>

<!--Carbon Database Utils Version-->
<org.wso2.carbon.database.utils.version>2.1.0</org.wso2.carbon.database.utils.version>
<org.wso2.carbon.database.utils.version>2.2.1</org.wso2.carbon.database.utils.version>
<org.wso2.carbon.database.utils.version.range>[2.1.0,2.2.0)</org.wso2.carbon.database.utils.version.range>

<!--Carbon Identity Framework Version-->
Expand Down

0 comments on commit 394e6e4

Please sign in to comment.