Skip to content

Commit

Permalink
Merge pull request #295 from darshanasbg/add-audit-data
Browse files Browse the repository at this point in the history
  • Loading branch information
darshanasbg authored Dec 13, 2024
2 parents 97740cd + 0178f61 commit fb24796
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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() {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;";
Expand All @@ -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) " +
Expand Down Expand Up @@ -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 =
Expand All @@ -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) " +
Expand Down Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand All @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand All @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "_";

Expand Down Expand Up @@ -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());
}
}

0 comments on commit fb24796

Please sign in to comment.