Add Scheduled for push notification sending

revert-70aa11f8
warunalakshitha 8 years ago
parent 65667119f9
commit 9fa4eedca5

@ -31,4 +31,10 @@ public interface NotificationStrategy {
*/ */
void undeploy(); void undeploy();
/**
* Provides push notification configuration
*
*/
PushNotificationConfig getConfig();
} }

@ -26,11 +26,13 @@ import java.util.Map;
public class PushNotificationConfig { public class PushNotificationConfig {
private String type; private String type;
private boolean isScheduled;
Map<String, String> properties; Map<String, String> properties;
public PushNotificationConfig(String type, Map<String, String> properties) { public PushNotificationConfig(String type, boolean isScheduled, Map<String, String> properties) {
this.type = type; this.type = type;
this.properties = properties; this.properties = properties;
this.isScheduled = isScheduled;
} }
@XmlElement(name = "Type", required = true) @XmlElement(name = "Type", required = true)
@ -38,6 +40,10 @@ public class PushNotificationConfig {
return type; return type;
} }
public boolean isScheduled() {
return isScheduled;
}
public Map<String, String> getProperties() { public Map<String, String> getProperties() {
return properties; return properties;
} }

@ -86,4 +86,12 @@ public final class DeviceManagementConstants {
public static final String ACTIVITY = "ACTIVITY_"; public static final String ACTIVITY = "ACTIVITY_";
} }
public static final class PushNotifications {
private PushNotifications() {
throw new AssertionError();
}
public static final int DEFAULT_BATCH_DELAY_MILLS = 60000;
public static final int DEFAULT_BATCH_SIZE = 1000;
}
} }

@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.core.config;
import org.wso2.carbon.device.mgt.core.config.identity.IdentityConfigurations; import org.wso2.carbon.device.mgt.core.config.identity.IdentityConfigurations;
import org.wso2.carbon.device.mgt.core.config.pagination.PaginationConfiguration; import org.wso2.carbon.device.mgt.core.config.pagination.PaginationConfiguration;
import org.wso2.carbon.device.mgt.core.config.policy.PolicyConfiguration; import org.wso2.carbon.device.mgt.core.config.policy.PolicyConfiguration;
import org.wso2.carbon.device.mgt.core.config.push.notification.PushNotificationConfiguration;
import org.wso2.carbon.device.mgt.core.config.task.TaskConfiguration; import org.wso2.carbon.device.mgt.core.config.task.TaskConfiguration;
import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElement;
@ -39,7 +40,7 @@ public final class DeviceManagementConfig {
private IdentityConfigurations identityConfigurations; private IdentityConfigurations identityConfigurations;
private PolicyConfiguration policyConfiguration; private PolicyConfiguration policyConfiguration;
private PaginationConfiguration paginationConfiguration; private PaginationConfiguration paginationConfiguration;
private List<String> pushNotificationProviders; private PushNotificationConfiguration pushNotificationConfiguration;
@XmlElement(name = "ManagementRepository", required = true) @XmlElement(name = "ManagementRepository", required = true)
@ -79,16 +80,6 @@ public final class DeviceManagementConfig {
this.taskConfiguration = taskConfiguration; this.taskConfiguration = taskConfiguration;
} }
@XmlElementWrapper(name = "PushNotificationProviders", required = true)
@XmlElement(name = "Provider", required = true)
public List<String> getPushNotificationProviders() {
return pushNotificationProviders;
}
public void setPushNotificationProviders(List<String> pushNotificationProviders) {
this.pushNotificationProviders = pushNotificationProviders;
}
@XmlElement(name = "PaginationConfiguration", required = true) @XmlElement(name = "PaginationConfiguration", required = true)
public PaginationConfiguration getPaginationConfiguration() { public PaginationConfiguration getPaginationConfiguration() {
return paginationConfiguration; return paginationConfiguration;
@ -98,5 +89,13 @@ public final class DeviceManagementConfig {
this.paginationConfiguration = paginationConfiguration; this.paginationConfiguration = paginationConfiguration;
} }
@XmlElement(name = "PushNotificationConfiguration", required = true)
public PushNotificationConfiguration getPushNotificationConfiguration() {
return pushNotificationConfiguration;
}
public void setPushNotificationConfiguration(PushNotificationConfiguration pushNotificationConfiguration) {
this.pushNotificationConfiguration = pushNotificationConfiguration;
}
} }

