From becaf355461ebc742e12354898edd420e0f8b512 Mon Sep 17 00:00:00 2001 From: Amanda Randombage <74random.amanda@gmail.com> Date: Fri, 11 Oct 2019 08:59:31 +0000 Subject: [PATCH] Permanent delete DAO fix --- .../core/dao/impl/AbstractDeviceDAOImpl.java | 174 +++++++++++++----- .../DeviceManagementProviderServiceImpl.java | 4 +- .../template/dao/DeviceTypePluginDAOImpl.java | 13 +- .../dao/PropertyBasedPluginDAOImpl.java | 13 +- 4 files changed, 142 insertions(+), 62 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index 55a7a345ee..5685e20362 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -1744,8 +1744,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } } } catch (SQLException e) { - String msg ="Error occurred while deleting the devices: " + deviceIdentifiers; - log.error(msg,e); + String msg = "Error occurred while deleting the devices: " + deviceIdentifiers; + log.error(msg, e); throw new DeviceManagementDAOException(msg, e); } } @@ -1759,9 +1759,14 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { private void removeDeviceDetail(Connection conn, List deviceIds) throws DeviceManagementDAOException { String sql = "DELETE FROM DM_DEVICE_DETAIL WHERE DEVICE_ID = ?"; try { - executeBatchOperation(conn, sql, deviceIds); + if (!executeBatchOperation(conn, sql, deviceIds)) { + String msg = "Failed to remove device details of devices with deviceIds : " + deviceIds + + " while executing batch operation"; + log.error(msg); + throw new DeviceManagementDAOException(msg); + } } catch (SQLException e) { - String msg = "Error occurred while removing device details."; + String msg = "SQL error occurred while removing device details of devices with deviceIds : " + deviceIds; log.error(msg, e); throw new DeviceManagementDAOException(msg, e); } @@ -1776,9 +1781,14 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { private void removeDeviceLocation(Connection conn, List deviceIds) throws DeviceManagementDAOException { String sql = "DELETE FROM DM_DEVICE_LOCATION WHERE DEVICE_ID = ?"; try { - executeBatchOperation(conn, sql, deviceIds); + if (!executeBatchOperation(conn, sql, deviceIds)) { + String msg = "Failed to remove locations of devices with deviceIds : " + deviceIds + + " while executing batch operation"; + log.error(msg); + throw new DeviceManagementDAOException(msg); + } } catch (SQLException e) { - String msg = "Error occurred while obtaining locations of devices."; + String msg = "SQL error occurred while removing locations of devices with deviceIds : " + deviceIds; log.error(msg, e); throw new DeviceManagementDAOException(msg, e); } @@ -1793,9 +1803,14 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { private void removeDeviceInfo(Connection conn, List deviceIds) throws DeviceManagementDAOException { String sql = "DELETE FROM DM_DEVICE_INFO WHERE DEVICE_ID = ?"; try { - executeBatchOperation(conn, sql, deviceIds); + if (!executeBatchOperation(conn, sql, deviceIds)) { + String msg = "Failed to remove device info of devices with deviceIds : " + deviceIds + + " while executing batch operation"; + log.error(msg); + throw new DeviceManagementDAOException(msg); + } } catch (SQLException e) { - String msg = "Error occurred while removing device info."; + String msg = "SQL error occurred while removing device info of devices with deviceIds : " + deviceIds; log.error(msg, e); throw new DeviceManagementDAOException(msg, e); } @@ -1810,9 +1825,14 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { private void removeDeviceNotification(Connection conn, List deviceIds) throws DeviceManagementDAOException { String sql = "DELETE FROM DM_NOTIFICATION WHERE DEVICE_ID = ?"; try { - executeBatchOperation(conn, sql, deviceIds); + if (!executeBatchOperation(conn, sql, deviceIds)) { + String msg = "Failed to remove device notifications of devices with deviceIds : " + deviceIds + + " while executing batch operation"; + log.error(msg); + throw new DeviceManagementDAOException(msg); + } } catch (SQLException e) { - String msg = "Error occurred while removing device notifications."; + String msg = "SQL error occurred while removing device notifications of devices with deviceIds : " + deviceIds; log.error(msg, e); throw new DeviceManagementDAOException(msg, e); } @@ -1829,9 +1849,15 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { throws DeviceManagementDAOException { String sql = "DELETE FROM DM_DEVICE_APPLICATION_MAPPING WHERE DEVICE_ID = ?"; try { - executeBatchOperation(conn, sql, deviceIds); + if (!executeBatchOperation(conn, sql, deviceIds)) { + String msg = "Failed to remove device application mapping of of devices with deviceIds : " + deviceIds + + " while executing batch operation"; + log.error(msg); + throw new DeviceManagementDAOException(msg); + } } catch (SQLException e) { - String msg = "Error occurred while removing device application mapping"; + String msg = "SQL error occurred while removing device application mapping of devices with deviceIds : " + + deviceIds; log.error(msg, e); throw new DeviceManagementDAOException(msg, e); } @@ -1847,9 +1873,14 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { throws DeviceManagementDAOException { String sql = "DELETE FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ?"; try { - executeBatchOperation(conn, sql, deviceIds); + if (!executeBatchOperation(conn, sql, deviceIds)) { + String msg = "Failed to remove policies applied on devices with deviceIds : " + deviceIds + + " while executing batch operation"; + log.error(msg); + throw new DeviceManagementDAOException(msg); + } } catch (SQLException e) { - String msg = "Error occurred while removing policies applied on devices"; + String msg = "SQL error occurred while removing policies applied on devices with deviceIds : " + deviceIds; log.error(msg, e); throw new DeviceManagementDAOException(msg, e); } @@ -1864,9 +1895,14 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { private void removeDevicePolicy(Connection conn, List deviceIds) throws DeviceManagementDAOException { String sql = "DELETE FROM DM_DEVICE_POLICY WHERE DEVICE_ID = ?"; try { - executeBatchOperation(conn, sql, deviceIds); + if (!executeBatchOperation(conn, sql, deviceIds)) { + String msg = "Failed to remove policies of devices with deviceIds : " + deviceIds + + " while executing batch operation"; + log.error(msg); + throw new DeviceManagementDAOException(msg); + } } catch (SQLException e) { - String msg = "Error occurred while removing policies of devices"; + String msg = "SQL error occurred while removing policies of devices with deviceIds : " + deviceIds; log.error(msg, e); throw new DeviceManagementDAOException(msg, e); } @@ -1882,9 +1918,15 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { throws DeviceManagementDAOException { String sql = "DELETE FROM DM_DEVICE_DETAIL WHERE ENROLMENT_ID = ?"; try { - executeBatchOperation(conn, sql, enrollmentIds); + if (!executeBatchOperation(conn, sql, enrollmentIds)) { + String msg = "Failed to remove enrollment details of devices with enrollmentIds : " + enrollmentIds + + " while executing batch operation"; + log.error(msg); + throw new DeviceManagementDAOException(msg); + } } catch (SQLException e) { - String msg = "Error occurred while removing enrollment details of devices"; + String msg = "SQL error occurred while removing enrollment details of devices with enrollmentIds : " + + enrollmentIds; log.error(msg, e); throw new DeviceManagementDAOException(msg, e); } @@ -1900,9 +1942,15 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { throws DeviceManagementDAOException { String sql = "DELETE FROM DM_DEVICE_LOCATION WHERE ENROLMENT_ID = ?"; try { - executeBatchOperation(conn, sql, enrollmentIds); + if (!executeBatchOperation(conn, sql, enrollmentIds)) { + String msg = "Failed to remove enrollment locations of devices with enrollmentIds : " + enrollmentIds + + " while executing batch operation"; + log.error(msg); + throw new DeviceManagementDAOException(msg); + } } catch (SQLException e) { - String msg = "Error occurred while removing enrollment locations of devices"; + String msg = "SQL error occurred while removing enrollment locations of devices with enrollmentIds : " + + enrollmentIds; log.error(msg, e); throw new DeviceManagementDAOException(msg, e); } @@ -1918,9 +1966,15 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { throws DeviceManagementDAOException { String sql = "DELETE FROM DM_DEVICE_INFO WHERE ENROLMENT_ID = ?"; try { - executeBatchOperation(conn, sql, enrollmentIds); + if (!executeBatchOperation(conn, sql, enrollmentIds)) { + String msg = "Failed to remove enrollment info of devices with enrollmentIds : " + enrollmentIds + + " while executing batch operation"; + log.error(msg); + throw new DeviceManagementDAOException(msg); + } } catch (SQLException e) { - String msg = "Error occurred while removing enrollment info of devices"; + String msg = "SQL error occurred while removing enrollment info of devices with enrollmentIds : " + + enrollmentIds; log.error(msg, e); throw new DeviceManagementDAOException(msg, e); } @@ -1936,9 +1990,15 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { throws DeviceManagementDAOException { String sql = "DELETE FROM DM_DEVICE_APPLICATION_MAPPING WHERE ENROLMENT_ID = ?"; try { - executeBatchOperation(conn, sql, enrollmentIds); + if (!executeBatchOperation(conn, sql, enrollmentIds)) { + String msg = "Failed to remove enrollment device application mapping of devices with enrollmentIds : " + + enrollmentIds + " while executing batch operation"; + log.error(msg); + throw new DeviceManagementDAOException(msg); + } } catch (SQLException e) { - String msg = "Error occurred while removing enrollment device application mapping"; + String msg = "SQL error occurred while removing enrollment device application mapping of devices with " + + "enrollmentIds : " + enrollmentIds; log.error(msg, e); throw new DeviceManagementDAOException(msg, e); } @@ -1954,9 +2014,15 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { throws DeviceManagementDAOException { String sql = "DELETE FROM DM_DEVICE_OPERATION_RESPONSE WHERE ENROLMENT_ID = ?"; try { - executeBatchOperation(conn, sql, enrollmentIds); + if (!executeBatchOperation(conn, sql, enrollmentIds)) { + String msg = "Failed to remove device operation response of devices with enrollmentIds : " + + enrollmentIds + " while executing batch operation"; + log.error(msg); + throw new DeviceManagementDAOException(msg); + } } catch (SQLException e) { - String msg = "Error occurred while removing device operation response"; + String msg = "SQL error occurred while removing device operation response of devices with enrollmentIds : " + + enrollmentIds; log.error(msg, e); throw new DeviceManagementDAOException(msg, e); } @@ -1972,9 +2038,15 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { throws DeviceManagementDAOException { String sql = "DELETE FROM DM_ENROLMENT_OP_MAPPING WHERE ENROLMENT_ID = ?"; try { - executeBatchOperation(conn, sql, enrollmentIds); + if (!executeBatchOperation(conn, sql, enrollmentIds)) { + String msg = "Failed to remove enrollment operation mapping of devices with enrollmentIds : " + + enrollmentIds + " while executing batch operation"; + log.error(msg); + throw new DeviceManagementDAOException(msg); + } } catch (SQLException e) { - String msg = "Error occurred while removing enrollment operation mapping"; + String msg = "SQL error occurred while removing enrollment operation mapping of devices with enrollmentIds :" + + enrollmentIds; log.error(msg, e); throw new DeviceManagementDAOException(msg, e); } @@ -1990,9 +2062,15 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { throws DeviceManagementDAOException { String sql = "DELETE FROM DM_ENROLMENT WHERE DEVICE_ID = ?"; try { - executeBatchOperation(conn, sql, enrollmentIds); + if (!executeBatchOperation(conn, sql, enrollmentIds)) { + String msg = "Failed to remove enrollments of devices with enrollmentIds : " + enrollmentIds + + " while executing batch operation"; + log.error(msg); + throw new DeviceManagementDAOException(msg); + } } catch (SQLException e) { - String msg = "Error occurred while removing enrollments of devices"; + String msg = "SQL error occurred while removing enrollments of devices with enrollmentIds : " + + enrollmentIds; log.error(msg, e); throw new DeviceManagementDAOException(msg, e); } @@ -2007,9 +2085,15 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { private void removeDeviceGroupMapping(Connection conn, List deviceIds) throws DeviceManagementDAOException { String sql = "DELETE FROM DM_DEVICE_GROUP_MAP WHERE DEVICE_ID = ?"; try { - executeBatchOperation(conn, sql, deviceIds); + if (!executeBatchOperation(conn, sql, deviceIds)) { + String msg = "Failed to remove device group mapping of devices with deviceIds : " + deviceIds + + " while executing batch operation"; + log.error(msg); + throw new DeviceManagementDAOException(msg); + } } catch (SQLException e) { - String msg = "Error occurred while removing device group mapping"; + String msg = "SQL error occurred while removing device group mapping of devices with deviceIds : " + + deviceIds; log.error(msg, e); throw new DeviceManagementDAOException(msg, e); } @@ -2024,9 +2108,13 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { private void removeDevice(Connection conn, List deviceIds) throws DeviceManagementDAOException { String sql = "DELETE FROM DM_DEVICE WHERE ID = ?"; try { - executeBatchOperation(conn, sql, deviceIds); + if (!executeBatchOperation(conn, sql, deviceIds)) { + String msg = "Failed to remove devices with deviceIds : " + deviceIds + " while executing batch operation"; + log.error(msg); + throw new DeviceManagementDAOException(msg); + } } catch (SQLException e) { - String msg = "Error occurred while removing devices."; + String msg = "SQL error occurred while removing devices with deviceIds : " + deviceIds; log.error(msg, e); throw new DeviceManagementDAOException(msg, e); } @@ -2036,12 +2124,15 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { * This method executes batch operations for a given list of primary keys * where the statement only has one param of type int, following the given pattern: * DELETE FROM TABLE WHERE ID = ? + * + * This method does not check if the number of rows affected by the executeBatch() method is 0 + * because there can be tables with no records to delete. * @param sql SQL statement * @param conn Connection object * @param identifiers list of device ids (primary keys) * @throws SQLException if deletion fails. */ - private void executeBatchOperation(Connection conn, String sql, List identifiers) throws SQLException { + private boolean executeBatchOperation(Connection conn, String sql, List identifiers) throws SQLException { try (PreparedStatement ps = conn.prepareStatement(sql)) { if (conn.getMetaData().supportsBatchUpdates()) { for (int identifier : identifiers) { @@ -2049,18 +2140,17 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { ps.addBatch(); } for (int i : ps.executeBatch()) { - if (i == 0 || i == Statement.SUCCESS_NO_INFO || i == Statement.EXECUTE_FAILED) { - break; + if (i == Statement.SUCCESS_NO_INFO || i == Statement.EXECUTE_FAILED) { + return false; } } } else { - for (int enrollmentId : identifiers) { - ps.setInt(1, enrollmentId); - if (ps.executeUpdate() == 0) { - break; - } + for (int identifier : identifiers) { + ps.setInt(1, identifier); + ps.executeUpdate(); } } } + return true; } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 3e452f2ee5..e32da792b4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -523,7 +523,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public boolean deleteDevices(List deviceIdentifiers) throws DeviceManagementException, InvalidDeviceException { - List deviceIds = new ArrayList<>(); + HashSet deviceIds = new HashSet<>(); List enrollmentIds = new ArrayList<>(); Map> deviceIdentifierMap = new HashMap<>(); Map deviceManagerMap = new HashMap<>(); @@ -577,7 +577,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } } //deleting device from the core - deviceDAO.deleteDevices(deviceIdentifiers, deviceIds, enrollmentIds); + deviceDAO.deleteDevices(deviceIdentifiers, new ArrayList<>(deviceIds), enrollmentIds); for (Map.Entry entry : deviceManagerMap.entrySet()) { try { // deleting device from the plugin level diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/DeviceTypePluginDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/DeviceTypePluginDAOImpl.java index e1427897d2..c7378f1631 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/DeviceTypePluginDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/DeviceTypePluginDAOImpl.java @@ -220,7 +220,6 @@ public class DeviceTypePluginDAOImpl implements PluginDAO { public boolean deleteDevices(List deviceIdentifiers) throws DeviceTypeMgtPluginException { try { Connection conn = deviceTypeDAOHandler.getConnection(); - boolean status = true; try (PreparedStatement ps = conn.prepareStatement(deleteDBQueryForDeleteDevice)) { if (conn.getMetaData().supportsBatchUpdates()) { for (String deviceId : deviceIdentifiers) { @@ -228,22 +227,18 @@ public class DeviceTypePluginDAOImpl implements PluginDAO { ps.addBatch(); } for (int i : ps.executeBatch()) { - if (i == 0 || i == Statement.SUCCESS_NO_INFO || i == Statement.EXECUTE_FAILED) { - status = false; - break; + if (i == Statement.SUCCESS_NO_INFO || i == Statement.EXECUTE_FAILED) { + return false; } } } else { for (String deviceId : deviceIdentifiers) { ps.setString(1, deviceId); - if (ps.executeUpdate() == 0) { - status = false; - break; - } + ps.executeUpdate(); } } } - return status; + return true; } catch (SQLException e) { String msg = "Error occurred while deleting the data in " + deviceDAODefinition.getDeviceTableName(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/PropertyBasedPluginDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/PropertyBasedPluginDAOImpl.java index 7cc32f6315..b60e70c4c2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/PropertyBasedPluginDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/PropertyBasedPluginDAOImpl.java @@ -222,7 +222,6 @@ public class PropertyBasedPluginDAOImpl implements PluginDAO { public boolean deleteDevices(List deviceIdentifiers) throws DeviceTypeMgtPluginException { try { Connection conn = deviceTypeDAOHandler.getConnection(); - boolean status = true; try (PreparedStatement ps = conn.prepareStatement("DELETE FROM DM_DEVICE_PROPERTIES WHERE DEVICE_IDENTIFICATION = ?")) { if (conn.getMetaData().supportsBatchUpdates()) { for (String deviceId : deviceIdentifiers) { @@ -230,22 +229,18 @@ public class PropertyBasedPluginDAOImpl implements PluginDAO { ps.addBatch(); } for (int i : ps.executeBatch()) { - if (i == 0 || i == Statement.SUCCESS_NO_INFO || i == Statement.EXECUTE_FAILED) { - status = false; - break; + if (i == Statement.SUCCESS_NO_INFO || i == Statement.EXECUTE_FAILED) { + return false; } } } else { for (String deviceId : deviceIdentifiers) { ps.setString(1, deviceId); - if (ps.executeUpdate() == 0) { - status = false; - break; - } + ps.executeUpdate(); } } } - return status; + return true; } catch (SQLException e) { String msg = "Error occurred while deleting the data of the devices: '" + deviceIdentifiers + "'of type: " + deviceType;