From 4378feedde8285f1dd8ddf6de7d472348aa0f69d Mon Sep 17 00:00:00 2001 From: Yohan Avishke Date: Thu, 9 Jan 2020 11:19:16 +0530 Subject: [PATCH] Change try catch positions and add error messages to required positions --- .../DeviceManagementProviderServiceImpl.java | 56 +++++++++++-------- 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 0174a8b93f..01948680e5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -3606,37 +3606,45 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv throws DeviceManagementException { List deviceIdentifiers; - try { - if (deviceType == null || deviceTypeName == null || deviceTypeName.isEmpty()) { - String msg = "Error, device type cannot be null or empty"; - log.error(msg); - return false; + if (deviceType == null || StringUtils.isBlank(deviceTypeName)) { + String msg = "Error, device type cannot be null or empty or a blank space"; + log.error(msg); + return false; + } + List devices = getAllDevices(deviceTypeName, false); + if (devices == null || devices.isEmpty()) { + if (log.isDebugEnabled()) { + log.debug("No devices found for the device type: " + deviceTypeName); } - List 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 + disEnrollDevices(devices); + // delete devices + deviceIdentifiers = devices.stream() + .map(Device::getDeviceIdentifier).collect(Collectors.toList()); + try { + if(!deleteDevices(deviceIdentifiers)){ + log.error("Failed to delete devices of device type: " + deviceTypeName); + return false; } - } else { - // dis-enroll devices - disEnrollDevices(devices); - // delete devices - deviceIdentifiers = devices.stream() - .map(Device::getDeviceIdentifier).collect(Collectors.toList()); - deleteDevices(deviceIdentifiers); - } - // remove device type versions - if (!deleteDeviceTypeVersions(deviceType)){ - return false; + } catch (InvalidDeviceException e) { + String msg = "Error occurred while deleting devices of type: " + deviceTypeName; + log.error(msg); + throw new DeviceManagementException(msg, e); } + } + + // remove device type versions + if (!deleteDeviceTypeVersions(deviceType)) { + log.error("Failed to delete device type vesions for device type: " + deviceTypeName); + return false; + } + + try { // delete device type DeviceManagementDAOFactory.beginTransaction(); 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;