From f9660de3347954d58f0bbecec249cf1e2bb53f40 Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Sun, 13 Oct 2019 11:58:58 +0530 Subject: [PATCH] Minor fixes and improvement for permanent delete devices --- .../carbon/device/mgt/core/dao/DeviceDAO.java | 3 +- .../DeviceManagementProviderServiceImpl.java | 53 ++++++++++--------- .../cdmf.page.devices/public/js/listing.js | 4 +- 3 files changed, 33 insertions(+), 27 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java index 5631f8d6f95..26e5571790c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java @@ -533,5 +533,6 @@ public interface DeviceDAO { * @param enrollmentIds list of enrollment ids. * @throws DeviceManagementDAOException when no enrolments are found for the given device. */ - void deleteDevices(List deviceIdentifiers, List deviceIds, List enrollmentIds) throws DeviceManagementDAOException; + void deleteDevices(List deviceIdentifiers, List deviceIds, List enrollmentIds) + throws DeviceManagementDAOException; } 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 e32da792b4d..ee2a2ec4838 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 @@ -522,43 +522,39 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } @Override - public boolean deleteDevices(List deviceIdentifiers) throws DeviceManagementException, InvalidDeviceException { + public boolean deleteDevices(List deviceIdentifiers) throws DeviceManagementException, + InvalidDeviceException { + if (deviceIdentifiers == null || deviceIdentifiers.isEmpty()) { + String msg = "Required values of device identifiers are not set to permanently delete device/s."; + log.error(msg); + throw new InvalidDeviceException(msg); + } HashSet deviceIds = new HashSet<>(); List enrollmentIds = new ArrayList<>(); + List validDeviceIdentifiers = new ArrayList<>(); Map> deviceIdentifierMap = new HashMap<>(); Map deviceManagerMap = new HashMap<>(); List deviceCacheKeyList = new ArrayList<>(); int tenantId = this.getTenantId(); - List existingDevices; try { DeviceManagementDAOFactory.beginTransaction(); - existingDevices = deviceDAO.getDevicesByIdentifiers(deviceIdentifiers, tenantId); - for (Device device : existingDevices) { - deviceIdentifiers.remove(device.getDeviceIdentifier()); - } - if (!deviceIdentifiers.isEmpty()) { - String msg = - "Couldn't find device ids for all the requested device identifiers. " + - "Therefore payload should contain device identifiers which are not in the system. " + - "Invalid device identifiers are " + deviceIdentifiers.toString(); - log.error(msg); - DeviceManagementDAOFactory.rollbackTransaction(); - throw new InvalidDeviceException(msg); - } + List existingDevices = deviceDAO.getDevicesByIdentifiers(deviceIdentifiers, tenantId); + DeviceCacheKey deviceCacheKey; for (Device device : existingDevices) { - if (!device.getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.REMOVED)) { + if (!EnrolmentInfo.Status.REMOVED.equals(device.getEnrolmentInfo().getStatus())) { + DeviceManagementDAOFactory.rollbackTransaction(); String msg = "Device " + device.getDeviceIdentifier() + " of type " + device.getType() - + " is not dis-enrolled to permanently delete the device"; + + " is not dis-enrolled to permanently delete the device"; log.error(msg); - DeviceManagementDAOFactory.rollbackTransaction(); throw new InvalidDeviceException(msg); } - DeviceCacheKey deviceCacheKey = new DeviceCacheKey(); + deviceCacheKey = new DeviceCacheKey(); deviceCacheKey.setDeviceId(device.getDeviceIdentifier()); deviceCacheKey.setDeviceType(device.getType()); deviceCacheKey.setTenantId(tenantId); deviceCacheKeyList.add(deviceCacheKey); deviceIds.add(device.getId()); + validDeviceIdentifiers.add(device.getDeviceIdentifier()); enrollmentIds.add(device.getEnrolmentInfo().getId()); if (deviceIdentifierMap.containsKey(device.getType())) { deviceIdentifierMap.get(device.getType()).add(device.getDeviceIdentifier()); @@ -567,17 +563,23 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv new ArrayList<>(Arrays.asList(device.getDeviceIdentifier()))); DeviceManager deviceManager = this.getDeviceManager(device.getType()); if (deviceManager == null) { - if (log.isDebugEnabled()) { - log.debug("Device Manager associated with the device type '" - + device.getType() + "' is null. Therefore, not attempting method 'deleteDevice'"); - } + log.error("Device Manager associated with the device type '" +device.getType() + + "' is null. Therefore, not attempting method 'deleteDevice'"); return false; } deviceManagerMap.put(device.getType(), deviceManager); } } + if (deviceIds.isEmpty()) { + String msg = "No device IDs found for the device identifiers '" + deviceIdentifiers + "'"; + log.error(msg); + throw new InvalidDeviceException(msg); + } + if (log.isDebugEnabled()) { + log.debug("Permanently deleting the details of devices : " + validDeviceIdentifiers); + } //deleting device from the core - deviceDAO.deleteDevices(deviceIdentifiers, new ArrayList<>(deviceIds), enrollmentIds); + deviceDAO.deleteDevices(validDeviceIdentifiers, new ArrayList<>(deviceIds), enrollmentIds); for (Map.Entry entry : deviceManagerMap.entrySet()) { try { // deleting device from the plugin level @@ -594,6 +596,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } DeviceManagementDAOFactory.commitTransaction(); this.removeDevicesFromCache(deviceCacheKeyList); + if (log.isDebugEnabled()) { + log.debug("Successfully permanently deleted the details of devices : " + validDeviceIdentifiers); + } return true; } catch (TransactionManagementException e) { String msg = "Error occurred while initiating transaction"; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/public/js/listing.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/public/js/listing.js index 498b65aaf0e..429682a2098 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/public/js/listing.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/public/js/listing.js @@ -900,6 +900,8 @@ function attachDeviceEvents() { var deviceId = $(this).data("deviceid"); if (deviceId) { deviceIdentifiers = [deviceId]; + $(modalPopupContent).html($('#delete-device-modal-content').html()); + showPopup(); } else { var selectedDevices = getSelectedDevices(); if (selectedDevices.length == 0) { @@ -932,8 +934,6 @@ function attachDeviceEvents() { } } - $(modalPopupContent).html($('#delete-device-modal-content').html()); - showPopup(); $("a#delete-device-yes-link").click(function () { deleteDevices(deviceIdentifiers);