Minor fixes and improvement for permanent delete devices

3.x.x
Saad Sahibjan 5 years ago
parent 1f907ea283
commit f9660de334

@ -533,5 +533,6 @@ public interface DeviceDAO {
* @param enrollmentIds list of enrollment ids. * @param enrollmentIds list of enrollment ids.
* @throws DeviceManagementDAOException when no enrolments are found for the given device. * @throws DeviceManagementDAOException when no enrolments are found for the given device.
*/ */
void deleteDevices(List<String> deviceIdentifiers, List<Integer> deviceIds, List<Integer> enrollmentIds) throws DeviceManagementDAOException; void deleteDevices(List<String> deviceIdentifiers, List<Integer> deviceIds, List<Integer> enrollmentIds)
throws DeviceManagementDAOException;
} }

@ -522,43 +522,39 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} }
@Override @Override
public boolean deleteDevices(List<String> deviceIdentifiers) throws DeviceManagementException, InvalidDeviceException { public boolean deleteDevices(List<String> deviceIdentifiers) throws DeviceManagementException,
InvalidDeviceException {
if (deviceIdentifiers == null || deviceIdentifiers.isEmpty()) {
String msg = "Required values of device identifiers are not set to permanently delete device/s.";
log.error(msg);
throw new InvalidDeviceException(msg);
}
HashSet<Integer> deviceIds = new HashSet<>(); HashSet<Integer> deviceIds = new HashSet<>();
List<Integer> enrollmentIds = new ArrayList<>(); List<Integer> enrollmentIds = new ArrayList<>();
List<String> validDeviceIdentifiers = new ArrayList<>();
Map<String, List<String>> deviceIdentifierMap = new HashMap<>(); Map<String, List<String>> deviceIdentifierMap = new HashMap<>();
Map<String, DeviceManager> deviceManagerMap = new HashMap<>(); Map<String, DeviceManager> deviceManagerMap = new HashMap<>();
List<DeviceCacheKey> deviceCacheKeyList = new ArrayList<>(); List<DeviceCacheKey> deviceCacheKeyList = new ArrayList<>();
int tenantId = this.getTenantId(); int tenantId = this.getTenantId();
List<Device> existingDevices;
try { try {
DeviceManagementDAOFactory.beginTransaction(); DeviceManagementDAOFactory.beginTransaction();
existingDevices = deviceDAO.getDevicesByIdentifiers(deviceIdentifiers, tenantId); List<Device> existingDevices = deviceDAO.getDevicesByIdentifiers(deviceIdentifiers, tenantId);
DeviceCacheKey deviceCacheKey;
for (Device device : existingDevices) { for (Device device : existingDevices) {
deviceIdentifiers.remove(device.getDeviceIdentifier()); if (!EnrolmentInfo.Status.REMOVED.equals(device.getEnrolmentInfo().getStatus())) {
}
if (!deviceIdentifiers.isEmpty()) {
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(); DeviceManagementDAOFactory.rollbackTransaction();
throw new InvalidDeviceException(msg);
}
for (Device device : existingDevices) {
if (!device.getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.REMOVED)) {
String msg = "Device " + device.getDeviceIdentifier() + " of type " + device.getType() String msg = "Device " + device.getDeviceIdentifier() + " of type " + device.getType()
+ " is not dis-enrolled to permanently delete the device"; + " is not dis-enrolled to permanently delete the device";
log.error(msg); log.error(msg);
DeviceManagementDAOFactory.rollbackTransaction();
throw new InvalidDeviceException(msg); throw new InvalidDeviceException(msg);
} }
DeviceCacheKey deviceCacheKey = new DeviceCacheKey(); deviceCacheKey = new DeviceCacheKey();
deviceCacheKey.setDeviceId(device.getDeviceIdentifier()); deviceCacheKey.setDeviceId(device.getDeviceIdentifier());
deviceCacheKey.setDeviceType(device.getType()); deviceCacheKey.setDeviceType(device.getType());
deviceCacheKey.setTenantId(tenantId); deviceCacheKey.setTenantId(tenantId);
deviceCacheKeyList.add(deviceCacheKey); deviceCacheKeyList.add(deviceCacheKey);
deviceIds.add(device.getId()); deviceIds.add(device.getId());
validDeviceIdentifiers.add(device.getDeviceIdentifier());
enrollmentIds.add(device.getEnrolmentInfo().getId()); enrollmentIds.add(device.getEnrolmentInfo().getId());
if (deviceIdentifierMap.containsKey(device.getType())) { if (deviceIdentifierMap.containsKey(device.getType())) {
deviceIdentifierMap.get(device.getType()).add(device.getDeviceIdentifier()); deviceIdentifierMap.get(device.getType()).add(device.getDeviceIdentifier());
@ -567,17 +563,23 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
new ArrayList<>(Arrays.asList(device.getDeviceIdentifier()))); new ArrayList<>(Arrays.asList(device.getDeviceIdentifier())));
DeviceManager deviceManager = this.getDeviceManager(device.getType()); DeviceManager deviceManager = this.getDeviceManager(device.getType());
if (deviceManager == null) { if (deviceManager == null) {
if (log.isDebugEnabled()) { log.error("Device Manager associated with the device type '" +device.getType() +
log.debug("Device Manager associated with the device type '" "' is null. Therefore, not attempting method 'deleteDevice'");
+ device.getType() + "' is null. Therefore, not attempting method 'deleteDevice'");
}
return false; return false;
} }
deviceManagerMap.put(device.getType(), deviceManager); deviceManagerMap.put(device.getType(), deviceManager);
} }
} }
if (deviceIds.isEmpty()) {
String msg = "No device IDs found for the device identifiers '" + deviceIdentifiers + "'";
log.error(msg);
throw new InvalidDeviceException(msg);
}
if (log.isDebugEnabled()) {
log.debug("Permanently deleting the details of devices : " + validDeviceIdentifiers);
}
//deleting device from the core //deleting device from the core
deviceDAO.deleteDevices(deviceIdentifiers, new ArrayList<>(deviceIds), enrollmentIds); deviceDAO.deleteDevices(validDeviceIdentifiers, new ArrayList<>(deviceIds), enrollmentIds);
for (Map.Entry<String, DeviceManager> entry : deviceManagerMap.entrySet()) { for (Map.Entry<String, DeviceManager> entry : deviceManagerMap.entrySet()) {
try { try {
// deleting device from the plugin level // deleting device from the plugin level
@ -594,6 +596,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} }
DeviceManagementDAOFactory.commitTransaction(); DeviceManagementDAOFactory.commitTransaction();
this.removeDevicesFromCache(deviceCacheKeyList); this.removeDevicesFromCache(deviceCacheKeyList);
if (log.isDebugEnabled()) {
log.debug("Successfully permanently deleted the details of devices : " + validDeviceIdentifiers);
}
return true; return true;
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
String msg = "Error occurred while initiating transaction"; String msg = "Error occurred while initiating transaction";

@ -900,6 +900,8 @@ function attachDeviceEvents() {
var deviceId = $(this).data("deviceid"); var deviceId = $(this).data("deviceid");
if (deviceId) { if (deviceId) {
deviceIdentifiers = [deviceId]; deviceIdentifiers = [deviceId];
$(modalPopupContent).html($('#delete-device-modal-content').html());
showPopup();
} else { } else {
var selectedDevices = getSelectedDevices(); var selectedDevices = getSelectedDevices();
if (selectedDevices.length == 0) { if (selectedDevices.length == 0) {
@ -932,8 +934,6 @@ function attachDeviceEvents() {
} }
} }
$(modalPopupContent).html($('#delete-device-modal-content').html());
showPopup();
$("a#delete-device-yes-link").click(function () { $("a#delete-device-yes-link").click(function () {
deleteDevices(deviceIdentifiers); deleteDevices(deviceIdentifiers);

Loading…
Cancel
Save