mirror of
https://repository.entgra.net/community/device-mgt-core.git
synced 2025-10-06 02:01:45 +00:00
Merge branch 'permanent-delete' into 'master'
Permanent delete API modification ## Purpose > Delete multple devices from one API call. ## Goals > Avoid invoking the API multiple times to delete a set of devices. ## Approach > Delete the records of the devices as batch operations in the core level via a DEeviceDAO object and in the plugin level by creating a DeviceManager object for each device type. ## Documentation > N/A ## Automation tests - Unit tests > None - Integration tests > None ## Security checks - Followed secure coding standards? (yes) - Ran FindSecurityBugs plugin and verified report? (no) - Confirmed that this PR doesn't commit any keys, passwords, tokens, usernames, or other secrets? (yes) ## Related MRs > https://gitlab.com/entgra/carbon-device-mgt/merge_requests/198 ## Test environment > N/A ## Learning > N/A See merge request entgra/carbon-device-mgt!241
This commit is contained in:
commit
d6bc33b0a2
@ -249,7 +249,7 @@ public interface DeviceManagementAdminService {
|
|||||||
List<String> deviceIdentifiers);
|
List<String> deviceIdentifiers);
|
||||||
|
|
||||||
@DELETE
|
@DELETE
|
||||||
@Path("/type/{device-type}/id/{device-id}")
|
@Path("/permanent-delete")
|
||||||
@ApiOperation(
|
@ApiOperation(
|
||||||
produces = MediaType.APPLICATION_JSON,
|
produces = MediaType.APPLICATION_JSON,
|
||||||
consumes = MediaType.APPLICATION_JSON,
|
consumes = MediaType.APPLICATION_JSON,
|
||||||
@ -282,37 +282,20 @@ public interface DeviceManagementAdminService {
|
|||||||
description = "Date and time the resource has been modified the last time.\n" +
|
description = "Date and time the resource has been modified the last time.\n" +
|
||||||
"Used by caches, or in conditional requests."),
|
"Used by caches, or in conditional requests."),
|
||||||
}),
|
}),
|
||||||
@ApiResponse(
|
|
||||||
code = 304,
|
|
||||||
message = "Not Modified. Empty body because the client already has the latest " +
|
|
||||||
"version of the requested resource."),
|
|
||||||
@ApiResponse(
|
@ApiResponse(
|
||||||
code = 400,
|
code = 400,
|
||||||
message = "Bad Request. \n Invalid request or validation error.",
|
message = "Bad Request. \n Invalid request or validation error.",
|
||||||
response = ErrorResponse.class),
|
response = ErrorResponse.class),
|
||||||
@ApiResponse(
|
|
||||||
code = 404,
|
|
||||||
message = "Not Found. \n No device is found under the provided type and id.",
|
|
||||||
response = ErrorResponse.class),
|
|
||||||
@ApiResponse(
|
@ApiResponse(
|
||||||
code = 500,
|
code = 500,
|
||||||
message = "Internal Server Error. \n " +
|
message = "Internal Server Error. \n " +
|
||||||
"Server error occurred while retrieving information requested device.",
|
"Server error occurred while retrieving information requested device.",
|
||||||
response = ErrorResponse.class)
|
response = ErrorResponse.class)
|
||||||
})
|
})
|
||||||
Response deleteDevicePermanently(
|
Response deleteDevicesPermanently(
|
||||||
@ApiParam(
|
@ApiParam(
|
||||||
name = "device-type",
|
name = "Device Identifiers",
|
||||||
value = "The device type, such as ios, android, or windows.",
|
value = "List of device identifiers.",
|
||||||
required = true)
|
required = true)
|
||||||
@PathParam("device-type")
|
List<String> deviceIdentifiers);
|
||||||
@Size(max = 45)
|
|
||||||
String deviceType,
|
|
||||||
@ApiParam(
|
|
||||||
name = "device-id",
|
|
||||||
value = "The device identifier of the device.",
|
|
||||||
required = true)
|
|
||||||
@PathParam("device-id")
|
|
||||||
@Size(max = 45)
|
|
||||||
String deviceId);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -138,26 +138,26 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe
|
|||||||
|
|
||||||
@DELETE
|
@DELETE
|
||||||
@Override
|
@Override
|
||||||
@Path("/type/{device-type}/id/{device-id}")
|
@Path("/permanent-delete")
|
||||||
public Response deleteDevicePermanently(@PathParam("device-type") String deviceType,
|
public Response deleteDevicesPermanently(List<String> deviceIdentifiers) {
|
||||||
@PathParam("device-id") String deviceId) {
|
|
||||||
DeviceManagementProviderService deviceManagementProviderService =
|
DeviceManagementProviderService deviceManagementProviderService =
|
||||||
DeviceMgtAPIUtils.getDeviceManagementService();
|
DeviceMgtAPIUtils.getDeviceManagementService();
|
||||||
try {
|
try {
|
||||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(deviceId, deviceType);
|
if (!deviceManagementProviderService.deleteDevices(deviceIdentifiers)) {
|
||||||
Device persistedDevice = deviceManagementProviderService.getDevice(deviceIdentifier, true);
|
String msg = "Found un-deployed device type.";
|
||||||
if (persistedDevice == null) {
|
|
||||||
String msg = "No device found with the device type: " + deviceType +
|
|
||||||
" having the device ID: " + deviceId + " to permanently delete.";
|
|
||||||
log.error(msg);
|
log.error(msg);
|
||||||
return Response.status(Response.Status.NOT_FOUND).entity(
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(
|
||||||
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
|
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
|
||||||
}
|
}
|
||||||
boolean response = deviceManagementProviderService.deleteDevice(deviceIdentifier);
|
return Response.status(Response.Status.OK).entity(true).build();
|
||||||
return Response.status(Response.Status.OK).entity(response).build();
|
|
||||||
} catch (DeviceManagementException e) {
|
} catch (DeviceManagementException e) {
|
||||||
String msg = "Error encountered while permanently deleting device of type : " + deviceType + " and " +
|
String msg = "Error encountered while permanently deleting devices";
|
||||||
"ID : " + deviceId;
|
log.error(msg, e);
|
||||||
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(
|
||||||
|
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
|
||||||
|
}
|
||||||
|
catch (InvalidDeviceException e) {
|
||||||
|
String msg = "Found Invalid devices";
|
||||||
log.error(msg, e);
|
log.error(msg, e);
|
||||||
return Response.status(Response.Status.BAD_REQUEST).entity(
|
return Response.status(Response.Status.BAD_REQUEST).entity(
|
||||||
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
|
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
|
||||||
|
|||||||
@ -97,13 +97,11 @@ public interface DeviceManager {
|
|||||||
boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException;
|
boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method to delete a particular device from CDM.
|
* Method to delete multiple devices from CDM.
|
||||||
*
|
* @param deviceIdentifiers Fully qualified device identifier list
|
||||||
* @param deviceId Fully qualified device identifier
|
|
||||||
* @return A boolean indicating the status of the operation.
|
|
||||||
* @throws DeviceManagementException If some unusual behaviour is observed while deleting a device
|
* @throws DeviceManagementException If some unusual behaviour is observed while deleting a device
|
||||||
*/
|
*/
|
||||||
boolean deleteDevice(DeviceIdentifier deviceId, Device device) throws DeviceManagementException;
|
void deleteDevices(List<String> deviceIdentifiers) throws DeviceManagementException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method to retrieve the status of the registration process of a particular device.
|
* Method to retrieve the status of the registration process of a particular device.
|
||||||
|
|||||||
@ -522,11 +522,12 @@ public interface DeviceDAO {
|
|||||||
List<Device> getDevicesByIdentifiers(List<String> deviceIdentifiers, int tenantId)
|
List<Device> getDevicesByIdentifiers(List<String> deviceIdentifiers, int tenantId)
|
||||||
throws DeviceManagementDAOException;
|
throws DeviceManagementDAOException;
|
||||||
|
|
||||||
/**
|
/***
|
||||||
* This method is used to permanently delete the device and its related details
|
* This method is used to permanently delete devices and their related details
|
||||||
* @param deviceIdentifier device id
|
* @param deviceIdentifiers List of device identifiers.
|
||||||
* @param tenantId tenant id
|
* @param deviceIds list of device ids (primary keys).
|
||||||
* @throws DeviceManagementDAOException
|
* @param enrollmentIds list of enrollment ids.
|
||||||
|
* @throws DeviceManagementDAOException when no enrolments are found for the given device.
|
||||||
*/
|
*/
|
||||||
void deleteDevice(DeviceIdentifier deviceIdentifier, int tenantId) throws DeviceManagementDAOException;
|
void deleteDevices(List<String> deviceIdentifiers, List<Integer> deviceIds, List<Integer> enrollmentIds) throws DeviceManagementDAOException;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -56,6 +56,7 @@ import java.sql.PreparedStatement;
|
|||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.sql.Timestamp;
|
import java.sql.Timestamp;
|
||||||
|
import java.sql.Statement;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
@ -1560,9 +1561,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|||||||
ps.setInt(index++, tenantId);
|
ps.setInt(index++, tenantId);
|
||||||
ps.setInt(index, tenantId);
|
ps.setInt(index, tenantId);
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
if (rs.next()) {
|
while (rs.next()) {
|
||||||
Device device = DeviceManagementDAOUtil.loadDevice(rs);
|
devices.add(DeviceManagementDAOUtil.loadDevice(rs));
|
||||||
devices.add(device);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1572,369 +1572,394 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|||||||
+ " given device identifiers.", e);
|
+ " given device identifiers.", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deleteDevice(DeviceIdentifier deviceIdentifier, int tenantId) throws DeviceManagementDAOException {
|
@Override
|
||||||
String deviceIdentifierId = deviceIdentifier.getId();
|
public void deleteDevices(List<String> deviceIdentifiers, List<Integer> deviceIds, List<Integer> enrollmentIds)
|
||||||
String deviceType = deviceIdentifier.getType();
|
throws DeviceManagementDAOException {
|
||||||
Connection conn;
|
Connection conn;
|
||||||
try {
|
try {
|
||||||
conn = this.getConnection();
|
conn = this.getConnection();
|
||||||
int deviceId = getDeviceId(conn, deviceIdentifier, tenantId);
|
if (enrollmentIds.isEmpty()) {
|
||||||
if (deviceId == -1) {
|
String msg = "Enrollments not found for the devices: " + deviceIdentifiers;
|
||||||
String msg = "Device " + deviceIdentifierId + " of type " + deviceType + " is not found";
|
|
||||||
log.error(msg);
|
log.error(msg);
|
||||||
throw new DeviceManagementDAOException(msg);
|
throw new DeviceManagementDAOException(msg);
|
||||||
} else {
|
} else {
|
||||||
List<Integer> enrollmentIds = getEnrollmentIds(conn, deviceId, tenantId);
|
removeDeviceDetail(conn, deviceIds);
|
||||||
if (enrollmentIds == null || enrollmentIds.isEmpty()) {
|
if (log.isDebugEnabled()) {
|
||||||
String msg = "Enrollments not found for the device " + deviceIdentifierId + " of type "
|
log.debug("Successfully removed device detail data of devices: " + deviceIdentifiers);
|
||||||
+ deviceType;
|
}
|
||||||
log.error(msg);
|
removeDeviceLocation(conn, deviceIds);
|
||||||
throw new DeviceManagementDAOException(msg);
|
if (log.isDebugEnabled()) {
|
||||||
} else {
|
log.debug("Successfully removed device location data of devices: " + deviceIdentifiers);
|
||||||
removeDeviceDetail(conn, deviceId);
|
}
|
||||||
if (log.isDebugEnabled()) {
|
removeDeviceInfo(conn, deviceIds);
|
||||||
log.debug("Successfully removed device detail data of device " + deviceIdentifierId
|
if (log.isDebugEnabled()) {
|
||||||
+ " of type " + deviceType);
|
log.debug("Successfully removed device info data of devices: " + deviceIdentifiers);
|
||||||
}
|
}
|
||||||
removeDeviceLocation(conn, deviceId);
|
removeDeviceNotification(conn, deviceIds);
|
||||||
if (log.isDebugEnabled()) {
|
if (log.isDebugEnabled()) {
|
||||||
log.debug("Successfully removed device location data of device " + deviceIdentifierId
|
log.debug("Successfully removed device notification data of devices: " + deviceIdentifiers);
|
||||||
+ " of type " + deviceType);
|
}
|
||||||
}
|
removeDeviceApplicationMapping(conn, deviceIds);
|
||||||
removeDeviceInfo(conn, deviceId);
|
if (log.isDebugEnabled()) {
|
||||||
if (log.isDebugEnabled()) {
|
log.debug("Successfully removed device application mapping data of devices: "
|
||||||
log.debug("Successfully removed device info data of device " + deviceIdentifierId
|
+ deviceIdentifiers);
|
||||||
+ " of type " + deviceType);
|
}
|
||||||
}
|
removeDevicePolicyApplied(conn, deviceIds);
|
||||||
removeDeviceNotification(conn, deviceId);
|
if (log.isDebugEnabled()) {
|
||||||
if (log.isDebugEnabled()) {
|
log.debug("Successfully removed device applied policy data of devices: " + deviceIdentifiers);
|
||||||
log.debug("Successfully removed device notification data of device " + deviceIdentifierId
|
}
|
||||||
+ " of type " + deviceType);
|
removeDevicePolicy(conn, deviceIds);
|
||||||
}
|
if (log.isDebugEnabled()) {
|
||||||
removeDeviceApplicationMapping(conn, deviceId);
|
log.debug("Successfully removed device policy data of devices: " + deviceIdentifiers);
|
||||||
if (log.isDebugEnabled()) {
|
}
|
||||||
log.debug("Successfully removed device application mapping data of device "
|
if (log.isDebugEnabled()) {
|
||||||
+ deviceIdentifierId + " of type " + deviceType);
|
log.debug("Starting to remove " + enrollmentIds.size() + " enrollment data of devices with " +
|
||||||
}
|
"identifiers: " + deviceIdentifiers);
|
||||||
removeDevicePolicyApplied(conn, deviceId);
|
}
|
||||||
if (log.isDebugEnabled()) {
|
removeEnrollmentDeviceDetail(conn, enrollmentIds);
|
||||||
log.debug("Successfully removed device applied policy data of device " + deviceIdentifierId
|
removeEnrollmentDeviceLocation(conn, enrollmentIds);
|
||||||
+ " of type " + deviceType);
|
removeEnrollmentDeviceInfo(conn, enrollmentIds);
|
||||||
}
|
removeEnrollmentDeviceApplicationMapping(conn, enrollmentIds);
|
||||||
removeDevicePolicy(conn, deviceId);
|
removeDeviceOperationResponse(conn, enrollmentIds);
|
||||||
if (log.isDebugEnabled()) {
|
removeEnrollmentOperationMapping(conn, enrollmentIds);
|
||||||
log.debug("Successfully removed device policy data of device " + deviceIdentifierId
|
if (log.isDebugEnabled()) {
|
||||||
+ " of type " + deviceType);
|
log.debug("Successfully removed enrollment device details, enrollment device location," +
|
||||||
}
|
"enrollment device info, enrollment device application mapping, " +
|
||||||
if (log.isDebugEnabled()) {
|
"enrollment device operation response, enrollment operation mapping data of " +
|
||||||
log.debug("Starting to remove " + enrollmentIds.size() + " enrollment data of device "
|
"devices with identifiers: " + deviceIdentifiers);
|
||||||
+ deviceIdentifierId + " of type " + deviceType);
|
}
|
||||||
}
|
removeDeviceEnrollment(conn, deviceIds);
|
||||||
for (Integer enrollmentId : enrollmentIds) {
|
if (log.isDebugEnabled()) {
|
||||||
removeEnrollmentDeviceDetail(conn, enrollmentId);
|
log.debug("Successfully removed device enrollment data of devices: " + deviceIdentifiers);
|
||||||
removeEnrollmentDeviceLocation(conn, enrollmentId);
|
}
|
||||||
removeEnrollmentDeviceInfo(conn, enrollmentId);
|
removeDeviceGroupMapping(conn, deviceIds);
|
||||||
removeEnrollmentDeviceApplicationMapping(conn, enrollmentId);
|
if (log.isDebugEnabled()) {
|
||||||
removeDeviceOperationResponse(conn, enrollmentId);
|
log.debug("Successfully removed device group mapping data of devices: " + deviceIdentifiers);
|
||||||
removeEnrollmentOperationMapping(conn, enrollmentId);
|
}
|
||||||
}
|
removeDevice(conn, deviceIds);
|
||||||
if (log.isDebugEnabled()) {
|
if (log.isDebugEnabled()) {
|
||||||
log.debug("Successfully removed enrollment device details, enrollment device location, " +
|
log.debug("Successfully permanently deleted the device of devices: " + deviceIdentifiers);
|
||||||
"enrollment device info, enrollment device application mapping, " +
|
|
||||||
"enrollment device operation response, enrollment operation mapping data of device "
|
|
||||||
+ deviceIdentifierId + " of type " + deviceType);
|
|
||||||
}
|
|
||||||
removeDeviceEnrollment(conn, deviceId);
|
|
||||||
if (log.isDebugEnabled()) {
|
|
||||||
log.debug("Successfully removed device enrollment data of device " + deviceIdentifierId
|
|
||||||
+ " of type " + deviceType);
|
|
||||||
}
|
|
||||||
removeDeviceGroupMapping(conn, deviceId);
|
|
||||||
if (log.isDebugEnabled()) {
|
|
||||||
log.debug("Successfully removed device group mapping data of device " + deviceIdentifierId
|
|
||||||
+ " of type " + deviceType);
|
|
||||||
}
|
|
||||||
removeDevice(conn, deviceId);
|
|
||||||
if (log.isDebugEnabled()) {
|
|
||||||
log.debug("Successfully permanently deleted the device of device " + deviceIdentifierId
|
|
||||||
+ " of type " + deviceType);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new DeviceManagementDAOException("Error occurred while deleting the device " + deviceIdentifierId
|
String msg ="Error occurred while deleting the devices: " + deviceIdentifiers;
|
||||||
+ " of type " + deviceType, e);
|
log.error(msg,e);
|
||||||
|
throw new DeviceManagementDAOException(msg, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getDeviceId(Connection conn, DeviceIdentifier deviceIdentifier, int tenantId)
|
/***
|
||||||
throws DeviceManagementDAOException {
|
* This method removes records of a given list of devices from the DM_DEVICE_DETAIL table
|
||||||
PreparedStatement stmt = null;
|
* @param conn Connection object
|
||||||
ResultSet rs = null;
|
* @param deviceIds list of device ids (primary keys)
|
||||||
int deviceId = -1;
|
* @throws DeviceManagementDAOException if deletion fails
|
||||||
|
*/
|
||||||
|
private void removeDeviceDetail(Connection conn, List<Integer> deviceIds) throws DeviceManagementDAOException {
|
||||||
|
String sql = "DELETE FROM DM_DEVICE_DETAIL WHERE DEVICE_ID = ?";
|
||||||
try {
|
try {
|
||||||
String sql = "SELECT ID FROM DM_DEVICE WHERE DEVICE_IDENTIFICATION = ? AND TENANT_ID = ?";
|
executeBatchOperation(conn, sql, deviceIds);
|
||||||
stmt = conn.prepareStatement(sql);
|
} catch (SQLException e) {
|
||||||
stmt.setString(1, deviceIdentifier.getId());
|
String msg = "Error occurred while removing device details.";
|
||||||
stmt.setInt(2, tenantId);
|
log.error(msg, e);
|
||||||
rs = stmt.executeQuery();
|
throw new DeviceManagementDAOException(msg, e);
|
||||||
if (rs.next()) {
|
}
|
||||||
deviceId = rs.getInt("ID");
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* This method removes records of a given list of devices from the DM_DEVICE_LOCATION table
|
||||||
|
* @param conn Connection object
|
||||||
|
* @param deviceIds list of device ids (primary keys)
|
||||||
|
* @throws DeviceManagementDAOException if deletion fails
|
||||||
|
*/
|
||||||
|
private void removeDeviceLocation(Connection conn, List<Integer> deviceIds) throws DeviceManagementDAOException {
|
||||||
|
String sql = "DELETE FROM DM_DEVICE_LOCATION WHERE DEVICE_ID = ?";
|
||||||
|
try {
|
||||||
|
executeBatchOperation(conn, sql, deviceIds);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while obtaining locations of devices.";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementDAOException(msg, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* This method removes records of a given list of devices from the DM_DEVICE_INFO table
|
||||||
|
* @param conn Connection object
|
||||||
|
* @param deviceIds list of device ids (primary keys)
|
||||||
|
* @throws DeviceManagementDAOException if deletion fails
|
||||||
|
*/
|
||||||
|
private void removeDeviceInfo(Connection conn, List<Integer> deviceIds) throws DeviceManagementDAOException {
|
||||||
|
String sql = "DELETE FROM DM_DEVICE_INFO WHERE DEVICE_ID = ?";
|
||||||
|
try {
|
||||||
|
executeBatchOperation(conn, sql, deviceIds);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while removing device info.";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementDAOException(msg, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* This method removes records of a given list of devices from the DM_NOTIFICATION table
|
||||||
|
* @param conn Connection object
|
||||||
|
* @param deviceIds list of device ids (primary keys)
|
||||||
|
* @throws DeviceManagementDAOException if deletion fails
|
||||||
|
*/
|
||||||
|
private void removeDeviceNotification(Connection conn, List<Integer> deviceIds) throws DeviceManagementDAOException {
|
||||||
|
String sql = "DELETE FROM DM_NOTIFICATION WHERE DEVICE_ID = ?";
|
||||||
|
try {
|
||||||
|
executeBatchOperation(conn, sql, deviceIds);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while removing device notifications.";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementDAOException(msg, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* This method removes records of a given list of devices from the DM_DEVICE_APPLICATION_MAPPING table
|
||||||
|
* @param conn Connection object
|
||||||
|
* @param deviceIds list of device ids (primary keys)
|
||||||
|
* @throws DeviceManagementDAOException if deletion fails
|
||||||
|
*/
|
||||||
|
private void removeDeviceApplicationMapping(Connection conn, List<Integer> deviceIds)
|
||||||
|
throws DeviceManagementDAOException {
|
||||||
|
String sql = "DELETE FROM DM_DEVICE_APPLICATION_MAPPING WHERE DEVICE_ID = ?";
|
||||||
|
try {
|
||||||
|
executeBatchOperation(conn, sql, deviceIds);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while removing device application mapping";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementDAOException(msg, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* This method removes records of a given list of devices from the DM_DEVICE_POLICY_APPLIED table
|
||||||
|
* @param conn Connection object
|
||||||
|
* @param deviceIds list of device ids (primary keys)
|
||||||
|
* @throws DeviceManagementDAOException if deletion fails
|
||||||
|
*/
|
||||||
|
private void removeDevicePolicyApplied(Connection conn, List<Integer> deviceIds)
|
||||||
|
throws DeviceManagementDAOException {
|
||||||
|
String sql = "DELETE FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ?";
|
||||||
|
try {
|
||||||
|
executeBatchOperation(conn, sql, deviceIds);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while removing policies applied on devices";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementDAOException(msg, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* This method removes records of a given list of devices from the DM_DEVICE_POLICY table
|
||||||
|
* @param conn Connection object
|
||||||
|
* @param deviceIds list of device ids (primary keys)
|
||||||
|
* @throws DeviceManagementDAOException if deletion fails
|
||||||
|
*/
|
||||||
|
private void removeDevicePolicy(Connection conn, List<Integer> deviceIds) throws DeviceManagementDAOException {
|
||||||
|
String sql = "DELETE FROM DM_DEVICE_POLICY WHERE DEVICE_ID = ?";
|
||||||
|
try {
|
||||||
|
executeBatchOperation(conn, sql, deviceIds);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while removing policies of devices";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementDAOException(msg, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* This method removes records of a given list of enrollments from the DM_DEVICE_DETAIL table
|
||||||
|
* @param conn Connection object
|
||||||
|
* @param enrollmentIds list of enrollment ids (primary keys)
|
||||||
|
* @throws DeviceManagementDAOException if deletion fails
|
||||||
|
*/
|
||||||
|
private void removeEnrollmentDeviceDetail(Connection conn, List<Integer> enrollmentIds)
|
||||||
|
throws DeviceManagementDAOException {
|
||||||
|
String sql = "DELETE FROM DM_DEVICE_DETAIL WHERE ENROLMENT_ID = ?";
|
||||||
|
try {
|
||||||
|
executeBatchOperation(conn, sql, enrollmentIds);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while removing enrollment details of devices";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementDAOException(msg, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* This method removes records of a given list of enrollments from the DM_DEVICE_LOCATION table
|
||||||
|
* @param conn Connection object
|
||||||
|
* @param enrollmentIds list of enrollment ids (primary keys)
|
||||||
|
* @throws DeviceManagementDAOException if deletion fails
|
||||||
|
*/
|
||||||
|
private void removeEnrollmentDeviceLocation(Connection conn, List<Integer> enrollmentIds)
|
||||||
|
throws DeviceManagementDAOException {
|
||||||
|
String sql = "DELETE FROM DM_DEVICE_LOCATION WHERE ENROLMENT_ID = ?";
|
||||||
|
try {
|
||||||
|
executeBatchOperation(conn, sql, enrollmentIds);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while removing enrollment locations of devices";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementDAOException(msg, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* This method removes records of a given list of enrollments from the DM_DEVICE_INFO table
|
||||||
|
* @param conn Connection object
|
||||||
|
* @param enrollmentIds list of enrollment ids (primary keys)
|
||||||
|
* @throws DeviceManagementDAOException if deletion fails
|
||||||
|
*/
|
||||||
|
private void removeEnrollmentDeviceInfo(Connection conn, List<Integer> enrollmentIds)
|
||||||
|
throws DeviceManagementDAOException {
|
||||||
|
String sql = "DELETE FROM DM_DEVICE_INFO WHERE ENROLMENT_ID = ?";
|
||||||
|
try {
|
||||||
|
executeBatchOperation(conn, sql, enrollmentIds);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while removing enrollment info of devices";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementDAOException(msg, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* This method removes records of a given list of enrollments from the DM_DEVICE_APPLICATION_MAPPING table
|
||||||
|
* @param conn Connection object
|
||||||
|
* @param enrollmentIds list of enrollment ids (primary keys)
|
||||||
|
* @throws DeviceManagementDAOException if deletion fails
|
||||||
|
*/
|
||||||
|
private void removeEnrollmentDeviceApplicationMapping(Connection conn, List<Integer> enrollmentIds)
|
||||||
|
throws DeviceManagementDAOException {
|
||||||
|
String sql = "DELETE FROM DM_DEVICE_APPLICATION_MAPPING WHERE ENROLMENT_ID = ?";
|
||||||
|
try {
|
||||||
|
executeBatchOperation(conn, sql, enrollmentIds);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while removing enrollment device application mapping";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementDAOException(msg, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* This method removes records of a given list of enrollments from the DM_DEVICE_OPERATION_RESPONSE table
|
||||||
|
* @param conn Connection object
|
||||||
|
* @param enrollmentIds list of enrollment ids (primary keys)
|
||||||
|
* @throws DeviceManagementDAOException if deletion fails
|
||||||
|
*/
|
||||||
|
private void removeDeviceOperationResponse(Connection conn, List<Integer> enrollmentIds)
|
||||||
|
throws DeviceManagementDAOException {
|
||||||
|
String sql = "DELETE FROM DM_DEVICE_OPERATION_RESPONSE WHERE ENROLMENT_ID = ?";
|
||||||
|
try {
|
||||||
|
executeBatchOperation(conn, sql, enrollmentIds);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while removing device operation response";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementDAOException(msg, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* This method removes records of a given list of enrollments from the DM_ENROLMENT_OP_MAPPING table
|
||||||
|
* @param conn Connection object
|
||||||
|
* @param enrollmentIds list of enrollment ids (primary keys)
|
||||||
|
* @throws DeviceManagementDAOException if deletion fails
|
||||||
|
*/
|
||||||
|
private void removeEnrollmentOperationMapping(Connection conn, List<Integer> enrollmentIds)
|
||||||
|
throws DeviceManagementDAOException {
|
||||||
|
String sql = "DELETE FROM DM_ENROLMENT_OP_MAPPING WHERE ENROLMENT_ID = ?";
|
||||||
|
try {
|
||||||
|
executeBatchOperation(conn, sql, enrollmentIds);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while removing enrollment operation mapping";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementDAOException(msg, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* This method removes records of a given list of enrollments from the DM_ENROLMENT table
|
||||||
|
* @param conn Connection object
|
||||||
|
* @param enrollmentIds list of enrollment ids (primary keys)
|
||||||
|
* @throws DeviceManagementDAOException if deletion fails
|
||||||
|
*/
|
||||||
|
private void removeDeviceEnrollment(Connection conn, List<Integer> enrollmentIds)
|
||||||
|
throws DeviceManagementDAOException {
|
||||||
|
String sql = "DELETE FROM DM_ENROLMENT WHERE DEVICE_ID = ?";
|
||||||
|
try {
|
||||||
|
executeBatchOperation(conn, sql, enrollmentIds);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while removing enrollments of devices";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementDAOException(msg, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* This method removes records of a given list of devices from the DM_DEVICE_GROUP_MAP table
|
||||||
|
* @param conn Connection object
|
||||||
|
* @param deviceIds list of device ids (primary keys)
|
||||||
|
* @throws DeviceManagementDAOException if deletion fails
|
||||||
|
*/
|
||||||
|
private void removeDeviceGroupMapping(Connection conn, List<Integer> deviceIds) throws DeviceManagementDAOException {
|
||||||
|
String sql = "DELETE FROM DM_DEVICE_GROUP_MAP WHERE DEVICE_ID = ?";
|
||||||
|
try {
|
||||||
|
executeBatchOperation(conn, sql, deviceIds);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while removing device group mapping";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementDAOException(msg, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* This method removes records of a given list of devices from the DM_DEVICE table
|
||||||
|
* @param conn Connection object
|
||||||
|
* @param deviceIds list of device ids (primary keys)
|
||||||
|
* @throws DeviceManagementDAOException if deletion fails
|
||||||
|
*/
|
||||||
|
private void removeDevice(Connection conn, List<Integer> deviceIds) throws DeviceManagementDAOException {
|
||||||
|
String sql = "DELETE FROM DM_DEVICE WHERE ID = ?";
|
||||||
|
try {
|
||||||
|
executeBatchOperation(conn, sql, deviceIds);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while removing devices.";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementDAOException(msg, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* This method executes batch operations for a given list of primary keys
|
||||||
|
* where the statement only has one param of type int, following the given pattern:
|
||||||
|
* DELETE FROM TABLE WHERE ID = ?
|
||||||
|
* @param sql SQL statement
|
||||||
|
* @param conn Connection object
|
||||||
|
* @param identifiers list of device ids (primary keys)
|
||||||
|
* @throws SQLException if deletion fails.
|
||||||
|
*/
|
||||||
|
private void executeBatchOperation(Connection conn, String sql, List<Integer> identifiers) throws SQLException {
|
||||||
|
try (PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||||
|
if (conn.getMetaData().supportsBatchUpdates()) {
|
||||||
|
for (int identifier : identifiers) {
|
||||||
|
ps.setInt(1, identifier);
|
||||||
|
ps.addBatch();
|
||||||
|
}
|
||||||
|
for (int i : ps.executeBatch()) {
|
||||||
|
if (i == 0 || i == Statement.SUCCESS_NO_INFO || i == Statement.EXECUTE_FAILED) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (int enrollmentId : identifiers) {
|
||||||
|
ps.setInt(1, enrollmentId);
|
||||||
|
if (ps.executeUpdate() == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new DeviceManagementDAOException("Error occurred while retrieving device id of the device", e);
|
|
||||||
} finally {
|
|
||||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
|
||||||
}
|
|
||||||
return deviceId;
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<Integer> getEnrollmentIds(Connection conn, int deviceId, int tenantId) throws DeviceManagementDAOException {
|
|
||||||
PreparedStatement stmt = null;
|
|
||||||
ResultSet rs = null;
|
|
||||||
List<Integer> enrollmentIds = new ArrayList<>();
|
|
||||||
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();
|
|
||||||
while (rs.next()) {
|
|
||||||
enrollmentIds.add(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 enrollmentIds;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void removeDeviceDetail(Connection conn, int deviceId) throws DeviceManagementDAOException {
|
|
||||||
PreparedStatement stmt = 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, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void removeDeviceLocation(Connection conn, int deviceId) throws DeviceManagementDAOException {
|
|
||||||
PreparedStatement stmt = 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, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void removeDeviceInfo(Connection conn, int deviceId) throws DeviceManagementDAOException {
|
|
||||||
PreparedStatement stmt = 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, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void removeDeviceNotification(Connection conn, int deviceId) throws DeviceManagementDAOException {
|
|
||||||
PreparedStatement stmt = 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, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void removeDeviceApplicationMapping(Connection conn, int deviceId) throws DeviceManagementDAOException {
|
|
||||||
PreparedStatement stmt = 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, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void removeDevicePolicyApplied(Connection conn, int deviceId) throws DeviceManagementDAOException {
|
|
||||||
PreparedStatement stmt = 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, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void removeDevicePolicy(Connection conn, int deviceId) throws DeviceManagementDAOException {
|
|
||||||
PreparedStatement stmt = 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, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void removeEnrollmentDeviceDetail(Connection conn, int enrollmentId) throws DeviceManagementDAOException {
|
|
||||||
PreparedStatement stmt = 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, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void removeEnrollmentDeviceLocation(Connection conn, int enrollmentId) throws DeviceManagementDAOException {
|
|
||||||
PreparedStatement stmt = 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, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void removeEnrollmentDeviceInfo(Connection conn, int enrollmentId) throws DeviceManagementDAOException {
|
|
||||||
PreparedStatement stmt = 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, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void removeEnrollmentDeviceApplicationMapping(Connection conn, int enrollmentId)
|
|
||||||
throws DeviceManagementDAOException {
|
|
||||||
PreparedStatement stmt = 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, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void removeDeviceOperationResponse(Connection conn, int enrollmentId) throws DeviceManagementDAOException {
|
|
||||||
PreparedStatement stmt = 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, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void removeEnrollmentOperationMapping(Connection conn, int enrollmentId)
|
|
||||||
throws DeviceManagementDAOException {
|
|
||||||
PreparedStatement stmt = 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, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void removeDeviceEnrollment(Connection conn, int deviceId) throws DeviceManagementDAOException {
|
|
||||||
PreparedStatement stmt = 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, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void removeDeviceGroupMapping(Connection conn, int deviceId) throws DeviceManagementDAOException {
|
|
||||||
PreparedStatement stmt = 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, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void removeDevice(Connection conn, int deviceId) throws DeviceManagementDAOException {
|
|
||||||
PreparedStatement stmt = 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, null);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -597,7 +597,7 @@ public interface DeviceManagementProviderService {
|
|||||||
|
|
||||||
boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException;
|
boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException;
|
||||||
|
|
||||||
boolean deleteDevice(DeviceIdentifier deviceId) throws DeviceManagementException;
|
boolean deleteDevices(List<String> deviceIdentifiers) throws DeviceManagementException, InvalidDeviceException;
|
||||||
|
|
||||||
boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException;
|
boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException;
|
||||||
|
|
||||||
|
|||||||
@ -99,6 +99,7 @@ import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy;
|
|||||||
import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService;
|
import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService;
|
||||||
import org.wso2.carbon.device.mgt.core.DeviceManagementConstants;
|
import org.wso2.carbon.device.mgt.core.DeviceManagementConstants;
|
||||||
import org.wso2.carbon.device.mgt.core.DeviceManagementPluginRepository;
|
import org.wso2.carbon.device.mgt.core.DeviceManagementPluginRepository;
|
||||||
|
import org.wso2.carbon.device.mgt.core.cache.DeviceCacheKey;
|
||||||
import org.wso2.carbon.device.mgt.core.cache.impl.DeviceCacheManagerImpl;
|
import org.wso2.carbon.device.mgt.core.cache.impl.DeviceCacheManagerImpl;
|
||||||
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
|
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
|
||||||
import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig;
|
import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig;
|
||||||
@ -142,6 +143,7 @@ import java.util.Enumeration;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@ -519,72 +521,90 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean deleteDevice(DeviceIdentifier deviceId) throws DeviceManagementException {
|
public boolean deleteDevices(List<String> deviceIdentifiers) throws DeviceManagementException, InvalidDeviceException {
|
||||||
if (deviceId == null) {
|
List<Integer> deviceIds = new ArrayList<>();
|
||||||
String msg = "Required values are not set to permanently delete device";
|
List<Integer> enrollmentIds = new ArrayList<>();
|
||||||
log.error(msg);
|
Map<String, List<String>> deviceIdentifierMap = new HashMap<>();
|
||||||
throw new DeviceManagementException(msg);
|
Map<String, DeviceManager> deviceManagerMap = new HashMap<>();
|
||||||
}
|
List<DeviceCacheKey> deviceCacheKeyList = new ArrayList<>();
|
||||||
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();
|
int tenantId = this.getTenantId();
|
||||||
|
List<Device> existingDevices;
|
||||||
Device device = this.getDevice(deviceId, false);
|
try {
|
||||||
if (device == null) {
|
DeviceManagementDAOFactory.beginTransaction();
|
||||||
if (log.isDebugEnabled()) {
|
existingDevices = deviceDAO.getDevicesByIdentifiers(deviceIdentifiers, tenantId);
|
||||||
log.debug("Device not found for id '" + deviceId.getId() + "'");
|
if (existingDevices.size() != deviceIdentifiers.size()) {
|
||||||
|
for (Device device : existingDevices) {
|
||||||
|
deviceIdentifiers.remove(device.getDeviceIdentifier());
|
||||||
|
}
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
return false;
|
for (Device device : existingDevices) {
|
||||||
}
|
if (!device.getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.REMOVED)) {
|
||||||
|
String msg = "Device " + device.getDeviceIdentifier() + " of type " + device.getType()
|
||||||
if (!device.getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.REMOVED)) {
|
+ " is not dis-enrolled to permanently delete the device";
|
||||||
String msg = "Device " + deviceId.getId() + " of type " + deviceId.getType() + " is not dis-enrolled to " +
|
log.error(msg);
|
||||||
"permanently delete the device";
|
DeviceManagementDAOFactory.rollbackTransaction();
|
||||||
log.error(msg);
|
throw new InvalidDeviceException(msg);
|
||||||
throw new DeviceManagementException(msg);
|
}
|
||||||
} else {
|
DeviceCacheKey deviceCacheKey = new DeviceCacheKey();
|
||||||
try {
|
deviceCacheKey.setDeviceId(device.getDeviceIdentifier());
|
||||||
DeviceManagementDAOFactory.beginTransaction();
|
deviceCacheKey.setDeviceType(device.getType());
|
||||||
deviceDAO.deleteDevice(deviceId, tenantId);
|
deviceCacheKey.setTenantId(tenantId);
|
||||||
|
deviceCacheKeyList.add(deviceCacheKey);
|
||||||
|
deviceIds.add(device.getId());
|
||||||
|
enrollmentIds.add(device.getEnrolmentInfo().getId());
|
||||||
|
if (deviceIdentifierMap.containsKey(device.getType())) {
|
||||||
|
deviceIdentifierMap.get(device.getType()).add(device.getDeviceIdentifier());
|
||||||
|
} else {
|
||||||
|
deviceIdentifierMap.put(device.getType(),
|
||||||
|
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'");
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
deviceManagerMap.put(device.getType(), deviceManager);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//deleting device from the core
|
||||||
|
deviceDAO.deleteDevices(deviceIdentifiers, deviceIds, enrollmentIds);
|
||||||
|
for (Map.Entry<String, DeviceManager> entry : deviceManagerMap.entrySet()) {
|
||||||
try {
|
try {
|
||||||
deviceManager.deleteDevice(deviceId, device);
|
// deleting device from the plugin level
|
||||||
|
entry.getValue().deleteDevices(deviceIdentifierMap.get(entry.getKey()));
|
||||||
} catch (DeviceManagementException e) {
|
} catch (DeviceManagementException e) {
|
||||||
String msg = "Error occurred while permanently deleting '" + deviceId.getType() +
|
String msg = "Error occurred while permanently deleting '" + entry.getKey() +
|
||||||
"' device with the identifier '" + deviceId.getId() + "' in plugin.";
|
"' devices with the identifiers: '" + deviceIdentifierMap.get(entry.getKey())
|
||||||
|
+ "' in plugin.";
|
||||||
log.error(msg, e);
|
log.error(msg, e);
|
||||||
|
// a DeviceManagementException is thrown when the device deletion fails from the plugin level.
|
||||||
|
// Here, that exception is caught and a DeviceManagementDAOException is thrown
|
||||||
throw new DeviceManagementDAOException(msg, e);
|
throw new DeviceManagementDAOException(msg, e);
|
||||||
}
|
}
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
DeviceManagementDAOFactory.commitTransaction();
|
||||||
|
this.removeDevicesFromCache(deviceCacheKeyList);
|
||||||
|
return true;
|
||||||
|
} catch (TransactionManagementException e) {
|
||||||
|
String msg = "Error occurred while initiating transaction";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
} catch (DeviceManagementDAOException e) {
|
||||||
|
DeviceManagementDAOFactory.rollbackTransaction();
|
||||||
|
String msg = "Error occurred while permanently deleting '" + deviceIdentifiers +
|
||||||
|
"' devices";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -2976,6 +2996,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
|
|||||||
DeviceCacheManagerImpl.getInstance().removeDeviceFromCache(deviceIdentifier, this.getTenantId());
|
DeviceCacheManagerImpl.getInstance().removeDeviceFromCache(deviceIdentifier, this.getTenantId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* This method removes a given list of devices from the cache
|
||||||
|
* @param deviceList list of DeviceCacheKey objects
|
||||||
|
*/
|
||||||
|
private void removeDevicesFromCache(List<DeviceCacheKey> deviceList) {
|
||||||
|
DeviceCacheManagerImpl.getInstance().removeDevicesFromCache(deviceList);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<GeoCluster> findGeoClusters(String deviceType, GeoCoordinate southWest, GeoCoordinate northEast,
|
public List<GeoCluster> findGeoClusters(String deviceType, GeoCoordinate southWest, GeoCoordinate northEast,
|
||||||
int geohashLength) throws DeviceManagementException {
|
int geohashLength) throws DeviceManagementException {
|
||||||
|
|||||||
@ -587,31 +587,36 @@ public class DeviceTypeManager implements DeviceManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean deleteDevice(DeviceIdentifier deviceIdentifier, Device device) throws DeviceManagementException {
|
public void deleteDevices(List<String> deviceIdentifierList) throws DeviceManagementException {
|
||||||
if (propertiesExist) {
|
if (propertiesExist) {
|
||||||
boolean status;
|
|
||||||
Device existingDevice = this.getDevice(deviceIdentifier);
|
|
||||||
if (existingDevice == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
if (log.isDebugEnabled()) {
|
if (log.isDebugEnabled()) {
|
||||||
log.debug("Deleting the details of " + deviceType + " device : " + device.getDeviceIdentifier());
|
log.debug("Deleting the details of " + deviceType + " devices : " + deviceIdentifierList);
|
||||||
}
|
}
|
||||||
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().beginTransaction();
|
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().beginTransaction();
|
||||||
status = deviceTypePluginDAOManager.getDeviceDAO().deleteDevice(existingDevice);
|
if (deviceTypePluginDAOManager.getDeviceDAO().deleteDevices(deviceIdentifierList)) {
|
||||||
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().commitTransaction();
|
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().commitTransaction();
|
||||||
|
} else {
|
||||||
|
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().rollbackTransaction();
|
||||||
|
String msg = "Error occurred while deleting the " + deviceType + " devices: '" +
|
||||||
|
deviceIdentifierList;
|
||||||
|
log.error(msg);
|
||||||
|
throw new DeviceManagementException(msg);
|
||||||
|
}
|
||||||
} catch (DeviceTypeMgtPluginException e) {
|
} catch (DeviceTypeMgtPluginException e) {
|
||||||
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().rollbackTransaction();
|
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().rollbackTransaction();
|
||||||
throw new DeviceManagementException(
|
if (log.isDebugEnabled()) {
|
||||||
"Error occurred while deleting the " + deviceType + " device: '" +
|
log.debug("Error occurred while deleting the " + deviceType + " devices: '" +
|
||||||
device.getDeviceIdentifier() + "'", e);
|
deviceIdentifierList + "'. Transaction rolled back");
|
||||||
|
}
|
||||||
|
String msg= "Error occurred while deleting the " + deviceType + " devices: '" +
|
||||||
|
deviceIdentifierList;
|
||||||
|
log.error(msg,e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
} finally {
|
} finally {
|
||||||
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().closeConnection();
|
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().closeConnection();
|
||||||
}
|
}
|
||||||
return status;
|
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -39,6 +39,7 @@ import org.apache.commons.lang.StringUtils;
|
|||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.wso2.carbon.device.mgt.common.Device;
|
import org.wso2.carbon.device.mgt.common.Device;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
||||||
import org.wso2.carbon.device.mgt.extensions.device.type.template.exception.DeviceTypeMgtPluginException;
|
import org.wso2.carbon.device.mgt.extensions.device.type.template.exception.DeviceTypeMgtPluginException;
|
||||||
import org.wso2.carbon.device.mgt.extensions.device.type.template.util.DeviceTypeUtils;
|
import org.wso2.carbon.device.mgt.extensions.device.type.template.util.DeviceTypeUtils;
|
||||||
|
|
||||||
@ -46,6 +47,7 @@ import java.sql.Connection;
|
|||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Statement;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -215,30 +217,39 @@ public class DeviceTypePluginDAOImpl implements PluginDAO {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean deleteDevice(Device device) throws DeviceTypeMgtPluginException {
|
public boolean deleteDevices(List<String> deviceIdentifiers) throws DeviceTypeMgtPluginException {
|
||||||
boolean status = false;
|
|
||||||
Connection conn;
|
|
||||||
PreparedStatement stmt = null;
|
|
||||||
try {
|
try {
|
||||||
conn = deviceTypeDAOHandler.getConnection();
|
Connection conn = deviceTypeDAOHandler.getConnection();
|
||||||
stmt = conn.prepareStatement(deleteDBQueryForDeleteDevice);
|
boolean status = true;
|
||||||
stmt.setString(1, device.getDeviceIdentifier());
|
try (PreparedStatement ps = conn.prepareStatement(deleteDBQueryForDeleteDevice)) {
|
||||||
int rows = stmt.executeUpdate();
|
if (conn.getMetaData().supportsBatchUpdates()) {
|
||||||
if (rows > 0) {
|
for (String deviceId : deviceIdentifiers) {
|
||||||
status = true;
|
ps.setString(1, deviceId);
|
||||||
if (log.isDebugEnabled()) {
|
ps.addBatch();
|
||||||
log.debug("Device " + device.getDeviceIdentifier() + " data has been deleted.");
|
}
|
||||||
|
for (int i : ps.executeBatch()) {
|
||||||
|
if (i == 0 || i == Statement.SUCCESS_NO_INFO || i == Statement.EXECUTE_FAILED) {
|
||||||
|
status = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (String deviceId : deviceIdentifiers) {
|
||||||
|
ps.setString(1, deviceId);
|
||||||
|
if (ps.executeUpdate() == 0) {
|
||||||
|
status = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return status;
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
String msg = "Error occurred while deleting the device '" + device.getDeviceIdentifier() + "' data in "
|
String msg = "Error occurred while deleting the data in "
|
||||||
+ deviceDAODefinition.getDeviceTableName();
|
+ deviceDAODefinition.getDeviceTableName();
|
||||||
log.error(msg, e);
|
log.error(msg, e);
|
||||||
throw new DeviceTypeMgtPluginException(msg, e);
|
throw new DeviceTypeMgtPluginException(msg, e);
|
||||||
} finally {
|
|
||||||
DeviceTypeUtils.cleanupResources(stmt, null);
|
|
||||||
}
|
}
|
||||||
return status;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getDeviceTableColumnNames() {
|
private String getDeviceTableColumnNames() {
|
||||||
|
|||||||
@ -50,5 +50,5 @@ public interface PluginDAO {
|
|||||||
|
|
||||||
List<Device> getAllDevices() throws DeviceTypeMgtPluginException;
|
List<Device> getAllDevices() throws DeviceTypeMgtPluginException;
|
||||||
|
|
||||||
boolean deleteDevice(Device device) throws DeviceTypeMgtPluginException;
|
boolean deleteDevices(List<String> deviceIdentifiers) throws DeviceTypeMgtPluginException;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -46,6 +46,7 @@ import org.wso2.carbon.device.mgt.extensions.device.type.template.util.DeviceTyp
|
|||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.Statement;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -218,22 +219,38 @@ public class PropertyBasedPluginDAOImpl implements PluginDAO {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean deleteDevice(Device device) throws DeviceTypeMgtPluginException {
|
public boolean deleteDevices(List<String> deviceIdentifiers) throws DeviceTypeMgtPluginException {
|
||||||
Connection conn;
|
|
||||||
PreparedStatement stmt = null;
|
|
||||||
try {
|
try {
|
||||||
conn = deviceTypeDAOHandler.getConnection();
|
Connection conn = deviceTypeDAOHandler.getConnection();
|
||||||
stmt = conn.prepareStatement("DELETE FROM DM_DEVICE_PROPERTIES WHERE DEVICE_IDENTIFICATION = ?");
|
boolean status = true;
|
||||||
stmt.setString(1, device.getDeviceIdentifier());
|
try (PreparedStatement ps = conn.prepareStatement("DELETE FROM DM_DEVICE_PROPERTIES WHERE DEVICE_IDENTIFICATION = ?")) {
|
||||||
stmt.executeUpdate();
|
if (conn.getMetaData().supportsBatchUpdates()) {
|
||||||
return true;
|
for (String deviceId : deviceIdentifiers) {
|
||||||
|
ps.setString(1, deviceId);
|
||||||
|
ps.addBatch();
|
||||||
|
}
|
||||||
|
for (int i : ps.executeBatch()) {
|
||||||
|
if (i == 0 || i == Statement.SUCCESS_NO_INFO || i == Statement.EXECUTE_FAILED) {
|
||||||
|
status = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (String deviceId : deviceIdentifiers) {
|
||||||
|
ps.setString(1, deviceId);
|
||||||
|
if (ps.executeUpdate() == 0) {
|
||||||
|
status = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return status;
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
String msg = "Error occurred while deleting the device '" + device.getDeviceIdentifier() + "' data on"
|
String msg = "Error occurred while deleting the data of the devices: '" + deviceIdentifiers + "'of type: "
|
||||||
+ deviceType;
|
+ deviceType;
|
||||||
log.error(msg, e);
|
log.error(msg, e);
|
||||||
throw new DeviceTypeMgtPluginException(msg, e);
|
throw new DeviceTypeMgtPluginException(msg, e);
|
||||||
} finally {
|
|
||||||
DeviceTypeUtils.cleanupResources(stmt, null);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user