From 410e04c0159dc4fa60494475080a8d9323213fce Mon Sep 17 00:00:00 2001 From: prathabanKavin Date: Tue, 7 Nov 2023 18:38:15 +0530 Subject: [PATCH 1/2] Fix device permant delete with mssql --- .../impl/device/SQLServerDeviceDAOImpl.java | 36 ++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) 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 5628b77a2d..97fcfb6af1 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 @@ -18,13 +18,10 @@ package io.entgra.device.mgt.core.device.mgt.core.dao.impl.device; +import io.entgra.device.mgt.core.device.mgt.common.*; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import io.entgra.device.mgt.core.device.mgt.common.Count; -import io.entgra.device.mgt.core.device.mgt.common.Device; -import io.entgra.device.mgt.core.device.mgt.common.DeviceBilling; -import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest; import io.entgra.device.mgt.core.device.mgt.common.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; @@ -1474,4 +1471,35 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl { return geoClusters; } */ + + @Override + public void refactorDeviceStatus(Connection conn, List validDevices) throws DeviceManagementDAOException { + String updateQuery = "UPDATE DM_DEVICE_STATUS SET STATUS = ? WHERE ID = ?"; + String selectLastMatchingRecordQuery = "SELECT TOP 1 ID FROM DM_DEVICE_STATUS WHERE ENROLMENT_ID = ? AND DEVICE_ID = ? ORDER BY ID DESC"; + + 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); + } + } } From 27d582456b17fc7fa44b7a03d4ba5036c8e616f9 Mon Sep 17 00:00:00 2001 From: prathabanKavin Date: Tue, 7 Nov 2023 18:46:28 +0530 Subject: [PATCH 2/2] Add single class imports --- .../mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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 97fcfb6af1..a09185533c 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 @@ -18,7 +18,10 @@ 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.EnrolmentInfo; +import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory;