From d6501cbf8cb83448dbb54008ccda0a8f898a9cef Mon Sep 17 00:00:00 2001 From: geethkokila Date: Mon, 13 Jun 2016 10:15:26 +0530 Subject: [PATCH] Fixing the device search issues --- .../search/mgt/dao/impl/SearchDAOImpl.java | 5 ++- .../search/mgt/impl/QueryBuilderImpl.java | 40 ++++++++++--------- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/dao/impl/SearchDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/dao/impl/SearchDAOImpl.java index 49cca5ee27..f4917e0b84 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/dao/impl/SearchDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/dao/impl/SearchDAOImpl.java @@ -228,7 +228,8 @@ public class SearchDAOImpl implements SearchDAO { conn = this.getConnection(); String query = "SELECT * FROM DM_DEVICE_INFO WHERE DEVICE_ID IN (?) ORDER BY DEVICE_ID ;"; stmt = conn.prepareStatement(query); - if (conn.getMetaData().getDatabaseProductName().contains("H2")) { + if (conn.getMetaData().getDatabaseProductName().contains("H2") || + conn.getMetaData().getDatabaseProductName().contains("MySQL")) { String inData = Utils.getDeviceIdsAsString(devices); stmt.setString(1, inData); } else { @@ -252,7 +253,7 @@ public class SearchDAOImpl implements SearchDAO { } } } catch (SQLException e) { - e.printStackTrace(); + throw new SearchDAOException("Error occurred while retrieving the device properties.", e); } return devices; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/QueryBuilderImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/QueryBuilderImpl.java index 5864966f0c..826e94d18c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/QueryBuilderImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/QueryBuilderImpl.java @@ -112,11 +112,11 @@ public class QueryBuilderImpl implements QueryBuilder { String querySuffix = ""; for (Condition con : conditions) { - if (Utils.checkDeviceDetailsColumns(con.getKey())) { - querySuffix = querySuffix + " AND DD." + Utils.getDeviceDetailsColumnNames().get(con.getKey()) + - con.getOperator() + Utils.getConvertedValue(con.getKey(), con.getValue()); - } else if (Utils.checkDeviceLocationColumns(con.getKey())) { - querySuffix = querySuffix + " AND DL." + Utils.getDeviceLocationColumnNames().get(con.getKey()) + + if (Utils.checkDeviceDetailsColumns(con.getKey().toLowerCase())) { + querySuffix = querySuffix + " AND DD." + Utils.getDeviceDetailsColumnNames().get(con.getKey().toLowerCase()) + + con.getOperator() + Utils.getConvertedValue(con.getKey().toLowerCase(), con.getValue()); + } else if (Utils.checkDeviceLocationColumns(con.getKey().toLowerCase())) { + querySuffix = querySuffix + " AND DL." + Utils.getDeviceLocationColumnNames().get(con.getKey().toLowerCase()) + con.getOperator() + con.getValue(); } } @@ -130,11 +130,11 @@ public class QueryBuilderImpl implements QueryBuilder { String querySuffix = ""; for (Condition con : conditions) { - if (Utils.checkDeviceDetailsColumns(con.getKey())) { - querySuffix = querySuffix + " OR DD." + Utils.getDeviceDetailsColumnNames().get(con.getKey()) + + if (Utils.checkDeviceDetailsColumns(con.getKey().toLowerCase())) { + querySuffix = querySuffix + " OR DD." + Utils.getDeviceDetailsColumnNames().get(con.getKey().toLowerCase()) + con.getOperator() + Utils.getConvertedValue(con.getKey(), con.getValue()); - } else if (Utils.checkDeviceLocationColumns(con.getKey())) { - querySuffix = querySuffix + " OR DL." + Utils.getDeviceLocationColumnNames().get(con.getKey()) + + } else if (Utils.checkDeviceLocationColumns(con.getKey().toLowerCase())) { + querySuffix = querySuffix + " OR DL." + Utils.getDeviceLocationColumnNames().get(con.getKey().toLowerCase()) + con.getOperator() + con.getValue(); } } @@ -190,15 +190,17 @@ public class QueryBuilderImpl implements QueryBuilder { private String getGenericQueryPart() { return "SELECT D.ID, D.DESCRIPTION, D.NAME, \n" + - " D.DEVICE_TYPE_ID, D.DEVICE_IDENTIFICATION, DT.ID AS DEVICE_TYPE_ID, \n" + + "D.DEVICE_TYPE_ID, D.DEVICE_IDENTIFICATION, DT.ID AS DEVICE_TYPE_ID, \n" + "DT.NAME AS DEVICE_TYPE_NAME, DD.DEVICE_ID, DD.DEVICE_MODEL, DD.VENDOR, \n" + "DD.OS_VERSION, DD.BATTERY_LEVEL, DD.INTERNAL_TOTAL_MEMORY, DD.INTERNAL_AVAILABLE_MEMORY,\n" + "DD.EXTERNAL_TOTAL_MEMORY, DD.EXTERNAL_AVAILABLE_MEMORY, DD.CONNECTION_TYPE, \n" + "DD.SSID, DD.CPU_USAGE, DD.TOTAL_RAM_MEMORY, DD.AVAILABLE_RAM_MEMORY, \n" + "DD.PLUGGED_IN, DD.UPDATE_TIMESTAMP, DL.LATITUDE, DL.LONGITUDE, DL.STREET1, DL.STREET2, DL.CITY, DL.ZIP, \n" + - "DL.STATE, DL.COUNTRY, DL.UPDATE_TIMESTAMP AS DL_UPDATED_TIMESTAMP " + - "FROM DM_DEVICE_DETAIL AS DD, DM_DEVICE AS D, DM_DEVICE_LOCATION AS DL, " + - "DM_DEVICE_TYPE AS DT WHERE DEVICE_TYPE_ID=D.DEVICE_TYPE_ID AND D.TENANT_ID = " + + "DL.STATE, DL.COUNTRY, DL.UPDATE_TIMESTAMP AS DL_UPDATED_TIMESTAMP \n" + + "FROM DM_DEVICE_DETAIL AS DD INNER JOIN DM_DEVICE AS D ON D.ID=DD.DEVICE_ID\n" + + "LEFT JOIN DM_DEVICE_LOCATION AS DL ON DL.DEVICE_ID=D.ID \n" + + "INNER JOIN DM_DEVICE_TYPE AS DT ON DT.ID=D.DEVICE_TYPE_ID\n" + + "WHERE D.TENANT_ID = " + PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); @@ -207,16 +209,18 @@ public class QueryBuilderImpl implements QueryBuilder { private String getPropertyQueryPart() { return "SELECT D.ID, D.DESCRIPTION, D.NAME, \n" + - " D.DEVICE_TYPE_ID, D.DEVICE_IDENTIFICATION, DT.ID AS DEVICE_TYPE_ID, \n" + + "D.DEVICE_TYPE_ID, D.DEVICE_IDENTIFICATION, DT.ID AS DEVICE_TYPE_ID, \n" + "DT.NAME AS DEVICE_TYPE_NAME, DD.DEVICE_ID, DD.DEVICE_MODEL, DD.VENDOR, \n" + "DD.OS_VERSION, DD.BATTERY_LEVEL, DD.INTERNAL_TOTAL_MEMORY, DD.INTERNAL_AVAILABLE_MEMORY,\n" + "DD.EXTERNAL_TOTAL_MEMORY, DD.EXTERNAL_AVAILABLE_MEMORY, DD.CONNECTION_TYPE, \n" + "DD.SSID, DD.CPU_USAGE, DD.TOTAL_RAM_MEMORY, DD.AVAILABLE_RAM_MEMORY, \n" + "DD.PLUGGED_IN, DD.UPDATE_TIMESTAMP, DL.LATITUDE, DL.LONGITUDE, DL.STREET1, DL.STREET2, DL.CITY, DL.ZIP, \n" + - "DL.STATE, DL.COUNTRY, DL.UPDATE_TIMESTAMP AS DL_UPDATED_TIMESTAMP, " + - "DI.KEY_FIELD, DI.VALUE_FIELD FROM DM_DEVICE_DETAIL AS DD, " + - "DM_DEVICE AS D, DM_DEVICE_LOCATION AS DL, \n" + - "DM_DEVICE_INFO AS DI, DM_DEVICE_TYPE AS DT WHERE DEVICE_TYPE_ID=D.DEVICE_TYPE_ID AND D.TENANT_ID = " + + "DL.STATE, DL.COUNTRY, DL.UPDATE_TIMESTAMP AS DL_UPDATED_TIMESTAMP, DI.KEY_FIELD, DI.VALUE_FIELD \n" + + "FROM DM_DEVICE_DETAIL AS DD INNER JOIN DM_DEVICE AS D ON D.ID=DD.DEVICE_ID\n" + + "LEFT JOIN DM_DEVICE_LOCATION AS DL ON DL.DEVICE_ID=D.ID \n" + + "INNER JOIN DM_DEVICE_TYPE AS DT ON DT.ID=D.DEVICE_TYPE_ID\n" + + "LEFT JOIN DM_DEVICE_INFO AS DI ON DI.DEVICE_ID=D.ID\n" + + "WHERE D.TENANT_ID = " + PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); }