@ -35,6 +35,10 @@ public class Operation implements Serializable {
REPEAT, NO_REPEAT, PAUSE_SEQUENCE, STOP_SEQUENCE REPEAT, NO_REPEAT, PAUSE_SEQUENCE, STOP_SEQUENCE
} }
public enum PushStatus {
SCHEDULED, IN_PROGRESS, COMPLETED
}
private String code; private String code;
private Properties properties; private Properties properties;
private Type type; private Type type;

@ -48,6 +48,7 @@ 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.permission.mgt.PermissionManagerServiceImpl; import org.wso2.carbon.device.mgt.core.permission.mgt.PermissionManagerServiceImpl;
import org.wso2.carbon.device.mgt.core.push.notification.mgt.PushNotificationProviderRepository; import org.wso2.carbon.device.mgt.core.push.notification.mgt.PushNotificationProviderRepository;
import org.wso2.carbon.device.mgt.core.push.notification.mgt.task.PushNotificationSchedulerTask;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl;
import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService;
@ -62,6 +63,11 @@ import org.wso2.carbon.utils.ConfigurationContextService;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/** /**
* @scr.component name="org.wso2.carbon.device.manager" immediate="true" * @scr.component name="org.wso2.carbon.device.manager" immediate="true"
@ -151,7 +157,8 @@ public class DeviceManagementServiceComponent {
this.initOperationsManager(); this.initOperationsManager();
PushNotificationProviderRepository pushNotificationRepo = new PushNotificationProviderRepository(); PushNotificationProviderRepository pushNotificationRepo = new PushNotificationProviderRepository();
List<String> pushNotificationProviders = config.getPushNotificationProviders(); List<String> pushNotificationProviders = config.getPushNotificationConfiguration()
.getPushNotificationProviders();
if (pushNotificationProviders != null) { if (pushNotificationProviders != null) {
for (String pushNoteProvider : pushNotificationProviders) { for (String pushNoteProvider : pushNotificationProviders) {
pushNotificationRepo.addProvider(pushNoteProvider); pushNotificationRepo.addProvider(pushNoteProvider);
@ -179,6 +186,24 @@ public class DeviceManagementServiceComponent {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Device management core bundle has been successfully initialized"); log.debug("Device management core bundle has been successfully initialized");
} }
// Start Push Notification Scheduler Task
if (config.getPushNotificationConfiguration().isSchedulerTaskEnabled()) {
if (config.getPushNotificationConfiguration().getSchedulerBatchSize() <= 0) {
log.error("Push notification batch size cannot be 0 or less than 0. Setting default batch size to:" +
" " + DeviceManagementConstants.PushNotifications.DEFAULT_BATCH_SIZE);
config.getPushNotificationConfiguration().setSchedulerBatchSize(DeviceManagementConstants.PushNotifications
.DEFAULT_BATCH_SIZE);
}
if (config.getPushNotificationConfiguration().getSchedulerBatchDelayMills() <= 0) {
log.error("Push notification batch delay cannot be 0 or less than 0. Setting default batch delay " +
"milliseconds to" + DeviceManagementConstants.PushNotifications.DEFAULT_BATCH_DELAY_MILLS);
config.getPushNotificationConfiguration().setSchedulerBatchDelayMills(DeviceManagementConstants.PushNotifications
.DEFAULT_BATCH_DELAY_MILLS);
}
ScheduledExecutorService pushNotificationExecutor = Executors.newScheduledThreadPool(1);
pushNotificationExecutor.schedule(new PushNotificationSchedulerTask(), config.getPushNotificationConfiguration()
.getSchedulerBatchDelayMills(), TimeUnit.MILLISECONDS);
}
} catch (Throwable e) { } catch (Throwable e) {
log.error("Error occurred while initializing device management core bundle", e); log.error("Error occurred while initializing device management core bundle", e);
} }

@ -145,6 +145,11 @@ public class OperationManagerImpl implements OperationManager {
int operationId = this.lookupOperationDAO(operation).addOperation(operationDto); int operationId = this.lookupOperationDAO(operation).addOperation(operationDto);
boolean isScheduledOperation = this.isTaskScheduledOperation(operation, deviceIds); boolean isScheduledOperation = this.isTaskScheduledOperation(operation, deviceIds);
boolean isNotRepeated = false; boolean isNotRepeated = false;
boolean isScheduled = false;
if (notificationStrategy != null) {
isScheduled = notificationStrategy.getConfig().isScheduled();
}
boolean hasExistingTaskOperation; boolean hasExistingTaskOperation;
int enrolmentId; int enrolmentId;
if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Control.NO_REPEAT == operationDto. if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Control.NO_REPEAT == operationDto.
@ -161,7 +166,7 @@ public class OperationManagerImpl implements OperationManager {
if (isScheduledOperation) { if (isScheduledOperation) {
hasExistingTaskOperation = operationDAO.updateTaskOperation(enrolmentId, operationCode); hasExistingTaskOperation = operationDAO.updateTaskOperation(enrolmentId, operationCode);
if (!hasExistingTaskOperation) { if (!hasExistingTaskOperation) {
operationMappingDAO.addOperationMapping(operationId, enrolmentId); operationMappingDAO.addOperationMapping(operationId, enrolmentId, isScheduled);
} }
} else if (isNotRepeated) { } else if (isNotRepeated) {
operationDAO.updateEnrollmentOperationsStatus(enrolmentId, operationCode, operationDAO.updateEnrollmentOperationsStatus(enrolmentId, operationCode,
@ -169,17 +174,22 @@ public class OperationManagerImpl implements OperationManager {
Operation.Status.PENDING, Operation.Status.PENDING,
org.wso2.carbon.device.mgt.core.dto.operation.mgt. org.wso2.carbon.device.mgt.core.dto.operation.mgt.
Operation.Status.REPEATED); Operation.Status.REPEATED);
operationMappingDAO.addOperationMapping(operationId, enrolmentId); operationMappingDAO.addOperationMapping(operationId, enrolmentId, isScheduled);
} else { } else {
operationMappingDAO.addOperationMapping(operationId, enrolmentId); operationMappingDAO.addOperationMapping(operationId, enrolmentId, isScheduled);
} }
if (notificationStrategy != null) { if (notificationStrategy != null && !notificationStrategy.getConfig().isScheduled()) {
try { try {
notificationStrategy.execute(new NotificationContext(deviceId, operation)); notificationStrategy.execute(new NotificationContext(deviceId, operation));
operationMappingDAO.updateOperationMapping(operationId, enrolmentId, org.wso2.carbon
.device.mgt.core.dto.operation.mgt.Operation.PushStatus.COMPLETED);
} catch (PushNotificationExecutionFailedException e) { } catch (PushNotificationExecutionFailedException e) {
log.error("Error occurred while sending push notifications to " + log.error("Error occurred while sending push notifications to " +
deviceId.getType() + " device carrying id '" + deviceId.getType() + " device carrying id '" +
deviceId + "'", e); deviceId + "'", e);
// Reschedule if push notification failed.
operationMappingDAO.updateOperationMapping(operationId, enrolmentId, org.wso2.carbon
.device.mgt.core.dto.operation.mgt.Operation.PushStatus.SCHEDULED);
} }
} }
} }

@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.core.operation.mgt.dao;
import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationMapping;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationResponse; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationResponse;
import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation;
@ -80,4 +81,7 @@ public interface OperationDAO {
boolean resetAttemptCount(int enrolmentId) throws OperationManagementDAOException; boolean resetAttemptCount(int enrolmentId) throws OperationManagementDAOException;
List<OperationMapping> getOperationMappingsByStatus(Operation.Status opStatus, Operation.PushStatus pushStatus,
int limit) throws OperationManagementDAOException;;
} }

@ -18,10 +18,20 @@
*/ */
package org.wso2.carbon.device.mgt.core.operation.mgt.dao; package org.wso2.carbon.device.mgt.core.operation.mgt.dao;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationMapping;
import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation;
import java.util.List;
public interface OperationMappingDAO { public interface OperationMappingDAO {
void addOperationMapping(int operationId, Integer deviceIds) throws OperationManagementDAOException; void addOperationMapping(int operationId, Integer deviceId, boolean isScheduled) throws OperationManagementDAOException;
void removeOperationMapping(int operationId, Integer deviceId) throws OperationManagementDAOException;
void removeOperationMapping(int operationId, Integer deviceIds) throws OperationManagementDAOException; void updateOperationMapping(int operationId, Integer deviceId, Operation.PushStatus pushStatus) throws
OperationManagementDAOException;
void updateOperationMapping(List<OperationMapping> operationMappingList, Operation.PushStatus pushStatus) throws
OperationManagementDAOException;
} }

