diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/Filter.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/Filter.java index 272e9ba8a3..88f7e90a0a 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/Filter.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/Filter.java @@ -118,6 +118,11 @@ public class Filter { */ private boolean isNotRetired; + /** + * To check whether web applications should be returned + */ + private boolean withWebApps; + public int getLimit() { return limit; } @@ -221,4 +226,12 @@ public class Filter { public void setNotRetired(boolean notRetired) { isNotRetired = notRetired; } + + public boolean isWithWebApps() { + return withWebApps; + } + + public void setWithWebApps(boolean withWebApps) { + this.withWebApps = withWebApps; + } } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java index dd4a517dd0..3047baf25b 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java @@ -174,7 +174,11 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic sql += "AND AP_APP_RELEASE.CURRENT_STATE = ? "; } if (deviceTypeId != -1) { - sql += "AND AP_APP.DEVICE_TYPE_ID = ? "; + sql += "AND (AP_APP.DEVICE_TYPE_ID = ? "; + if (filter.isWithWebApps()) { + sql += "OR AP_APP.DEVICE_TYPE_ID = 0 "; + } + sql += ") "; } if (filter.isNotRetired()) { sql += "AND AP_APP.STATUS != 'RETIRED' "; @@ -305,7 +309,11 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic sql += " AND AP_APP_RELEASE.CURRENT_STATE = ?"; } if (deviceTypeId != -1) { - sql += " AND AP_APP.DEVICE_TYPE_ID = ?"; + sql += "AND (AP_APP.DEVICE_TYPE_ID = ? "; + if (filter.isWithWebApps()) { + sql += "OR AP_APP.DEVICE_TYPE_ID = 0 "; + } + sql += ") "; } if (filter.isNotRetired()) { sql += " AND AP_APP.STATUS != 'RETIRED'"; diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/dm-db-h2.sql b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/dm-db-h2.sql index ad1493e17a..bd56e1ce0f 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/dm-db-h2.sql +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/dm-db-h2.sql @@ -125,6 +125,8 @@ CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( ID INTEGER AUTO_INCREMENT NOT NULL, DEVICE_ID INTEGER NOT NULL, + DEVICE_TYPE VARCHAR(300) NOT NULL, + DEVICE_IDENTIFICATION VARCHAR(300) NOT NULL, OWNER VARCHAR(255) NOT NULL, OWNERSHIP VARCHAR(45) DEFAULT NULL, STATUS VARCHAR(50) NULL, diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/sql/h2.sql b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/sql/h2.sql index c079d1e214..a842fdf2ab 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/sql/h2.sql +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/sql/h2.sql @@ -149,6 +149,8 @@ CREATE TABLE IF NOT EXISTS DM_OPERATION ( CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( ID INTEGER AUTO_INCREMENT NOT NULL, DEVICE_ID INTEGER NOT NULL, + DEVICE_TYPE VARCHAR(300) NOT NULL, + DEVICE_IDENTIFICATION VARCHAR(300) NOT NULL, OWNER VARCHAR(50) NOT NULL, OWNERSHIP VARCHAR(45) DEFAULT NULL, STATUS VARCHAR(50) NULL, diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/GroupManagementServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/GroupManagementServiceImpl.java index 0373c9c367..7681176ecc 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/GroupManagementServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/GroupManagementServiceImpl.java @@ -279,7 +279,7 @@ public class GroupManagementServiceImpl implements GroupManagementService { ); return Response.status(Response.Status.OK).build(); } catch (GroupManagementException e) { - String msg = "Error occurred while adding new group."; + String msg = "Error occurred while updating group. "; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } catch (GroupNotExistException e) { diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/util/RequestValidationUtil.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/util/RequestValidationUtil.java index b4e1d5b57a..e3e319272c 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/util/RequestValidationUtil.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/util/RequestValidationUtil.java @@ -562,7 +562,11 @@ public class RequestValidationUtil { } if (operationCode != null && !operationCode.isEmpty()) { - validateOperationCodeFiltering(operationCode, type); + /* + Commenting this as dynamic device types doesn't have configuration based feature manager which + used to define fixed set of operation codes. + */ +// validateOperationCodeFiltering(operationCode, type); operationLogFilters.setOperationCode(operationCode); } return operationLogFilters; 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 7b53197118..e88fb52ae0 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.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/DeviceIdentifier.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/DeviceIdentifier.java index a1bb3fe51e..d02f844071 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/DeviceIdentifier.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/DeviceIdentifier.java @@ -69,9 +69,20 @@ public class DeviceIdentifier implements Serializable{ @Override public String toString() { - return "deviceId {" + - "id='" + id + '\'' + - ", type='" + type + '\'' + - '}'; + return type + "|" + id; } + + @Override + public int hashCode() { + return toString().hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof DeviceIdentifier) { + return (this.hashCode() == obj.hashCode()); + } + return false; + } + } 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 047c073084..7804660e9f 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 @@ -301,10 +301,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/DeviceManagementConstants.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/DeviceManagementConstants.java index 6fae7901d5..9f6f24db08 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/DeviceManagementConstants.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/DeviceManagementConstants.java @@ -89,6 +89,7 @@ public final class DeviceManagementConstants { public static final String POLICY_OPERATION_CODE = PolicyOperation.POLICY_OPERATION_CODE; public static final String POLICY_REVOKE_OPERATION_CODE = OperationMgtConstants.OperationCodes.POLICY_REVOKE; public static final String EVENT_CONFIG_OPERATION_CODE = OperationMgtConstants.OperationCodes.EVENT_CONFIG; + public static final String EVENT_UPDATE_OPERATION_CODE = OperationMgtConstants.OperationCodes.EVENT_UPDATE; public static final String EVENT_REVOKE_OPERATION_CODE = OperationMgtConstants.OperationCodes.EVENT_REVOKE; } 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/cache/DeviceCacheKey.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/cache/DeviceCacheKey.java index a5931f408e..d25cd2673a 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/cache/DeviceCacheKey.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/cache/DeviceCacheKey.java @@ -28,7 +28,6 @@ public class DeviceCacheKey { private String deviceId; private String deviceType; private int tenantId; - private volatile int hashCode; public String getDeviceId() { return deviceId; @@ -55,27 +54,21 @@ public class DeviceCacheKey { } @Override - public boolean equals(Object obj) { - if (obj == null) { - return false; - } - if (!DeviceCacheKey.class.isAssignableFrom(obj.getClass())) { - return false; - } - final DeviceCacheKey other = (DeviceCacheKey) obj; - String thisId = this.deviceId + "-" + this.deviceType + "_" + this.tenantId; - String otherId = other.deviceId + "-" + other.deviceType + "_" + other.tenantId; - if (!thisId.equals(otherId)) { - return false; - } - return true; + public int hashCode() { + return toString().hashCode(); } @Override - public int hashCode() { - if (hashCode == 0) { - hashCode = Objects.hash(deviceId, deviceType, tenantId); + public String toString() { + return tenantId + "|" + deviceType + "|" + deviceId; + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof DeviceCacheKey) { + return (this.hashCode() == obj.hashCode()); } - return hashCode; + return false; } + } 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 b708ca7cf6..0c9455fc6b 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; @@ -477,6 +480,7 @@ public interface DeviceDAO { * @return returns list of device types. * @throws DeviceManagementDAOException */ + @Deprecated List getDeviceTypes() throws DeviceManagementDAOException; /** diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/EnrollmentDAO.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/EnrollmentDAO.java index 719f1c648f..302c5ff721 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/EnrollmentDAO.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/EnrollmentDAO.java @@ -18,6 +18,7 @@ package io.entgra.device.mgt.core.device.mgt.core.dao; import io.entgra.device.mgt.core.device.mgt.common.Device; +import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier; import io.entgra.device.mgt.core.device.mgt.common.EnrolmentInfo; import io.entgra.device.mgt.core.device.mgt.common.EnrolmentInfo.Status; @@ -25,7 +26,8 @@ import java.util.List; public interface EnrollmentDAO { - EnrolmentInfo addEnrollment(int deviceId, EnrolmentInfo enrolmentInfo, int tenantId) throws DeviceManagementDAOException; + EnrolmentInfo addEnrollment(int deviceId, DeviceIdentifier deviceIdentifier, + EnrolmentInfo enrolmentInfo, int tenantId) throws DeviceManagementDAOException; int updateEnrollment(EnrolmentInfo enrolmentInfo, int tenantId) throws DeviceManagementDAOException; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index ba47b628cf..2059d4faf3 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,8 +160,9 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { + "d1.ID AS DEVICE_ID, " + "d1.DESCRIPTION, " + "d1.NAME AS DEVICE_NAME, " - + "d1.DEVICE_TYPE, " - + "d1.DEVICE_IDENTIFICATION, " + + "d1.LAST_UPDATED_TIMESTAMP, " + + "e.DEVICE_TYPE, " + + "e.DEVICE_IDENTIFICATION, " + "e.OWNER, " + "e.OWNERSHIP, " + "e.STATUS, " @@ -150,11 +175,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { + "d.ID, " + "d.DESCRIPTION, " + "d.NAME, " - + "t.NAME AS DEVICE_TYPE, " - + "d.DEVICE_IDENTIFICATION " - + "FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE " - + "t.NAME = ? AND " - + "t.ID = d.DEVICE_TYPE_ID AND " + + "d.LAST_UPDATED_TIMESTAMP " + + "FROM DM_DEVICE d WHERE " + "d.DEVICE_IDENTIFICATION = ? AND " + "d.TENANT_ID = ?"; @@ -175,7 +197,6 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { try (PreparedStatement stmt = conn.prepareStatement(sql)) { int paramIndx = 1; - stmt.setString(paramIndx++, deviceData.getDeviceIdentifier().getType()); stmt.setString(paramIndx++, deviceData.getDeviceIdentifier().getId()); stmt.setInt(paramIndx++, tenantId); if (deviceData.getLastModifiedDate() != null) { @@ -212,23 +233,25 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { Device device = null; try { conn = this.getConnection(); - String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + - "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, " + - "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME, " + - "t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE " + - "t.NAME = ? AND t.ID = d.DEVICE_TYPE_ID AND d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID " + - "AND TENANT_ID = ? ORDER BY e.DATE_OF_LAST_UPDATE DESC, e.STATUS ASC"; - // Status adeed as an orderby clause to fix a bug : when an existing device is + String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, " + + "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 = ?) d1 WHERE d1.ID = e.DEVICE_ID " + + "AND e.DEVICE_TYPE = ? AND TENANT_ID = ? ORDER BY e.DATE_OF_LAST_UPDATE DESC, e.STATUS ASC"; + // Status added as an order-by clause to fix a bug : when an existing device is // re-enrolled, earlier enrollment is marked as removed and a new enrollment is added. // However, both enrollments share the same time stamp. When retrieving the device - // due to same timestamp, enrollment information is incorrect, intermittently. Hence + // due to same timestamp, enrollment information is incorrect, intermittently. Hence, // status also should be taken into consideration when ordering. This should not present a // problem for other status transitions, as there would be an intermediary removed // state in between. stmt = conn.prepareStatement(sql); - stmt.setString(1, deviceIdentifier.getType()); - stmt.setString(2, deviceIdentifier.getId()); - stmt.setInt(3, tenantId); + stmt.setString(1, deviceIdentifier.getId()); + stmt.setInt(2, tenantId); + stmt.setString(3, deviceIdentifier.getType()); stmt.setInt(4, tenantId); rs = stmt.executeQuery(); if (rs.next()) { @@ -252,16 +275,14 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { try { conn = this.getConnection(); String sql = "SELECT " + - "d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + - "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, " + - "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, t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION " + - "FROM " + - "DM_DEVICE d, DM_DEVICE_TYPE t " + - "WHERE " + - "t.ID = d.DEVICE_TYPE_ID AND d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ?) d1 " + + " (SELECT d.ID, d.DESCRIPTION, d.NAME, d.LAST_UPDATED_TIMESTAMP " + + "FROM DM_DEVICE d " + + "WHERE d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ?) d1 " + "WHERE " + "d1.ID = e.DEVICE_ID " + "AND TENANT_ID = ? " + @@ -299,16 +320,18 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { Device device = null; try { conn = this.getConnection(); - String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + - "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, " + - "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME, " + - "t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE " + - "t.NAME = ? AND t.ID = d.DEVICE_TYPE_ID AND d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ?) d1 WHERE 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.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ?) d1 " + + "WHERE e.DEVICE_TYPE = ? AND d1.ID = e.DEVICE_ID " + "AND TENANT_ID = ? AND e.OWNER = ? ORDER BY e.DATE_OF_LAST_UPDATE DESC"; stmt = conn.prepareStatement(sql); - stmt.setString(1, deviceIdentifier.getType()); - stmt.setString(2, deviceIdentifier.getId()); - stmt.setInt(3, tenantId); + stmt.setString(1, deviceIdentifier.getId()); + stmt.setInt(2, tenantId); + stmt.setString(3, deviceIdentifier.getType()); stmt.setInt(4, tenantId); stmt.setString(5, owner); rs = stmt.executeQuery(); @@ -333,18 +356,20 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { Device device = null; try { conn = this.getConnection(); - String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + - "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, " + - "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME, " + - "t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t " + - "WHERE t.NAME = ? AND t.ID = d.DEVICE_TYPE_ID AND d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ? " + - "AND d.LAST_UPDATED_TIMESTAMP > ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? ORDER BY e.DATE_OF_LAST_UPDATE DESC"; + String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, " + + "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"; stmt = conn.prepareStatement(sql); int paramIdx = 1; - stmt.setString(paramIdx++, deviceIdentifier.getType()); stmt.setString(paramIdx++, deviceIdentifier.getId()); stmt.setInt(paramIdx++, tenantId); stmt.setTimestamp(paramIdx++, new Timestamp(since.getTime())); + stmt.setString(paramIdx++, deviceIdentifier.getType()); stmt.setInt(paramIdx, tenantId); rs = stmt.executeQuery(); if (rs.next()) { @@ -412,7 +437,6 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { List deviceProperties = new ArrayList<>(); try { conn = this.getConnection(); - List> outputLists = new ArrayList<>(); String sql = "SELECT " + "p.DEVICE_IDENTIFICATION, " + "p.DEVICE_TYPE_NAME, " + @@ -426,7 +450,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { int iterationCount = 0; StringBuilder propertyQuery = new StringBuilder(" "); - for (Map.Entry stringStringEntry : deviceProps.entrySet()) { + for (Map.Entry ignored : deviceProps.entrySet()) { String tempTableId = "t".concat(Integer.toString(iterationCount++)); propertyQuery.append("JOIN DM_DEVICE_PROPERTIES ") .append(tempTableId).append(" ").append("ON p.DEVICE_IDENTIFICATION = ") @@ -526,17 +550,17 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { Device device = null; try { conn = this.getConnection(); - String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + - "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, " + + 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, " + - "t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION " + + "(SELECT d.ID, d.DESCRIPTION, d.NAME, d.LAST_UPDATED_TIMESTAMP " + "FROM" + - " DM_DEVICE d, DM_DEVICE_TYPE t " + + " DM_DEVICE d " + "WHERE " + - "t.ID = d.DEVICE_TYPE_ID AND d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ? AND" + + "d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ? AND" + " d.LAST_UPDATED_TIMESTAMP > ?) d1 " + "WHERE" + " d1.ID = e.DEVICE_ID AND TENANT_ID = ? " + @@ -570,20 +594,22 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { Device device = null; try { conn = this.getConnection(); - String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + - "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, " + - "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME, " + - "t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t " + - "WHERE t.NAME = ? AND t.ID = d.DEVICE_TYPE_ID AND d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ? " + + String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, " + + "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 = ? " + - "ORDER BY e.DATE_OF_LAST_UPDATE DESC"; + "AND e.DEVICE_TYPE = ? ORDER BY e.DATE_OF_LAST_UPDATE DESC"; stmt = conn.prepareStatement(sql); - stmt.setString(1, deviceIdentifier.getType()); - stmt.setString(2, deviceIdentifier.getId()); - stmt.setInt(3, tenantId); - stmt.setTimestamp(4, new Timestamp(since.getTime())); - stmt.setInt(5, tenantId); - stmt.setString(6, owner); + stmt.setString(1, deviceIdentifier.getId()); + stmt.setInt(2, tenantId); + stmt.setTimestamp(3, new Timestamp(since.getTime())); + stmt.setInt(4, tenantId); + stmt.setString(5, owner); + stmt.setString(6, deviceIdentifier.getType()); rs = stmt.executeQuery(); if (rs.next()) { device = DeviceManagementDAOUtil.loadMatchingDevice(rs, false); @@ -606,16 +632,18 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { Device device = null; try { conn = this.getConnection(); - String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + - "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, " + - "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME, " + - "t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE " + - "t.NAME = ? AND t.ID = d.DEVICE_TYPE_ID AND d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ?) d1 WHERE 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 d1.ID = e.DEVICE_ID AND e.DEVICE_TYPE = ? AND e.DEVICE_IDENTIFICATION = ? " + "AND TENANT_ID = ? AND e.STATUS = ? ORDER BY e.DATE_OF_LAST_UPDATE DESC"; stmt = conn.prepareStatement(sql); - stmt.setString(1, deviceIdentifier.getType()); - stmt.setString(2, deviceIdentifier.getId()); - stmt.setInt(3, tenantId); + stmt.setInt(1, tenantId); + stmt.setString(2, deviceIdentifier.getType()); + stmt.setString(3, deviceIdentifier.getId()); stmt.setInt(4, tenantId); stmt.setString(5, status.toString()); rs = stmt.executeQuery(); @@ -639,9 +667,10 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { String sql = "SELECT d1.ID AS DEVICE_ID, " + "d1.DESCRIPTION, " + "d1.NAME AS DEVICE_NAME, " + - "d1.DEVICE_TYPE, " + + "d1.LAST_UPDATED_TIMESTAMP, " + + "e.DEVICE_TYPE, " + "e.TENANT_ID, " + - "d1.DEVICE_IDENTIFICATION, " + + "e.DEVICE_IDENTIFICATION, " + "e.OWNER, " + "e.OWNERSHIP, " + "e.STATUS, " + @@ -653,19 +682,16 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { "(SELECT d.ID, " + "d.DESCRIPTION, " + "d.NAME, " + - "t.NAME AS DEVICE_TYPE, " + - "d.DEVICE_IDENTIFICATION " + - "FROM DM_DEVICE d, " + - "DM_DEVICE_TYPE t " + - "WHERE t.NAME = ? " + - "AND t.ID = d.DEVICE_TYPE_ID " + - "AND d.DEVICE_IDENTIFICATION = ?) d1 " + - "WHERE d1.ID = e.DEVICE_ID " + + "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 = ? " + "ORDER BY e.DATE_OF_LAST_UPDATE DESC"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { - stmt.setString(1, deviceIdentifier.getType()); - stmt.setString(2, deviceIdentifier.getId()); + stmt.setString(1, deviceIdentifier.getId()); + stmt.setString(2, deviceIdentifier.getType()); try (ResultSet rs = stmt.executeQuery()) { SingletonMap deviceMap = null; @@ -695,11 +721,12 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { Device device = null; try { conn = this.getConnection(); - String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + - "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, " + - "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME, " + - "t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE " + - "d.ID = ? AND t.ID = d.DEVICE_TYPE_ID AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID " + + String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, " + + "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); stmt.setInt(1, deviceId); @@ -726,11 +753,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, 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 AS DEVICE_ID, " + - "d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " + - "DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?) d1 " + + 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); @@ -758,24 +785,21 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { List devices = null; try { conn = this.getConnection(); - String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + - "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, " + - "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, " + - "d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " + - "DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND t.NAME = ? " + - "AND t.ID = d.DEVICE_TYPE_ID AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ?" + - " ORDER BY e.DATE_OF_LAST_UPDATE DESC"; + 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.setString(1, type); - stmt.setInt(2, tenantId); + stmt.setInt(1, tenantId); + stmt.setString(2, type); stmt.setInt(3, tenantId); rs = stmt.executeQuery(); devices = new ArrayList<>(); while (rs.next()) { Device device = DeviceManagementDAOUtil.loadActiveDevice(rs, false); - if (device != null) { - devices.add(device); - } + devices.add(device); } } catch (SQLException e) { String msg = "Error occurred while listing devices for type '" + type + "'"; @@ -791,40 +815,38 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { @Override public List getAllocatedDevices(String type, int tenantId, int activeServerCount, int serverIndex) throws DeviceManagementDAOException { - List devices = null; + List devices; try { Connection conn = this.getConnection(); String sql = "SELECT d1.ID AS DEVICE_ID," + - " d1.DESCRIPTION," + - " d1.NAME AS DEVICE_NAME," + - " d1.DEVICE_TYPE," + - " d1.DEVICE_IDENTIFICATION," + - " e.OWNER," + - " e.OWNERSHIP," + - " e.STATUS," + - " e.IS_TRANSFERRED," + - " e.DATE_OF_LAST_UPDATE," + - " e.DATE_OF_ENROLMENT," + - " e.ID AS ENROLMENT_ID " + - "FROM DM_ENROLMENT e," + - " (SELECT d.ID," + - " d.DESCRIPTION," + - " d.NAME," + - " d.DEVICE_IDENTIFICATION," + - " t.NAME AS DEVICE_TYPE" + - " FROM DM_DEVICE d, DM_DEVICE_TYPE t" + - " WHERE DEVICE_TYPE_ID = t.ID" + - " AND t.NAME = ?" + - " AND t.ID = d.DEVICE_TYPE_ID" + - " AND d.TENANT_ID = ?) d1 " + - "WHERE d1.ID = e.DEVICE_ID" + - " AND TENANT_ID = ?" + - " AND MOD(d1.ID, ?) = ? " + - "ORDER BY e.DATE_OF_LAST_UPDATE DESC"; + " d1.DESCRIPTION," + + " d1.NAME AS DEVICE_NAME," + + " 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.TENANT_ID = ?" + + " AND MOD(d1.ID, ?) = ? " + + "ORDER BY e.DATE_OF_LAST_UPDATE DESC"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { - stmt.setString(1, type); - stmt.setInt(2, tenantId); + stmt.setInt(1, tenantId); + stmt.setString(2, type); stmt.setInt(3, tenantId); stmt.setInt(4, activeServerCount); stmt.setInt(5, serverIndex); @@ -833,20 +855,18 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { try (ResultSet rs = stmt.executeQuery()) { while (rs.next()) { Device device = DeviceManagementDAOUtil.loadActiveDevice(rs, false); - if (device != null) { - devices.add(device); - } + devices.add(device); } } catch (Exception e) { String msg = "Error encountered while populating allocated active devices for server with index : " + serverIndex + - " active-server-count " + activeServerCount + " device-type " + type + " tenant-id " + tenantId; + " active-server-count " + activeServerCount + " device-type " + type + " tenant-id " + tenantId; log.error(msg, e); throw new DeviceManagementDAOException(msg, e); } } } catch (SQLException e) { String msg = "Error encountered while retrieving allocated devices for server with index : " + serverIndex + - " active-server-count " + activeServerCount + " device-type " + type + " tenant-id " + tenantId; + " active-server-count " + activeServerCount + " device-type " + type + " tenant-id " + tenantId; log.error(msg, e); throw new DeviceManagementDAOException(msg, e); } @@ -861,12 +881,13 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { List devices = new ArrayList<>(); try { conn = this.getConnection(); - String sql = "SELECT e1.OWNER, e1.OWNERSHIP, e1.ENROLMENT_ID, e1.DEVICE_ID, e1.STATUS, e1.IS_TRANSFERRED, e1.DATE_OF_LAST_UPDATE," + - " e1.DATE_OF_ENROLMENT, d.DESCRIPTION, d.NAME AS DEVICE_NAME, d.DEVICE_IDENTIFICATION, t.NAME " + - "AS DEVICE_TYPE FROM DM_DEVICE d, (SELECT e.OWNER, e.OWNERSHIP, e.ID AS ENROLMENT_ID, " + - "e.DEVICE_ID, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT FROM DM_ENROLMENT e WHERE " + - "e.TENANT_ID = ? AND LOWER(e.OWNER) = LOWER(?)) e1, DM_DEVICE_TYPE t WHERE d.ID = e1.DEVICE_ID " + - "AND t.ID = d.DEVICE_TYPE_ID ORDER BY e1.DATE_OF_LAST_UPDATE DESC"; + String sql = "SELECT e1.OWNER, e1.OWNERSHIP, e1.ENROLMENT_ID, e1.DEVICE_ID, e1.STATUS, e1.IS_TRANSFERRED, " + + "e1.DATE_OF_LAST_UPDATE, e1.DATE_OF_ENROLMENT, d.DESCRIPTION, d.NAME AS DEVICE_NAME, " + + "d.DEVICE_IDENTIFICATION, 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 " + + "WHERE d.ID = e1.DEVICE_ID ORDER BY e1.DATE_OF_LAST_UPDATE DESC"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); stmt.setString(2, username); @@ -894,12 +915,13 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { List devices = new ArrayList<>(); try { conn = this.getConnection(); - String sql = "SELECT e1.OWNER, e1.OWNERSHIP, e1.ENROLMENT_ID, e1.DEVICE_ID, e1.STATUS, e1.IS_TRANSFERRED, e1.DATE_OF_LAST_UPDATE," + - " e1.DATE_OF_ENROLMENT, d.DESCRIPTION, d.NAME AS DEVICE_NAME, d.DEVICE_IDENTIFICATION, t.NAME " + - "AS DEVICE_TYPE FROM DM_DEVICE d, (SELECT e.OWNER, e.OWNERSHIP, e.ID AS ENROLMENT_ID, " + - "e.DEVICE_ID, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT FROM DM_ENROLMENT e WHERE " + - "e.TENANT_ID = ? AND LOWER(e.OWNER) = LOWER(?) ORDER BY e.DATE_OF_LAST_UPDATE DESC) e1, DM_DEVICE_TYPE t WHERE d.ID = e1.DEVICE_ID " + - "AND t.ID = d.DEVICE_TYPE_ID AND t.NAME= ?"; + String sql = "SELECT e1.OWNER, e1.OWNERSHIP, e1.ENROLMENT_ID, e1.DEVICE_ID, e1.STATUS, e1.IS_TRANSFERRED, " + + "e1.DATE_OF_LAST_UPDATE, e1.DATE_OF_ENROLMENT, d.DESCRIPTION, d.NAME AS DEVICE_NAME, " + + "d.DEVICE_IDENTIFICATION, 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 " + + "WHERE d.ID = e1.DEVICE_ID AND e1.DEVICE_TYPE = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); stmt.setString(2, username); @@ -928,7 +950,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { return devices; } Connection conn = this.getConnection(); - StringJoiner joiner = new StringJoiner(",","SELECT " + StringJoiner joiner = new StringJoiner(",", "SELECT " + "e1.OWNER, " + "e1.OWNERSHIP, " + "e1.ENROLMENT_ID, " @@ -940,40 +962,38 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { + "d.DESCRIPTION, " + "d.NAME AS DEVICE_NAME, " + "d.DEVICE_IDENTIFICATION, " - + "t.NAME AS DEVICE_TYPE " + + "d.LAST_UPDATED_TIMESTAMP, " + + "e1.DEVICE_TYPE " + "FROM " + "DM_DEVICE d, " + "(SELECT " - + "e.OWNER, " - + "e.OWNERSHIP, " - + "e.ID AS ENROLMENT_ID, " - + "e.DEVICE_ID, " - + "e.STATUS, " - + "e.IS_TRANSFERRED, " - + "e.DATE_OF_LAST_UPDATE, " - + "e.DATE_OF_ENROLMENT " - + "FROM " - + "DM_ENROLMENT e " - + "WHERE " - + "e.TENANT_ID = ? AND " - + "LOWER(e.OWNER) = LOWER(?) AND " - + "e.STATUS IN (", - ")) e1, " - + "DM_DEVICE_TYPE t " - + "WHERE d.ID = e1.DEVICE_ID AND " - + "t.ID = d.DEVICE_TYPE_ID " - + "ORDER BY e1.DATE_OF_LAST_UPDATE DESC"); + + "e.OWNER, " + + "e.DEVICE_TYPE, " + + "e.OWNERSHIP, " + + "e.ID AS ENROLMENT_ID, " + + "e.DEVICE_ID, " + + "e.STATUS, " + + "e.IS_TRANSFERRED, " + + "e.DATE_OF_LAST_UPDATE, " + + "e.DATE_OF_ENROLMENT " + + "FROM " + + "DM_ENROLMENT e " + + "WHERE " + + "e.TENANT_ID = ? AND " + + "LOWER(e.OWNER) = LOWER(?) AND " + + "e.STATUS IN (", + ")) e1 ORDER BY e1.DATE_OF_LAST_UPDATE DESC"); deviceStatuses.stream().map(ignored -> "?").forEach(joiner::add); - String query = joiner.toString(); + String query = joiner.toString(); try (PreparedStatement stmt = conn.prepareStatement(query)) { int index = 1; stmt.setInt(index++, tenantId); stmt.setString(index++, username); - for (String deviceId : deviceStatuses) { - stmt.setObject(index++, deviceId); - } + for (String status : deviceStatuses) { + stmt.setObject(index++, status); + } try (ResultSet rs = stmt.executeQuery()) { while (rs.next()) { Device device = DeviceManagementDAOUtil.loadDevice(rs); @@ -1016,14 +1036,12 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { "(SELECT gd.DEVICE_ID, " + "gd.DESCRIPTION, " + "gd.NAME, " + - "gd.DEVICE_IDENTIFICATION, " + - "t.NAME AS DEVICE_TYPE " + + "gd.DEVICE_IDENTIFICATION " + "FROM " + "(SELECT d.ID AS DEVICE_ID, " + "d.DESCRIPTION, " + "d.NAME, " + - "d.DEVICE_IDENTIFICATION, " + - "d.DEVICE_TYPE_ID " + + "d.DEVICE_IDENTIFICATION " + "FROM DM_DEVICE d, " + "(SELECT dgm.DEVICE_ID " + "FROM DM_DEVICE_GROUP_MAP dgm " + @@ -1035,18 +1053,18 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { sql = sql + " AND d.NAME LIKE ?"; isDeviceNameProvided = true; } - sql = sql + ") gd, DM_DEVICE_TYPE t WHERE gd.DEVICE_TYPE_ID = t.ID"; + sql = sql + ") gd WHERE 1=1"; //Add query for last updated timestamp if (since != null) { sql = sql + " AND d.LAST_UPDATED_TIMESTAMP > ?"; isSinceProvided = true; } + sql = sql + " ) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? "; //Add the query for device-type if (deviceType != null && !deviceType.isEmpty()) { - sql = sql + " AND t.NAME = ?"; + sql = sql + " AND e.DEVICE_TYPE = ?"; isDeviceTypeProvided = true; } - sql = sql + " ) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? "; //Add the query for ownership if (ownership != null && !ownership.isEmpty()) { sql = sql + " AND e.OWNERSHIP = ?"; @@ -1075,10 +1093,10 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { if (isSinceProvided) { stmt.setTimestamp(paramIdx++, new Timestamp(since.getTime())); } + stmt.setInt(paramIdx++, tenantId); if (isDeviceTypeProvided) { stmt.setString(paramIdx++, deviceType); } - stmt.setInt(paramIdx++, tenantId); if (isOwnershipProvided) { stmt.setString(paramIdx++, ownership); } @@ -1108,7 +1126,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } } - private Connection getConnection() throws SQLException { + protected Connection getConnection() throws SQLException { return DeviceManagementDAOFactory.getConnection(); } @@ -1116,7 +1134,6 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { * Get device count of user. * * @return device count - * @throws DeviceManagementDAOException */ @Override public int getDeviceCount(String username, int tenantId) throws DeviceManagementDAOException { @@ -1126,13 +1143,11 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { int deviceCount = 0; try { conn = this.getConnection(); - String sql = "SELECT COUNT(d1.DEVICE_ID) AS DEVICE_COUNT FROM DM_ENROLMENT e, (SELECT d.ID AS DEVICE_ID FROM " + - "DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?) d1 WHERE " + - "d1.DEVICE_ID = e.DEVICE_ID AND LOWER(e.OWNER) = LOWER(?) AND TENANT_ID = ?"; + String sql = "SELECT COUNT(ID) AS DEVICE_COUNT FROM DM_ENROLMENT WHERE " + + "LOWER(OWNER) = LOWER(?) AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); - stmt.setInt(1, tenantId); - stmt.setString(2, username); - stmt.setInt(3, tenantId); + stmt.setString(1, username); + stmt.setInt(2, tenantId); rs = stmt.executeQuery(); if (rs.next()) { deviceCount = rs.getInt("DEVICE_COUNT"); @@ -1149,7 +1164,6 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { * Get device count of user. * * @return device count - * @throws DeviceManagementDAOException */ @Override public int getDeviceCount(String type, String status, int tenantId) throws DeviceManagementDAOException { @@ -1159,14 +1173,12 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { int deviceCount = 0; try { conn = this.getConnection(); - String sql = "SELECT COUNT(d.ID) AS DEVICE_COUNT FROM (SELECT e.DEVICE_ID FROM DM_ENROLMENT e WHERE " + - "TENANT_ID = ? AND STATUS = ?) e, DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.ID = e.DEVICE_ID AND " + - "d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ? AND t.NAME=?"; + String sql = "SELECT COUNT(ID) AS DEVICE_COUNT FROM DM_ENROLMENT WHERE " + + "DEVICE_TYPE = ? AND STATUS = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); - stmt.setInt(1, tenantId); + stmt.setString(1, type); stmt.setString(2, status); stmt.setInt(3, tenantId); - stmt.setString(4, type); rs = stmt.executeQuery(); if (rs.next()) { deviceCount = rs.getInt("DEVICE_COUNT"); @@ -1187,17 +1199,15 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { List deviceIDs = new ArrayList<>(); try { conn = this.getConnection(); - String sql = "SELECT d.DEVICE_IDENTIFICATION AS DEVICE_IDS FROM (SELECT e.DEVICE_ID FROM DM_ENROLMENT e WHERE " + - "TENANT_ID = ? AND STATUS = ?) e, DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.ID = e.DEVICE_ID AND " + - "d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ? AND t.NAME=?"; + String sql = "SELECT DEVICE_IDENTIFICATION FROM DM_ENROLMENT WHERE " + + "DEVICE_TYPE = ? AND STATUS = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); - stmt.setInt(1, tenantId); + stmt.setString(1, type); stmt.setString(2, status); stmt.setInt(3, tenantId); - stmt.setString(4, type); rs = stmt.executeQuery(); while (rs.next()) { - deviceIDs.add(rs.getString("DEVICE_IDS")); + deviceIDs.add(rs.getString("DEVICE_IDENTIFICATION")); } } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while retrieving tenants which have " + @@ -1218,13 +1228,13 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { return false; } conn = this.getConnection(); - StringBuilder sql = new StringBuilder("UPDATE DM_ENROLMENT SET STATUS = ? WHERE DEVICE_ID IN " + - "(SELECT d.ID FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.DEVICE_IDENTIFICATION IN ("); + StringBuilder sql = new StringBuilder("UPDATE DM_ENROLMENT SET STATUS = ? " + + "WHERE DEVICE_IDENTIFICATION IN ("); for (int i = 0; i < devices.size(); i++) { sql.append("?,"); } sql.deleteCharAt(sql.length() - 1); - sql.append(") AND t.NAME = ? AND d.TENANT_ID = ?) AND TENANT_ID = ?"); + sql.append(") AND DEVICE_TYPE = ? AND TENANT_ID = ?"); stmt = conn.prepareStatement(sql.toString()); stmt.setString(1, status); int index = 1; @@ -1233,7 +1243,6 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } stmt.setString(++index, deviceType); stmt.setInt(++index, tenantId); - stmt.setInt(++index, tenantId); stmt.executeUpdate(); } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while updating enrollment status in bulk", e); @@ -1247,7 +1256,6 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { * Get device count of all devices. * * @return device count - * @throws DeviceManagementDAOException */ @Override public int getDeviceCount(int tenantId) throws DeviceManagementDAOException { @@ -1257,12 +1265,10 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { int deviceCount = 0; try { conn = this.getConnection(); - String sql = "SELECT COUNT(d1.DEVICE_ID) AS DEVICE_COUNT FROM DM_ENROLMENT e, (SELECT d.ID AS DEVICE_ID FROM " + - "DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?) d1 WHERE " + - "d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?"; + String sql = "SELECT COUNT(ID) AS DEVICE_COUNT FROM DM_ENROLMENT WHERE " + + "TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); - stmt.setInt(2, tenantId); rs = stmt.executeQuery(); if (rs.next()) { deviceCount = rs.getInt("DEVICE_COUNT"); @@ -1300,25 +1306,24 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { "(SELECT " + "d.ID, " + "d.NAME, " + - "d.DEVICE_IDENTIFICATION, " + - "t.NAME AS DEVICE_TYPE " + + "d.DEVICE_IDENTIFICATION " + "FROM " + - "DM_DEVICE d, " + - "DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?"; + "DM_DEVICE d " + + "WHERE 1=1 "; //Add query for last updated timestamp if (since != null) { sql = sql + " AND d.LAST_UPDATED_TIMESTAMP > ?"; isSinceProvided = true; } - if (deviceType != null && !deviceType.isEmpty()) { - sql = sql + " AND t.NAME = ?"; - isDeviceTypeProvided = true; - } if (deviceName != null && !deviceName.isEmpty()) { sql = sql + " AND d.NAME LIKE ?"; isDeviceNameProvided = true; } sql = sql + ") d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ?"; + if (deviceType != null && !deviceType.isEmpty()) { + sql = sql + " AND e.DEVICE_TYPE = ?"; + isDeviceTypeProvided = true; + } if (ownership != null && !ownership.isEmpty()) { sql = sql + " AND e.OWNERSHIP = ?"; isOwnershipProvided = true; @@ -1338,17 +1343,16 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { try (PreparedStatement stmt = conn.prepareStatement(sql)) { int paramIdx = 1; - stmt.setInt(paramIdx++, tenantId); if (isSinceProvided) { stmt.setTimestamp(paramIdx++, new Timestamp(since.getTime())); } - if (isDeviceTypeProvided) { - stmt.setString(paramIdx++, request.getDeviceType()); - } if (isDeviceNameProvided) { stmt.setString(paramIdx++, "%" + request.getDeviceName() + "%"); } stmt.setInt(paramIdx++, tenantId); + if (isDeviceTypeProvided) { + stmt.setString(paramIdx++, request.getDeviceType()); + } if (isOwnershipProvided) { stmt.setString(paramIdx++, request.getOwnership()); } @@ -1384,13 +1388,10 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { int deviceCount = 0; try { conn = this.getConnection(); - String sql = "SELECT COUNT(d1.ID) AS DEVICE_COUNT FROM DM_ENROLMENT e, (SELECT d.ID FROM DM_DEVICE d, " + - "DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND t.NAME = ? " + - "AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ?"; + String sql = "SELECT COUNT(ID) AS DEVICE_COUNT FROM DM_ENROLMENT WHERE DEVICE_TYPE = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, type); stmt.setInt(2, tenantId); - stmt.setInt(3, tenantId); rs = stmt.executeQuery(); if (rs.next()) { deviceCount = rs.getInt("DEVICE_COUNT"); @@ -1410,12 +1411,11 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { int deviceCount = 0; try { conn = this.getConnection(); - String sql = "SELECT COUNT(e1.DEVICE_ID) AS DEVICE_COUNT FROM DM_DEVICE d, (SELECT e.DEVICE_ID " + - "FROM DM_ENROLMENT e WHERE e.TENANT_ID = ? AND LOWER(e.OWNER) = LOWER(?)) " + - "e1, DM_DEVICE_TYPE t WHERE d.ID = e1.DEVICE_ID AND t.ID = d.DEVICE_TYPE_ID"; + String sql = "SELECT COUNT(ID) AS DEVICE_COUNT FROM DM_ENROLMENT WHERE LOWER(OWNER) = LOWER(?)" + + " AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); - stmt.setInt(1, tenantId); - stmt.setString(2, username); + stmt.setString(1, username); + stmt.setInt(2, tenantId); ResultSet rs = stmt.executeQuery(); if (rs.next()) { @@ -1437,8 +1437,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { int deviceCount = 0; try { conn = this.getConnection(); - String sql = "SELECT COUNT(d1.ID) AS DEVICE_COUNT FROM DM_ENROLMENT e, (SELECT d.ID FROM DM_DEVICE d, " + - "DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.NAME LIKE ? AND d.TENANT_ID = ?) d1 " + + String sql = "SELECT COUNT(d1.ID) AS DEVICE_COUNT FROM DM_ENROLMENT e, (SELECT d.ID FROM DM_DEVICE d " + + "WHERE d.NAME LIKE ? AND d.TENANT_ID = ?) d1 " + "WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, deviceName + "%"); @@ -1465,13 +1465,11 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { int deviceCount = 0; try { conn = this.getConnection(); - String sql = "SELECT COUNT(d.ID) AS DEVICE_COUNT FROM (SELECT e.DEVICE_ID FROM DM_ENROLMENT e WHERE " + - "TENANT_ID = ? AND OWNERSHIP = ?) e, DM_DEVICE d, " + - "DM_DEVICE_TYPE t WHERE d.ID = e.DEVICE_ID AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?"; + String sql = "SELECT COUNT(ID) AS DEVICE_COUNT FROM DM_ENROLMENT WHERE " + + "TENANT_ID = ? AND OWNERSHIP = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); stmt.setString(2, ownerShip); - stmt.setInt(3, tenantId); ResultSet rs = stmt.executeQuery(); if (rs.next()) { @@ -1493,13 +1491,11 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { int deviceCount = 0; try { conn = this.getConnection(); - String sql = "SELECT COUNT(d.ID) AS DEVICE_COUNT FROM (SELECT e.DEVICE_ID FROM DM_ENROLMENT e WHERE " + - "TENANT_ID = ? AND STATUS = ?) e, DM_DEVICE d, " + - "DM_DEVICE_TYPE t WHERE d.ID = e.DEVICE_ID AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?"; + String sql = "SELECT COUNT(ID) AS DEVICE_COUNT FROM DM_ENROLMENT WHERE " + + "TENANT_ID = ? AND STATUS = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); stmt.setString(2, status); - stmt.setInt(3, tenantId); ResultSet rs = stmt.executeQuery(); if (rs.next()) { @@ -1521,14 +1517,12 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { int deviceCount = 0; try { conn = this.getConnection(); - String sql = "SELECT COUNT(d.ID) AS DEVICE_COUNT FROM (SELECT e.DEVICE_ID FROM DM_ENROLMENT e WHERE " + - "TENANT_ID = ? AND STATUS = ?) e, DM_DEVICE d, " + - "DM_DEVICE_TYPE t WHERE t.NAME = ? AND d.ID = e.DEVICE_ID AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?"; + String sql = "SELECT COUNT(ID) AS DEVICE_COUNT FROM DM_ENROLMENT WHERE " + + "TENANT_ID = ? AND STATUS = ? AND DEVICE_TYPE = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); stmt.setString(2, status); stmt.setString(3, deviceType); - stmt.setInt(4, tenantId); ResultSet rs = stmt.executeQuery(); if (rs.next()) { @@ -1551,17 +1545,20 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { int enrolmentId = -1; try { conn = this.getConnection(); - String sql = "INSERT INTO DM_ENROLMENT(DEVICE_ID, OWNER, OWNERSHIP, STATUS, IS_TRANSFERRED, " + - "DATE_OF_ENROLMENT, DATE_OF_LAST_UPDATE, TENANT_ID) VALUES(?, ?, ?, ?, ?, ?, ?, ?)"; + String sql = "INSERT INTO DM_ENROLMENT(DEVICE_ID, DEVICE_TYPE, DEVICE_IDENTIFICATION, OWNER, OWNERSHIP, " + + "STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, DATE_OF_LAST_UPDATE, TENANT_ID) " + + "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; stmt = conn.prepareStatement(sql, new String[]{"id"}); stmt.setInt(1, device.getId()); - stmt.setString(2, device.getEnrolmentInfo().getOwner()); - stmt.setString(3, device.getEnrolmentInfo().getOwnership().toString()); - stmt.setString(4, device.getEnrolmentInfo().getStatus().toString()); - stmt.setBoolean(5, device.getEnrolmentInfo().isTransferred()); - stmt.setTimestamp(6, new Timestamp(new Date().getTime())); - stmt.setTimestamp(7, new Timestamp(new Date().getTime())); - stmt.setInt(8, tenantId); + stmt.setString(2, device.getType()); + stmt.setString(3, device.getDeviceIdentifier()); + stmt.setString(4, device.getEnrolmentInfo().getOwner()); + stmt.setString(5, device.getEnrolmentInfo().getOwnership().toString()); + stmt.setString(6, device.getEnrolmentInfo().getStatus().toString()); + stmt.setBoolean(7, device.getEnrolmentInfo().isTransferred()); + stmt.setTimestamp(8, new Timestamp(new Date().getTime())); + stmt.setTimestamp(9, new Timestamp(new Date().getTime())); + stmt.setInt(10, tenantId); stmt.execute(); rs = stmt.getGeneratedKeys(); @@ -1583,16 +1580,14 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { PreparedStatement stmt = null; try { conn = this.getConnection(); - String sql = "UPDATE DM_ENROLMENT SET STATUS = ? WHERE DEVICE_ID = (SELECT d.ID FROM DM_DEVICE d, " + - "DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.DEVICE_IDENTIFICATION = ? " + - "AND t.NAME = ? AND d.TENANT_ID = ?) AND OWNER = ? AND TENANT_ID = ?"; + String sql = "UPDATE DM_ENROLMENT SET STATUS = ? WHERE DEVICE_IDENTIFICATION = ? " + + "AND DEVICE_TYPE = ? AND OWNER = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, status.toString()); stmt.setString(2, deviceId.getId()); stmt.setString(3, deviceId.getType()); - stmt.setInt(4, tenantId); - stmt.setString(5, currentOwner); - stmt.setInt(6, tenantId); + stmt.setString(4, currentOwner); + stmt.setInt(5, tenantId); stmt.executeUpdate(); } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment", e); @@ -1611,15 +1606,13 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { Status status = null; try { conn = this.getConnection(); - String sql = "SELECT STATUS FROM DM_ENROLMENT WHERE DEVICE_ID = (SELECT d.ID FROM DM_DEVICE d, " + - "DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.DEVICE_IDENTIFICATION = ? " + - "AND t.NAME = ? AND d.TENANT_ID = ?) AND OWNER = ? AND TENANT_ID = ?"; + String sql = "SELECT STATUS FROM DM_ENROLMENT WHERE DEVICE_IDENTIFICATION = ? " + + "AND DEVICE_TYPE = ? AND OWNER = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, deviceId.getId()); stmt.setString(2, deviceId.getType()); - stmt.setInt(3, tenantId); - stmt.setString(4, currentOwner); - stmt.setInt(5, tenantId); + stmt.setString(3, currentOwner); + stmt.setInt(4, tenantId); rs = stmt.executeQuery(); if (rs.next()) { @@ -1651,9 +1644,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { + "TENANT_ID " + "FROM DM_ENROLMENT " + "WHERE " - + "DEVICE_ID = (SELECT d.ID " + - "FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID " + - "AND d.DEVICE_IDENTIFICATION = ? AND t.NAME = ? AND d.TENANT_ID = ?) " + + "DEVICE_IDENTIFICATION = ? AND DEVICE_TYPE = ? " + "AND OWNER = ? "; if (!StringUtils.isBlank(request.getOwnership())) { @@ -1664,7 +1655,6 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setString(paramIdx++, deviceId.getId()); stmt.setString(paramIdx++, deviceId.getType()); - stmt.setInt(paramIdx++, tenantId); stmt.setString(paramIdx++, request.getOwner()); if (!StringUtils.isBlank(request.getOwnership())) { stmt.setString(paramIdx++, request.getOwnership()); @@ -1686,23 +1676,21 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } @Override - public EnrolmentInfo getActiveEnrolment(DeviceIdentifier deviceId, int tenantId) throws DeviceManagementDAOException { + public EnrolmentInfo getActiveEnrolment(DeviceIdentifier deviceId, int tenantId) + throws DeviceManagementDAOException { Connection conn; PreparedStatement stmt = null; ResultSet rs = null; EnrolmentInfo enrolmentInfo = null; try { conn = this.getConnection(); - String sql = "SELECT ID AS ENROLMENT_ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, " + - "DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = (SELECT d.ID " + - "FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID " + - "AND d.DEVICE_IDENTIFICATION = ? AND t.NAME = ? AND d.TENANT_ID = ?) " + - "AND TENANT_ID = ? AND STATUS != 'REMOVED'"; + String sql = "SELECT ID AS ENROLMENT_ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, IS_TRANSFERRED, " + + "DATE_OF_ENROLMENT, DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT " + + "WHERE DEVICE_IDENTIFICATION = ? AND DEVICE_TYPE = ? AND TENANT_ID = ? AND STATUS != 'REMOVED'"; stmt = conn.prepareStatement(sql); stmt.setString(1, deviceId.getId()); stmt.setString(2, deviceId.getType()); stmt.setInt(3, tenantId); - stmt.setInt(4, tenantId); rs = stmt.executeQuery(); if (rs.next()) { enrolmentInfo = DeviceManagementDAOUtil.loadEnrolment(rs); @@ -1725,12 +1713,13 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { List devices = new ArrayList<>(); try { conn = this.getConnection(); - String sql = "SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, t.NAME AS DEVICE_TYPE, " + - "d.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, " + - "e.DATE_OF_ENROLMENT, e.ENROLMENT_ID FROM (SELECT e.ID, e.DEVICE_ID, e.OWNER, e" + - ".OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_ENROLMENT, e.DATE_OF_LAST_UPDATE, e.ID AS " + - "ENROLMENT_ID FROM DM_ENROLMENT e WHERE TENANT_ID = ? AND STATUS = ?) e, DM_DEVICE d, DM_DEVICE_TYPE t " + - "WHERE DEVICE_ID = e.DEVICE_ID AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?"; + String sql = "SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, 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.DEVICE_IDENTIFICATION, e.OWNER, " + + "e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_ENROLMENT, e.DATE_OF_LAST_UPDATE, e.ID AS " + + "ENROLMENT_ID FROM DM_ENROLMENT e WHERE TENANT_ID = ? AND STATUS = ?) e, " + + "DM_DEVICE d WHERE d.ID = e.DEVICE_ID AND d.TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); stmt.setString(2, status.toString()); @@ -1751,6 +1740,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } @Override + @Deprecated public List getDeviceTypes() throws DeviceManagementDAOException { Connection conn; @@ -1779,9 +1769,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { * Returns the collection of devices that has been updated after the time given in the timestamp passed in. * * @param timestamp Timestamp in long, after which the devices have been updated. - * @param tenantId Tenant id of the currently logged in user. + * @param tenantId Tenant id of the currently logged-in user. * @return A collection of devices that have been updated after the provided timestamp - * @throws DeviceManagementDAOException */ public List getDevices(long timestamp, int tenantId) throws DeviceManagementDAOException { Connection conn; @@ -1790,11 +1779,12 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { List devices = new ArrayList<>(); try { conn = this.getConnection(); - String sql = "SELECT d1.DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + - "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_LAST_UPDATE, " + - "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID AS DEVICE_ID, " + - "d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE, d.LAST_UPDATED_TIMESTAMP FROM DM_DEVICE d, " + - "DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ? AND d.LAST_UPDATED_TIMESTAMP < CURRENT_TIMESTAMP) d1 " + + 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 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 = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); @@ -1854,7 +1844,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { "MIN(DEVICE.ID) AS DEVICE_ID, " + "MIN(DEVICE.NAME) AS DEVICE_NAME, " + "MIN(DEVICE.DESCRIPTION) AS DESCRIPTION, " + - "MIN(DEVICE_TYPE.NAME) AS DEVICE_TYPE, " + + "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, " + "MIN(ENROLMENT.OWNER) AS OWNER, " + @@ -1864,11 +1855,11 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { "MIN(ENROLMENT.DATE_OF_LAST_UPDATE) AS DATE_OF_LAST_UPDATE, " + "MIN(ENROLMENT.STATUS) AS STATUS " + "FROM DM_DEVICE_LOCATION AS DEVICE_LOCATION, DM_DEVICE AS DEVICE, " + - "DM_DEVICE_TYPE AS DEVICE_TYPE, DM_ENROLMENT AS ENROLMENT " + + "DM_ENROLMENT AS ENROLMENT " + "WHERE DEVICE_LOCATION.LATITUDE BETWEEN ? AND ? " + "AND DEVICE_LOCATION.LONGITUDE BETWEEN ? AND ? "; if (geoQuery.getDeviceTypes() != null && !geoQuery.getDeviceTypes().isEmpty()) { - sql += "AND DEVICE_TYPE.NAME IN ("; + sql += "AND ENROLMENT.DEVICE_TYPE IN ("; sql += String.join(", ", Collections.nCopies(geoQuery.getDeviceTypes().size(), "?")); sql += ") "; @@ -1905,7 +1896,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { if (geoQuery.getUpdatedBefore() != 0 || geoQuery.getUpdatedAfter() != 0) { sql += "AND ENROLMENT.DATE_OF_LAST_UPDATE BETWEEN ? AND ? "; } - sql += "AND DEVICE.ID = DEVICE_LOCATION.DEVICE_ID AND DEVICE.DEVICE_TYPE_ID = DEVICE_TYPE.ID " + + sql += "AND DEVICE.ID = DEVICE_LOCATION.DEVICE_ID " + "AND DEVICE.ID = ENROLMENT.DEVICE_ID " + "AND DEVICE.TENANT_ID = ? AND DEVICE.TENANT_ID = ENROLMENT.TENANT_ID GROUP BY GEOHASH_PREFIX"; stmt = conn.prepareStatement(sql); @@ -1917,27 +1908,27 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { stmt.setDouble(index++, geoQuery.getSouthWest().getLongitude()); stmt.setDouble(index++, geoQuery.getNorthEast().getLongitude()); if (geoQuery.getDeviceTypes() != null) { - for (String s: geoQuery.getDeviceTypes()) { + for (String s : geoQuery.getDeviceTypes()) { stmt.setString(index++, s); } } if (geoQuery.getDeviceIdentifiers() != null) { - for (String s: geoQuery.getDeviceIdentifiers()) { + for (String s : geoQuery.getDeviceIdentifiers()) { stmt.setString(index++, s); } } if (geoQuery.getOwners() != null) { - for (String s: geoQuery.getOwners()) { + for (String s : geoQuery.getOwners()) { stmt.setString(index++, s); } } if (geoQuery.getOwnerships() != null) { - for (String s: geoQuery.getOwnerships()) { + for (String s : geoQuery.getOwnerships()) { stmt.setString(index++, s); } } if (geoQuery.getStatuses() != null) { - for (Status s: geoQuery.getStatuses()) { + for (Status s : geoQuery.getStatuses()) { stmt.setString(index++, s.toString()); } } @@ -2013,15 +2004,15 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { StringJoiner joiner = new StringJoiner(",", "SELECT " - + "d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " - + "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, " - + "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, t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION " - + "FROM DM_DEVICE d, DM_DEVICE_TYPE t " + + "(SELECT d.ID, d.DESCRIPTION, d.NAME, d.LAST_UPDATED_TIMESTAMP " + + "FROM DM_DEVICE d " + "WHERE " - + "t.ID = d.DEVICE_TYPE_ID AND d.DEVICE_IDENTIFICATION IN (", + + "d.DEVICE_IDENTIFICATION IN (", ") AND d.TENANT_ID = ?) d1 " + "WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? " + "ORDER BY e.DATE_OF_LAST_UPDATE DESC, e.STATUS ASC"); @@ -2071,15 +2062,16 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { StringJoiner joiner = new StringJoiner(",", "SELECT " - + "d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " - + "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, " + + "d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, e.DEVICE_TYPE, " + + "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, t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION " - + "FROM DM_DEVICE d, DM_DEVICE_TYPE t " + + "(SELECT d.ID, d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, d.LAST_UPDATED_TIMESTAMP " + + "FROM DM_DEVICE d " + "WHERE " - + "t.ID = d.DEVICE_TYPE_ID AND d.DEVICE_IDENTIFICATION IN (", + + " d.DEVICE_IDENTIFICATION IN (", ") AND d.TENANT_ID = ?) d1 " + "WHERE d1.ID = e.DEVICE_ID AND " + statusJoiner.toString() + "AND TENANT_ID = ? ORDER BY e.DATE_OF_LAST_UPDATE DESC, e.STATUS ASC"); @@ -2113,7 +2105,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } public List getDeviceLocationInfo(DeviceIdentifier deviceIdentifier, long from, - long to) throws DeviceManagementDAOException { + long to) throws DeviceManagementDAOException { List deviceLocationHistories = new ArrayList<>(); String sql = "SELECT " + "DEVICE_ID, " @@ -2245,8 +2237,9 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { "d.ID AS DEVICE_ID, " + "d.DESCRIPTION, " + "d.NAME AS DEVICE_NAME, " + - "t.NAME AS DEVICE_TYPE, " + + "e.DEVICE_TYPE, " + "d.DEVICE_IDENTIFICATION, " + + "d.LAST_UPDATED_TIMESTAMP, " + "e.OWNER, " + "e.OWNERSHIP, " + "e.STATUS, " + @@ -2256,9 +2249,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { "e.ID AS ENROLMENT_ID " + "FROM DM_DEVICE AS d " + "INNER JOIN DM_ENROLMENT AS e ON d.ID = e.DEVICE_ID " + - "INNER JOIN DM_DEVICE_TYPE AS t ON d.DEVICE_TYPE_ID = t.ID " + "WHERE " + - "t.NAME = ? AND e.TENANT_ID = ? AND d.ID " + + "e.DEVICE_TYPE = ? AND e.TENANT_ID = ? AND d.ID " + "NOT IN " + "(SELECT a.DEVICE_ID FROM DM_APPLICATION AS a WHERE a.APP_IDENTIFIER = ?"; @@ -2310,9 +2302,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { "COUNT(d.ID) AS DEVICE_COUNT " + "FROM DM_DEVICE AS d " + "INNER JOIN DM_ENROLMENT AS e ON d.ID = e.DEVICE_ID " + - "INNER JOIN DM_DEVICE_TYPE AS t ON d.DEVICE_TYPE_ID = t.ID " + "WHERE " + - "t.NAME = ? AND e.TENANT_ID = ? AND d.ID " + + "e.DEVICE_TYPE = ? AND e.TENANT_ID = ? AND d.ID " + "NOT IN " + "(SELECT a.DEVICE_ID FROM DM_APPLICATION AS a WHERE a.APP_IDENTIFIER = ?"; @@ -2354,40 +2345,40 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { throws DeviceManagementDAOException { try { Connection conn = getConnection(); - String sql = "" + + String sql = "SELECT e1.owner," + - "e1.ownership," + - "e1.enrolment_id," + - "e1.device_id," + - "e1.status," + - "e1.date_of_last_update," + - "e1.date_of_enrolment," + - "d.description," + - "d.NAME AS DEVICE_NAME," + - "d.device_identification," + - "t.NAME AS DEVICE_TYPE " + - "FROM dm_device d," + - "(SELECT e.owner," + - "e.ownership," + - "e.id AS ENROLMENT_ID," + - "e.device_id," + - "e.status, " + - "e.date_of_last_update, " + - "e.date_of_enrolment " + - "FROM dm_enrolment e " + - "INNER JOIN " + - "(SELECT DEVICE_ID " + - "FROM DM_DEVICE_INFO " + - "WHERE " + - "KEY_FIELD = 'encryptionEnabled' " + - "AND VALUE_FIELD = ?) AS di " + - "ON di.DEVICE_ID = e.DEVICE_ID " + - "WHERE e.tenant_id = ?) e1, " + - "dm_device_type t " + - "WHERE d.id = e1.device_id " + - "AND t.id = d.device_type_id " + - "ORDER BY e1.date_of_last_update DESC " + - "LIMIT ? OFFSET ?"; + "e1.ownership," + + "e1.enrolment_id," + + "e1.device_id," + + "e1.status," + + "e1.date_of_last_update," + + "e1.date_of_enrolment," + + "d.description," + + "d.NAME AS DEVICE_NAME," + + "d.device_identification," + + "d.LAST_UPDATED_TIMESTAMP, " + + "e1.DEVICE_TYPE " + + "FROM dm_device d," + + "(SELECT e.owner," + + "e.ownership," + + "e.id AS ENROLMENT_ID," + + "e.device_id," + + "e.status, " + + "e.date_of_last_update, " + + "e.date_of_enrolment, " + + "e.DEVICE_TYPE " + + "FROM dm_enrolment e " + + "INNER JOIN " + + "(SELECT DEVICE_ID " + + "FROM DM_DEVICE_INFO " + + "WHERE " + + "KEY_FIELD = 'encryptionEnabled' " + + "AND VALUE_FIELD = ?) AS di " + + "ON di.DEVICE_ID = e.DEVICE_ID " + + "WHERE e.tenant_id = ?) e1 " + + "WHERE d.id = e1.device_id " + + "ORDER BY e1.date_of_last_update DESC " + + "LIMIT ? OFFSET ?"; try (PreparedStatement ps = conn.prepareStatement(sql)) { ps.setBoolean(1, isEncrypted); @@ -2417,27 +2408,15 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { throws DeviceManagementDAOException { try { Connection conn = getConnection(); - String sql = "" + - "SELECT " + - "COUNT(e1.DEVICE_ID) AS DEVICE_COUNT " + - "FROM dm_device d," + - "(SELECT e.id AS ENROLMENT_ID, " + - "e.device_id " + - "FROM dm_enrolment e " + - "INNER JOIN " + - "(SELECT DEVICE_ID " + - "FROM DM_DEVICE_INFO " + - "WHERE KEY_FIELD = 'encryptionEnabled' " + - "AND VALUE_FIELD = ?) AS di " + - "ON di.DEVICE_ID = e.DEVICE_ID " + - "WHERE e.tenant_id = ?) e1, " + - "dm_device_type t " + - "WHERE d.id = e1.device_id " + - "AND t.id = d.device_type_id "; + String sql = + "SELECT COUNT(DEVICE_ID) AS DEVICE_COUNT " + + "FROM DM_DEVICE_INFO " + + "WHERE KEY_FIELD = 'encryptionEnabled' " + + "AND VALUE_FIELD = ?"; + //TODO: Add tenant column to DM_DEVICE_INFO table try (PreparedStatement ps = conn.prepareStatement(sql)) { ps.setBoolean(1, isEncrypted); - ps.setInt(2, tenantId); try (ResultSet rs = ps.executeQuery()) { return rs.next() ? rs.getInt("DEVICE_COUNT") : 0; @@ -2833,42 +2812,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(); @@ -3071,110 +3014,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, " - + "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 = ?"; - } - 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 { @@ -3289,10 +3128,12 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); List devices = new ArrayList<>(); String query = "SELECT e.ID AS ENROLMENT_ID, e.DEVICE_ID, e.OWNER, e.OWNERSHIP, e.DATE_OF_ENROLMENT, " + - "e.DATE_OF_LAST_UPDATE, e.IS_TRANSFERRED, e.STATUS,d.DEVICE_NAME, d.DESCRIPTION, d.DEVICE_TYPE, d.DEVICE_IDENTIFICATION " + - "FROM DM_ENROLMENT e,(SELECT d1.ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_IDENTIFICATION, " + - "t1.NAME AS DEVICE_TYPE FROM DM_DEVICE d1, DM_DEVICE_TYPE t1 WHERE d1.DEVICE_TYPE_ID = t1.ID " + - "AND d1.TENANT_ID = ?) d WHERE e.STATUS NOT IN ('DELETED', 'REMOVED') AND e.DATE_OF_ENROLMENT > ? AND e.TENANT_ID = ?"; + "e.DATE_OF_LAST_UPDATE, e.IS_TRANSFERRED, e.STATUS, d.DEVICE_NAME, d.DESCRIPTION, " + + "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 = ?"; try { Connection connection = DeviceManagementDAOFactory.getConnection(); try (PreparedStatement preparedStatement = connection.prepareStatement(query)) { @@ -3314,14 +3155,16 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } return devices; } + public List getDevicesEnrolledPriorTo(Date priorTo) throws DeviceManagementDAOException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); List devices = new ArrayList<>(); String query = "SELECT e.ID AS ENROLMENT_ID, e.DEVICE_ID, e.OWNER, e.OWNERSHIP, e.DATE_OF_ENROLMENT, " + - "e.DATE_OF_LAST_UPDATE, e.IS_TRANSFERRED, e.STATUS, d.DEVICE_NAME, d.DESCRIPTION, d.DEVICE_TYPE, d.DEVICE_IDENTIFICATION " + - "FROM DM_ENROLMENT e,(SELECT d1.ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_IDENTIFICATION, " + - "t1.NAME AS DEVICE_TYPE FROM DM_DEVICE d1, DM_DEVICE_TYPE t1 WHERE d1.DEVICE_TYPE_ID = t1.ID " + - "AND d1.TENANT_ID = ?) d WHERE e.STATUS NOT IN ('DELETED', 'REMOVED') AND e.DATE_OF_ENROLMENT < ? AND e.TENANT_ID = ?"; + "e.DATE_OF_LAST_UPDATE, e.IS_TRANSFERRED, e.STATUS, d.DEVICE_NAME, d.DESCRIPTION, " + + "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 = ?"; try { Connection connection = DeviceManagementDAOFactory.getConnection(); try (PreparedStatement preparedStatement = connection.prepareStatement(query)) { @@ -3343,4 +3186,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/AbstractEnrollmentDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractEnrollmentDAOImpl.java index 69803a487e..f4e990d577 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractEnrollmentDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractEnrollmentDAOImpl.java @@ -17,6 +17,8 @@ */ package io.entgra.device.mgt.core.device.mgt.core.dao.impl; +import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.context.PrivilegedCarbonContext; import io.entgra.device.mgt.core.device.mgt.common.Device; import io.entgra.device.mgt.core.device.mgt.common.DeviceManagementConstants; import io.entgra.device.mgt.core.device.mgt.common.EnrolmentInfo; @@ -34,24 +36,29 @@ import java.util.List; public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { @Override - public EnrolmentInfo addEnrollment(int deviceId, EnrolmentInfo enrolmentInfo, + public EnrolmentInfo addEnrollment(int deviceId, DeviceIdentifier deviceIdentifier, EnrolmentInfo enrolmentInfo, int tenantId) throws DeviceManagementDAOException { Connection conn; PreparedStatement stmt = null; ResultSet rs = null; try { conn = this.getConnection(); - String sql = "INSERT INTO DM_ENROLMENT(DEVICE_ID, OWNER, OWNERSHIP, STATUS, " + - "DATE_OF_ENROLMENT, DATE_OF_LAST_UPDATE, TENANT_ID) VALUES(?, ?, ?, ?, ?, ?, ?)"; - stmt = conn.prepareStatement(sql, new String[] {"id"}); + String sql = "INSERT INTO DM_ENROLMENT(DEVICE_ID, DEVICE_TYPE, DEVICE_IDENTIFICATION, OWNER, OWNERSHIP, " + + "STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, DATE_OF_LAST_UPDATE, TENANT_ID) " + + "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; Timestamp enrollmentTime = new Timestamp(new Date().getTime()); + + stmt = conn.prepareStatement(sql, new String[]{"id"}); stmt.setInt(1, deviceId); - stmt.setString(2, enrolmentInfo.getOwner()); - stmt.setString(3, enrolmentInfo.getOwnership().toString()); - stmt.setString(4, enrolmentInfo.getStatus().toString()); - stmt.setTimestamp(5, enrollmentTime); - stmt.setTimestamp(6, enrollmentTime); - stmt.setInt(7, tenantId); + stmt.setString(2, deviceIdentifier.getType()); + stmt.setString(3, deviceIdentifier.getId()); + stmt.setString(4, enrolmentInfo.getOwner()); + stmt.setString(5, enrolmentInfo.getOwnership().toString()); + stmt.setString(6, enrolmentInfo.getStatus().toString()); + stmt.setBoolean(7, enrolmentInfo.isTransferred()); + stmt.setTimestamp(8, enrollmentTime); + stmt.setTimestamp(9, enrollmentTime); + stmt.setInt(10, tenantId); stmt.execute(); rs = stmt.getGeneratedKeys(); @@ -65,7 +72,6 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { } return null; } catch (SQLException e) { - e.printStackTrace(); throw new DeviceManagementDAOException("Error occurred while adding enrolment configuration", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); @@ -76,7 +82,6 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { public int updateEnrollment(EnrolmentInfo enrolmentInfo, int tenantId) throws DeviceManagementDAOException { Connection conn; PreparedStatement stmt = null; - ResultSet rs = null; try { conn = this.getConnection(); String sql = "UPDATE DM_ENROLMENT SET OWNERSHIP = ?, STATUS = ?, DATE_OF_LAST_UPDATE = ? " + @@ -87,12 +92,11 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { stmt.setTimestamp(3, new Timestamp(new Date().getTime())); stmt.setInt(4, enrolmentInfo.getId()); stmt.setInt(5, tenantId); - int updatedCount = stmt.executeUpdate(); - return updatedCount; + return stmt.executeUpdate(); } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while updating enrolment configuration", e); } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); + DeviceManagementDAOUtil.cleanupResources(stmt, null); } } @@ -100,7 +104,6 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { public boolean updateEnrollmentStatus(List enrolmentInfos) throws DeviceManagementDAOException { Connection conn; PreparedStatement stmt = null; - ResultSet rs = null; boolean status = false; int updateStatus = -1; try { @@ -127,7 +130,7 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while updating enrolment status of given device-list.", e); } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); + DeviceManagementDAOUtil.cleanupResources(stmt, null); } return status; } @@ -142,7 +145,7 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { try { conn = this.getConnection(); String sql = "DELETE FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql, new String[] {"id"}); + stmt = conn.prepareStatement(sql, new String[]{"id"}); stmt.setInt(1, deviceId); stmt.setString(2, currentOwner); stmt.setInt(3, tenantId); @@ -172,12 +175,12 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { stmt.setString(1, owner); stmt.setInt(2, tenantID); rs = stmt.executeQuery(); - if(rs.next()){ + if (rs.next()) { count = rs.getInt("COUNT"); } } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while trying to get device " + - "count of Owner : "+owner, e); + "count of Owner : " + owner, e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } @@ -192,11 +195,11 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { @Override public boolean setStatusAllDevices(String currentOwner, EnrolmentInfo.Status status, int tenantId) - throws DeviceManagementDAOException{ + throws DeviceManagementDAOException { Connection conn; PreparedStatement stmt = null; Timestamp updateTime = new Timestamp(new Date().getTime()); - if(getCountOfDevicesOfOwner(currentOwner, tenantId) > 0){ + if (getCountOfDevicesOfOwner(currentOwner, tenantId) > 0) { try { conn = this.getConnection(); String sql = "UPDATE DM_ENROLMENT SET STATUS = ?, DATE_OF_LAST_UPDATE = ? WHERE OWNER = ? AND TENANT_ID = ?"; @@ -231,8 +234,8 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { stmt.setInt(3, enrolmentID); stmt.setInt(4, tenantId); int updatedRowCount = stmt.executeUpdate(); - if (updatedRowCount != 1){ - throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment: "+ + if (updatedRowCount != 1) { + throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment: " + updatedRowCount + " rows were updated instead of one row!!!"); } // save the device status history @@ -249,10 +252,11 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { return addDeviceStatus(config.getId(), config.getStatus()); } - public boolean addDeviceStatus(String currentOwner, EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException { + public boolean addDeviceStatus(String currentOwner, EnrolmentInfo.Status status, int tenantId) + throws DeviceManagementDAOException { Connection conn; String changedBy = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); - if (changedBy == null){ + if (changedBy == null) { changedBy = DeviceManagementConstants.MaintenanceProperties.MAINTENANCE_USER; } PreparedStatement stmt = null; @@ -276,7 +280,7 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { sql = "INSERT INTO DM_DEVICE_STATUS (ENROLMENT_ID, DEVICE_ID, STATUS, UPDATE_TIME, CHANGED_BY) VALUES(?, ?, ?, ?, ?)"; try (PreparedStatement ps = conn.prepareStatement(sql)) { if (conn.getMetaData().supportsBatchUpdates()) { - for(int[] info: enrolmentInfoList){ + for (int[] info : enrolmentInfoList) { ps.setInt(1, info[0]); ps.setInt(2, info[1]); ps.setString(3, status.toString()); @@ -291,7 +295,7 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { } } } else { - for(int[] info: enrolmentInfoList){ + for (int[] info : enrolmentInfoList) { ps.setInt(1, info[0]); ps.setInt(2, info[1]); ps.setString(3, status.toString()); @@ -315,14 +319,15 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { public boolean addDeviceStatus(int enrolmentId, EnrolmentInfo.Status status) throws DeviceManagementDAOException { Connection conn; String changedBy = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); - if (changedBy == null){ + if (changedBy == null) { changedBy = DeviceManagementConstants.MaintenanceProperties.MAINTENANCE_USER; } PreparedStatement stmt = null; try { conn = this.getConnection(); - // get the device id and last udpated status from the device status table - String sql = "SELECT DEVICE_ID, STATUS FROM DM_DEVICE_STATUS WHERE ENROLMENT_ID = ? ORDER BY UPDATE_TIME DESC LIMIT 1"; + // get the device id and last updated status from the device status table + String sql = "SELECT DEVICE_ID, STATUS FROM DM_DEVICE_STATUS " + + "WHERE ENROLMENT_ID = ? ORDER BY UPDATE_TIME DESC LIMIT 1"; stmt = conn.prepareStatement(sql); stmt.setInt(1, enrolmentId); ResultSet rs = stmt.executeQuery(); @@ -348,12 +353,14 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { } else { // if there were no records corresponding to the enrolment id this is a problem. i.e. enrolment // id is invalid - throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment: no record for enrolment id " + enrolmentId); + throw new DeviceManagementDAOException("Error occurred while setting the status of " + + "device enrolment: no record for enrolment id " + enrolmentId); } DeviceManagementDAOUtil.cleanupResources(stmt, null); } - sql = "INSERT INTO DM_DEVICE_STATUS (ENROLMENT_ID, DEVICE_ID, STATUS, UPDATE_TIME, CHANGED_BY) VALUES(?, ?, ?, ?, ?)"; + sql = "INSERT INTO DM_DEVICE_STATUS (ENROLMENT_ID, DEVICE_ID, STATUS, UPDATE_TIME, CHANGED_BY) " + + "VALUES(?, ?, ?, ?, ?)"; stmt = conn.prepareStatement(sql); Timestamp updateTime = new Timestamp(new Date().getTime()); stmt.setInt(1, enrolmentId); @@ -372,6 +379,7 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { } return true; } + @Override public EnrolmentInfo.Status getStatus(int deviceId, String currentOwner, int tenantId) throws DeviceManagementDAOException { @@ -529,11 +537,11 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { } } - private Connection getConnection() throws SQLException { + protected Connection getConnection() throws SQLException { return DeviceManagementDAOFactory.getConnection(); } - private EnrolmentInfo loadEnrolment(ResultSet rs) throws SQLException { + protected EnrolmentInfo loadEnrolment(ResultSet rs) throws SQLException { EnrolmentInfo enrolmentInfo = new EnrolmentInfo(); enrolmentInfo.setOwner(rs.getString("OWNER")); enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.valueOf(rs.getString("OWNERSHIP"))); diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/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 17236e678f..2cb472d1ff 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 @@ -1283,8 +1283,9 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { + "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, " @@ -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 (", ")"); @@ -1344,8 +1344,9 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { + "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, " @@ -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 d6f6af0a73..f22d6ae35c 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,17 +18,25 @@ package io.entgra.device.mgt.core.device.mgt.core.dao.impl.device; -import io.entgra.device.mgt.core.device.mgt.common.*; +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.DeviceManagementConstants; +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; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import java.sql.*; +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; @@ -63,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, " + @@ -82,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 " + @@ -120,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 = ?"; @@ -155,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; @@ -171,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); } @@ -191,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<>(); @@ -222,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 " + @@ -263,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 " + @@ -306,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 " + @@ -347,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 " + @@ -391,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, " + @@ -404,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); @@ -440,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, " + @@ -523,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, " + @@ -537,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 = ?"; @@ -581,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; @@ -589,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); } @@ -613,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<>(); @@ -634,8 +646,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { } @Override - public List searchDevicesInGroup(PaginationRequest request, int tenantId) - throws DeviceManagementDAOException { + public List searchDevicesInGroup(PaginationRequest request, int tenantId) throws DeviceManagementDAOException { List devices = null; int groupId = request.getGroupId(); String deviceType = request.getDeviceType(); @@ -660,8 +671,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, " + @@ -674,13 +686,13 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { "gd.DESCRIPTION, " + "gd.NAME, " + "gd.DEVICE_IDENTIFICATION, " + - "t.NAME AS DEVICE_TYPE " + + "gd.LAST_UPDATED_TIMESTAMP " + "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 " + @@ -692,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 > ?"; + sql = sql + " AND gd.LAST_UPDATED_TIMESTAMP > ?"; isSinceProvided = true; } + sql = sql + " ) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND e.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 = ?"; @@ -744,7 +756,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { } } } - sql = sql + " LIMIT ?,?"; + sql = sql + " LIMIT ? OFFSET ?"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { int paramIdx = 1; @@ -756,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); } @@ -781,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<>(); @@ -809,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()) { @@ -843,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()) { @@ -878,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()) { @@ -916,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, " + @@ -934,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 = ?"; @@ -945,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; @@ -959,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()) { @@ -990,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, " + @@ -999,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()) { @@ -1011,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)) { @@ -1027,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<>(); @@ -1055,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()) { @@ -1113,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 ? "; @@ -1128,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(); @@ -1179,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); @@ -1214,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(); @@ -1264,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, " @@ -1272,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 " @@ -1289,7 +1299,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { } query = query.concat("WHERE DM_DEVICE.ID IN ("); StringJoiner joiner = new StringJoiner(",", query , - ") AND DM_DEVICE.TENANT_ID = ? AND e.STATUS != ?"); + ") AND DM_DEVICE.TENANT_ID = ? AND e.STATUS != ? AND e.STATUS != ?"); deviceIds.stream().map(ignored -> "?").forEach(joiner::add); query = joiner.toString(); @@ -1324,7 +1334,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { } } - query = query + " LIMIT ?,?"; + query = query + " LIMIT ? OFFSET ?"; try (PreparedStatement ps = conn.prepareStatement(query)) { @@ -1333,6 +1343,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { } ps.setInt(index++, tenantId); ps.setString(index++, EnrolmentInfo.Status.REMOVED.toString()); + ps.setString(index++, EnrolmentInfo.Status.DELETED.toString()); if (isDeviceNameProvided) { ps.setString(index++, name + "%"); } @@ -1356,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()) { @@ -1435,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, " + @@ -1449,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 = ? "; @@ -1478,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<>(); @@ -1548,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 7cfcd9946d..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,1440 +18,55 @@ package io.entgra.device.mgt.core.device.mgt.core.dao.impl.device; -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.PaginationRequest; -import io.entgra.device.mgt.core.device.mgt.common.device.details.DeviceInfo; +import io.entgra.device.mgt.core.device.mgt.common.EnrolmentInfo; import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOException; -import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOFactory; -import io.entgra.device.mgt.core.device.mgt.core.dao.impl.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.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import java.sql.*; -import java.util.ArrayList; -import java.util.Date; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; 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"; - - 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()); - - 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); - } - } - - //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; - } - - 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"; - 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); - } - } + for (Device device : validDevices) { - @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 = ?"); - - 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"; + selectStatement.setInt(1, device.getEnrolmentInfo().getId()); + selectStatement.setInt(2, device.getId()); - try (PreparedStatement ps = conn.prepareStatement(query)) { - - for (Integer deviceId : deviceIds) { - ps.setObject(index++, deviceId); - } - - ps.setInt(index++, tenantId); - 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); - } + ResultSet resultSet = selectStatement.executeQuery(); + int lastRecordId = 0; + if (resultSet.next()) { + lastRecordId = resultSet.getInt("ID"); } - try (ResultSet rs = ps.executeQuery()) { - if (rs.next()) { - return rs.getInt("DEVICE_ID"); - } - return 0; - } + updateStatement.setString(1, String.valueOf(EnrolmentInfo.Status.DELETED)); + updateStatement.setInt(2, lastRecordId); + updateStatement.execute(); } - } 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"; + String msg = "SQL error occurred while updating device status properties."; 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/device/PostgreSQLDeviceDAOImpl.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/PostgreSQLDeviceDAOImpl.java index 82e35a3842..68d125ed04 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/PostgreSQLDeviceDAOImpl.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/PostgreSQLDeviceDAOImpl.java @@ -21,10 +21,9 @@ package io.entgra.device.mgt.core.device.mgt.core.dao.impl.device; 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.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; @@ -40,7 +39,7 @@ import java.util.Map; /** * This class holds the generic implementation of DeviceDAO which can be used to support ANSI db syntax. */ -public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl { +public class PostgreSQLDeviceDAOImpl extends GenericDeviceDAOImpl { private static final Log log = LogFactory.getLog(PostgreSQLDeviceDAOImpl.class); @@ -1051,7 +1050,7 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl { + "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 DM_DEVICE.TENANT_ID = ? AND e.STATUS NOT IN (?, ?)"); deviceIds.stream().map(ignored -> "?").forEach(joiner::add); String query = joiner.toString(); @@ -1093,6 +1092,8 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl { } ps.setInt(index++, tenantId); + ps.setString(index++, EnrolmentInfo.Status.REMOVED.toString()); + ps.setString(index++, EnrolmentInfo.Status.DELETED.toString()); if (isDeviceNameProvided) { ps.setString(index++, name + "%"); } @@ -1291,7 +1292,4 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl { } } - 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/device/SQLServerDeviceDAOImpl.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/SQLServerDeviceDAOImpl.java index 40a785a639..fcdb36e084 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/SQLServerDeviceDAOImpl.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/SQLServerDeviceDAOImpl.java @@ -24,8 +24,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; import org.apache.commons.lang.StringUtils; @@ -46,7 +44,7 @@ import java.util.Map; /** * This class holds the generic implementation of DeviceDAO which can be used to support ANSI db syntax. */ -public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl { +public class SQLServerDeviceDAOImpl extends GenericDeviceDAOImpl { private static final Log log = LogFactory.getLog(SQLServerDeviceDAOImpl.class); @@ -924,7 +922,7 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl { + "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 DM_DEVICE.TENANT_ID = ? AND e.STATUS != ? AND e.STATUS != ?"); deviceIds.stream().map(ignored -> "?").forEach(joiner::add); String query = joiner.toString(); @@ -966,6 +964,8 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl { } ps.setInt(index++, tenantId); + ps.setString(index++, EnrolmentInfo.Status.REMOVED.toString()); + ps.setString(index++, EnrolmentInfo.Status.DELETED.toString()); if (isDeviceNameProvided) { ps.setString(index++, name + "%"); } @@ -1113,10 +1113,6 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl { } } - private Connection getConnection() throws SQLException { - return DeviceManagementDAOFactory.getConnection(); - } - @Override public List getDevicesByDuration(PaginationRequest request, int tenantId, String fromDate, String toDate) diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/enrolment/GenericEnrollmentDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/enrolment/GenericEnrollmentDAOImpl.java index 33519e5182..831e058ad9 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/enrolment/GenericEnrollmentDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/enrolment/GenericEnrollmentDAOImpl.java @@ -17,532 +17,8 @@ */ package io.entgra.device.mgt.core.device.mgt.core.dao.impl.enrolment; -import io.entgra.device.mgt.core.device.mgt.common.Device; -import io.entgra.device.mgt.core.device.mgt.common.DeviceManagementConstants; -import io.entgra.device.mgt.core.device.mgt.common.EnrolmentInfo; -import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOException; -import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOFactory; import io.entgra.device.mgt.core.device.mgt.core.dao.impl.AbstractEnrollmentDAOImpl; -import io.entgra.device.mgt.core.device.mgt.core.dao.util.DeviceManagementDAOUtil; -import org.wso2.carbon.context.PrivilegedCarbonContext; - -import java.sql.*; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; public class GenericEnrollmentDAOImpl extends AbstractEnrollmentDAOImpl { - @Override - public EnrolmentInfo addEnrollment(int deviceId, EnrolmentInfo enrolmentInfo, - int tenantId) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - try { - conn = this.getConnection(); - String sql = "INSERT INTO DM_ENROLMENT(DEVICE_ID, OWNER, OWNERSHIP, STATUS, " + - "DATE_OF_ENROLMENT, DATE_OF_LAST_UPDATE, TENANT_ID) VALUES(?, ?, ?, ?, ?, ?, ?)"; - stmt = conn.prepareStatement(sql, new String[] {"id"}); - Timestamp enrollmentTime = new Timestamp(new Date().getTime()); - stmt.setInt(1, deviceId); - stmt.setString(2, enrolmentInfo.getOwner()); - stmt.setString(3, enrolmentInfo.getOwnership().toString()); - stmt.setString(4, enrolmentInfo.getStatus().toString()); - stmt.setTimestamp(5, enrollmentTime); - stmt.setTimestamp(6, enrollmentTime); - stmt.setInt(7, tenantId); - stmt.execute(); - - rs = stmt.getGeneratedKeys(); - if (rs.next()) { - int enrolmentId = rs.getInt(1); - enrolmentInfo.setId(enrolmentId); - enrolmentInfo.setDateOfEnrolment(enrollmentTime.getTime()); - enrolmentInfo.setDateOfLastUpdate(enrollmentTime.getTime()); - addDeviceStatus(enrolmentId, enrolmentInfo.getStatus()); - return enrolmentInfo; - } - return null; - } catch (SQLException e) { - e.printStackTrace(); - throw new DeviceManagementDAOException("Error occurred while adding enrolment configuration", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - } - - @Override - public int updateEnrollment(EnrolmentInfo enrolmentInfo, int tenantId) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - try { - conn = this.getConnection(); - String sql = "UPDATE DM_ENROLMENT SET OWNERSHIP = ?, STATUS = ?, DATE_OF_LAST_UPDATE = ? " + - "WHERE ID = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql); - stmt.setString(1, enrolmentInfo.getOwnership().toString()); - stmt.setString(2, enrolmentInfo.getStatus().toString()); - stmt.setTimestamp(3, new Timestamp(new Date().getTime())); - stmt.setInt(4, enrolmentInfo.getId()); - stmt.setInt(5, tenantId); - int updatedCount = stmt.executeUpdate(); - return updatedCount; - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while updating enrolment configuration", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - } - - @Override - public boolean updateEnrollmentStatus(List enrolmentInfos) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - boolean status = false; - int updateStatus = -1; - try { - conn = this.getConnection(); - String sql = "UPDATE DM_ENROLMENT SET STATUS = ? WHERE ID = ?"; - stmt = conn.prepareStatement(sql); - if (conn.getMetaData().supportsBatchUpdates()) { - for (EnrolmentInfo enrolmentInfo : enrolmentInfos) { - stmt.setString(1, enrolmentInfo.getStatus().toString()); - stmt.setInt(2, enrolmentInfo.getId()); - stmt.addBatch(); - } - updateStatus = stmt.executeBatch().length; - } else { - for (EnrolmentInfo enrolmentInfo : enrolmentInfos) { - stmt.setString(1, enrolmentInfo.getStatus().toString()); - stmt.setInt(2, enrolmentInfo.getId()); - updateStatus = stmt.executeUpdate(); - } - } - if (updateStatus > 0) { - status = true; - } - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while updating enrolment status of given device-list.", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - return status; - } - - @Override - public int removeEnrollment(int deviceId, String currentOwner, - int tenantId) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - int status = -1; - try { - conn = this.getConnection(); - String sql = "DELETE FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql, new String[] {"id"}); - stmt.setInt(1, deviceId); - stmt.setString(2, currentOwner); - stmt.setInt(3, tenantId); - stmt.executeUpdate(); - - rs = stmt.getGeneratedKeys(); - if (rs.next()) { - status = 1; - } - return status; - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while removing device enrolment", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - } - - private int getCountOfDevicesOfOwner(String owner, int tenantID) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - int count = 0; - try { - conn = this.getConnection(); - String checkQuery = "SELECT COUNT(ID) AS COUNT FROM DM_ENROLMENT WHERE OWNER = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(checkQuery); - stmt.setString(1, owner); - stmt.setInt(2, tenantID); - rs = stmt.executeQuery(); - if(rs.next()){ - count = rs.getInt("COUNT"); - } - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while trying to get device " + - "count of Owner : "+owner, e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - return count; - } - - @Override - public boolean setStatus(String currentOwner, EnrolmentInfo.Status status, - int tenantId) throws DeviceManagementDAOException { - return setStatusAllDevices(currentOwner, status, tenantId); - } - - @Override - public boolean setStatusAllDevices(String currentOwner, EnrolmentInfo.Status status, int tenantId) - throws DeviceManagementDAOException{ - Connection conn; - PreparedStatement stmt = null; - Timestamp updateTime = new Timestamp(new Date().getTime()); - if(getCountOfDevicesOfOwner(currentOwner, tenantId) > 0){ - try { - conn = this.getConnection(); - String sql = "UPDATE DM_ENROLMENT SET STATUS = ?, DATE_OF_LAST_UPDATE = ? WHERE OWNER = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql); - stmt.setString(1, status.toString()); - stmt.setTimestamp(2, updateTime); - stmt.setString(3, currentOwner); - stmt.setInt(4, tenantId); - stmt.executeUpdate(); - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, null); - } - return addDeviceStatus(currentOwner, status, tenantId); - } else { - return false; - } - } - - @Override - public boolean setStatus(int enrolmentID, EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - Timestamp updateTime = new Timestamp(new Date().getTime()); - try { - conn = this.getConnection(); - String sql = "UPDATE DM_ENROLMENT SET STATUS = ?, DATE_OF_LAST_UPDATE = ? WHERE ID = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql); - stmt.setString(1, status.toString()); - stmt.setTimestamp(2, updateTime); - stmt.setInt(3, enrolmentID); - stmt.setInt(4, tenantId); - int updatedRowCount = stmt.executeUpdate(); - if (updatedRowCount != 1){ - throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment: "+ - updatedRowCount + " rows were updated instead of one row!!!"); - } - // save the device status history - addDeviceStatus(enrolmentID, status); - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, null); - } - return true; - } - - public boolean addDeviceStatus(EnrolmentInfo config) throws DeviceManagementDAOException { - return addDeviceStatus(config.getId(), config.getStatus()); - } - - public boolean addDeviceStatus(String currentOwner, EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException { - Connection conn; - String changedBy = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); - if (changedBy == null){ - changedBy = DeviceManagementConstants.MaintenanceProperties.MAINTENANCE_USER; - } - PreparedStatement stmt = null; - ResultSet rs = null; - List enrolmentInfoList = new ArrayList<>(); - try { - conn = this.getConnection(); - String sql = "SELECT ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, " + - "DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE OWNER = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql); - stmt.setString(1, currentOwner); - stmt.setInt(2, tenantId); - rs = stmt.executeQuery(); - while (rs.next()) { - int enrolmentId = rs.getInt("ID"); - int deviceId = rs.getInt("DEVICE_ID"); - enrolmentInfoList.add(new int[]{enrolmentId, deviceId}); - } - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - Timestamp updateTime = new Timestamp(new Date().getTime()); - sql = "INSERT INTO DM_DEVICE_STATUS (ENROLMENT_ID, DEVICE_ID, STATUS, UPDATE_TIME, CHANGED_BY) VALUES(?, ?, ?, ?, ?)"; - try (PreparedStatement ps = conn.prepareStatement(sql)) { - if (conn.getMetaData().supportsBatchUpdates()) { - for(int[] info: enrolmentInfoList){ - ps.setInt(1, info[0]); - ps.setInt(2, info[1]); - ps.setString(3, status.toString()); - ps.setTimestamp(4, updateTime); - ps.setString(5, changedBy); - ps.addBatch(); - } - int[] batchResult = ps.executeBatch(); - for (int i : batchResult) { - if (i == 0 || i == Statement.SUCCESS_NO_INFO || i == Statement.EXECUTE_FAILED) { - return false; - } - } - } else { - for(int[] info: enrolmentInfoList){ - ps.setInt(1, info[0]); - ps.setInt(2, info[1]); - ps.setString(3, status.toString()); - ps.setTimestamp(4, updateTime); - ps.setString(5, changedBy); - ps.execute(); - } - - } - } - - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while retrieving the enrolments " + - "information of owner '" + currentOwner + "'", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - return true; - } - - public boolean addDeviceStatus(int enrolmentId, EnrolmentInfo.Status status) throws DeviceManagementDAOException { - Connection conn; - String changedBy = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); - if (changedBy == null){ - changedBy = DeviceManagementConstants.MaintenanceProperties.MAINTENANCE_USER; - } - PreparedStatement stmt = null; - try { - conn = this.getConnection(); - // get the device id and last udpated status from the device status table - String sql = "SELECT DEVICE_ID, STATUS FROM DM_DEVICE_STATUS WHERE ENROLMENT_ID = ? ORDER BY UPDATE_TIME DESC LIMIT 1"; - stmt = conn.prepareStatement(sql); - stmt.setInt(1, enrolmentId); - ResultSet rs = stmt.executeQuery(); - int deviceId = -1; - EnrolmentInfo.Status previousStatus = null; - if (rs.next()) { - // if there is a record corresponding to the enrolment we save the status and the device id - previousStatus = EnrolmentInfo.Status.valueOf(rs.getString("STATUS")); - deviceId = rs.getInt("DEVICE_ID"); - } - DeviceManagementDAOUtil.cleanupResources(stmt, null); - // if there was no record for the enrolment or the previous status is not the same as the current status - // we'll add a record - if (previousStatus == null || previousStatus != status){ - if (deviceId == -1) { - // we need the device id in order to add a new record, therefore we get it from the enrolment table - sql = "SELECT DEVICE_ID FROM DM_ENROLMENT WHERE ID = ?"; - stmt = conn.prepareStatement(sql); - stmt.setInt(1, enrolmentId); - rs = stmt.executeQuery(); - if (rs.next()) { - deviceId = rs.getInt("DEVICE_ID"); - } else { - // if there were no records corresponding to the enrolment id this is a problem. i.e. enrolment - // id is invalid - throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment: no record for enrolment id " + enrolmentId); - } - DeviceManagementDAOUtil.cleanupResources(stmt, null); - } - - sql = "INSERT INTO DM_DEVICE_STATUS (ENROLMENT_ID, DEVICE_ID, STATUS, UPDATE_TIME, CHANGED_BY) VALUES(?, ?, ?, ?, ?)"; - stmt = conn.prepareStatement(sql); - Timestamp updateTime = new Timestamp(new Date().getTime()); - stmt.setInt(1, enrolmentId); - stmt.setInt(2, deviceId); - stmt.setString(3, status.toString()); - stmt.setTimestamp(4, updateTime); - stmt.setString(5, changedBy); - stmt.execute(); - } else { - // no need to update status since the last recorded status is the same as the current status - } - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while setting the status of device", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, null); - } - return true; - } - @Override - public EnrolmentInfo.Status getStatus(int deviceId, String currentOwner, - int tenantId) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - EnrolmentInfo.Status status = null; - try { - conn = this.getConnection(); - String sql = "SELECT STATUS FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql); - stmt.setInt(1, deviceId); - stmt.setString(2, currentOwner); - stmt.setInt(3, tenantId); - rs = stmt.executeQuery(); - if (rs.next()) { - status = EnrolmentInfo.Status.valueOf(rs.getString("STATUS")); - } - return status; - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - } - - @Override - public EnrolmentInfo getEnrollment(int deviceId, String currentOwner, - int tenantId) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - EnrolmentInfo enrolmentInfo = null; - try { - conn = this.getConnection(); - String sql = "SELECT ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, " + - "DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql); - stmt.setInt(1, deviceId); - stmt.setString(2, currentOwner); - stmt.setInt(3, tenantId); - rs = stmt.executeQuery(); - if (rs.next()) { - enrolmentInfo = this.loadEnrolment(rs); - } - return enrolmentInfo; - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while retrieving the enrolment " + - "information of user '" + currentOwner + "' upon device '" + deviceId + "'", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - } - - @Override - public EnrolmentInfo getEnrollment(int deviceId, int tenantId) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - EnrolmentInfo enrolmentInfo = null; - try { - conn = this.getConnection(); - String sql = "SELECT ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, " + - "DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND TENANT_ID = ? " + - "ORDER BY DATE_OF_LAST_UPDATE DESC"; - stmt = conn.prepareStatement(sql); - stmt.setInt(1, deviceId); - stmt.setInt(2, tenantId); - rs = stmt.executeQuery(); - if (rs.next()) { - enrolmentInfo = this.loadEnrolment(rs); - } - return enrolmentInfo; - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while retrieving the enrolment " + - "information of device '" + deviceId + "'", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - } - - @Override - public List getEnrollmentsOfUser(int deviceId, String user, int tenantId) - throws DeviceManagementDAOException { - List enrolmentInfos = new ArrayList<>(); - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - EnrolmentInfo enrolmentInfo = null; - try { - conn = this.getConnection(); - String sql = "SELECT ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, " + - "DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql); - stmt.setInt(1, deviceId); - stmt.setString(2, user); - stmt.setInt(3, tenantId); - rs = stmt.executeQuery(); - while (rs.next()) { - enrolmentInfo = this.loadEnrolment(rs); - enrolmentInfos.add(enrolmentInfo); - } - return enrolmentInfos; - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while retrieving the enrolments " + - "information of user '" + user + "' upon device '" + deviceId + "'", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - } - - @Override - public boolean updateOwnerOfEnrollment(List devices, String owner, int tenantId) - throws DeviceManagementDAOException { - try { - Connection conn = this.getConnection(); - boolean updateStatus = true; - String sql = "UPDATE DM_ENROLMENT " - + "SET OWNER = ?, IS_TRANSFERRED = ?, DATE_OF_LAST_UPDATE = ? " - + "WHERE ID = ? AND TENANT_ID = ?"; - try (PreparedStatement ps = conn.prepareStatement(sql)) { - if (conn.getMetaData().supportsBatchUpdates()) { - for (Device device : devices) { - ps.setString(1, owner); - ps.setBoolean(2, device.getEnrolmentInfo().isTransferred()); - ps.setTimestamp(3, new Timestamp(new Date().getTime())); - ps.setInt(4, device.getEnrolmentInfo().getId()); - ps.setInt(5, tenantId); - ps.addBatch(); - } - int[] batchResult = ps.executeBatch(); - for (int i : batchResult) { - if (i == 0 || i == Statement.SUCCESS_NO_INFO || i == Statement.EXECUTE_FAILED) { - updateStatus = false; - break; - } - } - } else { - for (Device device : devices) { - ps.setString(1, owner); - ps.setBoolean(2, device.getEnrolmentInfo().isTransferred()); - ps.setInt(3, device.getId()); - ps.setInt(4, tenantId); - if (ps.executeUpdate() == 0) { - updateStatus = false; - break; - } - } - } - } - return updateStatus; - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while obtaining the DB connection to update the " - + "owner of the device enrollment.", e); - } - } - - private Connection getConnection() throws SQLException { - return DeviceManagementDAOFactory.getConnection(); - } - - private EnrolmentInfo loadEnrolment(ResultSet rs) throws SQLException { - EnrolmentInfo enrolmentInfo = new EnrolmentInfo(); - enrolmentInfo.setOwner(rs.getString("OWNER")); - enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.valueOf(rs.getString("OWNERSHIP"))); - enrolmentInfo.setTransferred(rs.getBoolean("IS_TRANSFERRED")); - enrolmentInfo.setDateOfEnrolment(rs.getTimestamp("DATE_OF_ENROLMENT").getTime()); - enrolmentInfo.setDateOfLastUpdate(rs.getTimestamp("DATE_OF_LAST_UPDATE").getTime()); - enrolmentInfo.setStatus(EnrolmentInfo.Status.valueOf(rs.getString("STATUS"))); - enrolmentInfo.setId(rs.getInt("ID")); - return enrolmentInfo; - } - } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/enrolment/SQLServerEnrollmentDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/enrolment/SQLServerEnrollmentDAOImpl.java index 20cd4b2378..7ff839512f 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/enrolment/SQLServerEnrollmentDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/enrolment/SQLServerEnrollmentDAOImpl.java @@ -17,301 +17,22 @@ */ package io.entgra.device.mgt.core.device.mgt.core.dao.impl.enrolment; -import io.entgra.device.mgt.core.device.mgt.common.Device; import io.entgra.device.mgt.core.device.mgt.common.DeviceManagementConstants; import io.entgra.device.mgt.core.device.mgt.common.EnrolmentInfo; import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOException; -import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOFactory; import io.entgra.device.mgt.core.device.mgt.core.dao.impl.AbstractEnrollmentDAOImpl; import io.entgra.device.mgt.core.device.mgt.core.dao.util.DeviceManagementDAOUtil; import org.wso2.carbon.context.PrivilegedCarbonContext; -import java.sql.*; -import java.util.ArrayList; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Timestamp; import java.util.Date; -import java.util.List; public class SQLServerEnrollmentDAOImpl extends AbstractEnrollmentDAOImpl { - @Override - public EnrolmentInfo addEnrollment(int deviceId, EnrolmentInfo enrolmentInfo, - int tenantId) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - try { - conn = this.getConnection(); - String sql = "INSERT INTO DM_ENROLMENT(DEVICE_ID, OWNER, OWNERSHIP, STATUS, " + - "DATE_OF_ENROLMENT, DATE_OF_LAST_UPDATE, TENANT_ID) VALUES(?, ?, ?, ?, ?, ?, ?)"; - stmt = conn.prepareStatement(sql, new String[] {"id"}); - Timestamp enrollmentTime = new Timestamp(new Date().getTime()); - stmt.setInt(1, deviceId); - stmt.setString(2, enrolmentInfo.getOwner()); - stmt.setString(3, enrolmentInfo.getOwnership().toString()); - stmt.setString(4, enrolmentInfo.getStatus().toString()); - stmt.setTimestamp(5, enrollmentTime); - stmt.setTimestamp(6, enrollmentTime); - stmt.setInt(7, tenantId); - stmt.execute(); - - rs = stmt.getGeneratedKeys(); - if (rs.next()) { - int enrolmentId = rs.getInt(1); - enrolmentInfo.setId(enrolmentId); - enrolmentInfo.setDateOfEnrolment(enrollmentTime.getTime()); - enrolmentInfo.setDateOfLastUpdate(enrollmentTime.getTime()); - addDeviceStatus(enrolmentId, enrolmentInfo.getStatus()); - return enrolmentInfo; - } - return null; - } catch (SQLException e) { - e.printStackTrace(); - throw new DeviceManagementDAOException("Error occurred while adding enrolment configuration", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - } - - @Override - public int updateEnrollment(EnrolmentInfo enrolmentInfo, int tenantId) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - try { - conn = this.getConnection(); - String sql = "UPDATE DM_ENROLMENT SET OWNERSHIP = ?, STATUS = ?, DATE_OF_LAST_UPDATE = ? " + - "WHERE ID = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql); - stmt.setString(1, enrolmentInfo.getOwnership().toString()); - stmt.setString(2, enrolmentInfo.getStatus().toString()); - stmt.setTimestamp(3, new Timestamp(new Date().getTime())); - stmt.setInt(4, enrolmentInfo.getId()); - stmt.setInt(5, tenantId); - int updatedCount = stmt.executeUpdate(); - return updatedCount; - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while updating enrolment configuration", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - } - - @Override - public boolean updateEnrollmentStatus(List enrolmentInfos) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - boolean status = false; - int updateStatus = -1; - try { - conn = this.getConnection(); - String sql = "UPDATE DM_ENROLMENT SET STATUS = ? WHERE ID = ?"; - stmt = conn.prepareStatement(sql); - if (conn.getMetaData().supportsBatchUpdates()) { - for (EnrolmentInfo enrolmentInfo : enrolmentInfos) { - stmt.setString(1, enrolmentInfo.getStatus().toString()); - stmt.setInt(2, enrolmentInfo.getId()); - stmt.addBatch(); - } - updateStatus = stmt.executeBatch().length; - } else { - for (EnrolmentInfo enrolmentInfo : enrolmentInfos) { - stmt.setString(1, enrolmentInfo.getStatus().toString()); - stmt.setInt(2, enrolmentInfo.getId()); - updateStatus = stmt.executeUpdate(); - } - } - if (updateStatus > 0) { - status = true; - } - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while updating enrolment status of given device-list.", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - return status; - } - - @Override - public int removeEnrollment(int deviceId, String currentOwner, - int tenantId) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - int status = -1; - try { - conn = this.getConnection(); - String sql = "DELETE FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql, new String[] {"id"}); - stmt.setInt(1, deviceId); - stmt.setString(2, currentOwner); - stmt.setInt(3, tenantId); - stmt.executeUpdate(); - - rs = stmt.getGeneratedKeys(); - if (rs.next()) { - status = 1; - } - return status; - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while removing device enrolment", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - } - - private int getCountOfDevicesOfOwner(String owner, int tenantID) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - int count = 0; - try { - conn = this.getConnection(); - String checkQuery = "SELECT COUNT(ID) AS COUNT FROM DM_ENROLMENT WHERE OWNER = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(checkQuery); - stmt.setString(1, owner); - stmt.setInt(2, tenantID); - rs = stmt.executeQuery(); - if(rs.next()){ - count = rs.getInt("COUNT"); - } - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while trying to get device " + - "count of Owner : "+owner, e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - return count; - } - - @Override - public boolean setStatus(String currentOwner, EnrolmentInfo.Status status, - int tenantId) throws DeviceManagementDAOException { - return setStatusAllDevices(currentOwner, status, tenantId); - } - - @Override - public boolean setStatusAllDevices(String currentOwner, EnrolmentInfo.Status status, int tenantId) - throws DeviceManagementDAOException{ - Connection conn; - PreparedStatement stmt = null; - Timestamp updateTime = new Timestamp(new Date().getTime()); - if(getCountOfDevicesOfOwner(currentOwner, tenantId) > 0){ - try { - conn = this.getConnection(); - String sql = "UPDATE DM_ENROLMENT SET STATUS = ?, DATE_OF_LAST_UPDATE = ? WHERE OWNER = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql); - stmt.setString(1, status.toString()); - stmt.setTimestamp(2, updateTime); - stmt.setString(3, currentOwner); - stmt.setInt(4, tenantId); - stmt.executeUpdate(); - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, null); - } - return addDeviceStatus(currentOwner, status, tenantId); - } else { - return false; - } - } - - @Override - public boolean setStatus(int enrolmentID, EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - Timestamp updateTime = new Timestamp(new Date().getTime()); - try { - conn = this.getConnection(); - String sql = "UPDATE DM_ENROLMENT SET STATUS = ?, DATE_OF_LAST_UPDATE = ? WHERE ID = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql); - stmt.setString(1, status.toString()); - stmt.setTimestamp(2, updateTime); - stmt.setInt(3, enrolmentID); - stmt.setInt(4, tenantId); - int updatedRowCount = stmt.executeUpdate(); - if (updatedRowCount != 1){ - throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment: "+ - updatedRowCount + " rows were updated instead of one row!!!"); - } - // save the device status history - addDeviceStatus(enrolmentID, status); - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, null); - } - return true; - } - - public boolean addDeviceStatus(EnrolmentInfo config) throws DeviceManagementDAOException { - return addDeviceStatus(config.getId(), config.getStatus()); - } - - public boolean addDeviceStatus(String currentOwner, EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException { - Connection conn; - String changedBy = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); - if (changedBy == null){ - changedBy = DeviceManagementConstants.MaintenanceProperties.MAINTENANCE_USER; - } - PreparedStatement stmt = null; - ResultSet rs = null; - List enrolmentInfoList = new ArrayList<>(); - try { - conn = this.getConnection(); - String sql = "SELECT ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, " + - "DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE OWNER = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql); - stmt.setString(1, currentOwner); - stmt.setInt(2, tenantId); - rs = stmt.executeQuery(); - while (rs.next()) { - int enrolmentId = rs.getInt("ID"); - int deviceId = rs.getInt("DEVICE_ID"); - enrolmentInfoList.add(new int[]{enrolmentId, deviceId}); - } - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - Timestamp updateTime = new Timestamp(new Date().getTime()); - sql = "INSERT INTO DM_DEVICE_STATUS (ENROLMENT_ID, DEVICE_ID, STATUS, UPDATE_TIME, CHANGED_BY) VALUES(?, ?, ?, ?, ?)"; - try (PreparedStatement ps = conn.prepareStatement(sql)) { - if (conn.getMetaData().supportsBatchUpdates()) { - for(int[] info: enrolmentInfoList){ - ps.setInt(1, info[0]); - ps.setInt(2, info[1]); - ps.setString(3, status.toString()); - ps.setTimestamp(4, updateTime); - ps.setString(5, changedBy); - ps.addBatch(); - } - int[] batchResult = ps.executeBatch(); - for (int i : batchResult) { - if (i == 0 || i == Statement.SUCCESS_NO_INFO || i == Statement.EXECUTE_FAILED) { - return false; - } - } - } else { - for(int[] info: enrolmentInfoList){ - ps.setInt(1, info[0]); - ps.setInt(2, info[1]); - ps.setString(3, status.toString()); - ps.setTimestamp(4, updateTime); - ps.setString(5, changedBy); - ps.execute(); - } - - } - } - - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while retrieving the enrolments " + - "information of owner '" + currentOwner + "'", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - return true; - } - public boolean addDeviceStatus(int enrolmentId, EnrolmentInfo.Status status) throws DeviceManagementDAOException { Connection conn; String changedBy = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); @@ -320,8 +41,8 @@ public class SQLServerEnrollmentDAOImpl extends AbstractEnrollmentDAOImpl { } PreparedStatement stmt = null; try { - conn = this.getConnection(); - // get the device id and last udpated status from the device status table + conn = getConnection(); + // get the device id and last updated status from the device status table String sql = "SELECT TOP 1 DEVICE_ID, STATUS FROM DM_DEVICE_STATUS WHERE ENROLMENT_ID = ? ORDER BY UPDATE_TIME DESC"; stmt = conn.prepareStatement(sql); stmt.setInt(1, enrolmentId); @@ -372,177 +93,5 @@ public class SQLServerEnrollmentDAOImpl extends AbstractEnrollmentDAOImpl { } return true; } - @Override - public EnrolmentInfo.Status getStatus(int deviceId, String currentOwner, - int tenantId) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - EnrolmentInfo.Status status = null; - try { - conn = this.getConnection(); - String sql = "SELECT STATUS FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql); - stmt.setInt(1, deviceId); - stmt.setString(2, currentOwner); - stmt.setInt(3, tenantId); - rs = stmt.executeQuery(); - if (rs.next()) { - status = EnrolmentInfo.Status.valueOf(rs.getString("STATUS")); - } - return status; - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - } - - @Override - public EnrolmentInfo getEnrollment(int deviceId, String currentOwner, - int tenantId) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - EnrolmentInfo enrolmentInfo = null; - try { - conn = this.getConnection(); - String sql = "SELECT ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, " + - "DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql); - stmt.setInt(1, deviceId); - stmt.setString(2, currentOwner); - stmt.setInt(3, tenantId); - rs = stmt.executeQuery(); - if (rs.next()) { - enrolmentInfo = this.loadEnrolment(rs); - } - return enrolmentInfo; - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while retrieving the enrolment " + - "information of user '" + currentOwner + "' upon device '" + deviceId + "'", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - } - - @Override - public EnrolmentInfo getEnrollment(int deviceId, int tenantId) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - EnrolmentInfo enrolmentInfo = null; - try { - conn = this.getConnection(); - String sql = "SELECT ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, " + - "DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND TENANT_ID = ? " + - "ORDER BY DATE_OF_LAST_UPDATE DESC"; - stmt = conn.prepareStatement(sql); - stmt.setInt(1, deviceId); - stmt.setInt(2, tenantId); - rs = stmt.executeQuery(); - if (rs.next()) { - enrolmentInfo = this.loadEnrolment(rs); - } - return enrolmentInfo; - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while retrieving the enrolment " + - "information of device '" + deviceId + "'", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - } - - @Override - public List getEnrollmentsOfUser(int deviceId, String user, int tenantId) - throws DeviceManagementDAOException { - List enrolmentInfos = new ArrayList<>(); - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - EnrolmentInfo enrolmentInfo = null; - try { - conn = this.getConnection(); - String sql = "SELECT ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, " + - "DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql); - stmt.setInt(1, deviceId); - stmt.setString(2, user); - stmt.setInt(3, tenantId); - rs = stmt.executeQuery(); - while (rs.next()) { - enrolmentInfo = this.loadEnrolment(rs); - enrolmentInfos.add(enrolmentInfo); - } - return enrolmentInfos; - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while retrieving the enrolments " + - "information of user '" + user + "' upon device '" + deviceId + "'", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - } - - @Override - public boolean updateOwnerOfEnrollment(List devices, String owner, int tenantId) - throws DeviceManagementDAOException { - try { - Connection conn = this.getConnection(); - boolean updateStatus = true; - String sql = "UPDATE DM_ENROLMENT " - + "SET OWNER = ?, IS_TRANSFERRED = ?, DATE_OF_LAST_UPDATE = ? " - + "WHERE ID = ? AND TENANT_ID = ?"; - try (PreparedStatement ps = conn.prepareStatement(sql)) { - if (conn.getMetaData().supportsBatchUpdates()) { - for (Device device : devices) { - ps.setString(1, owner); - ps.setBoolean(2, device.getEnrolmentInfo().isTransferred()); - ps.setTimestamp(3, new Timestamp(new Date().getTime())); - ps.setInt(4, device.getEnrolmentInfo().getId()); - ps.setInt(5, tenantId); - ps.addBatch(); - } - int[] batchResult = ps.executeBatch(); - for (int i : batchResult) { - if (i == 0 || i == Statement.SUCCESS_NO_INFO || i == Statement.EXECUTE_FAILED) { - updateStatus = false; - break; - } - } - } else { - for (Device device : devices) { - ps.setString(1, owner); - ps.setBoolean(2, device.getEnrolmentInfo().isTransferred()); - ps.setInt(3, device.getId()); - ps.setInt(4, tenantId); - if (ps.executeUpdate() == 0) { - updateStatus = false; - break; - } - } - } - } - return updateStatus; - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while obtaining the DB connection to update the " - + "owner of the device enrollment.", e); - } - } - - private Connection getConnection() throws SQLException { - return DeviceManagementDAOFactory.getConnection(); - } - - private EnrolmentInfo loadEnrolment(ResultSet rs) throws SQLException { - EnrolmentInfo enrolmentInfo = new EnrolmentInfo(); - enrolmentInfo.setOwner(rs.getString("OWNER")); - enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.valueOf(rs.getString("OWNERSHIP"))); - enrolmentInfo.setTransferred(rs.getBoolean("IS_TRANSFERRED")); - enrolmentInfo.setDateOfEnrolment(rs.getTimestamp("DATE_OF_ENROLMENT").getTime()); - enrolmentInfo.setDateOfLastUpdate(rs.getTimestamp("DATE_OF_LAST_UPDATE").getTime()); - enrolmentInfo.setStatus(EnrolmentInfo.Status.valueOf(rs.getString("STATUS"))); - enrolmentInfo.setId(rs.getInt("ID")); - return enrolmentInfo; - } } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/geofence/GenericGeofenceDAOImpl.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/geofence/GenericGeofenceDAOImpl.java index 7cf29f3da1..f1575b7642 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/geofence/GenericGeofenceDAOImpl.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/geofence/GenericGeofenceDAOImpl.java @@ -625,6 +625,9 @@ public class GenericGeofenceDAOImpl extends AbstractGeofenceDAOImpl { geofenceData.setGroupData(groupMap); } } + if (geofenceData != null) { + geofenceData.setGroupIds(new ArrayList<>(groupMap.keySet())); + } return geofenceData; } } catch (SQLException 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/geofence/SQLServerGeofenceDAOImpl.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/geofence/SQLServerGeofenceDAOImpl.java index c1a719f446..db15476b48 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/geofence/SQLServerGeofenceDAOImpl.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/geofence/SQLServerGeofenceDAOImpl.java @@ -157,6 +157,9 @@ public class SQLServerGeofenceDAOImpl extends AbstractGeofenceDAOImpl { geofenceData.setFenceShape(rst.getString("FENCE_SHAPE")); geofenceData.setGroupData(groupMap); } + if (geofenceData != null) { + geofenceData.setGroupIds(new ArrayList<>(groupMap.keySet())); + } return geofenceData; } } 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 ae070fcdff..fcb86cf2ee 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 d7c2f98f8a..2d17e8bdbb 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 2b68c264cf..340705e28f 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 5ef0222258..443aeedbda 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 @@ -228,6 +228,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/event/config/EventOperationExecutor.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/event/config/EventOperationExecutor.java index c022226cb3..bc0a51b548 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/event/config/EventOperationExecutor.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/event/config/EventOperationExecutor.java @@ -76,6 +76,16 @@ public class EventOperationExecutor implements Runnable { } //extend with another cases to handle other types of events } + /** + * Build operation to create EVENT_UPDATE operation. + * @param operation Operation object to build + */ + private void buildEventUpdateOperation(ProfileOperation operation) { + if (eventSource.equalsIgnoreCase(DeviceManagementConstants.EventServices.GEOFENCE)) { + createGeoFenceUpdateOperation(operation); + } //extend with another cases to handle other types of events + } + /** * Build operation to create EVENT_CONFIG operation. * @param operation Operation object to build @@ -120,6 +130,18 @@ public class EventOperationExecutor implements Runnable { * @param operation operation object to set the payload */ private void createGeoFenceRevokeOperation(ProfileOperation operation) { + changeGeoFenceOperation(operation); + } + + /** + * Create EVENT_UPDATE operation object and attach payload to configure geo fence events + * @param operation operation object to set the payload + */ + private void createGeoFenceUpdateOperation(ProfileOperation operation) { + changeGeoFenceOperation(operation); + } + + private void changeGeoFenceOperation(ProfileOperation operation) { GeoFenceEventMeta geoFenceMeta = (GeoFenceEventMeta) eventMetaData; EventRevokeOperation eventRevokeOperation = new EventRevokeOperation(); eventRevokeOperation.setEventSource(eventSource); @@ -180,6 +202,9 @@ public class EventOperationExecutor implements Runnable { if (operationCode.equalsIgnoreCase(OperationMgtConstants.OperationCodes.EVENT_CONFIG)) { operation.setCode(OperationMgtConstants.OperationCodes.EVENT_CONFIG); buildEventConfigOperation(operation); + } else if (operationCode.equalsIgnoreCase(OperationMgtConstants.OperationCodes.EVENT_UPDATE)){ + operation.setCode(OperationMgtConstants.OperationCodes.EVENT_UPDATE); + buildEventUpdateOperation(operation); } else if (operationCode.equalsIgnoreCase(OperationMgtConstants.OperationCodes.EVENT_REVOKE)){ operation.setCode(OperationMgtConstants.OperationCodes.EVENT_REVOKE); buildEventRevokeOperation(operation); 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/event/config/GroupAssignmentEventOperationExecutor.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/event/config/GroupAssignmentEventOperationExecutor.java index 272ec28348..e6f292d166 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/event/config/GroupAssignmentEventOperationExecutor.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/event/config/GroupAssignmentEventOperationExecutor.java @@ -106,6 +106,8 @@ public class GroupAssignmentEventOperationExecutor implements Runnable { operation.setType(Operation.Type.PROFILE); if (operationCode.equalsIgnoreCase(OperationMgtConstants.OperationCodes.EVENT_CONFIG)) { buildEventConfigOperationObject(operation); + } else if (operationCode.equalsIgnoreCase(OperationMgtConstants.OperationCodes.EVENT_UPDATE)) { + buildEventUpdateOperation(operation); } else if (operationCode.equalsIgnoreCase(OperationMgtConstants.OperationCodes.EVENT_REVOKE)) { buildEventRevokeOperation(operation); } @@ -162,6 +164,19 @@ public class GroupAssignmentEventOperationExecutor implements Runnable { } } + /** + * Build EVENT_UPDATE operation attaching event payload + * @param operation operation object to build + * @throws EventConfigurationException if not events found for the specific group + */ + private void buildEventUpdateOperation(ProfileOperation operation) throws EventConfigurationException { + for (String eventSource : this.eventSources) { + if (eventSource.equalsIgnoreCase(DeviceManagementConstants.EventServices.GEOFENCE)) { + setGeoFenceUpdateOperationContent(operation); + } //add other cases to handle other types of events + } + } + /** * Build EVENT_CONFIG operation attaching event payload * @param operation operation object to build @@ -217,6 +232,18 @@ public class GroupAssignmentEventOperationExecutor implements Runnable { * @param operation operation object to attach payload */ private void setGeoFenceRevokeOperationContent(ProfileOperation operation){ + changeGeoFenceOperation(operation); + } + + /** + * Set operation payload GeoFence for EVENT_UPDATE operation + * @param operation operation object to attach payload + */ + private void setGeoFenceUpdateOperationContent(ProfileOperation operation){ + changeGeoFenceOperation(operation); + } + + private void changeGeoFenceOperation(ProfileOperation operation) { List revokeOperationList = new ArrayList<>(); for (GeofenceData geofenceData : this.geoFencesOfGroup) { EventRevokeOperation eventRevokeOperation = new EventRevokeOperation(); 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/geo/service/GeoLocationProviderServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java index 7b846ebe1e..add64ebac9 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java @@ -1307,7 +1307,7 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic DeviceManagementConstants.EventServices.GEOFENCE, new GeoFenceEventMeta(geofenceData), tenantId, geofenceData.getGroupIds()); } catch (EventConfigurationException e) { - String msg = "Failed while creating EVENT_REVOKE operation creation task entry while updating geo fence " + String msg = "Failed while creating EVENT_CONFIG operation creation task entry while updating geo fence " + geofenceData.getFenceName() + " of the tenant " + tenantId; log.error(msg, e); throw new GeoLocationBasedServiceException(msg, e); @@ -1634,11 +1634,11 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic log.debug("Update geofence event completed."); } try { - eventConfigService.createEventOperationTask(OperationMgtConstants.OperationCodes.EVENT_REVOKE, + eventConfigService.createEventOperationTask(OperationMgtConstants.OperationCodes.EVENT_UPDATE, DeviceManagementConstants.EventServices.GEOFENCE, new GeoFenceEventMeta(geofenceData), tenantId, geofenceData.getGroupIds()); } catch (EventConfigurationException e) { - String msg = "Failed while creating EVENT_REVOKE operation creation task entry while updating geo fence " + String msg = "Failed while creating EVENT_UPDATE operation creation task entry while updating geo fence " + geofenceData.getFenceName() + " of the tenant " + tenantId; log.error(msg, e); throw new GeoLocationBasedServiceException(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/operation/mgt/OperationManagerImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/operation/mgt/OperationManagerImpl.java index c2d98755f0..2776e59aec 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -206,6 +206,7 @@ public class OperationManagerImpl implements OperationManager { String initiatedBy = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); if (initiatedBy == null && (isScheduledOperation || operation.getCode().equalsIgnoreCase(OperationMgtConstants.OperationCodes.EVENT_CONFIG) + || operation.getCode().equalsIgnoreCase(OperationMgtConstants.OperationCodes.EVENT_UPDATE) || operation.getCode().equalsIgnoreCase(OperationMgtConstants.OperationCodes.EVENT_REVOKE))) { operation.setInitiatedBy(SYSTEM); } else if (StringUtils.isEmpty(operation.getInitiatedBy())) { @@ -1460,6 +1461,7 @@ public class OperationManagerImpl implements OperationManager { case DeviceManagementConstants.AuthorizationSkippedOperationCodes.POLICY_OPERATION_CODE: case DeviceManagementConstants.AuthorizationSkippedOperationCodes.EVENT_CONFIG_OPERATION_CODE: case DeviceManagementConstants.AuthorizationSkippedOperationCodes.EVENT_REVOKE_OPERATION_CODE: + case DeviceManagementConstants.AuthorizationSkippedOperationCodes.EVENT_UPDATE_OPERATION_CODE: case DeviceManagementConstants.AuthorizationSkippedOperationCodes.POLICY_REVOKE_OPERATION_CODE: case DeviceManagementConstants.AuthorizationSkippedOperationCodes.MONITOR_OPERATION_CODE: status = true; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/operation/mgt/OperationMgtConstants.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/operation/mgt/OperationMgtConstants.java index 920da1526f..f0bfa8a330 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/operation/mgt/OperationMgtConstants.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/operation/mgt/OperationMgtConstants.java @@ -26,6 +26,7 @@ public class OperationMgtConstants { public static final String POLICY_REVOKE = "POLICY_REVOKE"; public static final String EVENT_CONFIG = "EVENT_CONFIG"; + public static final String EVENT_UPDATE = "EVENT_UPDATE"; public static final String EVENT_REVOKE = "EVENT_REVOKE"; } } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java index ddb77a24a4..8322905a5e 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java @@ -17,7 +17,6 @@ */ package io.entgra.device.mgt.core.device.mgt.core.operation.mgt.dao.impl; -import io.entgra.device.mgt.core.device.mgt.common.MDMAppConstants; import io.entgra.device.mgt.core.device.mgt.core.dto.operation.mgt.ProfileOperation; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -44,8 +43,6 @@ import io.entgra.device.mgt.core.device.mgt.core.operation.mgt.dao.util.Operatio import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; -import java.io.ObjectInputStream; -import java.io.ByteArrayInputStream; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -59,7 +56,6 @@ import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; -import java.util.Properties; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -575,14 +571,12 @@ public class GenericOperationDAOImpl implements OperationDAO { Connection conn = OperationManagementDAOFactory.getConnection(); String sql = "SELECT eom.ENROLMENT_ID, eom.OPERATION_ID, eom.ID AS EOM_MAPPING_ID, " - + "dor.ID AS OP_RES_ID, de.DEVICE_ID, d.DEVICE_IDENTIFICATION, d.DEVICE_TYPE_ID, " - + "dt.NAME AS DEVICE_TYPE_NAME, eom.STATUS, eom.CREATED_TIMESTAMP, " + + "dor.ID AS OP_RES_ID, de.DEVICE_ID, de.DEVICE_IDENTIFICATION, de.DEVICE_TYPE, " + + "eom.STATUS, eom.CREATED_TIMESTAMP, " + "eom.UPDATED_TIMESTAMP, op.OPERATION_CODE, op.TYPE AS OPERATION_TYPE, " + "dor.OPERATION_RESPONSE, op.INITIATED_BY, dor.RECEIVED_TIMESTAMP, dor.IS_LARGE_RESPONSE FROM " + "DM_ENROLMENT_OP_MAPPING eom INNER JOIN DM_OPERATION op " - + "ON op.ID=eom.OPERATION_ID INNER JOIN DM_ENROLMENT de " - + "ON de.ID=eom.ENROLMENT_ID INNER JOIN DM_DEVICE d ON d.ID=de.DEVICE_ID " - + "INNER JOIN DM_DEVICE_TYPE dt ON dt.ID=d.DEVICE_TYPE_ID " + + "ON op.ID=eom.OPERATION_ID INNER JOIN DM_ENROLMENT de ON de.ID=eom.ENROLMENT_ID " + "LEFT JOIN DM_DEVICE_OPERATION_RESPONSE dor ON dor.ENROLMENT_ID=de.id " + "AND dor.OPERATION_ID = eom.OPERATION_ID WHERE eom.OPERATION_ID " + "IN (SELECT * FROM TABLE(x INT = ?)) AND de.TENANT_ID = ?"; @@ -617,7 +611,7 @@ public class GenericOperationDAOImpl implements OperationDAO { DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); - deviceIdentifier.setType(rs.getString("DEVICE_TYPE_NAME")); + deviceIdentifier.setType(rs.getString("DEVICE_TYPE")); activityStatus.setDeviceIdentifier(deviceIdentifier); @@ -655,7 +649,7 @@ public class GenericOperationDAOImpl implements OperationDAO { DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); - deviceIdentifier.setType(rs.getString("DEVICE_TYPE_NAME")); + deviceIdentifier.setType(rs.getString("DEVICE_TYPE")); activityStatus.setDeviceIdentifier(deviceIdentifier); activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); @@ -715,8 +709,8 @@ public class GenericOperationDAOImpl implements OperationDAO { + "eom.OPERATION_ID, eom.ID AS EOM_MAPPING_ID, " + "dor.ID AS OP_RES_ID, " + "de.DEVICE_ID, " - + "d.DEVICE_IDENTIFICATION, " - + "d.DEVICE_TYPE_ID, dt.NAME AS DEVICE_TYPE_NAME, " + + "de.DEVICE_IDENTIFICATION, " + + "de.DEVICE_TYPE, " + "eom.STATUS, eom.CREATED_TIMESTAMP, " + "eom.UPDATED_TIMESTAMP, " + "op.OPERATION_CODE, " @@ -727,11 +721,9 @@ public class GenericOperationDAOImpl implements OperationDAO { + "op.INITIATED_BY FROM DM_ENROLMENT_OP_MAPPING AS eom " + "INNER JOIN DM_OPERATION AS op ON op.ID=eom.OPERATION_ID " + "INNER JOIN DM_ENROLMENT AS de ON de.ID=eom.ENROLMENT_ID " - + "INNER JOIN DM_DEVICE AS d ON d.ID=de.DEVICE_ID " - + "INNER JOIN DM_DEVICE_TYPE AS dt ON dt.ID=d.DEVICE_TYPE_ID " + "LEFT JOIN DM_DEVICE_OPERATION_RESPONSE AS dor ON dor.ENROLMENT_ID=de.id " + "AND dor.OPERATION_ID = eom.OPERATION_ID " - + "WHERE eom.OPERATION_ID = ? AND de.device_id = ? AND de.TENANT_ID = ?"; + + "WHERE eom.OPERATION_ID = ? AND de.DEVICE_ID = ? AND de.TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, operationId); @@ -756,7 +748,7 @@ public class GenericOperationDAOImpl implements OperationDAO { if (enrolmentId != rs.getInt("ENROLMENT_ID")) { activityStatus = new ActivityStatus(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(rs.getString("DEVICE_IDENTIFICATION"), - rs.getString("DEVICE_TYPE_NAME")); + rs.getString("DEVICE_TYPE")); activityStatus.setDeviceIdentifier(deviceIdentifier); activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); @@ -850,15 +842,13 @@ public class GenericOperationDAOImpl implements OperationDAO { " op.TYPE AS OPERATION_TYPE, " + " opm.STATUS, " + " en.DEVICE_ID, " + - " de.DEVICE_IDENTIFICATION, " + - " dt.NAME AS DEVICE_TYPE, " + + " en.DEVICE_IDENTIFICATION, " + + " en.DEVICE_TYPE, " + " de.TENANT_ID " + " FROM" + " DM_ENROLMENT_OP_MAPPING opm " + " INNER JOIN DM_OPERATION op ON opm.OPERATION_ID = op.ID " + " INNER JOIN DM_ENROLMENT en ON opm.ENROLMENT_ID = en.ID " + - " INNER JOIN DM_DEVICE de ON en.DEVICE_ID = de.ID " + - " INNER JOIN DM_DEVICE_TYPE dt ON dt.ID = de.DEVICE_TYPE_ID " + " WHERE " + " op.OPERATION_CODE = ? " + " AND de.TENANT_ID = ? " + diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/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 4488127481..63c5832457 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 @@ -661,6 +661,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 853926f8eb..910ef25a8b 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; @@ -329,7 +327,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv if ((updateStatus > 0) || EnrolmentInfo.Status.REMOVED. equals(existingEnrolmentInfo.getStatus())) { enrollment = enrollmentDAO - .addEnrollment(existingDevice.getId(), newEnrolmentInfo, tenantId); + .addEnrollment(existingDevice.getId(), deviceIdentifier, + newEnrolmentInfo, tenantId); if (enrollment == null) { DeviceManagementDAOFactory.rollbackTransaction(); throw new DeviceManagementException( @@ -383,7 +382,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv if (type != null) { int deviceId = deviceDAO.addDevice(type.getId(), device, tenantId); device.setId(deviceId); - enrollment = enrollmentDAO.addEnrollment(deviceId, device.getEnrolmentInfo(), tenantId); + enrollment = enrollmentDAO.addEnrollment(deviceId, deviceIdentifier, device.getEnrolmentInfo(), tenantId); if (enrollment == null) { DeviceManagementDAOFactory.rollbackTransaction(); throw new DeviceManagementException( @@ -459,6 +458,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) { @@ -497,10 +521,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 ) { @@ -508,7 +533,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(); @@ -623,20 +656,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() + @@ -1115,7 +1155,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv List deviceStatus; for (Device device : allDevices) { long dateDiff = 0; - deviceStatus = getDeviceStatusHistory(device, null, endDate, true); + deviceStatus = getDeviceStatusHistoryInsideTransaction(device, null, endDate, true); if (device.getEnrolmentInfo().getDateOfEnrolment() < startDate.getTime()) { if (!deviceStatus.isEmpty() && (String.valueOf(deviceStatus.get(0).getStatus()).equals("REMOVED") || String.valueOf(deviceStatus.get(0).getStatus()).equals("DELETED"))) { @@ -2170,23 +2210,52 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } } - @Override - public List getDeviceStatusHistory(Device device, Date fromDate, Date toDate, boolean billingStatus) throws DeviceManagementException { + /* + This is just to avoid breaking the billing functionality as it required to call getDeviceStatusHistory method + without transaction handling. + */ + private List getDeviceStatusHistoryInsideTransaction( + Device device, Date fromDate, Date toDate, boolean billingStatus) + throws DeviceManagementException { if (log.isDebugEnabled()) { log.debug("get status history of device: " + device.getDeviceIdentifier()); } try { + DeviceManagementDAOFactory.getConnection(); int tenantId = this.getTenantId(); return deviceStatusDAO.getStatus(device.getId(), tenantId, fromDate, toDate, billingStatus); } catch (DeviceManagementDAOException e) { - DeviceManagementDAOFactory.rollbackTransaction(); - String msg = "Error occurred while retrieving status history"; + String msg = "Error occurred in retrieving status history for device :" + device.getDeviceIdentifier(); log.error(msg, e); throw new DeviceManagementException(msg, e); - } catch (Exception e) { + } catch (SQLException e) { + String msg = "Error occurred while opening a connection to the data source"; + log.info(msg, e); + throw new DeviceManagementException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Override + public List getDeviceStatusHistory(Device device, Date fromDate, Date toDate, boolean billingStatus) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("get status history of device: " + device.getDeviceIdentifier()); + } + try { + DeviceManagementDAOFactory.openConnection(); + int tenantId = this.getTenantId(); + return deviceStatusDAO.getStatus(device.getId(), tenantId, fromDate, toDate, billingStatus); + } catch (DeviceManagementDAOException e) { String msg = "Error occurred in retrieving status history for device :" + device.getDeviceIdentifier(); log.error(msg, e); throw new DeviceManagementException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while opening a connection to the data source"; + log.info(msg, e); + throw new DeviceManagementException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); } } @@ -4463,15 +4532,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 f55bb1a0e2..5a677ab779 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 @@ -22,6 +22,7 @@ import io.entgra.device.mgt.core.device.mgt.common.Device; import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier; import io.entgra.device.mgt.core.device.mgt.common.EnrolmentInfo.Status; import io.entgra.device.mgt.core.device.mgt.common.exceptions.TransactionManagementException; +import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest; import io.entgra.device.mgt.core.device.mgt.core.TestUtils; import io.entgra.device.mgt.core.device.mgt.core.common.BaseDeviceManagementTest; import io.entgra.device.mgt.core.device.mgt.core.common.TestDataHolder; @@ -36,6 +37,8 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.Collections; +import java.util.List; public class DevicePersistTests extends BaseDeviceManagementTest { @@ -227,4 +230,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(); + } + } + } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/dao/DeviceStatusPersistenceTests.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/dao/DeviceStatusPersistenceTests.java index 47ef0237b7..d26e2e292b 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/dao/DeviceStatusPersistenceTests.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/dao/DeviceStatusPersistenceTests.java @@ -21,6 +21,7 @@ import io.entgra.device.mgt.core.device.mgt.common.Device; import io.entgra.device.mgt.core.device.mgt.common.EnrolmentInfo; import io.entgra.device.mgt.core.device.mgt.common.type.mgt.DeviceStatus; import io.entgra.device.mgt.core.device.mgt.core.common.BaseDeviceManagementTest; +import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier; import io.entgra.device.mgt.core.device.mgt.core.common.TestDataHolder; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -29,7 +30,12 @@ import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import java.sql.SQLException; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.TreeSet; import java.util.stream.Stream; import static io.entgra.device.mgt.core.device.mgt.common.EnrolmentInfo.Status.*; @@ -214,7 +220,8 @@ public class DeviceStatusPersistenceTests extends BaseDeviceManagementTest { EnrolmentInfo source = new EnrolmentInfo(owner, EnrolmentInfo.OwnerShip.BYOD, initialStatus); try { DeviceManagementDAOFactory.openConnection(); - EnrolmentInfo config = enrollmentDAO.addEnrollment(deviceId, source, tenantId); + EnrolmentInfo config = enrollmentDAO.addEnrollment(deviceId, + new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()), source, tenantId); device.setEnrolmentInfo(config); return config.getId(); } catch (DeviceManagementDAOException | SQLException e) { diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/dao/EnrolmentPersistenceTests.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/dao/EnrolmentPersistenceTests.java index 98834416ca..c693682ddf 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/dao/EnrolmentPersistenceTests.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/dao/EnrolmentPersistenceTests.java @@ -18,6 +18,7 @@ package io.entgra.device.mgt.core.device.mgt.core.dao; import io.entgra.device.mgt.core.device.mgt.common.Device; +import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier; import io.entgra.device.mgt.core.device.mgt.common.EnrolmentInfo; import io.entgra.device.mgt.core.device.mgt.core.common.BaseDeviceManagementTest; import io.entgra.device.mgt.core.device.mgt.core.common.TestDataHolder; @@ -58,7 +59,8 @@ public class EnrolmentPersistenceTests extends BaseDeviceManagementTest { DeviceDAO deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); deviceId = deviceDAO.addDevice(TestDataHolder.initialTestDeviceType.getId(), device, TestDataHolder.SUPER_TENANT_ID); device.setId(deviceId); - enrollmentDAO.addEnrollment(deviceId, source, TestDataHolder.SUPER_TENANT_ID); + enrollmentDAO.addEnrollment(deviceId, new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()), + source, TestDataHolder.SUPER_TENANT_ID); } catch (DeviceManagementDAOException | SQLException e) { log.error("Error occurred while adding enrollment", e); } finally { diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/sql/h2.sql b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/sql/h2.sql index 9aefaa3229..92d95fb8fc 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/sql/h2.sql +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/sql/h2.sql @@ -149,6 +149,8 @@ CREATE TABLE IF NOT EXISTS DM_OPERATION ( CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( ID INTEGER AUTO_INCREMENT NOT NULL, DEVICE_ID INTEGER NOT NULL, + DEVICE_TYPE VARCHAR(300) NOT NULL, + DEVICE_IDENTIFICATION VARCHAR(300) NOT NULL, OWNER VARCHAR(50) NOT NULL, OWNERSHIP VARCHAR(45) DEFAULT NULL, STATUS VARCHAR(50) NULL, diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.extensions/src/test/resources/sql-files/h2.sql b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.extensions/src/test/resources/sql-files/h2.sql index 4070434f9e..2d6cea544e 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.extensions/src/test/resources/sql-files/h2.sql +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.extensions/src/test/resources/sql-files/h2.sql @@ -79,6 +79,8 @@ CREATE TABLE IF NOT EXISTS DM_OPERATION ( CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( ID INTEGER AUTO_INCREMENT NOT NULL, DEVICE_ID INTEGER NOT NULL, + DEVICE_TYPE VARCHAR(300) NOT NULL, + DEVICE_IDENTIFICATION VARCHAR(300) NOT NULL, OWNER VARCHAR(50) NOT NULL, OWNERSHIP VARCHAR(45) DEFAULT NULL, STATUS VARCHAR(50) NULL, diff --git a/components/heartbeat-management/io.entgra.device.mgt.core.server.bootup.heartbeat.beacon/src/main/java/io/entgra/device/mgt/core/server/bootup/heartbeat/beacon/dao/HeartBeatBeaconDAOFactory.java b/components/heartbeat-management/io.entgra.device.mgt.core.server.bootup.heartbeat.beacon/src/main/java/io/entgra/device/mgt/core/server/bootup/heartbeat/beacon/dao/HeartBeatBeaconDAOFactory.java index 91ff56e949..b58ec1bde8 100644 --- a/components/heartbeat-management/io.entgra.device.mgt.core.server.bootup.heartbeat.beacon/src/main/java/io/entgra/device/mgt/core/server/bootup/heartbeat/beacon/dao/HeartBeatBeaconDAOFactory.java +++ b/components/heartbeat-management/io.entgra.device.mgt.core.server.bootup.heartbeat.beacon/src/main/java/io/entgra/device/mgt/core/server/bootup/heartbeat/beacon/dao/HeartBeatBeaconDAOFactory.java @@ -40,7 +40,7 @@ public class HeartBeatBeaconDAOFactory { private static final Log log = LogFactory.getLog(HeartBeatBeaconDAOFactory.class); private static DataSource dataSource; - private static ThreadLocal currentConnection = new ThreadLocal<>(); + private static final ThreadLocal currentConnection = new ThreadLocal<>(); /** * Get instance of GroupDAO @@ -62,7 +62,6 @@ public class HeartBeatBeaconDAOFactory { /** * Begin transaction with datasource for write data * - * @throws TransactionManagementException */ public static void beginTransaction() throws TransactionManagementException { Connection conn = currentConnection.get(); @@ -83,7 +82,6 @@ public class HeartBeatBeaconDAOFactory { /** * Open connection to the datasource for read data * - * @throws SQLException */ public static void openConnection() throws SQLException { Connection conn = currentConnection.get(); @@ -100,7 +98,6 @@ public class HeartBeatBeaconDAOFactory { * Get current connection to datasource * * @return current connection - * @throws SQLException */ public static Connection getConnection() throws SQLException { Connection conn = currentConnection.get(); @@ -187,7 +184,7 @@ public class HeartBeatBeaconDAOFactory { List jndiPropertyList = jndiConfig.getJndiProperties(); if (jndiPropertyList != null) { - Hashtable jndiProperties = new Hashtable(); + Hashtable jndiProperties = new Hashtable<>(); for (JNDILookupDefinition.JNDIProperty prop : jndiPropertyList) { jndiProperties.put(prop.getName(), prop.getValue()); } diff --git a/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/main/java/io/entgra/device/mgt/core/operation/template/cache/OperationTemplateCacheLoader.java b/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/main/java/io/entgra/device/mgt/core/operation/template/cache/OperationTemplateCacheLoader.java index e92ffbadcc..4b9316fad9 100644 --- a/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/main/java/io/entgra/device/mgt/core/operation/template/cache/OperationTemplateCacheLoader.java +++ b/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/main/java/io/entgra/device/mgt/core/operation/template/cache/OperationTemplateCacheLoader.java @@ -59,9 +59,8 @@ public class OperationTemplateCacheLoader extends CacheLoader deviceSubTypeCache = CacheBuilder.newBuilder() .expireAfterWrite(15, TimeUnit.MINUTES) @@ -143,7 +145,7 @@ public class DeviceSubTypeServiceImpl implements DeviceSubTypeService { } @Override - public DeviceSubType getDeviceSubType(String subTypeId, int tenantId, String deviceType) + public synchronized DeviceSubType getDeviceSubType(String subTypeId, int tenantId, String deviceType) throws SubTypeMgtPluginException { try { DeviceSubTypeCacheKey key = DeviceSubTypeMgtUtil.getDeviceSubTypeCacheKey(tenantId, subTypeId, deviceType); diff --git a/components/subtype-mgt/io.entgra.device.mgt.core.subtype.mgt/src/test/resources/carbon-home/dbscripts/dm-db-h2.sql b/components/subtype-mgt/io.entgra.device.mgt.core.subtype.mgt/src/test/resources/carbon-home/dbscripts/dm-db-h2.sql index 929eb1738a..92151968d3 100644 --- a/components/subtype-mgt/io.entgra.device.mgt.core.subtype.mgt/src/test/resources/carbon-home/dbscripts/dm-db-h2.sql +++ b/components/subtype-mgt/io.entgra.device.mgt.core.subtype.mgt/src/test/resources/carbon-home/dbscripts/dm-db-h2.sql @@ -97,6 +97,8 @@ CREATE TABLE IF NOT EXISTS DM_OPERATION ( CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( ID INTEGER AUTO_INCREMENT NOT NULL, DEVICE_ID INTEGER NOT NULL, + DEVICE_TYPE VARCHAR(300) NOT NULL, + DEVICE_IDENTIFICATION VARCHAR(300) NOT NULL, OWNER VARCHAR(255) NOT NULL, OWNERSHIP VARCHAR(45) DEFAULT NULL, STATUS VARCHAR(50) NULL, diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/conf/cdm-config.xml b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/conf/cdm-config.xml index a9214f24a8..f4b6d5266b 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/conf/cdm-config.xml +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/conf/cdm-config.xml @@ -295,6 +295,14 @@ Internal/devicemgt-user + + /permission/admin/device-mgt/devices/any-group/permitted-actions-under-owning-group + + Apply permitted actions on any group + dm:group:any:permitted + Internal/devicemgt-user + + diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/conf_templates/templates/repository/conf/cdm-config.xml.j2 b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/conf_templates/templates/repository/conf/cdm-config.xml.j2 index 259a12301a..59e026f679 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/conf_templates/templates/repository/conf/cdm-config.xml.j2 +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/conf_templates/templates/repository/conf/cdm-config.xml.j2 @@ -469,6 +469,14 @@ Internal/devicemgt-user + + /permission/admin/device-mgt/devices/any-group/permitted-actions-under-owning-group + + Apply permitted actions on any group + dm:group:any:permitted + Internal/devicemgt-user + + diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql index 8b2858ca80..af229599c6 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -100,6 +100,8 @@ CREATE TABLE IF NOT EXISTS DM_OPERATION ( CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( ID INTEGER AUTO_INCREMENT NOT NULL, DEVICE_ID INTEGER NOT NULL, + DEVICE_TYPE VARCHAR(300) NOT NULL, + DEVICE_IDENTIFICATION VARCHAR(300) NOT NULL, OWNER VARCHAR(255) NOT NULL, OWNERSHIP VARCHAR(45) DEFAULT NULL, STATUS VARCHAR(50) NULL, diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql index 6c7aa649fd..dc05a39464 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql @@ -142,6 +142,8 @@ IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[D CREATE TABLE DM_ENROLMENT ( ID INTEGER IDENTITY(1,1) NOT NULL, DEVICE_ID INTEGER NOT NULL, + DEVICE_TYPE VARCHAR(300) NOT NULL, + DEVICE_IDENTIFICATION VARCHAR(300) NOT NULL, OWNER VARCHAR(255) NOT NULL, OWNERSHIP VARCHAR(45) DEFAULT NULL, STATUS VARCHAR(50) NULL, diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql index 065862a36b..cd28fb74f1 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql @@ -52,8 +52,8 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE ( ID INTEGER AUTO_INCREMENT NOT NULL, DESCRIPTION TEXT DEFAULT NULL, NAME VARCHAR(100) DEFAULT NULL, - DEVICE_TYPE_ID INT(11) DEFAULT NULL, - DEVICE_IDENTIFICATION VARCHAR(300) DEFAULT NULL, + DEVICE_TYPE_ID INT(11) NOT NULL, + DEVICE_IDENTIFICATION VARCHAR(300) NOT NULL, LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL, TENANT_ID INTEGER DEFAULT 0, PRIMARY KEY (ID), @@ -63,6 +63,8 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE ( CREATE INDEX IDX_DM_DEVICE ON DM_DEVICE(TENANT_ID, DEVICE_TYPE_ID); CREATE INDEX IDX_DM_DEVICE_TYPE_ID_DEVICE_IDENTIFICATION ON DM_DEVICE(TENANT_ID, DEVICE_TYPE_ID,DEVICE_IDENTIFICATION); +CREATE INDEX IDX_DM_DEVICE_DEVICE_IDENTIFICATION ON DM_DEVICE(DEVICE_IDENTIFICATION); +CREATE INDEX IDX_DM_DEVICE_LAST_UPDATED_TIMESTAMP ON DM_DEVICE(LAST_UPDATED_TIMESTAMP); CREATE TABLE IF NOT EXISTS DM_DEVICE_PROPERTIES ( DEVICE_TYPE_NAME VARCHAR(300) NOT NULL, @@ -119,6 +121,8 @@ CREATE INDEX IDX_OP_INITIATED_BY ON DM_OPERATION (INITIATED_BY ASC); CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( ID INTEGER AUTO_INCREMENT NOT NULL, DEVICE_ID INTEGER NOT NULL, + DEVICE_TYPE VARCHAR(300) NOT NULL, + DEVICE_IDENTIFICATION VARCHAR(300) NOT NULL, OWNER VARCHAR(255) NOT NULL, OWNERSHIP VARCHAR(45) DEFAULT NULL, STATUS VARCHAR(50) NULL, @@ -130,6 +134,14 @@ CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( CONSTRAINT FK_DM_DEVICE_ENROLMENT FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION )ENGINE = InnoDB; +CREATE INDEX IDX_ENROLMENT_DATE_OF_LAST_UPDATE ON DM_ENROLMENT(DATE_OF_LAST_UPDATE); +CREATE INDEX IDX_ENROLMENT_DEVICE_IDENTIFICATION ON DM_ENROLMENT(DEVICE_IDENTIFICATION); +CREATE INDEX IDX_ENROLMENT_DEVICE_TYPE ON DM_ENROLMENT(DEVICE_TYPE); +CREATE INDEX IDX_ENROLMENT_FK_DEVICE_ID ON DM_ENROLMENT(DEVICE_ID); +CREATE INDEX IDX_ENROLMENT_STATUS ON DM_ENROLMENT(STATUS); +CREATE INDEX IDX_ENROLMENT_TENANT_ID ON DM_ENROLMENT(TENANT_ID); + + CREATE TABLE IF NOT EXISTS DM_DEVICE_STATUS ( ID INTEGER AUTO_INCREMENT NOT NULL, ENROLMENT_ID INTEGER NOT NULL, @@ -143,9 +155,6 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_STATUS ( CONSTRAINT FK_DM_DEVICE_STATUS_ENROLMENT FOREIGN KEY (ENROLMENT_ID) REFERENCES DM_ENROLMENT (ID) ON DELETE CASCADE ON UPDATE CASCADE )ENGINE = InnoDB; -CREATE INDEX IDX_ENROLMENT_FK_DEVICE_ID ON DM_ENROLMENT(DEVICE_ID); -CREATE INDEX IDX_ENROLMENT_DEVICE_ID_TENANT_ID ON DM_ENROLMENT(DEVICE_ID, TENANT_ID); -CREATE INDEX IDX_ENROLMENT_DEVICE_ID_TENANT_ID_STATUS ON DM_ENROLMENT(DEVICE_ID, TENANT_ID, STATUS); CREATE TABLE IF NOT EXISTS DM_ENROLMENT_OP_MAPPING ( ID INTEGER AUTO_INCREMENT NOT NULL, diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql index eccc831141..e500c55046 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql @@ -205,6 +205,8 @@ WHEN (NEW.ID IS NULL) CREATE TABLE DM_ENROLMENT ( ID NUMBER(10) NOT NULL, DEVICE_ID NUMBER(10) NOT NULL, + DEVICE_TYPE VARCHAR2(300) NOT NULL, + DEVICE_IDENTIFICATION VARCHAR2(300) NOT NULL, OWNER VARCHAR2(255) NOT NULL, OWNERSHIP VARCHAR2(45) DEFAULT NULL, STATUS VARCHAR2(50) NULL, diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql index 371baa0763..c0368f5826 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql @@ -114,6 +114,8 @@ CREATE SEQUENCE DM_ENROLMENT_seq; CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( ID INTEGER DEFAULT NEXTVAL ('DM_ENROLMENT_seq') NOT NULL, DEVICE_ID INTEGER NOT NULL, + DEVICE_TYPE VARCHAR(300) NOT NULL, + DEVICE_IDENTIFICATION VARCHAR(300) NOT NULL, OWNER VARCHAR(50) NOT NULL, OWNERSHIP VARCHAR(45) DEFAULT NULL, STATUS VARCHAR(50) NULL,