From fb79d61b60d833fc1072669412724355c508054a Mon Sep 17 00:00:00 2001 From: Charitha Goonetilleke Date: Tue, 7 May 2024 19:17:30 +0530 Subject: [PATCH] Add DEVICE_TYPE and DEVICE_IDENTIFICATION fields to DM_ENROLLMENT table --- .../carbon-home/dbscripts/dm-db-h2.sql | 2 + .../src/test/resources/sql/h2.sql | 2 + .../core/device/mgt/core/dao/DeviceDAO.java | 1 + .../device/mgt/core/dao/EnrollmentDAO.java | 4 +- .../core/dao/impl/AbstractDeviceDAOImpl.java | 670 ++++++++---------- .../dao/impl/AbstractEnrollmentDAOImpl.java | 77 +- .../core/dao/impl/AbstractEventConfigDAO.java | 4 - .../enrolment/GenericEnrollmentDAOImpl.java | 524 -------------- .../enrolment/SQLServerEnrollmentDAOImpl.java | 456 +----------- .../mgt/dao/impl/GenericOperationDAOImpl.java | 32 +- .../DeviceManagementProviderServiceImpl.java | 5 +- .../dao/DeviceStatusPersistenceTests.java | 4 +- .../core/dao/EnrolmentPersistenceTests.java | 4 +- .../src/test/resources/sql/h2.sql | 2 + .../src/test/resources/sql-files/h2.sql | 2 + .../policy/mgt/core/PolicyDAOTestCase.java | 3 +- .../src/test/resources/sql/CreateH2TestDB.sql | 2 + .../carbon-home/dbscripts/dm-db-h2.sql | 2 + .../src/main/resources/dbscripts/cdm/h2.sql | 2 + .../main/resources/dbscripts/cdm/mssql.sql | 2 + .../main/resources/dbscripts/cdm/mysql.sql | 14 +- .../main/resources/dbscripts/cdm/oracle.sql | 2 + .../resources/dbscripts/cdm/postgresql.sql | 2 + 23 files changed, 400 insertions(+), 1418 deletions(-) diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/dm-db-h2.sql b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/dm-db-h2.sql index 046e93de838..13ac69dfc65 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/dm-db-h2.sql +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/dm-db-h2.sql @@ -125,6 +125,8 @@ CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( ID INTEGER AUTO_INCREMENT NOT NULL, DEVICE_ID INTEGER NOT NULL, + DEVICE_TYPE VARCHAR(300) NOT NULL, + DEVICE_IDENTIFICATION VARCHAR(300) NOT NULL, OWNER VARCHAR(255) NOT NULL, OWNERSHIP VARCHAR(45) DEFAULT NULL, STATUS VARCHAR(50) NULL, diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/sql/h2.sql b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/sql/h2.sql index c079d1e214f..a842fdf2abe 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/sql/h2.sql +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/sql/h2.sql @@ -149,6 +149,8 @@ CREATE TABLE IF NOT EXISTS DM_OPERATION ( CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( ID INTEGER AUTO_INCREMENT NOT NULL, DEVICE_ID INTEGER NOT NULL, + DEVICE_TYPE VARCHAR(300) NOT NULL, + DEVICE_IDENTIFICATION VARCHAR(300) NOT NULL, OWNER VARCHAR(50) NOT NULL, OWNERSHIP VARCHAR(45) DEFAULT NULL, STATUS VARCHAR(50) NULL, diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/DeviceDAO.java index 2e7e69e358a..0fd55c870df 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/DeviceDAO.java @@ -477,6 +477,7 @@ public interface DeviceDAO { * @return returns list of device types. * @throws DeviceManagementDAOException */ + @Deprecated List getDeviceTypes() throws DeviceManagementDAOException; /** diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/EnrollmentDAO.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/EnrollmentDAO.java index 1bce2627d92..e42d5c1524a 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/EnrollmentDAO.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/EnrollmentDAO.java @@ -18,6 +18,7 @@ package io.entgra.device.mgt.core.device.mgt.core.dao; import io.entgra.device.mgt.core.device.mgt.common.Device; +import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier; import io.entgra.device.mgt.core.device.mgt.common.EnrolmentInfo; import io.entgra.device.mgt.core.device.mgt.common.EnrolmentInfo.Status; @@ -26,7 +27,8 @@ import java.util.List; public interface EnrollmentDAO { - EnrolmentInfo addEnrollment(int deviceId, EnrolmentInfo enrolmentInfo, int tenantId) throws DeviceManagementDAOException; + EnrolmentInfo addEnrollment(int deviceId, DeviceIdentifier deviceIdentifier, + EnrolmentInfo enrolmentInfo, int tenantId) throws DeviceManagementDAOException; int updateEnrollment(EnrolmentInfo enrolmentInfo, int tenantId) throws DeviceManagementDAOException; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index 8c17c204a8c..ba1cb3cf7fd 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -136,8 +136,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { + "d1.ID AS DEVICE_ID, " + "d1.DESCRIPTION, " + "d1.NAME AS DEVICE_NAME, " - + "d1.DEVICE_TYPE, " - + "d1.DEVICE_IDENTIFICATION, " + + "e.DEVICE_TYPE, " + + "e.DEVICE_IDENTIFICATION, " + "e.OWNER, " + "e.OWNERSHIP, " + "e.STATUS, " @@ -149,12 +149,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { + "(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.NAME " + + "FROM DM_DEVICE d" + "d.DEVICE_IDENTIFICATION = ? AND " + "d.TENANT_ID = ?"; @@ -212,23 +208,23 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { Device device = null; try { conn = this.getConnection(); - String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + - "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, " + - "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 = ? ORDER BY e.DATE_OF_LAST_UPDATE DESC, e.STATUS ASC"; - // Status adeed as an orderby clause to fix a bug : when an existing device is + String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, e.DEVICE_TYPE, " + + "e.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, " + + "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME " + + "FROM DM_DEVICE d WHERE " + + "d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID " + + "AND e.DEVICE_TYPE = ? AND TENANT_ID = ? ORDER BY e.DATE_OF_LAST_UPDATE DESC, e.STATUS ASC"; + // Status added as an order-by clause to fix a bug : when an existing device is // re-enrolled, earlier enrollment is marked as removed and a new enrollment is added. // However, both enrollments share the same time stamp. When retrieving the device - // due to same timestamp, enrollment information is incorrect, intermittently. Hence + // due to same timestamp, enrollment information is incorrect, intermittently. Hence, // status also should be taken into consideration when ordering. This should not present a // problem for other status transitions, as there would be an intermediary removed // state in between. stmt = conn.prepareStatement(sql); - stmt.setString(1, deviceIdentifier.getType()); - stmt.setString(2, deviceIdentifier.getId()); - stmt.setInt(3, tenantId); + stmt.setString(1, deviceIdentifier.getId()); + stmt.setInt(2, tenantId); + stmt.setString(3, deviceIdentifier.getType()); stmt.setInt(4, tenantId); rs = stmt.executeQuery(); if (rs.next()) { @@ -252,16 +248,14 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { try { conn = this.getConnection(); String sql = "SELECT " + - "d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + - "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, " + + "d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, e.DEVICE_TYPE, " + + "e.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, " + "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.ID = d.DEVICE_TYPE_ID AND d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ?) d1 " + + " (SELECT d.ID, d.DESCRIPTION, d.NAME " + + "FROM DM_DEVICE d " + + "WHERE d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ?) d1 " + "WHERE " + "d1.ID = e.DEVICE_ID " + "AND TENANT_ID = ? " + @@ -299,16 +293,17 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { Device device = null; try { conn = this.getConnection(); - String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + - "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, " + - "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 " + + String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, e.DEVICE_TYPE, " + + "e.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, " + + "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, " + + "(SELECT d.ID, d.DESCRIPTION, d.NAME " + + "FROM DM_DEVICE d WHERE d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ?) d1 " + + "WHERE e.DEVICE_TYPE = ? AND d1.ID = e.DEVICE_ID " + "AND TENANT_ID = ? AND e.OWNER = ? ORDER BY e.DATE_OF_LAST_UPDATE DESC"; stmt = conn.prepareStatement(sql); - stmt.setString(1, deviceIdentifier.getType()); - stmt.setString(2, deviceIdentifier.getId()); - stmt.setInt(3, tenantId); + stmt.setString(1, deviceIdentifier.getId()); + stmt.setInt(2, tenantId); + stmt.setString(3, deviceIdentifier.getType()); stmt.setInt(4, tenantId); stmt.setString(5, owner); rs = stmt.executeQuery(); @@ -333,18 +328,18 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { Device device = null; try { conn = this.getConnection(); - String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + - "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, " + - "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 = ? " + - "AND d.LAST_UPDATED_TIMESTAMP > ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? ORDER BY e.DATE_OF_LAST_UPDATE DESC"; + String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, e.DEVICE_TYPE, " + + "e.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, " + + "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME " + + "FROM DM_DEVICE d WHERE d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ? " + + "AND d.LAST_UPDATED_TIMESTAMP > ?) d1 WHERE e.DEVICE_TYPE = ? " + + "AND 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()); stmt.setString(paramIdx++, deviceIdentifier.getId()); stmt.setInt(paramIdx++, tenantId); stmt.setTimestamp(paramIdx++, new Timestamp(since.getTime())); + stmt.setString(paramIdx++, deviceIdentifier.getType()); stmt.setInt(paramIdx, tenantId); rs = stmt.executeQuery(); if (rs.next()) { @@ -412,7 +407,6 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { List deviceProperties = new ArrayList<>(); try { conn = this.getConnection(); - List> outputLists = new ArrayList<>(); String sql = "SELECT " + "p.DEVICE_IDENTIFICATION, " + "p.DEVICE_TYPE_NAME, " + @@ -426,7 +420,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { int iterationCount = 0; StringBuilder propertyQuery = new StringBuilder(" "); - for (Map.Entry stringStringEntry : deviceProps.entrySet()) { + for (Map.Entry ignored : deviceProps.entrySet()) { String tempTableId = "t".concat(Integer.toString(iterationCount++)); propertyQuery.append("JOIN DM_DEVICE_PROPERTIES ") .append(tempTableId).append(" ").append("ON p.DEVICE_IDENTIFICATION = ") @@ -526,17 +520,16 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { Device device = null; try { conn = this.getConnection(); - String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + - "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, " + + String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, e.DEVICE_TYPE, " + + "e.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, " + "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 " + + "(SELECT d.ID, d.DESCRIPTION, d.NAME " + "FROM" + - " DM_DEVICE d, DM_DEVICE_TYPE t " + + " DM_DEVICE d " + "WHERE " + - "t.ID = d.DEVICE_TYPE_ID AND d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ? AND" + + "d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ? AND" + " d.LAST_UPDATED_TIMESTAMP > ?) d1 " + "WHERE" + " d1.ID = e.DEVICE_ID AND TENANT_ID = ? " + @@ -570,20 +563,20 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { Device device = null; try { conn = this.getConnection(); - String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + - "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, " + - "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 = ? " + + String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, e.DEVICE_TYPE, " + + "e.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, " + + "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME " + + "FROM DM_DEVICE d WHERE " + + "d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ? " + "AND d.LAST_UPDATED_TIMESTAMP > ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? AND e.OWNER = ? " + - "ORDER BY e.DATE_OF_LAST_UPDATE DESC"; + "AND e.DEVICE_TYPE = ? ORDER BY e.DATE_OF_LAST_UPDATE DESC"; stmt = conn.prepareStatement(sql); - stmt.setString(1, deviceIdentifier.getType()); - stmt.setString(2, deviceIdentifier.getId()); - stmt.setInt(3, tenantId); - stmt.setTimestamp(4, new Timestamp(since.getTime())); - stmt.setInt(5, tenantId); - stmt.setString(6, owner); + stmt.setString(1, deviceIdentifier.getId()); + stmt.setInt(2, tenantId); + stmt.setTimestamp(3, new Timestamp(since.getTime())); + stmt.setInt(4, tenantId); + stmt.setString(5, owner); + stmt.setString(6, deviceIdentifier.getType()); rs = stmt.executeQuery(); if (rs.next()) { device = DeviceManagementDAOUtil.loadMatchingDevice(rs, false); @@ -606,16 +599,16 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { Device device = null; try { conn = this.getConnection(); - String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + - "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, " + - "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 " + + String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, e.DEVICE_TYPE, " + + "e.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, " + + "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME " + + "FROM DM_DEVICE d WHERE " + + "d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND e.DEVICE_TYPE = ? AND e.DEVICE_IDENTIFICATION = ? " + "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()); - stmt.setInt(3, tenantId); + stmt.setInt(1, tenantId); + stmt.setString(2, deviceIdentifier.getType()); + stmt.setString(3, deviceIdentifier.getId()); stmt.setInt(4, tenantId); stmt.setString(5, status.toString()); rs = stmt.executeQuery(); @@ -639,7 +632,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { String sql = "SELECT d1.ID AS DEVICE_ID, " + "d1.DESCRIPTION, " + "d1.NAME AS DEVICE_NAME, " + - "d1.DEVICE_TYPE, " + + "e.DEVICE_TYPE, " + "e.TENANT_ID, " + "d1.DEVICE_IDENTIFICATION, " + "e.OWNER, " + @@ -653,19 +646,15 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { "(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 = ?) d1 " + - "WHERE d1.ID = e.DEVICE_ID " + + "FROM DM_DEVICE d " + + "WHERE d.DEVICE_IDENTIFICATION = ?) d1 " + + "WHERE d1.ID = e.DEVICE_ID AND e.DEVICE_TYPE = ? " + "ORDER BY e.DATE_OF_LAST_UPDATE DESC"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { - stmt.setString(1, deviceIdentifier.getType()); - stmt.setString(2, deviceIdentifier.getId()); + stmt.setString(1, deviceIdentifier.getId()); + stmt.setString(2, deviceIdentifier.getType()); try (ResultSet rs = stmt.executeQuery()) { SingletonMap deviceMap = null; @@ -695,11 +684,10 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { Device device = null; try { conn = this.getConnection(); - String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + - "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, " + - "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 " + - "d.ID = ? AND t.ID = d.DEVICE_TYPE_ID AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID " + + String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, e.DEVICE_TYPE, " + + "e.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, " + + "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME " + + "FROM DM_DEVICE d WHERE d.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.setInt(1, deviceId); @@ -726,11 +714,10 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { List devices; try { conn = this.getConnection(); - String sql = "SELECT d1.DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + - "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, " + + String sql = "SELECT d1.DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, e.DEVICE_TYPE, " + + "e.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, " + "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 " + + "d.DESCRIPTION, d.NAME FROM DM_DEVICE d WHERE d.TENANT_ID = ?) d1 " + "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); @@ -758,24 +745,20 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { List devices = null; try { conn = this.getConnection(); - String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + - "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, " + + String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, e.DEVICE_TYPE, " + + "e.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, " + "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 = ?" + - " ORDER BY e.DATE_OF_LAST_UPDATE DESC"; + "d.NAME FROM DM_DEVICE d WHERE d.TENANT_ID = ?) d1 WHERE e.DEVICE_TYPE = ? AND 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); + stmt.setInt(1, tenantId); + stmt.setString(2, type); stmt.setInt(3, tenantId); rs = stmt.executeQuery(); devices = new ArrayList<>(); while (rs.next()) { Device device = DeviceManagementDAOUtil.loadActiveDevice(rs, false); - if (device != null) { - devices.add(device); - } + devices.add(device); } } catch (SQLException e) { String msg = "Error occurred while listing devices for type '" + type + "'"; @@ -791,40 +774,36 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { @Override public List getAllocatedDevices(String type, int tenantId, int activeServerCount, int serverIndex) throws DeviceManagementDAOException { - List devices = null; + List devices; try { Connection conn = this.getConnection(); String sql = "SELECT d1.ID AS DEVICE_ID," + - " d1.DESCRIPTION," + - " d1.NAME AS DEVICE_NAME," + - " d1.DEVICE_TYPE," + - " d1.DEVICE_IDENTIFICATION," + - " e.OWNER," + - " e.OWNERSHIP," + - " e.STATUS," + - " e.IS_TRANSFERRED," + - " e.DATE_OF_LAST_UPDATE," + - " 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 MOD(d1.ID, ?) = ? " + - "ORDER BY e.DATE_OF_LAST_UPDATE DESC"; + " d1.DESCRIPTION," + + " d1.NAME AS DEVICE_NAME," + + " e.DEVICE_TYPE," + + " e.DEVICE_IDENTIFICATION," + + " e.OWNER," + + " e.OWNERSHIP," + + " e.STATUS," + + " e.IS_TRANSFERRED," + + " e.DATE_OF_LAST_UPDATE," + + " e.DATE_OF_ENROLMENT," + + " e.ID AS ENROLMENT_ID " + + "FROM DM_ENROLMENT e," + + " (SELECT d.ID," + + " d.DESCRIPTION," + + " d.NAME" + + " FROM DM_DEVICE d" + + " WHERE d.TENANT_ID = ?) d1 " + + "WHERE d1.ID = e.DEVICE_ID" + + " AND e.DEVICE_TYPE = ?" + + " AND e.TENANT_ID = ?" + + " AND MOD(d1.ID, ?) = ? " + + "ORDER BY e.DATE_OF_LAST_UPDATE DESC"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { - stmt.setString(1, type); - stmt.setInt(2, tenantId); + stmt.setInt(1, tenantId); + stmt.setString(2, type); stmt.setInt(3, tenantId); stmt.setInt(4, activeServerCount); stmt.setInt(5, serverIndex); @@ -833,20 +812,18 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { try (ResultSet rs = stmt.executeQuery()) { while (rs.next()) { Device device = DeviceManagementDAOUtil.loadActiveDevice(rs, false); - if (device != null) { - devices.add(device); - } + devices.add(device); } } catch (Exception e) { String msg = "Error encountered while populating allocated active devices for server with index : " + serverIndex + - " active-server-count " + activeServerCount + " device-type " + type + " tenant-id " + tenantId; + " active-server-count " + activeServerCount + " device-type " + type + " tenant-id " + tenantId; log.error(msg, e); throw new DeviceManagementDAOException(msg, e); } } } catch (SQLException e) { String msg = "Error encountered while retrieving allocated devices for server with index : " + serverIndex + - " active-server-count " + activeServerCount + " device-type " + type + " tenant-id " + tenantId; + " active-server-count " + activeServerCount + " device-type " + type + " tenant-id " + tenantId; log.error(msg, e); throw new DeviceManagementDAOException(msg, e); } @@ -861,12 +838,13 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { List devices = new ArrayList<>(); try { conn = this.getConnection(); - String sql = "SELECT e1.OWNER, e1.OWNERSHIP, e1.ENROLMENT_ID, e1.DEVICE_ID, e1.STATUS, e1.IS_TRANSFERRED, e1.DATE_OF_LAST_UPDATE," + - " 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.IS_TRANSFERRED, 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 " + - "AND t.ID = d.DEVICE_TYPE_ID ORDER BY e1.DATE_OF_LAST_UPDATE DESC"; + String sql = "SELECT e1.OWNER, e1.OWNERSHIP, e1.ENROLMENT_ID, e1.DEVICE_ID, e1.STATUS, e1.IS_TRANSFERRED, " + + "e1.DATE_OF_LAST_UPDATE, e1.DATE_OF_ENROLMENT, d.DESCRIPTION, d.NAME AS DEVICE_NAME, " + + "d.DEVICE_IDENTIFICATION, e1.DEVICE_TYPE FROM DM_DEVICE d, " + + "(SELECT e.OWNER, e.OWNERSHIP, e.ID AS ENROLMENT_ID, e.DEVICE_TYPE, " + + "e.DEVICE_ID, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT " + + "FROM DM_ENROLMENT e WHERE e.TENANT_ID = ? AND LOWER(e.OWNER) = LOWER(?)) e1 " + + "WHERE d.ID = e1.DEVICE_ID ORDER BY e1.DATE_OF_LAST_UPDATE DESC"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); stmt.setString(2, username); @@ -894,12 +872,13 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { List devices = new ArrayList<>(); try { conn = this.getConnection(); - String sql = "SELECT e1.OWNER, e1.OWNERSHIP, e1.ENROLMENT_ID, e1.DEVICE_ID, e1.STATUS, e1.IS_TRANSFERRED, e1.DATE_OF_LAST_UPDATE," + - " 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.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT FROM DM_ENROLMENT e WHERE " + - "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= ?"; + String sql = "SELECT e1.OWNER, e1.OWNERSHIP, e1.ENROLMENT_ID, e1.DEVICE_ID, e1.STATUS, e1.IS_TRANSFERRED, " + + "e1.DATE_OF_LAST_UPDATE, e1.DATE_OF_ENROLMENT, d.DESCRIPTION, d.NAME AS DEVICE_NAME, " + + "d.DEVICE_IDENTIFICATION, e1.DEVICE_TYPE FROM DM_DEVICE d, " + + "(SELECT e.OWNER, e.DEVICE_TYPE, e.OWNERSHIP, e.ID AS ENROLMENT_ID, e.DEVICE_ID, e.STATUS, " + + "e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT FROM DM_ENROLMENT e WHERE " + + "e.TENANT_ID = ? AND LOWER(e.OWNER) = LOWER(?) ORDER BY e.DATE_OF_LAST_UPDATE DESC) e1 " + + "WHERE d.ID = e1.DEVICE_ID AND e1.DEVICE_TYPE = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); stmt.setString(2, username); @@ -928,7 +907,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { return devices; } Connection conn = this.getConnection(); - StringJoiner joiner = new StringJoiner(",","SELECT " + StringJoiner joiner = new StringJoiner(",", "SELECT " + "e1.OWNER, " + "e1.OWNERSHIP, " + "e1.ENROLMENT_ID, " @@ -940,40 +919,37 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { + "d.DESCRIPTION, " + "d.NAME AS DEVICE_NAME, " + "d.DEVICE_IDENTIFICATION, " - + "t.NAME AS DEVICE_TYPE " + + "e.DEVICE_TYPE " + "FROM " + "DM_DEVICE d, " + "(SELECT " - + "e.OWNER, " - + "e.OWNERSHIP, " - + "e.ID AS ENROLMENT_ID, " - + "e.DEVICE_ID, " - + "e.STATUS, " - + "e.IS_TRANSFERRED, " - + "e.DATE_OF_LAST_UPDATE, " - + "e.DATE_OF_ENROLMENT " - + "FROM " - + "DM_ENROLMENT e " - + "WHERE " - + "e.TENANT_ID = ? AND " - + "LOWER(e.OWNER) = LOWER(?) AND " - + "e.STATUS IN (", - ")) e1, " - + "DM_DEVICE_TYPE t " - + "WHERE d.ID = e1.DEVICE_ID AND " - + "t.ID = d.DEVICE_TYPE_ID " - + "ORDER BY e1.DATE_OF_LAST_UPDATE DESC"); + + "e.OWNER, " + + "e.DEVICE_TYPE " + + "e.OWNERSHIP, " + + "e.ID AS ENROLMENT_ID, " + + "e.DEVICE_ID, " + + "e.STATUS, " + + "e.IS_TRANSFERRED, " + + "e.DATE_OF_LAST_UPDATE, " + + "e.DATE_OF_ENROLMENT " + + "FROM " + + "DM_ENROLMENT e " + + "WHERE " + + "e.TENANT_ID = ? AND " + + "LOWER(e.OWNER) = LOWER(?) AND " + + "e.STATUS IN (", + ")) e1 ORDER BY e1.DATE_OF_LAST_UPDATE DESC"); deviceStatuses.stream().map(ignored -> "?").forEach(joiner::add); - String query = joiner.toString(); + String query = joiner.toString(); try (PreparedStatement stmt = conn.prepareStatement(query)) { int index = 1; stmt.setInt(index++, tenantId); stmt.setString(index++, username); - for (String deviceId : deviceStatuses) { - stmt.setObject(index++, deviceId); - } + for (String status : deviceStatuses) { + stmt.setObject(index++, status); + } try (ResultSet rs = stmt.executeQuery()) { while (rs.next()) { Device device = DeviceManagementDAOUtil.loadDevice(rs); @@ -1017,13 +993,11 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { "gd.DESCRIPTION, " + "gd.NAME, " + "gd.DEVICE_IDENTIFICATION, " + - "t.NAME AS DEVICE_TYPE " + "FROM " + "(SELECT d.ID AS DEVICE_ID, " + "d.DESCRIPTION, " + "d.NAME, " + "d.DEVICE_IDENTIFICATION, " + - "d.DEVICE_TYPE_ID " + "FROM DM_DEVICE d, " + "(SELECT dgm.DEVICE_ID " + "FROM DM_DEVICE_GROUP_MAP dgm " + @@ -1035,18 +1009,18 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { sql = sql + " AND d.NAME LIKE ?"; isDeviceNameProvided = true; } - sql = sql + ") gd, DM_DEVICE_TYPE t WHERE gd.DEVICE_TYPE_ID = t.ID"; + sql = sql + ") gd WHERE 1=1"; //Add query for last updated timestamp if (since != null) { sql = sql + " AND d.LAST_UPDATED_TIMESTAMP > ?"; isSinceProvided = true; } + sql = sql + " ) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? "; //Add the query for device-type if (deviceType != null && !deviceType.isEmpty()) { - sql = sql + " AND t.NAME = ?"; + sql = sql + " AND e.DEVICE_TYPE = ?"; isDeviceTypeProvided = true; } - sql = sql + " ) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? "; //Add the query for ownership if (ownership != null && !ownership.isEmpty()) { sql = sql + " AND e.OWNERSHIP = ?"; @@ -1075,10 +1049,10 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { if (isSinceProvided) { stmt.setTimestamp(paramIdx++, new Timestamp(since.getTime())); } + stmt.setInt(paramIdx++, tenantId); if (isDeviceTypeProvided) { stmt.setString(paramIdx++, deviceType); } - stmt.setInt(paramIdx++, tenantId); if (isOwnershipProvided) { stmt.setString(paramIdx++, ownership); } @@ -1116,7 +1090,6 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { * Get device count of user. * * @return device count - * @throws DeviceManagementDAOException */ @Override public int getDeviceCount(String username, int tenantId) throws DeviceManagementDAOException { @@ -1126,13 +1099,11 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { int deviceCount = 0; try { conn = this.getConnection(); - String sql = "SELECT COUNT(d1.DEVICE_ID) AS DEVICE_COUNT FROM DM_ENROLMENT e, (SELECT d.ID AS DEVICE_ID 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 LOWER(e.OWNER) = LOWER(?) AND TENANT_ID = ?"; + String sql = "SELECT COUNT(ID) AS DEVICE_COUNT FROM DM_ENROLMENT WHERE " + + "LOWER(OWNER) = LOWER(?) AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); - stmt.setInt(1, tenantId); - stmt.setString(2, username); - stmt.setInt(3, tenantId); + stmt.setString(1, username); + stmt.setInt(2, tenantId); rs = stmt.executeQuery(); if (rs.next()) { deviceCount = rs.getInt("DEVICE_COUNT"); @@ -1149,7 +1120,6 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { * Get device count of user. * * @return device count - * @throws DeviceManagementDAOException */ @Override public int getDeviceCount(String type, String status, int tenantId) throws DeviceManagementDAOException { @@ -1159,14 +1129,12 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { int deviceCount = 0; try { conn = this.getConnection(); - String sql = "SELECT COUNT(d.ID) AS DEVICE_COUNT FROM (SELECT e.DEVICE_ID FROM DM_ENROLMENT e WHERE " + - "TENANT_ID = ? AND STATUS = ?) e, DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.ID = e.DEVICE_ID AND " + - "d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ? AND t.NAME=?"; + String sql = "SELECT COUNT(ID) AS DEVICE_COUNT FROM DM_ENROLMENT WHERE " + + "DEVICE_TYPE = ? AND STATUS = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); - stmt.setInt(1, tenantId); + stmt.setString(1, type); stmt.setString(2, status); stmt.setInt(3, tenantId); - stmt.setString(4, type); rs = stmt.executeQuery(); if (rs.next()) { deviceCount = rs.getInt("DEVICE_COUNT"); @@ -1187,17 +1155,15 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { List deviceIDs = new ArrayList<>(); try { conn = this.getConnection(); - String sql = "SELECT d.DEVICE_IDENTIFICATION AS DEVICE_IDS FROM (SELECT e.DEVICE_ID FROM DM_ENROLMENT e WHERE " + - "TENANT_ID = ? AND STATUS = ?) e, DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.ID = e.DEVICE_ID AND " + - "d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ? AND t.NAME=?"; + String sql = "SELECT DEVICE_IDENTIFICATION FROM DM_ENROLMENT WHERE " + + "DEVICE_TYPE = ? AND STATUS = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); - stmt.setInt(1, tenantId); + stmt.setString(1, type); stmt.setString(2, status); stmt.setInt(3, tenantId); - stmt.setString(4, type); rs = stmt.executeQuery(); while (rs.next()) { - deviceIDs.add(rs.getString("DEVICE_IDS")); + deviceIDs.add(rs.getString("DEVICE_IDENTIFICATION")); } } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while retrieving tenants which have " + @@ -1218,13 +1184,13 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { return false; } conn = this.getConnection(); - StringBuilder sql = new StringBuilder("UPDATE DM_ENROLMENT SET STATUS = ? WHERE DEVICE_ID IN " + - "(SELECT d.ID FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.DEVICE_IDENTIFICATION IN ("); + StringBuilder sql = new StringBuilder("UPDATE DM_ENROLMENT SET STATUS = ? " + + "WHERE DEVICE_IDENTIFICATION IN ("); for (int i = 0; i < devices.size(); i++) { sql.append("?,"); } sql.deleteCharAt(sql.length() - 1); - sql.append(") AND t.NAME = ? AND d.TENANT_ID = ?) AND TENANT_ID = ?"); + sql.append(") AND DEVICE_TYPE = ? AND TENANT_ID = ?"); stmt = conn.prepareStatement(sql.toString()); stmt.setString(1, status); int index = 1; @@ -1233,7 +1199,6 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } stmt.setString(++index, deviceType); stmt.setInt(++index, tenantId); - stmt.setInt(++index, tenantId); stmt.executeUpdate(); } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while updating enrollment status in bulk", e); @@ -1247,7 +1212,6 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { * Get device count of all devices. * * @return device count - * @throws DeviceManagementDAOException */ @Override public int getDeviceCount(int tenantId) throws DeviceManagementDAOException { @@ -1257,12 +1221,10 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { int deviceCount = 0; try { conn = this.getConnection(); - String sql = "SELECT COUNT(d1.DEVICE_ID) AS DEVICE_COUNT FROM DM_ENROLMENT e, (SELECT d.ID AS DEVICE_ID 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 = ?"; + String sql = "SELECT COUNT(ID) AS DEVICE_COUNT FROM DM_ENROLMENT WHERE " + + "TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); - stmt.setInt(2, tenantId); rs = stmt.executeQuery(); if (rs.next()) { deviceCount = rs.getInt("DEVICE_COUNT"); @@ -1300,25 +1262,24 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { "(SELECT " + "d.ID, " + "d.NAME, " + - "d.DEVICE_IDENTIFICATION, " + - "t.NAME AS DEVICE_TYPE " + + "d.DEVICE_IDENTIFICATION " + "FROM " + - "DM_DEVICE d, " + - "DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?"; + "DM_DEVICE d " + + "WHERE 1=1 "; //Add query for last updated timestamp if (since != null) { sql = sql + " AND d.LAST_UPDATED_TIMESTAMP > ?"; isSinceProvided = true; } - if (deviceType != null && !deviceType.isEmpty()) { - sql = sql + " AND t.NAME = ?"; - isDeviceTypeProvided = true; - } if (deviceName != null && !deviceName.isEmpty()) { sql = sql + " AND d.NAME LIKE ?"; isDeviceNameProvided = true; } sql = sql + ") d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ?"; + if (deviceType != null && !deviceType.isEmpty()) { + sql = sql + " AND e.DEVICE_TYPE = ?"; + isDeviceTypeProvided = true; + } if (ownership != null && !ownership.isEmpty()) { sql = sql + " AND e.OWNERSHIP = ?"; isOwnershipProvided = true; @@ -1338,17 +1299,16 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { try (PreparedStatement stmt = conn.prepareStatement(sql)) { int paramIdx = 1; - stmt.setInt(paramIdx++, tenantId); if (isSinceProvided) { stmt.setTimestamp(paramIdx++, new Timestamp(since.getTime())); } - if (isDeviceTypeProvided) { - stmt.setString(paramIdx++, request.getDeviceType()); - } if (isDeviceNameProvided) { stmt.setString(paramIdx++, "%" + request.getDeviceName() + "%"); } stmt.setInt(paramIdx++, tenantId); + if (isDeviceTypeProvided) { + stmt.setString(paramIdx++, request.getDeviceType()); + } if (isOwnershipProvided) { stmt.setString(paramIdx++, request.getOwnership()); } @@ -1384,13 +1344,10 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { int deviceCount = 0; try { conn = this.getConnection(); - String sql = "SELECT COUNT(d1.ID) AS DEVICE_COUNT FROM DM_ENROLMENT e, (SELECT d.ID FROM DM_DEVICE d, " + - "DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND t.NAME = ? " + - "AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ?"; + String sql = "SELECT COUNT(ID) AS DEVICE_COUNT FROM DM_ENROLMENT WHERE DEVICE_TYPE = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, type); stmt.setInt(2, tenantId); - stmt.setInt(3, tenantId); rs = stmt.executeQuery(); if (rs.next()) { deviceCount = rs.getInt("DEVICE_COUNT"); @@ -1410,12 +1367,11 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { int deviceCount = 0; try { conn = this.getConnection(); - String sql = "SELECT COUNT(e1.DEVICE_ID) AS DEVICE_COUNT FROM DM_DEVICE d, (SELECT e.DEVICE_ID " + - "FROM DM_ENROLMENT e WHERE e.TENANT_ID = ? AND LOWER(e.OWNER) = LOWER(?)) " + - "e1, DM_DEVICE_TYPE t WHERE d.ID = e1.DEVICE_ID AND t.ID = d.DEVICE_TYPE_ID"; + String sql = "SELECT COUNT(ID) AS DEVICE_COUNT FROM DM_ENROLMENT WHERE LOWER(OWNER) = LOWER(?)" + + " AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); - stmt.setInt(1, tenantId); - stmt.setString(2, username); + stmt.setString(1, username); + stmt.setInt(2, tenantId); ResultSet rs = stmt.executeQuery(); if (rs.next()) { @@ -1437,8 +1393,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { int deviceCount = 0; try { conn = this.getConnection(); - String sql = "SELECT COUNT(d1.ID) AS DEVICE_COUNT FROM DM_ENROLMENT e, (SELECT d.ID FROM DM_DEVICE d, " + - "DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.NAME LIKE ? AND d.TENANT_ID = ?) d1 " + + String sql = "SELECT COUNT(d1.ID) AS DEVICE_COUNT FROM DM_ENROLMENT e, (SELECT d.ID FROM DM_DEVICE d " + + "WHERE d.NAME LIKE ? AND d.TENANT_ID = ?) d1 " + "WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, deviceName + "%"); @@ -1465,13 +1421,11 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { int deviceCount = 0; try { conn = this.getConnection(); - String sql = "SELECT COUNT(d.ID) AS DEVICE_COUNT FROM (SELECT e.DEVICE_ID FROM DM_ENROLMENT e WHERE " + - "TENANT_ID = ? AND OWNERSHIP = ?) e, DM_DEVICE d, " + - "DM_DEVICE_TYPE t WHERE d.ID = e.DEVICE_ID AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?"; + String sql = "SELECT COUNT(ID) AS DEVICE_COUNT FROM DM_ENROLMENT WHERE " + + "TENANT_ID = ? AND OWNERSHIP = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); stmt.setString(2, ownerShip); - stmt.setInt(3, tenantId); ResultSet rs = stmt.executeQuery(); if (rs.next()) { @@ -1493,13 +1447,11 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { int deviceCount = 0; try { conn = this.getConnection(); - String sql = "SELECT COUNT(d.ID) AS DEVICE_COUNT FROM (SELECT e.DEVICE_ID FROM DM_ENROLMENT e WHERE " + - "TENANT_ID = ? AND STATUS = ?) e, DM_DEVICE d, " + - "DM_DEVICE_TYPE t WHERE d.ID = e.DEVICE_ID AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?"; + String sql = "SELECT COUNT(ID) AS DEVICE_COUNT FROM DM_ENROLMENT WHERE " + + "TENANT_ID = ? AND STATUS = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); stmt.setString(2, status); - stmt.setInt(3, tenantId); ResultSet rs = stmt.executeQuery(); if (rs.next()) { @@ -1521,14 +1473,12 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { int deviceCount = 0; try { conn = this.getConnection(); - String sql = "SELECT COUNT(d.ID) AS DEVICE_COUNT FROM (SELECT e.DEVICE_ID FROM DM_ENROLMENT e WHERE " + - "TENANT_ID = ? AND STATUS = ?) e, DM_DEVICE d, " + - "DM_DEVICE_TYPE t WHERE t.NAME = ? AND d.ID = e.DEVICE_ID AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?"; + String sql = "SELECT COUNT(ID) AS DEVICE_COUNT FROM DM_ENROLMENT WHERE " + + "TENANT_ID = ? AND STATUS = ? AND DEVICE_TYPE = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); stmt.setString(2, status); stmt.setString(3, deviceType); - stmt.setInt(4, tenantId); ResultSet rs = stmt.executeQuery(); if (rs.next()) { @@ -1551,17 +1501,20 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { int enrolmentId = -1; try { conn = this.getConnection(); - String sql = "INSERT INTO DM_ENROLMENT(DEVICE_ID, OWNER, OWNERSHIP, STATUS, IS_TRANSFERRED, " + - "DATE_OF_ENROLMENT, DATE_OF_LAST_UPDATE, TENANT_ID) VALUES(?, ?, ?, ?, ?, ?, ?, ?)"; + String sql = "INSERT INTO DM_ENROLMENT(DEVICE_ID, DEVICE_TYPE, DEVICE_IDENTIFICATION, OWNER, OWNERSHIP, " + + "STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, DATE_OF_LAST_UPDATE, TENANT_ID) " + + "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; stmt = conn.prepareStatement(sql, new String[]{"id"}); stmt.setInt(1, device.getId()); - stmt.setString(2, device.getEnrolmentInfo().getOwner()); - stmt.setString(3, device.getEnrolmentInfo().getOwnership().toString()); - stmt.setString(4, device.getEnrolmentInfo().getStatus().toString()); - stmt.setBoolean(5, device.getEnrolmentInfo().isTransferred()); - stmt.setTimestamp(6, new Timestamp(new Date().getTime())); - stmt.setTimestamp(7, new Timestamp(new Date().getTime())); - stmt.setInt(8, tenantId); + stmt.setString(2, device.getType()); + stmt.setString(3, device.getDeviceIdentifier()); + stmt.setString(4, device.getEnrolmentInfo().getOwner()); + stmt.setString(5, device.getEnrolmentInfo().getOwnership().toString()); + stmt.setString(6, device.getEnrolmentInfo().getStatus().toString()); + stmt.setBoolean(7, device.getEnrolmentInfo().isTransferred()); + stmt.setTimestamp(8, new Timestamp(new Date().getTime())); + stmt.setTimestamp(9, new Timestamp(new Date().getTime())); + stmt.setInt(10, tenantId); stmt.execute(); rs = stmt.getGeneratedKeys(); @@ -1583,16 +1536,14 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { PreparedStatement stmt = null; try { conn = this.getConnection(); - String sql = "UPDATE DM_ENROLMENT SET STATUS = ? 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 OWNER = ? AND TENANT_ID = ?"; + String sql = "UPDATE DM_ENROLMENT SET STATUS = ? WHERE DEVICE_IDENTIFICATION = ? " + + "AND DEVICE_TYPE = ? AND OWNER = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, status.toString()); stmt.setString(2, deviceId.getId()); stmt.setString(3, deviceId.getType()); - stmt.setInt(4, tenantId); - stmt.setString(5, currentOwner); - stmt.setInt(6, tenantId); + stmt.setString(4, currentOwner); + stmt.setInt(5, tenantId); stmt.executeUpdate(); } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment", e); @@ -1611,15 +1562,13 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { Status status = null; try { conn = this.getConnection(); - String sql = "SELECT STATUS 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 OWNER = ? AND TENANT_ID = ?"; + String sql = "SELECT STATUS FROM DM_ENROLMENT WHERE DEVICE_IDENTIFICATION = ? " + + "AND DEVICE_TYPE = ? AND OWNER = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, deviceId.getId()); stmt.setString(2, deviceId.getType()); - stmt.setInt(3, tenantId); - stmt.setString(4, currentOwner); - stmt.setInt(5, tenantId); + stmt.setString(3, currentOwner); + stmt.setInt(4, tenantId); rs = stmt.executeQuery(); if (rs.next()) { @@ -1651,9 +1600,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { + "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 = ?) " + + "DEVICE_IDENTIFICATION = ? AND DEVICE_TYPE = ? " + "AND OWNER = ? "; if (!StringUtils.isBlank(request.getOwnership())) { @@ -1664,7 +1611,6 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setString(paramIdx++, deviceId.getId()); stmt.setString(paramIdx++, deviceId.getType()); - stmt.setInt(paramIdx++, tenantId); stmt.setString(paramIdx++, request.getOwner()); if (!StringUtils.isBlank(request.getOwnership())) { stmt.setString(paramIdx++, request.getOwnership()); @@ -1686,23 +1632,21 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } @Override - public EnrolmentInfo getActiveEnrolment(DeviceIdentifier deviceId, int tenantId) throws DeviceManagementDAOException { + 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, IS_TRANSFERRED, 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 != 'REMOVED'"; + String sql = "SELECT ID AS ENROLMENT_ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, IS_TRANSFERRED, " + + "DATE_OF_ENROLMENT, DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT " + + "WHERE DEVICE_IDENTIFICATION = ? AND DEVICE_TYPE = ? AND TENANT_ID = ? AND STATUS != 'REMOVED'"; stmt = conn.prepareStatement(sql); stmt.setString(1, deviceId.getId()); stmt.setString(2, deviceId.getType()); stmt.setInt(3, tenantId); - stmt.setInt(4, tenantId); rs = stmt.executeQuery(); if (rs.next()) { enrolmentInfo = DeviceManagementDAOUtil.loadEnrolment(rs); @@ -1725,12 +1669,12 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { List devices = new ArrayList<>(); try { conn = this.getConnection(); - String sql = "SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, t.NAME AS DEVICE_TYPE, " + - "d.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, " + - "e.DATE_OF_ENROLMENT, e.ENROLMENT_ID FROM (SELECT e.ID, e.DEVICE_ID, e.OWNER, e" + - ".OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_ENROLMENT, e.DATE_OF_LAST_UPDATE, e.ID AS " + - "ENROLMENT_ID FROM DM_ENROLMENT e WHERE TENANT_ID = ? AND STATUS = ?) e, DM_DEVICE d, DM_DEVICE_TYPE t " + - "WHERE DEVICE_ID = e.DEVICE_ID AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?"; + String sql = "SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, e.DEVICE_TYPE, " + + "e.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, " + + "e.DATE_OF_ENROLMENT, e.ENROLMENT_ID FROM (SELECT e.ID, e.DEVICE_ID, e.DEVICE_TYPE, e.OWNER, " + + "e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_ENROLMENT, e.DATE_OF_LAST_UPDATE, e.ID AS " + + "ENROLMENT_ID FROM DM_ENROLMENT e WHERE TENANT_ID = ? AND STATUS = ?) e, " + + "DM_DEVICE d WHERE d.ID = e.DEVICE_ID AND d.TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); stmt.setString(2, status.toString()); @@ -1751,6 +1695,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } @Override + @Deprecated public List getDeviceTypes() throws DeviceManagementDAOException { Connection conn; @@ -1779,9 +1724,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { * Returns the collection of devices that has been updated after the time given in the timestamp passed in. * * @param timestamp Timestamp in long, after which the devices have been updated. - * @param tenantId Tenant id of the currently logged in user. + * @param tenantId Tenant id of the currently logged-in user. * @return A collection of devices that have been updated after the provided timestamp - * @throws DeviceManagementDAOException */ public List getDevices(long timestamp, int tenantId) throws DeviceManagementDAOException { Connection conn; @@ -1790,11 +1734,11 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { List devices = new ArrayList<>(); try { conn = this.getConnection(); - String sql = "SELECT d1.DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + + String sql = "SELECT d1.DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, e.DEVICE_TYPE, " + "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, " + "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, d.LAST_UPDATED_TIMESTAMP FROM DM_DEVICE d, " + - "DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ? AND d.LAST_UPDATED_TIMESTAMP < CURRENT_TIMESTAMP) d1 " + + "d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, d.LAST_UPDATED_TIMESTAMP FROM DM_DEVICE d " + + "WHERE d.TENANT_ID = ? AND d.LAST_UPDATED_TIMESTAMP < CURRENT_TIMESTAMP) d1 " + "WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); @@ -1854,7 +1798,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { "MIN(DEVICE.ID) AS DEVICE_ID, " + "MIN(DEVICE.NAME) AS DEVICE_NAME, " + "MIN(DEVICE.DESCRIPTION) AS DESCRIPTION, " + - "MIN(DEVICE_TYPE.NAME) AS DEVICE_TYPE, " + + "MIN(ENROLMENT.DEVICE_TYPE) AS DEVICE_TYPE, " + "MIN(DEVICE.DEVICE_IDENTIFICATION) AS DEVICE_IDENTIFICATION, " + "MIN(ENROLMENT.ID) AS ENROLMENT_ID, " + "MIN(ENROLMENT.OWNER) AS OWNER, " + @@ -1864,11 +1808,11 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { "MIN(ENROLMENT.DATE_OF_LAST_UPDATE) AS DATE_OF_LAST_UPDATE, " + "MIN(ENROLMENT.STATUS) AS STATUS " + "FROM DM_DEVICE_LOCATION AS DEVICE_LOCATION, DM_DEVICE AS DEVICE, " + - "DM_DEVICE_TYPE AS DEVICE_TYPE, DM_ENROLMENT AS ENROLMENT " + + "DM_ENROLMENT AS ENROLMENT " + "WHERE DEVICE_LOCATION.LATITUDE BETWEEN ? AND ? " + "AND DEVICE_LOCATION.LONGITUDE BETWEEN ? AND ? "; if (geoQuery.getDeviceTypes() != null && !geoQuery.getDeviceTypes().isEmpty()) { - sql += "AND DEVICE_TYPE.NAME IN ("; + sql += "AND ENROLMENT.DEVICE_TYPE IN ("; sql += String.join(", ", Collections.nCopies(geoQuery.getDeviceTypes().size(), "?")); sql += ") "; @@ -1905,7 +1849,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { if (geoQuery.getUpdatedBefore() != 0 || geoQuery.getUpdatedAfter() != 0) { sql += "AND ENROLMENT.DATE_OF_LAST_UPDATE BETWEEN ? AND ? "; } - sql += "AND DEVICE.ID = DEVICE_LOCATION.DEVICE_ID AND DEVICE.DEVICE_TYPE_ID = DEVICE_TYPE.ID " + + sql += "AND DEVICE.ID = DEVICE_LOCATION.DEVICE_ID " + "AND DEVICE.ID = ENROLMENT.DEVICE_ID " + "AND DEVICE.TENANT_ID = ? AND DEVICE.TENANT_ID = ENROLMENT.TENANT_ID GROUP BY GEOHASH_PREFIX"; stmt = conn.prepareStatement(sql); @@ -1917,27 +1861,27 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { stmt.setDouble(index++, geoQuery.getSouthWest().getLongitude()); stmt.setDouble(index++, geoQuery.getNorthEast().getLongitude()); if (geoQuery.getDeviceTypes() != null) { - for (String s: geoQuery.getDeviceTypes()) { + for (String s : geoQuery.getDeviceTypes()) { stmt.setString(index++, s); } } if (geoQuery.getDeviceIdentifiers() != null) { - for (String s: geoQuery.getDeviceIdentifiers()) { + for (String s : geoQuery.getDeviceIdentifiers()) { stmt.setString(index++, s); } } if (geoQuery.getOwners() != null) { - for (String s: geoQuery.getOwners()) { + for (String s : geoQuery.getOwners()) { stmt.setString(index++, s); } } if (geoQuery.getOwnerships() != null) { - for (String s: geoQuery.getOwnerships()) { + for (String s : geoQuery.getOwnerships()) { stmt.setString(index++, s); } } if (geoQuery.getStatuses() != null) { - for (Status s: geoQuery.getStatuses()) { + for (Status s : geoQuery.getStatuses()) { stmt.setString(index++, s.toString()); } } @@ -2013,15 +1957,15 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { StringJoiner joiner = new StringJoiner(",", "SELECT " - + "d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " - + "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, " + + "d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, e.DEVICE_TYPE, " + + "e.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, " + "e.DATE_OF_LAST_UPDATE, 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 " + + "(SELECT d.ID, d.DESCRIPTION, d.NAME " + + "FROM DM_DEVICE d " + "WHERE " - + "t.ID = d.DEVICE_TYPE_ID AND d.DEVICE_IDENTIFICATION IN (", + + "d.DEVICE_IDENTIFICATION IN (", ") AND d.TENANT_ID = ?) d1 " + "WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? " + "ORDER BY e.DATE_OF_LAST_UPDATE DESC, e.STATUS ASC"); @@ -2071,15 +2015,15 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { StringJoiner joiner = new StringJoiner(",", "SELECT " - + "d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + + "d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, e.DEVICE_TYPE, " + "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, " + "e.DATE_OF_LAST_UPDATE, 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 " + + "(SELECT d.ID, d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION " + + "FROM DM_DEVICE d " + "WHERE " - + "t.ID = d.DEVICE_TYPE_ID AND d.DEVICE_IDENTIFICATION IN (", + + " d.DEVICE_IDENTIFICATION IN (", ") AND d.TENANT_ID = ?) d1 " + "WHERE d1.ID = e.DEVICE_ID AND " + statusJoiner.toString() + "AND TENANT_ID = ? ORDER BY e.DATE_OF_LAST_UPDATE DESC, e.STATUS ASC"); @@ -2113,7 +2057,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } public List getDeviceLocationInfo(DeviceIdentifier deviceIdentifier, long from, - long to) throws DeviceManagementDAOException { + long to) throws DeviceManagementDAOException { List deviceLocationHistories = new ArrayList<>(); String sql = "SELECT " + "DEVICE_ID, " @@ -2245,7 +2189,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { "d.ID AS DEVICE_ID, " + "d.DESCRIPTION, " + "d.NAME AS DEVICE_NAME, " + - "t.NAME AS DEVICE_TYPE, " + + "e.DEVICE_TYPE, " + "d.DEVICE_IDENTIFICATION, " + "e.OWNER, " + "e.OWNERSHIP, " + @@ -2256,9 +2200,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { "e.ID AS ENROLMENT_ID " + "FROM DM_DEVICE AS d " + "INNER JOIN DM_ENROLMENT AS e ON d.ID = e.DEVICE_ID " + - "INNER JOIN DM_DEVICE_TYPE AS t ON d.DEVICE_TYPE_ID = t.ID " + "WHERE " + - "t.NAME = ? AND e.TENANT_ID = ? AND d.ID " + + "e.DEVICE_TYPE = ? AND e.TENANT_ID = ? AND d.ID " + "NOT IN " + "(SELECT a.DEVICE_ID FROM DM_APPLICATION AS a WHERE a.APP_IDENTIFIER = ?"; @@ -2310,9 +2253,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { "COUNT(d.ID) AS DEVICE_COUNT " + "FROM DM_DEVICE AS d " + "INNER JOIN DM_ENROLMENT AS e ON d.ID = e.DEVICE_ID " + - "INNER JOIN DM_DEVICE_TYPE AS t ON d.DEVICE_TYPE_ID = t.ID " + "WHERE " + - "t.NAME = ? AND e.TENANT_ID = ? AND d.ID " + + "e.DEVICE_TYPE = ? AND e.TENANT_ID = ? AND d.ID " + "NOT IN " + "(SELECT a.DEVICE_ID FROM DM_APPLICATION AS a WHERE a.APP_IDENTIFIER = ?"; @@ -2354,40 +2296,39 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { throws DeviceManagementDAOException { try { Connection conn = getConnection(); - String sql = "" + + String sql = "SELECT e1.owner," + - "e1.ownership," + - "e1.enrolment_id," + - "e1.device_id," + - "e1.status," + - "e1.date_of_last_update," + - "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 " + - "INNER JOIN " + - "(SELECT DEVICE_ID " + - "FROM DM_DEVICE_INFO " + - "WHERE " + - "KEY_FIELD = 'encryptionEnabled' " + - "AND VALUE_FIELD = ?) AS di " + - "ON di.DEVICE_ID = e.DEVICE_ID " + - "WHERE e.tenant_id = ?) e1, " + - "dm_device_type t " + - "WHERE d.id = e1.device_id " + - "AND t.id = d.device_type_id " + - "ORDER BY e1.date_of_last_update DESC " + - "LIMIT ? OFFSET ?"; + "e1.ownership," + + "e1.enrolment_id," + + "e1.device_id," + + "e1.status," + + "e1.date_of_last_update," + + "e1.date_of_enrolment," + + "d.description," + + "d.NAME AS DEVICE_NAME," + + "d.device_identification," + + "e1.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 " + + "e.DEVICE_TYPE " + + "FROM dm_enrolment e " + + "INNER JOIN " + + "(SELECT DEVICE_ID " + + "FROM DM_DEVICE_INFO " + + "WHERE " + + "KEY_FIELD = 'encryptionEnabled' " + + "AND VALUE_FIELD = ?) AS di " + + "ON di.DEVICE_ID = e.DEVICE_ID " + + "WHERE e.tenant_id = ?) e1, " + + "WHERE d.id = e1.device_id " + + "ORDER BY e1.date_of_last_update DESC " + + "LIMIT ? OFFSET ?"; try (PreparedStatement ps = conn.prepareStatement(sql)) { ps.setBoolean(1, isEncrypted); @@ -2417,23 +2358,12 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { throws DeviceManagementDAOException { try { Connection conn = getConnection(); - String sql = "" + - "SELECT " + - "COUNT(e1.DEVICE_ID) AS DEVICE_COUNT " + - "FROM dm_device d," + - "(SELECT e.id AS ENROLMENT_ID, " + - "e.device_id " + - "FROM dm_enrolment e " + - "INNER JOIN " + - "(SELECT DEVICE_ID " + - "FROM DM_DEVICE_INFO " + - "WHERE KEY_FIELD = 'encryptionEnabled' " + - "AND VALUE_FIELD = ?) AS di " + - "ON di.DEVICE_ID = e.DEVICE_ID " + - "WHERE e.tenant_id = ?) e1, " + - "dm_device_type t " + - "WHERE d.id = e1.device_id " + - "AND t.id = d.device_type_id "; + String sql = + "SELECT COUNT(DEVICE_ID) " + + "FROM DM_DEVICE_INFO " + + "WHERE KEY_FIELD = 'encryptionEnabled' " + + "AND VALUE_FIELD = ?"; + //TODO: Add tenant column to DM_DEVICE_INFO table try (PreparedStatement ps = conn.prepareStatement(sql)) { ps.setBoolean(1, isEncrypted); @@ -3092,16 +3022,15 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { + "DM_DEVICE.ID AS DEVICE_ID, " + "DM_DEVICE.NAME AS DEVICE_NAME, " + "DM_DEVICE.DESCRIPTION AS DESCRIPTION, " - + "DM_DEVICE.DEVICE_TYPE_ID, " - + "DM_DEVICE.DEVICE_IDENTIFICATION AS DEVICE_IDENTIFICATION, " + + "e.DEVICE_TYPE, " + + "e.DEVICE_IDENTIFICATION AS DEVICE_IDENTIFICATION, " + "e.ID AS ENROLMENT_ID, " + "e.OWNER, " + "e.OWNERSHIP, " + "e.DATE_OF_ENROLMENT, " + "e.DATE_OF_LAST_UPDATE, " + "e.STATUS, " - + "e.IS_TRANSFERRED, " - + "device_types.NAME AS DEVICE_TYPE " + + "e.IS_TRANSFERRED " + "FROM DM_DEVICE_GROUP_MAP " + "INNER JOIN DM_DEVICE ON " + "DM_DEVICE_GROUP_MAP.DEVICE_ID = DM_DEVICE.ID " @@ -3110,8 +3039,6 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { + "INNER JOIN DM_ENROLMENT e ON " + "DM_DEVICE.ID = e.DEVICE_ID AND " + "DM_DEVICE.TENANT_ID = e.TENANT_ID " - + "INNER JOIN (SELECT ID, NAME FROM DM_DEVICE_TYPE) AS device_types ON " - + "device_types.ID = DM_DEVICE.DEVICE_TYPE_ID " + "WHERE DM_DEVICE.ID IN (", ") AND DM_DEVICE.TENANT_ID = ?"); @@ -3289,10 +3216,11 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); List devices = new ArrayList<>(); String query = "SELECT e.ID AS ENROLMENT_ID, e.DEVICE_ID, e.OWNER, e.OWNERSHIP, e.DATE_OF_ENROLMENT, " + - "e.DATE_OF_LAST_UPDATE, e.IS_TRANSFERRED, e.STATUS,d.DEVICE_NAME, d.DESCRIPTION, d.DEVICE_TYPE, d.DEVICE_IDENTIFICATION " + - "FROM DM_ENROLMENT e,(SELECT d1.ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_IDENTIFICATION, " + - "t1.NAME AS DEVICE_TYPE FROM DM_DEVICE d1, DM_DEVICE_TYPE t1 WHERE d1.DEVICE_TYPE_ID = t1.ID " + - "AND d1.TENANT_ID = ?) d WHERE e.STATUS NOT IN ('DELETED', 'REMOVED') AND e.DATE_OF_ENROLMENT > ? AND e.TENANT_ID = ?"; + "e.DATE_OF_LAST_UPDATE, e.IS_TRANSFERRED, e.STATUS,d.DEVICE_NAME, d.DESCRIPTION, e.DEVICE_TYPE, " + + "e.DEVICE_IDENTIFICATION " + + "FROM DM_ENROLMENT e, (SELECT d1.ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME FROM DM_DEVICE d1 " + + "WHERE d1.TENANT_ID = ?) d WHERE e.STATUS NOT IN ('DELETED', 'REMOVED') " + + "AND e.DATE_OF_ENROLMENT > ? AND e.TENANT_ID = ?"; try { Connection connection = DeviceManagementDAOFactory.getConnection(); try (PreparedStatement preparedStatement = connection.prepareStatement(query)) { @@ -3314,14 +3242,16 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } return devices; } + public List getDevicesEnrolledPriorTo(Date priorTo) throws DeviceManagementDAOException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); List devices = new ArrayList<>(); String query = "SELECT e.ID AS ENROLMENT_ID, e.DEVICE_ID, e.OWNER, e.OWNERSHIP, e.DATE_OF_ENROLMENT, " + - "e.DATE_OF_LAST_UPDATE, e.IS_TRANSFERRED, e.STATUS, d.DEVICE_NAME, d.DESCRIPTION, d.DEVICE_TYPE, d.DEVICE_IDENTIFICATION " + - "FROM DM_ENROLMENT e,(SELECT d1.ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_IDENTIFICATION, " + - "t1.NAME AS DEVICE_TYPE FROM DM_DEVICE d1, DM_DEVICE_TYPE t1 WHERE d1.DEVICE_TYPE_ID = t1.ID " + - "AND d1.TENANT_ID = ?) d WHERE e.STATUS NOT IN ('DELETED', 'REMOVED') AND e.DATE_OF_ENROLMENT < ? AND e.TENANT_ID = ?"; + "e.DATE_OF_LAST_UPDATE, e.IS_TRANSFERRED, e.STATUS, d.DEVICE_NAME, d.DESCRIPTION, e.DEVICE_TYPE, " + + "e.DEVICE_IDENTIFICATION " + + "FROM DM_ENROLMENT e, (SELECT d1.ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME FROM DM_DEVICE d1 " + + "WHERE d1.TENANT_ID = ?) d WHERE e.STATUS NOT IN ('DELETED', 'REMOVED') " + + "AND e.DATE_OF_ENROLMENT < ? AND e.TENANT_ID = ?"; try { Connection connection = DeviceManagementDAOFactory.getConnection(); try (PreparedStatement preparedStatement = connection.prepareStatement(query)) { diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractEnrollmentDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractEnrollmentDAOImpl.java index 98050fe3186..8bde3352275 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractEnrollmentDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractEnrollmentDAOImpl.java @@ -17,6 +17,7 @@ */ package io.entgra.device.mgt.core.device.mgt.core.dao.impl; +import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.context.PrivilegedCarbonContext; import io.entgra.device.mgt.core.device.mgt.common.Device; import io.entgra.device.mgt.core.device.mgt.common.DeviceManagementConstants; @@ -39,24 +40,29 @@ import java.util.List; public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { @Override - public EnrolmentInfo addEnrollment(int deviceId, EnrolmentInfo enrolmentInfo, + public EnrolmentInfo addEnrollment(int deviceId, DeviceIdentifier deviceIdentifier, EnrolmentInfo enrolmentInfo, int tenantId) throws DeviceManagementDAOException { Connection conn; PreparedStatement stmt = null; ResultSet rs = null; try { conn = this.getConnection(); - String sql = "INSERT INTO DM_ENROLMENT(DEVICE_ID, OWNER, OWNERSHIP, STATUS, " + - "DATE_OF_ENROLMENT, DATE_OF_LAST_UPDATE, TENANT_ID) VALUES(?, ?, ?, ?, ?, ?, ?)"; - stmt = conn.prepareStatement(sql, new String[] {"id"}); + String sql = "INSERT INTO DM_ENROLMENT(DEVICE_ID, DEVICE_TYPE, DEVICE_IDENTIFICATION, OWNER, OWNERSHIP, " + + "STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, DATE_OF_LAST_UPDATE, TENANT_ID) " + + "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; Timestamp enrollmentTime = new Timestamp(new Date().getTime()); + + stmt = conn.prepareStatement(sql, new String[]{"id"}); stmt.setInt(1, deviceId); - stmt.setString(2, enrolmentInfo.getOwner()); - stmt.setString(3, enrolmentInfo.getOwnership().toString()); - stmt.setString(4, enrolmentInfo.getStatus().toString()); - stmt.setTimestamp(5, enrollmentTime); - stmt.setTimestamp(6, enrollmentTime); - stmt.setInt(7, tenantId); + stmt.setString(2, deviceIdentifier.getType()); + stmt.setString(3, deviceIdentifier.getId()); + stmt.setString(4, enrolmentInfo.getOwner()); + stmt.setString(5, enrolmentInfo.getOwnership().toString()); + stmt.setString(6, enrolmentInfo.getStatus().toString()); + stmt.setBoolean(7, enrolmentInfo.isTransferred()); + stmt.setTimestamp(8, enrollmentTime); + stmt.setTimestamp(9, enrollmentTime); + stmt.setInt(10, tenantId); stmt.execute(); rs = stmt.getGeneratedKeys(); @@ -70,7 +76,6 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { } return null; } catch (SQLException e) { - e.printStackTrace(); throw new DeviceManagementDAOException("Error occurred while adding enrolment configuration", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); @@ -81,7 +86,6 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { public int updateEnrollment(EnrolmentInfo enrolmentInfo, int tenantId) throws DeviceManagementDAOException { Connection conn; PreparedStatement stmt = null; - ResultSet rs = null; try { conn = this.getConnection(); String sql = "UPDATE DM_ENROLMENT SET OWNERSHIP = ?, STATUS = ?, DATE_OF_LAST_UPDATE = ? " + @@ -92,12 +96,11 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { stmt.setTimestamp(3, new Timestamp(new Date().getTime())); stmt.setInt(4, enrolmentInfo.getId()); stmt.setInt(5, tenantId); - int updatedCount = stmt.executeUpdate(); - return updatedCount; + return stmt.executeUpdate(); } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while updating enrolment configuration", e); } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); + DeviceManagementDAOUtil.cleanupResources(stmt, null); } } @@ -105,7 +108,6 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { public boolean updateEnrollmentStatus(List enrolmentInfos) throws DeviceManagementDAOException { Connection conn; PreparedStatement stmt = null; - ResultSet rs = null; boolean status = false; int updateStatus = -1; try { @@ -132,7 +134,7 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while updating enrolment status of given device-list.", e); } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); + DeviceManagementDAOUtil.cleanupResources(stmt, null); } return status; } @@ -147,7 +149,7 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { try { conn = this.getConnection(); String sql = "DELETE FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql, new String[] {"id"}); + stmt = conn.prepareStatement(sql, new String[]{"id"}); stmt.setInt(1, deviceId); stmt.setString(2, currentOwner); stmt.setInt(3, tenantId); @@ -177,12 +179,12 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { stmt.setString(1, owner); stmt.setInt(2, tenantID); rs = stmt.executeQuery(); - if(rs.next()){ + if (rs.next()) { count = rs.getInt("COUNT"); } } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while trying to get device " + - "count of Owner : "+owner, e); + "count of Owner : " + owner, e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } @@ -197,11 +199,11 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { @Override public boolean setStatusAllDevices(String currentOwner, EnrolmentInfo.Status status, int tenantId) - throws DeviceManagementDAOException{ + throws DeviceManagementDAOException { Connection conn; PreparedStatement stmt = null; Timestamp updateTime = new Timestamp(new Date().getTime()); - if(getCountOfDevicesOfOwner(currentOwner, tenantId) > 0){ + if (getCountOfDevicesOfOwner(currentOwner, tenantId) > 0) { try { conn = this.getConnection(); String sql = "UPDATE DM_ENROLMENT SET STATUS = ?, DATE_OF_LAST_UPDATE = ? WHERE OWNER = ? AND TENANT_ID = ?"; @@ -236,8 +238,8 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { stmt.setInt(3, enrolmentID); stmt.setInt(4, tenantId); int updatedRowCount = stmt.executeUpdate(); - if (updatedRowCount != 1){ - throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment: "+ + if (updatedRowCount != 1) { + throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment: " + updatedRowCount + " rows were updated instead of one row!!!"); } // save the device status history @@ -254,10 +256,11 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { return addDeviceStatus(config.getId(), config.getStatus()); } - public boolean addDeviceStatus(String currentOwner, EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException { + public boolean addDeviceStatus(String currentOwner, EnrolmentInfo.Status status, int tenantId) + throws DeviceManagementDAOException { Connection conn; String changedBy = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); - if (changedBy == null){ + if (changedBy == null) { changedBy = DeviceManagementConstants.MaintenanceProperties.MAINTENANCE_USER; } PreparedStatement stmt = null; @@ -281,7 +284,7 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { sql = "INSERT INTO DM_DEVICE_STATUS (ENROLMENT_ID, DEVICE_ID, STATUS, UPDATE_TIME, CHANGED_BY) VALUES(?, ?, ?, ?, ?)"; try (PreparedStatement ps = conn.prepareStatement(sql)) { if (conn.getMetaData().supportsBatchUpdates()) { - for(int[] info: enrolmentInfoList){ + for (int[] info : enrolmentInfoList) { ps.setInt(1, info[0]); ps.setInt(2, info[1]); ps.setString(3, status.toString()); @@ -296,7 +299,7 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { } } } else { - for(int[] info: enrolmentInfoList){ + for (int[] info : enrolmentInfoList) { ps.setInt(1, info[0]); ps.setInt(2, info[1]); ps.setString(3, status.toString()); @@ -320,14 +323,15 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { public boolean addDeviceStatus(int enrolmentId, EnrolmentInfo.Status status) throws DeviceManagementDAOException { Connection conn; String changedBy = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); - if (changedBy == null){ + if (changedBy == null) { changedBy = DeviceManagementConstants.MaintenanceProperties.MAINTENANCE_USER; } PreparedStatement stmt = null; try { conn = this.getConnection(); - // get the device id and last udpated status from the device status table - String sql = "SELECT DEVICE_ID, STATUS FROM DM_DEVICE_STATUS WHERE ENROLMENT_ID = ? ORDER BY UPDATE_TIME DESC LIMIT 1"; + // get the device id and last updated status from the device status table + String sql = "SELECT DEVICE_ID, STATUS FROM DM_DEVICE_STATUS " + + "WHERE ENROLMENT_ID = ? ORDER BY UPDATE_TIME DESC LIMIT 1"; stmt = conn.prepareStatement(sql); stmt.setInt(1, enrolmentId); ResultSet rs = stmt.executeQuery(); @@ -353,12 +357,14 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { } else { // if there were no records corresponding to the enrolment id this is a problem. i.e. enrolment // id is invalid - throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment: no record for enrolment id " + enrolmentId); + throw new DeviceManagementDAOException("Error occurred while setting the status of " + + "device enrolment: no record for enrolment id " + enrolmentId); } DeviceManagementDAOUtil.cleanupResources(stmt, null); } - sql = "INSERT INTO DM_DEVICE_STATUS (ENROLMENT_ID, DEVICE_ID, STATUS, UPDATE_TIME, CHANGED_BY) VALUES(?, ?, ?, ?, ?)"; + sql = "INSERT INTO DM_DEVICE_STATUS (ENROLMENT_ID, DEVICE_ID, STATUS, UPDATE_TIME, CHANGED_BY) " + + "VALUES(?, ?, ?, ?, ?)"; stmt = conn.prepareStatement(sql); Timestamp updateTime = new Timestamp(new Date().getTime()); stmt.setInt(1, enrolmentId); @@ -377,6 +383,7 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { } return true; } + @Override public EnrolmentInfo.Status getStatus(int deviceId, String currentOwner, int tenantId) throws DeviceManagementDAOException { @@ -534,11 +541,11 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { } } - private Connection getConnection() throws SQLException { + protected Connection getConnection() throws SQLException { return DeviceManagementDAOFactory.getConnection(); } - private EnrolmentInfo loadEnrolment(ResultSet rs) throws SQLException { + protected EnrolmentInfo loadEnrolment(ResultSet rs) throws SQLException { EnrolmentInfo enrolmentInfo = new EnrolmentInfo(); enrolmentInfo.setOwner(rs.getString("OWNER")); enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.valueOf(rs.getString("OWNERSHIP"))); diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractEventConfigDAO.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractEventConfigDAO.java index e5780003d11..dfe882ed44c 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractEventConfigDAO.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractEventConfigDAO.java @@ -21,7 +21,6 @@ package io.entgra.device.mgt.core.device.mgt.core.dao.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import io.entgra.device.mgt.core.device.mgt.common.event.config.EventConfig; -import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOFactory; import io.entgra.device.mgt.core.device.mgt.core.dao.EventConfigDAO; import io.entgra.device.mgt.core.device.mgt.core.dao.EventManagementDAOException; import io.entgra.device.mgt.core.device.mgt.core.dao.EventManagementDAOFactory; @@ -30,12 +29,9 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.sql.Timestamp; import java.util.ArrayList; import java.util.Collections; -import java.util.HashMap; import java.util.List; -import java.util.Map; public abstract class AbstractEventConfigDAO implements EventConfigDAO { private static final Log log = LogFactory.getLog(AbstractEventConfigDAO.class); diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/enrolment/GenericEnrollmentDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/enrolment/GenericEnrollmentDAOImpl.java index 188af00f886..831e058ad9c 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/enrolment/GenericEnrollmentDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/enrolment/GenericEnrollmentDAOImpl.java @@ -17,532 +17,8 @@ */ package io.entgra.device.mgt.core.device.mgt.core.dao.impl.enrolment; -import org.wso2.carbon.context.PrivilegedCarbonContext; -import io.entgra.device.mgt.core.device.mgt.common.Device; -import io.entgra.device.mgt.core.device.mgt.common.DeviceManagementConstants; -import io.entgra.device.mgt.core.device.mgt.common.EnrolmentInfo; -import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOException; -import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOFactory; import io.entgra.device.mgt.core.device.mgt.core.dao.impl.AbstractEnrollmentDAOImpl; -import io.entgra.device.mgt.core.device.mgt.core.dao.util.DeviceManagementDAOUtil; - -import java.sql.*; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; public class GenericEnrollmentDAOImpl extends AbstractEnrollmentDAOImpl { - @Override - public EnrolmentInfo addEnrollment(int deviceId, EnrolmentInfo enrolmentInfo, - int tenantId) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - try { - conn = this.getConnection(); - String sql = "INSERT INTO DM_ENROLMENT(DEVICE_ID, OWNER, OWNERSHIP, STATUS, " + - "DATE_OF_ENROLMENT, DATE_OF_LAST_UPDATE, TENANT_ID) VALUES(?, ?, ?, ?, ?, ?, ?)"; - stmt = conn.prepareStatement(sql, new String[] {"id"}); - Timestamp enrollmentTime = new Timestamp(new Date().getTime()); - stmt.setInt(1, deviceId); - stmt.setString(2, enrolmentInfo.getOwner()); - stmt.setString(3, enrolmentInfo.getOwnership().toString()); - stmt.setString(4, enrolmentInfo.getStatus().toString()); - stmt.setTimestamp(5, enrollmentTime); - stmt.setTimestamp(6, enrollmentTime); - stmt.setInt(7, tenantId); - stmt.execute(); - - rs = stmt.getGeneratedKeys(); - if (rs.next()) { - int enrolmentId = rs.getInt(1); - enrolmentInfo.setId(enrolmentId); - enrolmentInfo.setDateOfEnrolment(enrollmentTime.getTime()); - enrolmentInfo.setDateOfLastUpdate(enrollmentTime.getTime()); - addDeviceStatus(enrolmentId, enrolmentInfo.getStatus()); - return enrolmentInfo; - } - return null; - } catch (SQLException e) { - e.printStackTrace(); - throw new DeviceManagementDAOException("Error occurred while adding enrolment configuration", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - } - - @Override - public int updateEnrollment(EnrolmentInfo enrolmentInfo, int tenantId) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - try { - conn = this.getConnection(); - String sql = "UPDATE DM_ENROLMENT SET OWNERSHIP = ?, STATUS = ?, DATE_OF_LAST_UPDATE = ? " + - "WHERE ID = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql); - stmt.setString(1, enrolmentInfo.getOwnership().toString()); - stmt.setString(2, enrolmentInfo.getStatus().toString()); - stmt.setTimestamp(3, new Timestamp(new Date().getTime())); - stmt.setInt(4, enrolmentInfo.getId()); - stmt.setInt(5, tenantId); - int updatedCount = stmt.executeUpdate(); - return updatedCount; - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while updating enrolment configuration", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - } - - @Override - public boolean updateEnrollmentStatus(List enrolmentInfos) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - boolean status = false; - int updateStatus = -1; - try { - conn = this.getConnection(); - String sql = "UPDATE DM_ENROLMENT SET STATUS = ? WHERE ID = ?"; - stmt = conn.prepareStatement(sql); - if (conn.getMetaData().supportsBatchUpdates()) { - for (EnrolmentInfo enrolmentInfo : enrolmentInfos) { - stmt.setString(1, enrolmentInfo.getStatus().toString()); - stmt.setInt(2, enrolmentInfo.getId()); - stmt.addBatch(); - } - updateStatus = stmt.executeBatch().length; - } else { - for (EnrolmentInfo enrolmentInfo : enrolmentInfos) { - stmt.setString(1, enrolmentInfo.getStatus().toString()); - stmt.setInt(2, enrolmentInfo.getId()); - updateStatus = stmt.executeUpdate(); - } - } - if (updateStatus > 0) { - status = true; - } - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while updating enrolment status of given device-list.", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - return status; - } - - @Override - public int removeEnrollment(int deviceId, String currentOwner, - int tenantId) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - int status = -1; - try { - conn = this.getConnection(); - String sql = "DELETE FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql, new String[] {"id"}); - stmt.setInt(1, deviceId); - stmt.setString(2, currentOwner); - stmt.setInt(3, tenantId); - stmt.executeUpdate(); - - rs = stmt.getGeneratedKeys(); - if (rs.next()) { - status = 1; - } - return status; - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while removing device enrolment", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - } - - private int getCountOfDevicesOfOwner(String owner, int tenantID) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - int count = 0; - try { - conn = this.getConnection(); - String checkQuery = "SELECT COUNT(ID) AS COUNT FROM DM_ENROLMENT WHERE OWNER = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(checkQuery); - stmt.setString(1, owner); - stmt.setInt(2, tenantID); - rs = stmt.executeQuery(); - if(rs.next()){ - count = rs.getInt("COUNT"); - } - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while trying to get device " + - "count of Owner : "+owner, e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - return count; - } - - @Override - public boolean setStatus(String currentOwner, EnrolmentInfo.Status status, - int tenantId) throws DeviceManagementDAOException { - return setStatusAllDevices(currentOwner, status, tenantId); - } - - @Override - public boolean setStatusAllDevices(String currentOwner, EnrolmentInfo.Status status, int tenantId) - throws DeviceManagementDAOException{ - Connection conn; - PreparedStatement stmt = null; - Timestamp updateTime = new Timestamp(new Date().getTime()); - if(getCountOfDevicesOfOwner(currentOwner, tenantId) > 0){ - try { - conn = this.getConnection(); - String sql = "UPDATE DM_ENROLMENT SET STATUS = ?, DATE_OF_LAST_UPDATE = ? WHERE OWNER = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql); - stmt.setString(1, status.toString()); - stmt.setTimestamp(2, updateTime); - stmt.setString(3, currentOwner); - stmt.setInt(4, tenantId); - stmt.executeUpdate(); - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, null); - } - return addDeviceStatus(currentOwner, status, tenantId); - } else { - return false; - } - } - - @Override - public boolean setStatus(int enrolmentID, EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - Timestamp updateTime = new Timestamp(new Date().getTime()); - try { - conn = this.getConnection(); - String sql = "UPDATE DM_ENROLMENT SET STATUS = ?, DATE_OF_LAST_UPDATE = ? WHERE ID = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql); - stmt.setString(1, status.toString()); - stmt.setTimestamp(2, updateTime); - stmt.setInt(3, enrolmentID); - stmt.setInt(4, tenantId); - int updatedRowCount = stmt.executeUpdate(); - if (updatedRowCount != 1){ - throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment: "+ - updatedRowCount + " rows were updated instead of one row!!!"); - } - // save the device status history - addDeviceStatus(enrolmentID, status); - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, null); - } - return true; - } - - public boolean addDeviceStatus(EnrolmentInfo config) throws DeviceManagementDAOException { - return addDeviceStatus(config.getId(), config.getStatus()); - } - - public boolean addDeviceStatus(String currentOwner, EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException { - Connection conn; - String changedBy = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); - if (changedBy == null){ - changedBy = DeviceManagementConstants.MaintenanceProperties.MAINTENANCE_USER; - } - PreparedStatement stmt = null; - ResultSet rs = null; - List enrolmentInfoList = new ArrayList<>(); - try { - conn = this.getConnection(); - String sql = "SELECT ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, " + - "DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE OWNER = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql); - stmt.setString(1, currentOwner); - stmt.setInt(2, tenantId); - rs = stmt.executeQuery(); - while (rs.next()) { - int enrolmentId = rs.getInt("ID"); - int deviceId = rs.getInt("DEVICE_ID"); - enrolmentInfoList.add(new int[]{enrolmentId, deviceId}); - } - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - Timestamp updateTime = new Timestamp(new Date().getTime()); - sql = "INSERT INTO DM_DEVICE_STATUS (ENROLMENT_ID, DEVICE_ID, STATUS, UPDATE_TIME, CHANGED_BY) VALUES(?, ?, ?, ?, ?)"; - try (PreparedStatement ps = conn.prepareStatement(sql)) { - if (conn.getMetaData().supportsBatchUpdates()) { - for(int[] info: enrolmentInfoList){ - ps.setInt(1, info[0]); - ps.setInt(2, info[1]); - ps.setString(3, status.toString()); - ps.setTimestamp(4, updateTime); - ps.setString(5, changedBy); - ps.addBatch(); - } - int[] batchResult = ps.executeBatch(); - for (int i : batchResult) { - if (i == 0 || i == Statement.SUCCESS_NO_INFO || i == Statement.EXECUTE_FAILED) { - return false; - } - } - } else { - for(int[] info: enrolmentInfoList){ - ps.setInt(1, info[0]); - ps.setInt(2, info[1]); - ps.setString(3, status.toString()); - ps.setTimestamp(4, updateTime); - ps.setString(5, changedBy); - ps.execute(); - } - - } - } - - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while retrieving the enrolments " + - "information of owner '" + currentOwner + "'", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - return true; - } - - public boolean addDeviceStatus(int enrolmentId, EnrolmentInfo.Status status) throws DeviceManagementDAOException { - Connection conn; - String changedBy = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); - if (changedBy == null){ - changedBy = DeviceManagementConstants.MaintenanceProperties.MAINTENANCE_USER; - } - PreparedStatement stmt = null; - try { - conn = this.getConnection(); - // get the device id and last udpated status from the device status table - String sql = "SELECT DEVICE_ID, STATUS FROM DM_DEVICE_STATUS WHERE ENROLMENT_ID = ? ORDER BY UPDATE_TIME DESC LIMIT 1"; - stmt = conn.prepareStatement(sql); - stmt.setInt(1, enrolmentId); - ResultSet rs = stmt.executeQuery(); - int deviceId = -1; - EnrolmentInfo.Status previousStatus = null; - if (rs.next()) { - // if there is a record corresponding to the enrolment we save the status and the device id - previousStatus = EnrolmentInfo.Status.valueOf(rs.getString("STATUS")); - deviceId = rs.getInt("DEVICE_ID"); - } - DeviceManagementDAOUtil.cleanupResources(stmt, null); - // if there was no record for the enrolment or the previous status is not the same as the current status - // we'll add a record - if (previousStatus == null || previousStatus != status){ - if (deviceId == -1) { - // we need the device id in order to add a new record, therefore we get it from the enrolment table - sql = "SELECT DEVICE_ID FROM DM_ENROLMENT WHERE ID = ?"; - stmt = conn.prepareStatement(sql); - stmt.setInt(1, enrolmentId); - rs = stmt.executeQuery(); - if (rs.next()) { - deviceId = rs.getInt("DEVICE_ID"); - } else { - // if there were no records corresponding to the enrolment id this is a problem. i.e. enrolment - // id is invalid - throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment: no record for enrolment id " + enrolmentId); - } - DeviceManagementDAOUtil.cleanupResources(stmt, null); - } - - sql = "INSERT INTO DM_DEVICE_STATUS (ENROLMENT_ID, DEVICE_ID, STATUS, UPDATE_TIME, CHANGED_BY) VALUES(?, ?, ?, ?, ?)"; - stmt = conn.prepareStatement(sql); - Timestamp updateTime = new Timestamp(new Date().getTime()); - stmt.setInt(1, enrolmentId); - stmt.setInt(2, deviceId); - stmt.setString(3, status.toString()); - stmt.setTimestamp(4, updateTime); - stmt.setString(5, changedBy); - stmt.execute(); - } else { - // no need to update status since the last recorded status is the same as the current status - } - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while setting the status of device", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, null); - } - return true; - } - @Override - public EnrolmentInfo.Status getStatus(int deviceId, String currentOwner, - int tenantId) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - EnrolmentInfo.Status status = null; - try { - conn = this.getConnection(); - String sql = "SELECT STATUS FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql); - 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")); - } - return status; - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - } - - @Override - public EnrolmentInfo getEnrollment(int deviceId, String currentOwner, - int tenantId) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - EnrolmentInfo enrolmentInfo = null; - try { - conn = this.getConnection(); - String sql = "SELECT ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, " + - "DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql); - stmt.setInt(1, deviceId); - stmt.setString(2, currentOwner); - stmt.setInt(3, tenantId); - rs = stmt.executeQuery(); - if (rs.next()) { - enrolmentInfo = this.loadEnrolment(rs); - } - return enrolmentInfo; - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while retrieving the enrolment " + - "information of user '" + currentOwner + "' upon device '" + deviceId + "'", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - } - - @Override - public EnrolmentInfo getEnrollment(int deviceId, int tenantId) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - EnrolmentInfo enrolmentInfo = null; - try { - conn = this.getConnection(); - String sql = "SELECT ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, " + - "DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND TENANT_ID = ? " + - "ORDER BY DATE_OF_LAST_UPDATE DESC"; - stmt = conn.prepareStatement(sql); - stmt.setInt(1, deviceId); - stmt.setInt(2, tenantId); - rs = stmt.executeQuery(); - if (rs.next()) { - enrolmentInfo = this.loadEnrolment(rs); - } - return enrolmentInfo; - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while retrieving the enrolment " + - "information of device '" + deviceId + "'", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - } - - @Override - public List getEnrollmentsOfUser(int deviceId, String user, int tenantId) - throws DeviceManagementDAOException { - List enrolmentInfos = new ArrayList<>(); - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - EnrolmentInfo enrolmentInfo = null; - try { - conn = this.getConnection(); - String sql = "SELECT ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, " + - "DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql); - stmt.setInt(1, deviceId); - stmt.setString(2, user); - stmt.setInt(3, tenantId); - rs = stmt.executeQuery(); - while (rs.next()) { - enrolmentInfo = this.loadEnrolment(rs); - enrolmentInfos.add(enrolmentInfo); - } - return enrolmentInfos; - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while retrieving the enrolments " + - "information of user '" + user + "' upon device '" + deviceId + "'", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - } - - @Override - public boolean updateOwnerOfEnrollment(List devices, String owner, int tenantId) - throws DeviceManagementDAOException { - try { - Connection conn = this.getConnection(); - boolean updateStatus = true; - String sql = "UPDATE DM_ENROLMENT " - + "SET OWNER = ?, IS_TRANSFERRED = ?, DATE_OF_LAST_UPDATE = ? " - + "WHERE ID = ? AND TENANT_ID = ?"; - try (PreparedStatement ps = conn.prepareStatement(sql)) { - if (conn.getMetaData().supportsBatchUpdates()) { - for (Device device : devices) { - ps.setString(1, owner); - ps.setBoolean(2, device.getEnrolmentInfo().isTransferred()); - ps.setTimestamp(3, new Timestamp(new Date().getTime())); - ps.setInt(4, device.getEnrolmentInfo().getId()); - ps.setInt(5, tenantId); - ps.addBatch(); - } - int[] batchResult = ps.executeBatch(); - for (int i : batchResult) { - if (i == 0 || i == Statement.SUCCESS_NO_INFO || i == Statement.EXECUTE_FAILED) { - updateStatus = false; - break; - } - } - } else { - for (Device device : devices) { - ps.setString(1, owner); - ps.setBoolean(2, device.getEnrolmentInfo().isTransferred()); - ps.setInt(3, device.getId()); - ps.setInt(4, tenantId); - if (ps.executeUpdate() == 0) { - updateStatus = false; - break; - } - } - } - } - return updateStatus; - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while obtaining the DB connection to update the " - + "owner of the device enrollment.", e); - } - } - - private Connection getConnection() throws SQLException { - return DeviceManagementDAOFactory.getConnection(); - } - - private EnrolmentInfo loadEnrolment(ResultSet rs) throws SQLException { - EnrolmentInfo enrolmentInfo = new EnrolmentInfo(); - enrolmentInfo.setOwner(rs.getString("OWNER")); - enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.valueOf(rs.getString("OWNERSHIP"))); - enrolmentInfo.setTransferred(rs.getBoolean("IS_TRANSFERRED")); - enrolmentInfo.setDateOfEnrolment(rs.getTimestamp("DATE_OF_ENROLMENT").getTime()); - enrolmentInfo.setDateOfLastUpdate(rs.getTimestamp("DATE_OF_LAST_UPDATE").getTime()); - enrolmentInfo.setStatus(EnrolmentInfo.Status.valueOf(rs.getString("STATUS"))); - enrolmentInfo.setId(rs.getInt("ID")); - return enrolmentInfo; - } - } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/enrolment/SQLServerEnrollmentDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/enrolment/SQLServerEnrollmentDAOImpl.java index 18074e08419..73903d15f1d 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/enrolment/SQLServerEnrollmentDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/enrolment/SQLServerEnrollmentDAOImpl.java @@ -17,6 +17,7 @@ */ package io.entgra.device.mgt.core.device.mgt.core.dao.impl.enrolment; +import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.context.PrivilegedCarbonContext; import io.entgra.device.mgt.core.device.mgt.common.Device; import io.entgra.device.mgt.core.device.mgt.common.DeviceManagementConstants; @@ -32,285 +33,6 @@ import java.util.List; public class SQLServerEnrollmentDAOImpl extends AbstractEnrollmentDAOImpl { - @Override - public EnrolmentInfo addEnrollment(int deviceId, EnrolmentInfo enrolmentInfo, - int tenantId) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - try { - conn = this.getConnection(); - String sql = "INSERT INTO DM_ENROLMENT(DEVICE_ID, OWNER, OWNERSHIP, STATUS, " + - "DATE_OF_ENROLMENT, DATE_OF_LAST_UPDATE, TENANT_ID) VALUES(?, ?, ?, ?, ?, ?, ?)"; - stmt = conn.prepareStatement(sql, new String[] {"id"}); - Timestamp enrollmentTime = new Timestamp(new Date().getTime()); - stmt.setInt(1, deviceId); - stmt.setString(2, enrolmentInfo.getOwner()); - stmt.setString(3, enrolmentInfo.getOwnership().toString()); - stmt.setString(4, enrolmentInfo.getStatus().toString()); - stmt.setTimestamp(5, enrollmentTime); - stmt.setTimestamp(6, enrollmentTime); - stmt.setInt(7, tenantId); - stmt.execute(); - - rs = stmt.getGeneratedKeys(); - if (rs.next()) { - int enrolmentId = rs.getInt(1); - enrolmentInfo.setId(enrolmentId); - enrolmentInfo.setDateOfEnrolment(enrollmentTime.getTime()); - enrolmentInfo.setDateOfLastUpdate(enrollmentTime.getTime()); - addDeviceStatus(enrolmentId, enrolmentInfo.getStatus()); - return enrolmentInfo; - } - return null; - } catch (SQLException e) { - e.printStackTrace(); - throw new DeviceManagementDAOException("Error occurred while adding enrolment configuration", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - } - - @Override - public int updateEnrollment(EnrolmentInfo enrolmentInfo, int tenantId) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - try { - conn = this.getConnection(); - String sql = "UPDATE DM_ENROLMENT SET OWNERSHIP = ?, STATUS = ?, DATE_OF_LAST_UPDATE = ? " + - "WHERE ID = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql); - stmt.setString(1, enrolmentInfo.getOwnership().toString()); - stmt.setString(2, enrolmentInfo.getStatus().toString()); - stmt.setTimestamp(3, new Timestamp(new Date().getTime())); - stmt.setInt(4, enrolmentInfo.getId()); - stmt.setInt(5, tenantId); - int updatedCount = stmt.executeUpdate(); - return updatedCount; - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while updating enrolment configuration", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - } - - @Override - public boolean updateEnrollmentStatus(List enrolmentInfos) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - boolean status = false; - int updateStatus = -1; - try { - conn = this.getConnection(); - String sql = "UPDATE DM_ENROLMENT SET STATUS = ? WHERE ID = ?"; - stmt = conn.prepareStatement(sql); - if (conn.getMetaData().supportsBatchUpdates()) { - for (EnrolmentInfo enrolmentInfo : enrolmentInfos) { - stmt.setString(1, enrolmentInfo.getStatus().toString()); - stmt.setInt(2, enrolmentInfo.getId()); - stmt.addBatch(); - } - updateStatus = stmt.executeBatch().length; - } else { - for (EnrolmentInfo enrolmentInfo : enrolmentInfos) { - stmt.setString(1, enrolmentInfo.getStatus().toString()); - stmt.setInt(2, enrolmentInfo.getId()); - updateStatus = stmt.executeUpdate(); - } - } - if (updateStatus > 0) { - status = true; - } - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while updating enrolment status of given device-list.", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - return status; - } - - @Override - public int removeEnrollment(int deviceId, String currentOwner, - int tenantId) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - int status = -1; - try { - conn = this.getConnection(); - String sql = "DELETE FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql, new String[] {"id"}); - stmt.setInt(1, deviceId); - stmt.setString(2, currentOwner); - stmt.setInt(3, tenantId); - stmt.executeUpdate(); - - rs = stmt.getGeneratedKeys(); - if (rs.next()) { - status = 1; - } - return status; - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while removing device enrolment", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - } - - private int getCountOfDevicesOfOwner(String owner, int tenantID) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - int count = 0; - try { - conn = this.getConnection(); - String checkQuery = "SELECT COUNT(ID) AS COUNT FROM DM_ENROLMENT WHERE OWNER = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(checkQuery); - stmt.setString(1, owner); - stmt.setInt(2, tenantID); - rs = stmt.executeQuery(); - if(rs.next()){ - count = rs.getInt("COUNT"); - } - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while trying to get device " + - "count of Owner : "+owner, e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - return count; - } - - @Override - public boolean setStatus(String currentOwner, EnrolmentInfo.Status status, - int tenantId) throws DeviceManagementDAOException { - return setStatusAllDevices(currentOwner, status, tenantId); - } - - @Override - public boolean setStatusAllDevices(String currentOwner, EnrolmentInfo.Status status, int tenantId) - throws DeviceManagementDAOException{ - Connection conn; - PreparedStatement stmt = null; - Timestamp updateTime = new Timestamp(new Date().getTime()); - if(getCountOfDevicesOfOwner(currentOwner, tenantId) > 0){ - try { - conn = this.getConnection(); - String sql = "UPDATE DM_ENROLMENT SET STATUS = ?, DATE_OF_LAST_UPDATE = ? WHERE OWNER = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql); - stmt.setString(1, status.toString()); - stmt.setTimestamp(2, updateTime); - stmt.setString(3, currentOwner); - stmt.setInt(4, tenantId); - stmt.executeUpdate(); - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, null); - } - return addDeviceStatus(currentOwner, status, tenantId); - } else { - return false; - } - } - - @Override - public boolean setStatus(int enrolmentID, EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - Timestamp updateTime = new Timestamp(new Date().getTime()); - try { - conn = this.getConnection(); - String sql = "UPDATE DM_ENROLMENT SET STATUS = ?, DATE_OF_LAST_UPDATE = ? WHERE ID = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql); - stmt.setString(1, status.toString()); - stmt.setTimestamp(2, updateTime); - stmt.setInt(3, enrolmentID); - stmt.setInt(4, tenantId); - int updatedRowCount = stmt.executeUpdate(); - if (updatedRowCount != 1){ - throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment: "+ - updatedRowCount + " rows were updated instead of one row!!!"); - } - // save the device status history - addDeviceStatus(enrolmentID, status); - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, null); - } - return true; - } - - public boolean addDeviceStatus(EnrolmentInfo config) throws DeviceManagementDAOException { - return addDeviceStatus(config.getId(), config.getStatus()); - } - - public boolean addDeviceStatus(String currentOwner, EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException { - Connection conn; - String changedBy = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); - if (changedBy == null){ - changedBy = DeviceManagementConstants.MaintenanceProperties.MAINTENANCE_USER; - } - PreparedStatement stmt = null; - ResultSet rs = null; - List enrolmentInfoList = new ArrayList<>(); - try { - conn = this.getConnection(); - String sql = "SELECT ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, " + - "DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE OWNER = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql); - stmt.setString(1, currentOwner); - stmt.setInt(2, tenantId); - rs = stmt.executeQuery(); - while (rs.next()) { - int enrolmentId = rs.getInt("ID"); - int deviceId = rs.getInt("DEVICE_ID"); - enrolmentInfoList.add(new int[]{enrolmentId, deviceId}); - } - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - Timestamp updateTime = new Timestamp(new Date().getTime()); - sql = "INSERT INTO DM_DEVICE_STATUS (ENROLMENT_ID, DEVICE_ID, STATUS, UPDATE_TIME, CHANGED_BY) VALUES(?, ?, ?, ?, ?)"; - try (PreparedStatement ps = conn.prepareStatement(sql)) { - if (conn.getMetaData().supportsBatchUpdates()) { - for(int[] info: enrolmentInfoList){ - ps.setInt(1, info[0]); - ps.setInt(2, info[1]); - ps.setString(3, status.toString()); - ps.setTimestamp(4, updateTime); - ps.setString(5, changedBy); - ps.addBatch(); - } - int[] batchResult = ps.executeBatch(); - for (int i : batchResult) { - if (i == 0 || i == Statement.SUCCESS_NO_INFO || i == Statement.EXECUTE_FAILED) { - return false; - } - } - } else { - for(int[] info: enrolmentInfoList){ - ps.setInt(1, info[0]); - ps.setInt(2, info[1]); - ps.setString(3, status.toString()); - ps.setTimestamp(4, updateTime); - ps.setString(5, changedBy); - ps.execute(); - } - - } - } - - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while retrieving the enrolments " + - "information of owner '" + currentOwner + "'", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - return true; - } - public boolean addDeviceStatus(int enrolmentId, EnrolmentInfo.Status status) throws DeviceManagementDAOException { Connection conn; String changedBy = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); @@ -319,8 +41,8 @@ public class SQLServerEnrollmentDAOImpl extends AbstractEnrollmentDAOImpl { } PreparedStatement stmt = null; try { - conn = this.getConnection(); - // get the device id and last udpated status from the device status table + conn = getConnection(); + // get the device id and last updated status from the device status table String sql = "SELECT TOP 1 DEVICE_ID, STATUS FROM DM_DEVICE_STATUS WHERE ENROLMENT_ID = ? ORDER BY UPDATE_TIME DESC"; stmt = conn.prepareStatement(sql); stmt.setInt(1, enrolmentId); @@ -371,177 +93,5 @@ public class SQLServerEnrollmentDAOImpl extends AbstractEnrollmentDAOImpl { } return true; } - @Override - public EnrolmentInfo.Status getStatus(int deviceId, String currentOwner, - int tenantId) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - EnrolmentInfo.Status status = null; - try { - conn = this.getConnection(); - String sql = "SELECT STATUS FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql); - 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")); - } - return status; - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - } - - @Override - public EnrolmentInfo getEnrollment(int deviceId, String currentOwner, - int tenantId) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - EnrolmentInfo enrolmentInfo = null; - try { - conn = this.getConnection(); - String sql = "SELECT ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, " + - "DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql); - stmt.setInt(1, deviceId); - stmt.setString(2, currentOwner); - stmt.setInt(3, tenantId); - rs = stmt.executeQuery(); - if (rs.next()) { - enrolmentInfo = this.loadEnrolment(rs); - } - return enrolmentInfo; - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while retrieving the enrolment " + - "information of user '" + currentOwner + "' upon device '" + deviceId + "'", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - } - - @Override - public EnrolmentInfo getEnrollment(int deviceId, int tenantId) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - EnrolmentInfo enrolmentInfo = null; - try { - conn = this.getConnection(); - String sql = "SELECT ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, " + - "DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND TENANT_ID = ? " + - "ORDER BY DATE_OF_LAST_UPDATE DESC"; - stmt = conn.prepareStatement(sql); - stmt.setInt(1, deviceId); - stmt.setInt(2, tenantId); - rs = stmt.executeQuery(); - if (rs.next()) { - enrolmentInfo = this.loadEnrolment(rs); - } - return enrolmentInfo; - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while retrieving the enrolment " + - "information of device '" + deviceId + "'", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - } - - @Override - public List getEnrollmentsOfUser(int deviceId, String user, int tenantId) - throws DeviceManagementDAOException { - List enrolmentInfos = new ArrayList<>(); - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - EnrolmentInfo enrolmentInfo = null; - try { - conn = this.getConnection(); - String sql = "SELECT ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, " + - "DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql); - stmt.setInt(1, deviceId); - stmt.setString(2, user); - stmt.setInt(3, tenantId); - rs = stmt.executeQuery(); - while (rs.next()) { - enrolmentInfo = this.loadEnrolment(rs); - enrolmentInfos.add(enrolmentInfo); - } - return enrolmentInfos; - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while retrieving the enrolments " + - "information of user '" + user + "' upon device '" + deviceId + "'", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - } - - @Override - public boolean updateOwnerOfEnrollment(List devices, String owner, int tenantId) - throws DeviceManagementDAOException { - try { - Connection conn = this.getConnection(); - boolean updateStatus = true; - String sql = "UPDATE DM_ENROLMENT " - + "SET OWNER = ?, IS_TRANSFERRED = ?, DATE_OF_LAST_UPDATE = ? " - + "WHERE ID = ? AND TENANT_ID = ?"; - try (PreparedStatement ps = conn.prepareStatement(sql)) { - if (conn.getMetaData().supportsBatchUpdates()) { - for (Device device : devices) { - ps.setString(1, owner); - ps.setBoolean(2, device.getEnrolmentInfo().isTransferred()); - ps.setTimestamp(3, new Timestamp(new Date().getTime())); - ps.setInt(4, device.getEnrolmentInfo().getId()); - ps.setInt(5, tenantId); - ps.addBatch(); - } - int[] batchResult = ps.executeBatch(); - for (int i : batchResult) { - if (i == 0 || i == Statement.SUCCESS_NO_INFO || i == Statement.EXECUTE_FAILED) { - updateStatus = false; - break; - } - } - } else { - for (Device device : devices) { - ps.setString(1, owner); - ps.setBoolean(2, device.getEnrolmentInfo().isTransferred()); - ps.setInt(3, device.getId()); - ps.setInt(4, tenantId); - if (ps.executeUpdate() == 0) { - updateStatus = false; - break; - } - } - } - } - return updateStatus; - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while obtaining the DB connection to update the " - + "owner of the device enrollment.", e); - } - } - - private Connection getConnection() throws SQLException { - return DeviceManagementDAOFactory.getConnection(); - } - - private EnrolmentInfo loadEnrolment(ResultSet rs) throws SQLException { - EnrolmentInfo enrolmentInfo = new EnrolmentInfo(); - enrolmentInfo.setOwner(rs.getString("OWNER")); - enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.valueOf(rs.getString("OWNERSHIP"))); - enrolmentInfo.setTransferred(rs.getBoolean("IS_TRANSFERRED")); - enrolmentInfo.setDateOfEnrolment(rs.getTimestamp("DATE_OF_ENROLMENT").getTime()); - enrolmentInfo.setDateOfLastUpdate(rs.getTimestamp("DATE_OF_LAST_UPDATE").getTime()); - enrolmentInfo.setStatus(EnrolmentInfo.Status.valueOf(rs.getString("STATUS"))); - enrolmentInfo.setId(rs.getInt("ID")); - return enrolmentInfo; - } } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java index ddb77a24a42..8322905a5e1 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java @@ -17,7 +17,6 @@ */ package io.entgra.device.mgt.core.device.mgt.core.operation.mgt.dao.impl; -import io.entgra.device.mgt.core.device.mgt.common.MDMAppConstants; import io.entgra.device.mgt.core.device.mgt.core.dto.operation.mgt.ProfileOperation; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -44,8 +43,6 @@ import io.entgra.device.mgt.core.device.mgt.core.operation.mgt.dao.util.Operatio import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; -import java.io.ObjectInputStream; -import java.io.ByteArrayInputStream; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -59,7 +56,6 @@ import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; -import java.util.Properties; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -575,14 +571,12 @@ public class GenericOperationDAOImpl implements OperationDAO { Connection conn = OperationManagementDAOFactory.getConnection(); String sql = "SELECT eom.ENROLMENT_ID, eom.OPERATION_ID, eom.ID AS EOM_MAPPING_ID, " - + "dor.ID AS OP_RES_ID, de.DEVICE_ID, d.DEVICE_IDENTIFICATION, d.DEVICE_TYPE_ID, " - + "dt.NAME AS DEVICE_TYPE_NAME, eom.STATUS, eom.CREATED_TIMESTAMP, " + + "dor.ID AS OP_RES_ID, de.DEVICE_ID, de.DEVICE_IDENTIFICATION, de.DEVICE_TYPE, " + + "eom.STATUS, eom.CREATED_TIMESTAMP, " + "eom.UPDATED_TIMESTAMP, op.OPERATION_CODE, op.TYPE AS OPERATION_TYPE, " + "dor.OPERATION_RESPONSE, op.INITIATED_BY, dor.RECEIVED_TIMESTAMP, dor.IS_LARGE_RESPONSE FROM " + "DM_ENROLMENT_OP_MAPPING eom INNER JOIN DM_OPERATION op " - + "ON op.ID=eom.OPERATION_ID INNER JOIN DM_ENROLMENT de " - + "ON de.ID=eom.ENROLMENT_ID INNER JOIN DM_DEVICE d ON d.ID=de.DEVICE_ID " - + "INNER JOIN DM_DEVICE_TYPE dt ON dt.ID=d.DEVICE_TYPE_ID " + + "ON op.ID=eom.OPERATION_ID INNER JOIN DM_ENROLMENT de ON de.ID=eom.ENROLMENT_ID " + "LEFT JOIN DM_DEVICE_OPERATION_RESPONSE dor ON dor.ENROLMENT_ID=de.id " + "AND dor.OPERATION_ID = eom.OPERATION_ID WHERE eom.OPERATION_ID " + "IN (SELECT * FROM TABLE(x INT = ?)) AND de.TENANT_ID = ?"; @@ -617,7 +611,7 @@ public class GenericOperationDAOImpl implements OperationDAO { DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); - deviceIdentifier.setType(rs.getString("DEVICE_TYPE_NAME")); + deviceIdentifier.setType(rs.getString("DEVICE_TYPE")); activityStatus.setDeviceIdentifier(deviceIdentifier); @@ -655,7 +649,7 @@ public class GenericOperationDAOImpl implements OperationDAO { DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); - deviceIdentifier.setType(rs.getString("DEVICE_TYPE_NAME")); + deviceIdentifier.setType(rs.getString("DEVICE_TYPE")); activityStatus.setDeviceIdentifier(deviceIdentifier); activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); @@ -715,8 +709,8 @@ public class GenericOperationDAOImpl implements OperationDAO { + "eom.OPERATION_ID, eom.ID AS EOM_MAPPING_ID, " + "dor.ID AS OP_RES_ID, " + "de.DEVICE_ID, " - + "d.DEVICE_IDENTIFICATION, " - + "d.DEVICE_TYPE_ID, dt.NAME AS DEVICE_TYPE_NAME, " + + "de.DEVICE_IDENTIFICATION, " + + "de.DEVICE_TYPE, " + "eom.STATUS, eom.CREATED_TIMESTAMP, " + "eom.UPDATED_TIMESTAMP, " + "op.OPERATION_CODE, " @@ -727,11 +721,9 @@ public class GenericOperationDAOImpl implements OperationDAO { + "op.INITIATED_BY FROM DM_ENROLMENT_OP_MAPPING AS eom " + "INNER JOIN DM_OPERATION AS op ON op.ID=eom.OPERATION_ID " + "INNER JOIN DM_ENROLMENT AS de ON de.ID=eom.ENROLMENT_ID " - + "INNER JOIN DM_DEVICE AS d ON d.ID=de.DEVICE_ID " - + "INNER JOIN DM_DEVICE_TYPE AS dt ON dt.ID=d.DEVICE_TYPE_ID " + "LEFT JOIN DM_DEVICE_OPERATION_RESPONSE AS dor ON dor.ENROLMENT_ID=de.id " + "AND dor.OPERATION_ID = eom.OPERATION_ID " - + "WHERE eom.OPERATION_ID = ? AND de.device_id = ? AND de.TENANT_ID = ?"; + + "WHERE eom.OPERATION_ID = ? AND de.DEVICE_ID = ? AND de.TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, operationId); @@ -756,7 +748,7 @@ public class GenericOperationDAOImpl implements OperationDAO { if (enrolmentId != rs.getInt("ENROLMENT_ID")) { activityStatus = new ActivityStatus(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(rs.getString("DEVICE_IDENTIFICATION"), - rs.getString("DEVICE_TYPE_NAME")); + rs.getString("DEVICE_TYPE")); activityStatus.setDeviceIdentifier(deviceIdentifier); activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); @@ -850,15 +842,13 @@ public class GenericOperationDAOImpl implements OperationDAO { " op.TYPE AS OPERATION_TYPE, " + " opm.STATUS, " + " en.DEVICE_ID, " + - " de.DEVICE_IDENTIFICATION, " + - " dt.NAME AS DEVICE_TYPE, " + + " en.DEVICE_IDENTIFICATION, " + + " en.DEVICE_TYPE, " + " de.TENANT_ID " + " FROM" + " DM_ENROLMENT_OP_MAPPING opm " + " INNER JOIN DM_OPERATION op ON opm.OPERATION_ID = op.ID " + " INNER JOIN DM_ENROLMENT en ON opm.ENROLMENT_ID = en.ID " + - " INNER JOIN DM_DEVICE de ON en.DEVICE_ID = de.ID " + - " INNER JOIN DM_DEVICE_TYPE dt ON dt.ID = de.DEVICE_TYPE_ID " + " WHERE " + " op.OPERATION_CODE = ? " + " AND de.TENANT_ID = ? " + diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 7aad6f8f2ea..a62aaada9eb 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -329,7 +329,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv if ((updateStatus > 0) || EnrolmentInfo.Status.REMOVED. equals(existingEnrolmentInfo.getStatus())) { enrollment = enrollmentDAO - .addEnrollment(existingDevice.getId(), newEnrolmentInfo, tenantId); + .addEnrollment(existingDevice.getId(), deviceIdentifier, + newEnrolmentInfo, tenantId); if (enrollment == null) { DeviceManagementDAOFactory.rollbackTransaction(); throw new DeviceManagementException( @@ -375,7 +376,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv if (type != null) { int deviceId = deviceDAO.addDevice(type.getId(), device, tenantId); device.setId(deviceId); - enrollment = enrollmentDAO.addEnrollment(deviceId, device.getEnrolmentInfo(), tenantId); + enrollment = enrollmentDAO.addEnrollment(deviceId, deviceIdentifier, device.getEnrolmentInfo(), tenantId); if (enrollment == null) { DeviceManagementDAOFactory.rollbackTransaction(); throw new DeviceManagementException( diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/dao/DeviceStatusPersistenceTests.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/dao/DeviceStatusPersistenceTests.java index 3019e94c4e2..6375f5b9dac 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/dao/DeviceStatusPersistenceTests.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/dao/DeviceStatusPersistenceTests.java @@ -17,6 +17,7 @@ */ package io.entgra.device.mgt.core.device.mgt.core.dao; +import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.testng.Assert; @@ -215,7 +216,8 @@ public class DeviceStatusPersistenceTests extends BaseDeviceManagementTest { EnrolmentInfo source = new EnrolmentInfo(owner, EnrolmentInfo.OwnerShip.BYOD, initialStatus); try { DeviceManagementDAOFactory.openConnection(); - EnrolmentInfo config = enrollmentDAO.addEnrollment(deviceId, source, tenantId); + EnrolmentInfo config = enrollmentDAO.addEnrollment(deviceId, + new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()), source, tenantId); device.setEnrolmentInfo(config); return config.getId(); } catch (DeviceManagementDAOException | SQLException e) { diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/dao/EnrolmentPersistenceTests.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/dao/EnrolmentPersistenceTests.java index c46f525c14b..c7b8de10b1e 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/dao/EnrolmentPersistenceTests.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/dao/EnrolmentPersistenceTests.java @@ -17,6 +17,7 @@ */ package io.entgra.device.mgt.core.device.mgt.core.dao; +import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.testng.Assert; @@ -58,7 +59,8 @@ public class EnrolmentPersistenceTests extends BaseDeviceManagementTest { DeviceDAO deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); deviceId = deviceDAO.addDevice(TestDataHolder.initialTestDeviceType.getId(), device, TestDataHolder.SUPER_TENANT_ID); device.setId(deviceId); - enrollmentDAO.addEnrollment(deviceId, source, TestDataHolder.SUPER_TENANT_ID); + enrollmentDAO.addEnrollment(deviceId, new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()), + source, TestDataHolder.SUPER_TENANT_ID); } catch (DeviceManagementDAOException | SQLException e) { log.error("Error occurred while adding enrollment", e); } finally { diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/sql/h2.sql b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/sql/h2.sql index 00c3c804a2c..aea2602f2ee 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/sql/h2.sql +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/sql/h2.sql @@ -149,6 +149,8 @@ CREATE TABLE IF NOT EXISTS DM_OPERATION ( CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( ID INTEGER AUTO_INCREMENT NOT NULL, DEVICE_ID INTEGER NOT NULL, + DEVICE_TYPE VARCHAR(300) NOT NULL, + DEVICE_IDENTIFICATION VARCHAR(300) NOT NULL, OWNER VARCHAR(50) NOT NULL, OWNERSHIP VARCHAR(45) DEFAULT NULL, STATUS VARCHAR(50) NULL, diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.extensions/src/test/resources/sql-files/h2.sql b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.extensions/src/test/resources/sql-files/h2.sql index 3dae0cb2616..d69d5e354b3 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.extensions/src/test/resources/sql-files/h2.sql +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.extensions/src/test/resources/sql-files/h2.sql @@ -79,6 +79,8 @@ CREATE TABLE IF NOT EXISTS DM_OPERATION ( CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( ID INTEGER AUTO_INCREMENT NOT NULL, DEVICE_ID INTEGER NOT NULL, + DEVICE_TYPE VARCHAR(300) NOT NULL, + DEVICE_IDENTIFICATION VARCHAR(300) NOT NULL, OWNER VARCHAR(50) NOT NULL, OWNERSHIP VARCHAR(45) DEFAULT NULL, STATUS VARCHAR(50) NULL, diff --git a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/test/java/io/entgra/device/mgt/core/policy/mgt/core/PolicyDAOTestCase.java b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/test/java/io/entgra/device/mgt/core/policy/mgt/core/PolicyDAOTestCase.java index d31916cf740..c6a7e0f03d1 100644 --- a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/test/java/io/entgra/device/mgt/core/policy/mgt/core/PolicyDAOTestCase.java +++ b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/test/java/io/entgra/device/mgt/core/policy/mgt/core/PolicyDAOTestCase.java @@ -102,7 +102,8 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest { DeviceManagementDAOFactory.beginTransaction(); for (Device device : devices) { int id = deviceDAO.addDevice(type.getId(), device, -1234); - enrollmentDAO.addEnrollment(id, device.getEnrolmentInfo(), -1234); + enrollmentDAO.addEnrollment(id, new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()), + device.getEnrolmentInfo(), -1234); } } catch (TransactionManagementException e) { log.error("Error occurred while adding device enrolment", e); diff --git a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql index 2a2d497eb8c..a9fa35a6fb0 100644 --- a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql +++ b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql @@ -99,6 +99,8 @@ DROP TABLE IF EXISTS DM_ENROLMENT; CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( ID INTEGER AUTO_INCREMENT NOT NULL, DEVICE_ID INTEGER NOT NULL, + DEVICE_TYPE VARCHAR(300) NOT NULL, + DEVICE_IDENTIFICATION VARCHAR(300) NOT NULL, OWNER VARCHAR(50) NOT NULL, OWNERSHIP VARCHAR(45) DEFAULT NULL, STATUS VARCHAR(50) NULL, diff --git a/components/subtype-mgt/io.entgra.device.mgt.core.subtype.mgt/src/test/resources/carbon-home/dbscripts/dm-db-h2.sql b/components/subtype-mgt/io.entgra.device.mgt.core.subtype.mgt/src/test/resources/carbon-home/dbscripts/dm-db-h2.sql index 295433b55c1..046cfbec1db 100644 --- a/components/subtype-mgt/io.entgra.device.mgt.core.subtype.mgt/src/test/resources/carbon-home/dbscripts/dm-db-h2.sql +++ b/components/subtype-mgt/io.entgra.device.mgt.core.subtype.mgt/src/test/resources/carbon-home/dbscripts/dm-db-h2.sql @@ -97,6 +97,8 @@ CREATE TABLE IF NOT EXISTS DM_OPERATION ( CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( ID INTEGER AUTO_INCREMENT NOT NULL, DEVICE_ID INTEGER NOT NULL, + DEVICE_TYPE VARCHAR(300) NOT NULL, + DEVICE_IDENTIFICATION VARCHAR(300) NOT NULL, OWNER VARCHAR(255) NOT NULL, OWNERSHIP VARCHAR(45) DEFAULT NULL, STATUS VARCHAR(50) NULL, diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql index 072ce0a81d9..f34d6183e26 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -100,6 +100,8 @@ CREATE TABLE IF NOT EXISTS DM_OPERATION ( CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( ID INTEGER AUTO_INCREMENT NOT NULL, DEVICE_ID INTEGER NOT NULL, + DEVICE_TYPE VARCHAR(300) NOT NULL, + DEVICE_IDENTIFICATION VARCHAR(300) NOT NULL, OWNER VARCHAR(255) NOT NULL, OWNERSHIP VARCHAR(45) DEFAULT NULL, STATUS VARCHAR(50) NULL, diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql index 6c7aa649fd5..dc05a394646 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql @@ -142,6 +142,8 @@ IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[D CREATE TABLE DM_ENROLMENT ( ID INTEGER IDENTITY(1,1) NOT NULL, DEVICE_ID INTEGER NOT NULL, + DEVICE_TYPE VARCHAR(300) NOT NULL, + DEVICE_IDENTIFICATION VARCHAR(300) NOT NULL, OWNER VARCHAR(255) NOT NULL, OWNERSHIP VARCHAR(45) DEFAULT NULL, STATUS VARCHAR(50) NULL, diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql index 065862a36be..9d3751e5d79 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql @@ -52,8 +52,8 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE ( ID INTEGER AUTO_INCREMENT NOT NULL, DESCRIPTION TEXT DEFAULT NULL, NAME VARCHAR(100) DEFAULT NULL, - DEVICE_TYPE_ID INT(11) DEFAULT NULL, - DEVICE_IDENTIFICATION VARCHAR(300) DEFAULT NULL, + DEVICE_TYPE_ID INT(11) NOT NULL, + DEVICE_IDENTIFICATION VARCHAR(300) NOT NULL, LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL, TENANT_ID INTEGER DEFAULT 0, PRIMARY KEY (ID), @@ -119,6 +119,8 @@ CREATE INDEX IDX_OP_INITIATED_BY ON DM_OPERATION (INITIATED_BY ASC); CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( ID INTEGER AUTO_INCREMENT NOT NULL, DEVICE_ID INTEGER NOT NULL, + DEVICE_TYPE VARCHAR(300) NOT NULL, + DEVICE_IDENTIFICATION VARCHAR(300) NOT NULL, OWNER VARCHAR(255) NOT NULL, OWNERSHIP VARCHAR(45) DEFAULT NULL, STATUS VARCHAR(50) NULL, @@ -130,6 +132,11 @@ 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 )ENGINE = InnoDB; +CREATE INDEX IDX_ENROLMENT_FK_DEVICE_ID ON DM_ENROLMENT(DEVICE_ID); +CREATE INDEX IDX_ENROLMENT_DEVICE_TYPE_ID ON DM_ENROLMENT(DEVICE_TYPE_ID); +CREATE INDEX IDX_ENROLMENT_DEVICE_IDENTIFICATION ON DM_ENROLMENT(DEVICE_IDENTIFICATION); +CREATE INDEX IDX_ENROLMENT_STATUS ON DM_ENROLMENT(STATUS); + CREATE TABLE IF NOT EXISTS DM_DEVICE_STATUS ( ID INTEGER AUTO_INCREMENT NOT NULL, ENROLMENT_ID INTEGER NOT NULL, @@ -143,9 +150,6 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_STATUS ( CONSTRAINT FK_DM_DEVICE_STATUS_ENROLMENT FOREIGN KEY (ENROLMENT_ID) REFERENCES DM_ENROLMENT (ID) ON DELETE CASCADE ON UPDATE CASCADE )ENGINE = InnoDB; -CREATE INDEX IDX_ENROLMENT_FK_DEVICE_ID ON DM_ENROLMENT(DEVICE_ID); -CREATE INDEX IDX_ENROLMENT_DEVICE_ID_TENANT_ID ON DM_ENROLMENT(DEVICE_ID, TENANT_ID); -CREATE INDEX IDX_ENROLMENT_DEVICE_ID_TENANT_ID_STATUS ON DM_ENROLMENT(DEVICE_ID, TENANT_ID, STATUS); CREATE TABLE IF NOT EXISTS DM_ENROLMENT_OP_MAPPING ( ID INTEGER AUTO_INCREMENT NOT NULL, diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql index eccc831141c..e500c550466 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql @@ -205,6 +205,8 @@ WHEN (NEW.ID IS NULL) CREATE TABLE DM_ENROLMENT ( ID NUMBER(10) NOT NULL, DEVICE_ID NUMBER(10) NOT NULL, + DEVICE_TYPE VARCHAR2(300) NOT NULL, + DEVICE_IDENTIFICATION VARCHAR2(300) NOT NULL, OWNER VARCHAR2(255) NOT NULL, OWNERSHIP VARCHAR2(45) DEFAULT NULL, STATUS VARCHAR2(50) NULL, diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql index 371baa0763a..c0368f58266 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql @@ -114,6 +114,8 @@ CREATE SEQUENCE DM_ENROLMENT_seq; CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( ID INTEGER DEFAULT NEXTVAL ('DM_ENROLMENT_seq') NOT NULL, DEVICE_ID INTEGER NOT NULL, + DEVICE_TYPE VARCHAR(300) NOT NULL, + DEVICE_IDENTIFICATION VARCHAR(300) NOT NULL, OWNER VARCHAR(50) NOT NULL, OWNERSHIP VARCHAR(45) DEFAULT NULL, STATUS VARCHAR(50) NULL,