Fix the device information delete issue

revert-70aa11f8
geethkokila 7 years ago
parent 7dd3f29ff7
commit 54fc0be69f

@ -128,9 +128,12 @@ public interface UserManagementAdminService {
@Path("/{username}/devices")
@DELETE @DELETE
@Path("/{username}/devices")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation( @ApiOperation(
consumes = MediaType.APPLICATION_JSON,
produces = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON,
httpMethod = HTTPConstants.HEADER_DELETE, httpMethod = HTTPConstants.HEADER_DELETE,
value = "Delete a users associated devices.", value = "Delete a users associated devices.",
@ -186,6 +189,8 @@ public interface UserManagementAdminService {
//DELETE devices/type/virtual_firealarm/id/us06ww93auzp //DELETE devices/type/virtual_firealarm/id/us06ww93auzp
@DELETE @DELETE
@Path("/type/{device-type}/id/{device-id}") @Path("/type/{device-type}/id/{device-id}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation( @ApiOperation(
produces = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON,
consumes = MediaType.APPLICATION_JSON, consumes = MediaType.APPLICATION_JSON,

@ -19,6 +19,8 @@
package org.wso2.carbon.device.mgt.core.privacy.dao.impl; 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.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.privacy.dao.PrivacyComplianceDAO; import org.wso2.carbon.device.mgt.core.privacy.dao.PrivacyComplianceDAO;
@ -34,6 +36,8 @@ import java.util.List;
public class PrivacyComplianceDAOImpl implements PrivacyComplianceDAO { public class PrivacyComplianceDAOImpl implements PrivacyComplianceDAO {
private static final Log log = LogFactory.getLog(PrivacyComplianceDAOImpl.class);
@Override @Override
public List<DeviceEnrollmentMapping> getDevicesOfUser(String username, int tenantId) throws PrivacyComplianceDAOException { public List<DeviceEnrollmentMapping> getDevicesOfUser(String username, int tenantId) throws PrivacyComplianceDAOException {
@ -43,16 +47,16 @@ public class PrivacyComplianceDAOImpl implements PrivacyComplianceDAO {
List<DeviceEnrollmentMapping> deviceIds = new ArrayList<>(); List<DeviceEnrollmentMapping> deviceIds = new ArrayList<>();
try { try {
conn = this.getConnection(); 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 = conn.prepareStatement(sql);
stmt.setString(1, username); stmt.setString(1, username);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
stmt.executeUpdate(); rs = stmt.executeQuery();
while (rs.next()) { while (rs.next()) {
DeviceEnrollmentMapping mapping = new DeviceEnrollmentMapping(); DeviceEnrollmentMapping mapping = new DeviceEnrollmentMapping();
mapping.setDeviceId(rs.getInt("DEVICE_ID")); mapping.setDeviceId(rs.getInt("DEVICE_ID"));
mapping.setEnrolmentId(rs.getInt("ENROLMENT_ID")); mapping.setEnrolmentId(rs.getInt("ID"));
deviceIds.add(mapping); deviceIds.add(mapping);
} }
if (deviceIds.isEmpty()) { if (deviceIds.isEmpty()) {
@ -102,7 +106,9 @@ public class PrivacyComplianceDAOImpl implements PrivacyComplianceDAO {
stmt.executeUpdate(); stmt.executeUpdate();
} catch (SQLException e) { } 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 { } finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null); DeviceManagementDAOUtil.cleanupResources(stmt, null);
} }

@ -58,6 +58,10 @@ public class PrivacyComplianceProviderImpl implements PrivacyComplianceProvider
DeviceManagementDAOFactory.beginTransaction(); DeviceManagementDAOFactory.beginTransaction();
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
List<DeviceEnrollmentMapping> enrollmentMappings = complianceDAO.getDevicesOfUser(username, tenantId); List<DeviceEnrollmentMapping> enrollmentMappings = complianceDAO.getDevicesOfUser(username, tenantId);
if(enrollmentMappings == null || enrollmentMappings.isEmpty()){
log.info("No enrolments found with the user..!");
return;
}
Map<Integer, List<Integer>> deviceMap = new HashMap<>(); Map<Integer, List<Integer>> deviceMap = new HashMap<>();
int x = -1; int x = -1;
for (DeviceEnrollmentMapping m : enrollmentMappings) { for (DeviceEnrollmentMapping m : enrollmentMappings) {
@ -79,7 +83,7 @@ public class PrivacyComplianceProviderImpl implements PrivacyComplianceProvider
complianceDAO.deleteDeviceDetails(deviceId, enrolmentId); complianceDAO.deleteDeviceDetails(deviceId, enrolmentId);
complianceDAO.deleteDeviceProperties(deviceId, enrolmentId, tenantId); complianceDAO.deleteDeviceProperties(deviceId, enrolmentId, tenantId);
complianceDAO.deleteDeviceLocation(deviceId, enrolmentId); complianceDAO.deleteDeviceLocation(deviceId, enrolmentId);
complianceDAO.deleteDeviceEnrollments(deviceId, enrolmentId); complianceDAO.deleteDeviceEnrollments(deviceId, tenantId);
} }
complianceDAO.deleteDevice(deviceId, tenantId); complianceDAO.deleteDevice(deviceId, tenantId);
} }
@ -116,6 +120,8 @@ public class PrivacyComplianceProviderImpl implements PrivacyComplianceProvider
complianceDAO.deleteDeviceDetails(device.getId(), device.getEnrolmentInfo().getId()); complianceDAO.deleteDeviceDetails(device.getId(), device.getEnrolmentInfo().getId());
complianceDAO.deleteDeviceProperties(device.getId(), device.getEnrolmentInfo().getId(), tenantId); complianceDAO.deleteDeviceProperties(device.getId(), device.getEnrolmentInfo().getId(), tenantId);
complianceDAO.deleteDeviceLocation(device.getId(), device.getEnrolmentInfo().getId()); complianceDAO.deleteDeviceLocation(device.getId(), device.getEnrolmentInfo().getId());
complianceDAO.deleteDeviceEnrollments(device.getId(), tenantId);
complianceDAO.deleteDevice(device.getId(), tenantId);
DeviceManagementDAOFactory.commitTransaction(); DeviceManagementDAOFactory.commitTransaction();
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
DeviceManagementDAOFactory.rollbackTransaction(); DeviceManagementDAOFactory.rollbackTransaction();

Loading…
Cancel
Save