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 08de1ba0..c6df9315 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 @@ -153,6 +153,10 @@ public String toString() { */ public static class NotificationTableColumns { + public static final String NOTIFICATION_TYPE_SCHEMA_VERSION = "1.0.0"; + public static final String ORG_TEMPLATE_SCHEMA_VERSION = "1.0.0"; + public static final String APP_TEMPLATE_SCHEMA_VERSION = "1.0.0"; + public static final String ID = "ID"; public static final String TYPE_KEY = "TYPE_KEY"; public static final String NAME = "NAME"; @@ -167,5 +171,11 @@ public static class NotificationTableColumns { public static final String CONTENT_TYPE = "CONTENT_TYPE"; public static final String TYPE_ID = "TYPE_ID"; public static final String APP_ID = "APP_ID"; + public static final String VERSION = "VERSION"; + public static final String CREATED_AT = "CREATED_AT"; + public static final String UPDATED_AT = "UPDATED_AT"; + + private NotificationTableColumns() { + } } } 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 747575f9..6791f0f1 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 @@ -26,7 +26,8 @@ public class SQLConstants { // sql constants for notification type public static final String INSERT_NOTIFICATION_TYPE_SQL = "INSERT INTO IDN_NOTIFICATION_TYPE " + - "(TYPE_KEY, NAME, CHANNEL, TENANT_ID) VALUES (:TYPE_KEY;, :NAME;, :CHANNEL;, :TENANT_ID;)"; + "(TYPE_KEY, NAME, CHANNEL, TENANT_ID, VERSION, CREATED_AT, UPDATED_AT) " + + "VALUES (:TYPE_KEY;, :NAME;, :CHANNEL;, :TENANT_ID;, :VERSION;, :CREATED_AT;, :UPDATED_AT;)"; public static final String GET_NOTIFICATION_TYPE_SQL = "SELECT NAME FROM IDN_NOTIFICATION_TYPE " + "WHERE TYPE_KEY = :TYPE_KEY; AND CHANNEL = :CHANNEL; AND TENANT_ID = :TENANT_ID;"; @@ -43,9 +44,9 @@ public class SQLConstants { // sql constants for org notification template public static final String INSERT_ORG_NOTIFICATION_TEMPLATE_SQL = "INSERT INTO IDN_NOTIFICATION_ORG_TEMPLATE " + - "(TEMPLATE_KEY, LOCALE, CONTENT, CONTENT_TYPE, TYPE_ID, TENANT_ID) " + + "(TEMPLATE_KEY, LOCALE, CONTENT, CONTENT_TYPE, TYPE_ID, TENANT_ID, VERSION, CREATED_AT, UPDATED_AT) " + "VALUES (:TEMPLATE_KEY;, :LOCALE;, :CONTENT;, :CONTENT_TYPE;, (" + - GET_NOTIFICATION_TYPE_ID_SQL + "), :TENANT_ID;)"; + GET_NOTIFICATION_TYPE_ID_SQL + "), :TENANT_ID;, :VERSION;, :CREATED_AT;, :UPDATED_AT;)"; 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) " + @@ -82,7 +83,7 @@ public class SQLConstants { "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; " + + "SET CONTENT = :CONTENT;, CONTENT_TYPE = :CONTENT_TYPE;, UPDATED_AT = :UPDATED_AT; " + "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 = @@ -105,9 +106,9 @@ public class SQLConstants { // sql constants for app notification template public static final String INSERT_APP_NOTIFICATION_TEMPLATE_SQL = "INSERT INTO IDN_NOTIFICATION_APP_TEMPLATE " + - "(TEMPLATE_KEY, LOCALE, CONTENT, CONTENT_TYPE, TYPE_ID, APP_ID, TENANT_ID) " + + "(TEMPLATE_KEY, LOCALE, CONTENT, CONTENT_TYPE, TYPE_ID, APP_ID, TENANT_ID, VERSION, CREATED_AT, UPDATED_AT) " + "VALUES (:TEMPLATE_KEY;, :LOCALE;, :CONTENT;, :CONTENT_TYPE;, (" + - GET_NOTIFICATION_TYPE_ID_SQL + "), :APP_ID;, :TENANT_ID;)"; + GET_NOTIFICATION_TYPE_ID_SQL + "), :APP_ID;, :TENANT_ID;, :VERSION;, :CREATED_AT;, :UPDATED_AT;)"; 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) " + @@ -148,7 +149,7 @@ public class SQLConstants { ") 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; " + + "SET CONTENT = :CONTENT;, CONTENT_TYPE = :CONTENT_TYPE;, UPDATED_AT = :UPDATED_AT; " + "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 = 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 47c01851..dab88b48 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 @@ -28,13 +28,16 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; +import java.sql.Timestamp; 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.APP_TEMPLATE_SCHEMA_VERSION; 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.CREATED_AT; 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; @@ -43,6 +46,8 @@ import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.TENANT_ID; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.TYPE_ID; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.TYPE_KEY; +import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.UPDATED_AT; +import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.VERSION; 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; @@ -60,7 +65,9 @@ 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.CALENDER; import static org.wso2.carbon.email.mgt.util.I18nEmailUtil.getContentByteArray; +import static org.wso2.carbon.email.mgt.util.I18nEmailUtil.getCurrentTime; import static org.wso2.carbon.email.mgt.util.I18nEmailUtil.setContent; /** @@ -90,6 +97,10 @@ public void addNotificationTemplate(NotificationTemplate notificationTemplate, S preparedStatement.setString(LOCALE, locale); if (isUnicodeSupported) { preparedStatement.setBinaryStream(CONTENT, contentStream, contentLength); + Timestamp currentTime = getCurrentTime(); + preparedStatement.setTimeStamp(CREATED_AT, currentTime, CALENDER); + preparedStatement.setTimeStamp(UPDATED_AT, currentTime, CALENDER); + preparedStatement.setString(VERSION, APP_TEMPLATE_SCHEMA_VERSION); } else if (isHybrid) { preparedStatement.setBinaryStream(CONTENT, contentStream, contentLength); preparedStatement.setString(SUBJECT, notificationTemplate.getSubject()); @@ -281,6 +292,7 @@ public void updateNotificationTemplate(NotificationTemplate notificationTemplate preparedStatement -> { if (isUnicodeSupported) { preparedStatement.setBinaryStream(CONTENT, contentStream, contentLength); + preparedStatement.setTimeStamp(UPDATED_AT, getCurrentTime(), CALENDER); } else if (isHybrid) { preparedStatement.setBinaryStream(CONTENT, contentStream, contentLength); preparedStatement.setString(SUBJECT, notificationTemplate.getSubject()); @@ -298,6 +310,9 @@ public void updateNotificationTemplate(NotificationTemplate notificationTemplate preparedStatement.setInt(TENANT_ID, tenantId); preparedStatement.setString(APP_ID, applicationUuid); preparedStatement.setInt(TENANT_ID, tenantId); + + Timestamp currentTime = getCurrentTime(); + preparedStatement.setTimeStamp(UPDATED_AT, currentTime, CALENDER); }); } catch (DataAccessException e) { String error = diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/NotificationTypeDAO.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/NotificationTypeDAO.java index 76371e4b..06ec70fd 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/NotificationTypeDAO.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/store/dao/NotificationTypeDAO.java @@ -23,16 +23,24 @@ import org.wso2.carbon.identity.core.util.JdbcUtils; import org.wso2.carbon.identity.governance.exceptions.notiification.NotificationTemplateManagerServerException; +import java.sql.Timestamp; +import java.util.Date; import java.util.List; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.CHANNEL; +import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.CREATED_AT; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.NAME; +import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.NOTIFICATION_TYPE_SCHEMA_VERSION; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.TENANT_ID; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.TYPE_KEY; +import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.UPDATED_AT; +import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.VERSION; import static org.wso2.carbon.email.mgt.constants.SQLConstants.DELETE_NOTIFICATION_TYPE_BY_ID_SQL; import static org.wso2.carbon.email.mgt.constants.SQLConstants.GET_NOTIFICATION_TYPE_SQL; import static org.wso2.carbon.email.mgt.constants.SQLConstants.INSERT_NOTIFICATION_TYPE_SQL; import static org.wso2.carbon.email.mgt.constants.SQLConstants.LIST_NOTIFICATION_TYPES_SQL; +import static org.wso2.carbon.email.mgt.util.I18nEmailUtil.CALENDER; +import static org.wso2.carbon.email.mgt.util.I18nEmailUtil.getCurrentTime; /** * This class is to perform CRUD operations for Notification Types. @@ -49,6 +57,11 @@ public void addNotificationTemplateType(String type, String displayName, String preparedStatement.setString(NAME, displayName); preparedStatement.setString(CHANNEL, channelName); preparedStatement.setInt(TENANT_ID, tenantId); + + Timestamp currentTime = getCurrentTime(); + preparedStatement.setTimeStamp(CREATED_AT, currentTime, CALENDER); + preparedStatement.setTimeStamp(UPDATED_AT, currentTime, CALENDER); + preparedStatement.setString(VERSION, NOTIFICATION_TYPE_SCHEMA_VERSION); }), displayName, false); } catch (DataAccessException e) { String error = 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 32b95e45..15332074 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 @@ -30,20 +30,25 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; +import java.sql.Timestamp; 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.CREATED_AT; 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.ORG_TEMPLATE_SCHEMA_VERSION; 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; import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.TYPE_KEY; +import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.UPDATED_AT; +import static org.wso2.carbon.email.mgt.constants.I18nMgtConstants.NotificationTableColumns.VERSION; import static org.wso2.carbon.email.mgt.constants.SQLConstants.DELETE_ORG_NOTIFICATION_TEMPLATES_BY_TYPE_SQL; 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; @@ -60,7 +65,9 @@ import static org.wso2.carbon.email.mgt.constants.SQLConstants.UPDATE_ORG_NOTIFICATION_TEMPLATE_HYBRID_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.CALENDER; import static org.wso2.carbon.email.mgt.util.I18nEmailUtil.getContentByteArray; +import static org.wso2.carbon.email.mgt.util.I18nEmailUtil.getCurrentTime; import static org.wso2.carbon.email.mgt.util.I18nEmailUtil.setContent; /** @@ -92,6 +99,10 @@ public void addNotificationTemplate(NotificationTemplate notificationTemplate, i preparedStatement.setString(LOCALE, locale); if (isUnicodeSupported) { preparedStatement.setBinaryStream(CONTENT, contentStream, contentLength); + Timestamp currentTime = getCurrentTime(); + preparedStatement.setTimeStamp(CREATED_AT, currentTime, CALENDER); + preparedStatement.setTimeStamp(UPDATED_AT, currentTime, CALENDER); + preparedStatement.setString(VERSION, ORG_TEMPLATE_SCHEMA_VERSION); } else if (isHybrid) { preparedStatement.setBinaryStream(CONTENT, contentStream, contentLength); preparedStatement.setString(SUBJECT, notificationTemplate.getSubject()); @@ -293,6 +304,7 @@ public void updateNotificationTemplate(NotificationTemplate notificationTemplate preparedStatement -> { if (isUnicodeSupported) { preparedStatement.setBinaryStream(CONTENT, contentStream, contentLength); + preparedStatement.setTimeStamp(UPDATED_AT, getCurrentTime(), CALENDER); } else if (isHybrid) { preparedStatement.setBinaryStream(CONTENT, contentStream, contentLength); preparedStatement.setString(SUBJECT, notificationTemplate.getSubject()); @@ -309,6 +321,9 @@ public void updateNotificationTemplate(NotificationTemplate notificationTemplate preparedStatement.setString(CHANNEL, channelName); preparedStatement.setInt(TENANT_ID, tenantId); preparedStatement.setInt(TENANT_ID, tenantId); + + Timestamp currentTime = getCurrentTime(); + preparedStatement.setTimeStamp(UPDATED_AT, currentTime, CALENDER); }); } catch (DataAccessException e) { String error = diff --git a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/util/I18nEmailUtil.java b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/util/I18nEmailUtil.java index a46c7cc7..f2383016 100644 --- a/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/util/I18nEmailUtil.java +++ b/components/email-mgt/org.wso2.carbon.email.mgt/src/main/java/org/wso2/carbon/email/mgt/util/I18nEmailUtil.java @@ -45,15 +45,22 @@ import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.sql.SQLException; +import java.sql.Timestamp; import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; import java.util.List; import java.util.Locale; +import java.util.TimeZone; + +import static java.time.ZoneOffset.UTC; public class I18nEmailUtil { private static final Log log = LogFactory.getLog(I18nEmailUtil.class); public static final String CHARSET_CONSTANT = "charset"; public static final String CHARSET_UTF_8 = CHARSET_CONSTANT + "=" + StandardCharsets.UTF_8; + public static final Calendar CALENDER = Calendar.getInstance(TimeZone.getTimeZone(UTC)); private static final String HYPHEN = "-"; private static final String UNDERSCORE = "_"; @@ -328,4 +335,14 @@ public static void setContent(InputStream contentStream, NotificationTemplate no throw new SQLException("Error while reading content data.", e); } } + + /** + * Get the current time as a timestamp in UTC. + * + * @return the current time as a timestamp + */ + public static Timestamp getCurrentTime() { + + return new Timestamp(new Date().getTime()); + } }