From 48ae5c58e9cfbc9bf26fa6a0d80d3dc55df0d063 Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Thu, 30 May 2019 18:41:23 +0530 Subject: [PATCH] Add deleteDevice service and impl --- .../DeviceManagementProviderService.java | 2 + .../DeviceManagementProviderServiceImpl.java | 61 +++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index c2c7ef4813..43ac2c8567 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -582,6 +582,8 @@ public interface DeviceManagementProviderService { boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException; + boolean deleteDevice(DeviceIdentifier deviceId) throws DeviceManagementException; + boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException; boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException; 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 151352c7b1..621cc966e3 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 @@ -512,6 +512,67 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return deviceManager.disenrollDevice(deviceId); } + @Override + public boolean deleteDevice(DeviceIdentifier deviceId) throws DeviceManagementException { + if (deviceId == null) { + String msg = "Required values are not set to permanently delete device"; + log.error(msg); + throw new DeviceManagementException(msg); + } + if (log.isDebugEnabled()) { + log.debug("Permanently deleting device: " + deviceId.getId() + " of type '" + deviceId.getType() + "'"); + } + DeviceManager deviceManager = this.getDeviceManager(deviceId.getType()); + if (deviceManager == null) { + if (log.isDebugEnabled()) { + log.debug("Device Manager associated with the device type '" + deviceId.getType() + "' is null. " + + "Therefore, not attempting method 'deleteDevice'"); + } + return false; + } + + int tenantId = this.getTenantId(); + + Device device = this.getDevice(deviceId, false); + if (device == null) { + if (log.isDebugEnabled()) { + log.debug("Device not found for id '" + deviceId.getId() + "'"); + } + return false; + } + + if (!device.getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.REMOVED)) { + String msg = "Device " + deviceId.getId() + " of type " + deviceId.getType() + " is not dis-enrolled to " + + "permanently delete the device"; + log.error(msg); + throw new DeviceManagementException(msg); + } else { + try { + DeviceManagementDAOFactory.beginTransaction(); + deviceDAO.deleteDevice(deviceId, tenantId); + DeviceManagementDAOFactory.commitTransaction(); + this.removeDeviceFromCache(deviceId); + } catch (DeviceManagementDAOException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while permanently deleting '" + deviceId.getType() + + "' device with the identifier '" + deviceId.getId() + "'"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (TransactionManagementException e) { + String msg = "Error occurred while initiating transaction"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred while permanently deleting device: " + deviceId.getId(); + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + return true; + } + @Override public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException { Device device = this.getDevice(deviceId, false);