|
|
@ -38,6 +38,8 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus;
|
|
|
|
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.operation.mgt.OperationManager;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.policy.mgt.Policy;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.policy.mgt.ProfileFeature;
|
|
|
|
import org.wso2.carbon.device.mgt.common.push.notification.NotificationContext;
|
|
|
|
import org.wso2.carbon.device.mgt.common.push.notification.NotificationContext;
|
|
|
|
import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy;
|
|
|
|
import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy;
|
|
|
|
import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig;
|
|
|
|
import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig;
|
|
|
@ -64,13 +66,7 @@ import org.wso2.carbon.device.mgt.core.task.impl.DeviceTaskManagerImpl;
|
|
|
|
import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
|
|
|
|
import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
|
|
|
|
|
|
|
|
|
|
|
|
import java.sql.SQLException;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.Calendar;
|
|
|
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* This class implements all the functionality exposed as part of the OperationManager. Any transaction initiated
|
|
|
|
* This class implements all the functionality exposed as part of the OperationManager. Any transaction initiated
|
|
|
@ -161,8 +157,8 @@ public class OperationManagerImpl implements OperationManager {
|
|
|
|
List<DeviceIdentifier> validDeviceIds = deviceValidationResult.getValidDeviceIDList();
|
|
|
|
List<DeviceIdentifier> validDeviceIds = deviceValidationResult.getValidDeviceIDList();
|
|
|
|
if (validDeviceIds.size() > 0) {
|
|
|
|
if (validDeviceIds.size() > 0) {
|
|
|
|
DeviceIDHolder deviceAuthorizationResult = this.authorizeDevices(operation, validDeviceIds);
|
|
|
|
DeviceIDHolder deviceAuthorizationResult = this.authorizeDevices(operation, validDeviceIds);
|
|
|
|
List<DeviceIdentifier> authorizedDeviceList = deviceAuthorizationResult.getValidDeviceIDList();
|
|
|
|
List<DeviceIdentifier> authorizedDeviceIds = deviceAuthorizationResult.getValidDeviceIDList();
|
|
|
|
if (authorizedDeviceList.size() <= 0) {
|
|
|
|
if (authorizedDeviceIds.size() <= 0) {
|
|
|
|
log.warn("User : " + getUser() + " is not authorized to perform operations on given device-list.");
|
|
|
|
log.warn("User : " + getUser() + " is not authorized to perform operations on given device-list.");
|
|
|
|
Activity activity = new Activity();
|
|
|
|
Activity activity = new Activity();
|
|
|
|
//Send the operation statuses only for admin triggered operations
|
|
|
|
//Send the operation statuses only for admin triggered operations
|
|
|
@ -189,86 +185,71 @@ public class OperationManagerImpl implements OperationManager {
|
|
|
|
OperationManagementDAOFactory.beginTransaction();
|
|
|
|
OperationManagementDAOFactory.beginTransaction();
|
|
|
|
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation operationDto =
|
|
|
|
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation operationDto =
|
|
|
|
OperationDAOUtil.convertOperation(operation);
|
|
|
|
OperationDAOUtil.convertOperation(operation);
|
|
|
|
|
|
|
|
int enrolmentId;
|
|
|
|
|
|
|
|
String operationCode = operationDto.getCode();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<Device> authorizedDevices = new ArrayList<>();
|
|
|
|
|
|
|
|
List<Device> ignoredDevices = new ArrayList<>();
|
|
|
|
|
|
|
|
for (DeviceIdentifier deviceId : authorizedDeviceIds) {
|
|
|
|
|
|
|
|
Device device = getDevice(deviceId);
|
|
|
|
|
|
|
|
authorizedDevices.add(device);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (operationDto.getControl() ==
|
|
|
|
|
|
|
|
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Control.NO_REPEAT) {
|
|
|
|
|
|
|
|
int existingOperationID;
|
|
|
|
|
|
|
|
for (Device device : authorizedDevices) {
|
|
|
|
|
|
|
|
enrolmentId = device.getEnrolmentInfo().getId();
|
|
|
|
|
|
|
|
existingOperationID = operationDAO.getExistingOperationID(enrolmentId, operationCode);
|
|
|
|
|
|
|
|
if (existingOperationID > 0) {
|
|
|
|
|
|
|
|
ignoredDevices.add(device);
|
|
|
|
|
|
|
|
operation.setId(existingOperationID);
|
|
|
|
|
|
|
|
this.sendNotification(operation, device);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (ignoredDevices.size() > 0) {
|
|
|
|
|
|
|
|
if (authorizedDevices.size() == ignoredDevices.size()) {
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
|
|
|
log.debug("All the devices contain a pending operation for the Operation Code: "
|
|
|
|
|
|
|
|
+ operationCode);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Activity activity = new Activity();
|
|
|
|
|
|
|
|
//Send the operation statuses only for admin triggered operations
|
|
|
|
|
|
|
|
String deviceType = validDeviceIds.get(0).getType();
|
|
|
|
|
|
|
|
activity.setActivityStatus(this.getActivityStatus(deviceValidationResult, deviceAuthorizationResult,
|
|
|
|
|
|
|
|
deviceType));
|
|
|
|
|
|
|
|
return activity;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
authorizedDevices.removeAll(ignoredDevices);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int operationId = this.lookupOperationDAO(operation).addOperation(operationDto);
|
|
|
|
int operationId = this.lookupOperationDAO(operation).addOperation(operationDto);
|
|
|
|
boolean isNotRepeated = false;
|
|
|
|
|
|
|
|
boolean isScheduled = false;
|
|
|
|
boolean isScheduled = false;
|
|
|
|
NotificationStrategy notificationStrategy = getNotificationStrategy();
|
|
|
|
NotificationStrategy notificationStrategy = getNotificationStrategy();
|
|
|
|
|
|
|
|
|
|
|
|
// check whether device list is greater than batch size notification strategy has enable to send push
|
|
|
|
// check whether device list is greater than batch size notification strategy has enable to send push
|
|
|
|
// notification using scheduler task
|
|
|
|
// notification using scheduler task
|
|
|
|
if (DeviceConfigurationManager.getInstance().getDeviceManagementConfig().
|
|
|
|
if (DeviceConfigurationManager.getInstance().getDeviceManagementConfig().
|
|
|
|
getPushNotificationConfiguration().getSchedulerBatchSize() <= authorizedDeviceList.size() &&
|
|
|
|
getPushNotificationConfiguration().getSchedulerBatchSize() <= authorizedDeviceIds.size() &&
|
|
|
|
notificationStrategy != null) {
|
|
|
|
notificationStrategy != null) {
|
|
|
|
isScheduled = notificationStrategy.getConfig().isScheduled();
|
|
|
|
isScheduled = notificationStrategy.getConfig().isScheduled();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
boolean hasExistingTaskOperation;
|
|
|
|
|
|
|
|
int enrolmentId;
|
|
|
|
|
|
|
|
List<Device> devices = new ArrayList<>();
|
|
|
|
|
|
|
|
if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Control.NO_REPEAT == operationDto.
|
|
|
|
|
|
|
|
getControl()) {
|
|
|
|
|
|
|
|
isNotRepeated = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//TODO have to create a sql to load device details from deviceDAO using single query.
|
|
|
|
//TODO have to create a sql to load device details from deviceDAO using single query.
|
|
|
|
String operationCode = operationDto.getCode();
|
|
|
|
for (Device device : authorizedDevices) {
|
|
|
|
for (DeviceIdentifier deviceId : authorizedDeviceList) {
|
|
|
|
|
|
|
|
Device device = getDevice(deviceId);
|
|
|
|
|
|
|
|
devices.add(device);
|
|
|
|
|
|
|
|
enrolmentId = device.getEnrolmentInfo().getId();
|
|
|
|
enrolmentId = device.getEnrolmentInfo().getId();
|
|
|
|
//Do not repeat the task operations
|
|
|
|
//Do not repeat the task operations
|
|
|
|
if (isScheduledOperation) {
|
|
|
|
operationMappingDAO.addOperationMapping(operationId, enrolmentId, isScheduled);
|
|
|
|
hasExistingTaskOperation = operationDAO.updateTaskOperation(enrolmentId, operationCode);
|
|
|
|
|
|
|
|
if (!hasExistingTaskOperation) {
|
|
|
|
|
|
|
|
operationMappingDAO.addOperationMapping(operationId, enrolmentId, isScheduled);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (isNotRepeated) {
|
|
|
|
|
|
|
|
operationDAO.updateEnrollmentOperationsStatus(enrolmentId, operationCode,
|
|
|
|
|
|
|
|
org.wso2.carbon.device.mgt.core.dto.operation.mgt.
|
|
|
|
|
|
|
|
Operation.Status.PENDING,
|
|
|
|
|
|
|
|
org.wso2.carbon.device.mgt.core.dto.operation.mgt.
|
|
|
|
|
|
|
|
Operation.Status.REPEATED);
|
|
|
|
|
|
|
|
operationMappingDAO.addOperationMapping(operationId, enrolmentId, isScheduled);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
operationMappingDAO.addOperationMapping(operationId, enrolmentId, isScheduled);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
OperationManagementDAOFactory.commitTransaction();
|
|
|
|
OperationManagementDAOFactory.commitTransaction();
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
if (isScheduled) {
|
|
|
|
If notification strategy has not enable to send push notification using scheduler task we will send
|
|
|
|
for (Device device : authorizedDevices) {
|
|
|
|
notification immediately. This is done in separate loop inorder to prevent overlap with DB insert
|
|
|
|
this.sendNotification(operation, device);
|
|
|
|
operations with the possible db update operations trigger followed by pending operation call.
|
|
|
|
|
|
|
|
Otherwise device may call pending operation while DB is locked for write and deadlock can occur.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (notificationStrategy != null && !isScheduled) {
|
|
|
|
|
|
|
|
for (Device device : devices) {
|
|
|
|
|
|
|
|
DeviceIdentifier deviceId = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType());
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
|
|
|
log.debug("Sending push notification to " + deviceId + " from add operation method.");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
operation.setId(operationId);
|
|
|
|
|
|
|
|
operation.setActivityId(DeviceManagementConstants.OperationAttributes.ACTIVITY + operationId);
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
notificationStrategy.execute(new NotificationContext(deviceId, operation));
|
|
|
|
|
|
|
|
} catch (PushNotificationExecutionFailedException e) {
|
|
|
|
|
|
|
|
log.error("Error occurred while sending push notifications to " + deviceId.getType() +
|
|
|
|
|
|
|
|
" device carrying id '" + deviceId + "'", e);
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
Reschedule if push notification failed. Doing db transactions in atomic way to prevent
|
|
|
|
|
|
|
|
deadlocks.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
enrolmentId = device.getEnrolmentInfo().getId();
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
operationMappingDAO.updateOperationMapping(operationId, enrolmentId, org.wso2.carbon
|
|
|
|
|
|
|
|
.device.mgt.core.dto.operation.mgt.Operation.PushNotificationStatus.SCHEDULED);
|
|
|
|
|
|
|
|
OperationManagementDAOFactory.commitTransaction();
|
|
|
|
|
|
|
|
} catch (OperationManagementDAOException ex) {
|
|
|
|
|
|
|
|
// Not throwing this exception in order to keep sending remaining notifications if any.
|
|
|
|
|
|
|
|
log.error("Error occurred while setting push notification status to SCHEDULED.", ex);
|
|
|
|
|
|
|
|
OperationManagementDAOFactory.rollbackTransaction();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -298,6 +279,195 @@ public class OperationManagerImpl implements OperationManager {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Operation getPolicyRevokeOperation() {
|
|
|
|
|
|
|
|
CommandOperation policyRevokeOperation = new CommandOperation();
|
|
|
|
|
|
|
|
policyRevokeOperation.setEnabled(true);
|
|
|
|
|
|
|
|
policyRevokeOperation.setCode(OperationMgtConstants.OperationCodes.POLICY_REVOKE);
|
|
|
|
|
|
|
|
policyRevokeOperation.setType(Operation.Type.COMMAND);
|
|
|
|
|
|
|
|
return policyRevokeOperation;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private Operation transformPolicy(Policy policy) {
|
|
|
|
|
|
|
|
List<ProfileFeature> effectiveFeatures = policy.getProfile().getProfileFeaturesList();
|
|
|
|
|
|
|
|
List<ProfileOperation> profileOperationList = new ArrayList<ProfileOperation>();
|
|
|
|
|
|
|
|
PolicyOperation policyOperation = new PolicyOperation();
|
|
|
|
|
|
|
|
policyOperation.setEnabled(true);
|
|
|
|
|
|
|
|
policyOperation.setType(org.wso2.carbon.device.mgt.common.operation.mgt.Operation.Type.POLICY);
|
|
|
|
|
|
|
|
policyOperation.setCode(PolicyOperation.POLICY_OPERATION_CODE);
|
|
|
|
|
|
|
|
for (ProfileFeature feature : effectiveFeatures) {
|
|
|
|
|
|
|
|
ProfileOperation profileOperation = new ProfileOperation();
|
|
|
|
|
|
|
|
profileOperation.setCode(feature.getFeatureCode());
|
|
|
|
|
|
|
|
profileOperation.setEnabled(true);
|
|
|
|
|
|
|
|
profileOperation.setStatus(org.wso2.carbon.device.mgt.common.operation.mgt.Operation.Status.PENDING);
|
|
|
|
|
|
|
|
profileOperation.setType(org.wso2.carbon.device.mgt.common.operation.mgt.Operation.Type.PROFILE);
|
|
|
|
|
|
|
|
profileOperation.setPayLoad(feature.getContent());
|
|
|
|
|
|
|
|
profileOperationList.add(profileOperation);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
policyOperation.setProfileOperations(profileOperationList);
|
|
|
|
|
|
|
|
policyOperation.setPayLoad(policyOperation.getProfileOperations());
|
|
|
|
|
|
|
|
return policyOperation;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void addOperationsForPolicyRevoke(Policy policy, List<DeviceIdentifier> deviceIds)
|
|
|
|
|
|
|
|
throws OperationManagementException, InvalidDeviceException {
|
|
|
|
|
|
|
|
Operation revokeOperation = getPolicyRevokeOperation();
|
|
|
|
|
|
|
|
Operation operation = transformPolicy(policy);
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
|
|
|
log.debug("operation:[" + operation.toString() + "]");
|
|
|
|
|
|
|
|
for (DeviceIdentifier deviceIdentifier : deviceIds) {
|
|
|
|
|
|
|
|
log.debug("device identifier id:[" + deviceIdentifier.getId() + "] type:[" +
|
|
|
|
|
|
|
|
deviceIdentifier.getType() + "]");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
DeviceIDHolder deviceValidationResult = DeviceManagerUtil.validateDeviceIdentifiers(deviceIds);
|
|
|
|
|
|
|
|
List<DeviceIdentifier> validDeviceIds = deviceValidationResult.getValidDeviceIDList();
|
|
|
|
|
|
|
|
if (validDeviceIds.size() > 0) {
|
|
|
|
|
|
|
|
DeviceIDHolder deviceAuthorizationResult = this.authorizeDevices(operation, validDeviceIds);
|
|
|
|
|
|
|
|
List<DeviceIdentifier> authorizedDeviceList = deviceAuthorizationResult.getValidDeviceIDList();
|
|
|
|
|
|
|
|
OperationManagementDAOFactory.beginTransaction();
|
|
|
|
|
|
|
|
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation policyOperationDto =
|
|
|
|
|
|
|
|
OperationDAOUtil.convertOperation(operation);
|
|
|
|
|
|
|
|
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation revokeOperationDto =
|
|
|
|
|
|
|
|
OperationDAOUtil.convertOperation(revokeOperation);
|
|
|
|
|
|
|
|
boolean isScheduledOperation = this.isTaskScheduledOperation(operation);
|
|
|
|
|
|
|
|
boolean isNotRepeated = false;
|
|
|
|
|
|
|
|
boolean isScheduled = false;
|
|
|
|
|
|
|
|
NotificationStrategy notificationStrategy = getNotificationStrategy();
|
|
|
|
|
|
|
|
// check whether device list is greater than batch size notification strategy has enable to send push
|
|
|
|
|
|
|
|
// notification using scheduler task
|
|
|
|
|
|
|
|
if (DeviceConfigurationManager.getInstance().getDeviceManagementConfig().
|
|
|
|
|
|
|
|
getPushNotificationConfiguration().getSchedulerBatchSize() <= authorizedDeviceList.size() &&
|
|
|
|
|
|
|
|
notificationStrategy != null) {
|
|
|
|
|
|
|
|
isScheduled = notificationStrategy.getConfig().isScheduled();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
List<org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation> operationList = new LinkedList<org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation>();
|
|
|
|
|
|
|
|
operationList.add(revokeOperationDto);
|
|
|
|
|
|
|
|
operationList.add(policyOperationDto);
|
|
|
|
|
|
|
|
List<Integer> operationIds = this.lookupOperationDAO(operation).addOperations(operationList);
|
|
|
|
|
|
|
|
List<Device> devices = new ArrayList<>();
|
|
|
|
|
|
|
|
if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Control.NO_REPEAT == policyOperationDto.
|
|
|
|
|
|
|
|
getControl()) {
|
|
|
|
|
|
|
|
isNotRepeated = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//Need to happen for both revoke and new policy operation
|
|
|
|
|
|
|
|
addOperationMappings(authorizedDeviceList, revokeOperationDto, operationIds.get(0), isScheduledOperation,
|
|
|
|
|
|
|
|
isNotRepeated, isScheduled, devices);
|
|
|
|
|
|
|
|
sendPushNotifications(revokeOperation, operationIds.get(0), isScheduled, notificationStrategy, devices);
|
|
|
|
|
|
|
|
//Need to happen for both revoke and new policy operation
|
|
|
|
|
|
|
|
addOperationMappings(authorizedDeviceList, policyOperationDto, operationIds.get(1), isScheduledOperation,
|
|
|
|
|
|
|
|
isNotRepeated, isScheduled, devices);
|
|
|
|
|
|
|
|
sendPushNotifications(operation, operationIds.get(1), isScheduled, notificationStrategy, devices);
|
|
|
|
|
|
|
|
OperationManagementDAOFactory.commitTransaction();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
throw new InvalidDeviceException("Invalid device Identifiers found.");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (OperationManagementDAOException e) {
|
|
|
|
|
|
|
|
OperationManagementDAOFactory.rollbackTransaction();
|
|
|
|
|
|
|
|
throw new OperationManagementException("Error occurred while adding operation", e);
|
|
|
|
|
|
|
|
} catch (TransactionManagementException e) {
|
|
|
|
|
|
|
|
throw new OperationManagementException("Error occurred while initiating the transaction", e);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
OperationManagementDAOFactory.closeConnection();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private String addOperationMappings(List<DeviceIdentifier> authorizedDeviceList, org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation operationDto, int operationId, boolean isScheduledOperation, boolean isNotRepeated, boolean isScheduled, List<Device> devices) throws OperationManagementException, OperationManagementDAOException {
|
|
|
|
|
|
|
|
int enrolmentId;
|
|
|
|
|
|
|
|
int existingTaskOperationId;//TODO have to create a sql to load device details from deviceDAO using single query.
|
|
|
|
|
|
|
|
String operationCode = operationDto.getCode();
|
|
|
|
|
|
|
|
for (DeviceIdentifier deviceId : authorizedDeviceList) {
|
|
|
|
|
|
|
|
Device device = getDevice(deviceId);
|
|
|
|
|
|
|
|
devices.add(device);
|
|
|
|
|
|
|
|
enrolmentId = device.getEnrolmentInfo().getId();
|
|
|
|
|
|
|
|
//Do not repeat the task operations
|
|
|
|
|
|
|
|
if (isScheduledOperation) {
|
|
|
|
|
|
|
|
existingTaskOperationId = operationDAO.getExistingOperationID(enrolmentId, operationCode);
|
|
|
|
|
|
|
|
if (existingTaskOperationId != -1) {
|
|
|
|
|
|
|
|
operationMappingDAO.addOperationMapping(operationId, enrolmentId, isScheduled);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (isNotRepeated) {
|
|
|
|
|
|
|
|
operationDAO.updateEnrollmentOperationsStatus(enrolmentId, operationCode,
|
|
|
|
|
|
|
|
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING,
|
|
|
|
|
|
|
|
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.REPEATED);
|
|
|
|
|
|
|
|
operationMappingDAO.addOperationMapping(operationId, enrolmentId, isScheduled);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
operationMappingDAO.addOperationMapping(operationId, enrolmentId, isScheduled);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return operationCode;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
* If notification strategy has not enable to send push notification using scheduler task we will send
|
|
|
|
|
|
|
|
* notification immediately. This is done in separate loop inorder to prevent overlap with DB insert
|
|
|
|
|
|
|
|
* operations with the possible db update operations trigger followed by pending operation call.
|
|
|
|
|
|
|
|
* Otherwise device may call pending operation while DB is locked for write and deadlock can occur.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private void sendPushNotifications(Operation operation, int operationId, boolean isScheduled, NotificationStrategy notificationStrategy, List<Device> devices) {
|
|
|
|
|
|
|
|
int enrolmentId;
|
|
|
|
|
|
|
|
if (notificationStrategy != null && !isScheduled) {
|
|
|
|
|
|
|
|
for (Device device : devices) {
|
|
|
|
|
|
|
|
DeviceIdentifier deviceId = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType());
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
|
|
|
log.debug("Sending push notification to " + deviceId + " from add operation method.");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
operation.setId(operationId);
|
|
|
|
|
|
|
|
operation.setActivityId(DeviceManagementConstants.OperationAttributes.ACTIVITY + operationId);
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
notificationStrategy.execute(new NotificationContext(deviceId, operation));
|
|
|
|
|
|
|
|
} catch (PushNotificationExecutionFailedException e) {
|
|
|
|
|
|
|
|
log.error("Error occurred while sending push notifications to " + deviceId.getType() +
|
|
|
|
|
|
|
|
" device carrying id '" + deviceId + "'", e);
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
Reschedule if push notification failed. Doing db transactions in atomic way to prevent
|
|
|
|
|
|
|
|
deadlocks.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
enrolmentId = device.getEnrolmentInfo().getId();
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
operationMappingDAO.updateOperationMapping(operationId, enrolmentId, org.wso2.carbon
|
|
|
|
|
|
|
|
.device.mgt.core.dto.operation.mgt.Operation.PushNotificationStatus.SCHEDULED);
|
|
|
|
|
|
|
|
} catch (OperationManagementDAOException ex) {
|
|
|
|
|
|
|
|
// Not throwing this exception in order to keep sending remaining notifications if any.
|
|
|
|
|
|
|
|
log.error("Error occurred while setting push notification status to SCHEDULED.", ex);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void sendNotification(Operation operation, Device device) {
|
|
|
|
|
|
|
|
NotificationStrategy notificationStrategy = getNotificationStrategy();
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
* If notification strategy has not enable to send push notification using scheduler task we will send
|
|
|
|
|
|
|
|
* notification immediately. This is done in separate loop inorder to prevent overlap with DB insert
|
|
|
|
|
|
|
|
* operations with the possible db update operations trigger followed by pending operation call.
|
|
|
|
|
|
|
|
* Otherwise device may call pending operation while DB is locked for write and deadlock can occur.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (notificationStrategy != null) {
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
|
|
|
log.debug("Sending push notification to " + device.getDeviceIdentifier() + " from add operation method.");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType());
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
notificationStrategy.execute(new NotificationContext(deviceIdentifier, operation));
|
|
|
|
|
|
|
|
} catch (PushNotificationExecutionFailedException e) {
|
|
|
|
|
|
|
|
log.error("Error occurred while sending push notifications to " + device.getType() +
|
|
|
|
|
|
|
|
" device carrying id '" + device.getDeviceIdentifier() + "'", e);
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
* Reschedule if push notification failed. Doing db transactions in atomic way to prevent
|
|
|
|
|
|
|
|
* deadlocks.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
operationMappingDAO.updateOperationMapping(operation.getId(), device.getEnrolmentInfo().getId(), org.wso2.carbon
|
|
|
|
|
|
|
|
.device.mgt.core.dto.operation.mgt.Operation.PushNotificationStatus.SCHEDULED);
|
|
|
|
|
|
|
|
OperationManagementDAOFactory.commitTransaction();
|
|
|
|
|
|
|
|
} catch (OperationManagementDAOException ex) {
|
|
|
|
|
|
|
|
// Not throwing this exception in order to keep sending remaining notifications if any.
|
|
|
|
|
|
|
|
log.error("Error occurred while setting push notification status to SCHEDULED.", ex);
|
|
|
|
|
|
|
|
OperationManagementDAOFactory.rollbackTransaction();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private List<ActivityStatus> getActivityStatus(DeviceIDHolder deviceIdValidationResult, DeviceIDHolder deviceAuthResult,
|
|
|
|
private List<ActivityStatus> getActivityStatus(DeviceIDHolder deviceIdValidationResult, DeviceIDHolder deviceAuthResult,
|
|
|
|
String deviceType) {
|
|
|
|
String deviceType) {
|
|
|
|
List<ActivityStatus> activityStatuses = new ArrayList<>();
|
|
|
|
List<ActivityStatus> activityStatuses = new ArrayList<>();
|
|
|
@ -906,6 +1076,22 @@ public class OperationManagerImpl implements OperationManager {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public List<Activity> getActivitiesUpdatedAfterByUser(long timestamp, String user, int limit, int offset)
|
|
|
|
|
|
|
|
throws OperationManagementException {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
OperationManagementDAOFactory.openConnection();
|
|
|
|
|
|
|
|
return operationDAO.getActivitiesUpdatedAfterByUser(timestamp, user, limit, offset);
|
|
|
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
|
|
|
throw new OperationManagementException("Error occurred while opening a connection to the data source.", e);
|
|
|
|
|
|
|
|
} catch (OperationManagementDAOException e) {
|
|
|
|
|
|
|
|
throw new OperationManagementException("Error occurred while getting the activity list changed after a " +
|
|
|
|
|
|
|
|
"given time which are added by user : " + user, e);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
OperationManagementDAOFactory.closeConnection();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public int getActivityCountUpdatedAfter(long timestamp) throws OperationManagementException {
|
|
|
|
public int getActivityCountUpdatedAfter(long timestamp) throws OperationManagementException {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
@ -921,6 +1107,21 @@ public class OperationManagerImpl implements OperationManager {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public int getActivityCountUpdatedAfterByUser(long timestamp, String user) throws OperationManagementException {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
OperationManagementDAOFactory.openConnection();
|
|
|
|
|
|
|
|
return operationDAO.getActivityCountUpdatedAfterByUser(timestamp, user);
|
|
|
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
|
|
|
throw new OperationManagementException("Error occurred while opening a connection to the data source.", e);
|
|
|
|
|
|
|
|
} catch (OperationManagementDAOException e) {
|
|
|
|
|
|
|
|
throw new OperationManagementException("Error occurred while getting the activity count changed after a " +
|
|
|
|
|
|
|
|
"given time which are added by user :" + user, e);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
OperationManagementDAOFactory.closeConnection();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private OperationDAO lookupOperationDAO(Operation operation) {
|
|
|
|
private OperationDAO lookupOperationDAO(Operation operation) {
|
|
|
|
|
|
|
|
|
|
|
|
if (operation instanceof CommandOperation) {
|
|
|
|
if (operation instanceof CommandOperation) {
|
|
|
@ -1065,4 +1266,4 @@ public class OperationManagerImpl implements OperationManager {
|
|
|
|
private boolean isSameUser(String user, String owner) {
|
|
|
|
private boolean isSameUser(String user, String owner) {
|
|
|
|
return user.equalsIgnoreCase(owner);
|
|
|
|
return user.equalsIgnoreCase(owner);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|