Update device type delete service to reduce generality of the method by specifying different methods for each single responsibility

feature/appm-store/pbac
Yohan Avishke 5 years ago
parent b76a838dea
commit 1c2d1f52a0

@ -772,6 +772,24 @@ public interface DeviceManagementProviderService {
DeviceTypeVersion getDeviceTypeVersion(String deviceTypeName, String version) throws
DeviceManagementException;
/**
* Remove all versions of a device type
*
* @param deviceType Device type object
* @return True if device type versions are removed
* @throws DeviceManagementException Will be thrown if any service level or DAO level error occurs
*/
boolean deleteDeviceTypeVersions(DeviceType deviceType)
throws DeviceManagementException;
/**
* Dis-enroll all devices passed
*
* @param devices List of devices to dis-enroll
* @throws DeviceManagementException Will be thrown if any service level or DAO level error occurs
*/
void disEnrollDevices(List<Device> devices) throws DeviceManagementException;
/**
* Permanently delete a device type with all it's devices
*

@ -153,6 +153,7 @@ import java.util.List;
import java.util.Arrays;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;
public class DeviceManagementProviderServiceImpl implements DeviceManagementProviderService,
PluginInitializationListener {
@ -3604,109 +3605,150 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
public boolean deleteDeviceType(String deviceTypeName, DeviceType deviceType)
throws DeviceManagementException {
List<String> deviceIdentifiers;
List<DeviceTypeVersion> deviceTypeVersions;
boolean result;
int tenantId = getTenantId();
try {
if (deviceType == null || deviceTypeName.isEmpty()) {
if (deviceType == null || deviceTypeName == null || deviceTypeName.isEmpty()) {
String msg = "Error, device type cannot be null or empty";
log.error(msg);
return false;
}
DeviceManagementDAOFactory.beginTransaction();
List<Device> devices = deviceDAO.getDevices(deviceTypeName, this.getTenantId());
List<Device> devices = getAllDevices(deviceTypeName, false);
if (devices == null || devices.isEmpty()) {
if (log.isDebugEnabled()) {
log.debug("No devices found for the device type: " + deviceTypeName);
}
} else {
// dis-enroll devices
deviceIdentifiers = new ArrayList<>();
for (Device device : devices) {
if (device.getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.REMOVED)) {
if (log.isDebugEnabled()) {
log.debug("Device: " + device.getName() + " has already dis-enrolled");
}
} else {
device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime());
device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.REMOVED);
// different try blocks are used to isolate transactions
try {
enrollmentDAO.updateEnrollment(device.getId(), device.getEnrolmentInfo(),
tenantId);
} catch (DeviceManagementDAOException e) {
DeviceManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while dis-enrolling device: " +
device.getName();
log.error(msg, e);
throw new DeviceManagementException(msg, e);
}
try {
deviceDAO.updateDevice(device, tenantId);
} catch (DeviceManagementDAOException e) {
DeviceManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while updating device: " +
device.getName();
log.error(msg, e);
throw new DeviceManagementException(msg, e);
}
}
deviceIdentifiers.add(device.getDeviceIdentifier());
}
DeviceManagementDAOFactory.commitTransaction();
DeviceManagementDAOFactory.closeConnection();
disEnrollDevices(devices);
// delete devices
deviceIdentifiers = devices.stream()
.map(Device::getDeviceIdentifier).collect(Collectors.toList());
deleteDevices(deviceIdentifiers);
}
// remove device type versions
if (!deleteDeviceTypeVersions(deviceType)){
return false;
}
// delete device type
DeviceManagementDAOFactory.beginTransaction();
deviceTypeVersions = deviceTypeDAO.getDeviceTypeVersions(deviceType.getId(), deviceTypeName);
deviceTypeDAO.deleteDeviceType(getTenantId(), deviceType.getId());
DeviceManagementDAOFactory.commitTransaction();
} catch (InvalidDeviceException e) {
String msg = "Error occurred while deleting devices of type: " + deviceTypeName;
log.error(msg);
throw new DeviceManagementException(msg, e);
} catch (DeviceManagementDAOException e) {
DeviceManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while deleting device type of: " + deviceTypeName;
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);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
return true;
}
@Override
public void disEnrollDevices(List<Device> devices)
throws DeviceManagementException {
int tenantId = getTenantId();
try {
DeviceManagementDAOFactory.beginTransaction();
for (Device device : devices) {
if (device.getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.REMOVED)) {
if (log.isDebugEnabled()) {
log.debug("Device: " + device.getName() + " has already dis-enrolled");
}
} else {
device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime());
device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.REMOVED);
// different try blocks are used to isolate transactions
try {
enrollmentDAO.updateEnrollment(device.getId(), device.getEnrolmentInfo(),
tenantId);
} catch (DeviceManagementDAOException e) {
DeviceManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while dis-enrolling device: " +
device.getName();
log.error(msg, e);
throw new DeviceManagementException(msg, e);
}
try {
deviceDAO.updateDevice(device, tenantId);
} catch (DeviceManagementDAOException e) {
DeviceManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while updating device: " +
device.getName();
log.error(msg, e);
throw new DeviceManagementException(msg, e);
}
}
}
DeviceManagementDAOFactory.commitTransaction();
} catch (TransactionManagementException e) {
String msg = "Error occurred while initiating transaction";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Override
public boolean deleteDeviceTypeVersions(DeviceType deviceType)
throws DeviceManagementException {
boolean result;
String deviceTypeName = deviceType.getName();
try {
DeviceManagementDAOFactory.beginTransaction();
List<DeviceTypeVersion> deviceTypeVersions = deviceTypeDAO
.getDeviceTypeVersions(deviceType.getId(), deviceTypeName);
if (deviceTypeVersions == null || deviceTypeVersions.isEmpty()) {
if (log.isDebugEnabled()) {
log.debug("Device of type: " + deviceTypeName + "doesn't have any type versions");
log.debug("Device of type: " + deviceTypeName + "doesn't have any type " +
"versions");
}
} else {
for (DeviceTypeVersion deviceTypeVersion : deviceTypeVersions) {
result = deviceTypeDAO.isDeviceTypeVersionModifiable(deviceType.getId()
, deviceTypeVersion.getVersionName(), tenantId);
, deviceTypeVersion.getVersionName(), getTenantId());
if (!result) {
String msg = "Device type of: " + deviceTypeName + "is unauthorized to modify " +
"version";
String msg = "Device type of: " + deviceTypeName + "is unauthorized to " +
"modify version";
log.error(msg);
return false;
}
deviceTypeVersion.setVersionStatus("REMOVED");
result = deviceTypeDAO.updateDeviceTypeVersion(deviceTypeVersion);
if (!result) {
String msg = "Could not delete the version of device type: " +
deviceTypeName;
String msg = "Could not delete the version of device type: " + deviceTypeName;
log.error(msg);
return false;
}
}
DeviceManagementDAOFactory.commitTransaction();
}
// delete device type
deviceTypeDAO.deleteDeviceType(tenantId, deviceType.getId());
DeviceManagementDAOFactory.commitTransaction();
} catch (InvalidDeviceException e) {
String msg = "Error occurred while deleting devices of type: " + deviceTypeName;
log.error(msg);
} 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 deleting device type of: " + deviceTypeName;
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);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
return true;
}
@Override
public DeviceConfiguration getDeviceConfiguration(Map<String, String> deviceProps)
throws DeviceManagementException, DeviceNotFoundException, UnauthorizedDeviceAccessException,

Loading…
Cancel
Save