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

merge-requests/418/head
Yohan Avishke 5 years ago
parent b76a838dea
commit 1c2d1f52a0

@ -772,6 +772,24 @@ public interface DeviceManagementProviderService {
DeviceTypeVersion getDeviceTypeVersion(String deviceTypeName, String version) throws DeviceTypeVersion getDeviceTypeVersion(String deviceTypeName, String version) throws
DeviceManagementException; 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 * 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.Arrays;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.stream.Collectors;
public class DeviceManagementProviderServiceImpl implements DeviceManagementProviderService, public class DeviceManagementProviderServiceImpl implements DeviceManagementProviderService,
PluginInitializationListener { PluginInitializationListener {
@ -3604,109 +3605,150 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
public boolean deleteDeviceType(String deviceTypeName, DeviceType deviceType) public boolean deleteDeviceType(String deviceTypeName, DeviceType deviceType)
throws DeviceManagementException { throws DeviceManagementException {
List<String> deviceIdentifiers; List<String> deviceIdentifiers;
List<DeviceTypeVersion> deviceTypeVersions;
boolean result;
int tenantId = getTenantId();
try { try {
if (deviceType == null || deviceTypeName.isEmpty()) { if (deviceType == null || deviceTypeName == null || deviceTypeName.isEmpty()) {
String msg = "Error, device type cannot be null or empty"; String msg = "Error, device type cannot be null or empty";
log.error(msg); log.error(msg);
return false; return false;
} }
DeviceManagementDAOFactory.beginTransaction(); List<Device> devices = getAllDevices(deviceTypeName, false);
List<Device> devices = deviceDAO.getDevices(deviceTypeName, this.getTenantId());
if (devices == null || devices.isEmpty()) { if (devices == null || devices.isEmpty()) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("No devices found for the device type: " + deviceTypeName); log.debug("No devices found for the device type: " + deviceTypeName);
} }
} else { } else {
// dis-enroll devices // dis-enroll devices
deviceIdentifiers = new ArrayList<>(); disEnrollDevices(devices);
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();
// delete devices // delete devices
deviceIdentifiers = devices.stream()
.map(Device::getDeviceIdentifier).collect(Collectors.toList());
deleteDevices(deviceIdentifiers); deleteDevices(deviceIdentifiers);
} }
// remove device type versions // remove device type versions
if (!deleteDeviceTypeVersions(deviceType)){
return false;
}
// delete device type
DeviceManagementDAOFactory.beginTransaction(); 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 (deviceTypeVersions == null || deviceTypeVersions.isEmpty()) {
if (log.isDebugEnabled()) { 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 { } else {
for (DeviceTypeVersion deviceTypeVersion : deviceTypeVersions) { for (DeviceTypeVersion deviceTypeVersion : deviceTypeVersions) {
result = deviceTypeDAO.isDeviceTypeVersionModifiable(deviceType.getId() result = deviceTypeDAO.isDeviceTypeVersionModifiable(deviceType.getId()
, deviceTypeVersion.getVersionName(), tenantId); , deviceTypeVersion.getVersionName(), getTenantId());
if (!result) { if (!result) {
String msg = "Device type of: " + deviceTypeName + "is unauthorized to modify " + String msg = "Device type of: " + deviceTypeName + "is unauthorized to " +
"version"; "modify version";
log.error(msg); log.error(msg);
return false; return false;
} }
deviceTypeVersion.setVersionStatus("REMOVED");
result = deviceTypeDAO.updateDeviceTypeVersion(deviceTypeVersion); result = deviceTypeDAO.updateDeviceTypeVersion(deviceTypeVersion);
if (!result) { if (!result) {
String msg = "Could not delete the version of device type: " + String msg = "Could not delete the version of device type: " + deviceTypeName;
deviceTypeName;
log.error(msg); log.error(msg);
return false; return false;
} }
} }
DeviceManagementDAOFactory.commitTransaction();
} }
// delete device type } catch (TransactionManagementException e) {
deviceTypeDAO.deleteDeviceType(tenantId, deviceType.getId()); String msg = "Error occurred while initiating transaction";
DeviceManagementDAOFactory.commitTransaction(); log.error(msg, e);
} catch (InvalidDeviceException e) {
String msg = "Error occurred while deleting devices of type: " + deviceTypeName;
log.error(msg);
throw new DeviceManagementException(msg, e); throw new DeviceManagementException(msg, e);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
DeviceManagementDAOFactory.rollbackTransaction(); DeviceManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while deleting device type of: " + deviceTypeName; String msg = "Error occurred while deleting device type of: " + deviceTypeName;
log.error(msg, e); log.error(msg, e);
throw new DeviceManagementException(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 { } finally {
DeviceManagementDAOFactory.closeConnection(); DeviceManagementDAOFactory.closeConnection();
} }
return true; return true;
} }
@Override @Override
public DeviceConfiguration getDeviceConfiguration(Map<String, String> deviceProps) public DeviceConfiguration getDeviceConfiguration(Map<String, String> deviceProps)
throws DeviceManagementException, DeviceNotFoundException, UnauthorizedDeviceAccessException, throws DeviceManagementException, DeviceNotFoundException, UnauthorizedDeviceAccessException,

Loading…
Cancel
Save