From a774bc89aa2214f0c57559a85ca0f18289c39b7a Mon Sep 17 00:00:00 2001 From: manoj Date: Fri, 15 May 2015 02:43:41 +0530 Subject: [PATCH] Device Type not present in DB, return null instead of dummy object --- .../mgt/core/dao/impl/DeviceTypeDAOImpl.java | 10 +++---- .../DeviceManagementServiceComponent.java | 3 +-- .../mgt/core/util/DeviceManagerUtil.java | 26 +++++++++---------- 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java index caac62456f..f2f0ac8937 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java @@ -120,6 +120,7 @@ public class DeviceTypeDAOImpl implements DeviceTypeDAO { Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; + DeviceType deviceType = null; try { conn = this.getConnection(); String sql = "SELECT ID From DM_DEVICE_TYPE WHERE NAME = ?"; @@ -127,14 +128,11 @@ public class DeviceTypeDAOImpl implements DeviceTypeDAO { stmt.setString(1, type); rs = stmt.executeQuery(); - int id = -1; if (rs.next()) { - id = rs.getInt("ID"); + deviceType = new DeviceType(); + deviceType.setId(rs.getInt("ID")); + deviceType.setName(type); } - DeviceType deviceType = new DeviceType(); - deviceType.setId(id); - deviceType.setName(type); - return deviceType; } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while fetch device type id for device type " + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java index ab985dcda2..661eb61693 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java @@ -55,10 +55,9 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOF import org.wso2.carbon.device.mgt.core.service.DeviceManagementService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementServiceImpl; import org.wso2.carbon.device.mgt.core.util.DeviceManagementSchemaInitializer; +import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.ndatasource.core.DataSourceService; import org.wso2.carbon.registry.core.service.RegistryService; -import org.wso2.carbon.user.core.service.RealmService; - import java.util.ArrayList; import java.util.List; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java index 80e2b5b371..f716bf6e2e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java @@ -111,23 +111,23 @@ public final class DeviceManagerUtil { /** * Adds a new device type to the database if it does not exists. * - * @param deviceType device type + * @param typeName device type * @return status of the operation */ - public static boolean registerDeviceType(String deviceType) throws DeviceManagementException { + public static boolean registerDeviceType(String typeName) throws DeviceManagementException { boolean status; try { DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); - DeviceType deviceTypeId = deviceTypeDAO.getDeviceType(deviceType); - if (deviceTypeId == null) { + DeviceType deviceType = deviceTypeDAO.getDeviceType(typeName); + if (deviceType == null) { DeviceType dt = new DeviceType(); - dt.setName(deviceType); + dt.setName(typeName); deviceTypeDAO.addDeviceType(dt); } status = true; } catch (DeviceManagementDAOException e) { throw new DeviceManagementException("Error occurred while registering the device type '" + - deviceType + "'", e); + typeName + "'", e); } return status; } @@ -135,22 +135,22 @@ public final class DeviceManagerUtil { /** * Un-registers an existing device type from the device management metadata repository. * - * @param deviceType device type + * @param typeName device type * @return status of the operation */ - public static boolean unregisterDeviceType(String deviceType) throws DeviceManagementException { + public static boolean unregisterDeviceType(String typeName) throws DeviceManagementException { try { DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); - DeviceType deviceTypeId = deviceTypeDAO.getDeviceType(deviceType); - if (deviceTypeId == null) { + DeviceType deviceType = deviceTypeDAO.getDeviceType(typeName); + if (deviceType == null) { DeviceType dt = new DeviceType(); - dt.setName(deviceType); - deviceTypeDAO.removeDeviceType(deviceType); + dt.setName(typeName); + deviceTypeDAO.removeDeviceType(typeName); } return true; } catch (DeviceManagementDAOException e) { throw new DeviceManagementException("Error occurred while registering the device type '" + - deviceType + "'", e); + typeName + "'", e); } }