diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java index 9960ea23b6c..f88c19a2539 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java @@ -113,12 +113,6 @@ public interface DeviceManagementService { required = false) @QueryParam("user") String user, - @ApiParam( - name = "roleName", - value = "Role name of the devices to be fetched.", - required = false) - @QueryParam("roleName") - String roleName, @ApiParam( name = "ownership", allowableValues = "BYOD, COPE", diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index 2fafc9b1290..bf93cdd9e90 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -65,7 +65,6 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { @QueryParam("name") String name, @QueryParam("type") String type, @QueryParam("user") String user, - @QueryParam("roleName") String roleName, @QueryParam("ownership") String ownership, @QueryParam("status") String status, @QueryParam("since") String since, diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EnrolmentInfo.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EnrolmentInfo.java index 2ba839243c1..70e666431c0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EnrolmentInfo.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EnrolmentInfo.java @@ -40,11 +40,9 @@ public class EnrolmentInfo implements Serializable { @ApiModelProperty(name = "id", value = "ID of the device in the WSO2 EMM device information database.", required = true) private int id; - @ApiModelProperty(name = "device", value = "Enrolled device.", required = true) - private Device device; - @ApiModelProperty(name = "dateOfEnrolment", value = "Date of the device enrollment.", required = true ) + @ApiModelProperty(name = "dateOfEnrolment", value = "Date of the device enrollment. This value is not necessary.", required = false ) private Long dateOfEnrolment; - @ApiModelProperty(name = "dateOfLastUpdate", value = "Date of the device's last update.", required = true ) + @ApiModelProperty(name = "dateOfLastUpdate", value = "Date of the device's last update. This value is not necessary.", required = false ) private Long dateOfLastUpdate; @ApiModelProperty(name = "ownership", value = "Defines the ownership details. The ownership type can be any of the" + " following values.\n" + @@ -60,8 +58,7 @@ public class EnrolmentInfo implements Serializable { public EnrolmentInfo() { } - public EnrolmentInfo(Device device, String owner, OwnerShip ownership, Status status) { - this.device = device; + public EnrolmentInfo(String owner, OwnerShip ownership, Status status) { this.owner = owner; this.ownership = ownership; this.status = status; @@ -115,14 +112,6 @@ public class EnrolmentInfo implements Serializable { this.owner = owner; } - public Device getDevice() { - return device; - } - - public void setDevice(Device device) { - this.device = device; - } - @Override public boolean equals(Object obj) { if (obj instanceof EnrolmentInfo) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/EnrollmentDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/EnrollmentDAOImpl.java index abb9b1f87c5..2972697b953 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/EnrollmentDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/EnrollmentDAOImpl.java @@ -73,18 +73,16 @@ public class EnrollmentDAOImpl implements EnrollmentDAO { int status = -1; try { conn = this.getConnection(); - String sql = "UPDATE DM_ENROLMENT SET OWNERSHIP = ?, STATUS = ?, " + - "DATE_OF_ENROLMENT = ?, DATE_OF_LAST_UPDATE = ? WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?" + - " AND ID = ?"; + String sql = "UPDATE DM_ENROLMENT SET OWNERSHIP = ?, STATUS = ?, DATE_OF_LAST_UPDATE = ? WHERE DEVICE_ID = ?" + + " AND OWNER = ? AND TENANT_ID = ? AND ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, enrolmentInfo.getOwnership().toString()); stmt.setString(2, enrolmentInfo.getStatus().toString()); - stmt.setTimestamp(3, new Timestamp(enrolmentInfo.getDateOfEnrolment())); - stmt.setTimestamp(4, new Timestamp(new Date().getTime())); - stmt.setInt(5, deviceId); - stmt.setString(6, enrolmentInfo.getOwner()); - stmt.setInt(7, tenantId); - stmt.setInt(8, enrolmentInfo.getId()); + stmt.setTimestamp(3, new Timestamp(new Date().getTime())); + stmt.setInt(4, deviceId); + stmt.setString(5, enrolmentInfo.getOwner()); + stmt.setInt(6, tenantId); + stmt.setInt(7, enrolmentInfo.getId()); stmt.executeUpdate(); return status; } catch (SQLException e) { @@ -102,14 +100,12 @@ public class EnrollmentDAOImpl implements EnrollmentDAO { int status = -1; try { conn = this.getConnection(); - String sql = "UPDATE DM_ENROLMENT SET OWNERSHIP = ?, STATUS = ?, " + - "DATE_OF_ENROLMENT = ?, DATE_OF_LAST_UPDATE = ? WHERE ID = ?"; + String sql = "UPDATE DM_ENROLMENT SET OWNERSHIP = ?, STATUS = ?, DATE_OF_LAST_UPDATE = ? WHERE ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, enrolmentInfo.getOwnership().toString()); stmt.setString(2, enrolmentInfo.getStatus().toString()); - stmt.setTimestamp(3, new Timestamp(enrolmentInfo.getDateOfEnrolment())); - stmt.setTimestamp(4, new Timestamp(new Date().getTime())); - stmt.setInt(5, enrolmentInfo.getId()); + stmt.setTimestamp(3, new Timestamp(new Date().getTime())); + stmt.setInt(4, enrolmentInfo.getId()); stmt.executeUpdate(); return status; } catch (SQLException e) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java index 5dbe2870b20..41949eeb188 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -32,6 +32,8 @@ 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.PushNotificationExecutionFailedException; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; +import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; +import org.wso2.carbon.device.mgt.core.config.task.TaskConfiguration; 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; @@ -106,16 +108,34 @@ public class OperationManagerImpl implements OperationManager { org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation operationDto = OperationDAOUtil.convertOperation(operation); int operationId = this.lookupOperationDAO(operation).addOperation(operationDto); + boolean isScheduledOperation = this.isTaskScheduledOperation(operation); + boolean isNotRepeated = false; + boolean hasExistingTaskOperation; + int enrolmentId; + if (operationDto.getControl() == + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Control.NO_REPEAT) { + isNotRepeated = true; + } + //TODO have to create a sql to load device details from deviceDAO using single query. + String operationCode = operationDto.getCode(); for (DeviceIdentifier deviceId : deviceIds) { Device device = getDevice(deviceId); - if (operationDto.getControl() == - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Control.NO_REPEAT) { - operationDAO.updateEnrollmentOperationsStatus(device.getEnrolmentInfo().getId(), operationDto.getCode(), - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING, - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.REPEATED); + enrolmentId = device.getEnrolmentInfo().getId(); + //Do not repeat the task operations + if (isScheduledOperation) { + hasExistingTaskOperation = operationDAO.updateTaskOperation(enrolmentId, operationCode); + if (!hasExistingTaskOperation) { + operationMappingDAO.addOperationMapping(operationId, enrolmentId); + } + } 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); + } else { + operationMappingDAO.addOperationMapping(operationId, enrolmentId); } - operationMappingDAO.addOperationMapping(operationId, device.getEnrolmentInfo().getId()); if (notificationStrategy != null) { try { notificationStrategy.execute(new NotificationContext(deviceId, operation)); @@ -129,7 +149,7 @@ public class OperationManagerImpl implements OperationManager { OperationManagementDAOFactory.commitTransaction(); Activity activity = new Activity(); activity.setActivityId(DeviceManagementConstants.OperationAttributes.ACTIVITY + operationId); - activity.setCode(operationDto.getCode()); + activity.setCode(operationCode); activity.setCreatedTimeStamp(new Date().toString()); activity.setType(Activity.Type.valueOf(operationDto.getType().toString())); return activity; @@ -788,4 +808,15 @@ public class OperationManagerImpl implements OperationManager { return enrolmentId; } + private boolean isTaskScheduledOperation(Operation operation) { + TaskConfiguration taskConfiguration = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(). + getTaskConfiguration(); + for (TaskConfiguration.Operation op:taskConfiguration.getOperations()) { + if (operation.getCode().equals(op.getOperationName())) { + return true; + } + } + return false; + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java index 51a9ba4df74..e07d251319d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java @@ -60,6 +60,8 @@ public interface OperationDAO { void updateEnrollmentOperationsStatus(int enrolmentId, String operationCode, Operation.Status existingStatus, Operation.Status newStatus) throws OperationManagementDAOException; + boolean updateTaskOperation(int enrolmentId, String operationCode) throws OperationManagementDAOException; + void addOperationResponse(int enrolmentId, int operationId, Object operationResponse) throws OperationManagementDAOException; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java index ce14718b263..e710763e73f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java @@ -154,6 +154,44 @@ public class GenericOperationDAOImpl implements OperationDAO { } } + @Override + public boolean updateTaskOperation(int enrolmentId, String operationCode) + throws OperationManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs = null; + boolean result = false; + try { + Connection connection = OperationManagementDAOFactory.getConnection(); + String query = "SELECT EOM.ID FROM DM_ENROLMENT_OP_MAPPING AS EOM INNER JOIN DM_OPERATION DM " + + "ON DM.ID = EOM.OPERATION_ID WHERE EOM.ENROLMENT_ID = ? AND DM.OPERATION_CODE = ? AND " + + "EOM.STATUS = ?;"; + stmt = connection.prepareStatement(query); + stmt.setInt(1, enrolmentId); + stmt.setString(2, operationCode); + stmt.setString(3, Operation.Status.PENDING.toString()); + // This will return only one result always. + rs = stmt.executeQuery(); + int id = 0; + if (rs.next()) { + id = rs.getInt("ID"); + } + if (id != 0) { + stmt = connection.prepareStatement("UPDATE DM_ENROLMENT_OP_MAPPING SET UPDATED_TIMESTAMP = ? " + + "WHERE ID = ?"); + stmt.setLong(1, System.currentTimeMillis() / 1000); + stmt.setInt(2, id); + stmt.executeUpdate(); + result = true; + } + } catch (SQLException e) { + throw new OperationManagementDAOException("Error occurred while update device mapping operation status " + + "metadata", e); + } finally { + OperationManagementDAOUtil.cleanupResources(stmt); + } + return result; + } + @Override public void addOperationResponse(int enrolmentId, int operationId, Object operationResponse) throws OperationManagementDAOException { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/EnrolmentPersistenceTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/EnrolmentPersistenceTests.java index 6d7e4b4bb6a..7ba74caa10d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/EnrolmentPersistenceTests.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/EnrolmentPersistenceTests.java @@ -41,7 +41,7 @@ public class EnrolmentPersistenceTests extends BaseDeviceManagementTest { /* Initializing source enrolment configuration bean to be tested */ EnrolmentInfo source = - new EnrolmentInfo(null, owner, EnrolmentInfo.OwnerShip.BYOD, + new EnrolmentInfo(owner, EnrolmentInfo.OwnerShip.BYOD, EnrolmentInfo.Status.CREATED); /* Adding dummy enrolment configuration to the device management metadata store */