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 27d415f33c..cc1219c3a1 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 @@ -334,6 +334,16 @@ public interface DeviceDAO { EnrolmentInfo getEnrolment(DeviceIdentifier deviceId, String currentUser, int tenantId) throws DeviceManagementDAOException; + /** + * This method is used to retrieve current active enrollment of a given device and tenant id. + * + * @param deviceId device id. + * @param tenantId tenant id. + * @return returns EnrolmentInfo object. + * @throws DeviceManagementDAOException + */ + EnrolmentInfo getActiveEnrolment(DeviceIdentifier deviceId, int tenantId) throws DeviceManagementDAOException; + /** * This method is used to retrieve devices of a given enrollment status. * diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index 0f32390685..fb78e28567 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -28,6 +28,7 @@ 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.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.dto.DeviceType; +import org.wso2.carbon.utils.xml.StringUtils; import java.sql.Connection; import java.sql.PreparedStatement; @@ -793,6 +794,38 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } } + @Override + public EnrolmentInfo getActiveEnrolment(DeviceIdentifier deviceId, int tenantId) throws DeviceManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + EnrolmentInfo enrolmentInfo = null; + try { + conn = this.getConnection(); + String sql = "SELECT ID AS ENROLMENT_ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, DATE_OF_ENROLMENT, " + + "DATE_OF_LAST_UPDATE, TENANT_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 TENANT_ID = ? AND STATUS in ('ACTIVE','UNREACHABLE','INACTIVE')"; + stmt = conn.prepareStatement(sql); + stmt.setString(1, deviceId.getId()); + stmt.setString(2, deviceId.getType()); + stmt.setInt(3, tenantId); + stmt.setInt(5, tenantId); + rs = stmt.executeQuery(); + if (rs.next()) { + enrolmentInfo = DeviceManagementDAOUtil.loadEnrolment(rs); + } + return enrolmentInfo; + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the enrolment " + + "information of device '" + deviceId.getId() + "' of type : " + + deviceId.getType(), e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + } + public int getEnrolmentByStatus(DeviceIdentifier deviceId, Status status, int tenantId) throws DeviceManagementDAOException { Connection conn; 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 27e15a9397..284a3bc892 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 @@ -274,15 +274,15 @@ public class OperationManagerImpl implements OperationManager { deviceId.getId() + "'"); } - int enrolmentId = this.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE); - if (enrolmentId < 0) { + EnrolmentInfo enrolmentInfo = this.getActiveEnrolmentInfo(deviceId); + if (enrolmentInfo == null) { return null; } try { OperationManagementDAOFactory.openConnection(); List operationList = - operationDAO.getOperationsForDevice(enrolmentId); + operationDAO.getOperationsForDevice(enrolmentInfo.getId()); operations = new ArrayList<>(); for (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation : operationList) { @@ -314,13 +314,13 @@ public class OperationManagerImpl implements OperationManager { deviceId.getId() + "'"); } - int enrolmentId = this.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE); - if (enrolmentId < 0) { + EnrolmentInfo enrolmentInfo = this.getActiveEnrolmentInfo(deviceId); + if (enrolmentInfo == null) { throw new OperationManagementException("Device not found for given device " + "Identifier:" + deviceId.getId() + " and given type" + deviceId.getType()); } - + int enrolmentId = enrolmentInfo.getId(); try { OperationManagementDAOFactory.openConnection(); List operationList = @@ -364,7 +364,7 @@ public class OperationManagerImpl implements OperationManager { } // - EnrolmentInfo enrolmentInfo = this.getEnrolmentInfo(deviceId); + EnrolmentInfo enrolmentInfo = this.getActiveEnrolmentInfo(deviceId); if (enrolmentInfo == null) { throw new OperationManagementException("Device not found for the given device Identifier:" + deviceId.getId() + " and given type:" + @@ -425,8 +425,8 @@ public class OperationManagerImpl implements OperationManager { deviceId.getId() + "'"); } - int enrolmentId = this.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE); - if (enrolmentId < 0) { + EnrolmentInfo enrolmentInfo = this.getActiveEnrolmentInfo(deviceId); + if (enrolmentInfo == null) { throw new OperationManagementException("Device not found for given device " + "Identifier:" + deviceId.getId() + " and given type" + deviceId.getType()); @@ -435,7 +435,7 @@ public class OperationManagerImpl implements OperationManager { try { OperationManagementDAOFactory.openConnection(); org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO.getNextOperation( - enrolmentId); + enrolmentInfo.getId()); if (dtoOperation != null) { if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND.equals(dtoOperation.getType() )) { @@ -480,9 +480,15 @@ public class OperationManagerImpl implements OperationManager { deviceId.getId() + "'"); } - int enrolmentId = this.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE); + EnrolmentInfo enrolmentInfo = this.getActiveEnrolmentInfo(deviceId); + if (enrolmentInfo == null) { + throw new OperationManagementException( + "Device not found for device id:" + deviceId.getId() + " " + "type:" + + deviceId.getType()); + } try { + int enrolmentId = enrolmentInfo.getId(); OperationManagementDAOFactory.beginTransaction(); boolean isUpdated = false; if (operation.getStatus() != null) { @@ -543,8 +549,8 @@ public class OperationManagerImpl implements OperationManager { deviceId.getId() + "'"); } - int enrolmentId = this.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE); - if (enrolmentId < 0) { + EnrolmentInfo enrolmentInfo = this.getActiveEnrolmentInfo(deviceId); + if (enrolmentInfo == null) { throw new OperationManagementException("Device not found for given device identifier: " + deviceId.getId() + " type: " + deviceId.getType()); } @@ -552,7 +558,8 @@ public class OperationManagerImpl implements OperationManager { try { OperationManagementDAOFactory.openConnection(); org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO. - getOperationByDeviceAndId(enrolmentId, operationId); + getOperationByDeviceAndId(enrolmentInfo.getId(), + operationId); if (dtoOperation.getType(). equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND)) { org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation commandOperation; @@ -602,14 +609,15 @@ public class OperationManagerImpl implements OperationManager { deviceId.getId() + "'"); } - int enrolmentId = this.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE); - if (enrolmentId < 0) { + EnrolmentInfo enrolmentInfo = this.getActiveEnrolmentInfo(deviceId); + if (enrolmentInfo == null) { throw new OperationManagementException( "Device not found for device id:" + deviceId.getId() + " " + "type:" + deviceId.getType()); } try { + int enrolmentId = enrolmentInfo.getId(); OperationManagementDAOFactory.openConnection(); 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()); @@ -920,6 +928,25 @@ public class OperationManagerImpl implements OperationManager { return enrolmentInfo; } + private EnrolmentInfo getActiveEnrolmentInfo(DeviceIdentifier deviceId) throws OperationManagementException { + EnrolmentInfo enrolmentInfo; + try { + DeviceManagementDAOFactory.openConnection(); + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + enrolmentInfo = deviceDAO.getActiveEnrolment(deviceId, tenantId); + } catch (DeviceManagementDAOException e) { + throw new OperationManagementException("Error occurred while retrieving enrollment data of '" + + deviceId.getType() + "' device carrying the identifier '" + + deviceId.getId() + "'", e); + } catch (SQLException e) { + throw new OperationManagementException( + "Error occurred while opening a connection to the data source", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + return enrolmentInfo; + } + private boolean setEnrolmentStatus(int enrolmentId, EnrolmentInfo.Status status) throws OperationManagementException { boolean updateStatus; try {