From e054a49aeaed228329cd3ed1350e070045013d56 Mon Sep 17 00:00:00 2001 From: Ace Date: Wed, 10 Jan 2018 12:26:52 +0530 Subject: [PATCH] Adding fix for https://github.com/wso2/product-iots/issues/1579 --- .../dao/impl/AbstractNotificationDAOImpl.java | 23 +++++++++++-------- .../dao/impl/OracleNotificationDAOImpl.java | 7 ++++-- .../src/test/resources/sql/h2.sql | 1 + .../src/main/resources/dbscripts/cdm/h2.sql | 1 + .../main/resources/dbscripts/cdm/mssql.sql | 1 + .../main/resources/dbscripts/cdm/mysql.sql | 1 + .../main/resources/dbscripts/cdm/oracle.sql | 1 + .../resources/dbscripts/cdm/postgresql.sql | 1 + 8 files changed, 25 insertions(+), 11 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/dao/impl/AbstractNotificationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/dao/impl/AbstractNotificationDAOImpl.java index 74d63ee1593..c16c05ef3ab 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/dao/impl/AbstractNotificationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/dao/impl/AbstractNotificationDAOImpl.java @@ -26,6 +26,7 @@ import org.wso2.carbon.device.mgt.core.notification.mgt.dao.util.NotificationDAO import java.sql.*; import java.util.ArrayList; +import java.util.Date; import java.util.List; /** @@ -43,14 +44,15 @@ public abstract class AbstractNotificationDAOImpl implements NotificationDAO { try { conn = NotificationManagementDAOFactory.getConnection(); String sql = - "INSERT INTO DM_NOTIFICATION(DEVICE_ID, OPERATION_ID, STATUS, DESCRIPTION, TENANT_ID) " + - "VALUES (?, ?, ?, ?, ?)"; + "INSERT INTO DM_NOTIFICATION(DEVICE_ID, OPERATION_ID, STATUS, DESCRIPTION, " + + "TENANT_ID, LAST_UPDATED_TIMESTAMP) VALUES (?, ?, ?, ?, ?, ?)"; stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); stmt.setInt(1, deviceId); stmt.setInt(2, notification.getOperationId()); stmt.setString(3, notification.getStatus().toString()); stmt.setString(4, notification.getDescription()); stmt.setInt(5, tenantId); + stmt.setTimestamp(6, new Timestamp(new Date().getTime())); stmt.execute(); rs = stmt.getGeneratedKeys(); if (rs.next()) { @@ -103,13 +105,14 @@ public abstract class AbstractNotificationDAOImpl implements NotificationDAO { int rows; try { conn = NotificationManagementDAOFactory.getConnection(); - String sql = "UPDATE DM_NOTIFICATION SET OPERATION_ID = ?, STATUS = ?, DESCRIPTION = ? " + - "WHERE NOTIFICATION_ID = ?"; + String sql = "UPDATE DM_NOTIFICATION SET OPERATION_ID = ?, STATUS = ?, DESCRIPTION = ?, " + + "LAST_UPDATED_TIMESTAMP = ? WHERE NOTIFICATION_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, notification.getOperationId()); stmt.setString(2, notification.getStatus().toString()); stmt.setString(3, notification.getDescription()); - stmt.setInt(4, notification.getNotificationId()); + stmt.setTimestamp(4, new Timestamp(new Date().getTime())); + stmt.setInt(5, notification.getNotificationId()); rows = stmt.executeUpdate(); } catch (Exception e) { throw new NotificationManagementException("Error occurred while updating the " + @@ -128,10 +131,11 @@ public abstract class AbstractNotificationDAOImpl implements NotificationDAO { int rows; try { conn = NotificationManagementDAOFactory.getConnection(); - String sql = "UPDATE DM_NOTIFICATION SET STATUS = ? WHERE NOTIFICATION_ID = ?"; + String sql = "UPDATE DM_NOTIFICATION SET STATUS = ?, LAST_UPDATED_TIMESTAMP = ? WHERE NOTIFICATION_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, status.toString()); - stmt.setInt(2, notificationId); + stmt.setTimestamp(2, new Timestamp(new Date().getTime())); + stmt.setInt(3, notificationId); rows = stmt.executeUpdate(); } catch (Exception e) { throw new NotificationManagementException("Error occurred while updating the status of " + @@ -150,10 +154,11 @@ public abstract class AbstractNotificationDAOImpl implements NotificationDAO { int rows; try { conn = NotificationManagementDAOFactory.getConnection(); - String sql = "UPDATE DM_NOTIFICATION SET STATUS = ? WHERE TENANT_ID= ?"; + String sql = "UPDATE DM_NOTIFICATION SET STATUS = ?, LAST_UPDATED_TIMESTAMP = ? WHERE TENANT_ID= ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, status.toString()); - stmt.setInt(2, tenantID); + stmt.setTimestamp(2, new Timestamp(new Date().getTime())); + stmt.setInt(3, tenantID); rows = stmt.executeUpdate(); } catch (Exception e) { throw new NotificationManagementException("Error while trying to clear all " + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/dao/impl/OracleNotificationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/dao/impl/OracleNotificationDAOImpl.java index 45460021884..bf6360ed6e4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/dao/impl/OracleNotificationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/dao/impl/OracleNotificationDAOImpl.java @@ -26,6 +26,7 @@ import org.wso2.carbon.device.mgt.core.notification.mgt.dao.util.NotificationDAO import java.sql.*; import java.util.ArrayList; +import java.util.Date; import java.util.List; /** @@ -41,14 +42,16 @@ public class OracleNotificationDAOImpl extends AbstractNotificationDAOImpl { int notificationId = -1; try { conn = NotificationManagementDAOFactory.getConnection(); - String sql = "INSERT INTO DM_NOTIFICATION(DEVICE_ID, OPERATION_ID, STATUS, DESCRIPTION, TENANT_ID) " - + "VALUES (?, ?, ?, ?, ?)"; + String sql = "INSERT INTO DM_NOTIFICATION(DEVICE_ID, OPERATION_ID, STATUS, " + + "DESCRIPTION, TENANT_ID, LAST_UPDATED_TIMESTAMP) " + + "VALUES (?, ?, ?, ?, ?, ?)"; stmt = conn.prepareStatement(sql, new int[] { 1 }); stmt.setInt(1, deviceId); stmt.setInt(2, notification.getOperationId()); stmt.setString(3, notification.getStatus().toString()); stmt.setString(4, notification.getDescription()); stmt.setInt(5, tenantId); + stmt.setTimestamp(6, new Timestamp(new Date().getTime())); stmt.execute(); rs = stmt.getGeneratedKeys(); if (rs.next()) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql index bbb21a6e559..e40dc8d52fe 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql @@ -385,6 +385,7 @@ CREATE TABLE IF NOT EXISTS DM_NOTIFICATION ( TENANT_ID INTEGER NOT NULL, STATUS VARCHAR(10) NULL, DESCRIPTION VARCHAR(1000) NULL, + LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL, PRIMARY KEY (NOTIFICATION_ID), CONSTRAINT fk_dm_device_notification FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql index c3f777825ea..5dbf1636bec 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -385,6 +385,7 @@ CREATE TABLE IF NOT EXISTS DM_NOTIFICATION ( TENANT_ID INTEGER NOT NULL, STATUS VARCHAR(10) NULL, DESCRIPTION VARCHAR(1000) NULL, + LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL, PRIMARY KEY (NOTIFICATION_ID), CONSTRAINT fk_dm_device_notification FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql index fe20f1a6db5..eca23550272 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql @@ -437,6 +437,7 @@ CREATE TABLE DM_NOTIFICATION ( TENANT_ID INTEGER NOT NULL, STATUS VARCHAR(10) NULL, DESCRIPTION VARCHAR(1000) NULL, + LAST_UPDATED_TIMESTAMP DATETIME2 NOT NULL, PRIMARY KEY (NOTIFICATION_ID), CONSTRAINT FL_DM_NOTIFICATION FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql index 29fe4ded33d..2a85f469440 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql @@ -452,6 +452,7 @@ CREATE TABLE IF NOT EXISTS DM_NOTIFICATION ( TENANT_ID INTEGER NOT NULL, STATUS VARCHAR(10) NULL, DESCRIPTION VARCHAR(1000) NULL, + LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL, PRIMARY KEY (NOTIFICATION_ID), CONSTRAINT fk_dm_device_notification FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql index 7288dc0ab6a..8f5c88d8712 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql @@ -747,6 +747,7 @@ CREATE TABLE DM_NOTIFICATION ( TENANT_ID NUMBER(10) NOT NULL, STATUS VARCHAR2(10) NULL, DESCRIPTION VARCHAR2(1000) NULL, + LAST_UPDATED_TIMESTAMP TIMESTAMP(0) NOT NULL, CONSTRAINT PK_DM_NOTIFICATION PRIMARY KEY (NOTIFICATION_ID), CONSTRAINT FK_DM_DEVICE_NOTIFICATION FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID), diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql index 52343021690..f00821ecaaa 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql @@ -395,6 +395,7 @@ CREATE TABLE IF NOT EXISTS DM_NOTIFICATION ( TENANT_ID INTEGER NOT NULL, STATUS VARCHAR(10) NULL, DESCRIPTION VARCHAR(1000) NULL, + LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL, CONSTRAINT fk_dm_device_notification FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT fk_dm_operation_notification FOREIGN KEY (OPERATION_ID) REFERENCES