From 7dea32d2e34631994a0c0d7488b53a467f4f3aba Mon Sep 17 00:00:00 2001 From: Charitha Goonetilleke Date: Wed, 15 May 2024 10:19:00 +0530 Subject: [PATCH] Performance improvements --- .../mgt/core/device/mgt/common/Device.java | 11 + .../pom.xml | 8 +- .../core/device/mgt/core/dao/DeviceDAO.java | 3 + .../core/dao/impl/AbstractDeviceDAOImpl.java | 301 ++-- .../core/dao/impl/AbstractGroupDAOImpl.java | 27 +- .../dao/impl/device/GenericDeviceDAOImpl.java | 393 +++-- .../dao/impl/device/OracleDeviceDAOImpl.java | 1434 +---------------- .../dao/impl/group/GenericGroupDAOImpl.java | 17 +- .../dao/impl/group/OracleGroupDAOImpl.java | 15 +- .../impl/group/PostgreSQLGroupDAOImpl.java | 15 +- .../dao/impl/group/SQLServerGroupDAOImpl.java | 13 +- .../dao/util/DeviceManagementDAOUtil.java | 1 + .../impl/DeviceInformationManagerImpl.java | 7 +- .../DeviceManagementProviderService.java | 2 + .../DeviceManagementProviderServiceImpl.java | 68 +- .../mgt/core/dao/DevicePersistTests.java | 59 + 16 files changed, 571 insertions(+), 1803 deletions(-) diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/Device.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/Device.java index ffd31e1f33..b75f557077 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/Device.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/Device.java @@ -57,6 +57,9 @@ public class Device implements Serializable { required = true) private String deviceIdentifier; + @ApiModelProperty(name = "updatedTimeStamp", value = "Last updated timestamp of the device.") + private long lastUpdatedTimeStamp; + @ApiModelProperty(name = "enrolmentInfo", value = "This defines the device registration related information. " + "It is mandatory to define this information.", required = true) private EnrolmentInfo enrolmentInfo; @@ -221,6 +224,14 @@ public class Device implements Serializable { this.historySnapshot = historySnapshot; } + public long getLastUpdatedTimeStamp() { + return lastUpdatedTimeStamp; + } + + public void setLastUpdatedTimeStamp(long lastUpdatedTimeStamp) { + this.lastUpdatedTimeStamp = lastUpdatedTimeStamp; + } + public static class Property { private String name; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/pom.xml b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/pom.xml index 9827ec7084..fe7df106e3 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/pom.xml +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/pom.xml @@ -137,10 +137,10 @@ src/test/resources/testng.xml - src/test/resources/mysql-testng.xml - src/test/resources/mssql-testng.xml - src/test/resources/oracle-testng.xml - src/test/resources/postgre-testng.xml + + + + 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 0fd55c870d..731bb11d59 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 @@ -122,6 +122,9 @@ public interface DeviceDAO { */ boolean updateDevice(Device device, int tenantId) throws DeviceManagementDAOException; + boolean recordDeviceUpdate(DeviceIdentifier deviceIdentifier, int tenantId) + throws DeviceManagementDAOException; + Device getDevice(DeviceData deviceData, 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 ba1cb3cf7f..7ab1cdb6e3 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 @@ -108,7 +108,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { "WHERE DEVICE_TYPE_ID = (SELECT ID FROM DM_DEVICE_TYPE " + "WHERE NAME = ? AND (PROVIDER_TENANT_ID = ? OR SHARED_WITH_ALL_TENANTS = ?)) " + "AND DEVICE_IDENTIFICATION = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql, new String[]{"id"}); + stmt = conn.prepareStatement(sql); stmt.setString(1, device.getName()); stmt.setString(2, device.getDescription()); stmt.setTimestamp(3, new Timestamp(new Date().getTime())); @@ -127,6 +127,30 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } } + @Override + public boolean recordDeviceUpdate(DeviceIdentifier deviceIdentifier, int tenantId) + throws DeviceManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + int rows; + try { + conn = this.getConnection(); + String sql = "UPDATE DM_DEVICE SET LAST_UPDATED_TIMESTAMP = ? " + + "WHERE DEVICE_IDENTIFICATION = ? AND TENANT_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setTimestamp(1, new Timestamp(new Date().getTime())); + stmt.setString(2, deviceIdentifier.getId()); + stmt.setInt(3, tenantId); + rows = stmt.executeUpdate(); + return (rows > 0); + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while updating device update timestamp of " + + " device '" + deviceIdentifier + "'", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, null); + } + } + @Override public Device getDevice(DeviceData deviceData, int tenantId) throws DeviceManagementDAOException { Device device = null; @@ -136,6 +160,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { + "d1.ID AS DEVICE_ID, " + "d1.DESCRIPTION, " + "d1.NAME AS DEVICE_NAME, " + + "d1.LAST_UPDATED_TIMESTAMP, " + "e.DEVICE_TYPE, " + "e.DEVICE_IDENTIFICATION, " + "e.OWNER, " @@ -149,7 +174,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { + "(SELECT " + "d.ID, " + "d.DESCRIPTION, " - + "d.NAME " + + "d.NAME, " + + "d.LAST_UPDATED_TIMESTAMP " + "FROM DM_DEVICE d" + "d.DEVICE_IDENTIFICATION = ? AND " + "d.TENANT_ID = ?"; @@ -208,9 +234,11 @@ 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, e.DEVICE_TYPE, " + + String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, " + + "d1.LAST_UPDATED_TIMESTAMP, 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 " + + "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, " + + "(SELECT d.ID, d.DESCRIPTION, d.NAME, d.LAST_UPDATED_TIMESTAMP " + "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"; @@ -248,12 +276,12 @@ 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, 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 " + + "d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.LAST_UPDATED_TIMESTAMP, " + + "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 " + + " (SELECT d.ID, d.DESCRIPTION, d.NAME, d.LAST_UPDATED_TIMESTAMP " + "FROM DM_DEVICE d " + "WHERE d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ?) d1 " + "WHERE " + @@ -293,10 +321,11 @@ 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, e.DEVICE_TYPE, " + - "e.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, " + + "d1.LAST_UPDATED_TIMESTAMP, 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 " + + "(SELECT d.ID, d.DESCRIPTION, d.NAME, d.LAST_UPDATED_TIMESTAMP " + "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"; @@ -328,9 +357,11 @@ 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, 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 " + + String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, " + + "d1.LAST_UPDATED_TIMESTAMP, 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.LAST_UPDATED_TIMESTAMP " + "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"; @@ -520,12 +551,13 @@ 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, e.DEVICE_TYPE, " + - "e.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, " + + "d1.LAST_UPDATED_TIMESTAMP, 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 " + + "(SELECT d.ID, d.DESCRIPTION, d.NAME, d.LAST_UPDATED_TIMESTAMP " + "FROM" + " DM_DEVICE d " + "WHERE " + @@ -563,9 +595,11 @@ 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, 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 " + + String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, " + + "d1.LAST_UPDATED_TIMESTAMP, 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.LAST_UPDATED_TIMESTAMP " + "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 = ? " + @@ -599,9 +633,11 @@ 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, 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 " + + String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, " + + "d1.LAST_UPDATED_TIMESTAMP, 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.LAST_UPDATED_TIMESTAMP " + "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"; @@ -632,9 +668,10 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { String sql = "SELECT d1.ID AS DEVICE_ID, " + "d1.DESCRIPTION, " + "d1.NAME AS DEVICE_NAME, " + + "d1.LAST_UPDATED_TIMESTAMP, " + "e.DEVICE_TYPE, " + "e.TENANT_ID, " + - "d1.DEVICE_IDENTIFICATION, " + + "e.DEVICE_IDENTIFICATION, " + "e.OWNER, " + "e.OWNERSHIP, " + "e.STATUS, " + @@ -646,7 +683,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { "(SELECT d.ID, " + "d.DESCRIPTION, " + "d.NAME, " + - "d.DEVICE_IDENTIFICATION " + + "d.DEVICE_IDENTIFICATION, " + + "d.LAST_UPDATED_TIMESTAMP " + "FROM DM_DEVICE d " + "WHERE d.DEVICE_IDENTIFICATION = ?) d1 " + "WHERE d1.ID = e.DEVICE_ID AND e.DEVICE_TYPE = ? " + @@ -684,9 +722,11 @@ 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, 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 " + + String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, " + + "d1.LAST_UPDATED_TIMESTAMP, 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.LAST_UPDATED_TIMESTAMP " + "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); @@ -714,10 +754,11 @@ 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, 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 FROM DM_DEVICE d WHERE d.TENANT_ID = ?) d1 " + + String sql = "SELECT d1.DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.LAST_UPDATED_TIMESTAMP, " + + "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.LAST_UPDATED_TIMESTAMP 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); @@ -745,10 +786,11 @@ 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, 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 e.DEVICE_TYPE = ? AND d1.ID = e.DEVICE_ID " + + String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.LAST_UPDATED_TIMESTAMP, " + + "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.LAST_UPDATED_TIMESTAMP 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.setInt(1, tenantId); @@ -780,6 +822,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { String sql = "SELECT d1.ID AS DEVICE_ID," + " d1.DESCRIPTION," + " d1.NAME AS DEVICE_NAME," + + " d1.LAST_UPDATED_TIMESTAMP, " + " e.DEVICE_TYPE," + " e.DEVICE_IDENTIFICATION," + " e.OWNER," + @@ -792,7 +835,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { "FROM DM_ENROLMENT e," + " (SELECT d.ID," + " d.DESCRIPTION," + - " d.NAME" + + " d.NAME," + + " d.LAST_UPDATED_TIMESTAMP" + " FROM DM_DEVICE d" + " WHERE d.TENANT_ID = ?) d1 " + "WHERE d1.ID = e.DEVICE_ID" + @@ -840,7 +884,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { 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, e1.DEVICE_TYPE FROM DM_DEVICE d, " + + "d.DEVICE_IDENTIFICATION, d.LAST_UPDATED_TIMESTAMP, 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 " + @@ -874,7 +918,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { 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, e1.DEVICE_TYPE FROM DM_DEVICE d, " + + "d.DEVICE_IDENTIFICATION, d.LAST_UPDATED_TIMESTAMP, 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 " + @@ -919,6 +963,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { + "d.DESCRIPTION, " + "d.NAME AS DEVICE_NAME, " + "d.DEVICE_IDENTIFICATION, " + + "d.LAST_UPDATED_TIMESTAMP, " + "e.DEVICE_TYPE " + "FROM " + "DM_DEVICE d, " @@ -1082,7 +1127,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } } - private Connection getConnection() throws SQLException { + protected Connection getConnection() throws SQLException { return DeviceManagementDAOFactory.getConnection(); } @@ -1669,9 +1714,10 @@ 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, 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, " + + String sql = "SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, d.LAST_UPDATED_TIMESTAMP, " + + "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 = ?"; @@ -1735,8 +1781,9 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { try { conn = this.getConnection(); 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, " + + "d1.DEVICE_IDENTIFICATION, d1.LAST_UPDATED_TIMESTAMP, 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, 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 = ?"; @@ -1798,6 +1845,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { "MIN(DEVICE.ID) AS DEVICE_ID, " + "MIN(DEVICE.NAME) AS DEVICE_NAME, " + "MIN(DEVICE.DESCRIPTION) AS DESCRIPTION, " + + "MAX(DEVICE.LAST_UPDATED_TIMESTAMP) AS LAST_UPDATED_TIMESTAMP, " + "MIN(ENROLMENT.DEVICE_TYPE) AS DEVICE_TYPE, " + "MIN(DEVICE.DEVICE_IDENTIFICATION) AS DEVICE_IDENTIFICATION, " + "MIN(ENROLMENT.ID) AS ENROLMENT_ID, " + @@ -1957,12 +2005,12 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { StringJoiner joiner = new StringJoiner(",", "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 " + + "d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.LAST_UPDATED_TIMESTAMP, " + + "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 " + + "(SELECT d.ID, d.DESCRIPTION, d.NAME, d.LAST_UPDATED_TIMESTAMP " + "FROM DM_DEVICE d " + "WHERE " + "d.DEVICE_IDENTIFICATION IN (", @@ -2016,11 +2064,12 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { StringJoiner joiner = new StringJoiner(",", "SELECT " + "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, " + + "d1.DEVICE_IDENTIFICATION, d1.LAST_UPDATED_TIMESTAMP, 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 " + + "(SELECT d.ID, d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, d.LAST_UPDATED_TIMESTAMP " + "FROM DM_DEVICE d " + "WHERE " + " d.DEVICE_IDENTIFICATION IN (", @@ -2191,6 +2240,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { "d.NAME AS DEVICE_NAME, " + "e.DEVICE_TYPE, " + "d.DEVICE_IDENTIFICATION, " + + "d.LAST_UPDATED_TIMESTAMP, " + "e.OWNER, " + "e.OWNERSHIP, " + "e.STATUS, " + @@ -2307,6 +2357,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { "d.description," + "d.NAME AS DEVICE_NAME," + "d.device_identification," + + "d.LAST_UPDATED_TIMESTAMP, " + "e1.DEVICE_TYPE " + "FROM dm_device d," + "(SELECT e.owner," + @@ -2763,42 +2814,6 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } } - /*** - * This method updates the status of a given list of devices to DELETED state in the DM_DEVICE_STATUS table - * @param conn Connection object - * @param validDevices list of devices - * @throws DeviceManagementDAOException if updating fails - */ - public void refactorDeviceStatus(Connection conn, List validDevices) throws DeviceManagementDAOException { - String updateQuery = "UPDATE DM_DEVICE_STATUS SET STATUS = ? WHERE ID = ?"; - String selectLastMatchingRecordQuery = "SELECT ID FROM DM_DEVICE_STATUS WHERE ENROLMENT_ID = ? AND DEVICE_ID = ? ORDER BY ID DESC LIMIT 1"; - - try (PreparedStatement selectStatement = conn.prepareStatement(selectLastMatchingRecordQuery); - PreparedStatement updateStatement = conn.prepareStatement(updateQuery)) { - - for (Device device : validDevices) { - - selectStatement.setInt(1, device.getEnrolmentInfo().getId()); - selectStatement.setInt(2, device.getId()); - - ResultSet resultSet = selectStatement.executeQuery(); - int lastRecordId = 0; - if (resultSet.next()) { - lastRecordId = resultSet.getInt("ID"); - } - - updateStatement.setString(1, String.valueOf(Status.DELETED)); - updateStatement.setInt(2, lastRecordId); - updateStatement.execute(); - } - - } catch (SQLException e) { - String msg = "SQL error occurred while updating device status properties."; - log.error(msg, e); - throw new DeviceManagementDAOException(msg, e); - } - } - private String generateRandomString(int length) { String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; StringBuilder randomString = new StringBuilder(); @@ -3001,107 +3016,6 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } } - @Override - public List getGroupedDevicesDetails(PaginationRequest request, List deviceIds, String groupName, - int tenantId) throws DeviceManagementDAOException { - int limitValue = request.getRowCount(); - int offsetValue = request.getStartIndex(); - List status = request.getStatusList(); - String name = request.getDeviceName(); - String user = request.getOwner(); - String ownership = request.getOwnership(); - try { - List devices = new ArrayList<>(); - if (deviceIds.isEmpty()) { - return devices; - } - Connection conn = this.getConnection(); - int index = 1; - StringJoiner joiner = new StringJoiner(",", - "SELECT " - + "DM_DEVICE.ID AS DEVICE_ID, " - + "DM_DEVICE.NAME AS DEVICE_NAME, " - + "DM_DEVICE.DESCRIPTION AS DESCRIPTION, " - + "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 " - + "FROM DM_DEVICE_GROUP_MAP " - + "INNER JOIN DM_DEVICE ON " - + "DM_DEVICE_GROUP_MAP.DEVICE_ID = DM_DEVICE.ID " - + "INNER JOIN DM_GROUP ON " - + "DM_DEVICE_GROUP_MAP.GROUP_ID = DM_GROUP.ID " - + "INNER JOIN DM_ENROLMENT e ON " - + "DM_DEVICE.ID = e.DEVICE_ID AND " - + "DM_DEVICE.TENANT_ID = e.TENANT_ID " - + "WHERE DM_DEVICE.ID IN (", - ") AND DM_DEVICE.TENANT_ID = ?"); - - deviceIds.stream().map(ignored -> "?").forEach(joiner::add); - String query = joiner.toString(); - if (StringUtils.isNotBlank(groupName)) { - query += " AND DM_GROUP.GROUP_NAME = ?"; - } - if (StringUtils.isNotBlank(name)) { - query += " AND DM_DEVICE.NAME LIKE ?"; - } - if (StringUtils.isNotBlank(user)) { - query += " AND e.OWNER = ?"; - } - if (StringUtils.isNotBlank(ownership)) { - query += " AND e.OWNERSHIP = ?"; - } - if (status != null && !status.isEmpty()) { - query += buildStatusQuery(status); - } - - query += "LIMIT ? OFFSET ?"; - - try (PreparedStatement ps = conn.prepareStatement(query)) { - for (Integer deviceId : deviceIds) { - ps.setInt(index++, deviceId); - } - ps.setInt(index++, tenantId); - if (StringUtils.isNotBlank(groupName)) { - ps.setString(index++, groupName); - } - if (StringUtils.isNotBlank(name)) { - ps.setString(index++, name); - } - if (StringUtils.isNotBlank(user)) { - ps.setString(index++, user); - } - if (StringUtils.isNotBlank(ownership)) { - ps.setString(index++, ownership); - } - if (status != null && !status.isEmpty()) { - for (String deviceStatus : status) { - ps.setString(index++, deviceStatus); - } - } - ps.setInt(index++, limitValue); - ps.setInt(index, offsetValue); - - try (ResultSet rs = ps.executeQuery()) { - while (rs.next()) { - devices.add(DeviceManagementDAOUtil.loadDevice(rs)); - } - return devices; - } - } - } catch (SQLException e) { - String msg = "Error occurred while retrieving information of all registered devices " + - "according to device ids and the limit area."; - log.error(msg, e); - throw new DeviceManagementDAOException(msg, e); - } - } - @Override public int getGroupedDevicesCount(PaginationRequest request, List deviceIds, String groupName, int tenantId) throws DeviceManagementDAOException { @@ -3216,7 +3130,8 @@ 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, e.DEVICE_TYPE, " + + "e.DATE_OF_LAST_UPDATE, e.IS_TRANSFERRED, e.STATUS, d.DEVICE_NAME, d.DESCRIPTION, " + + "d.LAST_UPDATED_TIMESTAMP, 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') " + @@ -3247,8 +3162,8 @@ 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, e.DEVICE_TYPE, " + - "e.DEVICE_IDENTIFICATION " + + "e.DATE_OF_LAST_UPDATE, e.IS_TRANSFERRED, e.STATUS, d.DEVICE_NAME, d.DESCRIPTION, " + + "d.LAST_UPDATED_TIMESTAMP, 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 = ?"; @@ -3273,4 +3188,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } return devices; } + + public abstract void refactorDeviceStatus (Connection conn, List validDevices) + 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/AbstractGroupDAOImpl.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/AbstractGroupDAOImpl.java index b9c655c4ba..374c1ca0aa 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/AbstractGroupDAOImpl.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/AbstractGroupDAOImpl.java @@ -1282,9 +1282,10 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { StringJoiner joiner = new StringJoiner(",","SELECT " + "d1.DEVICE_ID, " + "d1.DESCRIPTION, " - + "d1.NAME AS DEVICE_NAME, " + + "e.DEVICE_NAME, " + "d1.DEVICE_TYPE, " + "d1.DEVICE_IDENTIFICATION, " + + "d1.LAST_UPDATED_TIMESTAMP, " + "e.OWNER, " + "e.OWNERSHIP, " + "e.STATUS, " @@ -1294,15 +1295,14 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { + "e.ID AS ENROLMENT_ID " + "FROM " + "DM_ENROLMENT e, " - + "(SELECT gd.DEVICE_ID, gd.DESCRIPTION, gd.NAME, gd.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE " + + "(SELECT gd.DEVICE_ID, gd.DESCRIPTION, gd.NAME, gd.DEVICE_IDENTIFICATION, gd.LAST_UPDATED_TIMESTAMP " + "FROM " - + "(SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, d.DEVICE_TYPE_ID " + + "(SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, d.LAST_UPDATED_TIMESTAMP " + "FROM DM_DEVICE d, " + "(SELECT dgm.DEVICE_ID " + "FROM DM_DEVICE_GROUP_MAP dgm " + "WHERE dgm.GROUP_ID = (SELECT ID FROM DM_GROUP WHERE GROUP_NAME = ? AND TENANT_ID = ?)) dgm1 " - + "WHERE d.ID = dgm1.DEVICE_ID AND d.TENANT_ID = ?) gd, DM_DEVICE_TYPE t " - + "WHERE gd.DEVICE_TYPE_ID = t.ID) d1 " + + "WHERE d.ID = dgm1.DEVICE_ID AND d.TENANT_ID = ?) gd) d1 " + "WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? AND e.STATUS IN (", ")"); @@ -1343,9 +1343,10 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { String sql = "SELECT " + "d1.DEVICE_ID, " + "d1.DESCRIPTION, " - + "d1.NAME AS DEVICE_NAME, " + + "e.DEVICE_NAME, " + "d1.DEVICE_TYPE, " + "d1.DEVICE_IDENTIFICATION, " + + "d1.LAST_UPDATED_TIMESTAMP, " + "e.OWNER, " + "e.OWNERSHIP, " + "e.STATUS, " @@ -1355,15 +1356,14 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { + "e.ID AS ENROLMENT_ID " + "FROM " + "DM_ENROLMENT e, " - + "(SELECT gd.DEVICE_ID, gd.DESCRIPTION, gd.NAME, gd.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE " + + "(SELECT gd.DEVICE_ID, gd.DESCRIPTION, gd.NAME, gd.DEVICE_IDENTIFICATION, gd.LAST_UPDATED_TIMESTAMP " + "FROM " - + "(SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, d.DEVICE_TYPE_ID " + + "(SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, d.LAST_UPDATED_TIMESTAMP " + "FROM DM_DEVICE d, " + "(SELECT dgm.DEVICE_ID " + "FROM DM_DEVICE_GROUP_MAP dgm " + "WHERE dgm.GROUP_ID = (SELECT ID FROM DM_GROUP WHERE GROUP_NAME = ? AND TENANT_ID = ?)) dgm1 " - + "WHERE d.ID = dgm1.DEVICE_ID AND d.TENANT_ID = ?) gd, DM_DEVICE_TYPE t " - + "WHERE gd.DEVICE_TYPE_ID = t.ID) d1 " + + "WHERE d.ID = dgm1.DEVICE_ID AND d.TENANT_ID = ?) gd) d1 " + "WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { @@ -1396,9 +1396,10 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { StringJoiner sql = new StringJoiner(",", "SELECT DEVICE.ID AS DEVICE_ID, " + "DEVICE.NAME AS DEVICE_NAME, " + - "DEVICE_TYPE.NAME AS DEVICE_TYPE, " + + "ENROLMENT.DEVICE_TYPE, " + "DEVICE.DESCRIPTION, " + "DEVICE.DEVICE_IDENTIFICATION, " + + "DEVICE.LAST_UPDATED_TIMESTAMP, " + "ENROLMENT.ID AS ENROLMENT_ID, " + "ENROLMENT.OWNER, " + "ENROLMENT.OWNERSHIP, " + @@ -1406,9 +1407,9 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { "ENROLMENT.DATE_OF_LAST_UPDATE, " + "ENROLMENT.STATUS, " + "ENROLMENT.IS_TRANSFERRED " + - "FROM DM_DEVICE AS DEVICE, DM_DEVICE_TYPE AS DEVICE_TYPE, DM_ENROLMENT " + + "FROM DM_DEVICE AS DEVICE, DM_ENROLMENT " + "AS ENROLMENT " + - "WHERE DEVICE_TYPE.NAME = ? AND DEVICE.ID " + + "WHERE ENROLMENT.DEVICE_TYPE = ? AND DEVICE.ID " + "NOT IN " + "(SELECT DEVICE_ID " + "FROM DM_DEVICE_GROUP_MAP " + 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/device/GenericDeviceDAOImpl.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/device/GenericDeviceDAOImpl.java index 7d7fe4bf5b..efe07d1335 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/device/GenericDeviceDAOImpl.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/device/GenericDeviceDAOImpl.java @@ -18,6 +18,7 @@ package io.entgra.device.mgt.core.device.mgt.core.dao.impl.device; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import io.entgra.device.mgt.core.device.mgt.common.Count; @@ -27,7 +28,6 @@ import io.entgra.device.mgt.core.device.mgt.common.EnrolmentInfo; import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest; import io.entgra.device.mgt.core.device.mgt.common.device.details.DeviceInfo; 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.AbstractDeviceDAOImpl; import io.entgra.device.mgt.core.device.mgt.core.dao.util.DeviceManagementDAOUtil; import io.entgra.device.mgt.core.device.mgt.core.report.mgt.Constants; @@ -71,14 +71,14 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { String serial = request.getSerialNumber(); boolean isSerialProvided = false; - try { Connection conn = getConnection(); String sql = "SELECT d1.ID AS DEVICE_ID, " + "d1.DESCRIPTION, " + "d1.NAME AS DEVICE_NAME, " + - "d1.DEVICE_TYPE, " + + "e.DEVICE_TYPE, " + "d1.DEVICE_IDENTIFICATION, " + + "d1.LAST_UPDATED_TIMESTAMP, " + "e.OWNER, " + "e.OWNERSHIP, " + "e.STATUS, " + @@ -90,15 +90,13 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { "(SELECT d.ID, " + "d.DESCRIPTION, " + "d.NAME, " + - "d.DEVICE_IDENTIFICATION, " + - "t.NAME AS DEVICE_TYPE "; + "d.LAST_UPDATED_TIMESTAMP, " + + "d.DEVICE_IDENTIFICATION "; //Filter by serial number or any Custom Property in DM_DEVICE_INFO if ((serial != null) || (request.getCustomProperty() != null && !request.getCustomProperty().isEmpty())) { sql = sql + - "FROM DM_DEVICE d " + - "INNER JOIN DM_DEVICE_TYPE t ON d.DEVICE_TYPE_ID = t.ID " + - "WHERE "; + "FROM DM_DEVICE d WHERE "; if (serial != null) { sql += "EXISTS (" + "SELECT VALUE_FIELD " + @@ -128,24 +126,24 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { } sql += "AND d.TENANT_ID = ? "; } else { - sql = sql + "FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ? "; + sql = sql + "FROM DM_DEVICE d WHERE d.TENANT_ID = ? "; } //Add query for last updated timestamp if (since != null) { sql = sql + " AND d.LAST_UPDATED_TIMESTAMP > ?"; isSinceProvided = true; } - //Add the query for device-type - if (deviceType != null && !deviceType.isEmpty()) { - sql = sql + " AND t.NAME = ?"; - isDeviceTypeProvided = true; - } //Add the query for device-name 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 = ?"; + //Add the query for device-type + if (deviceType != null && !deviceType.isEmpty()) { + sql = sql + " AND e.DEVICE_TYPE = ?"; + isDeviceTypeProvided = true; + } //Add the query for ownership if (ownership != null && !ownership.isEmpty()) { sql = sql + " AND e.OWNERSHIP = ?"; @@ -163,7 +161,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { sql += buildStatusQuery(statusList); isStatusProvided = true; } - sql = sql + " LIMIT ?,?"; + sql = sql + " LIMIT ? OFFSET ?"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { int paramIdx = 1; @@ -179,13 +177,13 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { if (isSinceProvided) { stmt.setTimestamp(paramIdx++, new Timestamp(since.getTime())); } - if (isDeviceTypeProvided) { - stmt.setString(paramIdx++, deviceType); - } if (isDeviceNameProvided) { stmt.setString(paramIdx++, "%" + deviceName + "%"); } stmt.setInt(paramIdx++, tenantId); + if (isDeviceTypeProvided) { + stmt.setString(paramIdx++, deviceType); + } if (isOwnershipProvided) { stmt.setString(paramIdx++, ownership); } @@ -199,8 +197,8 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { stmt.setString(paramIdx++, status); } } - stmt.setInt(paramIdx++, request.getStartIndex()); - stmt.setInt(paramIdx, request.getRowCount()); + stmt.setInt(paramIdx++, request.getRowCount()); + stmt.setInt(paramIdx, request.getStartIndex()); try (ResultSet rs = stmt.executeQuery()) { devices = new ArrayList<>(); @@ -230,6 +228,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { "DESCRIPTION, " + "NAME, " + "DATE_OF_ENROLMENT, " + + "LAST_UPDATED_TIMESTAMP, " + "STATUS, " + "DATE_OF_LAST_UPDATE, " + "TIMESTAMPDIFF(DAY, ?, DATE_OF_ENROLMENT) as DAYS_SINCE_ENROLLED " + @@ -271,6 +270,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { "NAME, " + "DATE_OF_ENROLMENT, " + "DATE_OF_LAST_UPDATE, " + + "d1.LAST_UPDATED_TIMESTAMP, " + "STATUS, " + "TIMESTAMPDIFF(DAY, DATE_OF_LAST_UPDATE, DATE_OF_ENROLMENT) AS DAYS_USED " + "from DM_DEVICE d, DM_ENROLMENT e " + @@ -314,6 +314,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { "DESCRIPTION, " + "NAME, " + "DATE_OF_ENROLMENT, " + + "LAST_UPDATED_TIMESTAMP, " + "STATUS, " + "DATE_OF_LAST_UPDATE, " + "TIMESTAMPDIFF(DAY, ?, ?) as DAYS_SINCE_ENROLLED " + @@ -355,6 +356,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { "NAME, " + "DATE_OF_ENROLMENT, " + "DATE_OF_LAST_UPDATE, " + + "LAST_UPDATED_TIMESTAMP, " + "STATUS, " + "TIMESTAMPDIFF(DAY, DATE_OF_LAST_UPDATE, ?) AS DAYS_USED " + "from DM_DEVICE d, DM_ENROLMENT e " + @@ -399,8 +401,9 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { "DM_DEVICE.ID AS DEVICE_ID, " + "DEVICE_IDENTIFICATION, " + "DESCRIPTION, " + + "LAST_UPDATED_TIMESTAMP, " + "DM_DEVICE.NAME AS DEVICE_NAME, " + - "DM_DEVICE_TYPE.NAME AS DEVICE_TYPE, " + + "DEVICE_TYPE, " + "DM_ENROLMENT.ID AS ENROLMENT_ID, " + "DATE_OF_ENROLMENT, " + "OWNER, " + @@ -412,7 +415,6 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { "FROM " + "DM_DEVICE " + "JOIN DM_ENROLMENT ON (DM_DEVICE.ID = DM_ENROLMENT.DEVICE_ID) " + - "JOIN DM_DEVICE_TYPE ON (DM_DEVICE.DEVICE_TYPE_ID = DM_DEVICE_TYPE.ID) " + "WHERE " + "DM_ENROLMENT.TENANT_ID = ? "; stmt = conn.prepareStatement(sql); @@ -448,6 +450,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { String sql = "SELECT " + "d1.ID AS DEVICE_ID, " + "d1.DEVICE_IDENTIFICATION, " + + "d1.LAST_UPDATED_TIMESTAMP, " + "e.STATUS, " + "e.OWNER, " + "e.IS_TRANSFERRED, " + @@ -531,8 +534,9 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { String sql = "SELECT d1.ID AS DEVICE_ID, " + "d1.DESCRIPTION, " + "d1.NAME AS DEVICE_NAME, " + - "d1.DEVICE_TYPE, " + + "e.DEVICE_TYPE, " + "d1.DEVICE_IDENTIFICATION, " + + "d1.LAST_UPDATED_TIMESTAMP, " + "e.OWNER, " + "e.OWNERSHIP, " + "e.STATUS, " + @@ -545,29 +549,29 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { "d.DESCRIPTION, " + "d.NAME, " + "d.DEVICE_IDENTIFICATION, " + - "t.NAME AS DEVICE_TYPE " + - "FROM DM_DEVICE d, DM_DEVICE_TYPE t "; + "d.LAST_UPDATED_TIMESTAMP " + + "FROM DM_DEVICE d "; //Add the query to filter active devices on timestamp if (since != null) { sql = sql + ", DM_DEVICE_DETAIL dt"; isSinceProvided = true; } - sql = sql + " WHERE DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?"; + sql = sql + " WHERE d.TENANT_ID = ?"; //Add query for last updated timestamp if (isSinceProvided) { sql = sql + " AND dt.DEVICE_ID = d.ID AND dt.UPDATE_TIMESTAMP > ?"; } - //Add the query for device-type - if (deviceType != null && !deviceType.isEmpty()) { - sql = sql + " AND t.NAME = ?"; - isDeviceTypeProvided = true; - } //Add the query for device-name 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 = ?"; + //Add the query for device-type + if (deviceType != null && !deviceType.isEmpty()) { + sql = sql + " AND e.DEVICE_TYPE = ?"; + isDeviceTypeProvided = true; + } //Add the query for ownership if (ownership != null && !ownership.isEmpty()) { sql = sql + " AND e.OWNERSHIP = ?"; @@ -589,7 +593,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { sql = sql + " AND MOD(d1.ID, ?) = ?"; isPartitionedTask = true; } - sql = sql + " LIMIT ?,?"; + sql = sql + " LIMIT ? OFFSET ?"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { int paramIdx = 1; @@ -597,13 +601,13 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { if (isSinceProvided) { stmt.setLong(paramIdx++, since.getTime()); } - if (isDeviceTypeProvided) { - stmt.setString(paramIdx++, deviceType); - } if (isDeviceNameProvided) { stmt.setString(paramIdx++, deviceName + "%"); } stmt.setInt(paramIdx++, tenantId); + if (isDeviceTypeProvided) { + stmt.setString(paramIdx++, deviceType); + } if (isOwnershipProvided) { stmt.setString(paramIdx++, ownership); } @@ -621,8 +625,8 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { stmt.setInt(paramIdx++, activeServerCount); stmt.setInt(paramIdx++, serverIndex); } - stmt.setInt(paramIdx++, request.getStartIndex()); - stmt.setInt(paramIdx, request.getRowCount()); + stmt.setInt(paramIdx++, request.getRowCount()); + stmt.setInt(paramIdx, request.getStartIndex()); try (ResultSet rs = stmt.executeQuery()) { devices = new ArrayList<>(); @@ -668,8 +672,9 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { String sql = "SELECT d1.DEVICE_ID, " + "d1.DESCRIPTION, " + "d1.NAME AS DEVICE_NAME, " + - "d1.DEVICE_TYPE, " + + "e.DEVICE_TYPE, " + "d1.DEVICE_IDENTIFICATION, " + + "d1.LAST_UPDATED_TIMESTAMP, " + "e.OWNER, " + "e.OWNERSHIP, " + "e.STATUS, " + @@ -682,13 +687,12 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { "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 " + + "d.LAST_UPDATED_TIMESTAMP " + "FROM DM_DEVICE d, " + "(SELECT dgm.DEVICE_ID " + "FROM DM_DEVICE_GROUP_MAP dgm " + @@ -700,19 +704,19 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { sql = sql + " AND d.NAME LIKE ?"; isDeviceNameProvided = true; } - sql = sql + ") gd, DM_DEVICE_TYPE t"; - sql = sql + " WHERE gd.DEVICE_TYPE_ID = t.ID"; + sql = sql + ") gd"; + sql = sql + " 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 = ?"; @@ -752,7 +756,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { } } } - sql = sql + " LIMIT ?,?"; + sql = sql + " LIMIT ? OFFSET ?"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { int paramIdx = 1; @@ -764,10 +768,10 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { 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); } @@ -789,8 +793,8 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { stmt.setString(paramIdx++, "%" + entry.getValue() + "%"); } } - stmt.setInt(paramIdx++, request.getStartIndex()); - stmt.setInt(paramIdx, request.getRowCount()); + stmt.setInt(paramIdx++, request.getRowCount()); + stmt.setInt(paramIdx, request.getStartIndex()); try (ResultSet rs = stmt.executeQuery()) { devices = new ArrayList<>(); @@ -817,17 +821,17 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { 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, " + + String sql = "SELECT e1.OWNER, e1.OWNERSHIP, e1.ENROLMENT_ID, e1.DEVICE_ID, e1.STATUS, e1.IS_TRANSFERRED, " + + "e1.DATE_OF_LAST_UPDATE, d.LAST_UPDATED_TIMESTAMP, " + + "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 e.OWNER = ?) e1, DM_DEVICE_TYPE t WHERE d.ID = e1.DEVICE_ID " + - "AND t.ID = d.DEVICE_TYPE_ID LIMIT ?,?"; + "e.TENANT_ID = ? AND e.OWNER = ?) e1 WHERE d.ID = e1.DEVICE_ID LIMIT ? OFFSET ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); stmt.setString(2, request.getOwner()); - stmt.setInt(3, request.getStartIndex()); - stmt.setInt(4, request.getRowCount()); + stmt.setInt(3, request.getRowCount()); + stmt.setInt(4, request.getStartIndex()); ResultSet rs = stmt.executeQuery(); while (rs.next()) { @@ -851,18 +855,19 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { List devices = new ArrayList<>(); 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, " + + "d1.DEVICE_IDENTIFICATION, d1.LAST_UPDATED_TIMESTAMP, 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.NAME, " + - "d.DESCRIPTION, t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, " + - "DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.NAME LIKE ? AND d.TENANT_ID = ?) d1 " + - "WHERE DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ?,?"; + "d.DESCRIPTION, d.LAST_UPDATED_TIMESTAMP, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d " + + "WHERE d.NAME LIKE ? AND d.TENANT_ID = ?) d1 " + + "WHERE DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ? OFFSET ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, request.getDeviceName() + "%"); stmt.setInt(2, tenantId); stmt.setInt(3, tenantId); - stmt.setInt(4, request.getStartIndex()); - stmt.setInt(5, request.getRowCount()); + stmt.setInt(4, request.getRowCount()); + stmt.setInt(5, request.getStartIndex()); ResultSet rs = stmt.executeQuery(); while (rs.next()) { @@ -886,18 +891,20 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { 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.ID AS ENROLMENT_ID FROM (SELECT e.ID, e.DEVICE_ID, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, " + + String sql = "SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, e.DEVICE_TYPE, " + + "d.DEVICE_IDENTIFICATION, d.LAST_UPDATED_TIMESTAMP, e.OWNER, e.OWNERSHIP, e.STATUS, " + + "e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, " + + "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM (SELECT e.ID, e.DEVICE_ID, e.OWNER, " + + "e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DEVICE_TYPE, " + "e.DATE_OF_ENROLMENT, e.DATE_OF_LAST_UPDATE, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e " + - "WHERE TENANT_ID = ? AND OWNERSHIP = ?) 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 = ? LIMIT ?,?"; + "WHERE TENANT_ID = ? AND OWNERSHIP = ?) e, DM_DEVICE d " + + "WHERE DEVICE_ID = e.DEVICE_ID AND d.TENANT_ID = ? LIMIT ? OFFSET ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); stmt.setString(2, request.getOwnership()); stmt.setInt(3, tenantId); - stmt.setInt(4, request.getStartIndex()); - stmt.setInt(5, request.getRowCount()); + stmt.setInt(4, request.getRowCount()); + stmt.setInt(5, request.getStartIndex()); ResultSet rs = stmt.executeQuery(); while (rs.next()) { @@ -924,8 +931,9 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { String sql = "SELECT d.ID AS DEVICE_ID, " + "d.DESCRIPTION, " + "d.NAME AS DEVICE_NAME, " + - "t.NAME AS DEVICE_TYPE, " + + "e.DEVICE_TYPE, " + "d.DEVICE_IDENTIFICATION, " + + "d.LAST_UPDATED_TIMESTAMP, " + "e.OWNER, " + "e.OWNERSHIP, " + "e.STATUS, " + @@ -942,6 +950,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { "e.IS_TRANSFERRED, " + "e.DATE_OF_ENROLMENT, " + "e.DATE_OF_LAST_UPDATE, " + + "e.DEVICE_TYPE, " + "e.ID AS ENROLMENT_ID " + "FROM DM_ENROLMENT e " + "WHERE TENANT_ID = ?"; @@ -953,12 +962,10 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { } sql += buildStatusQuery(statusList); sql += ") e, " + - "DM_DEVICE d, " + - "DM_DEVICE_TYPE t " + + "DM_DEVICE d " + "WHERE DEVICE_ID = e.DEVICE_ID " + - "AND d.DEVICE_TYPE_ID = t.ID " + "AND d.TENANT_ID = ? " + - "LIMIT ?,?"; + "LIMIT ? OFFSET ?"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { int paramIdx = 1; @@ -967,8 +974,8 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { stmt.setString(paramIdx++, status); } stmt.setInt(paramIdx++, tenantId); - stmt.setInt(paramIdx++, request.getStartIndex()); - stmt.setInt(paramIdx, request.getRowCount()); + stmt.setInt(paramIdx++, request.getRowCount()); + stmt.setInt(paramIdx, request.getStartIndex()); try (ResultSet rs = stmt.executeQuery()) { while (rs.next()) { @@ -998,8 +1005,9 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { String sql = "SELECT " + "d.ID AS DEVICE_ID, " + "d.DESCRIPTION,d.NAME AS DEVICE_NAME, " + - "t.NAME AS DEVICE_TYPE, " + - "d.DEVICE_IDENTIFICATION, " + + "d.LAST_UPDATED_TIMESTAMP, " + + "e.DEVICE_TYPE, " + + "e.DEVICE_IDENTIFICATION, " + "e.OWNER, " + "e.OWNERSHIP, " + "e.STATUS, " + @@ -1007,9 +1015,8 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { "e.DATE_OF_LAST_UPDATE," + "e.DATE_OF_ENROLMENT, " + "e.ID AS ENROLMENT_ID " + - "FROM DM_DEVICE AS d , DM_ENROLMENT AS e , DM_DEVICE_TYPE AS t " + + "FROM DM_DEVICE AS d, DM_ENROLMENT AS e " + "WHERE d.ID = e.DEVICE_ID AND " + - "d.DEVICE_TYPE_ID = t.ID AND " + "e.TENANT_ID = ? AND " + "e.DATE_OF_ENROLMENT BETWEEN ? AND ?"; if (statusList != null && !statusList.isEmpty()) { @@ -1019,7 +1026,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { if (ownership != null) { sql = sql + " AND e.OWNERSHIP = ?"; } - sql = sql + " LIMIT ?,?"; + sql = sql + " LIMIT ? OFFSET ?"; try (Connection conn = this.getConnection(); PreparedStatement stmt = conn.prepareStatement(sql)) { @@ -1035,8 +1042,8 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { if (ownership != null) { stmt.setString(paramIdx++, ownership); } - stmt.setInt(paramIdx++, request.getStartIndex()); - stmt.setInt(paramIdx, request.getRowCount()); + stmt.setInt(paramIdx++, request.getRowCount()); + stmt.setInt(paramIdx, request.getStartIndex()); try (ResultSet rs = stmt.executeQuery()) { devices = new ArrayList<>(); @@ -1063,11 +1070,9 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { String sql = "SELECT " + "COUNT(d.ID) AS DEVICE_COUNT " + - "FROM DM_DEVICE AS d , " + - "DM_ENROLMENT AS e , " + - "DM_DEVICE_TYPE AS t " + + "FROM DM_DEVICE AS d, " + + "DM_ENROLMENT AS e " + "WHERE d.ID = e.DEVICE_ID " + - "AND d.DEVICE_TYPE_ID = t.ID " + "AND e.TENANT_ID = ? " + "AND e.DATE_OF_ENROLMENT BETWEEN ? AND ?"; if (statusList != null && !statusList.isEmpty()) { @@ -1121,7 +1126,6 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { "COUNT(SUBSTRING(e.DATE_OF_ENROLMENT, 1, 10)) AS ENROLMENT_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 " + "AND e.TENANT_ID = ? " + "AND e.DATE_OF_ENROLMENT " + "BETWEEN ? AND ? "; @@ -1136,7 +1140,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { sql = sql + " AND e.OWNERSHIP = ?"; } - sql = sql + " GROUP BY SUBSTRING(e.DATE_OF_ENROLMENT, 1, 10) LIMIT ? OFFSET ?"; + sql = sql + " GROUP BY SUBSTRING(e.DATE_OF_ENROLMENT, 1, 10) LIMIT ?, ?"; try { Connection conn = this.getConnection(); @@ -1187,27 +1191,27 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { public List getDevicesByNameAndType(String deviceName, String type, int tenantId, int offset, int limit) throws DeviceManagementDAOException { - String filteringString = ""; - if (deviceName != null && !deviceName.isEmpty()) { - filteringString = filteringString + " AND d.NAME LIKE ?"; - } - - if (type != null && !type.isEmpty()) { - filteringString = filteringString + " AND t.NAME = ?"; - } - Connection conn; PreparedStatement stmt = null; List devices = new ArrayList<>(); ResultSet rs = 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, " + + "d1.DEVICE_IDENTIFICATION, d1.LAST_UPDATED_TIMESTAMP, 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.NAME, " + - "d.DESCRIPTION, 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 = ?" + filteringString + - ") d1 WHERE d1.ID = e.DEVICE_ID LIMIT ?, ?"; + "d.DESCRIPTION, d.DEVICE_IDENTIFICATION, d.LAST_UPDATED_TIMESTAMP FROM DM_DEVICE d " + + "WHERE d.TENANT_ID = ?"; + + if (deviceName != null && !deviceName.isEmpty()) { + sql += " AND d.NAME LIKE ? "; + } + sql += ") d1 WHERE d1.ID = e.DEVICE_ID"; + if (type != null && !type.isEmpty()) { + sql += " AND e.DEVICE_TYPE = ?"; + } + sql+=" LIMIT ? OFFSET ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); @@ -1222,8 +1226,8 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { stmt.setString(++i, type); } - stmt.setInt(++i, offset); stmt.setInt(++i, limit); + stmt.setInt(++i, offset); rs = stmt.executeQuery(); @@ -1272,7 +1276,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { + "DM_DEVICE.ID AS DEVICE_ID, " + "DM_DEVICE.NAME AS DEVICE_NAME, " + "DM_DEVICE.DESCRIPTION AS DESCRIPTION, " - + "DM_DEVICE.DEVICE_TYPE_ID, " + + "DM_DEVICE.LAST_UPDATED_TIMESTAMP, " + "DM_DEVICE.DEVICE_IDENTIFICATION AS DEVICE_IDENTIFICATION, " + "e.ID AS ENROLMENT_ID, " + "e.OWNER, " @@ -1280,14 +1284,12 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { + "e.DATE_OF_ENROLMENT, " + "e.DATE_OF_LAST_UPDATE, " + "e.STATUS, " - + "e.IS_TRANSFERRED, " - + "device_types.NAME AS DEVICE_TYPE " + + "e.DEVICE_TYPE, " + + "e.IS_TRANSFERRED " + "FROM DM_DEVICE " + "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 "; + + "DM_DEVICE.TENANT_ID = e.TENANT_ID "; if (null != serial && !serial.isEmpty()) { // Only if serial is provided, join with device info table query = query.concat("INNER JOIN DM_DEVICE_INFO i ON " @@ -1332,7 +1334,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { } } - query = query + " LIMIT ?,?"; + query = query + " LIMIT ? OFFSET ?"; try (PreparedStatement ps = conn.prepareStatement(query)) { @@ -1365,8 +1367,8 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { ps.setString(index++, "%" + entry.getValue() + "%"); } } - ps.setInt(index++, offsetValue); - ps.setInt(index, limitValue); + ps.setInt(index++, limitValue); + ps.setInt(index, offsetValue); try (ResultSet rs = ps.executeQuery()) { while (rs.next()) { @@ -1444,11 +1446,12 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { convert function performed in the query which will depend on the datasource */ String dataSourceType = conn.getMetaData().getDatabaseProductName(); String sql = "SELECT " + - "d1.DEVICE_TYPE, " + + "e.DEVICE_TYPE, " + "d1.DEVICE_ID, " + "d1.DEVICE_NAME, " + "d1.DESCRIPTION, " + "d1.DEVICE_IDENTIFICATION, " + + "d1.LAST_UPDATED_TIMESTAMP, " + "ddd.OS_VERSION, " + "e.ID AS ENROLMENT_ID, " + "e.OWNER, " + @@ -1458,20 +1461,10 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { "e.DATE_OF_LAST_UPDATE, " + "e.DATE_OF_ENROLMENT " + "FROM DM_DEVICE_INFO ddi," + - "DM_DEVICE_DETAIL ddd, " + - "DM_ENROLMENT e, " + - "(SELECT dt.NAME AS DEVICE_TYPE, " + - "d.ID AS DEVICE_ID, " + - "d.NAME AS DEVICE_NAME, " + - "DESCRIPTION, " + - "DEVICE_IDENTIFICATION " + - "FROM DM_DEVICE_TYPE dt, " + - "DM_DEVICE d " + - "WHERE dt.NAME = ? " + - "AND PROVIDER_TENANT_ID = ? " + - "AND dt.ID = d.DEVICE_TYPE_ID " + - ") d1 " + - "WHERE d1.DEVICE_ID = e.DEVICE_ID " + + "DM_DEVICE_DETAIL ddd, DM_ENROLMENT e, DM_DEVICE d1 " + + "WHERE e.DEVICE_TYPE = ? " + + "AND e.TENANT_ID = ? " + + "AND d1.DEVICE_ID = e.DEVICE_ID " + "AND d1.DEVICE_ID = ddi.DEVICE_ID " + "AND d1.DEVICE_ID = ddd.DEVICE_ID " + "AND ddi.KEY_FIELD = ? "; @@ -1487,8 +1480,8 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { ps.setInt(2, tenantId); ps.setString(3, Constants.OS_VALUE); ps.setLong(4, osValue); - ps.setInt(5, request.getRowCount()); - ps.setInt(6, request.getStartIndex()); + ps.setInt(5, request.getStartIndex()); + ps.setInt(6, request.getRowCount()); try (ResultSet rs = ps.executeQuery()) { List devices = new ArrayList<>(); @@ -1557,7 +1550,143 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { } } - private Connection getConnection() throws SQLException { - return DeviceManagementDAOFactory.getConnection(); + @Override + public List getGroupedDevicesDetails(PaginationRequest request, List deviceIds, String groupName, + int tenantId) throws DeviceManagementDAOException { + int limitValue = request.getRowCount(); + int offsetValue = request.getStartIndex(); + List status = request.getStatusList(); + String name = request.getDeviceName(); + String user = request.getOwner(); + String ownership = request.getOwnership(); + try { + List devices = new ArrayList<>(); + if (deviceIds.isEmpty()) { + return devices; + } + Connection conn = this.getConnection(); + int index = 1; + StringJoiner joiner = new StringJoiner(",", + "SELECT " + + "DM_DEVICE.ID AS DEVICE_ID, " + + "DM_DEVICE.NAME AS DEVICE_NAME, " + + "DM_DEVICE.DESCRIPTION AS DESCRIPTION, " + + "DM_DEVICE.LAST_UPDATED_TIMESTAMP, " + + "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 " + + "FROM DM_DEVICE_GROUP_MAP " + + "INNER JOIN DM_DEVICE ON " + + "DM_DEVICE_GROUP_MAP.DEVICE_ID = DM_DEVICE.ID " + + "INNER JOIN DM_GROUP ON " + + "DM_DEVICE_GROUP_MAP.GROUP_ID = DM_GROUP.ID " + + "INNER JOIN DM_ENROLMENT e ON " + + "DM_DEVICE.ID = e.DEVICE_ID AND " + + "DM_DEVICE.TENANT_ID = e.TENANT_ID " + + "WHERE DM_DEVICE.ID IN (", + ") AND DM_DEVICE.TENANT_ID = ?"); + + deviceIds.stream().map(ignored -> "?").forEach(joiner::add); + String query = joiner.toString(); + if (StringUtils.isNotBlank(groupName)) { + query += " AND DM_GROUP.GROUP_NAME = ?"; + } + if (StringUtils.isNotBlank(name)) { + query += " AND DM_DEVICE.NAME LIKE ?"; + } + if (StringUtils.isNotBlank(user)) { + query += " AND e.OWNER = ?"; + } + if (StringUtils.isNotBlank(ownership)) { + query += " AND e.OWNERSHIP = ?"; + } + if (status != null && !status.isEmpty()) { + query += buildStatusQuery(status); + } + + query += "LIMIT ? OFFSET ?"; + + try (PreparedStatement ps = conn.prepareStatement(query)) { + for (Integer deviceId : deviceIds) { + ps.setInt(index++, deviceId); + } + ps.setInt(index++, tenantId); + if (StringUtils.isNotBlank(groupName)) { + ps.setString(index++, groupName); + } + if (StringUtils.isNotBlank(name)) { + ps.setString(index++, name); + } + if (StringUtils.isNotBlank(user)) { + ps.setString(index++, user); + } + if (StringUtils.isNotBlank(ownership)) { + ps.setString(index++, ownership); + } + if (status != null && !status.isEmpty()) { + for (String deviceStatus : status) { + ps.setString(index++, deviceStatus); + } + } + ps.setInt(index++, limitValue); + ps.setInt(index, offsetValue); + + try (ResultSet rs = ps.executeQuery()) { + while (rs.next()) { + devices.add(DeviceManagementDAOUtil.loadDevice(rs)); + } + return devices; + } + } + } catch (SQLException e) { + String msg = "Error occurred while retrieving information of all registered devices " + + "according to device ids and the limit area."; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } } + + /*** + * This method updates the status of a given list of devices to DELETED state in the DM_DEVICE_STATUS table + * @param conn Connection object + * @param validDevices list of devices + * @throws DeviceManagementDAOException if updating fails + */ + public void refactorDeviceStatus(Connection conn, List validDevices) throws DeviceManagementDAOException { + String updateQuery = "UPDATE DM_DEVICE_STATUS SET STATUS = ? WHERE ID = ?"; + String selectLastMatchingRecordQuery = "SELECT ID FROM DM_DEVICE_STATUS WHERE ENROLMENT_ID = ? " + + "AND DEVICE_ID = ? ORDER BY ID DESC LIMIT 1"; + + try (PreparedStatement selectStatement = conn.prepareStatement(selectLastMatchingRecordQuery); + PreparedStatement updateStatement = conn.prepareStatement(updateQuery)) { + + for (Device device : validDevices) { + + selectStatement.setInt(1, device.getEnrolmentInfo().getId()); + selectStatement.setInt(2, device.getId()); + + ResultSet resultSet = selectStatement.executeQuery(); + int lastRecordId = 0; + if (resultSet.next()) { + lastRecordId = resultSet.getInt("ID"); + } + + updateStatement.setString(1, String.valueOf(EnrolmentInfo.Status.DELETED)); + updateStatement.setInt(2, lastRecordId); + updateStatement.execute(); + } + + } catch (SQLException e) { + String msg = "SQL error occurred while updating device status properties."; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + } + } 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/device/OracleDeviceDAOImpl.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/device/OracleDeviceDAOImpl.java index 25b05520b6..3effd783c1 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/device/OracleDeviceDAOImpl.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/device/OracleDeviceDAOImpl.java @@ -18,1449 +18,55 @@ package io.entgra.device.mgt.core.device.mgt.core.dao.impl.device; -import org.apache.commons.lang.StringUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import io.entgra.device.mgt.core.device.mgt.common.Count; import io.entgra.device.mgt.core.device.mgt.common.Device; -import io.entgra.device.mgt.core.device.mgt.common.DeviceBilling; -import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest; import io.entgra.device.mgt.core.device.mgt.common.EnrolmentInfo; -import io.entgra.device.mgt.core.device.mgt.common.device.details.DeviceInfo; 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.AbstractDeviceDAOImpl; -import io.entgra.device.mgt.core.device.mgt.core.dao.util.DeviceManagementDAOUtil; -import io.entgra.device.mgt.core.device.mgt.core.report.mgt.Constants; - +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; 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.Date; import java.util.List; -import java.util.StringJoiner; -import java.util.Map; /** * This class holds the generic implementation of DeviceDAO which can be used to support ANSI db syntax. */ -public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl { +public class OracleDeviceDAOImpl extends SQLServerDeviceDAOImpl { private static final Log log = LogFactory.getLog(OracleDeviceDAOImpl.class); @Override - public List getDevices(PaginationRequest request, int tenantId) - throws DeviceManagementDAOException { - Connection conn; - List devices = null; - String deviceType = request.getDeviceType(); - boolean isDeviceTypeProvided = false; - String deviceName = request.getDeviceName(); - boolean isDeviceNameProvided = false; - String owner = request.getOwner(); - boolean isOwnerProvided = false; - String ownerPattern = request.getOwnerPattern(); - boolean isOwnerPatternProvided = false; - String ownership = request.getOwnership(); - boolean isOwnershipProvided = false; - List statusList = request.getStatusList(); - boolean isStatusProvided = false; - Date since = request.getSince(); - boolean isSinceProvided = false; - String serial = request.getSerialNumber(); - boolean isSerialProvided = false; + public void refactorDeviceStatus(Connection conn, List validDevices) throws DeviceManagementDAOException { + String updateQuery = "UPDATE DM_DEVICE_STATUS SET STATUS = ? WHERE ID = ?"; + String selectLastMatchingRecordQuery = "SELECT ID FROM DM_DEVICE_STATUS " + + "WHERE ENROLMENT_ID = ? AND DEVICE_ID = ? ORDER BY ID DESC ROWNUMBER = 1"; - try { - conn = 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 "; + try (PreparedStatement selectStatement = conn.prepareStatement(selectLastMatchingRecordQuery); + PreparedStatement updateStatement = conn.prepareStatement(updateQuery)) { - //Filter by serial number or any Custom Property in DM_DEVICE_INFO - if (serial != null || !request.getCustomProperty().isEmpty()) { - sql = sql + - "FROM DM_DEVICE d " + - "INNER JOIN DM_DEVICE_TYPE t ON d.DEVICE_TYPE_ID = t.ID " + - "WHERE "; - if (serial != null) { - sql += "EXISTS (" + - "SELECT VALUE_FIELD " + - "FROM DM_DEVICE_INFO di " + - "WHERE di.DEVICE_ID = d.ID " + - "AND di.KEY_FIELD = 'serial' " + - "AND di.VALUE_FIELD LIKE ? ) "; - isSerialProvided = true; - } - if (!request.getCustomProperty().isEmpty()) { - if (serial != null) { - sql += "AND "; - } - boolean firstCondition = true; - for (Map.Entry entry : request.getCustomProperty().entrySet()) { - if (!firstCondition) { - sql += "AND "; - } - sql += "EXISTS (" + - "SELECT VALUE_FIELD " + - "FROM DM_DEVICE_INFO di " + - "WHERE di.DEVICE_ID = d.ID " + - "AND di.KEY_FIELD = '" + entry.getKey() + "' " + - "AND di.VALUE_FIELD LIKE ? ) "; - firstCondition = false; - } - } - sql += "AND d.TENANT_ID = ? "; - } else { - sql = sql + "FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ? "; - } - //Add query for last updated timestamp - if (since != null) { - sql = sql + " AND d.LAST_UPDATED_TIMESTAMP > ?"; - isSinceProvided = true; - } - //Add the query for device-type - if (deviceType != null && !deviceType.isEmpty()) { - sql = sql + " AND t.NAME = ?"; - isDeviceTypeProvided = true; - } - //Add the query for device-name - 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 = ?"; - //Add the query for ownership - if (ownership != null && !ownership.isEmpty()) { - sql = sql + " AND e.OWNERSHIP = ?"; - isOwnershipProvided = true; - } - //Add the query for owner - if (owner != null && !owner.isEmpty()) { - sql = sql + " AND e.OWNER = ?"; - isOwnerProvided = true; - } else if (ownerPattern != null && !ownerPattern.isEmpty()) { - sql = sql + " AND e.OWNER LIKE ?"; - isOwnerPatternProvided = true; - } - if (statusList != null && !statusList.isEmpty()) { - sql += buildStatusQuery(statusList); - isStatusProvided = true; - } - sql = sql + " ORDER BY ENROLMENT_ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + for (Device device : validDevices) { - try (PreparedStatement stmt = conn.prepareStatement(sql)) { - int paramIdx = 1; - if (isSerialProvided) { - stmt.setString(paramIdx++, "%" + serial + "%"); - } - if (!request.getCustomProperty().isEmpty()) { - for (Map.Entry entry : request.getCustomProperty().entrySet()) { - stmt.setString(paramIdx++, "%" + entry.getValue() + "%"); - } - } - stmt.setInt(paramIdx++, tenantId); - if (isSinceProvided) { - stmt.setTimestamp(paramIdx++, new Timestamp(since.getTime())); - } - if (isDeviceTypeProvided) { - stmt.setString(paramIdx++, deviceType); - } - if (isDeviceNameProvided) { - stmt.setString(paramIdx++, "%" + deviceName + "%"); - } - stmt.setInt(paramIdx++, tenantId); - if (isOwnershipProvided) { - stmt.setString(paramIdx++, ownership); - } - if (isOwnerProvided) { - stmt.setString(paramIdx++, owner); - } else if (isOwnerPatternProvided) { - stmt.setString(paramIdx++, ownerPattern + "%"); - } - if (isStatusProvided) { - for (String status : statusList) { - stmt.setString(paramIdx++, status); - } - } - stmt.setInt(paramIdx++, request.getStartIndex()); - stmt.setInt(paramIdx, request.getRowCount()); + selectStatement.setInt(1, device.getEnrolmentInfo().getId()); + selectStatement.setInt(2, device.getId()); - try (ResultSet rs = stmt.executeQuery()) { - devices = new ArrayList<>(); - while (rs.next()) { - Device device = DeviceManagementDAOUtil.loadDevice(rs); - devices.add(device); - } - return devices; + ResultSet resultSet = selectStatement.executeQuery(); + int lastRecordId = 0; + if (resultSet.next()) { + lastRecordId = resultSet.getInt("ID"); } - } - } catch (SQLException e) { - String msg = "Error occurred while retrieving information of all " + - "registered devices"; - log.error(msg, e); - throw new DeviceManagementDAOException(msg, e); - } - } - - //Return only not removed id list - @Override - public List getDevicesIds(PaginationRequest request, int tenantId) - throws DeviceManagementDAOException { - Connection conn; - List devices = null; - String owner = request.getOwner(); - boolean isOwnerProvided = false; - String ownership = request.getOwnership(); - boolean isOwnershipProvided = false; - List statusList = request.getStatusList(); - boolean isStatusProvided = false; - try { - conn = getConnection(); - String sql = "SELECT " + - "d1.ID AS DEVICE_ID, " + - "d1.DEVICE_IDENTIFICATION, " + - "e.STATUS, " + - "e.OWNER, " + - "e.IS_TRANSFERRED, " + - "e.ID AS ENROLMENT_ID " + - "FROM DM_ENROLMENT e, " + - "(SELECT d.ID, " + - "d.DEVICE_IDENTIFICATION " + - "FROM DM_DEVICE d WHERE d.TENANT_ID = ?) d1 " + - "WHERE d1.ID = e.DEVICE_ID AND e.TENANT_ID = ? "; - //Add the query for ownership - if (ownership != null && !ownership.isEmpty()) { - sql = sql + " AND e.OWNERSHIP = ?"; - isOwnershipProvided = true; - } - //Add the query for owner - if (owner != null && !owner.isEmpty()) { - sql = sql + " AND e.OWNER = ?"; - isOwnerProvided = true; - } - if (statusList != null && !statusList.isEmpty()) { - sql += buildStatusQuery(statusList); - isStatusProvided = true; + updateStatement.setString(1, String.valueOf(EnrolmentInfo.Status.DELETED)); + updateStatement.setInt(2, lastRecordId); + updateStatement.execute(); } - try (PreparedStatement stmt = conn.prepareStatement(sql)) { - int paramIdx = 1; - stmt.setInt(paramIdx++, tenantId); - stmt.setInt(paramIdx++, tenantId); - if (isOwnershipProvided) { - stmt.setString(paramIdx++, ownership); - } - if (isOwnerProvided) { - stmt.setString(paramIdx++, owner); - } - if (isStatusProvided) { - for (String status : statusList) { - stmt.setString(paramIdx++, status); - } - } - - try (ResultSet rs = stmt.executeQuery()) { - devices = new ArrayList<>(); - while (rs.next()) { - Device device = DeviceManagementDAOUtil.loadDeviceIds(rs); - devices.add(device); - } - return devices; - } - } } catch (SQLException e) { - String msg = "Error occurred while retrieving information of all " + - "registered devices"; + String msg = "SQL error occurred while updating device status properties."; log.error(msg, e); throw new DeviceManagementDAOException(msg, e); } } -// TODO - add Oracle support for below billing method - @Override - public List getNonRemovedYearlyDeviceList(int tenantId, Timestamp startDate, Timestamp endDate) - throws DeviceManagementDAOException { - return null; - } - - // TODO - add Oracle support for below billing method - @Override - public List getRemovedYearlyDeviceList(int tenantId, Timestamp startDate, Timestamp endDate) - throws DeviceManagementDAOException { - return null; - } - - // TODO - add Oracle support for below billing method - @Override - public List getNonRemovedPriorYearsDeviceList(int tenantId, Timestamp startDate, Timestamp endDate) - throws DeviceManagementDAOException { - return null; - } - - // TODO - add Oracle support for below billing method - @Override - public List getRemovedPriorYearsDeviceList(int tenantId, Timestamp startDate, Timestamp endDate) - throws DeviceManagementDAOException { - return null; - } - - @Override - public List getDeviceListWithoutPagination(int tenantId) throws DeviceManagementDAOException { - return null; - } - - @Override - public List getAllocatedDevices(PaginationRequest request, int tenantId, - int activeServerCount, int serverIndex) - throws DeviceManagementDAOException { - Connection conn; - List devices = null; - String deviceType = request.getDeviceType(); - boolean isDeviceTypeProvided = false; - String deviceName = request.getDeviceName(); - boolean isDeviceNameProvided = false; - String owner = request.getOwner(); - boolean isOwnerProvided = false; - String ownerPattern = request.getOwnerPattern(); - boolean isOwnerPatternProvided = false; - String ownership = request.getOwnership(); - boolean isOwnershipProvided = false; - List statusList = request.getStatusList(); - boolean isStatusProvided = false; - Date since = request.getSince(); - boolean isSinceProvided = false; - boolean isPartitionedTask = false; - - try { - conn = 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 "; - //Add the query to filter active devices on timestamp - if (since != null) { - sql = sql + ", DM_DEVICE_DETAIL dt"; - isSinceProvided = true; - } - sql = sql + " WHERE DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?"; - //Add query for last updated timestamp - if (isSinceProvided) { - sql = sql + " AND dt.DEVICE_ID = d.ID AND dt.UPDATE_TIMESTAMP > ?"; - } - //Add the query for device-type - if (deviceType != null && !deviceType.isEmpty()) { - sql = sql + " AND t.NAME = ?"; - isDeviceTypeProvided = true; - } - //Add the query for device-name - 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 = ?"; - //Add the query for ownership - if (ownership != null && !ownership.isEmpty()) { - sql = sql + " AND e.OWNERSHIP = ?"; - isOwnershipProvided = true; - } - //Add the query for owner - if (owner != null && !owner.isEmpty()) { - sql = sql + " AND e.OWNER = ?"; - isOwnerProvided = true; - } else if (ownerPattern != null && !ownerPattern.isEmpty()) { - sql = sql + " AND e.OWNER LIKE ?"; - isOwnerPatternProvided = true; - } - if (statusList != null && !statusList.isEmpty()) { - sql += buildStatusQuery(statusList); - isStatusProvided = true; - } - if (activeServerCount > 0){ - sql = sql + " AND MOD(d1.ID, ?) = ?"; - isPartitionedTask = true; - } - sql = sql + " ORDER BY ENROLMENT_ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; - - try (PreparedStatement stmt = conn.prepareStatement(sql)) { - int paramIdx = 1; - stmt.setInt(paramIdx++, tenantId); - if (isSinceProvided) { - stmt.setLong(paramIdx++, since.getTime()); - } - if (isDeviceTypeProvided) { - stmt.setString(paramIdx++, deviceType); - } - if (isDeviceNameProvided) { - stmt.setString(paramIdx++, deviceName + "%"); - } - stmt.setInt(paramIdx++, tenantId); - if (isOwnershipProvided) { - stmt.setString(paramIdx++, ownership); - } - if (isOwnerProvided) { - stmt.setString(paramIdx++, owner); - } else if (isOwnerPatternProvided) { - stmt.setString(paramIdx++, ownerPattern + "%"); - } - if (isStatusProvided) { - for (String status : statusList) { - stmt.setString(paramIdx++, status); - } - } - if (isPartitionedTask) { - stmt.setInt(paramIdx++, activeServerCount); - stmt.setInt(paramIdx++, serverIndex); - } - stmt.setInt(paramIdx++, request.getStartIndex()); - stmt.setInt(paramIdx, request.getRowCount()); - - try (ResultSet rs = stmt.executeQuery()) { - devices = new ArrayList<>(); - while (rs.next()) { - Device device = DeviceManagementDAOUtil.loadDevice(rs); - devices.add(device); - } - return devices; - } - } - } catch (SQLException e) { - String msg = "Error occurred while retrieving information of all " + - "registered devices"; - log.error(msg, e); - throw new DeviceManagementDAOException(msg, e); - } - } - - @Override - public List searchDevicesInGroup(PaginationRequest request, int tenantId) - throws DeviceManagementDAOException { - Connection conn; - List devices = null; - int groupId = request.getGroupId(); - String deviceType = request.getDeviceType(); - boolean isDeviceTypeProvided = false; - String deviceName = request.getDeviceName(); - boolean isDeviceNameProvided = false; - String owner = request.getOwner(); - boolean isOwnerProvided = false; - String ownerPattern = request.getOwnerPattern(); - boolean isOwnerPatternProvided = false; - String ownership = request.getOwnership(); - boolean isOwnershipProvided = false; - List statusList = request.getStatusList(); - boolean isStatusProvided = false; - Date since = request.getSince(); - boolean isSinceProvided = false; - String serial = request.getSerialNumber(); - boolean isSerialProvided = false; - - try { - conn = 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, " + - "e.DATE_OF_ENROLMENT, " + - "e.ID AS ENROLMENT_ID " + - "FROM DM_ENROLMENT e, " + - "(SELECT gd.DEVICE_ID, " + - "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 " + - "WHERE dgm.GROUP_ID = ?) dgm1 " + - "WHERE " + - "d.ID = dgm1.DEVICE_ID " + - "AND d.TENANT_ID = ?"; - //Add the query for device-name - if (deviceName != null && !deviceName.isEmpty()) { - sql = sql + " AND d.NAME LIKE ?"; - isDeviceNameProvided = true; - } - sql = sql + ") gd, DM_DEVICE_TYPE t WHERE gd.DEVICE_TYPE_ID = t.ID"; - //Add query for last updated timestamp - if (since != null) { - sql = sql + " AND d.LAST_UPDATED_TIMESTAMP > ?"; - isSinceProvided = true; - } - //Add the query for device-type - if (deviceType != null && !deviceType.isEmpty()) { - sql = sql + " AND t.NAME = ?"; - 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 = ?"; - isOwnershipProvided = true; - } - //Add the query for owner - if (owner != null && !owner.isEmpty()) { - sql = sql + " AND e.OWNER = ?"; - isOwnerProvided = true; - } else if (ownerPattern != null && !ownerPattern.isEmpty()) { - sql = sql + " AND e.OWNER LIKE ?"; - isOwnerPatternProvided = true; - } - if (statusList != null && !statusList.isEmpty()) { - sql += buildStatusQuery(statusList); - isStatusProvided = true; - } - //Filter Group with serial number or any Custom Property in DM_DEVICE_INFO - if (serial != null || !request.getCustomProperty().isEmpty()) { - if (serial != null) { - sql += "AND EXISTS (" + - "SELECT VALUE_FIELD " + - "FROM DM_DEVICE_INFO di " + - "WHERE di.DEVICE_ID = d1.DEVICE_ID " + - "AND di.KEY_FIELD = 'serial' " + - "AND di.VALUE_FIELD LIKE ?) "; - isSerialProvided = true; - } - if (!request.getCustomProperty().isEmpty()) { - for (Map.Entry entry : request.getCustomProperty().entrySet()) { - sql += "AND EXISTS (" + - "SELECT VALUE_FIELD " + - "FROM DM_DEVICE_INFO di2 " + - "WHERE di2.DEVICE_ID = d1.DEVICE_ID " + - "AND di2.KEY_FIELD = '" + entry.getKey() + "' " + - "AND di2.VALUE_FIELD LIKE ?)"; - } - } - } - sql = sql + " ORDER BY ENROLMENT_ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; - - try (PreparedStatement stmt = conn.prepareStatement(sql)) { - int paramIdx = 1; - stmt.setInt(paramIdx++, groupId); - stmt.setInt(paramIdx++, tenantId); - if (isDeviceNameProvided) { - stmt.setString(paramIdx++, "%" + deviceName + "%"); - } - if (isSinceProvided) { - stmt.setTimestamp(paramIdx++, new Timestamp(since.getTime())); - } - if (isDeviceTypeProvided) { - stmt.setString(paramIdx++, deviceType); - } - stmt.setInt(paramIdx++, tenantId); - if (isOwnershipProvided) { - stmt.setString(paramIdx++, ownership); - } - if (isOwnerProvided) { - stmt.setString(paramIdx++, owner); - } else if (isOwnerPatternProvided) { - stmt.setString(paramIdx++, ownerPattern + "%"); - } - if (isStatusProvided) { - for (String status : statusList) { - stmt.setString(paramIdx++, status); - } - } - if (isSerialProvided) { - stmt.setString(paramIdx++, "%" + serial + "%"); - } - if (!request.getCustomProperty().isEmpty()) { - for (Map.Entry entry : request.getCustomProperty().entrySet()) { - stmt.setString(paramIdx++, "%" + entry.getValue() + "%"); - } - } - stmt.setInt(paramIdx++, request.getStartIndex()); - stmt.setInt(paramIdx, request.getRowCount()); - - try (ResultSet rs = stmt.executeQuery()) { - devices = new ArrayList<>(); - while (rs.next()) { - Device device = DeviceManagementDAOUtil.loadDevice(rs); - devices.add(device); - } - return devices; - } - } - } catch (SQLException e) { - String msg = "Error occurred while retrieving information of" + - " devices belonging to group : " + groupId; - log.error(msg, e); - throw new DeviceManagementDAOException(msg, e); - } - } - - @Override - public List getDevicesOfUser(PaginationRequest request, int tenantId) - throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - 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 e.OWNER = ?) e1, DM_DEVICE_TYPE t WHERE d.ID = e1.DEVICE_ID " - + "AND t.ID = d.DEVICE_TYPE_ID ORDER BY ENROLMENT_ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; - stmt = conn.prepareStatement(sql); - stmt.setInt(1, tenantId); - stmt.setString(2, request.getOwner()); - stmt.setInt(3, request.getStartIndex()); - stmt.setInt(4, request.getRowCount()); - ResultSet rs = stmt.executeQuery(); - - while (rs.next()) { - Device device = DeviceManagementDAOUtil.loadDevice(rs); - devices.add(device); - } - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while fetching the list of devices belongs to '" + - request.getOwner() + "'", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, null); - } - return devices; - } - - @Override - public List getDevicesByName(PaginationRequest request, int tenantId) - throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - List devices = new ArrayList<>(); - 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.NAME, " - + "d.DESCRIPTION, t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, " - + "DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.NAME LIKE ? AND d.TENANT_ID = ?) d1 " - + "WHERE DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? ORDER BY ENROLMENT_ID " + - "OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; - stmt = conn.prepareStatement(sql); - stmt.setString(1, request.getDeviceName() + "%"); - stmt.setInt(2, tenantId); - stmt.setInt(3, tenantId); - stmt.setInt(4, request.getStartIndex()); - stmt.setInt(5, request.getRowCount()); - ResultSet rs = stmt.executeQuery(); - - while (rs.next()) { - Device device = DeviceManagementDAOUtil.loadDevice(rs); - devices.add(device); - } - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches " + - "'" + request.getDeviceName() + "'", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, null); - } - return devices; - } - - @Override - public List getDevicesByOwnership(PaginationRequest request, int tenantId) - throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - 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.ID AS 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 OWNERSHIP = ?) 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 = ? ORDER BY ENROLMENT_ID " - + "OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; - stmt = conn.prepareStatement(sql); - stmt.setInt(1, tenantId); - stmt.setString(2, request.getOwnership()); - stmt.setInt(3, tenantId); - stmt.setInt(4, request.getStartIndex()); - stmt.setInt(5, request.getRowCount()); - ResultSet rs = stmt.executeQuery(); - - while (rs.next()) { - Device device = DeviceManagementDAOUtil.loadDevice(rs); - devices.add(device); - } - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches to ownership " + - "'" + request.getOwnership() + "'", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, null); - } - return devices; - } - - @Override - public List getDevicesByStatus(PaginationRequest request, int tenantId) - throws DeviceManagementDAOException { - Connection conn; - List devices = new ArrayList<>(); - List statusList = request.getStatusList(); - - try { - conn = 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.ID AS 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 = ?"; - - if (statusList == null || statusList.isEmpty()) { - String msg = "Error occurred while fetching the list of devices. Status List can't " + - "be null or empty"; - log.error(msg); - throw new DeviceManagementDAOException(msg); - } - sql += buildStatusQuery(statusList); - sql += ") 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 = ?" + - "LIMIT ?,?"; - - try (PreparedStatement stmt = conn.prepareStatement(sql)) { - int paramIdx = 1; - stmt.setInt(paramIdx++, tenantId); - for (String status : statusList) { - stmt.setString(paramIdx++, status); - } - stmt.setInt(paramIdx++, tenantId); - stmt.setInt(paramIdx++, request.getStartIndex()); - stmt.setInt(paramIdx, request.getRowCount()); - try (ResultSet rs = stmt.executeQuery()) { - while (rs.next()) { - Device device = DeviceManagementDAOUtil.loadDevice(rs); - devices.add(device); - } - return devices; - } - } - } catch (SQLException e) { - String msg = "Error occurred while fetching the list of devices that matches to status " + - request.getStatusList().toString(); - log.error(msg, e); - throw new DeviceManagementDAOException(msg, e); - } - } - - @Override - public List getDevicesByDuration(PaginationRequest request, int tenantId, - String fromDate, String toDate) - throws DeviceManagementDAOException { - List devices; - List statusList = request.getStatusList(); - boolean isStatusProvided = false; - String ownership = request.getOwnership(); - - 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.ID AS ENROLMENT_ID " + - "FROM DM_DEVICE AS d , DM_ENROLMENT AS e , DM_DEVICE_TYPE AS t " + - "WHERE d.ID = e.DEVICE_ID AND " + - "d.DEVICE_TYPE_ID = t.ID AND " + - "e.TENANT_ID = ? AND " + - "e.DATE_OF_ENROLMENT BETWEEN ? AND ?"; - if (statusList != null && !statusList.isEmpty()) { - sql += buildStatusQuery(statusList); - isStatusProvided = true; - } - if (ownership != null) { - sql = sql + " AND e.OWNERSHIP = ?"; - } - sql = sql + " ORDER BY ENROLMENT_ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; - - try (Connection conn = this.getConnection(); - PreparedStatement stmt = conn.prepareStatement(sql)) { - int paramIdx = 1; - stmt.setInt(paramIdx++, tenantId); - stmt.setString(paramIdx++, fromDate); - stmt.setString(paramIdx++, toDate); - if (isStatusProvided) { - for (String status : statusList) { - stmt.setString(paramIdx++, status); - } - } - if (ownership != null) { - stmt.setString(paramIdx++, ownership); - } - stmt.setInt(paramIdx++, request.getStartIndex()); - stmt.setInt(paramIdx, request.getRowCount()); - - try (ResultSet rs = stmt.executeQuery()) { - devices = new ArrayList<>(); - while (rs.next()) { - Device device = DeviceManagementDAOUtil.loadDevice(rs); - devices.add(device); - } - return devices; - } - } catch (SQLException e) { - String msg = "Error occurred while retrieving information of all " + - "registered devices under tenant id " + tenantId; - log.error(msg, e); - throw new DeviceManagementDAOException(msg, e); - } - } - - @Override - public int getDevicesByDurationCount(List statusList, String ownership, String fromDate, String toDate, int tenantId) throws DeviceManagementDAOException { - return 0; - } - - @Override - public List getCountOfDevicesByDuration(PaginationRequest request, List statusList, int tenantId, - String fromDate, String toDate) - throws DeviceManagementDAOException { - List countList = new ArrayList<>(); - String ownership = request.getOwnership(); - boolean isStatusProvided; - - String sql = - "SELECT " + - "SUBSTRING(e.DATE_OF_ENROLMENT, 1, 10) AS ENROLMENT_DATE, " + - "COUNT(SUBSTRING(e.DATE_OF_ENROLMENT, 1, 10)) AS ENROLMENT_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 " + - "AND e.TENANT_ID = ? " + - "AND e.DATE_OF_ENROLMENT BETWEEN ? AND ? "; - - //Add the query for status - StringBuilder sqlBuilder = new StringBuilder(sql); - isStatusProvided = buildStatusQuery(statusList, sqlBuilder); - sql = sqlBuilder.toString(); - - if (ownership != null) { - sql = sql + " AND e.OWNERSHIP = ?"; - } - - sql = sql + " GROUP BY SUBSTRING(e.DATE_OF_ENROLMENT, 1, 10) OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; - - try { - Connection conn = this.getConnection(); - try (PreparedStatement stmt = conn.prepareStatement(sql)) { - int paramIdx = 1; - stmt.setInt(paramIdx++, tenantId); - stmt.setString(paramIdx++, fromDate); - stmt.setString(paramIdx++, toDate); - if (isStatusProvided) { - for (String status : statusList) { - stmt.setString(paramIdx++, status); - } - } - if (ownership != null) { - stmt.setString(paramIdx++, ownership); - } - stmt.setInt(paramIdx++, request.getStartIndex()); - stmt.setInt(paramIdx, request.getRowCount()); - try (ResultSet rs = stmt.executeQuery()) { - while (rs.next()) { - Count count = new Count( - rs.getString("ENROLMENT_DATE"), - rs.getInt("ENROLMENT_COUNT") - ); - countList.add(count); - } - } - } - } catch (SQLException e) { - String msg = "Error occurred while retrieving information of all " + - "registered devices under tenant id " + tenantId + " between " + fromDate + " to " + toDate; - log.error(msg, e); - throw new DeviceManagementDAOException(msg, e); - } - return countList; - } - - protected boolean buildStatusQuery(List statusList, StringBuilder sqlBuilder) { - if (statusList != null && !statusList.isEmpty() && !statusList.get(0).isEmpty()) { - sqlBuilder.append(" AND e.STATUS IN("); - for (int i = 0; i < statusList.size(); i++) { - sqlBuilder.append("?"); - if (i != statusList.size() - 1) { - sqlBuilder.append(","); - } - } - sqlBuilder.append(")"); - return true; - } else { - return false; - } - } - - /** - * Get the list of devices that matches with the given device name and (or) device type. - * - * @param deviceName Name of the device. - * @param tenantId Id of the current tenant - * @return device list - * @throws DeviceManagementDAOException - */ - @Override - public List getDevicesByNameAndType(String deviceName, String type, int tenantId, int offset, int limit) - throws DeviceManagementDAOException { - - String filteringString = ""; - if (deviceName != null && !deviceName.isEmpty()) { - filteringString = filteringString + " AND d.NAME LIKE ?"; - } - - if (type != null && !type.isEmpty()) { - filteringString = filteringString + " AND t.NAME = ?"; - } - - Connection conn; - PreparedStatement stmt = null; - List devices = new ArrayList<>(); - ResultSet rs = 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.NAME, " + - "d.DESCRIPTION, 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 = ?" + filteringString + - ") d1 WHERE d1.ID = e.DEVICE_ID ORDER BY ENROLMENT_ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; - stmt = conn.prepareStatement(sql); - stmt.setInt(1, tenantId); - - int i = 1; - - if (deviceName != null && !deviceName.isEmpty()) { - stmt.setString(++i, deviceName + "%"); - } - - if (type != null && !type.isEmpty()) { - stmt.setString(++i, type); - } - - stmt.setInt(++i, offset); - stmt.setInt(++i, limit); - - rs = stmt.executeQuery(); - - while (rs.next()) { - Device device = DeviceManagementDAOUtil.loadDevice(rs); - devices.add(device); - } - } catch (SQLException e) { - String msg = "Error occurred while fetching the list of devices corresponding" + - "to the mentioned filtering criteria"; - log.error(msg, e); - throw new DeviceManagementDAOException(msg, e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - return devices; - } - - @Override - public List getSubscribedDevices(PaginationRequest request, List deviceIds, int tenantId) - throws DeviceManagementDAOException { - Connection conn; - int limitValue = request.getRowCount(); - int offsetValue = request.getStartIndex(); - List status = request.getStatusList(); - String name = request.getDeviceName(); - String user = request.getOwner(); - String ownership = request.getOwnership(); - try { - List devices = new ArrayList<>(); - if (deviceIds.isEmpty()) { - return devices; - } - conn = this.getConnection(); - int index = 1; - - boolean isStatusProvided = false; - boolean isDeviceNameProvided = false; - boolean isOwnerProvided = false; - boolean isOwnershipProvided = false; - StringJoiner joiner = new StringJoiner(",", - "SELECT " - + "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.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 " - + "FROM DM_DEVICE " - + "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 AND e.STATUS NOT IN (?, ?)"); - - deviceIds.stream().map(ignored -> "?").forEach(joiner::add); - String query = joiner.toString(); - - if (name != null && !name.isEmpty()) { - query += " AND DM_DEVICE.NAME LIKE ?"; - isDeviceNameProvided = true; - } - if (ownership != null && !ownership.isEmpty()) { - query += " AND e.OWNERSHIP = ?"; - isOwnershipProvided = true; - } - if (user != null && !user.isEmpty()) { - query += " AND e.OWNER = ?"; - isOwnerProvided = true; - } - if (status != null && !status.isEmpty()) { - query += buildStatusQuery(status); - isStatusProvided = true; - } - // Loop through custom properties and add conditions - if (!request.getCustomProperty().isEmpty()) { - for (Map.Entry entry : request.getCustomProperty().entrySet()) { - query += " AND EXISTS (" + - "SELECT VALUE_FIELD " + - "FROM DM_DEVICE_INFO di2 " + - "WHERE di2.DEVICE_ID = DM_DEVICE.ID " + - "AND di2.KEY_FIELD = '" + entry.getKey() + "' " + - "AND di2.VALUE_FIELD LIKE ?)"; - } - } - - query = query + " ORDER BY DM_DEVICE.ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; - - try (PreparedStatement ps = conn.prepareStatement(query)) { - - for (Integer deviceId : deviceIds) { - ps.setObject(index++, deviceId); - } - - ps.setInt(index++, tenantId); - ps.setString(index++, EnrolmentInfo.Status.REMOVED.toString()); - ps.setString(index++, EnrolmentInfo.Status.DELETED.toString()); - if (isDeviceNameProvided) { - ps.setString(index++, name + "%"); - } - if (isOwnershipProvided) { - ps.setString(index++, ownership); - } - if (isOwnerProvided) { - ps.setString(index++, user); - } - if (isStatusProvided) { - for (String deviceStatus : status) { - ps.setString(index++, deviceStatus); - } - } - // Set custom property values in the loop - if (!request.getCustomProperty().isEmpty()) { - for (Map.Entry entry : request.getCustomProperty().entrySet()) { - ps.setString(index++, "%" + entry.getValue() + "%"); - } - } - ps.setInt(index++, offsetValue); - ps.setInt(index, limitValue); - - try (ResultSet rs = ps.executeQuery()) { - while (rs.next()) { - devices.add(DeviceManagementDAOUtil.loadDevice(rs)); - } - return devices; - } - } - } catch (SQLException e) { - String msg = "Error occurred while retrieving information of all registered devices " + - "according to device ids and the limit area."; - log.error(msg, e); - throw new DeviceManagementDAOException(msg, e); - } - } - - @Override - public int getSubscribedDeviceCount(List deviceIds, int tenantId, List status) - throws DeviceManagementDAOException { - try { - if (deviceIds.isEmpty()) { - return 0; - } - Connection conn = this.getConnection(); - int index = 1; - StringJoiner joiner = new StringJoiner(",", - "SELECT " + - "COUNT(e.DEVICE_ID) AS DEVICE_ID " + - "FROM DM_ENROLMENT AS e, DM_DEVICE AS f " + - "WHERE " + - "e.DEVICE_ID=f.ID AND " + - "e.DEVICE_ID IN (", ") AND e.TENANT_ID = ?"); - - deviceIds.stream().map(ignored -> "?").forEach(joiner::add); - String query = joiner.toString(); - - if (status != null && !status.isEmpty()) { - query += buildStatusQuery(status); - } - - try (PreparedStatement ps = conn.prepareStatement(query)) { - for (Integer deviceId : deviceIds) { - ps.setObject(index++, deviceId); - } - - ps.setInt(index++, tenantId); - if (status != null && !status.isEmpty()) { - for (String deviceStatus : status) { - ps.setString(index++, deviceStatus); - } - } - - try (ResultSet rs = ps.executeQuery()) { - if (rs.next()) { - return rs.getInt("DEVICE_ID"); - } - return 0; - } - } - } catch (SQLException e) { - String msg = "Error occurred while retrieving information of all registered devices " + - "according to device ids and the limit area."; - log.error(msg, e); - throw new DeviceManagementDAOException(msg, e); - } - } - - @Override - public List getDevicesExpiredByOSVersion(PaginationRequest request, int tenantId) - throws DeviceManagementDAOException { - try { - Long osValue = (Long) request.getProperty(Constants.OS_VALUE); - Connection conn = getConnection(); - String sql = "SELECT " + - "d1.DEVICE_TYPE, " + - "d1.DEVICE_ID, " + - "d1.DEVICE_NAME, " + - "d1.DESCRIPTION, " + - "d1.DEVICE_IDENTIFICATION, " + - "ddd.OS_VERSION, " + - "e.ID AS ENROLMENT_ID, " + - "e.OWNER, " + - "e.OWNERSHIP, " + - "e.STATUS, " + - "e.IS_TRANSFERRED, " + - "e.DATE_OF_LAST_UPDATE, " + - "e.DATE_OF_ENROLMENT " + - "FROM DM_DEVICE_INFO ddi, " + - "DM_DEVICE_DETAIL ddd, " + - "DM_ENROLMENT e, " + - "(SELECT dt.NAME AS DEVICE_TYPE, " + - "d.ID AS DEVICE_ID, " + - "d.NAME AS DEVICE_NAME, " + - "DESCRIPTION, " + - "DEVICE_IDENTIFICATION " + - " FROM DM_DEVICE_TYPE dt, " + - "DM_DEVICE d " + - "WHERE dt.NAME = ? " + - "AND PROVIDER_TENANT_ID = ? " + - "AND dt.ID = d.DEVICE_TYPE_ID " + - ") d1 " + - "WHERE d1.DEVICE_ID = e.DEVICE_ID " + - "AND d1.DEVICE_ID = ddi.DEVICE_ID " + - "AND d1.DEVICE_ID = ddd.DEVICE_ID " + - "AND ddi.KEY_FIELD = ? " + - "AND CAST( ddi.VALUE_FIELD AS BIGINT ) < ? " + - "ORDER BY ENROLMENT_ID " + - "OFFSET ? ROWS " + - "FETCH NEXT ? ROWS ONLY"; - - try (PreparedStatement ps = conn.prepareStatement(sql)) { - ps.setString(1, request.getDeviceType()); - ps.setInt(2, tenantId); - ps.setString(3, Constants.OS_VALUE); - ps.setLong(4, osValue); - ps.setInt(5, request.getStartIndex()); - ps.setInt(6, request.getRowCount()); - - try (ResultSet rs = ps.executeQuery()) { - List devices = new ArrayList<>(); - while (rs.next()) { - Device device = DeviceManagementDAOUtil.loadDevice(rs); - DeviceInfo deviceInfo = new DeviceInfo(); - deviceInfo.setOsVersion(rs.getString("OS_VERSION")); - device.setDeviceInfo(deviceInfo); - devices.add(device); - } - return devices; - } - } - } catch (SQLException e) { - String msg = "Error occurred while building or executing queries to retrieve information " + - "of devices with an older OS build date"; - log.error(msg, e); - throw new DeviceManagementDAOException(msg, e); - } - } - - @Override - public int getCountOfDeviceExpiredByOSVersion(String deviceType, Long osValue, int tenantId) - throws DeviceManagementDAOException { - try { - Connection conn = getConnection(); - String sql = "SELECT " + - "COUNT(ddi.DEVICE_ID) AS DEVICE_COUNT " + - "FROM DM_DEVICE_INFO ddi, " + - "(SELECT d.ID AS DEVICE_ID " + - "FROM DM_DEVICE_TYPE dt, " + - "DM_DEVICE d " + - "WHERE dt.NAME = ? " + - "AND PROVIDER_TENANT_ID = ? " + - "AND dt.ID = d.DEVICE_TYPE_ID " + - ") d1 " + - "WHERE d1.DEVICE_ID = ddi.DEVICE_ID " + - "AND ddi.KEY_FIELD = ? " + - "AND CAST( ddi.VALUE_FIELD AS BIGINT ) < ?"; - - try (PreparedStatement ps = conn.prepareStatement(sql)) { - ps.setString(1, deviceType); - ps.setInt(2, tenantId); - ps.setString(3, Constants.OS_VALUE); - ps.setLong(4, osValue); - - try (ResultSet rs = ps.executeQuery()) { - if (rs.next()) { - return rs.getInt("DEVICE_COUNT"); - } - return 0; - } - } - } catch (SQLException e) { - String msg = "Error occurred while building or executing queries to retrieve the count " + - "of devices with an older OS build date"; - log.error(msg, e); - throw new DeviceManagementDAOException(msg, e); - } - } - - @Override - public List getDevicesByEncryptionStatus(PaginationRequest request, int tenantId, boolean isEncrypted) - throws DeviceManagementDAOException { - try { - Connection conn = getConnection(); - 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 " + - "OFFSET ? ROWS " + - "FETCH NEXT ? ROWS ONLY"; - - try (PreparedStatement ps = conn.prepareStatement(sql)) { - ps.setBoolean(1, isEncrypted); - ps.setInt(2, tenantId); - ps.setInt(3, request.getStartIndex()); - ps.setInt(4, request.getRowCount()); - - try (ResultSet rs = ps.executeQuery()) { - List devices = new ArrayList<>(); - if (rs.next()) { - Device device = DeviceManagementDAOUtil.loadDevice(rs); - devices.add(device); - } - return devices; - } - } - } catch (SQLException e) { - String msg = "Error occurred while building or executing queries to retrieve information " + - "of devices filtered by encryption status: " + isEncrypted; - log.error(msg, e); - throw new DeviceManagementDAOException(msg, e); - } - } - - @Override - public List getGroupedDevicesDetails(PaginationRequest request, List deviceIds, String groupName, - int tenantId) throws DeviceManagementDAOException { - int limitValue = request.getRowCount(); - int offsetValue = request.getStartIndex(); - try { - List devices = new ArrayList<>(); - if (deviceIds.isEmpty()) { - return devices; - } - Connection conn = this.getConnection(); - int index = 1; - StringJoiner joiner = new StringJoiner(",", - "SELECT " - + "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.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 " - + "FROM DM_DEVICE_GROUP_MAP " - + "INNER JOIN DM_DEVICE ON " - + "DM_DEVICE_GROUP_MAP.DEVICE_ID = DM_DEVICE.ID " - + "INNER JOIN DM_GROUP ON " - + "DM_DEVICE_GROUP_MAP.GROUP_ID = DM_GROUP.ID " - + "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 = ?"); - - deviceIds.stream().map(ignored -> "?").forEach(joiner::add); - String query = joiner.toString(); - if (StringUtils.isNotBlank(groupName)) { - query += " AND DM_GROUP.GROUP_NAME = ?"; - } - query += " ORDER BY DEVICE_ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; - - try (PreparedStatement ps = conn.prepareStatement(query)) { - for (Integer deviceId : deviceIds) { - ps.setInt(index++, deviceId); - } - ps.setInt(index++, tenantId); - if (StringUtils.isNotBlank(groupName)) { - ps.setString(index++, groupName); - } - ps.setInt(index++, offsetValue); - ps.setInt(index, limitValue); - - try (ResultSet rs = ps.executeQuery()) { - while (rs.next()) { - devices.add(DeviceManagementDAOUtil.loadDevice(rs)); - } - return devices; - } - } - } catch (SQLException e) { - String msg = "Error occurred while retrieving information of all registered devices " + - "according to device ids and the limit area."; - log.error(msg, e); - throw new DeviceManagementDAOException(msg, e); - } - } - - private Connection getConnection() throws SQLException { - return DeviceManagementDAOFactory.getConnection(); - } } 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/group/GenericGroupDAOImpl.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/group/GenericGroupDAOImpl.java index c3f620ae28..9108016e37 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/group/GenericGroupDAOImpl.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/group/GenericGroupDAOImpl.java @@ -45,25 +45,26 @@ public class GenericGroupDAOImpl extends AbstractGroupDAOImpl { List devices = null; try { conn = GroupManagementDAOFactory.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, " + + "d1.DEVICE_IDENTIFICATION, d1.LAST_UPDATED_TIMESTAMP, 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 gd.DEVICE_ID, gd.DESCRIPTION, gd.NAME, gd.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE " + + "(SELECT gd.DEVICE_ID, gd.DESCRIPTION, gd.NAME, gd.DEVICE_IDENTIFICATION, gd.LAST_UPDATED_TIMESTAMP " + "FROM " + - "(SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, d.DEVICE_TYPE_ID FROM" + + "(SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, d.LAST_UPDATED_TIMESTAMP FROM" + " DM_DEVICE d, (" + "SELECT dgm.DEVICE_ID FROM DM_DEVICE_GROUP_MAP dgm WHERE dgm.GROUP_ID = ?) dgm1 " + - "WHERE d.ID = dgm1.DEVICE_ID AND d.TENANT_ID = ?) gd, DM_DEVICE_TYPE t " + - "WHERE gd.DEVICE_TYPE_ID = t.ID) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ? , ?"; + "WHERE d.ID = dgm1.DEVICE_ID AND d.TENANT_ID = ?) gd) d1 " + + "WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ? OFFSET ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, groupId); stmt.setInt(2, tenantId); stmt.setInt(3, tenantId); //noinspection JpaQueryApiInspection - stmt.setInt(4, startIndex); + stmt.setInt(4, rowCount); //noinspection JpaQueryApiInspection - stmt.setInt(5, rowCount); + stmt.setInt(5, startIndex); rs = stmt.executeQuery(); devices = new ArrayList<>(); while (rs.next()) { 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/group/OracleGroupDAOImpl.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/group/OracleGroupDAOImpl.java index b5a927a30f..26547f417d 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/group/OracleGroupDAOImpl.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/group/OracleGroupDAOImpl.java @@ -192,16 +192,17 @@ public class OracleGroupDAOImpl extends AbstractGroupDAOImpl { List devices = null; try { conn = GroupManagementDAOFactory.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, " + + "d1.DEVICE_IDENTIFICATION, d1.LAST_UPDATED_TIMESTAMP, 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 gd.DEVICE_ID, gd.DESCRIPTION, gd.NAME, gd.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE " + + "(SELECT gd.DEVICE_ID, gd.DESCRIPTION, gd.NAME, gd.DEVICE_IDENTIFICATION, gd.LAST_UPDATED_TIMESTAMP " + "FROM " + - "(SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, d.DEVICE_TYPE_ID FROM" + + "(SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, d.LAST_UPDATED_TIMESTAMP FROM" + " DM_DEVICE d, (" + "SELECT dgm.DEVICE_ID FROM DM_DEVICE_GROUP_MAP dgm WHERE dgm.GROUP_ID = ?) dgm1 " + - "WHERE d.ID = dgm1.DEVICE_ID AND d.TENANT_ID = ?) gd, DM_DEVICE_TYPE t " + - "WHERE gd.DEVICE_TYPE_ID = t.ID) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + "WHERE d.ID = dgm1.DEVICE_ID AND d.TENANT_ID = ?) gd) d1 " + + "WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; stmt = conn.prepareStatement(sql); stmt.setInt(1, groupId); @@ -219,7 +220,7 @@ public class OracleGroupDAOImpl extends AbstractGroupDAOImpl { } } catch (SQLException e) { throw new GroupManagementDAOException("Error occurred while retrieving information of all " + - "registered devices", e); + "registered devices", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } 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/group/PostgreSQLGroupDAOImpl.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/group/PostgreSQLGroupDAOImpl.java index 67994b51a6..da0312c89c 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/group/PostgreSQLGroupDAOImpl.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/group/PostgreSQLGroupDAOImpl.java @@ -133,16 +133,17 @@ public class PostgreSQLGroupDAOImpl extends AbstractGroupDAOImpl { List devices = null; try { conn = GroupManagementDAOFactory.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, " + + "d1.DEVICE_IDENTIFICATION, d1.LAST_UPDATED_TIMESTAMP, 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 gd.DEVICE_ID, gd.DESCRIPTION, gd.NAME, gd.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE " + + "(SELECT gd.DEVICE_ID, gd.DESCRIPTION, gd.NAME, gd.DEVICE_IDENTIFICATION, gd.LAST_UPDATED_TIMESTAMP " + "FROM " + - "(SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, d.DEVICE_TYPE_ID FROM" + + "(SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, d.LAST_UPDATED_TIMESTAMP FROM" + " DM_DEVICE d, (" + "SELECT dgm.DEVICE_ID FROM DM_DEVICE_GROUP_MAP dgm WHERE dgm.GROUP_ID = ?) dgm1 " + - "WHERE d.ID = dgm1.DEVICE_ID AND d.TENANT_ID = ?) gd, DM_DEVICE_TYPE t " + - "WHERE gd.DEVICE_TYPE_ID = t.ID) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ? OFFSET ?"; + "WHERE d.ID = dgm1.DEVICE_ID AND d.TENANT_ID = ?) gd) d1 " + + "WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ? OFFSET ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, groupId); @@ -160,7 +161,7 @@ public class PostgreSQLGroupDAOImpl extends AbstractGroupDAOImpl { } } catch (SQLException e) { throw new GroupManagementDAOException("Error occurred while retrieving information of all " + - "registered devices", e); + "registered devices", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } 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/group/SQLServerGroupDAOImpl.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/group/SQLServerGroupDAOImpl.java index 340e4a8745..37a0ebba80 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/group/SQLServerGroupDAOImpl.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/group/SQLServerGroupDAOImpl.java @@ -192,16 +192,17 @@ public class SQLServerGroupDAOImpl extends AbstractGroupDAOImpl { List devices = null; try { conn = GroupManagementDAOFactory.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, " + + "d1.DEVICE_IDENTIFICATION, d1.LAST_UPDATED_TIMESTAMP, 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 gd.DEVICE_ID, gd.DESCRIPTION, gd.NAME, gd.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE " + + "(SELECT gd.DEVICE_ID, gd.DESCRIPTION, gd.NAME, gd.DEVICE_IDENTIFICATION, gd.LAST_UPDATED_TIMESTAMP " + "FROM " + - "(SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, d.DEVICE_TYPE_ID FROM" + + "(SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, d.LAST_UPDATED_TIMESTAMP FROM" + " DM_DEVICE d, (" + "SELECT dgm.DEVICE_ID FROM DM_DEVICE_GROUP_MAP dgm WHERE dgm.GROUP_ID = ?) dgm1 " + - "WHERE d.ID = dgm1.DEVICE_ID AND d.TENANT_ID = ?) gd, DM_DEVICE_TYPE t " + - "WHERE gd.DEVICE_TYPE_ID = t.ID) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? ORDER BY d1.DEVICE_ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + "WHERE d.ID = dgm1.DEVICE_ID AND d.TENANT_ID = ?) gd) d1 " + + "WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? ORDER BY d1.DEVICE_ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; stmt = conn.prepareStatement(sql); stmt.setInt(1, groupId); 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/util/DeviceManagementDAOUtil.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/util/DeviceManagementDAOUtil.java index 0d2d563f34..a8529398c7 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/util/DeviceManagementDAOUtil.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/util/DeviceManagementDAOUtil.java @@ -230,6 +230,7 @@ public final class DeviceManagementDAOUtil { device.setDescription(rs.getString("DESCRIPTION")); device.setType(rs.getString("DEVICE_TYPE")); device.setDeviceIdentifier(rs.getString("DEVICE_IDENTIFICATION")); + device.setLastUpdatedTimeStamp(rs.getTimestamp("LAST_UPDATED_TIMESTAMP").getTime()); device.setEnrolmentInfo(loadEnrolment(rs)); return device; } 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/device/details/mgt/impl/DeviceInformationManagerImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java index ff81879563..7af356c896 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java @@ -142,9 +142,12 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { log.info("Device identifier " + device.getDeviceIdentifier() + ", Device name " + "changed by user from " + device.getName() + " to " + name); device.setName(name); + deviceDAO.updateDevice(device, CarbonContext.getThreadLocalCarbonContext().getTenantId()); + } else { + deviceDAO.recordDeviceUpdate( + new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()), + CarbonContext.getThreadLocalCarbonContext().getTenantId()); } - - deviceDAO.updateDevice(device, CarbonContext.getThreadLocalCarbonContext().getTenantId()); DeviceManagementDAOFactory.commitTransaction(); //TODO :: This has to be fixed by adding the enrollment 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/DeviceManagementProviderService.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/DeviceManagementProviderService.java index 683971472b..689fc1b243 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/DeviceManagementProviderService.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/DeviceManagementProviderService.java @@ -669,6 +669,8 @@ public interface DeviceManagementProviderService { void addLicense(String deviceType, License license) throws DeviceManagementException; + boolean recordDeviceUpdate(DeviceIdentifier deviceIdentifier) throws DeviceManagementException; + boolean modifyEnrollment(Device device) throws DeviceManagementException; boolean enrollDevice(Device device) throws DeviceManagementException; 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 a62aaada9e..7b233b90ca 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 @@ -38,7 +38,6 @@ import org.wso2.carbon.CarbonConstants; import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext; import io.entgra.device.mgt.core.device.mgt.common.ActivityPaginationRequest; -import io.entgra.device.mgt.core.device.mgt.common.Billing; import io.entgra.device.mgt.core.device.mgt.common.Device; import io.entgra.device.mgt.core.device.mgt.common.DeviceEnrollmentInfoNotification; import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier; @@ -58,7 +57,6 @@ import io.entgra.device.mgt.core.device.mgt.common.StartupOperationConfig; import io.entgra.device.mgt.core.device.mgt.common.BillingResponse; import io.entgra.device.mgt.core.device.mgt.common.app.mgt.Application; import io.entgra.device.mgt.core.device.mgt.common.app.mgt.ApplicationManagementException; -import io.entgra.device.mgt.core.device.mgt.common.app.mgt.MobileAppTypes; import io.entgra.device.mgt.core.device.mgt.common.configuration.mgt.AmbiguousConfigurationException; import io.entgra.device.mgt.core.device.mgt.common.configuration.mgt.ConfigurationEntry; import io.entgra.device.mgt.core.device.mgt.common.configuration.mgt.ConfigurationManagementException; @@ -452,6 +450,31 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return status; } + @Override + public boolean recordDeviceUpdate(DeviceIdentifier deviceIdentifier) throws DeviceManagementException { + int tenantId = this.getTenantId(); + boolean isUpdated; + try { + DeviceManagementDAOFactory.beginTransaction(); + isUpdated = deviceDAO.recordDeviceUpdate(deviceIdentifier, tenantId); + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while setting updated " + + "timestamp of device: " + deviceIdentifier; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (TransactionManagementException e) { + String msg = "Error occurred while initiating transaction to set updated " + + "timestamp of device: " + deviceIdentifier; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + return isUpdated; + } + @Override public boolean modifyEnrollment(Device device) throws DeviceManagementException { if (device == null) { @@ -490,10 +513,11 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv if (device.getName() == null) { device.setName(currentDevice.getName()); } - deviceDAO.updateDevice(device, tenantId); + int updatedRows = enrollmentDAO.updateEnrollment(device.getEnrolmentInfo(), tenantId); boolean isEnableDeviceStatusCheck = deviceStatusManagementService.getDeviceStatusCheck(tenantId); - boolean isValidState = deviceStatusManagementService.isDeviceStatusValid(device.getType(),device.getEnrolmentInfo().getStatus().name(),tenantId); + boolean isValidState = deviceStatusManagementService.isDeviceStatusValid(device.getType(), + device.getEnrolmentInfo().getStatus().name(),tenantId); if (updatedRows == 1 && !deviceStatusManagementService.getDeviceStatusCheck(tenantId)){ enrollmentDAO.addDeviceStatus(device.getEnrolmentInfo().getId(), device.getEnrolmentInfo().getStatus()); } else if (updatedRows ==1 && isEnableDeviceStatusCheck && isValidState ) { @@ -501,7 +525,15 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } DeviceManagementDAOFactory.commitTransaction(); - log.info("Device enrolled successfully", deviceEnrolmentLogContextBuilder.setDeviceId(String.valueOf(currentDevice.getId())).setDeviceType(String.valueOf(currentDevice.getType())).setOwner(currentDevice.getEnrolmentInfo().getOwner()).setOwnership(String.valueOf(currentDevice.getEnrolmentInfo().getOwnership())).setTenantID(String.valueOf(tenantId)).setTenantDomain(tenantDomain).setUserName(userName).build()); + log.info("Device enrollment modified successfully", + deviceEnrolmentLogContextBuilder.setDeviceId(String.valueOf(currentDevice.getId())) + .setDeviceType(String.valueOf(currentDevice.getType())) + .setOwner(currentDevice.getEnrolmentInfo().getOwner()) + .setOwnership(String.valueOf(currentDevice.getEnrolmentInfo().getOwnership())) + .setTenantID(String.valueOf(tenantId)) + .setTenantDomain(tenantDomain) + .setUserName(userName).build()); + this.removeDeviceFromCache(deviceIdentifier); } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); @@ -616,20 +648,27 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } else if (updatedRows ==1 && isEnableDeviceStatusCheck && isValidState ) { enrollmentDAO.addDeviceStatus(device.getEnrolmentInfo().getId(), device.getEnrolmentInfo().getStatus()); } - deviceDAO.updateDevice(device, tenantId); DeviceManagementDAOFactory.commitTransaction(); this.removeDeviceFromCache(deviceId); - //procees to dis-enroll a device from traccar starts + //process to dis-enroll a device from traccar starts if (HttpReportingUtil.isTrackerEnabled()) { - DeviceManagementDataHolder.getInstance().getTraccarManagementService().unLinkTraccarDevice(device.getEnrolmentInfo().getId()); + DeviceManagementDataHolder.getInstance().getTraccarManagementService() + .unLinkTraccarDevice(device.getEnrolmentInfo().getId()); } else { if (log.isDebugEnabled()) { log.debug("Traccar is disabled"); } } - //procees to dis-enroll a device from traccar ends - log.info("Device disenrolled successfully", deviceEnrolmentLogContextBuilder.setDeviceId(String.valueOf(device.getId())).setDeviceType(String.valueOf(device.getType())).setOwner(device.getEnrolmentInfo().getOwner()).setOwnership(String.valueOf(device.getEnrolmentInfo().getOwnership())).setTenantID(String.valueOf(tenantId)).setTenantDomain(tenantDomain).setUserName(userName).build()); + //process to dis-enroll a device from traccar ends + log.info("Device disenrolled successfully", + deviceEnrolmentLogContextBuilder.setDeviceId(String.valueOf(device.getId())) + .setDeviceType(String.valueOf(device.getType())) + .setOwner(device.getEnrolmentInfo().getOwner()) + .setOwnership(String.valueOf(device.getEnrolmentInfo().getOwnership())) + .setTenantID(String.valueOf(tenantId)) + .setTenantDomain(tenantDomain) + .setUserName(userName).build()); } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); String msg = "Error occurred while dis-enrolling '" + deviceId.getType() + @@ -4456,15 +4495,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } catch (MetadataManagementException e) { throw new RuntimeException(e); } - try { - deviceDAO.updateDevice(device, tenantId); - } catch (DeviceManagementDAOException e) { - DeviceManagementDAOFactory.rollbackTransaction(); - String msg = "Error occurred while updating device: " + - device.getName(); - log.error(msg, e); - throw new DeviceManagementException(msg, e); - } } } DeviceManagementDAOFactory.commitTransaction(); 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/DevicePersistTests.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/DevicePersistTests.java index f069c1fb43..e76a4f7850 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/DevicePersistTests.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/DevicePersistTests.java @@ -18,6 +18,7 @@ package io.entgra.device.mgt.core.device.mgt.core.dao; +import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.testng.Assert; @@ -33,6 +34,8 @@ import io.entgra.device.mgt.core.device.mgt.core.common.TestDataHolder; import io.entgra.device.mgt.core.device.mgt.core.dto.DeviceType; import java.sql.*; +import java.util.Collections; +import java.util.List; public class DevicePersistTests extends BaseDeviceManagementTest { @@ -224,4 +227,60 @@ public class DevicePersistTests extends BaseDeviceManagementTest { DeviceManagementDAOFactory.closeConnection(); } } + + @Test(dependsOnMethods = "testAddDeviceTest") + public void getDevicesByIdentifiersTest() throws DeviceManagementDAOException, TransactionManagementException { + Device device = TestDataHolder.initialTestDevice; + try { + DeviceManagementDAOFactory.openConnection(); + DeviceIdentifier deviceId = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()); + List retrieved = deviceDAO.getDevicesByIdentifiers( + Collections.singletonList(deviceId.getId()), TestDataHolder.SUPER_TENANT_ID); + Assert.assertEquals(1, retrieved.size(), "Device count is not matched to expected."); + } catch (DeviceManagementDAOException | SQLException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the current status of the " + + "enrolment", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "testAddDeviceTest") + public void recordDeviceUpdateTest() throws DeviceManagementDAOException, TransactionManagementException { + Device device = TestDataHolder.initialTestDevice; + try { + DeviceManagementDAOFactory.beginTransaction(); + device = deviceDAO.getDevice(device.getDeviceIdentifier(), TestDataHolder.SUPER_TENANT_ID); + log.info("Device before update: " + device); + DeviceIdentifier deviceId = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()); + boolean updated = deviceDAO.recordDeviceUpdate(deviceId, TestDataHolder.SUPER_TENANT_ID); + Assert.assertTrue(updated, "Device timestamp is not updated."); + Device updatedDevice = deviceDAO.getDevice(device.getDeviceIdentifier(), TestDataHolder.SUPER_TENANT_ID); + log.info("Device after update: " + updatedDevice); + Assert.assertTrue(device.getLastUpdatedTimeStamp() < updatedDevice.getLastUpdatedTimeStamp(), + "Last updated timestamp is way older."); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the current status of the " + + "enrolment", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "testAddDeviceTest") + public void getDeviceByStatusTest() throws DeviceManagementDAOException, TransactionManagementException { + try { + DeviceManagementDAOFactory.beginTransaction(); + PaginationRequest pr = new PaginationRequest(0, 10); + pr.setStatusList(Collections.singletonList(Status.ACTIVE.name())); + List results = deviceDAO.getDevicesByStatus(pr, TestDataHolder.SUPER_TENANT_ID); + Assert.assertEquals(1, results.size(), "No device returned"); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the current status of the " + + "enrolment", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + }