|
|
@ -18,9 +18,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
package org.wso2.carbon.device.mgt.core.notification.mgt.dao.impl;
|
|
|
|
package org.wso2.carbon.device.mgt.core.notification.mgt.dao.impl;
|
|
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.notification.mgt.Notification;
|
|
|
|
import org.wso2.carbon.device.mgt.common.notification.mgt.Notification;
|
|
|
|
import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagementException;
|
|
|
|
import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagementException;
|
|
|
|
import org.wso2.carbon.device.mgt.core.notification.mgt.dao.NotificationDAO;
|
|
|
|
import org.wso2.carbon.device.mgt.core.notification.mgt.dao.NotificationDAO;
|
|
|
@ -36,167 +33,157 @@ import java.util.List;
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public class NotificationDAOImpl implements NotificationDAO {
|
|
|
|
public class NotificationDAOImpl implements NotificationDAO {
|
|
|
|
|
|
|
|
|
|
|
|
private static final Log log = LogFactory.getLog(NotificationDAOImpl.class);
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public int addNotification(int deviceId, int tenantId,
|
|
|
|
|
|
|
|
Notification notification) throws NotificationManagementException {
|
|
|
|
|
|
|
|
Connection conn;
|
|
|
|
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
|
|
|
|
ResultSet rs;
|
|
|
|
|
|
|
|
int notificationId = -1;
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
conn = NotificationManagementDAOFactory.getConnection();
|
|
|
|
|
|
|
|
String sql =
|
|
|
|
|
|
|
|
"INSERT INTO DM_NOTIFICATION(DEVICE_ID, OPERATION_ID, STATUS, DESCRIPTION, TENANT_ID) " +
|
|
|
|
|
|
|
|
"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.execute();
|
|
|
|
|
|
|
|
rs = stmt.getGeneratedKeys();
|
|
|
|
|
|
|
|
if (rs.next()) {
|
|
|
|
|
|
|
|
notificationId = rs.getInt(1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
throw new NotificationManagementException("Error occurred while adding the " +
|
|
|
|
|
|
|
|
"Notification for device id : " + deviceId,
|
|
|
|
|
|
|
|
e);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
NotificationDAOUtil.cleanupResources(stmt, null);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return notificationId;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public int addNotification(int deviceId, int tenantId, Notification notification) throws
|
|
|
|
public int updateNotification(Notification notification)
|
|
|
|
NotificationManagementException {
|
|
|
|
throws NotificationManagementException {
|
|
|
|
Connection conn;
|
|
|
|
Connection conn;
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
ResultSet rs;
|
|
|
|
int rows;
|
|
|
|
int notificationId = -1;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
conn = NotificationManagementDAOFactory.getConnection();
|
|
|
|
conn = NotificationManagementDAOFactory.getConnection();
|
|
|
|
String sql = "UPDATE DM_NOTIFICATION SET OPERATION_ID = ?, STATUS = ?, DESCRIPTION = ? " +
|
|
|
|
String sql =
|
|
|
|
"WHERE NOTIFICATION_ID = ?";
|
|
|
|
"INSERT INTO DM_NOTIFICATION(DEVICE_ID, OPERATION_ID, STATUS, DESCRIPTION, TENANT_ID) " +
|
|
|
|
stmt = conn.prepareStatement(sql);
|
|
|
|
"VALUES (?, ?, ?, ?, ?)";
|
|
|
|
stmt.setInt(1, notification.getOperationId());
|
|
|
|
stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
|
|
|
|
stmt.setString(2, notification.getStatus().toString());
|
|
|
|
stmt.setInt(1, deviceId);
|
|
|
|
stmt.setString(3, notification.getDescription());
|
|
|
|
stmt.setInt(2, notification.getOperationId());
|
|
|
|
stmt.setInt(4, notification.getNotificationId());
|
|
|
|
stmt.setString(3, notification.getStatus().toString());
|
|
|
|
rows = stmt.executeUpdate();
|
|
|
|
stmt.setString(4, notification.getDescription());
|
|
|
|
} catch (Exception e) {
|
|
|
|
stmt.setInt(5, tenantId);
|
|
|
|
throw new NotificationManagementException("Error occurred while updating the " +
|
|
|
|
stmt.execute();
|
|
|
|
"Notification id : " + notification.getNotificationId(), e);
|
|
|
|
rs = stmt.getGeneratedKeys();
|
|
|
|
} finally {
|
|
|
|
if (rs.next()) {
|
|
|
|
NotificationDAOUtil.cleanupResources(stmt, null);
|
|
|
|
notificationId = rs.getInt(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rows;
|
|
|
|
} catch (Exception e) {
|
|
|
|
}
|
|
|
|
throw new NotificationManagementException("Error occurred while adding the " +
|
|
|
|
|
|
|
|
"Notification for device id : " + deviceId,
|
|
|
|
|
|
|
|
e);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
NotificationDAOUtil.cleanupResources(stmt, null);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return notificationId;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public int updateNotification(Notification notification)
|
|
|
|
public int updateNotificationStatus(int notificationId, Notification.Status status)
|
|
|
|
throws NotificationManagementException {
|
|
|
|
throws NotificationManagementException {
|
|
|
|
Connection conn;
|
|
|
|
Connection conn;
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
int rows;
|
|
|
|
int rows;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
conn = NotificationManagementDAOFactory.getConnection();
|
|
|
|
conn = NotificationManagementDAOFactory.getConnection();
|
|
|
|
String sql = "UPDATE DM_NOTIFICATION SET OPERATION_ID = ?, STATUS = ?, DESCRIPTION = ? " +
|
|
|
|
String sql = "UPDATE DM_NOTIFICATION SET STATUS = ? WHERE NOTIFICATION_ID = ?";
|
|
|
|
"WHERE NOTIFICATION_ID = ?";
|
|
|
|
stmt = conn.prepareStatement(sql);
|
|
|
|
stmt = conn.prepareStatement(sql);
|
|
|
|
stmt.setString(1, status.toString());
|
|
|
|
stmt.setInt(1, notification.getOperationId());
|
|
|
|
stmt.setInt(2, notificationId);
|
|
|
|
stmt.setString(2, notification.getStatus().toString());
|
|
|
|
rows = stmt.executeUpdate();
|
|
|
|
stmt.setString(3, notification.getDescription());
|
|
|
|
} catch (Exception e) {
|
|
|
|
stmt.setInt(4, notification.getNotificationId());
|
|
|
|
throw new NotificationManagementException("Error occurred while updating the status of " +
|
|
|
|
rows = stmt.executeUpdate();
|
|
|
|
"Notification id : " + notificationId, e);
|
|
|
|
} catch (Exception e) {
|
|
|
|
} finally {
|
|
|
|
throw new NotificationManagementException("Error occurred while updating the " +
|
|
|
|
NotificationDAOUtil.cleanupResources(stmt, null);
|
|
|
|
"Notification id : " + notification.getNotificationId(), e);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
return rows;
|
|
|
|
NotificationDAOUtil.cleanupResources(stmt, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rows;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public int updateNotificationStatus(int notificationId, Notification.Status status)
|
|
|
|
public List<Notification> getAllNotifications(int tenantId) throws NotificationManagementException {
|
|
|
|
throws NotificationManagementException {
|
|
|
|
Connection conn;
|
|
|
|
Connection conn;
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
ResultSet rs = null;
|
|
|
|
int rows;
|
|
|
|
List<Notification> notifications = null;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
conn = NotificationManagementDAOFactory.getConnection();
|
|
|
|
conn = NotificationManagementDAOFactory.getConnection();
|
|
|
|
String sql = "UPDATE DM_NOTIFICATION SET STATUS = ? WHERE NOTIFICATION_ID = ?";
|
|
|
|
String sql =
|
|
|
|
stmt = conn.prepareStatement(sql);
|
|
|
|
"SELECT n1.NOTIFICATION_ID, n1.DEVICE_ID, n1.OPERATION_ID, n1.STATUS, n1.DESCRIPTION," +
|
|
|
|
stmt.setString(1, status.toString());
|
|
|
|
" d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, DM_DEVICE_TYPE t, (SELECT " +
|
|
|
|
stmt.setInt(2, notificationId);
|
|
|
|
"NOTIFICATION_ID, DEVICE_ID, OPERATION_ID, STATUS, DESCRIPTION FROM DM_NOTIFICATION WHERE " +
|
|
|
|
rows = stmt.executeUpdate();
|
|
|
|
"TENANT_ID = ?) n1 WHERE n1.DEVICE_ID = d.ID AND d.DEVICE_TYPE_ID=t.ID AND TENANT_ID = ?";
|
|
|
|
} catch (Exception e) {
|
|
|
|
stmt = conn.prepareStatement(sql);
|
|
|
|
throw new NotificationManagementException("Error occurred while updating the status of " +
|
|
|
|
stmt.setInt(1, tenantId);
|
|
|
|
"Notification id : " + notificationId, e);
|
|
|
|
stmt.setInt(2, tenantId);
|
|
|
|
} finally {
|
|
|
|
rs = stmt.executeQuery();
|
|
|
|
NotificationDAOUtil.cleanupResources(stmt, null);
|
|
|
|
notifications = new ArrayList<>();
|
|
|
|
}
|
|
|
|
while (rs.next()) {
|
|
|
|
return rows;
|
|
|
|
notifications.add(this.getNotification(rs));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
|
|
|
throw new NotificationManagementException(
|
|
|
|
|
|
|
|
"Error occurred while retrieving information of all notifications", e);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
NotificationDAOUtil.cleanupResources(stmt, rs);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return notifications;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public List<Notification> getAllNotifications(int tenantId)
|
|
|
|
public List<Notification> getNotificationsByStatus(Notification.Status status,
|
|
|
|
throws NotificationManagementException {
|
|
|
|
int tenantId) throws NotificationManagementException {
|
|
|
|
Connection conn;
|
|
|
|
Connection conn;
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
ResultSet rs = null;
|
|
|
|
ResultSet rs = null;
|
|
|
|
List<Notification> notifications = null;
|
|
|
|
List<Notification> notifications = null;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
conn = NotificationManagementDAOFactory.getConnection();
|
|
|
|
conn = NotificationManagementDAOFactory.getConnection();
|
|
|
|
String sql =
|
|
|
|
String sql = "SELECT n1.NOTIFICATION_ID, n1.DEVICE_ID, n1.OPERATION_ID, n1.STATUS," +
|
|
|
|
"SELECT n1.NOTIFICATION_ID, n1.DEVICE_ID, n1.OPERATION_ID, n1.STATUS, n1.DESCRIPTION," +
|
|
|
|
" n1.DESCRIPTION, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM " +
|
|
|
|
" d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, DM_DEVICE_TYPE t, (SELECT " +
|
|
|
|
"DM_DEVICE d, DM_DEVICE_TYPE t, (SELECT NOTIFICATION_ID, DEVICE_ID, " +
|
|
|
|
"NOTIFICATION_ID, DEVICE_ID, OPERATION_ID, STATUS, DESCRIPTION FROM DM_NOTIFICATION WHERE " +
|
|
|
|
"OPERATION_ID, STATUS, DESCRIPTION FROM DM_NOTIFICATION WHERE " +
|
|
|
|
"TENANT_ID = ?) n1 WHERE n1.DEVICE_ID = d.ID AND d.DEVICE_TYPE_ID=t.ID AND TENANT_ID = ?";
|
|
|
|
"TENANT_ID = ? AND STATUS = ?) n1 WHERE n1.DEVICE_ID = d.ID AND d.DEVICE_TYPE_ID=t.ID " +
|
|
|
|
stmt = conn.prepareStatement(sql);
|
|
|
|
"AND TENANT_ID = ?";
|
|
|
|
stmt.setInt(1, tenantId);
|
|
|
|
stmt = conn.prepareStatement(sql);
|
|
|
|
stmt.setInt(2, tenantId);
|
|
|
|
stmt.setInt(1, tenantId);
|
|
|
|
rs = stmt.executeQuery();
|
|
|
|
stmt.setString(2, status.toString());
|
|
|
|
notifications = new ArrayList<>();
|
|
|
|
stmt.setInt(3, tenantId);
|
|
|
|
while (rs.next()) {
|
|
|
|
rs = stmt.executeQuery();
|
|
|
|
notifications.add(this.getNotification(rs));
|
|
|
|
notifications = new ArrayList<>();
|
|
|
|
}
|
|
|
|
while (rs.next()) {
|
|
|
|
} catch (SQLException e) {
|
|
|
|
notifications.add(this.getNotification(rs));
|
|
|
|
throw new NotificationManagementException(
|
|
|
|
}
|
|
|
|
"Error occurred while retrieving information of all notifications", e);
|
|
|
|
} catch (SQLException e) {
|
|
|
|
} finally {
|
|
|
|
throw new NotificationManagementException(
|
|
|
|
NotificationDAOUtil.cleanupResources(stmt, rs);
|
|
|
|
"Error occurred while retrieving information of all " +
|
|
|
|
}
|
|
|
|
"notifications by status : " + status, e);
|
|
|
|
return notifications;
|
|
|
|
} finally {
|
|
|
|
}
|
|
|
|
NotificationDAOUtil.cleanupResources(stmt, rs);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return notifications;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
private Notification getNotification(ResultSet rs) throws SQLException {
|
|
|
|
public List<Notification> getNotificationsByStatus(Notification.Status status, int tenantId)
|
|
|
|
Notification notification = new Notification();
|
|
|
|
throws NotificationManagementException {
|
|
|
|
notification.setNotificationId(rs.getInt("NOTIFICATION_ID"));
|
|
|
|
Connection conn;
|
|
|
|
notification.setOperationId(rs.getInt("OPERATION_ID"));
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
notification.setDescription(rs.getString("DESCRIPTION"));
|
|
|
|
ResultSet rs = null;
|
|
|
|
notification.setStatus(rs.getString("STATUS"));
|
|
|
|
List<Notification> notifications = null;
|
|
|
|
return notification;
|
|
|
|
try {
|
|
|
|
}
|
|
|
|
conn = NotificationManagementDAOFactory.getConnection();
|
|
|
|
|
|
|
|
String sql = "SELECT n1.NOTIFICATION_ID, n1.DEVICE_ID, n1.OPERATION_ID, n1.STATUS," +
|
|
|
|
|
|
|
|
" n1.DESCRIPTION, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM " +
|
|
|
|
|
|
|
|
"DM_DEVICE d, DM_DEVICE_TYPE t, (SELECT NOTIFICATION_ID, DEVICE_ID, " +
|
|
|
|
|
|
|
|
"OPERATION_ID, STATUS, DESCRIPTION FROM DM_NOTIFICATION WHERE " +
|
|
|
|
|
|
|
|
"TENANT_ID = ? AND STATUS = ?) n1 WHERE n1.DEVICE_ID = d.ID AND d.DEVICE_TYPE_ID=t.ID " +
|
|
|
|
|
|
|
|
"AND TENANT_ID = ?";
|
|
|
|
|
|
|
|
stmt = conn.prepareStatement(sql);
|
|
|
|
|
|
|
|
stmt.setInt(1, tenantId);
|
|
|
|
|
|
|
|
stmt.setString(2, status.toString());
|
|
|
|
|
|
|
|
stmt.setInt(3, tenantId);
|
|
|
|
|
|
|
|
rs = stmt.executeQuery();
|
|
|
|
|
|
|
|
notifications = new ArrayList<>();
|
|
|
|
|
|
|
|
while (rs.next()) {
|
|
|
|
|
|
|
|
notifications.add(this.getNotification(rs));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
|
|
|
throw new NotificationManagementException(
|
|
|
|
|
|
|
|
"Error occurred while retrieving information of all " +
|
|
|
|
|
|
|
|
"notifications by status : " + status, e);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
NotificationDAOUtil.cleanupResources(stmt, rs);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return notifications;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Notification getNotification(ResultSet rs) throws SQLException {
|
|
|
|
|
|
|
|
Notification notification = new Notification();
|
|
|
|
|
|
|
|
notification.setNotificationId(rs.getInt("NOTIFICATION_ID"));
|
|
|
|
|
|
|
|
notification.setDeviceIdentifier(this.getDeviceIdentifier(rs));
|
|
|
|
|
|
|
|
notification.setOperationId(rs.getInt("OPERATION_ID"));
|
|
|
|
|
|
|
|
notification.setDescription(rs.getString("DESCRIPTION"));
|
|
|
|
|
|
|
|
notification.setStatus(rs.getString("STATUS"));
|
|
|
|
|
|
|
|
return notification;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private DeviceIdentifier getDeviceIdentifier(ResultSet rs) throws SQLException {
|
|
|
|
|
|
|
|
DeviceIdentifier identifier = new DeviceIdentifier();
|
|
|
|
|
|
|
|
identifier.setId(rs.getString("DEVICE_IDENTIFICATION"));
|
|
|
|
|
|
|
|
identifier.setType(rs.getString("DEVICE_TYPE"));
|
|
|
|
|
|
|
|
return identifier;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|