Performance improvements

appm_improvement
Charitha Goonetilleke 7 months ago
parent d1fe1baad0
commit 7dea32d2e3

@ -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;

@ -137,10 +137,10 @@
</systemPropertyVariables>
<suiteXmlFiles>
<file>src/test/resources/testng.xml</file>
<file>src/test/resources/mysql-testng.xml</file>
<file>src/test/resources/mssql-testng.xml</file>
<file>src/test/resources/oracle-testng.xml</file>
<file>src/test/resources/postgre-testng.xml</file>
<!-- <file>src/test/resources/mysql-testng.xml</file>-->
<!-- <file>src/test/resources/mssql-testng.xml</file>-->
<!-- <file>src/test/resources/oracle-testng.xml</file>-->
<!-- <file>src/test/resources/postgre-testng.xml</file>-->
</suiteXmlFiles>
</configuration>
</plugin>

@ -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;

@ -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<Device> 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<Device> 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<Device> 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<Device> 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<Device> getGroupedDevicesDetails(PaginationRequest request, List<Integer> deviceIds, String groupName,
int tenantId) throws DeviceManagementDAOException {
int limitValue = request.getRowCount();
int offsetValue = request.getStartIndex();
List<String> status = request.getStatusList();
String name = request.getDeviceName();
String user = request.getOwner();
String ownership = request.getOwnership();
try {
List<Device> 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<Integer> deviceIds, String groupName,
int tenantId) throws DeviceManagementDAOException {
@ -3216,7 +3130,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
List<Device> 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<Device> 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<Device> validDevices)
throws DeviceManagementDAOException;
}

@ -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 " +

@ -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<Device> 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<Device> 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<Device> 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<>();
@ -1064,10 +1071,8 @@ 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 " +
"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<Device> 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<Device> 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<Device> devices = new ArrayList<>();
@ -1557,7 +1550,143 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
}
private Connection getConnection() throws SQLException {
return DeviceManagementDAOFactory.getConnection();
@Override
public List<Device> getGroupedDevicesDetails(PaginationRequest request, List<Integer> deviceIds, String groupName,
int tenantId) throws DeviceManagementDAOException {
int limitValue = request.getRowCount();
int offsetValue = request.getStartIndex();
List<String> status = request.getStatusList();
String name = request.getDeviceName();
String user = request.getOwner();
String ownership = request.getOwnership();
try {
List<Device> 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<Device> 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);
}
}
}

@ -45,25 +45,26 @@ public class GenericGroupDAOImpl extends AbstractGroupDAOImpl {
List<Device> 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()) {

@ -192,16 +192,17 @@ public class OracleGroupDAOImpl extends AbstractGroupDAOImpl {
List<Device> 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);

@ -133,16 +133,17 @@ public class PostgreSQLGroupDAOImpl extends AbstractGroupDAOImpl {
List<Device> 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);

@ -192,16 +192,17 @@ public class SQLServerGroupDAOImpl extends AbstractGroupDAOImpl {
List<Device> 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);

@ -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;
}

@ -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());
}
DeviceManagementDAOFactory.commitTransaction();
//TODO :: This has to be fixed by adding the enrollment ID.

@ -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;

@ -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();

@ -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<Device> 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<Device> 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();
}
}
}

Loading…
Cancel
Save