@ -24,6 +24,7 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity;
import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus; import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationMapping;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationResponse; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationResponse;
import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationDAO; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationDAO;
@ -32,11 +33,20 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOF
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOUtil; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.util.OperationDAOUtil; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.util.OperationDAOUtil;
import java.io.*; import java.io.ByteArrayInputStream;
import java.sql.*; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.LinkedList;
import java.util.List; import java.util.List;
/** /**
@ -420,14 +430,13 @@ public class GenericOperationDAOImpl implements OperationDAO {
"WHERE opm.UPDATED_TIMESTAMP > ? \n" + "WHERE opm.UPDATED_TIMESTAMP > ? \n" +
"AND de.TENANT_ID = ? \n"; "AND de.TENANT_ID = ? \n";
if(timestamp == 0){ if (timestamp == 0) {
sql += "ORDER BY opm.OPERATION_ID LIMIT ? OFFSET ?;"; sql += "ORDER BY opm.OPERATION_ID LIMIT ? OFFSET ?;";
}else{ } else {
sql += "ORDER BY opm.UPDATED_TIMESTAMP asc LIMIT ? OFFSET ?"; sql += "ORDER BY opm.UPDATED_TIMESTAMP asc LIMIT ? OFFSET ?";
} }
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setLong(1, timestamp); stmt.setLong(1, timestamp);
@ -1082,4 +1091,41 @@ public class GenericOperationDAOImpl implements OperationDAO {
} }
return status; return status;
} }
@Override
public List<OperationMapping> getOperationMappingsByStatus(Operation.Status opStatus, Operation.PushStatus pushStatus,
int limit) throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
OperationMapping operationMapping;
List<OperationMapping> operationMappings = new LinkedList<>();
try {
Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "SELECT op_mappings.ENROLMENT_ID, op_mappings.OPERATION_ID, d_type.NAME ,d.TENANT_ID FROM " +
"DM_DEVICE d, DM_ENROLMENT_OP_MAPPING op_mappings, DM_DEVICE_TYPE d_type WHERE op_mappings" +
".STATUS = '?' AND op_mappings.PUSH_NOTIFICATION_STATUS = '?' AND d.DEVICE_TYPE_ID = d_type.ID " +
"AND d.ID=op_mappings.ENROLMENT_ID ORDER BY op_mappings.OPERATION_ID LIMIT ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, opStatus.toString());
stmt.setString(2, pushStatus.toString());
stmt.setInt(3, limit);
rs = stmt.executeQuery();
while (rs.next()) {
operationMapping = new OperationMapping();
operationMapping.setOperationId(rs.getInt("OPERATION_ID"));
operationMapping.setDeviceIdentifier(new DeviceIdentifier(String.valueOf(rs.getInt("ENROLMENT_ID")),
rs.getString("NAME")));
operationMapping.setTenantId(rs.getInt("TENANT_ID"));
operationMappings.add(operationMapping);
}
} catch (SQLException e) {
throw new OperationManagementDAOException("SQL error while getting operation mappings from database. ", e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs);
}
return operationMappings;
}
} }

@ -18,6 +18,7 @@
*/ */
package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl; package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationMapping;
import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory;
@ -27,23 +28,30 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationMappingDAO;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.List;
public class OperationMappingDAOImpl implements OperationMappingDAO { public class OperationMappingDAOImpl implements OperationMappingDAO {
@Override @Override
public void addOperationMapping(int operationId, Integer deviceId) throws OperationManagementDAOException { public void addOperationMapping(int operationId, Integer deviceId, boolean isScheduled) throws
OperationManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
long time = System.currentTimeMillis()/1000; long time = System.currentTimeMillis() / 1000;
Connection conn = OperationManagementDAOFactory.getConnection(); Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "INSERT INTO DM_ENROLMENT_OP_MAPPING(ENROLMENT_ID, OPERATION_ID, STATUS, CREATED_TIMESTAMP, " + String sql = "INSERT INTO DM_ENROLMENT_OP_MAPPING(ENROLMENT_ID, OPERATION_ID, STATUS, " +
"UPDATED_TIMESTAMP) VALUES (?, ?, ?, ?, ?)"; "PUSH_NOTIFICATION_STATUS, CREATED_TIMESTAMP, UPDATED_TIMESTAMP) VALUES (?, ?, ?, ?, ?, ?)";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, deviceId); stmt.setInt(1, deviceId);
stmt.setInt(2, operationId); stmt.setInt(2, operationId);
stmt.setString(3, Operation.Status.PENDING.toString()); stmt.setString(3, Operation.Status.PENDING.toString());
stmt.setLong(4, time); if (isScheduled) {
stmt.setString(4, Operation.PushStatus.SCHEDULED.toString());
} else {
stmt.setString(4, Operation.PushStatus.IN_PROGRESS.toString());
}
stmt.setLong(5, time); stmt.setLong(5, time);
stmt.setLong(6, time);
stmt.executeUpdate(); stmt.executeUpdate();
} catch (SQLException e) { } catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while persisting device operation mappings", e); throw new OperationManagementDAOException("Error occurred while persisting device operation mappings", e);
@ -54,13 +62,13 @@ public class OperationMappingDAOImpl implements OperationMappingDAO {
@Override @Override
public void removeOperationMapping(int operationId, public void removeOperationMapping(int operationId,
Integer deviceIds) throws OperationManagementDAOException { Integer deviceId) throws OperationManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
Connection conn = OperationManagementDAOFactory.getConnection(); Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "DELETE FROM DM_ENROLMENT_OP_MAPPING WHERE ENROLMENT_ID = ? AND OPERATION_ID = ?"; String sql = "DELETE FROM DM_ENROLMENT_OP_MAPPING WHERE ENROLMENT_ID = ? AND OPERATION_ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, 0); stmt.setInt(1, deviceId);
stmt.setInt(2, operationId); stmt.setInt(2, operationId);
stmt.executeUpdate(); stmt.executeUpdate();
} catch (SQLException e) { } catch (SQLException e) {
@ -70,4 +78,55 @@ public class OperationMappingDAOImpl implements OperationMappingDAO {
} }
} }
@Override
public void updateOperationMapping(int operationId, Integer deviceId, Operation.PushStatus pushStatus) throws OperationManagementDAOException {
PreparedStatement stmt = null;
try {
Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "UPDATE DM_ENROLMENT_OP_MAPPING SET PUSH_NOTIFICATION_STATUS = ? WHERE ENROLMENT_ID = ? and " +
"OPERATION_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, pushStatus.toString());
stmt.setInt(2, deviceId);
stmt.setInt(3, operationId);
stmt.executeUpdate();
} catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while updating device operation mappings", e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, null);
}
}
@Override
public void updateOperationMapping(List<OperationMapping> operationMappingList, Operation.PushStatus pushStatus) throws
OperationManagementDAOException {
PreparedStatement stmt = null;
try {
Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "UPDATE DM_ENROLMENT_OP_MAPPING SET PUSH_NOTIFICATION_STATUS = ? WHERE ENROLMENT_ID = ? and " +
"OPERATION_ID = ?";
stmt = conn.prepareStatement(sql);
if (conn.getMetaData().supportsBatchUpdates()) {
for (OperationMapping operationMapping : operationMappingList) {
stmt.setString(1, pushStatus.toString());
stmt.setInt(2, Integer.parseInt(operationMapping.getDeviceIdentifier().getId()));
stmt.setInt(3, operationMapping.getOperationId());
stmt.addBatch();
}
stmt.executeBatch();
} else {
for (OperationMapping operationMapping : operationMappingList) {
stmt.setString(1, pushStatus.toString());
stmt.setInt(2, Integer.parseInt(operationMapping.getDeviceIdentifier().getId()));
stmt.setInt(3, operationMapping.getOperationId());
stmt.executeUpdate();
}
}
} catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while updating device operation mappings as " +
"batch ", e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, null);
}
}
} }

