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 b3a594a95c..2c599474fc 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 @@ -92,4 +92,23 @@ public class EnrolmentInfo { this.device = device; } + @Override + public boolean equals(Object obj) { + if (obj instanceof EnrolmentInfo) { + EnrolmentInfo tempInfo = (EnrolmentInfo) obj; + if (owner != null && ownership != null + && tempInfo.getOwner() != null && tempInfo.getOwnership() != null) { + + if (owner.equals(tempInfo.getOwner()) && ownership.equals(tempInfo.getOwnership())) { + return true; + } else { + return false; + } + } else { + return false; + } + } else { + return false; + } + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java index 75cddd8141..5b5d0c66aa 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java @@ -62,5 +62,8 @@ public interface DeviceDAO { List getDevicesByStatus(EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException; + + int getEnrolmentByStatus(DeviceIdentifier deviceId, Status status, + int tenantId) throws DeviceManagementDAOException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java index bd0cbe6038..f82038ca3e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java @@ -435,6 +435,37 @@ public class DeviceDAOImpl implements DeviceDAO { } } + public int getEnrolmentByStatus(DeviceIdentifier deviceId, Status status, + int tenantId) throws DeviceManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + conn = this.getConnection(); + String sql = + "SELECT ID AS ENROLMENT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = (SELECT d.ID FROM DM_DEVICE d, " + + "DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.DEVICE_IDENTIFICATION = ? AND t.NAME = ? " + + "AND d.TENANT_ID = ?) AND STATUS = ? AND TENANT_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setString(1, deviceId.getId()); + stmt.setString(2, deviceId.getType()); + stmt.setInt(3, tenantId); + stmt.setString(4, status.toString()); + stmt.setInt(5, tenantId); + rs = stmt.executeQuery(); + if (rs.next()) { + return rs.getInt("ENROLMENT_ID"); + } else { + return -1; // if no results found + } + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the enrolment " + + "id of device '" + deviceId + "'", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + } + private Device loadDevice(ResultSet rs) throws SQLException { Device device = new Device(); device.setId(rs.getInt("DEVICE_ID")); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/EnrolmentDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/EnrolmentDAOImpl.java index f6b22e07d6..b616202f82 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/EnrolmentDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/EnrolmentDAOImpl.java @@ -77,7 +77,7 @@ public class EnrolmentDAOImpl implements EnrolmentDAO { stmt.setString(1, enrolmentInfo.getOwnership().toString()); stmt.setString(2, enrolmentInfo.getStatus().toString()); stmt.setTimestamp(3, new Timestamp(enrolmentInfo.getDateOfEnrolment())); - stmt.setTimestamp(4, new Timestamp(enrolmentInfo.getDateOfLastUpdate())); + stmt.setTimestamp(4, new Timestamp(new Date().getTime())); stmt.setInt(5, deviceId); stmt.setString(6, enrolmentInfo.getOwner()); stmt.setInt(7, tenantId); @@ -156,9 +156,9 @@ public class EnrolmentDAOImpl implements EnrolmentDAO { conn = this.getConnection(); String sql = "SELECT STATUS FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); - stmt.setInt(2, deviceId); - stmt.setString(3, currentOwner); - stmt.setInt(4, tenantId); + stmt.setInt(1, deviceId); + stmt.setString(2, currentOwner); + stmt.setInt(3, tenantId); rs = stmt.executeQuery(); if (rs.next()) { status = EnrolmentInfo.Status.valueOf(rs.getString("STATUS")); 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 dcc5da3ad1..419fe5e9ba 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 @@ -21,7 +21,6 @@ package org.wso2.carbon.device.mgt.core.operation.mgt; 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.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; @@ -32,7 +31,6 @@ 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.dao.DeviceTypeDAO; -import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationDAO; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; @@ -81,7 +79,7 @@ public class OperationManagerImpl implements OperationManager { log.debug("operation:[" + operation.toString() + "]"); for (DeviceIdentifier deviceIdentifier : deviceIds) { log.debug("device identifier id:[" + deviceIdentifier.getId() + "] type:[" + deviceIdentifier.getType() - + "]"); + + "]"); } } try { @@ -91,17 +89,17 @@ public class OperationManagerImpl implements OperationManager { int operationId = this.lookupOperationDAO(operation).addOperation(operationDto); - Device device; + int enrolmentId; int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); for (DeviceIdentifier deviceId : deviceIds) { - device = deviceDAO.getDevice(deviceId, tenantId); - if (device == null) { + enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); + if (enrolmentId < 0) { String errorMsg = "The operation not added for device.The device not found for " + - "device Identifier type -'" + deviceId.getType() + "' and device Id '" + - deviceId.getId(); - log.info(errorMsg); + "device Identifier type -'" + deviceId.getType() + "' and device Id '" + + deviceId.getId(); + log.error(errorMsg); } else { - operationMappingDAO.addOperationMapping(operationId, device.getId()); + operationMappingDAO.addOperationMapping(operationId, enrolmentId); } } OperationManagementDAOFactory.commitTransaction(); @@ -126,21 +124,25 @@ public class OperationManagerImpl implements OperationManager { @Override public List getOperations(DeviceIdentifier deviceId) throws OperationManagementException { + + List operations = new ArrayList(); + int enrolmentId = -1; + try { OperationManagementDAOFactory.getConnection(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - Device device = deviceDAO.getDevice(deviceId, tenantId); + enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); - if (device == null) { + if (enrolmentId < 0) { throw new OperationManagementException("Device not found for given device " + - "Identifier:" + deviceId.getId() + " and given type" + deviceId.getType()); + "Identifier:" + deviceId.getId() + " and given type" + deviceId.getType()); } - List operationList = - operationDAO.getOperationsForDevice(device.getId()); + List operationList = operationDAO + .getOperationsForDevice(enrolmentId); + Operation operation; - List operations = new ArrayList(); for (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation : operationList) { operation = OperationDAOUtil.convertOperation(dtoOperation); operations.add(operation); @@ -148,11 +150,11 @@ public class OperationManagerImpl implements OperationManager { return operations; } catch (OperationManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving the list of " + - "operations assigned for '" + deviceId.getType() + "' device '" + deviceId.getId() - + "'", e); + "operations assigned for '" + deviceId.getType() + "' device '" + deviceId.getId() + + "'", e); } catch (DeviceManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving metadata of '" + - deviceId.getType() + "' device carrying the identifier '" + deviceId.getId() + "'"); + deviceId.getType() + "' device carrying the identifier '" + deviceId.getId() + "'"); } finally { try { OperationManagementDAOFactory.closeConnection(); @@ -160,6 +162,7 @@ public class OperationManagerImpl implements OperationManager { log.warn("Error occurred while closing data source connection", e); } } + } @Override @@ -168,10 +171,10 @@ public class OperationManagerImpl implements OperationManager { if (log.isDebugEnabled()) { log.debug("Device identifier id:[" + deviceId.getId() + "] type:[" + deviceId.getType() - + "]"); + + "]"); } - Device device; + int enrolmentId = -1; List operations = new ArrayList(); List dtoOperationList = @@ -181,30 +184,24 @@ public class OperationManagerImpl implements OperationManager { OperationManagementDAOFactory.getConnection(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - device = deviceDAO.getDevice(deviceId, tenantId); + enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); - if (device == null) { + if (enrolmentId < 0) { throw new OperationManagementException("Device not found for given device " + - "Identifier:" + deviceId.getId() + " and given type:" + deviceId.getType()); - } - - if (device.getEnrolmentInfo().getStatus() != null && !device.getEnrolmentInfo().getStatus().equals( - EnrolmentInfo.Status.ACTIVE)) { - DeviceManagementDataHolder.getInstance().getDeviceManagementProvider() - .updateDeviceEnrolmentInfo(device, EnrolmentInfo.Status.ACTIVE); + "Identifier:" + deviceId.getId() + " and given type:" + deviceId.getType()); } - dtoOperationList.addAll(commandOperationDAO.getOperationsByDeviceAndStatus(device.getId(), - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); + dtoOperationList.addAll(commandOperationDAO.getOperationsByDeviceAndStatus( + enrolmentId, org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); - dtoOperationList.addAll(configOperationDAO.getOperationsByDeviceAndStatus(device.getId(), - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); + dtoOperationList.addAll(configOperationDAO.getOperationsByDeviceAndStatus( + enrolmentId, org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); - dtoOperationList.addAll(profileOperationDAO.getOperationsByDeviceAndStatus(device.getId(), - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); + dtoOperationList.addAll(profileOperationDAO.getOperationsByDeviceAndStatus( + enrolmentId, org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); - dtoOperationList.addAll(policyOperationDAO.getOperationsByDeviceAndStatus(device.getId(), - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); + dtoOperationList.addAll(policyOperationDAO.getOperationsByDeviceAndStatus( + enrolmentId, org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); Operation operation; for (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation : dtoOperationList) { @@ -215,17 +212,14 @@ public class OperationManagerImpl implements OperationManager { return operations; } catch (OperationManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving the list of " + - "pending operations assigned for '" + deviceId.getType() + "' device '" + - deviceId.getId() + "'", e); + "pending operations assigned for '" + deviceId.getType() + "' device '" + + deviceId.getId() + "'", e); } catch (DeviceManagementDAOException e) { String errorMsg = "Error occurred while retrieving the device " + - "for device Identifier type -'" + deviceId.getType() + "' and device Id '" - + deviceId.getId(); + "for device Identifier type -'" + deviceId.getType() + "' and device Id '" + + deviceId.getId(); log.error(errorMsg, e); throw new OperationManagementException(errorMsg, e); - } catch (DeviceManagementException e) { - throw new OperationManagementException("Error occurred while update enrol status: " + - deviceId.toString(), e); } finally { try { OperationManagementDAOFactory.closeConnection(); @@ -239,34 +233,22 @@ public class OperationManagerImpl implements OperationManager { public Operation getNextPendingOperation(DeviceIdentifier deviceId) throws OperationManagementException { if (log.isDebugEnabled()) { log.debug("device identifier id:[" + deviceId.getId() + "] type:[" + deviceId.getType() - + "]"); + + "]"); } Operation operation = null; - Device device; + int enrolmentId = -1; try { OperationManagementDAOFactory.getConnection(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - device = deviceDAO.getDevice(deviceId, tenantId); + enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); - if (device == null) { + if (enrolmentId < 0) { throw new OperationManagementException("Device not found for given device " + - "Identifier:" + deviceId.getId() + " and given type" + deviceId.getType()); + "Identifier:" + deviceId.getId() + " and given type" + deviceId.getType()); } org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO - .getNextOperation(device.getId()); - - if (device.getEnrolmentInfo().getStatus() != null && !device.getEnrolmentInfo().getStatus().equals( - EnrolmentInfo.Status.ACTIVE)) { - try { - DeviceManagementDataHolder.getInstance().getDeviceManagementProvider() - .updateDeviceEnrolmentInfo(device, - EnrolmentInfo.Status.ACTIVE); - } catch (DeviceManagementException e) { - throw new OperationManagementException("Error occurred while update enrol status: '" + - deviceId.toString() + "'", e); - } - } + .getNextOperation(enrolmentId); if (dtoOperation != null) { if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND @@ -293,7 +275,7 @@ public class OperationManagerImpl implements OperationManager { throw new OperationManagementException("Error occurred while retrieving next pending operation", e); } catch (DeviceManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving the device " + - "for device Identifier type -'" + deviceId.getType() + "' and device Id '" + deviceId.getId(), e); + "for device Identifier type -'" + deviceId.getType() + "' and device Id '" + deviceId.getId(), e); } finally { try { OperationManagementDAOFactory.closeConnection(); @@ -315,19 +297,21 @@ public class OperationManagerImpl implements OperationManager { OperationManagementDAOFactory.beginTransaction(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - Device device = deviceDAO.getDevice(deviceId, tenantId); + int enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); if (operation.getStatus() != null) { OperationManagementDAOFactory.beginTransaction(); - operationDAO.updateOperationStatus(device.getId(), operationId, - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status - .valueOf(operation.getStatus().toString())); + operationDAO.updateOperationStatus(enrolmentId, operationId, + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status + .valueOf(operation.getStatus().toString())); } + if (operation.getOperationResponse() != null) { - operationDAO.addOperationResponse(device.getId(), operationId, operation.getOperationResponse()); + OperationManagementDAOFactory.beginTransaction(); + operationDAO.addOperationResponse(enrolmentId, operationId, operation.getOperationResponse()); + OperationManagementDAOFactory.commitTransaction(); } - OperationManagementDAOFactory.commitTransaction(); } catch (OperationManagementDAOException e) { try { OperationManagementDAOFactory.rollbackTransaction(); @@ -335,7 +319,7 @@ public class OperationManagerImpl implements OperationManager { log.warn("Error occurred while roll-backing the update operation transaction", e1); } throw new OperationManagementException("Error occurred while updating the operation: " + operationId + - " status:" + operation.getStatus(), e); + " status:" + operation.getStatus(), e); } catch (DeviceManagementDAOException e) { try { OperationManagementDAOFactory.rollbackTransaction(); @@ -343,7 +327,7 @@ public class OperationManagerImpl implements OperationManager { log.warn("Error occurred while roll-backing the update operation transaction", e1); } throw new OperationManagementException("Error occurred while fetching the device for device identifier: " + - deviceId.getId() + "type:" + deviceId.getType(), e); + deviceId.getId() + "type:" + deviceId.getType(), e); } finally { try { OperationManagementDAOFactory.closeConnection(); @@ -381,25 +365,25 @@ public class OperationManagerImpl implements OperationManager { @Override public Operation getOperationByDeviceAndOperationId(DeviceIdentifier deviceId, int operationId) throws OperationManagementException { - Device device; + int enrolmentId = -1; Operation operation; if (log.isDebugEnabled()) { log.debug("Operation Id:" + operationId + " Device Type:" + deviceId.getType() + " Device Identifier:" + - deviceId.getId()); + deviceId.getId()); } try { OperationManagementDAOFactory.getConnection(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - device = deviceDAO.getDevice(deviceId, tenantId); - if (device == null) { + enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); + if (enrolmentId < 0) { throw new OperationManagementException("Device not found for given device identifier:" + - deviceId.getId() + " type:" + deviceId.getType()); + deviceId.getId() + " type:" + deviceId.getType()); } org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO - .getOperationByDeviceAndId(device.getId(), operationId); + .getOperationByDeviceAndId(enrolmentId, operationId); if (dtoOperation.getType() .equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND)) { @@ -412,27 +396,27 @@ public class OperationManagerImpl implements OperationManager { .equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG)) { dtoOperation = configOperationDAO.getOperation(dtoOperation.getId()); } else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type - .PROFILE)) { + .PROFILE)) { dtoOperation = profileOperationDAO.getOperation(dtoOperation.getId()); } else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type - .POLICY)) { + .POLICY)) { dtoOperation = policyOperationDAO.getOperation(dtoOperation.getId()); } if (dtoOperation == null) { throw new OperationManagementException("Operation not found for operation Id:" + operationId + - " device" + " Id:" + device.getId()); + " device id:" + deviceId.getId()); } operation = OperationDAOUtil.convertOperation(dtoOperation); } catch (OperationManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving the list of " + - "operations assigned for '" + deviceId.getType() + "' device '" + deviceId.getId() - + "'", e); + "operations assigned for '" + deviceId.getType() + "' device '" + deviceId.getId() + + "'", e); } catch (DeviceManagementDAOException e) { String errorMsg = "Error occurred while retrieving the device " + - "for device Identifier type -'" + deviceId.getType() + "' and device Id '" - + deviceId.getId(); + "for device Identifier type -'" + deviceId.getType() + "' and device Id '" + + deviceId.getId(); log.error(errorMsg, e); throw new OperationManagementException(errorMsg, e); } finally { @@ -455,25 +439,28 @@ public class OperationManagerImpl implements OperationManager { OperationManagementDAOFactory.getConnection(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - Device device = deviceDAO.getDevice(deviceId, tenantId); + int enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); - if (device == null) { + if (enrolmentId < 0) { throw new OperationManagementException("Device not found for device id:" + deviceId.getId() + " " + - "type:" + deviceId.getType()); + "type:" + deviceId.getType()); } org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status dtoOpStatus = org.wso2.carbon.device .mgt.core.dto.operation.mgt.Operation.Status.valueOf(status.toString()); - dtoOperationList.addAll(commandOperationDAO.getOperationsByDeviceAndStatus(device.getId(), dtoOpStatus)); + dtoOperationList.addAll(commandOperationDAO.getOperationsByDeviceAndStatus(enrolmentId, dtoOpStatus)); - dtoOperationList.addAll(configOperationDAO.getOperationsByDeviceAndStatus(device.getId(), - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); + dtoOperationList.addAll( + configOperationDAO.getOperationsByDeviceAndStatus(enrolmentId, + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); - dtoOperationList.addAll(profileOperationDAO.getOperationsByDeviceAndStatus(device.getId(), - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); + dtoOperationList.addAll( + profileOperationDAO.getOperationsByDeviceAndStatus(enrolmentId, + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); - dtoOperationList.addAll(policyOperationDAO.getOperationsByDeviceAndStatus(device.getId(), - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); + dtoOperationList.addAll( + policyOperationDAO.getOperationsByDeviceAndStatus(enrolmentId, + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); Operation operation; @@ -484,11 +471,11 @@ public class OperationManagerImpl implements OperationManager { return operations; } catch (OperationManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving the list of " + - "operations assigned for '" + deviceId.getType() + "' device '" + - deviceId.getId() + "' and status:" + status.toString(), e); + "operations assigned for '" + deviceId.getType() + "' device '" + + deviceId.getId() + "' and status:" + status.toString(), e); } catch (DeviceManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving the device " + - "for device Identifier type -'" + deviceId.getType() + "' and device Id '" + deviceId.getId(), e); + "for device Identifier type -'" + deviceId.getType() + "' and device Id '" + deviceId.getId(), e); } finally { try { OperationManagementDAOFactory.closeConnection(); @@ -521,10 +508,10 @@ public class OperationManagerImpl implements OperationManager { .equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG)) { dtoOperation = configOperationDAO.getOperation(dtoOperation.getId()); } else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type - .PROFILE)) { + .PROFILE)) { dtoOperation = profileOperationDAO.getOperation(dtoOperation.getId()); } else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type - .POLICY)) { + .POLICY)) { dtoOperation = policyOperationDAO.getOperation(dtoOperation.getId()); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/PolicyOperation.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/PolicyOperation.java index 8bce4931fd..0740728be0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/PolicyOperation.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/PolicyOperation.java @@ -22,15 +22,15 @@ import java.util.List; public class PolicyOperation extends Operation { + public static final String POLICY_OPERATION_CODE = "POLICY_BUNDLE"; + private List profileOperations; + public List getProfileOperations() { return profileOperations; } - public static final String POLICY_OPERATION_CODE = "POLICY_BUNDLE"; public void setProfileOperations(List profileOperations) { this.profileOperations = profileOperations; } - private List profileOperations; - } 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 6cb4058a85..5c6751776b 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 @@ -18,7 +18,6 @@ */ package org.wso2.carbon.device.mgt.core.operation.mgt.dao; -import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; import java.util.List; @@ -33,18 +32,18 @@ public interface OperationDAO { Operation getOperation(int operationId) throws OperationManagementDAOException; - Operation getOperationByDeviceAndId(int deviceId, int operationId) throws OperationManagementDAOException; + Operation getOperationByDeviceAndId(int enrolmentId, int operationId) throws OperationManagementDAOException; - List getOperationsByDeviceAndStatus(int deviceId, Operation.Status status) + List getOperationsByDeviceAndStatus(int enrolmentId, Operation.Status status) throws OperationManagementDAOException; - List getOperationsForDevice(int deviceId) throws OperationManagementDAOException; + List getOperationsForDevice(int enrolmentId) throws OperationManagementDAOException; - Operation getNextOperation(int deviceId) throws OperationManagementDAOException; + Operation getNextOperation(int enrolmentId) throws OperationManagementDAOException; - void updateOperationStatus(int deviceId, int operationId,Operation.Status status) + void updateOperationStatus(int enrolmentId, int operationId,Operation.Status status) throws OperationManagementDAOException; - void addOperationResponse(int deviceId, int operationId, Object operationResponse) + 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/CommandOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/CommandOperationDAOImpl.java index a57a728638..82514bfa8e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/CommandOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/CommandOperationDAOImpl.java @@ -130,7 +130,7 @@ public class CommandOperationDAOImpl extends OperationDAOImpl { } @Override - public List getOperationsByDeviceAndStatus(int deviceId, + public List getOperationsByDeviceAndStatus(int enrolmentId, Operation.Status status) throws OperationManagementDAOException { PreparedStatement stmt = null; @@ -146,11 +146,11 @@ public class CommandOperationDAOImpl extends OperationDAOImpl { Connection conn = OperationManagementDAOFactory.getConnection(); String sql = "Select co.OPERATION_ID,ENABLED from DM_COMMAND_OPERATION co " + "INNER JOIN " + - "(Select * From DM_DEVICE_OPERATION_MAPPING WHERE DEVICE_ID=? " + + "(Select * From DM_ENROLMENT_OPERATION_MAPPING WHERE ENROLMENT_ID=? " + "AND STATUS=? ) dm ON dm.OPERATION_ID = co.OPERATION_ID"; stmt = conn.prepareStatement(sql); - stmt.setInt(1, deviceId); + stmt.setInt(1, enrolmentId); stmt.setString(2, status.toString()); rs = stmt.executeQuery(); @@ -169,7 +169,7 @@ public class CommandOperationDAOImpl extends OperationDAOImpl { } } catch (SQLException e) { - String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + deviceId + + String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + enrolmentId + "' with status '" + status.toString(); log.error(errorMsg); throw new OperationManagementDAOException(errorMsg, e); 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/ConfigOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ConfigOperationDAOImpl.java index 584859a714..48e0c5f3a8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ConfigOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ConfigOperationDAOImpl.java @@ -168,7 +168,7 @@ public class ConfigOperationDAOImpl extends OperationDAOImpl { } @Override - public List getOperationsByDeviceAndStatus(int deviceId, + public List getOperationsByDeviceAndStatus(int enrolmentId, Operation.Status status) throws OperationManagementDAOException { PreparedStatement stmt = null; @@ -184,11 +184,11 @@ public class ConfigOperationDAOImpl extends OperationDAOImpl { Connection conn = OperationManagementDAOFactory.getConnection(); String sql = "Select co.OPERATION_ID, co.OPERATION_CONFIG from DM_CONFIG_OPERATION co " + "INNER JOIN " + - "(Select * From DM_DEVICE_OPERATION_MAPPING WHERE DEVICE_ID=? " + + "(Select * From DM_ENROLMENT_OPERATION_MAPPING WHERE ENROLMENT_ID=? " + "AND STATUS=?) dm ON dm.OPERATION_ID = co.OPERATION_ID"; stmt = conn.prepareStatement(sql); - stmt.setInt(1, deviceId); + stmt.setInt(1, enrolmentId); stmt.setString(2, status.toString()); rs = stmt.executeQuery(); @@ -211,7 +211,7 @@ public class ConfigOperationDAOImpl extends OperationDAOImpl { log.error(errorMsg, e); throw new OperationManagementDAOException(errorMsg, e); } catch (SQLException e) { - String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + deviceId + + String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + enrolmentId + "' with status '" + status.toString(); log.error(errorMsg); throw new OperationManagementDAOException(errorMsg, e); 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/OperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationDAOImpl.java index d20b201293..278199989e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationDAOImpl.java @@ -86,17 +86,17 @@ public class OperationDAOImpl implements OperationDAO { } } - public void updateOperationStatus(int deviceId, int operationId, Operation.Status status) + public void updateOperationStatus(int enrolmentId, int operationId, Operation.Status status) throws OperationManagementDAOException { PreparedStatement stmt = null; try { Connection connection = OperationManagementDAOFactory.getConnection(); - stmt = connection.prepareStatement("UPDATE DM_DEVICE_OPERATION_MAPPING O SET O.STATUS=? " + - "WHERE O.DEVICE_ID=? and O.OPERATION_ID=?"); + stmt = connection.prepareStatement("UPDATE DM_ENROLMENT_OPERATION_MAPPING O SET O.STATUS=? " + + "WHERE O.ENROLMENT_ID=? and O.OPERATION_ID=?"); stmt.setString(1, status.toString()); - stmt.setInt(2, deviceId); + stmt.setInt(2, enrolmentId); stmt.setInt(3, operationId); stmt.executeUpdate(); @@ -111,7 +111,7 @@ public class OperationDAOImpl implements OperationDAO { } @Override - public void addOperationResponse(int deviceId, int operationId, Object operationResponse) + public void addOperationResponse(int enrolmentId, int operationId, Object operationResponse) throws OperationManagementDAOException { PreparedStatement stmt = null; @@ -129,7 +129,7 @@ public class OperationDAOImpl implements OperationDAO { oos.writeObject(operationResponse); stmt.setInt(1, operationId); - stmt.setInt(2, deviceId); + stmt.setInt(2, enrolmentId); stmt.setBytes(3, bao.toByteArray()); stmt.executeUpdate(); @@ -214,7 +214,7 @@ public class OperationDAOImpl implements OperationDAO { } @Override - public Operation getOperationByDeviceAndId(int deviceId, int operationId) throws OperationManagementDAOException { + public Operation getOperationByDeviceAndId(int enrolmentId, int operationId) throws OperationManagementDAOException { PreparedStatement stmt = null; ResultSet rs = null; @@ -225,13 +225,13 @@ public class OperationDAOImpl implements OperationDAO { String sql = "SELECT o.ID, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, o.STATUS, o.OPERATION_CODE " + " From (SELECT ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, STATUS," + "OPERATION_CODE FROM DM_OPERATION WHERE id=?) o INNER JOIN (Select * from " + - "DM_DEVICE_OPERATION_MAPPING dm where dm.OPERATION_ID=? AND dm.DEVICE_ID=?) om " + + "DM_ENROLMENT_OPERATION_MAPPING dm where dm.OPERATION_ID=? AND dm.ENROLMENT_ID=?) om " + "ON o.ID = om.OPERATION_ID "; stmt = conn.prepareStatement(sql); stmt.setInt(1, operationId); stmt.setInt(2, operationId); - stmt.setInt(3, deviceId); + stmt.setInt(3, enrolmentId); rs = stmt.executeQuery(); if (rs.next()) { @@ -247,7 +247,7 @@ public class OperationDAOImpl implements OperationDAO { operation.setCode(rs.getString("OPERATION_CODE")); } } catch (SQLException e) { - String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + deviceId + + String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + enrolmentId + "' with id '" + operationId; log.error(errorMsg); throw new OperationManagementDAOException(errorMsg, e); @@ -259,7 +259,7 @@ public class OperationDAOImpl implements OperationDAO { } @Override - public List getOperationsByDeviceAndStatus(int deviceId, + public List getOperationsByDeviceAndStatus(int enrolmentId, Operation.Status status) throws OperationManagementDAOException { PreparedStatement stmt = null; @@ -272,11 +272,11 @@ public class OperationDAOImpl implements OperationDAO { Connection conn = OperationManagementDAOFactory.getConnection(); String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, OPERATION_CODE " + "FROM DM_OPERATION o " + - "INNER JOIN (Select * from DM_DEVICE_OPERATION_MAPPING dm " + - "where dm.DEVICE_ID=? and dm.STATUS=?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP ASC"; + "INNER JOIN (Select * from DM_ENROLMENT_OPERATION_MAPPING dm " + + "where dm.ENROLMENT_ID=? and dm.STATUS=?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP ASC"; stmt = conn.prepareStatement(sql); - stmt.setInt(1, deviceId); + stmt.setInt(1, enrolmentId); stmt.setString(2, status.toString()); rs = stmt.executeQuery(); @@ -296,7 +296,7 @@ public class OperationDAOImpl implements OperationDAO { operationList.add(operation); } } catch (SQLException e) { - String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + deviceId + + String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + enrolmentId + "' with status '" + status.toString(); log.error(errorMsg); throw new OperationManagementDAOException(errorMsg, e); @@ -308,7 +308,7 @@ public class OperationDAOImpl implements OperationDAO { } @Override - public List getOperationsForDevice(int deviceId) + public List getOperationsForDevice(int enrolmentId) throws OperationManagementDAOException { PreparedStatement stmt = null; @@ -321,11 +321,11 @@ public class OperationDAOImpl implements OperationDAO { Connection conn = OperationManagementDAOFactory.getConnection(); String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, " + "OPERATION_CODE,dm.STATUS FROM DM_OPERATION o " + - "INNER JOIN (Select * from DM_DEVICE_OPERATION_MAPPING dm " + - "where dm.DEVICE_ID=?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP ASC"; + "INNER JOIN (Select * from DM_ENROLMENT_OPERATION_MAPPING dm " + + "where dm.ENROLMENT_ID=?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP ASC"; stmt = conn.prepareStatement(sql); - stmt.setInt(1, deviceId); + stmt.setInt(1, enrolmentId); rs = stmt.executeQuery(); @@ -344,7 +344,7 @@ public class OperationDAOImpl implements OperationDAO { operationList.add(operation); } } catch (SQLException e) { - String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + deviceId + + String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + enrolmentId + "' with status '"; log.error(errorMsg); throw new OperationManagementDAOException(errorMsg, e); @@ -356,7 +356,7 @@ public class OperationDAOImpl implements OperationDAO { } @Override - public Operation getNextOperation(int deviceId) throws OperationManagementDAOException { + public Operation getNextOperation(int enrolmentId) throws OperationManagementDAOException { PreparedStatement stmt = null; ResultSet rs = null; @@ -365,11 +365,11 @@ public class OperationDAOImpl implements OperationDAO { Connection connection = OperationManagementDAOFactory.getConnection(); stmt = connection.prepareStatement("SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, " + "OPERATION_CODE FROM DM_OPERATION o " + - "INNER JOIN (Select * from DM_DEVICE_OPERATION_MAPPING dm " + - "where dm.DEVICE_ID=? AND dm.STATUS=?) om ON o.ID = om.OPERATION_ID " + + "INNER JOIN (Select * from DM_ENROLMENT_OPERATION_MAPPING dm " + + "where dm.ENROLMENT_ID=? AND dm.STATUS=?) om ON o.ID = om.OPERATION_ID " + "ORDER BY o.CREATED_TIMESTAMP ASC LIMIT 1"); - stmt.setInt(1, deviceId); + stmt.setInt(1, enrolmentId); stmt.setString(2, Operation.Status.PENDING.toString()); rs = stmt.executeQuery(); @@ -398,7 +398,7 @@ public class OperationDAOImpl implements OperationDAO { } - public List getOperationsByDeviceStatusAndType(int deviceId, + public List getOperationsByDeviceStatusAndType(int enrolmentId, Operation.Status status, Operation.Type type) throws OperationManagementDAOException { PreparedStatement stmt = null; @@ -412,12 +412,12 @@ public class OperationDAOImpl implements OperationDAO { String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, OPERATION_CODE FROM " + "(SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, OPERATION_CODE " + "FROM DM_OPERATION o WHERE o.TYPE=?) o " + - "INNER JOIN (Select * from DM_DEVICE_OPERATION_MAPPING dm " + - "where dm.DEVICE_ID=? and dm.STATUS=?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP ASC"; + "INNER JOIN (Select * from DM_ENROLMENT_OPERATION_MAPPING dm " + + "where dm.ENROLMENT_ID=? and dm.STATUS=?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP ASC"; stmt = conn.prepareStatement(sql); stmt.setString(1, type.toString()); - stmt.setInt(2, deviceId); + stmt.setInt(2, enrolmentId); stmt.setString(3, status.toString()); rs = stmt.executeQuery(); @@ -436,7 +436,7 @@ public class OperationDAOImpl implements OperationDAO { operationList.add(operation); } } catch (SQLException e) { - String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + deviceId + + String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + enrolmentId + "' with status '" + status.toString(); log.error(errorMsg); throw new OperationManagementDAOException(errorMsg, e); 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/OperationMappingDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationMappingDAOImpl.java index 96a1b6123f..9f69b1a6f5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationMappingDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationMappingDAOImpl.java @@ -35,7 +35,7 @@ public class OperationMappingDAOImpl implements OperationMappingDAO { PreparedStatement stmt = null; try { Connection conn = OperationManagementDAOFactory.getConnection(); - String sql = "INSERT INTO DM_DEVICE_OPERATION_MAPPING(DEVICE_ID, OPERATION_ID,STATUS) VALUES(?, ?,?)"; + String sql = "INSERT INTO DM_ENROLMENT_OPERATION_MAPPING(ENROLMENT_ID, OPERATION_ID, STATUS) VALUES (?, ?,?)"; stmt = conn.prepareStatement(sql); stmt.setInt(1, deviceId); stmt.setInt(2, operationId); @@ -54,7 +54,7 @@ public class OperationMappingDAOImpl implements OperationMappingDAO { PreparedStatement stmt = null; try { Connection conn = OperationManagementDAOFactory.getConnection(); - String sql = "DELETE FROM DM_DEVICE_OPERATION_MAPPING WHERE DEVICE_ID = ? AND OPERATION_ID = ?"; + String sql = "DELETE FROM DM_ENROLMENT_OPERATION_MAPPING WHERE ENROLMENT_ID = ? AND OPERATION_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, 0); stmt.setInt(2, operationId); 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/PolicyOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/PolicyOperationDAOImpl.java index 9ef96d2b51..59f758a473 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/PolicyOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/PolicyOperationDAOImpl.java @@ -171,7 +171,7 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl { } @Override - public List getOperationsByDeviceAndStatus(int deviceId, + public List getOperationsByDeviceAndStatus(int enrolmentId, Operation.Status status) throws OperationManagementDAOException { PreparedStatement stmt = null; @@ -187,11 +187,11 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl { Connection conn = OperationManagementDAOFactory.getConnection(); String sql = "Select po.OPERATION_ID, ENABLED, OPERATION_DETAILS from DM_POLICY_OPERATION po " + "INNER JOIN " + - "(Select * From DM_DEVICE_OPERATION_MAPPING WHERE DEVICE_ID=? " + + "(Select * From DM_ENROLMENT_OPERATION_MAPPING WHERE ENROLMENT_ID=? " + "AND STATUS=?) dm ON dm.OPERATION_ID = po.OPERATION_ID"; stmt = conn.prepareStatement(sql); - stmt.setInt(1, deviceId); + stmt.setInt(1, enrolmentId); stmt.setString(2, status.toString()); rs = stmt.executeQuery(); @@ -214,7 +214,7 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl { log.error(errorMsg, e); throw new OperationManagementDAOException(errorMsg, e); } catch (SQLException e) { - String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + deviceId + + String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + enrolmentId + "' with status '" + status.toString(); log.error(errorMsg); throw new OperationManagementDAOException(errorMsg, e); 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/ProfileOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ProfileOperationDAOImpl.java index b7b96513db..d8cab721c7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ProfileOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ProfileOperationDAOImpl.java @@ -167,7 +167,7 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl { } @Override - public List getOperationsByDeviceAndStatus(int deviceId, + public List getOperationsByDeviceAndStatus(int enrolmentId, Operation.Status status) throws OperationManagementDAOException { PreparedStatement stmt = null; @@ -183,11 +183,11 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl { Connection conn = OperationManagementDAOFactory.getConnection(); String sql = "Select po.OPERATION_ID, ENABLED, OPERATION_DETAILS from DM_PROFILE_OPERATION po " + "INNER JOIN " + - "(Select * From DM_DEVICE_OPERATION_MAPPING WHERE DEVICE_ID=? " + + "(Select * From DM_ENROLMENT_OPERATION_MAPPING WHERE ENROLMENT_ID=? " + "AND STATUS=?) dm ON dm.OPERATION_ID = po.OPERATION_ID"; stmt = conn.prepareStatement(sql); - stmt.setInt(1, deviceId); + stmt.setInt(1, enrolmentId); stmt.setString(2, status.toString()); rs = stmt.executeQuery(); @@ -210,7 +210,7 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl { log.error(errorMsg, e); throw new OperationManagementDAOException(errorMsg, e); } catch (SQLException e) { - String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + deviceId + + String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + enrolmentId + "' with status '" + status.toString(); log.error(errorMsg); throw new OperationManagementDAOException(errorMsg, e); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 225e3b4248..a77c6b4144 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -48,7 +48,7 @@ import java.util.Date; import java.util.List; public class DeviceManagementProviderServiceImpl implements DeviceManagementProviderService, - PluginInitializationListener { + PluginInitializationListener { private DeviceDAO deviceDAO; private DeviceTypeDAO deviceTypeDAO; @@ -67,7 +67,12 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementServiceComponent.registerPluginInitializationListener(this); } - DeviceManagementProviderServiceImpl(DeviceManagementPluginRepository pluginRepo, boolean test){ + /** + * This constructor calls from unit tests + * + * @param pluginRepo + */ + DeviceManagementProviderServiceImpl(DeviceManagementPluginRepository pluginRepo, boolean test) { this.pluginRepository = pluginRepo; initDataAccessObjects(); } @@ -106,31 +111,60 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public boolean enrollDevice(Device device) throws DeviceManagementException { - + boolean status = false; + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()); DeviceManager dms = this.getPluginRepository().getDeviceManagementService(device.getType()).getDeviceManager(); - boolean status = dms.enrollDevice(device); + dms.enrollDevice(device); try { - if (dms.isClaimable(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()))) { + if (dms.isClaimable(deviceIdentifier)) { device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.INACTIVE); } else { device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.ACTIVE); } int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - DeviceManagementDAOFactory.beginTransaction(); - - DeviceType type = deviceTypeDAO.getDeviceType(device.getType()); - int deviceId = deviceDAO.addDevice(type.getId(), device, tenantId); - int enrolmentId = enrolmentDAO.addEnrollment(deviceId, device.getEnrolmentInfo(), tenantId); - - if (log.isDebugEnabled()) { - log.debug("An enrolment is successfully created with the id '" + enrolmentId + "' associated with " + - "the device identified by key '" + device.getDeviceIdentifier() + "', which belongs to " + - "platform '" + device.getType() + " upon the user '" + - device.getEnrolmentInfo().getOwner() + "'"); + Device existingDevice = this.getDevice(deviceIdentifier); + + if (existingDevice != null) { + EnrolmentInfo existingEnrolmentInfo = existingDevice.getEnrolmentInfo(); + EnrolmentInfo newEnrolmentInfo = device.getEnrolmentInfo(); + if (existingEnrolmentInfo != null && newEnrolmentInfo != null) { + if (existingEnrolmentInfo.equals(newEnrolmentInfo)) { + device.getEnrolmentInfo().setDateOfEnrolment(existingEnrolmentInfo.getDateOfEnrolment()); + this.modifyEnrollment(device); + status = true; + } else { + this.setStatus(deviceIdentifier, existingEnrolmentInfo.getOwner(), EnrolmentInfo.Status.INACTIVE); + DeviceManagementDAOFactory.beginTransaction(); + int enrolmentId = enrolmentDAO.addEnrollment(existingDevice.getId(), newEnrolmentInfo, tenantId); + DeviceManagementDAOFactory.commitTransaction(); + + if (log.isDebugEnabled()) { + log.debug("An enrolment is successfully updated with the id '" + enrolmentId + + "' associated with " + "the device identified by key '" + device.getDeviceIdentifier() + + "', which belongs to " + "platform '" + device.getType() + " upon the user '" + + device.getEnrolmentInfo().getOwner() + "'"); + } + status = true; + } + } + } else { + DeviceManagementDAOFactory.beginTransaction(); + DeviceType type = deviceTypeDAO.getDeviceType(device.getType()); + int deviceId = deviceDAO.addDevice(type.getId(), device, tenantId); + int enrolmentId = enrolmentDAO.addEnrollment(deviceId, device.getEnrolmentInfo(), tenantId); + DeviceManagementDAOFactory.commitTransaction(); + + if (log.isDebugEnabled()) { + log.debug("An enrolment is successfully created with the id '" + enrolmentId + "' associated with " + + "the device identified by key '" + device.getDeviceIdentifier() + "', which belongs to " + + "platform '" + device.getType() + " upon the user '" + + device.getEnrolmentInfo().getOwner() + "'"); + } + status = true; } - DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { try { DeviceManagementDAOFactory.rollbackTransaction(); @@ -138,7 +172,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.warn("Error occurred while roll-backing the current transaction", e); } throw new DeviceManagementException("Error occurred while enrolling the device " + - "'" + device.getId() + "'", e); + "'" + device.getId() + "'", e); } finally { try { DeviceManagementDAOFactory.closeConnection(); @@ -150,7 +184,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } - @Override public boolean modifyEnrollment(Device device) throws DeviceManagementException { DeviceManager dms = @@ -171,7 +204,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.warn("Error occurred while roll-backing the current transaction", e); } throw new DeviceManagementException("Error occurred while modifying the device " + - "'" + device.getId() + "'", e); + "'" + device.getId() + "'", e); } finally { try { DeviceManagementDAOFactory.closeConnection(); @@ -189,16 +222,17 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManager dms = this.getPluginRepository().getDeviceManagementService(deviceId.getType()).getDeviceManager(); try { - Device device = deviceDAO.getDevice(deviceId,tenantId); + Device device = deviceDAO.getDevice(deviceId, tenantId); DeviceType deviceType = deviceTypeDAO.getDeviceType(device.getType()); device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.REMOVED); + enrolmentDAO.updateEnrollment(device.getId(), device.getEnrolmentInfo(), tenantId); deviceDAO.updateDevice(deviceType.getId(), device, tenantId); } catch (DeviceManagementDAOException e) { - String errorMsg = "Error occurred while fetch device for device Identifier:"; - log.error(errorMsg + deviceId.toString(),e); + String errorMsg = "Error occurred while fetch device for device Identifier:"; + log.error(errorMsg + deviceId.toString(), e); throw new DeviceManagementException(errorMsg, e); } @@ -236,7 +270,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv allDevices = deviceDAO.getDevices(tenantId); } catch (DeviceManagementDAOException e) { throw new DeviceManagementException("Error occurred while retrieving device list pertaining to " + - "the current tenant", e); + "the current tenant", e); } finally { try { DeviceManagementDAOFactory.closeConnection(); @@ -268,7 +302,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv allDevices = deviceDAO.getDevices(type, tenantId); } catch (DeviceManagementDAOException e) { throw new DeviceManagementException("Error occurred while retrieving all devices of type '" + - type + "' that are being managed within the scope of current tenant", e); + type + "' that are being managed within the scope of current tenant", e); } finally { try { DeviceManagementDAOFactory.closeConnection(); @@ -324,13 +358,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv try { messageHeader = messageHeader.replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants.FIRST_NAME + "\\}", - URLEncoder.encode(emailMessageProperties.getFirstName(), - EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); + URLEncoder.encode(emailMessageProperties.getFirstName(), + EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); messageBody = messageBody.trim() + System.getProperty("line.separator") + - System.getProperty("line.separator") + url.replaceAll("\\{" - + EmailConstants.EnrolmentEmailConstants.DOWNLOAD_URL + "\\}", - URLDecoder.decode(emailMessageProperties.getEnrolmentUrl(), - EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); + System.getProperty("line.separator") + url.replaceAll("\\{" + + EmailConstants.EnrolmentEmailConstants.DOWNLOAD_URL + "\\}", + URLDecoder.decode(emailMessageProperties.getEnrolmentUrl(), + EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); messageBuilder.append(messageHeader).append(System.getProperty("line.separator")) .append(System.getProperty("line.separator")); @@ -338,12 +372,12 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv messageBuilder.append(System.getProperty("line.separator")).append(System.getProperty("line.separator")); messageBuilder.append(messageFooter1.trim()) .append(System.getProperty("line.separator")).append(messageFooter2.trim()).append(System - .getProperty("line.separator")).append(messageFooter3.trim()); + .getProperty("line.separator")).append(messageFooter3.trim()); } catch (IOException e) { log.error("IO error in processing enrol email message " + emailMessageProperties); throw new DeviceManagementException("Error replacing tags in email template '" + - emailMessageProperties.getSubject() + "'", e); + emailMessageProperties.getSubject() + "'", e); } emailMessageProperties.setMessageBody(messageBuilder.toString()); emailMessageProperties.setSubject(subject); @@ -381,23 +415,23 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv try { messageHeader = messageHeader.replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants.FIRST_NAME + "\\}", - URLEncoder.encode(emailMessageProperties.getFirstName(), - EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); + URLEncoder.encode(emailMessageProperties.getFirstName(), + EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); messageBody = messageBody.trim().replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants - .USERNAME - + "\\}", - URLEncoder.encode(emailMessageProperties.getUserName(), EmailConstants.EnrolmentEmailConstants - .ENCODED_SCHEME)); + .USERNAME + + "\\}", + URLEncoder.encode(emailMessageProperties.getUserName(), EmailConstants.EnrolmentEmailConstants + .ENCODED_SCHEME)); messageBody = messageBody.replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants.PASSWORD + "\\}", - URLEncoder.encode(emailMessageProperties.getPassword(), EmailConstants.EnrolmentEmailConstants - .ENCODED_SCHEME)); + URLEncoder.encode(emailMessageProperties.getPassword(), EmailConstants.EnrolmentEmailConstants + .ENCODED_SCHEME)); messageBody = messageBody + System.getProperty("line.separator") + url.replaceAll("\\{" - + EmailConstants.EnrolmentEmailConstants.DOWNLOAD_URL + "\\}", - URLDecoder.decode(emailMessageProperties.getEnrolmentUrl(), - EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); + + EmailConstants.EnrolmentEmailConstants.DOWNLOAD_URL + "\\}", + URLDecoder.decode(emailMessageProperties.getEnrolmentUrl(), + EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); messageBuilder.append(messageHeader).append(System.getProperty("line.separator")); messageBuilder.append(messageBody).append(System.getProperty("line.separator")).append( @@ -408,7 +442,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } catch (IOException e) { log.error("IO error in processing enrol email message " + emailMessageProperties); throw new DeviceManagementException("Error replacing tags in email template '" + - emailMessageProperties.getSubject() + "'", e); + emailMessageProperties.getSubject() + "'", e); } emailMessageProperties.setMessageBody(messageBuilder.toString()); emailMessageProperties.setSubject(subject); @@ -423,7 +457,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv device = deviceDAO.getDevice(deviceId, tenantId); } catch (DeviceManagementDAOException e) { throw new DeviceManagementException("Error occurred while obtaining the device for id " + - "'" + deviceId.getId() + "'", e); + "'" + deviceId.getId() + "'", e); } finally { try { DeviceManagementDAOFactory.closeConnection(); @@ -526,7 +560,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public int addOperation(Operation operation, List devices) throws - OperationManagementException { + OperationManagementException { return DeviceManagementDataHolder.getInstance().getOperationManager().addOperation(operation, devices); } @@ -586,7 +620,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv userDevices = deviceDAO.getDevicesOfUser(username, tenantId); } catch (DeviceManagementDAOException e) { throw new DeviceManagementException("Error occurred while retrieving the list of devices that " + - "belong to the user '" + username + "'", e); + "belong to the user '" + username + "'", e); } finally { try { DeviceManagementDAOFactory.closeConnection(); @@ -617,11 +651,11 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv String[] users; int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); try { - users = DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId) + users = DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId) .getUserStoreManager().getUserListOfRole(role); } catch (UserStoreException e) { throw new DeviceManagementException("Error occurred while obtaining the users, who are assigned " + - "with the role '" + role + "'", e); + "with the role '" + role + "'", e); } List userDevices; @@ -681,7 +715,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv allDevices = deviceDAO.getDevicesByName(deviceName, tenantId); } catch (DeviceManagementDAOException e) { throw new DeviceManagementException("Error occurred while fetching the list of devices that matches to '" - + deviceName + "'", e); + + deviceName + "'", e); } finally { try { DeviceManagementDAOFactory.closeConnection(); @@ -713,8 +747,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); device.getEnrolmentInfo().setStatus(status); deviceDAO.updateDevice(deviceType.getId(), device, tenantId); - }catch (DeviceManagementDAOException deviceDaoEx){ - String errorMsg = "Error occured update device enrolment status : "+device.getId(); + } catch (DeviceManagementDAOException deviceDaoEx) { + String errorMsg = "Error occured update device enrolment status : " + device.getId(); log.error(errorMsg, deviceDaoEx); throw new DeviceManagementException(errorMsg, deviceDaoEx); } @@ -726,7 +760,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv pluginRepository.addDeviceManagementProvider(deviceManagementService); } catch (DeviceManagementException e) { log.error("Error occurred while registering device management plugin '" + - deviceManagementService.getType() + "'", e); + deviceManagementService.getType() + "'", e); } } @@ -736,7 +770,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv pluginRepository.removeDeviceManagementProvider(deviceManagementService); } catch (DeviceManagementException e) { log.error("Error occurred while un-registering device management plugin '" + - deviceManagementService.getType() + "'", e); + deviceManagementService.getType() + "'", e); } } @@ -772,6 +806,4 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } return devices; } - - } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql index 9a9c47e2e3..8919ac5441 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql @@ -50,18 +50,6 @@ CREATE TABLE IF NOT EXISTS DM_PROFILE_OPERATION ( DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); -CREATE TABLE IF NOT EXISTS DM_DEVICE_OPERATION_MAPPING ( - ID INTEGER AUTO_INCREMENT NOT NULL, - DEVICE_ID INTEGER NOT NULL, - OPERATION_ID INTEGER NOT NULL, - STATUS VARCHAR(50) NULL, - PRIMARY KEY (ID), - CONSTRAINT fk_dm_device_operation_mapping_device FOREIGN KEY (DEVICE_ID) REFERENCES - DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, - CONSTRAINT fk_dm_device_operation_mapping_operation FOREIGN KEY (OPERATION_ID) REFERENCES - DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION -); - CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( ID INTEGER AUTO_INCREMENT NOT NULL, DEVICE_ID INTEGER NOT NULL, @@ -75,6 +63,20 @@ CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( CONSTRAINT fk_dm_device_enrolment FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); + +CREATE TABLE IF NOT EXISTS DM_ENROLMENT_OPERATION_MAPPING ( + ID INTEGER AUTO_INCREMENT NOT NULL, + ENROLMENT_ID INTEGER NOT NULL, + OPERATION_ID INTEGER NOT NULL, + STATUS VARCHAR(50) NULL, + PRIMARY KEY (ID), + CONSTRAINT fk_dm_device_operation_mapping_device FOREIGN KEY (ENROLMENT_ID) REFERENCES + DM_ENROLMENT (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, + CONSTRAINT fk_dm_device_operation_mapping_operation FOREIGN KEY (OPERATION_ID) REFERENCES + DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION +); + + CREATE TABLE IF NOT EXISTS DM_APPLICATION ( ID INTEGER AUTO_INCREMENT NOT NULL, NAME VARCHAR(50) NOT NULL, diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Profile.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Profile.java index 8c09357f0c..259a1e19bc 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Profile.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Profile.java @@ -24,10 +24,11 @@ import org.wso2.carbon.device.mgt.core.dto.DeviceType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; +import java.io.Serializable; import java.sql.Timestamp; import java.util.List; @XmlRootElement -public class Profile { +public class Profile implements Serializable { private int profileId; private String profileName; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java index 92940de313..6d9bee3b45 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java @@ -89,12 +89,12 @@ public interface PolicyDAO { List getPolicyAppliedUsers(int policyId) throws PolicyManagerDAOException; - void addEffectivePolicyToDevice(int deviceId, int policyId, List profileFeatures) + void addEffectivePolicyToDevice(int deviceId, Policy policy) throws PolicyManagerDAOException; void setPolicyApplied(int deviceId) throws PolicyManagerDAOException; - void updateEffectivePolicyToDevice(int deviceId, int policyId, List profileFeatures) + void updateEffectivePolicyToDevice(int deviceId, Policy policy) throws PolicyManagerDAOException; boolean checkPolicyAvailable(int deviceId) throws PolicyManagerDAOException; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java index 08232ea7ed..5ef728fba0 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java @@ -416,7 +416,7 @@ public class PolicyDAOImpl implements PolicyDAO { } catch (SQLException e) { String msg = "Error occurred while inserting the criterion to policy (" + policy.getPolicyName() + ") " + - "to database."; + "to database."; log.error(msg, e); throw new PolicyManagerDAOException(msg, e); } finally { @@ -436,7 +436,7 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); String query = "INSERT INTO DM_POLICY_CRITERIA_PROPERTIES (POLICY_CRITERION_ID, PROP_KEY, PROP_VALUE, " + - "CONTENT) VALUES (?, ?, ?, ?)"; + "CONTENT) VALUES (?, ?, ?, ?)"; stmt = conn.prepareStatement(query); for (PolicyCriterion criterion : policyCriteria) { @@ -477,9 +477,9 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); String query = "SELECT DPC.ID, DPC.CRITERIA_ID, DPCP.PROP_KEY, DPCP.PROP_VALUE, DPCP.CONTENT FROM " + - "DM_POLICY_CRITERIA DPC LEFT JOIN DM_POLICY_CRITERIA_PROPERTIES DPCP " + - "ON DPCP.POLICY_CRITERION_ID = DPC.ID RIGHT JOIN DM_CRITERIA DC " + - "ON DC.ID=DPC.CRITERIA_ID WHERE DPC.POLICY_ID = ?"; + "DM_POLICY_CRITERIA DPC LEFT JOIN DM_POLICY_CRITERIA_PROPERTIES DPCP " + + "ON DPCP.POLICY_CRITERION_ID = DPC.ID RIGHT JOIN DM_CRITERIA DC " + + "ON DC.ID=DPC.CRITERIA_ID WHERE DPC.POLICY_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, policyId); resultSet = stmt.executeQuery(); @@ -525,7 +525,7 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); String query = "UPDATE DM_POLICY SET NAME= ?, TENANT_ID = ?, PROFILE_ID = ?, PRIORITY = ?, COMPLIANCE = ?" + - " WHERE ID = ?"; + " WHERE ID = ?"; stmt = conn.prepareStatement(query); stmt.setString(1, policy.getPolicyName()); stmt.setInt(2, policy.getTenantId()); @@ -749,7 +749,7 @@ public class PolicyDAOImpl implements PolicyDAO { @Override - public void addEffectivePolicyToDevice(int deviceId, int policyId, List profileFeatures) + public void addEffectivePolicyToDevice(int deviceId, Policy policy) throws PolicyManagerDAOException { Connection conn; @@ -758,11 +758,11 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); String query = "INSERT INTO DM_DEVICE_POLICY_APPLIED " + - "(DEVICE_ID, POLICY_ID, POLICY_CONTENT, CREATED_TIME, UPDATED_TIME) VALUES (?, ?, ?, ?, ?)"; + "(DEVICE_ID, POLICY_ID, POLICY_CONTENT, CREATED_TIME, UPDATED_TIME) VALUES (?, ?, ?, ?, ?)"; stmt = conn.prepareStatement(query); stmt.setInt(1, deviceId); - stmt.setInt(2, policyId); - stmt.setObject(3, profileFeatures); + stmt.setInt(2, policy.getId()); + stmt.setObject(3, policy); stmt.setTimestamp(4, currentTimestamp); stmt.setTimestamp(5, currentTimestamp); @@ -805,7 +805,7 @@ public class PolicyDAOImpl implements PolicyDAO { @Override - public void updateEffectivePolicyToDevice(int deviceId, int policyId, List profileFeatures) + public void updateEffectivePolicyToDevice(int deviceId, Policy policy) throws PolicyManagerDAOException { Connection conn; @@ -814,10 +814,10 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); String query = "UPDATE DM_DEVICE_POLICY_APPLIED SET POLICY_ID = ?, POLICY_CONTENT = ?, UPDATED_TIME = ?, " + - "APPLIED = ? WHERE DEVICE_ID = ?"; + "APPLIED = ? WHERE DEVICE_ID = ?"; stmt = conn.prepareStatement(query); - stmt.setInt(1, policyId); - stmt.setObject(2, profileFeatures); + stmt.setInt(1, policy.getId()); + stmt.setObject(2, policy); stmt.setTimestamp(3, currentTimestamp); stmt.setBoolean(4, false); stmt.setInt(5, deviceId); @@ -1068,7 +1068,7 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); String query = "INSERT INTO DM_POLICY (NAME, PROFILE_ID, TENANT_ID, PRIORITY, COMPLIANCE, OWNERSHIP_TYPE)" + - " VALUES (?, ?, ?, ?, ?, ?)"; + " VALUES (?, ?, ?, ?, ?, ?)"; stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); stmt.setString(1, policy.getPolicyName()); @@ -1245,28 +1245,30 @@ public class PolicyDAOImpl implements PolicyDAO { stmt.setInt(1, deviceId); resultSet = stmt.executeQuery(); - ByteArrayInputStream bais = null; ObjectInputStream ois = null; byte[] contentBytes; - try { - contentBytes = (byte[]) resultSet.getBytes("POLICY_CONTENT"); - bais = new ByteArrayInputStream(contentBytes); - ois = new ObjectInputStream(bais); - policy = (Policy) ois.readObject(); - } finally { - if (bais != null) { - try { - bais.close(); - } catch (IOException e) { - log.warn("Error occurred while closing ByteArrayOutputStream", e); + + while (resultSet.next()) { + try { + contentBytes = (byte[]) resultSet.getBytes("POLICY_CONTENT"); + bais = new ByteArrayInputStream(contentBytes); + ois = new ObjectInputStream(bais); + policy = (Policy) ois.readObject(); + } finally { + if (bais != null) { + try { + bais.close(); + } catch (IOException e) { + log.warn("Error occurred while closing ByteArrayOutputStream", e); + } } - } - if (ois != null) { - try { - ois.close(); - } catch (IOException e) { - log.warn("Error occurred while closing ObjectOutputStream", e); + if (ois != null) { + try { + ois.close(); + } catch (IOException e) { + log.warn("Error occurred while closing ObjectOutputStream", e); + } } } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java index f398658df9..edc02bb973 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java @@ -38,7 +38,7 @@ public interface PolicyManager { boolean deletePolicy(int policyId) throws PolicyManagementException; Policy addPolicyToDevice(List deviceIdentifierList, Policy policy) throws - PolicyManagementException; + PolicyManagementException; Policy addPolicyToRole(List roleNames, Policy policy) throws PolicyManagementException; @@ -60,10 +60,10 @@ public interface PolicyManager { List getPolicyAppliedDevicesIds(int policyId) throws PolicyManagementException; - void addAppliedPolicyFeaturesToDevice(DeviceIdentifier deviceIdentifier, int policyId, List - profileFeatures) throws PolicyManagementException; + void addAppliedPolicyFeaturesToDevice(DeviceIdentifier deviceIdentifier, Policy policy) + throws PolicyManagementException; - void addAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier, Policy policy) throws PolicyManagementException; + void addAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier, Policy policy) throws PolicyManagementException; boolean checkPolicyAvailable(DeviceIdentifier deviceIdentifier) throws PolicyManagementException; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java index 2cc6be7589..11db886791 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java @@ -214,21 +214,22 @@ public class MonitoringManagerImpl implements MonitoringManager { Map tempMap = new HashMap<>(); + if (complianceDatas != null) { + for (ComplianceData complianceData : complianceDatas) { - for (ComplianceData complianceData : complianceDatas) { + tempMap.put(complianceData.getDeviceId(), complianceData); - tempMap.put(complianceData.getDeviceId(), complianceData); - - if (complianceData.getAttempts() == 0) { - deviceIdsToAddOperation.put(complianceData.getDeviceId(), - deviceIds.get(complianceData.getDeviceId())); - } else { - deviceIdsWithExistingOperation.put(complianceData.getDeviceId(), - deviceIds.get(complianceData.getDeviceId())); - } - if (complianceData.getAttempts() >= 20) { - inactiveDeviceIds.put(complianceData.getDeviceId(), - deviceIds.get(complianceData.getDeviceId())); + if (complianceData.getAttempts() == 0) { + deviceIdsToAddOperation.put(complianceData.getDeviceId(), + deviceIds.get(complianceData.getDeviceId())); + } else { + deviceIdsWithExistingOperation.put(complianceData.getDeviceId(), + deviceIds.get(complianceData.getDeviceId())); + } + if (complianceData.getAttempts() >= 20) { + inactiveDeviceIds.put(complianceData.getDeviceId(), + deviceIds.get(complianceData.getDeviceId())); + } } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java index be174530e7..627b77e8ff 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java @@ -708,8 +708,8 @@ public class PolicyManagerImpl implements PolicyManager { } @Override - public void addAppliedPolicyFeaturesToDevice(DeviceIdentifier deviceIdentifier, int policyId, - List profileFeatures) throws PolicyManagementException { + public void addAppliedPolicyFeaturesToDevice(DeviceIdentifier deviceIdentifier, Policy policy) + throws PolicyManagementException { int deviceId = -1; try { @@ -719,9 +719,9 @@ public class PolicyManagerImpl implements PolicyManager { boolean exist = policyDAO.checkPolicyAvailable(deviceId); PolicyManagementDAOFactory.beginTransaction(); if (exist) { - policyDAO.updateEffectivePolicyToDevice(deviceId, policyId, profileFeatures); + policyDAO.updateEffectivePolicyToDevice(deviceId, policy); } else { - policyDAO.addEffectivePolicyToDevice(deviceId, policyId, profileFeatures); + policyDAO.addEffectivePolicyToDevice(deviceId, policy); } PolicyManagementDAOFactory.commitTransaction(); } catch (PolicyManagerDAOException e) { @@ -731,7 +731,7 @@ public class PolicyManagerImpl implements PolicyManager { log.warn("Error occurred while roll backing the transaction."); } String msg = "Error occurred while adding the evaluated policy to device (" + - deviceId + " - " + policyId + ")"; + deviceId + " - " + policy.getId() + ")"; log.error(msg, e); throw new PolicyManagementException(msg, e); } catch (DeviceManagementException e) { @@ -756,12 +756,10 @@ public class PolicyManagerImpl implements PolicyManager { if (exist) { Policy policySaved = policyDAO.getAppliedPolicy(deviceId); if (!policy.equals(policySaved)) { - policyDAO.updateEffectivePolicyToDevice(deviceId, policy.getId(), policy.getProfile(). - getProfileFeaturesList()); + policyDAO.updateEffectivePolicyToDevice(deviceId, policy); } } else { - policyDAO.addEffectivePolicyToDevice(deviceId, policy.getId(), policy.getProfile(). - getProfileFeaturesList()); + policyDAO.addEffectivePolicyToDevice(deviceId, policy); } PolicyManagementDAOFactory.commitTransaction(); } catch (PolicyManagerDAOException e) { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql index b7bd8e1f02..6061e7e8a5 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql @@ -79,14 +79,14 @@ CREATE TABLE IF NOT EXISTS DM_PROFILE_OPERATION ( DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); -CREATE TABLE IF NOT EXISTS DM_DEVICE_OPERATION_MAPPING ( +CREATE TABLE IF NOT EXISTS DM_ENROLMENT_OPERATION_MAPPING ( ID INTEGER AUTO_INCREMENT NOT NULL, - DEVICE_ID INTEGER NOT NULL, + ENROLMENT_ID INTEGER NOT NULL, OPERATION_ID INTEGER NOT NULL, STATUS VARCHAR(50) NULL, PRIMARY KEY (ID), - CONSTRAINT fk_dm_device_operation_mapping_device FOREIGN KEY (DEVICE_ID) REFERENCES - DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, + CONSTRAINT fk_dm_device_operation_mapping_device FOREIGN KEY (ENROLMENT_ID) REFERENCES + DM_ENROLMENT (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT fk_dm_device_operation_mapping_operation FOREIGN KEY (OPERATION_ID) REFERENCES DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql index 6c742534ff..a599e6989f 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -64,14 +64,28 @@ CREATE TABLE IF NOT EXISTS DM_PROFILE_OPERATION ( DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); -CREATE TABLE IF NOT EXISTS DM_DEVICE_OPERATION_MAPPING ( +CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( ID INTEGER AUTO_INCREMENT NOT NULL, DEVICE_ID INTEGER NOT NULL, + OWNER VARCHAR(50) NOT NULL, + OWNERSHIP VARCHAR(45) NULL DEFAULT NULL, + STATUS VARCHAR(50) NULL, + DATE_OF_ENROLMENT TIMESTAMP NULL DEFAULT NULL, + DATE_OF_LAST_UPDATE TIMESTAMP NULL DEFAULT NULL, + TENANT_ID INT NOT NULL, + PRIMARY KEY (ID), + CONSTRAINT fk_dm_device_enrolment FOREIGN KEY (DEVICE_ID) REFERENCES + DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION +); + +CREATE TABLE IF NOT EXISTS DM_ENROLMENT_OPERATION_MAPPING ( + ID INTEGER AUTO_INCREMENT NOT NULL, + ENROLMENT_ID INTEGER NOT NULL, OPERATION_ID INTEGER NOT NULL, STATUS VARCHAR(50) NULL, PRIMARY KEY (ID), - CONSTRAINT fk_dm_device_operation_mapping_device FOREIGN KEY (DEVICE_ID) REFERENCES - DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, + CONSTRAINT fk_dm_device_operation_mapping_device FOREIGN KEY (ENROLMENT_ID) REFERENCES + DM_ENROLMENT (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT fk_dm_device_operation_mapping_operation FOREIGN KEY (OPERATION_ID) REFERENCES DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); @@ -353,6 +367,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_APPLICATION_MAPPING ( DM_APPLICATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); + -- POLICY RELATED TABLES FINISHED -- diff --git a/features/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework.server.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework.server.feature/src/main/resources/dbscripts/cdm/h2.sql index 269942edf9..da8fc0e154 100644 --- a/features/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework.server.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework.server.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -62,14 +62,14 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_OPERATION ( DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); -CREATE TABLE IF NOT EXISTS DM_DEVICE_OPERATION_MAPPING ( +CREATE TABLE IF NOT EXISTS DM_ENROLMENT_OPERATION_MAPPING ( ID INTEGER AUTO_INCREMENT NOT NULL, - DEVICE_ID INTEGER NOT NULL, + ENROLMENT_ID INTEGER NOT NULL, OPERATION_ID INTEGER NOT NULL, STATUS VARCHAR(50) NULL, PRIMARY KEY (ID), - CONSTRAINT fk_dm_device_operation_mapping_device FOREIGN KEY (DEVICE_ID) REFERENCES - DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, + CONSTRAINT fk_dm_device_operation_mapping_device FOREIGN KEY (ENROLMENT_ID) REFERENCES + DM_ENROLMENT (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT fk_dm_device_operation_mapping_operation FOREIGN KEY (OPERATION_ID) REFERENCES DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION );