Added MSSQL, PostgreSQL and Oracle db support for Notification feature.

revert-70aa11f8
harshanl 8 years ago
parent 35ea88b9de
commit 657e4864fb

@ -68,6 +68,12 @@
<url>/devices/*/*</url> <url>/devices/*/*</url>
<method>GET</method> <method>GET</method>
</Permission> </Permission>
<Permission>
<name>View device info</name>
<path>/device-mgt/admin/devices/View</path>
<url>/devices/*/*/info</url>
<method>GET</method>
</Permission>
<Permission> <Permission>
<name>View device applications</name> <name>View device applications</name>
<path>/device-mgt/admin/devices/View-Applications</path> <path>/device-mgt/admin/devices/View-Applications</path>

@ -20,11 +20,13 @@ package org.wso2.carbon.device.mgt.core.notification.mgt.dao;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
import org.wso2.carbon.device.mgt.common.IllegalTransactionStateException; import org.wso2.carbon.device.mgt.common.IllegalTransactionStateException;
import org.wso2.carbon.device.mgt.common.TransactionManagementException; import org.wso2.carbon.device.mgt.common.TransactionManagementException;
import org.wso2.carbon.device.mgt.common.UnsupportedDatabaseEngineException;
import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig; import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig;
import org.wso2.carbon.device.mgt.core.config.datasource.JNDILookupDefinition; import org.wso2.carbon.device.mgt.core.config.datasource.JNDILookupDefinition;
import org.wso2.carbon.device.mgt.core.notification.mgt.dao.impl.NotificationDAOImpl; import org.wso2.carbon.device.mgt.core.notification.mgt.dao.impl.*;
import org.wso2.carbon.device.mgt.core.notification.mgt.dao.util.NotificationDAOUtil; import org.wso2.carbon.device.mgt.core.notification.mgt.dao.util.NotificationDAOUtil;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -39,20 +41,47 @@ import java.util.List;
public class NotificationManagementDAOFactory { public class NotificationManagementDAOFactory {
private static DataSource dataSource; private static DataSource dataSource;
private static String databaseEngine;
private static final Log log = LogFactory.getLog(NotificationManagementDAOFactory.class); private static final Log log = LogFactory.getLog(NotificationManagementDAOFactory.class);
private static ThreadLocal<Connection> currentConnection = new ThreadLocal<Connection>(); private static ThreadLocal<Connection> currentConnection = new ThreadLocal<Connection>();
public static NotificationDAO getNotificationDAO() { public static NotificationDAO getNotificationDAO() {
return new NotificationDAOImpl(); if (databaseEngine != null) {
switch (databaseEngine) {
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_ORACLE:
return new OracleNotificationDAOImpl();
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_MSSQL:
return new SQLServerNotificationDAOImpl();
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_POSTGRESQL:
return new PostgreSQLNotificationDAOImpl();
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_H2:
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_MYSQL:
return new GenericNotificationDAOImpl();
default:
throw new UnsupportedDatabaseEngineException("Unsupported database engine : " + databaseEngine);
}
}
throw new IllegalStateException("Database engine has not initialized properly.");
} }
public static void init(DataSourceConfig config) { public static void init(DataSourceConfig config) {
dataSource = resolveDataSource(config); dataSource = resolveDataSource(config);
try {
databaseEngine = dataSource.getConnection().getMetaData().getDatabaseProductName();
} catch (SQLException e) {
log.error("Error occurred while retrieving config.datasource connection", e);
}
} }
public static void init(DataSource dtSource) { public static void init(DataSource dtSource) {
dataSource = dtSource; dataSource = dtSource;
try {
databaseEngine = dataSource.getConnection().getMetaData().getDatabaseProductName();
} catch (SQLException e) {
log.error("Error occurred while retrieving config.datasource connection", e);
} }
}
public static void beginTransaction() throws TransactionManagementException { public static void beginTransaction() throws TransactionManagementException {
Connection conn = currentConnection.get(); Connection conn = currentConnection.get();

@ -35,7 +35,7 @@ import java.util.List;
/** /**
* Implementation of NotificationDAO which includes the methods to do CRUD operations on notification. * Implementation of NotificationDAO which includes the methods to do CRUD operations on notification.
*/ */
public abstract class NotificationDAOImpl implements NotificationDAO { public abstract class AbstractNotificationDAOImpl implements NotificationDAO {
@Override @Override
public int addNotification(int deviceId, int tenantId, public int addNotification(int deviceId, int tenantId,
@ -87,7 +87,7 @@ public abstract class NotificationDAOImpl implements NotificationDAO {
rs = stmt.executeQuery(); rs = stmt.executeQuery();
while (rs.next()) { while (rs.next()) {
notification = this.getNotification(rs); notification = NotificationDAOUtil.getNotification(rs);
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new NotificationManagementException( throw new NotificationManagementException(
@ -165,7 +165,7 @@ public abstract class NotificationDAOImpl implements NotificationDAO {
rs = stmt.executeQuery(); rs = stmt.executeQuery();
notifications = new ArrayList<>(); notifications = new ArrayList<>();
while (rs.next()) { while (rs.next()) {
notifications.add(this.getNotification(rs)); notifications.add(NotificationDAOUtil.getNotification(rs));
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new NotificationManagementException( throw new NotificationManagementException(
@ -238,7 +238,7 @@ public abstract class NotificationDAOImpl implements NotificationDAO {
rs = stmt.executeQuery(); rs = stmt.executeQuery();
notifications = new ArrayList<>(); notifications = new ArrayList<>();
while (rs.next()) { while (rs.next()) {
notifications.add(this.getNotification(rs)); notifications.add(NotificationDAOUtil.getNotification(rs));
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new NotificationManagementException( throw new NotificationManagementException(
@ -283,59 +283,4 @@ public abstract class NotificationDAOImpl implements NotificationDAO {
} }
return notificationCountByStatus; return notificationCountByStatus;
} }
@Override
public List<Notification> getNotificationsByStatus(PaginationRequest request, Notification.Status status, int tenantId) throws
NotificationManagementException{
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
List<Notification> notifications = null;
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 = ?";
sql = sql + " LIMIT ?,?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setString(2, status.toString());
stmt.setInt(3, tenantId);
int paramIdx = 4;
stmt.setInt(paramIdx++, request.getStartIndex());
stmt.setInt(paramIdx, request.getRowCount());
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.setOperationId(rs.getInt("OPERATION_ID"));
notification.setDescription(rs.getString("DESCRIPTION"));
notification.setStatus(rs.getString("STATUS"));
return notification;
}
} }

@ -0,0 +1,122 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* you may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.device.mgt.core.notification.mgt.dao.impl;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
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.core.notification.mgt.dao.NotificationManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.notification.mgt.dao.util.NotificationDAOUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* This class holds the generic implementation of NotificationDAO which can be used to support ANSI db syntax.
*/
public class GenericNotificationDAOImpl extends AbstractNotificationDAOImpl {
@Override
public List<Notification> getAllNotifications(PaginationRequest request, int tenantId) throws
NotificationManagementException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
List<Notification> notifications = null;
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 = ?) n1 WHERE n1.DEVICE_ID = d.ID AND d.DEVICE_TYPE_ID=t.ID AND TENANT_ID = ?";
sql = sql + " LIMIT ?,?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setInt(2, tenantId);
int paramIdx = 3;
stmt.setInt(paramIdx++, request.getStartIndex());
stmt.setInt(paramIdx, request.getRowCount());
rs = stmt.executeQuery();
notifications = new ArrayList<>();
while (rs.next()) {
notifications.add(NotificationDAOUtil.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
public List<Notification> getNotificationsByStatus(PaginationRequest request, Notification.Status status, int tenantId) throws
NotificationManagementException{
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
List<Notification> notifications = null;
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 = ?";
sql = sql + " LIMIT ?,?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setString(2, status.toString());
stmt.setInt(3, tenantId);
int paramIdx = 4;
stmt.setInt(paramIdx++, request.getStartIndex());
stmt.setInt(paramIdx, request.getRowCount());
rs = stmt.executeQuery();
notifications = new ArrayList<>();
while (rs.next()) {
notifications.add(NotificationDAOUtil.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;
}
}

@ -0,0 +1,122 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* you may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.device.mgt.core.notification.mgt.dao.impl;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
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.core.notification.mgt.dao.NotificationManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.notification.mgt.dao.util.NotificationDAOUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* This class holds the Oracle implementation of NotificationDAO which can be used to support Oracle db syntax.
*/
public class OracleNotificationDAOImpl extends AbstractNotificationDAOImpl {
@Override
public List<Notification> getAllNotifications(PaginationRequest request, int tenantId) throws
NotificationManagementException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
List<Notification> notifications = null;
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 = ?) n1 WHERE n1.DEVICE_ID = d.ID AND d.DEVICE_TYPE_ID=t.ID AND TENANT_ID = ?";
sql = sql + " WHERE OFFSET >= ? AND ROWNUM <= ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setInt(2, tenantId);
int paramIdx = 3;
stmt.setInt(paramIdx++, request.getStartIndex());
stmt.setInt(paramIdx, request.getRowCount());
rs = stmt.executeQuery();
notifications = new ArrayList<>();
while (rs.next()) {
notifications.add(NotificationDAOUtil.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
public List<Notification> getNotificationsByStatus(PaginationRequest request, Notification.Status status, int tenantId) throws
NotificationManagementException{
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
List<Notification> notifications = null;
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 = ?";
sql = sql + " OFFSET >= ? AND ROWNUM <= ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setString(2, status.toString());
stmt.setInt(3, tenantId);
int paramIdx = 4;
stmt.setInt(paramIdx++, request.getStartIndex());
stmt.setInt(paramIdx, request.getRowCount());
rs = stmt.executeQuery();
notifications = new ArrayList<>();
while (rs.next()) {
notifications.add(NotificationDAOUtil.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;
}
}

@ -0,0 +1,122 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* you may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.device.mgt.core.notification.mgt.dao.impl;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
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.core.notification.mgt.dao.NotificationManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.notification.mgt.dao.util.NotificationDAOUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* This class holds the implementation of NotificationDAO which can be used to support PostgreSQL db syntax.
*/
public class PostgreSQLNotificationDAOImpl extends AbstractNotificationDAOImpl {
@Override
public List<Notification> getAllNotifications(PaginationRequest request, int tenantId) throws
NotificationManagementException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
List<Notification> notifications = null;
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 = ?) n1 WHERE n1.DEVICE_ID = d.ID AND d.DEVICE_TYPE_ID=t.ID AND TENANT_ID = ?";
sql = sql + " LIMIT ? OFFSET ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setInt(2, tenantId);
int paramIdx = 3;
stmt.setInt(paramIdx++, request.getRowCount());
stmt.setInt(paramIdx, request.getStartIndex());
rs = stmt.executeQuery();
notifications = new ArrayList<>();
while (rs.next()) {
notifications.add(NotificationDAOUtil.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
public List<Notification> getNotificationsByStatus(PaginationRequest request, Notification.Status status, int tenantId) throws
NotificationManagementException{
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
List<Notification> notifications = null;
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 = ?";
sql = sql + " LIMIT ? OFFSET ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setString(2, status.toString());
stmt.setInt(3, tenantId);
int paramIdx = 4;
stmt.setInt(paramIdx++, request.getRowCount());
stmt.setInt(paramIdx, request.getStartIndex());
rs = stmt.executeQuery();
notifications = new ArrayList<>();
while (rs.next()) {
notifications.add(NotificationDAOUtil.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;
}
}

@ -0,0 +1,122 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* you may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.device.mgt.core.notification.mgt.dao.impl;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
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.core.notification.mgt.dao.NotificationManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.notification.mgt.dao.util.NotificationDAOUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* This class holds the implementation of NotificationDAO which can be used to support SQLServer db syntax.
*/
public class SQLServerNotificationDAOImpl extends AbstractNotificationDAOImpl {
@Override
public List<Notification> getAllNotifications(PaginationRequest request, int tenantId) throws
NotificationManagementException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
List<Notification> notifications = null;
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 = ?) n1 WHERE n1.DEVICE_ID = d.ID AND d.DEVICE_TYPE_ID=t.ID AND TENANT_ID = ?";
sql = sql + " OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setInt(2, tenantId);
int paramIdx = 3;
stmt.setInt(paramIdx++, request.getStartIndex());
stmt.setInt(paramIdx, request.getRowCount());
rs = stmt.executeQuery();
notifications = new ArrayList<>();
while (rs.next()) {
notifications.add(NotificationDAOUtil.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
public List<Notification> getNotificationsByStatus(PaginationRequest request, Notification.Status status, int tenantId) throws
NotificationManagementException{
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
List<Notification> notifications = null;
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 = ?";
sql = sql + " OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setString(2, status.toString());
stmt.setInt(3, tenantId);
int paramIdx = 4;
stmt.setInt(paramIdx++, request.getStartIndex());
stmt.setInt(paramIdx, request.getRowCount());
rs = stmt.executeQuery();
notifications = new ArrayList<>();
while (rs.next()) {
notifications.add(NotificationDAOUtil.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;
}
}

@ -21,6 +21,7 @@ package org.wso2.carbon.device.mgt.core.notification.mgt.dao.util;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.CarbonContext;
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.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.user.api.UserStoreException;
@ -123,4 +124,13 @@ public class NotificationDAOUtil {
throw new RuntimeException("Error in looking up data source: " + e.getMessage(), e); throw new RuntimeException("Error in looking up data source: " + e.getMessage(), e);
} }
} }
public static Notification getNotification(ResultSet rs) throws SQLException {
Notification notification = new Notification();
notification.setNotificationId(rs.getInt("NOTIFICATION_ID"));
notification.setOperationId(rs.getInt("OPERATION_ID"));
notification.setDescription(rs.getString("DESCRIPTION"));
notification.setStatus(rs.getString("STATUS"));
return notification;
}
} }

@ -46,8 +46,8 @@ public class PostgreSQLOperationDAOImpl extends GenericOperationDAOImpl {
List<Operation> operations = new ArrayList<Operation>(); List<Operation> operations = new ArrayList<Operation>();
try { try {
Connection conn = OperationManagementDAOFactory.getConnection(); Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, " + String sql = "SELECT o.ID, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, " +
"OPERATION_CODE, om.STATUS FROM DM_OPERATION o " + "o.OPERATION_CODE, om.STATUS FROM DM_OPERATION o " +
"INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm " + "INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm " +
"WHERE dm.ENROLMENT_ID = ?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP DESC LIMIT ? OFFSET ?"; "WHERE dm.ENROLMENT_ID = ?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP DESC LIMIT ? OFFSET ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
@ -72,7 +72,7 @@ public class PostgreSQLOperationDAOImpl extends GenericOperationDAOImpl {
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new OperationManagementDAOException("SQL error occurred while retrieving the operation " + throw new OperationManagementDAOException("SQL error occurred while retrieving the operation " +
"available for the device'" + enrolmentId + "' with status '", e); "available for the device'" + enrolmentId, e);
} finally { } finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOUtil.cleanupResources(stmt, rs);
} }
@ -89,7 +89,7 @@ public class PostgreSQLOperationDAOImpl extends GenericOperationDAOImpl {
List<Operation> operations = new ArrayList<Operation>(); List<Operation> operations = new ArrayList<Operation>();
try { try {
Connection conn = OperationManagementDAOFactory.getConnection(); Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, OPERATION_CODE " + String sql = "SELECT o.ID, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, o.OPERATION_CODE " +
"FROM DM_OPERATION o " + "FROM DM_OPERATION o " +
"INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm " + "INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm " +
"WHERE dm.ENROLMENT_ID = ? AND dm.STATUS = ?) om ON o.ID = om.OPERATION_ID ORDER BY " + "WHERE dm.ENROLMENT_ID = ? AND dm.STATUS = ?) om ON o.ID = om.OPERATION_ID ORDER BY " +

Loading…
Cancel
Save