@ -23,6 +23,7 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity;
import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus; import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationMapping;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationResponse; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationResponse;
import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException;
@ -37,6 +38,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List; import java.util.List;
/** /**
@ -361,4 +363,41 @@ public class OracleOperationDAOImpl extends GenericOperationDAOImpl {
} }
return 0; return 0;
} }
@Override
public List<OperationMapping> getOperationMappingsByStatus(Operation.Status opStatus, Operation.PushStatus pushStatus,
int limit) throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
OperationMapping operationMapping;
List<OperationMapping> operationMappings = new LinkedList<>();
try {
Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "SELECT op_mappings.ENROLMENT_ID, op_mappings.OPERATION_ID, d_type.NAME ,d.TENANT_ID FROM " +
"DM_DEVICE d, DM_ENROLMENT_OP_MAPPING op_mappings, DM_DEVICE_TYPE d_type WHERE op_mappings" +
".STATUS = '?' AND op_mappings.PUSH_NOTIFICATION_STATUS = '?' AND d.DEVICE_TYPE_ID = d_type.ID " +
"AND d.ID=op_mappings.ENROLMENT_ID AND ROWNUM <= ? ORDER BY op_mappings.OPERATION_ID";
stmt = conn.prepareStatement(sql);
stmt.setString(1, opStatus.toString());
stmt.setString(2, pushStatus.toString());
stmt.setInt(3, limit);
rs = stmt.executeQuery();
while (rs.next()) {
operationMapping = new OperationMapping();
operationMapping.setOperationId(rs.getInt("OPERATION_ID"));
operationMapping.setDeviceIdentifier(new DeviceIdentifier(String.valueOf(rs.getInt("ENROLMENT_ID")),
rs.getString("NAME")));
operationMapping.setTenantId(rs.getInt("TENANT_ID"));
operationMappings.add(operationMapping);
}
} catch (SQLException e) {
throw new OperationManagementDAOException("SQL error while getting operation mappings from database. ", e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs);
}
return operationMappings;
}
} }

@ -23,6 +23,7 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity;
import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus; import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationMapping;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationResponse; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationResponse;
import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException;
@ -37,6 +38,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List; import java.util.List;
/** /**
@ -264,4 +266,42 @@ public class SQLServerOperationDAOImpl extends GenericOperationDAOImpl {
return activities; return activities;
} }
@Override
public List<OperationMapping> getOperationMappingsByStatus(Operation.Status opStatus, Operation.PushStatus pushStatus,
int limit) throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
OperationMapping operationMapping;
List<OperationMapping> operationMappings = new LinkedList<>();
try {
Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "SELECT op_mappings.ENROLMENT_ID, op_mappings.OPERATION_ID, d_type.NAME ,d.TENANT_ID FROM " +
"DM_DEVICE d, DM_ENROLMENT_OP_MAPPING op_mappings, DM_DEVICE_TYPE d_type WHERE op_mappings" +
".STATUS = '?' AND op_mappings.PUSH_NOTIFICATION_STATUS = '?' AND d.DEVICE_TYPE_ID = d_type.ID " +
"AND d.ID=op_mappings.ENROLMENT_ID ORDER BY op_mappings.OPERATION_ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
stmt = conn.prepareStatement(sql);
stmt.setString(1, opStatus.toString());
stmt.setString(2, pushStatus.toString());
stmt.setInt(3, 0);
stmt.setInt(4, limit);
rs = stmt.executeQuery();
while (rs.next()) {
operationMapping = new OperationMapping();
operationMapping.setOperationId(rs.getInt("OPERATION_ID"));
operationMapping.setDeviceIdentifier(new DeviceIdentifier(String.valueOf(rs.getInt("ENROLMENT_ID")),
rs.getString("NAME")));
operationMapping.setTenantId(rs.getInt("TENANT_ID"));
operationMappings.add(operationMapping);
}
} catch (SQLException e) {
throw new OperationManagementDAOException("SQL error while getting operation mappings from database. ", e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs);
}
return operationMappings;
}
} }

@ -32,6 +32,7 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.Activity;
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
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.policy.mgt.PolicyMonitoringManager; import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyMonitoringManager;
import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
@ -130,6 +131,7 @@ public interface DeviceManagementProviderService {
/** /**
* This method returns the list of device owned by a user of given device type. * This method returns the list of device owned by a user of given device type.
*
* @param userName user name. * @param userName user name.
* @param deviceType device type name * @param deviceType device type name
* @return * @return
@ -211,12 +213,20 @@ public interface DeviceManagementProviderService {
* *
* @param deviceId identifier of the device that needs to be checked against the user. * @param deviceId identifier of the device that needs to be checked against the user.
* @param user username of the device owner. * @param user username of the device owner.
*
* @return true if the user owns the device else will return false. * @return true if the user owns the device else will return false.
* @throws DeviceManagementException If some unusual behaviour is observed while fetching the device. * @throws DeviceManagementException If some unusual behaviour is observed while fetching the device.
*/ */
boolean isEnrolled(DeviceIdentifier deviceId, String user) throws DeviceManagementException; boolean isEnrolled(DeviceIdentifier deviceId, String user) throws DeviceManagementException;
/**
* This method is used to get notification strategy for given device type
*
* @param deviceType Device type
* @return Notification Strategy for device type
* @throws DeviceManagementException
*/
NotificationStrategy getNotificationStrategyByDeviceType(String deviceType) throws DeviceManagementException;
License getLicense(String deviceType, String languageCode) throws DeviceManagementException; License getLicense(String deviceType, String languageCode) throws DeviceManagementException;
void addLicense(String deviceType, License license) throws DeviceManagementException; void addLicense(String deviceType, License license) throws DeviceManagementException;
@ -239,6 +249,7 @@ public interface DeviceManagementProviderService {
/** /**
* Returns the device of specified id. * Returns the device of specified id.
*
* @param deviceId device Id * @param deviceId device Id
* @return Device returns null when device is not avaialble. * @return Device returns null when device is not avaialble.
* @throws DeviceManagementException * @throws DeviceManagementException

@ -23,7 +23,21 @@ import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.CarbonConstants; import org.wso2.carbon.CarbonConstants;
import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.*; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.DeviceManager;
import org.wso2.carbon.device.mgt.common.DeviceNotFoundException;
import org.wso2.carbon.device.mgt.common.DeviceTypeIdentifier;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.FeatureManager;
import org.wso2.carbon.device.mgt.common.InitialOperationConfig;
import org.wso2.carbon.device.mgt.common.InvalidDeviceException;
import org.wso2.carbon.device.mgt.common.MonitoringOperation;
import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.common.TransactionManagementException;
import org.wso2.carbon.device.mgt.common.app.mgt.Application; import org.wso2.carbon.device.mgt.common.app.mgt.Application;
import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration;
import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo; import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo;
@ -37,7 +51,9 @@ import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException;
import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity;
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
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.policy.mgt.PolicyMonitoringManager; import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyMonitoringManager;
import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy;
import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService;
import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants;
import org.wso2.carbon.device.mgt.core.DeviceManagementPluginRepository; import org.wso2.carbon.device.mgt.core.DeviceManagementPluginRepository;
@ -689,6 +705,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} }
return devices; return devices;
} }
@Override @Override
public List<Device> getAllDevices(String deviceType) throws DeviceManagementException { public List<Device> getAllDevices(String deviceType) throws DeviceManagementException {
List<Device> devices = new ArrayList<>(); List<Device> devices = new ArrayList<>();
@ -1958,6 +1975,17 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
return false; return false;
} }
@Override
public NotificationStrategy getNotificationStrategyByDeviceType(String deviceType) throws DeviceManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
OperationManager operationManager = pluginRepository.getOperationManager(deviceType, tenantId);
if (operationManager != null) {
return operationManager.getNotificationStrategy();
} else {
throw new DeviceManagementException("Cannot find operation manager for given device type :" + deviceType);
}
}
/** /**
* Change device status. * Change device status.
* *
@ -2007,7 +2035,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); throw new DeviceManagementException("Error occurred while opening a connection to the data source", e);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
throw new DeviceManagementException("Error occurred while updating the enrollment information device for" + throw new DeviceManagementException("Error occurred while updating the enrollment information device for" +
"id '" + deviceId + "' ." , e); "id '" + deviceId + "' .", e);
} finally { } finally {
try { try {
DeviceManagementDAOFactory.getConnection().setAutoCommit(isAutoCommit); DeviceManagementDAOFactory.getConnection().setAutoCommit(isAutoCommit);

Loading…
Cancel
Save