From 972465b61ead9dcf89e153b65238c02c6e9550f9 Mon Sep 17 00:00:00 2001 From: Rasika Perera Date: Fri, 7 Jul 2017 17:16:28 +0530 Subject: [PATCH] Fixes https://github.com/wso2/product-iots/issues/1089, https://github.com/wso2/product-iots/issues/1120 --- .../core/dao/impl/AbstractDeviceDAOImpl.java | 13 ++-- .../dao/util/DeviceManagementDAOUtil.java | 61 +------------------ .../DeviceManagementProviderServiceImpl.java | 1 + 3 files changed, 9 insertions(+), 66 deletions(-) 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 202d514b679..3bab9aceed0 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 @@ -155,7 +155,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME, " + "t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t, DM_DEVICE_DETAIL dt " + "WHERE t.NAME = ? AND t.ID = d.DEVICE_TYPE_ID AND d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ? AND dt.DEVICE_ID = d.ID " + - "AND dt.UPDATE_TIMESTAMP > ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ?" ; + "AND dt.UPDATE_TIMESTAMP > ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? ORDER BY e.DATE_OF_LAST_UPDATE DESC" ; stmt = conn.prepareStatement(sql); int paramIdx = 1; stmt.setString(paramIdx++, deviceIdentifier.getType()); @@ -190,7 +190,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME, " + "t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE " + "t.NAME = ? AND t.ID = d.DEVICE_TYPE_ID AND d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID " + - "AND TENANT_ID = ? AND e.STATUS = ?"; + "AND TENANT_ID = ? AND e.STATUS = ? ORDER BY e.DATE_OF_LAST_UPDATE DESC"; stmt = conn.prepareStatement(sql); stmt.setString(1, deviceIdentifier.getType()); stmt.setString(2, deviceIdentifier.getId()); @@ -285,7 +285,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID AS DEVICE_ID, " + "d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " + "DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?) d1 " + - "WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?"; + "WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? ORDER BY e.DATE_OF_LAST_UPDATE DESC"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); stmt.setInt(2, tenantId); @@ -317,7 +317,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, " + "d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " + "DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND t.NAME = ? " + - "AND t.ID = d.DEVICE_TYPE_ID AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ?"; + "AND t.ID = d.DEVICE_TYPE_ID AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ?" + + " ORDER BY e.DATE_OF_LAST_UPDATE DESC"; stmt = conn.prepareStatement(sql); stmt.setString(1, type); stmt.setInt(2, tenantId); @@ -350,7 +351,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { " e1.DATE_OF_ENROLMENT, d.DESCRIPTION, d.NAME AS DEVICE_NAME, d.DEVICE_IDENTIFICATION, t.NAME " + "AS DEVICE_TYPE FROM DM_DEVICE d, (SELECT e.OWNER, e.OWNERSHIP, e.ID AS ENROLMENT_ID, " + "e.DEVICE_ID, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT FROM DM_ENROLMENT e WHERE " + - "e.TENANT_ID = ? AND LOWER(e.OWNER) = LOWER(?)) e1, DM_DEVICE_TYPE t WHERE d.ID = e1.DEVICE_ID " + + "e.TENANT_ID = ? AND LOWER(e.OWNER) = LOWER(?) ORDER BY e.DATE_OF_LAST_UPDATE DESC) e1, DM_DEVICE_TYPE t WHERE d.ID = e1.DEVICE_ID " + "AND t.ID = d.DEVICE_TYPE_ID"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); @@ -383,7 +384,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { " e1.DATE_OF_ENROLMENT, d.DESCRIPTION, d.NAME AS DEVICE_NAME, d.DEVICE_IDENTIFICATION, t.NAME " + "AS DEVICE_TYPE FROM DM_DEVICE d, (SELECT e.OWNER, e.OWNERSHIP, e.ID AS ENROLMENT_ID, " + "e.DEVICE_ID, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT FROM DM_ENROLMENT e WHERE " + - "e.TENANT_ID = ? AND LOWER(e.OWNER) = LOWER(?)) e1, DM_DEVICE_TYPE t WHERE d.ID = e1.DEVICE_ID " + + "e.TENANT_ID = ? AND LOWER(e.OWNER) = LOWER(?) ORDER BY e.DATE_OF_LAST_UPDATE DESC) e1, DM_DEVICE_TYPE t WHERE d.ID = e1.DEVICE_ID " + "AND t.ID = d.DEVICE_TYPE_ID AND t.NAME= ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java index ea998fdec7b..251b489d0df 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java @@ -200,33 +200,7 @@ public final class DeviceManagementDAOUtil { if (deviceInfoIncluded) { device.setDeviceInfo(loadDeviceInfo(rs)); } - - if (EnrolmentInfo.Status.ACTIVE.equals(device.getEnrolmentInfo().getStatus())) { - return device; - } - deviceMap.put(device.getEnrolmentInfo().getStatus(), device); - while (rs.next()) { - device = loadDevice(rs); - if (deviceInfoIncluded) { - device.setDeviceInfo(loadDeviceInfo(rs)); - } - if (EnrolmentInfo.Status.ACTIVE.equals(device.getEnrolmentInfo().getStatus())) { - return device; - } - if (device.getEnrolmentInfo() != null) { - deviceMap.put(device.getEnrolmentInfo().getStatus(), device); - } - } - if (deviceMap.containsKey(EnrolmentInfo.Status.UNREACHABLE)) { - return deviceMap.get(EnrolmentInfo.Status.UNREACHABLE); - } else if (deviceMap.containsKey(EnrolmentInfo.Status.INACTIVE)) { - return deviceMap.get(EnrolmentInfo.Status.INACTIVE); - } else if (deviceMap.containsKey(EnrolmentInfo.Status.CREATED)) { - return deviceMap.get(EnrolmentInfo.Status.CREATED); - } else if (deviceMap.containsKey(EnrolmentInfo.Status.UNCLAIMED)) { - return deviceMap.get(EnrolmentInfo.Status.UNCLAIMED); - } - return null; + return device; } //This method will retrieve most appropriate device information when there are multiple device enrollments for @@ -237,39 +211,6 @@ public final class DeviceManagementDAOUtil { if (deviceInfoIncluded) { device.setDeviceInfo(loadDeviceInfo(rs)); } - - if (EnrolmentInfo.Status.ACTIVE.equals(device.getEnrolmentInfo().getStatus())) { - return device; - } - while (rs.next()) { - device = loadDevice(rs); - if (deviceInfoIncluded) { - device.setDeviceInfo(loadDeviceInfo(rs)); - } - if (EnrolmentInfo.Status.ACTIVE.equals(device.getEnrolmentInfo().getStatus())) { - return device; - } - if (device.getEnrolmentInfo() != null) { - deviceMap.put(device.getEnrolmentInfo().getStatus(), device); - } - } - if (deviceMap.containsKey(EnrolmentInfo.Status.UNREACHABLE)) { - return deviceMap.get(EnrolmentInfo.Status.UNREACHABLE); - } else if (deviceMap.containsKey(EnrolmentInfo.Status.INACTIVE)) { - return deviceMap.get(EnrolmentInfo.Status.INACTIVE); - } else if (deviceMap.containsKey(EnrolmentInfo.Status.DISENROLLMENT_REQUESTED)) { - return deviceMap.get(EnrolmentInfo.Status.DISENROLLMENT_REQUESTED); - } else if (deviceMap.containsKey(EnrolmentInfo.Status.CREATED)) { - return deviceMap.get(EnrolmentInfo.Status.CREATED); - } else if (deviceMap.containsKey(EnrolmentInfo.Status.REMOVED)) { - return deviceMap.get(EnrolmentInfo.Status.REMOVED); - } else if (deviceMap.containsKey(EnrolmentInfo.Status.UNCLAIMED)) { - return deviceMap.get(EnrolmentInfo.Status.UNCLAIMED); - } else if (deviceMap.containsKey(EnrolmentInfo.Status.SUSPENDED)) { - return deviceMap.get(EnrolmentInfo.Status.SUSPENDED); - } else if (deviceMap.containsKey(EnrolmentInfo.Status.BLOCKED)) { - return deviceMap.get(EnrolmentInfo.Status.BLOCKED); - } return device; } 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 66e326d7d7e..1e099602831 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 @@ -213,6 +213,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv enrolmentId = enrollmentDAO. addEnrollment(existingDevice.getId(), newEnrolmentInfo, tenantId); DeviceManagementDAOFactory.commitTransaction(); + this.removeDeviceFromCache(deviceIdentifier); if (log.isDebugEnabled()) { log.debug("An enrolment is successfully added with the id '" + enrolmentId + "' associated with " + "the device identified by key '" +