From 9d8c52958df21d91101f7991afab0135911cb71c Mon Sep 17 00:00:00 2001 From: mharindu Date: Mon, 13 Jul 2015 17:24:45 +0530 Subject: [PATCH] Refactored SQL queries and added null checks --- .../mgt/core/dao/impl/DeviceDAOImpl.java | 4 +-- .../DeviceManagementProviderServiceImpl.java | 36 ++++++++++++------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java index 5c9b25da88..55d19152ff 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java @@ -145,7 +145,7 @@ public class DeviceDAOImpl implements DeviceDAO { List devices = null; try { conn = this.getConnection(); - String sql = "SELECT d1.DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + + String sql = "SELECT d1.DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, d1.DEVICE_IDENTIFICATION, " + "e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT FROM DM_ENROLMENT e, (SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME," + "d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, DM_DEVICE_TYPE t " + "WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?"; @@ -175,7 +175,7 @@ public class DeviceDAOImpl implements DeviceDAO { List devices = null; try { conn = this.getConnection(); - String selectDBQueryForType = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + + String selectDBQueryForType = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, d1.DEVICE_IDENTIFICATION," + "e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME, " + "d.DEVICE_IDENTIFICATION, d.OWNER, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, DM_DEVICE_TYPE t " + "WHERE d.DM_DEVICE.DEVICE_TYPE_ID = t.ID AND t.NAME = ? AND d.TENANT_ID = ?) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?"; 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 150287fdbd..0ff30129ad 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 @@ -229,8 +229,10 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv Device dmsDevice = this.getPluginRepository().getDeviceManagementService(device.getType()).getDevice( new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); - device.setFeatures(dmsDevice.getFeatures()); - device.setProperties(dmsDevice.getProperties()); + if (dmsDevice != null) { + device.setFeatures(dmsDevice.getFeatures()); + device.setProperties(dmsDevice.getProperties()); + } devices.add(device); } return devices; @@ -259,8 +261,10 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv Device dmsDevice = this.getPluginRepository().getDeviceManagementService(device.getType()).getDevice( new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); - device.setFeatures(dmsDevice.getFeatures()); - device.setProperties(dmsDevice.getProperties()); + if (dmsDevice != null) { + device.setFeatures(dmsDevice.getFeatures()); + device.setProperties(dmsDevice.getProperties()); + } devices.add(device); } return devices; @@ -409,8 +413,10 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv if (device != null) { DeviceManager dms = this.getPluginRepository().getDeviceManagementService(deviceId.getType()); Device pluginSpecificInfo = dms.getDevice(deviceId); - device.setProperties(pluginSpecificInfo.getProperties()); - device.setFeatures(pluginSpecificInfo.getFeatures()); + if (pluginSpecificInfo != null) { + device.setFeatures(pluginSpecificInfo.getFeatures()); + device.setProperties(pluginSpecificInfo.getProperties()); + } } return device; } @@ -554,8 +560,10 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv Device dmsDevice = this.getPluginRepository().getDeviceManagementService(device.getType()).getDevice( new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); - device.setFeatures(dmsDevice.getFeatures()); - device.setProperties(dmsDevice.getProperties()); + if (dmsDevice != null) { + device.setFeatures(dmsDevice.getFeatures()); + device.setProperties(dmsDevice.getProperties()); + } devices.add(device); } return devices; @@ -596,8 +604,10 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv Device dmsDevice = this.getPluginRepository().getDeviceManagementService(device.getType()).getDevice( new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); - device.setFeatures(dmsDevice.getFeatures()); - device.setProperties(dmsDevice.getProperties()); + if (dmsDevice != null) { + device.setFeatures(dmsDevice.getFeatures()); + device.setProperties(dmsDevice.getProperties()); + } devices.add(device); } } @@ -643,8 +653,10 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv Device dmsDevice = this.getPluginRepository().getDeviceManagementService(device.getType()).getDevice( new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); - device.setFeatures(dmsDevice.getFeatures()); - device.setProperties(dmsDevice.getProperties()); + if (dmsDevice != null) { + device.setFeatures(dmsDevice.getFeatures()); + device.setProperties(dmsDevice.getProperties()); + } devices.add(device); } return devices;