From be1bee9b9ada8b00d5c95389a935c69fc24c7544 Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Thu, 30 May 2019 18:21:28 +0530 Subject: [PATCH 1/4] Modify delete device API to have permanentDelete query parameter --- .../jaxrs/service/api/DeviceManagementService.java | 7 ++++++- .../service/impl/DeviceManagementServiceImpl.java | 14 +++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java index db0902ee362..96991ed2ce8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java @@ -899,7 +899,12 @@ public interface DeviceManagementService { required = true) @PathParam("device-id") @Size(max = 45) - String deviceId); + String deviceId, + @ApiParam( + name = "permanentDelete", + value = "Boolean flag indicating whether to permanently delete the device.", + required = false) + @QueryParam("permanentDelete") boolean permanentDelete); @GET @Path("/{type}/{id}/features") diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index b679323f97e..9175580ce93 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -323,7 +323,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { @DELETE @Override @Path("/type/{device-type}/id/{device-id}") - public Response deleteDevice(@PathParam("device-type") String deviceType, @PathParam("device-id") String deviceId) { + public Response deleteDevice(@PathParam("device-type") String deviceType, + @PathParam("device-id") String deviceId, + @QueryParam("permanentDelete") boolean permanentDelete) { DeviceManagementProviderService deviceManagementProviderService = DeviceMgtAPIUtils.getDeviceManagementService(); try { @@ -333,13 +335,19 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { return Response.status(Response.Status.NOT_FOUND).build(); } - boolean response = deviceManagementProviderService.disenrollDevice(deviceIdentifier); + boolean response; + + if (permanentDelete) { + response = deviceManagementProviderService.deleteDevice(deviceIdentifier); + } else { + response = deviceManagementProviderService.disenrollDevice(deviceIdentifier); + } return Response.status(Response.Status.OK).entity(response).build(); } catch (DeviceManagementException e) { String msg = "Error encountered while deleting device of type : " + deviceType + " and " + "ID : " + deviceId; - log.error(msg); + log.error(msg, e); return Response.status(Response.Status.BAD_REQUEST).entity( new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build() ).build(); From 48ae5c58e9cfbc9bf26fa6a0d80d3dc55df0d063 Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Thu, 30 May 2019 18:41:23 +0530 Subject: [PATCH 2/4] 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 c2c7ef4813f..43ac2c8567f 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 151352c7b1d..621cc966e36 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); From 52e971de7609e9fcb5bb76f79753d830bf07288a Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Thu, 30 May 2019 18:43:15 +0530 Subject: [PATCH 3/4] Add delete device dao related implementation --- .../carbon/device/mgt/core/dao/DeviceDAO.java | 8 + .../core/dao/impl/AbstractDeviceDAOImpl.java | 377 ++++++++++++++++++ 2 files changed, 385 insertions(+) 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 bdb7557ae49..b089477f06f 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 @@ -478,5 +478,13 @@ public interface DeviceDAO { * fails. */ List getDevicesByIdentifiers(List deviceIdentifiers, int tenantId) throws DeviceManagementDAOException; + + /** + * This method is used to permanently delete the device and its related details + * @param deviceIdentifier device id + * @param tenantId tenant id + * @throws DeviceManagementDAOException + */ + void deleteDevice(DeviceIdentifier deviceIdentifier, int tenantId) throws DeviceManagementDAOException; } 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 30cd6d4dcd2..aa07aba8b47 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 @@ -36,6 +36,7 @@ package org.wso2.carbon.device.mgt.core.dao.impl; +import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; @@ -62,6 +63,8 @@ import java.util.StringJoiner; public abstract class AbstractDeviceDAOImpl implements DeviceDAO { + private static final org.apache.commons.logging.Log log = LogFactory.getLog(AbstractDeviceDAOImpl.class); + @Override public int addDevice(int typeId, Device device, int tenantId) throws DeviceManagementDAOException { Connection conn; @@ -1404,5 +1407,379 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { e); } } + + public void deleteDevice(DeviceIdentifier deviceIdentifier, int tenantId) throws DeviceManagementDAOException { + Connection conn; + try { + conn = this.getConnection(); + int deviceId = getDeviceId(conn, deviceIdentifier, tenantId); + if (deviceId == -1) { + String msg = "Device " + deviceIdentifier.getId() + " of type " + deviceIdentifier.getType() + + " is not found"; + log.error(msg); + throw new DeviceManagementDAOException(msg); + } else { + int enrollmentId = getEnrollmentId(conn, deviceId, tenantId); + if (enrollmentId == -1) { + String msg = "Enrollment not found for the device " + deviceIdentifier.getId() + " of type " + + deviceIdentifier.getType(); + log.error(msg); + throw new DeviceManagementDAOException(msg); + } else { + removeDeviceDetail(conn, deviceId); + if (log.isDebugEnabled()) { + log.debug("Successfully removed device detail data"); + } + removeDeviceLocation(conn, deviceId); + if (log.isDebugEnabled()) { + log.debug("Successfully removed device location data"); + } + removeDeviceInfo(conn, deviceId); + if (log.isDebugEnabled()) { + log.debug("Successfully removed device info data"); + } + removeDeviceNotification(conn, deviceId); + if (log.isDebugEnabled()) { + log.debug("Successfully removed device notification data"); + } + removeDeviceApplicationMapping(conn, deviceId); + if (log.isDebugEnabled()) { + log.debug("Successfully removed device application mapping data"); + } + removeDevicePolicyApplied(conn, deviceId); + if (log.isDebugEnabled()) { + log.debug("Successfully removed device applied policy data"); + } + removeDevicePolicy(conn, deviceId); + if (log.isDebugEnabled()) { + log.debug("Successfully removed device policy data"); + } + removeEnrollmentDeviceDetail(conn, enrollmentId); + if (log.isDebugEnabled()) { + log.debug("Successfully removed enrollment device detail data"); + } + removeEnrollmentDeviceLocation(conn, enrollmentId); + if (log.isDebugEnabled()) { + log.debug("Successfully removed enrollment device location data"); + } + removeEnrollmentDeviceInfo(conn, enrollmentId); + if (log.isDebugEnabled()) { + log.debug("Successfully removed enrollment device info data"); + } + removeEnrollmentDeviceApplicationMapping(conn, enrollmentId); + if (log.isDebugEnabled()) { + log.debug("Successfully removed enrollment device application mapping data"); + } + removeDeviceOperationResponse(conn, enrollmentId); + if (log.isDebugEnabled()) { + log.debug("Successfully removed device operation response data"); + } + removeEnrollmentOperationMapping(conn, enrollmentId); + if (log.isDebugEnabled()) { + log.debug("Successfully removed enrollment operation mapping data"); + } + removeDeviceEnrollment(conn, deviceId); + if (log.isDebugEnabled()) { + log.debug("Successfully removed device enrollment data"); + } + removeDeviceGroupMapping(conn, deviceId); + if (log.isDebugEnabled()) { + log.debug("Successfully removed device group mapping data"); + } + removeDevice(conn, deviceId); + if (log.isDebugEnabled()) { + log.debug("Successfully permanently deleted the device"); + } + } + } + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while deleting the device", e); + } + } + + private int getDeviceId(Connection conn, DeviceIdentifier deviceIdentifier, int tenantId) + throws DeviceManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs = null; + int deviceId = -1; + try { + String sql = "SELECT ID FROM DM_DEVICE WHERE DEVICE_IDENTIFICATION = ? AND TENANT_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setString(1, deviceIdentifier.getId()); + stmt.setInt(2, tenantId); + rs = stmt.executeQuery(); + if (rs.next()) { + deviceId = rs.getInt("ID"); + } + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving device id of the device", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + return deviceId; + } + + private int getEnrollmentId(Connection conn, int deviceId, int tenantId) throws DeviceManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs = null; + int enrollmentId = -1; + try { + String sql = "SELECT ID FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND TENANT_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, deviceId); + stmt.setInt(2, tenantId); + rs = stmt.executeQuery(); + if (rs.next()) { + enrollmentId = rs.getInt("ID"); + } + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving enrollment id of the device", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + return enrollmentId; + } + + private void removeDeviceDetail(Connection conn, int deviceId) throws DeviceManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs = null; + try { + String sql = "DELETE FROM DM_DEVICE_DETAIL WHERE DEVICE_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, deviceId); + stmt.executeUpdate(); + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while removing device detail", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + } + + private void removeDeviceLocation(Connection conn, int deviceId) throws DeviceManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs = null; + try { + String sql = "DELETE FROM DM_DEVICE_LOCATION WHERE DEVICE_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, deviceId); + stmt.executeUpdate(); + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while removing device location", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + } + + private void removeDeviceInfo(Connection conn, int deviceId) throws DeviceManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs = null; + try { + String sql = "DELETE FROM DM_DEVICE_INFO WHERE DEVICE_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, deviceId); + stmt.executeUpdate(); + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while removing device info", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + } + private void removeDeviceNotification(Connection conn, int deviceId) throws DeviceManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs = null; + try { + String sql = "DELETE FROM DM_NOTIFICATION WHERE DEVICE_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, deviceId); + stmt.executeUpdate(); + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while removing device notification", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + } + + private void removeDeviceApplicationMapping(Connection conn, int deviceId) throws DeviceManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs = null; + try { + String sql = "DELETE FROM DM_DEVICE_APPLICATION_MAPPING WHERE DEVICE_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, deviceId); + stmt.executeUpdate(); + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while removing device application mapping", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + } + + private void removeDevicePolicyApplied(Connection conn, int deviceId) throws DeviceManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs = null; + try { + String sql = "DELETE FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, deviceId); + stmt.executeUpdate(); + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while removing device policy applied", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + } + + private void removeDevicePolicy(Connection conn, int deviceId) throws DeviceManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs = null; + try { + String sql = "DELETE FROM DM_DEVICE_POLICY WHERE DEVICE_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, deviceId); + stmt.executeUpdate(); + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while removing device policy", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + } + + private void removeEnrollmentDeviceDetail(Connection conn, int enrollmentId) throws DeviceManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs = null; + try { + String sql = "DELETE FROM DM_DEVICE_DETAIL WHERE ENROLMENT_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, enrollmentId); + stmt.executeUpdate(); + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while removing enrollment device detail", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + } + + private void removeEnrollmentDeviceLocation(Connection conn, int enrollmentId) throws DeviceManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs = null; + try { + String sql = "DELETE FROM DM_DEVICE_LOCATION WHERE ENROLMENT_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, enrollmentId); + stmt.executeUpdate(); + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while removing enrollment device location", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + } + + private void removeEnrollmentDeviceInfo(Connection conn, int enrollmentId) throws DeviceManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs = null; + try { + String sql = "DELETE FROM DM_DEVICE_INFO WHERE ENROLMENT_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, enrollmentId); + stmt.executeUpdate(); + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while removing enrollment device info", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + } + + private void removeEnrollmentDeviceApplicationMapping(Connection conn, int enrollmentId) + throws DeviceManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs = null; + try { + String sql = "DELETE FROM DM_DEVICE_APPLICATION_MAPPING WHERE ENROLMENT_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, enrollmentId); + stmt.executeUpdate(); + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while removing enrollment device application " + + "mapping", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + } + + private void removeDeviceOperationResponse(Connection conn, int enrollmentId) throws DeviceManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs = null; + try { + String sql = "DELETE FROM DM_DEVICE_OPERATION_RESPONSE WHERE ENROLMENT_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, enrollmentId); + stmt.executeUpdate(); + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while removing device operation response", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + } + + private void removeEnrollmentOperationMapping(Connection conn, int enrollmentId) + throws DeviceManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs = null; + try { + String sql = "DELETE FROM DM_ENROLMENT_OP_MAPPING WHERE ENROLMENT_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, enrollmentId); + stmt.executeUpdate(); + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while removing enrollment operation mapping", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + } + + private void removeDeviceEnrollment(Connection conn, int deviceId) throws DeviceManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs = null; + try { + String sql = "DELETE FROM DM_ENROLMENT WHERE DEVICE_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, deviceId); + stmt.executeUpdate(); + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while removing device enrollment", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + } + + private void removeDeviceGroupMapping(Connection conn, int deviceId) throws DeviceManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs = null; + try { + String sql = "DELETE FROM DM_DEVICE_GROUP_MAP WHERE DEVICE_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, deviceId); + stmt.executeUpdate(); + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while removing device group mapping", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + } + + private void removeDevice(Connection conn, int deviceId) throws DeviceManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs = null; + try { + String sql = "DELETE FROM DM_DEVICE WHERE ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, deviceId); + stmt.executeUpdate(); + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while removing device", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + } } From 1f001b09f17bf5680838029e8b1578326a9c10ec Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Fri, 31 May 2019 12:09:40 +0530 Subject: [PATCH 4/4] Fix tests related to deleteDevice API --- .../service/impl/DeviceManagementServiceImplTest.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImplTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImplTest.java index fc26189f409..6b923ba7757 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImplTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImplTest.java @@ -481,7 +481,8 @@ public class DeviceManagementServiceImplTest { public void testDeleteDevice() { PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService")) .toReturn(this.deviceManagementProviderService); - Response response = this.deviceManagementService.deleteDevice(TEST_DEVICE_TYPE, UUID.randomUUID().toString()); + Response response = this.deviceManagementService.deleteDevice(TEST_DEVICE_TYPE, UUID.randomUUID().toString() + , false); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); } @@ -491,7 +492,8 @@ public class DeviceManagementServiceImplTest { .toReturn(this.deviceManagementProviderService); Mockito.when(this.deviceManagementProviderService .getDevice(Mockito.any(DeviceIdentifier.class), Mockito.anyBoolean())).thenReturn(null); - Response response = this.deviceManagementService.deleteDevice(TEST_DEVICE_TYPE, UUID.randomUUID().toString()); + Response response = this.deviceManagementService.deleteDevice(TEST_DEVICE_TYPE, UUID.randomUUID().toString() + , false); Assert.assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode()); Mockito.reset(this.deviceManagementProviderService); } @@ -502,7 +504,8 @@ public class DeviceManagementServiceImplTest { .toReturn(this.deviceManagementProviderService); Mockito.when(this.deviceManagementProviderService.disenrollDevice(Mockito.any(DeviceIdentifier.class))) .thenThrow(new DeviceManagementException()); - Response response = this.deviceManagementService.deleteDevice(TEST_DEVICE_TYPE, UUID.randomUUID().toString()); + Response response = this.deviceManagementService.deleteDevice(TEST_DEVICE_TYPE, UUID.randomUUID().toString() + , false); Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode()); Mockito.reset(this.deviceManagementProviderService); }