From 54fc0be69f2619338d923ba39d9b3d47cd34fe17 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Fri, 16 Mar 2018 17:23:45 +0530 Subject: [PATCH] Fix the device information delete issue --- .../api/admin/UserManagementAdminService.java | 7 ++++++- .../privacy/dao/impl/PrivacyComplianceDAOImpl.java | 14 ++++++++++---- .../impl/PrivacyComplianceProviderImpl.java | 8 +++++++- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/UserManagementAdminService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/UserManagementAdminService.java index 93edca0451..bbe64367c4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/UserManagementAdminService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/UserManagementAdminService.java @@ -128,9 +128,12 @@ public interface UserManagementAdminService { - @Path("/{username}/devices") @DELETE + @Path("/{username}/devices") + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) @ApiOperation( + consumes = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON, httpMethod = HTTPConstants.HEADER_DELETE, value = "Delete a users associated devices.", @@ -186,6 +189,8 @@ public interface UserManagementAdminService { //DELETE devices/type/virtual_firealarm/id/us06ww93auzp @DELETE @Path("/type/{device-type}/id/{device-id}") + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) @ApiOperation( produces = MediaType.APPLICATION_JSON, consumes = MediaType.APPLICATION_JSON, diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/privacy/dao/impl/PrivacyComplianceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/privacy/dao/impl/PrivacyComplianceDAOImpl.java index 7c2b7d25b9..537766ddb3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/privacy/dao/impl/PrivacyComplianceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/privacy/dao/impl/PrivacyComplianceDAOImpl.java @@ -19,6 +19,8 @@ package org.wso2.carbon.device.mgt.core.privacy.dao.impl; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.privacy.dao.PrivacyComplianceDAO; @@ -34,6 +36,8 @@ import java.util.List; public class PrivacyComplianceDAOImpl implements PrivacyComplianceDAO { + private static final Log log = LogFactory.getLog(PrivacyComplianceDAOImpl.class); + @Override public List getDevicesOfUser(String username, int tenantId) throws PrivacyComplianceDAOException { @@ -43,16 +47,16 @@ public class PrivacyComplianceDAOImpl implements PrivacyComplianceDAO { List deviceIds = new ArrayList<>(); try { conn = this.getConnection(); - String sql = "SELECT * FROM DM_ENROLMENT WHERE OWNER = ? AND TENANT_ID = ? ORDER BY DEVICE_ID"; + String sql = "SELECT * FROM DM_ENROLMENT WHERE OWNER = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, username); stmt.setInt(2, tenantId); - stmt.executeUpdate(); + rs = stmt.executeQuery(); while (rs.next()) { DeviceEnrollmentMapping mapping = new DeviceEnrollmentMapping(); mapping.setDeviceId(rs.getInt("DEVICE_ID")); - mapping.setEnrolmentId(rs.getInt("ENROLMENT_ID")); + mapping.setEnrolmentId(rs.getInt("ID")); deviceIds.add(mapping); } if (deviceIds.isEmpty()) { @@ -102,7 +106,9 @@ public class PrivacyComplianceDAOImpl implements PrivacyComplianceDAO { stmt.executeUpdate(); } catch (SQLException e) { - throw new PrivacyComplianceDAOException("Error occurred while deleting the device enrolments", e); + String msg = "Error occurred while deleting the device enrolments"; + log.error(msg, e); + throw new PrivacyComplianceDAOException(msg, e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, null); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/privacy/impl/PrivacyComplianceProviderImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/privacy/impl/PrivacyComplianceProviderImpl.java index 072bf19c17..fc461a8713 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/privacy/impl/PrivacyComplianceProviderImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/privacy/impl/PrivacyComplianceProviderImpl.java @@ -58,6 +58,10 @@ public class PrivacyComplianceProviderImpl implements PrivacyComplianceProvider DeviceManagementDAOFactory.beginTransaction(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); List enrollmentMappings = complianceDAO.getDevicesOfUser(username, tenantId); + if(enrollmentMappings == null || enrollmentMappings.isEmpty()){ + log.info("No enrolments found with the user..!"); + return; + } Map> deviceMap = new HashMap<>(); int x = -1; for (DeviceEnrollmentMapping m : enrollmentMappings) { @@ -79,7 +83,7 @@ public class PrivacyComplianceProviderImpl implements PrivacyComplianceProvider complianceDAO.deleteDeviceDetails(deviceId, enrolmentId); complianceDAO.deleteDeviceProperties(deviceId, enrolmentId, tenantId); complianceDAO.deleteDeviceLocation(deviceId, enrolmentId); - complianceDAO.deleteDeviceEnrollments(deviceId, enrolmentId); + complianceDAO.deleteDeviceEnrollments(deviceId, tenantId); } complianceDAO.deleteDevice(deviceId, tenantId); } @@ -116,6 +120,8 @@ public class PrivacyComplianceProviderImpl implements PrivacyComplianceProvider complianceDAO.deleteDeviceDetails(device.getId(), device.getEnrolmentInfo().getId()); complianceDAO.deleteDeviceProperties(device.getId(), device.getEnrolmentInfo().getId(), tenantId); complianceDAO.deleteDeviceLocation(device.getId(), device.getEnrolmentInfo().getId()); + complianceDAO.deleteDeviceEnrollments(device.getId(), tenantId); + complianceDAO.deleteDevice(device.getId(), tenantId); DeviceManagementDAOFactory.commitTransaction(); } catch (TransactionManagementException e) { DeviceManagementDAOFactory.rollbackTransaction();