revert-70aa11f8
prabathabey 9 years ago
commit bd6316d8b5

@ -0,0 +1,93 @@
/*
* Copyright (c) 2015, 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.common.notification.mgt;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
/**
* DTO of Notification object which is used to communicate Operation notifications to MDM core.
*/
public class Notification {
public enum Status{
NEW, CHECKED
}
public enum Type{
ALERT,
}
private int notificationId;
private DeviceIdentifier deviceIdentifier;
private String description;
private int operationId;
private Status status;
public Status getStatus() {
return status;
}
public void setStatus(String status) {
this.status = Status.valueOf(status);
}
public int getNotificationId() {
return notificationId;
}
public void setNotificationId(int notificationId) {
this.notificationId = notificationId;
}
public DeviceIdentifier getDeviceIdentifier() {
return deviceIdentifier;
}
public void setDeviceIdentifier(DeviceIdentifier deviceIdentifier) {
this.deviceIdentifier = deviceIdentifier;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getOperationId() {
return operationId;
}
public void setOperationId(int operationId) {
this.operationId = operationId;
}
@Override
public String toString() {
return "Notification{" +
"notificationId='" + notificationId + '\'' +
", deviceId=" + deviceIdentifier.getId() +
", deviceType=" + deviceIdentifier.getType() +
", status=" + status +
", description='" + description + '\'' +
", operationId='" + operationId + '\'' +
'}';
}
}

@ -0,0 +1,60 @@
/*
* Copyright (c) 2015, 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.common.notification.mgt;
/**
* Custom exception class to be used in NotificationMgmt related functionalities.
*/
public class NotificationManagementException extends Exception {
private static final long serialVersionUID = -8933146283800122660L;
private String errorMessage;
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
public NotificationManagementException(String msg, Exception nestedEx) {
super(msg, nestedEx);
setErrorMessage(msg);
}
public NotificationManagementException(String message, Throwable cause) {
super(message, cause);
setErrorMessage(message);
}
public NotificationManagementException(String msg) {
super(msg);
setErrorMessage(msg);
}
public NotificationManagementException() {
super();
}
public NotificationManagementException(Throwable cause) {
super(cause);
}
}

@ -0,0 +1,36 @@
/*
* Copyright (c) 2015, 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.common.notification.mgt;
import java.util.List;
/**
* Defines the contract of NotificationManagementService.
*/
public interface NotificationManagementService {
public boolean addNotification(Notification notification) throws NotificationManagementException;
public boolean updateNotification(Notification notification) throws NotificationManagementException;
public boolean updateNotificationStatus(int notificationId, Notification.Status status) throws
NotificationManagementException;
public List<Notification> getAllNotifications() throws NotificationManagementException;
public List<Notification> getNotificationsByStatus(Notification.Status status) throws
NotificationManagementException;
}

@ -36,8 +36,9 @@ import java.io.StringWriter;
import java.nio.charset.Charset; import java.nio.charset.Charset;
/** /**
* This class implements all the functionality exposed as part of the TenantConfigurationManagementService. Main usage of * This class implements all the functionality exposed as part of the TenantConfigurationManagementService.
* this module is, saving/retrieving tenant configurations to the registry. * Main usage of this module is saving/retrieving tenant configurations to the registry.
*
*/ */
public class TenantConfigurationManagementServiceImpl public class TenantConfigurationManagementServiceImpl
implements TenantConfigurationManagementService { implements TenantConfigurationManagementService {

@ -63,6 +63,16 @@ public interface DeviceDAO {
*/ */
int removeDevice(DeviceIdentifier deviceId, int tenantId) throws DeviceManagementDAOException; int removeDevice(DeviceIdentifier deviceId, int tenantId) throws DeviceManagementDAOException;
/**
* This method is used to retrieve a device of a given device-identifier.
*
* @param deviceIdentifier device id.
* @param tenantId tenant id.
* @return returns the device object.
* @throws DeviceManagementDAOException
*/
Device getDevice(DeviceIdentifier deviceIdentifier, int tenantId) throws DeviceManagementDAOException;
/** /**
* This method is used to retrieve a device of a given id. * This method is used to retrieve a device of a given id.
* *
@ -71,7 +81,7 @@ public interface DeviceDAO {
* @return returns the device object. * @return returns the device object.
* @throws DeviceManagementDAOException * @throws DeviceManagementDAOException
*/ */
Device getDevice(DeviceIdentifier deviceId, int tenantId) throws DeviceManagementDAOException; Device getDevice(int deviceId, int tenantId) throws DeviceManagementDAOException;
/** /**
* This method is used to retrieve all the devices of a given tenant. * This method is used to retrieve all the devices of a given tenant.

@ -102,7 +102,7 @@ public class DeviceDAOImpl implements DeviceDAO {
} }
@Override @Override
public Device getDevice(DeviceIdentifier deviceId, int tenantId) throws DeviceManagementDAOException { public Device getDevice(DeviceIdentifier deviceIdentifier, int tenantId) throws DeviceManagementDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
@ -116,8 +116,8 @@ public class DeviceDAOImpl implements DeviceDAO {
"t.NAME = ? AND d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID " + "t.NAME = ? AND d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID " +
"AND TENANT_ID = ?"; "AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setString(1, deviceId.getType()); stmt.setString(1, deviceIdentifier.getType());
stmt.setString(2, deviceId.getId()); stmt.setString(2, deviceIdentifier.getId());
stmt.setInt(3, tenantId); stmt.setInt(3, tenantId);
stmt.setInt(4, tenantId); stmt.setInt(4, tenantId);
rs = stmt.executeQuery(); rs = stmt.executeQuery();
@ -126,7 +126,38 @@ public class DeviceDAOImpl implements DeviceDAO {
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while listing devices for type " + throw new DeviceManagementDAOException("Error occurred while listing devices for type " +
"'" + deviceId.getType() + "'", e); "'" + deviceIdentifier.getType() + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
return device;
}
@Override
public Device getDevice(int deviceId, int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
Device device = null;
try {
conn = this.getConnection();
String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " +
"d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME, " +
"t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE " +
"d.ID = ? AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID " +
"AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, deviceId);
stmt.setInt(2, tenantId);
stmt.setInt(3, tenantId);
rs = stmt.executeQuery();
if (rs.next()) {
device = this.loadDevice(rs);
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving device for id " +
"'" + deviceId + "'", e);
} finally { } finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs); DeviceManagementDAOUtil.cleanupResources(stmt, rs);
} }

@ -25,6 +25,8 @@ import org.wso2.carbon.apimgt.impl.APIManagerConfigurationService;
import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException;
import org.wso2.carbon.device.mgt.common.configuration.mgt.TenantConfigurationManagementService; import org.wso2.carbon.device.mgt.common.configuration.mgt.TenantConfigurationManagementService;
import org.wso2.carbon.device.mgt.common.notification.mgt.Notification;
import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagementService;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager;
import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService;
@ -39,6 +41,7 @@ import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig;
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.tenant.TenantConfigurationManagementServiceImpl; import org.wso2.carbon.device.mgt.core.config.tenant.TenantConfigurationManagementServiceImpl;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.notification.mgt.NotificationManagementServiceImpl;
import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl; import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
@ -178,6 +181,11 @@ public class DeviceManagementServiceComponent {
tenantConfiguration = new TenantConfigurationManagementServiceImpl(); tenantConfiguration = new TenantConfigurationManagementServiceImpl();
bundleContext.registerService(TenantConfigurationManagementService.class.getName(), tenantConfiguration, null); bundleContext.registerService(TenantConfigurationManagementService.class.getName(), tenantConfiguration, null);
/* Registering Notification Service */
NotificationManagementService notificationManagementService
= new NotificationManagementServiceImpl();
bundleContext.registerService(NotificationManagementService.class.getName(), notificationManagementService, null);
/* Registering App Management service */ /* Registering App Management service */
try { try {
AppManagementConfigurationManager.getInstance().initConfig(); AppManagementConfigurationManager.getInstance().initConfig();

@ -0,0 +1,172 @@
/*
* Copyright (c) 2015, 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;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.TransactionManagementException;
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.NotificationManagementService;
import org.wso2.carbon.device.mgt.core.dao.DeviceDAO;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.notification.mgt.dao.NotificationDAO;
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.SQLException;
import java.util.List;
/**
* This class implements the NotificationManagementService.
*/
public class NotificationManagementServiceImpl implements NotificationManagementService {
private static final Log log = LogFactory.getLog(NotificationManagementServiceImpl.class);
private NotificationDAO notificationDAO;
private DeviceDAO deviceDAO;
public NotificationManagementServiceImpl() {
this.notificationDAO = NotificationManagementDAOFactory.getNotificationDAO();
this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO();
}
@Override
public boolean addNotification(Notification notification) throws NotificationManagementException {
boolean status = false;
int deviceId, tenantId;
if (log.isDebugEnabled()) {
log.debug("Adding a Notification : [" + notification.toString() + "]");
}
try {
tenantId = NotificationDAOUtil.getTenantId();
DeviceManagementDAOFactory.openConnection();
Device device = deviceDAO.getDevice(notification.getDeviceIdentifier(), tenantId);
deviceId = device.getId();
} catch (SQLException e) {
throw new NotificationManagementException("Error occurred while opening a connection to" +
" the data source", e);
} catch (DeviceManagementDAOException e) {
throw new NotificationManagementException("Error occurred while retriving device data for " +
" adding notification", e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
try {
NotificationManagementDAOFactory.beginTransaction();
int notificationId = notificationDAO.addNotification(deviceId, tenantId, notification);
NotificationManagementDAOFactory.commitTransaction();
if (log.isDebugEnabled()) {
log.debug("Notification id : " + notificationId +" was added to the table.");
}
if(notificationId > 0) {
status = true;
}
} catch (TransactionManagementException e) {
NotificationManagementDAOFactory.rollbackTransaction();
throw new NotificationManagementException("Error occurred while adding notification", e);
} finally {
NotificationManagementDAOFactory.closeConnection();
}
return status;
}
@Override
public boolean updateNotification(Notification notification) throws NotificationManagementException {
boolean status = false;
if (log.isDebugEnabled()) {
log.debug("Updating Notification : [" + notification.toString() + "]");
}
try {
NotificationManagementDAOFactory.beginTransaction();
if(notificationDAO.updateNotification(notification) > 0 ) {
status = true;
}
NotificationManagementDAOFactory.commitTransaction();
if (log.isDebugEnabled()) {
log.debug("Notification id : " + notification.getNotificationId() +
" has updated successfully.");
}
} catch (TransactionManagementException e) {
NotificationManagementDAOFactory.rollbackTransaction();
throw new NotificationManagementException("Error occurred while updating notification ", e);
} finally {
NotificationManagementDAOFactory.closeConnection();
}
return status;
}
@Override
public boolean updateNotificationStatus(int notificationId, Notification.Status status)
throws NotificationManagementException {
boolean operationStatus = false;
if (log.isDebugEnabled()) {
log.debug("Updating Notification id : " + notificationId);
}
try {
NotificationManagementDAOFactory.beginTransaction();
if(notificationDAO.updateNotificationStatus(notificationId, status) > 0 ) {
operationStatus = true;
}
NotificationManagementDAOFactory.commitTransaction();
if (log.isDebugEnabled()) {
log.debug("Notification id : " + notificationId +" has updated successfully.");
}
} catch (TransactionManagementException e) {
NotificationManagementDAOFactory.rollbackTransaction();
throw new NotificationManagementException("Error occurred while updating notification", e);
} finally {
NotificationManagementDAOFactory.closeConnection();
}
return operationStatus;
}
@Override
public List<Notification> getAllNotifications() throws NotificationManagementException{
try {
NotificationManagementDAOFactory.openConnection();
return notificationDAO.getAllNotifications(NotificationDAOUtil.getTenantId());
} catch (SQLException e) {
throw new NotificationManagementException("Error occurred while opening a connection to" +
" the data source", e);
} finally {
NotificationManagementDAOFactory.closeConnection();
}
}
@Override
public List<Notification> getNotificationsByStatus(Notification.Status status)
throws NotificationManagementException{
try {
NotificationManagementDAOFactory.openConnection();
return notificationDAO.getNotificationsByStatus(status, NotificationDAOUtil.getTenantId());
} catch (SQLException e) {
throw new NotificationManagementException("Error occurred while opening a connection " +
"to the data source", e);
} finally {
NotificationManagementDAOFactory.closeConnection();
}
}
}

@ -0,0 +1,83 @@
/*
* Copyright (c) 2015, 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;
import org.wso2.carbon.device.mgt.common.notification.mgt.Notification;
import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagementException;
import java.util.List;
/**
* This class defines the methods to be implemented by NotificationDAO layer.
*/
public interface NotificationDAO {
/**
* This method is used to add a notification.
*
* @param deviceId device id.
* @param tenantId tenant id.
* @param notification Notification object.
* @return returns the id of the persisted Notification record.
* @throws NotificationManagementException
*/
int addNotification(int deviceId, int tenantId, Notification notification) throws
NotificationManagementException;
/**
* This method is used to update a notification.
*
* @param notification Notification object.
* @return returns the no of updated records.
* @throws NotificationManagementException
*/
int updateNotification(Notification notification) throws NotificationManagementException;
/**
* This method is used to update a notification status.
*
* @param notificationId notification id.
* @param status Notification.Status.
* @return returns the no of updated records.
* @throws NotificationManagementException
*/
int updateNotificationStatus(int notificationId, Notification.Status status)
throws NotificationManagementException;
/**
* This method is used to get all notifications based on tenant-id.
*
* @param tenantId tenant id.
* @return returns the matching notifications.
* @throws NotificationManagementException
*/
List<Notification> getAllNotifications(int tenantId) throws NotificationManagementException;
/**
* This method is used to get all notifications based on notification-status.
*
* @param status Notification.Status.
* @param tenantId tenant id.
* @return returns the matching notifications.
* @throws NotificationManagementException
*/
List<Notification> getNotificationsByStatus(Notification.Status status, int tenantId) throws
NotificationManagementException;
}

@ -0,0 +1,171 @@
/*
* Copyright (c) 2015, 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;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.IllegalTransactionStateException;
import org.wso2.carbon.device.mgt.common.TransactionManagementException;
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.notification.mgt.dao.impl.NotificationDAOImpl;
import org.wso2.carbon.device.mgt.core.notification.mgt.dao.util.NotificationDAOUtil;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Hashtable;
import java.util.List;
/**
* DAO factory class to be used in NotificationManagement related functionalities.
*/
public class NotificationManagementDAOFactory {
private static DataSource dataSource;
private static final Log log = LogFactory.getLog(NotificationManagementDAOFactory.class);
private static ThreadLocal<Connection> currentConnection = new ThreadLocal<Connection>();
public static NotificationDAO getNotificationDAO() {
return new NotificationDAOImpl();
}
public static void init(DataSourceConfig config) {
dataSource = resolveDataSource(config);
}
public static void init(DataSource dtSource) {
dataSource = dtSource;
}
public static void beginTransaction() throws TransactionManagementException {
Connection conn = currentConnection.get();
if (conn != null) {
throw new IllegalTransactionStateException("A transaction is already active within the context of " +
"this particular thread. Therefore, calling 'beginTransaction/openConnection' while another " +
"transaction is already active is a sign of improper transaction handling");
}
try {
conn = dataSource.getConnection();
conn.setAutoCommit(false);
currentConnection.set(conn);
} catch (SQLException e) {
throw new TransactionManagementException("Error occurred while retrieving config.datasource connection", e);
}
}
public static void openConnection() throws SQLException {
Connection conn = currentConnection.get();
if (conn != null) {
throw new IllegalTransactionStateException("A transaction is already active within the context of " +
"this particular thread. Therefore, calling 'beginTransaction/openConnection' while another " +
"transaction is already active is a sign of improper transaction handling");
}
conn = dataSource.getConnection();
currentConnection.set(conn);
}
public static Connection getConnection() throws SQLException {
Connection conn = currentConnection.get();
if (conn == null) {
throw new IllegalTransactionStateException("No connection is associated with the current transaction. " +
"This might have ideally been caused by not properly initiating the transaction via " +
"'beginTransaction'/'openConnection' methods");
}
return conn;
}
public static void commitTransaction() {
Connection conn = currentConnection.get();
if (conn == null) {
throw new IllegalTransactionStateException("No connection is associated with the current transaction. " +
"This might have ideally been caused by not properly initiating the transaction via " +
"'beginTransaction'/'openConnection' methods");
}
try {
conn.commit();
} catch (SQLException e) {
log.error("Error occurred while committing the transaction", e);
}
}
public static void rollbackTransaction() {
Connection conn = currentConnection.get();
if (conn == null) {
throw new IllegalTransactionStateException("No connection is associated with the current transaction. " +
"This might have ideally been caused by not properly initiating the transaction via " +
"'beginTransaction'/'openConnection' methods");
}
try {
conn.rollback();
} catch (SQLException e) {
log.warn("Error occurred while roll-backing the transaction", e);
}
}
public static void closeConnection() {
Connection conn = currentConnection.get();
if (conn == null) {
throw new IllegalTransactionStateException("No connection is associated with the current transaction. " +
"This might have ideally been caused by not properly initiating the transaction via " +
"'beginTransaction'/'openConnection' methods");
}
try {
conn.close();
} catch (SQLException e) {
log.warn("Error occurred while close the connection");
}
currentConnection.remove();
}
/**
* Resolve data source from the data source definition
*
* @param config data source configuration
* @return data source resolved from the data source definition
*/
private static DataSource resolveDataSource(DataSourceConfig config) {
DataSource dataSource = null;
if (config == null) {
throw new RuntimeException(
"Device Management Repository data source configuration " + "is null and " +
"thus, is not initialized");
}
JNDILookupDefinition jndiConfig = config.getJndiLookupDefinition();
if (jndiConfig != null) {
if (log.isDebugEnabled()) {
log.debug("Initializing Device Management Repository data source using the JNDI " +
"Lookup Definition");
}
List<JNDILookupDefinition.JNDIProperty> jndiPropertyList =
jndiConfig.getJndiProperties();
if (jndiPropertyList != null) {
Hashtable<Object, Object> jndiProperties = new Hashtable<Object, Object>();
for (JNDILookupDefinition.JNDIProperty prop : jndiPropertyList) {
jndiProperties.put(prop.getName(), prop.getValue());
}
dataSource = NotificationDAOUtil.lookupDataSource(jndiConfig.getJndiName(), jndiProperties);
} else {
dataSource = NotificationDAOUtil.lookupDataSource(jndiConfig.getJndiName(), null);
}
}
return dataSource;
}
}

@ -0,0 +1,214 @@
/*
* Copyright (c) 2015, 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.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.NotificationManagementException;
import org.wso2.carbon.device.mgt.core.notification.mgt.dao.NotificationDAO;
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.*;
import java.util.ArrayList;
import java.util.List;
/**
* Implementation of NotificationDAO which includes the methods to do CRUD operations on notification.
*/
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;
ResultSet rs;
int notificationId = -1;
try {
NotificationManagementDAOFactory.beginTransaction();
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);
}
NotificationManagementDAOFactory.commitTransaction();
} catch (Exception e) {
NotificationManagementDAOFactory.rollbackTransaction();
throw new NotificationManagementException("Error occurred while adding the " +
"Notification for device id : " + deviceId,
e);
} finally {
NotificationManagementDAOFactory.closeConnection();
}
return notificationId;
}
@Override
public int updateNotification(Notification notification)
throws NotificationManagementException {
Connection conn;
PreparedStatement stmt;
int rows;
try {
NotificationManagementDAOFactory.beginTransaction();
conn = NotificationManagementDAOFactory.getConnection();
String sql = "UPDATE DM_NOTIFICATION SET OPERATION_ID = ?, STATUS = ?, DESCRIPTION = ? " +
"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());
rows = stmt.executeUpdate();
NotificationManagementDAOFactory.commitTransaction();
} catch (Exception e) {
NotificationManagementDAOFactory.rollbackTransaction();
throw new NotificationManagementException("Error occurred while updating the " +
"Notification id : " + notification.getNotificationId(), e);
} finally {
NotificationManagementDAOFactory.closeConnection();
}
return rows;
}
@Override
public int updateNotificationStatus(int notificationId, Notification.Status status)
throws NotificationManagementException {
Connection conn;
PreparedStatement stmt;
int rows;
try {
NotificationManagementDAOFactory.beginTransaction();
conn = NotificationManagementDAOFactory.getConnection();
String sql = "UPDATE DM_NOTIFICATION SET STATUS = ? WHERE NOTIFICATION_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, status.toString());
stmt.setInt(2, notificationId);
rows = stmt.executeUpdate();
NotificationManagementDAOFactory.commitTransaction();
} catch (Exception e) {
NotificationManagementDAOFactory.rollbackTransaction();
throw new NotificationManagementException("Error occurred while updating the status of " +
"Notification id : " + notificationId, e);
} finally {
NotificationManagementDAOFactory.closeConnection();
}
return rows;
}
@Override
public List<Notification> getAllNotifications(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 = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setInt(2, 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", e);
} finally {
NotificationDAOUtil.cleanupResources(stmt, rs);
}
return notifications;
}
@Override
public List<Notification> getNotificationsByStatus(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 = ?";
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;
}
}

@ -0,0 +1,126 @@
/*
* Copyright (c) 2015, 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.util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagementException;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.user.core.tenant.TenantManager;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Hashtable;
/**
* This class includes the utility methods required by NotificationMgmt functionalities.
*/
public class NotificationDAOUtil {
private static final Log log = LogFactory.getLog(NotificationDAOUtil.class);
public static void cleanupResources(Connection conn, PreparedStatement stmt, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
log.warn("Error occurred while closing result set", e);
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
log.warn("Error occurred while closing prepared statement", e);
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
log.warn("Error occurred while closing database connection", e);
}
}
}
public static void cleanupResources(PreparedStatement stmt, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
log.warn("Error occurred while closing result set", e);
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
log.warn("Error occurred while closing prepared statement", e);
}
}
}
/**
* Get id of the current tenant.
*
* @return tenant id
* @throws org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException if an error is observed when getting tenant id
*/
public static int getTenantId() throws NotificationManagementException {
CarbonContext context = CarbonContext.getThreadLocalCarbonContext();
int tenantId = context.getTenantId();
if (tenantId != MultitenantConstants.INVALID_TENANT_ID) {
return tenantId;
}
String tenantDomain = context.getTenantDomain();
if (tenantDomain == null) {
String msg = "Tenant domain is not properly set and thus, is null";
throw new NotificationManagementException(msg);
}
TenantManager tenantManager = DeviceManagementDataHolder.getInstance().getTenantManager();
try {
tenantId = tenantManager.getTenantId(tenantDomain);
} catch (UserStoreException e) {
String msg =
"Error occurred while retrieving id from the domain of tenant " + tenantDomain;
throw new NotificationManagementException(msg);
}
return tenantId;
}
public static DataSource lookupDataSource(String dataSourceName,
final Hashtable<Object, Object> jndiProperties) {
try {
if (jndiProperties == null || jndiProperties.isEmpty()) {
return (DataSource) InitialContext.doLookup(dataSourceName);
}
final InitialContext context = new InitialContext(jndiProperties);
return (DataSource) context.lookup(dataSourceName);
} catch (Exception e) {
throw new RuntimeException("Error in looking up data source: " + e.getMessage(), e);
}
}
}

@ -382,9 +382,23 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_APPLICATION_MAPPING (
DM_APPLICATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION DM_APPLICATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
); );
-- POLICY RELATED TABLES FINISHED -- -- POLICY RELATED TABLES FINISHED --
-- NOTIFICATION TABLE --
CREATE TABLE IF NOT EXISTS DM_NOTIFICATION (
NOTIFICATION_ID INTEGER AUTO_INCREMENT NOT NULL,
DEVICE_ID INTEGER NOT NULL,
OPERATION_ID INTEGER NOT NULL,
TENANT_ID INTEGER NOT NULL,
STATUS VARCHAR(10) NULL,
DESCRIPTION VARCHAR(100) NULL,
PRIMARY KEY (ID),
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
DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
);
-- NOTIFICATION TABLE END --
-- TO:DO - Remove this INSERT sql statement. -- TO:DO - Remove this INSERT sql statement.
--Insert into DM_DEVICE_TYPE (ID,NAME) VALUES (1, 'android'); --Insert into DM_DEVICE_TYPE (ID,NAME) VALUES (1, 'android');

Loading…
Cancel
Save