From e2d67fc92c8036dce11e4e9d7c560d1b8d15f8b0 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Mon, 2 May 2016 14:36:14 +0530 Subject: [PATCH] Adding the changes for device search --- .../search/mgt/dao/impl/SearchDAOImpl.java | 201 +++++++++--------- .../search/mgt/impl/QueryBuilderImpl.java | 4 +- .../mgt/core/search/mgt/impl/Utils.java | 35 +++ .../device/mgt/core/Search/SearchDevice.java | 143 ++++++++----- 4 files changed, 228 insertions(+), 155 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 caf6f1929a..ac8f670b7a 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 @@ -34,7 +34,9 @@ import org.wso2.carbon.device.mgt.core.search.mgt.impl.Utils; import java.sql.*; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; public class SearchDAOImpl implements SearchDAO { @@ -52,59 +54,62 @@ public class SearchDAOImpl implements SearchDAO { PreparedStatement stmt = null; ResultSet rs; List devices = new ArrayList<>(); + Map devs = new HashMap<>(); try { conn = this.getConnection(); stmt = conn.prepareStatement(query); rs = stmt.executeQuery(); while (rs.next()) { - - Device device = new Device(); - device.setId(rs.getInt("ID")); - device.setDescription(rs.getString("DESCRIPTION")); - device.setName("NAME"); - device.setType(rs.getString("DEVICE_TYPE_NAME")); - device.setDeviceIdentifier(rs.getString("DEVICE_IDENTIFICATION")); - - DeviceIdentifier identifier = new DeviceIdentifier(); - identifier.setType(rs.getString("DEVICE_TYPE_NAME")); - identifier.setId(rs.getString("DEVICE_IDENTIFICATION")); - - DeviceInfo deviceInfo = new DeviceInfo(); - deviceInfo.setDeviceId(rs.getInt("ID")); - deviceInfo.setAvailableRAMMemory(rs.getDouble("AVAILABLE_RAM_MEMORY")); - deviceInfo.setBatteryLevel(rs.getDouble("BATTERY_LEVEL")); - deviceInfo.setConnectionType(rs.getString("CONNECTION_TYPE")); - deviceInfo.setCpuUsage(rs.getDouble("CPU_USAGE")); - deviceInfo.setDeviceModel(rs.getString("DEVICE_MODEL")); - deviceInfo.setExternalAvailableMemory(rs.getDouble("EXTERNAL_AVAILABLE_MEMORY")); - deviceInfo.setExternalTotalMemory(rs.getDouble("EXTERNAL_TOTAL_MEMORY")); - deviceInfo.setInternalAvailableMemory(rs.getDouble("INTERNAL_AVAILABLE_MEMORY")); - deviceInfo.setInternalTotalMemory(rs.getDouble("EXTERNAL_TOTAL_MEMORY")); - deviceInfo.setOsVersion(rs.getString("OS_VERSION")); - deviceInfo.setPluggedIn(rs.getBoolean("PLUGGED_IN")); - deviceInfo.setSsid(rs.getString("SSID")); - deviceInfo.setTotalRAMMemory(rs.getDouble("TOTAL_RAM_MEMORY")); - deviceInfo.setVendor(rs.getString("VENDOR")); - deviceInfo.setUpdatedTime(new java.util.Date(rs.getLong("UPDATE_TIMESTAMP"))); - - DeviceLocation deviceLocation = new DeviceLocation(); - deviceLocation.setLatitude(rs.getDouble("LATITUDE")); - deviceLocation.setLongitude(rs.getDouble("LONGITUDE")); - deviceLocation.setStreet1(rs.getString("STREET1")); - deviceLocation.setStreet2(rs.getString("STREET2")); - deviceLocation.setCity(rs.getString("CITY")); - deviceLocation.setState(rs.getString("STATE")); - deviceLocation.setZip(rs.getString("ZIP")); - deviceLocation.setCountry(rs.getString("COUNTRY")); - deviceLocation.setDeviceId(rs.getInt("ID")); - deviceLocation.setUpdatedTime(new java.util.Date(rs.getLong("DL_UPDATED_TIMESTAMP"))); - - DeviceWrapper wrapper = new DeviceWrapper(); - wrapper.setDevice(device); - wrapper.setDeviceInfo(deviceInfo); - wrapper.setDeviceLocation(deviceLocation); - wrapper.setDeviceIdentifier(identifier); - devices.add(wrapper); + if(!devs.containsKey(rs.getInt("ID"))) { + Device device = new Device(); + device.setId(rs.getInt("ID")); + device.setDescription(rs.getString("DESCRIPTION")); + device.setName("NAME"); + device.setType(rs.getString("DEVICE_TYPE_NAME")); + device.setDeviceIdentifier(rs.getString("DEVICE_IDENTIFICATION")); + + DeviceIdentifier identifier = new DeviceIdentifier(); + identifier.setType(rs.getString("DEVICE_TYPE_NAME")); + identifier.setId(rs.getString("DEVICE_IDENTIFICATION")); + + DeviceInfo deviceInfo = new DeviceInfo(); + deviceInfo.setDeviceId(rs.getInt("ID")); + deviceInfo.setAvailableRAMMemory(rs.getDouble("AVAILABLE_RAM_MEMORY")); + deviceInfo.setBatteryLevel(rs.getDouble("BATTERY_LEVEL")); + deviceInfo.setConnectionType(rs.getString("CONNECTION_TYPE")); + deviceInfo.setCpuUsage(rs.getDouble("CPU_USAGE")); + deviceInfo.setDeviceModel(rs.getString("DEVICE_MODEL")); + deviceInfo.setExternalAvailableMemory(rs.getDouble("EXTERNAL_AVAILABLE_MEMORY")); + deviceInfo.setExternalTotalMemory(rs.getDouble("EXTERNAL_TOTAL_MEMORY")); + deviceInfo.setInternalAvailableMemory(rs.getDouble("INTERNAL_AVAILABLE_MEMORY")); + deviceInfo.setInternalTotalMemory(rs.getDouble("EXTERNAL_TOTAL_MEMORY")); + deviceInfo.setOsVersion(rs.getString("OS_VERSION")); + deviceInfo.setPluggedIn(rs.getBoolean("PLUGGED_IN")); + deviceInfo.setSsid(rs.getString("SSID")); + deviceInfo.setTotalRAMMemory(rs.getDouble("TOTAL_RAM_MEMORY")); + deviceInfo.setVendor(rs.getString("VENDOR")); + deviceInfo.setUpdatedTime(new java.util.Date(rs.getLong("UPDATE_TIMESTAMP"))); + + DeviceLocation deviceLocation = new DeviceLocation(); + deviceLocation.setLatitude(rs.getDouble("LATITUDE")); + deviceLocation.setLongitude(rs.getDouble("LONGITUDE")); + deviceLocation.setStreet1(rs.getString("STREET1")); + deviceLocation.setStreet2(rs.getString("STREET2")); + deviceLocation.setCity(rs.getString("CITY")); + deviceLocation.setState(rs.getString("STATE")); + deviceLocation.setZip(rs.getString("ZIP")); + deviceLocation.setCountry(rs.getString("COUNTRY")); + deviceLocation.setDeviceId(rs.getInt("ID")); + deviceLocation.setUpdatedTime(new java.util.Date(rs.getLong("DL_UPDATED_TIMESTAMP"))); + + DeviceWrapper wrapper = new DeviceWrapper(); + wrapper.setDevice(device); + wrapper.setDeviceInfo(deviceInfo); + wrapper.setDeviceLocation(deviceLocation); + wrapper.setDeviceIdentifier(identifier); + devices.add(wrapper); + devs.put(device.getId(), device.getId()); + } } } catch (SQLException e) { @@ -132,59 +137,63 @@ public class SearchDAOImpl implements SearchDAO { PreparedStatement stmt = null; ResultSet rs; List devices = new ArrayList<>(); + Map devs = new HashMap<>(); try { conn = this.getConnection(); stmt = conn.prepareStatement(query); rs = stmt.executeQuery(); while (rs.next()) { - - Device device = new Device(); - device.setId(rs.getInt("ID")); - device.setDescription(rs.getString("DESCRIPTION")); - device.setName(rs.getString("NAME")); - device.setType(rs.getString("DEVICE_TYPE_NAME")); - device.setDeviceIdentifier(rs.getString("DEVICE_IDENTIFICATION")); - - DeviceIdentifier identifier = new DeviceIdentifier(); - identifier.setType(rs.getString("DEVICE_TYPE_NAME")); - identifier.setId(rs.getString("DEVICE_IDENTIFICATION")); - - DeviceInfo deviceInfo = new DeviceInfo(); - deviceInfo.setDeviceId(rs.getInt("ID")); - deviceInfo.setAvailableRAMMemory(rs.getDouble("AVAILABLE_RAM_MEMORY")); - deviceInfo.setBatteryLevel(rs.getDouble("BATTERY_LEVEL")); - deviceInfo.setConnectionType(rs.getString("CONNECTION_TYPE")); - deviceInfo.setCpuUsage(rs.getDouble("CPU_USAGE")); - deviceInfo.setDeviceModel(rs.getString("DEVICE_MODEL")); - deviceInfo.setExternalAvailableMemory(rs.getDouble("EXTERNAL_AVAILABLE_MEMORY")); - deviceInfo.setExternalTotalMemory(rs.getDouble("EXTERNAL_TOTAL_MEMORY")); - deviceInfo.setInternalAvailableMemory(rs.getDouble("INTERNAL_AVAILABLE_MEMORY")); - deviceInfo.setInternalTotalMemory(rs.getDouble("EXTERNAL_TOTAL_MEMORY")); - deviceInfo.setOsVersion(rs.getString("OS_VERSION")); - deviceInfo.setPluggedIn(rs.getBoolean("PLUGGED_IN")); - deviceInfo.setSsid(rs.getString("SSID")); - deviceInfo.setTotalRAMMemory(rs.getDouble("TOTAL_RAM_MEMORY")); - deviceInfo.setVendor(rs.getString("VENDOR")); - deviceInfo.setUpdatedTime(new java.util.Date(rs.getLong("UPDATE_TIMESTAMP"))); - - DeviceLocation deviceLocation = new DeviceLocation(); - deviceLocation.setLatitude(rs.getDouble("LATITUDE")); - deviceLocation.setLongitude(rs.getDouble("LONGITUDE")); - deviceLocation.setStreet1(rs.getString("STREET1")); - deviceLocation.setStreet2(rs.getString("STREET2")); - deviceLocation.setCity(rs.getString("CITY")); - deviceLocation.setState(rs.getString("STATE")); - deviceLocation.setZip(rs.getString("ZIP")); - deviceLocation.setCountry(rs.getString("COUNTRY")); - deviceLocation.setDeviceId(rs.getInt("ID")); - deviceLocation.setUpdatedTime(new java.util.Date(rs.getLong("DL_UPDATED_TIMESTAMP"))); - - DeviceWrapper wrapper = new DeviceWrapper(); - wrapper.setDevice(device); - wrapper.setDeviceInfo(deviceInfo); - wrapper.setDeviceLocation(deviceLocation); - wrapper.setDeviceIdentifier(identifier); - devices.add(wrapper); + if(!devs.containsKey(rs.getInt("ID"))) { + Device device = new Device(); + device.setId(rs.getInt("ID")); + device.setDescription(rs.getString("DESCRIPTION")); + device.setName(rs.getString("NAME")); + device.setType(rs.getString("DEVICE_TYPE_NAME")); + device.setDeviceIdentifier(rs.getString("DEVICE_IDENTIFICATION")); + + DeviceIdentifier identifier = new DeviceIdentifier(); + identifier.setType(rs.getString("DEVICE_TYPE_NAME")); + identifier.setId(rs.getString("DEVICE_IDENTIFICATION")); + + DeviceInfo deviceInfo = new DeviceInfo(); + deviceInfo.setDeviceId(rs.getInt("ID")); + deviceInfo.setAvailableRAMMemory(rs.getDouble("AVAILABLE_RAM_MEMORY")); + deviceInfo.setBatteryLevel(rs.getDouble("BATTERY_LEVEL")); + deviceInfo.setConnectionType(rs.getString("CONNECTION_TYPE")); + deviceInfo.setCpuUsage(rs.getDouble("CPU_USAGE")); + deviceInfo.setDeviceModel(rs.getString("DEVICE_MODEL")); + deviceInfo.setExternalAvailableMemory(rs.getDouble("EXTERNAL_AVAILABLE_MEMORY")); + deviceInfo.setExternalTotalMemory(rs.getDouble("EXTERNAL_TOTAL_MEMORY")); + deviceInfo.setInternalAvailableMemory(rs.getDouble("INTERNAL_AVAILABLE_MEMORY")); + deviceInfo.setInternalTotalMemory(rs.getDouble("EXTERNAL_TOTAL_MEMORY")); + deviceInfo.setOsVersion(rs.getString("OS_VERSION")); + deviceInfo.setPluggedIn(rs.getBoolean("PLUGGED_IN")); + deviceInfo.setSsid(rs.getString("SSID")); + deviceInfo.setTotalRAMMemory(rs.getDouble("TOTAL_RAM_MEMORY")); + deviceInfo.setVendor(rs.getString("VENDOR")); + deviceInfo.setUpdatedTime(new java.util.Date(rs.getLong("UPDATE_TIMESTAMP"))); + + DeviceLocation deviceLocation = new DeviceLocation(); + deviceLocation.setLatitude(rs.getDouble("LATITUDE")); + deviceLocation.setLongitude(rs.getDouble("LONGITUDE")); + deviceLocation.setStreet1(rs.getString("STREET1")); + deviceLocation.setStreet2(rs.getString("STREET2")); + deviceLocation.setCity(rs.getString("CITY")); + deviceLocation.setState(rs.getString("STATE")); + deviceLocation.setZip(rs.getString("ZIP")); + deviceLocation.setCountry(rs.getString("COUNTRY")); + deviceLocation.setDeviceId(rs.getInt("ID")); + deviceLocation.setUpdatedTime(new java.util.Date(rs.getLong("DL_UPDATED_TIMESTAMP"))); + + DeviceWrapper wrapper = new DeviceWrapper(); + wrapper.setDevice(device); + wrapper.setDeviceInfo(deviceInfo); + wrapper.setDeviceLocation(deviceLocation); + wrapper.setDeviceIdentifier(identifier); + + devices.add(wrapper); + devs.put(device.getId(), device.getId()); + } } } catch (SQLException e) { 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 60b46271c2..5864966f0c 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 @@ -114,7 +114,7 @@ public class QueryBuilderImpl implements QueryBuilder { for (Condition con : conditions) { if (Utils.checkDeviceDetailsColumns(con.getKey())) { querySuffix = querySuffix + " AND DD." + Utils.getDeviceDetailsColumnNames().get(con.getKey()) + - con.getOperator() + con.getValue(); + con.getOperator() + Utils.getConvertedValue(con.getKey(), con.getValue()); } else if (Utils.checkDeviceLocationColumns(con.getKey())) { querySuffix = querySuffix + " AND DL." + Utils.getDeviceLocationColumnNames().get(con.getKey()) + con.getOperator() + con.getValue(); @@ -132,7 +132,7 @@ public class QueryBuilderImpl implements QueryBuilder { for (Condition con : conditions) { if (Utils.checkDeviceDetailsColumns(con.getKey())) { querySuffix = querySuffix + " OR DD." + Utils.getDeviceDetailsColumnNames().get(con.getKey()) + - con.getOperator() + con.getValue(); + con.getOperator() + Utils.getConvertedValue(con.getKey(), con.getValue()); } else if (Utils.checkDeviceLocationColumns(con.getKey())) { querySuffix = querySuffix + " OR DL." + Utils.getDeviceLocationColumnNames().get(con.getKey()) + con.getOperator() + con.getValue(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/Utils.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/Utils.java index bc69e76033..3522228511 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/Utils.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/Utils.java @@ -60,6 +60,41 @@ public class Utils { } + public static boolean checkColumnType(String column) { + + boolean bool = false; + + switch (column) { + case "deviceModel": + bool = true; + break; + case "vendor": + bool = true; + break; + case "osVersion": + bool = true; + break; + case "connectionType": + bool = true; + break; + case "ssid": + bool = true; + break; + default: bool =false; + break; + } + + return bool; + } + + public static String getConvertedValue(String column, String value) { + + if(checkColumnType(column)){ + return "\'" + value + "\'"; + } else return value; + + } + public static Map getDeviceDetailsColumnNames() { return genericColumnsMap; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/Search/SearchDevice.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/Search/SearchDevice.java index 88acffe514..93c15914ad 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/Search/SearchDevice.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/Search/SearchDevice.java @@ -93,62 +93,91 @@ public class SearchDevice extends BaseDeviceManagementTest { } -// @Test -// public void doValidLocationSearch() throws Exception{ -// -// SearchContext context = new SearchContext(); -// List conditions = new ArrayList<>(); -// -// Condition cond = new Condition(); -// cond.setKey("LOCATION"); -// cond.setOperator("="); -// cond.setValue("Karan"); -// cond.setState(Condition.State.AND); -// conditions.add(cond); -// -// context.setConditions(conditions); -// -// SearchManagerService service = new SearchManagerServiceImpl(); -// List deviceWrappers = service.search(context); -// -// Gson gson = new Gson(); -// String bbbb = gson.toJson(deviceWrappers); -// log.info("Valid Search " + bbbb); -// -// -// for (DeviceWrapper dw : deviceWrappers) { -// log.debug(dw.getDevice().getDescription()); -// log.debug(dw.getDevice().getDeviceIdentifier()); -// } -// } -// -// @Test -// public void doInvalidLocationSearch() throws Exception{ -// -// SearchContext context = new SearchContext(); -// List conditions = new ArrayList<>(); -// -// Condition cond = new Condition(); -// cond.setKey("LOCATION"); -// cond.setOperator("="); -// cond.setValue("Colombo"); -// cond.setState(Condition.State.AND); -// conditions.add(cond); -// -// context.setConditions(conditions); -// -// SearchManagerService service = new SearchManagerServiceImpl(); -// List deviceWrappers = service.search(context); -// -// Gson gson = new Gson(); -// String bbbb = gson.toJson(deviceWrappers); -// log.info("Invalid Search " + bbbb); -// -// -// for (DeviceWrapper dw : deviceWrappers) { -// log.debug(dw.getDevice().getDescription()); -// log.debug(dw.getDevice().getDeviceIdentifier()); -// } -// } + @Test + public void doValidLocationSearch() throws Exception{ + + SearchContext context = new SearchContext(); + List conditions = new ArrayList<>(); + + Condition cond = new Condition(); + cond.setKey("LOCATION"); + cond.setOperator("="); + cond.setValue("Karan"); + cond.setState(Condition.State.AND); + conditions.add(cond); + + context.setConditions(conditions); + + SearchManagerService service = new SearchManagerServiceImpl(); + List deviceWrappers = service.search(context); + + Gson gson = new Gson(); + String bbbb = gson.toJson(deviceWrappers); + log.info("Valid Search " + bbbb); + + + for (DeviceWrapper dw : deviceWrappers) { + log.debug(dw.getDevice().getDescription()); + log.debug(dw.getDevice().getDeviceIdentifier()); + } + } + + @Test + public void doInvalidLocationSearch() throws Exception{ + + SearchContext context = new SearchContext(); + List conditions = new ArrayList<>(); + + Condition cond = new Condition(); + cond.setKey("LOCATION"); + cond.setOperator("="); + cond.setValue("Colombo"); + cond.setState(Condition.State.AND); + conditions.add(cond); + + context.setConditions(conditions); + + SearchManagerService service = new SearchManagerServiceImpl(); + List deviceWrappers = service.search(context); + + Gson gson = new Gson(); + String bbbb = gson.toJson(deviceWrappers); + log.info("Invalid Search " + bbbb); + + + for (DeviceWrapper dw : deviceWrappers) { + log.debug(dw.getDevice().getDescription()); + log.debug(dw.getDevice().getDeviceIdentifier()); + } + } + + @Test + public void doStringSearch() throws Exception{ + + SearchContext context = new SearchContext(); + List conditions = new ArrayList<>(); + + Condition cond = new Condition(); + cond.setKey("deviceModel"); + cond.setOperator("="); + cond.setValue("SM-T520"); + cond.setState(Condition.State.AND); + conditions.add(cond); + + context.setConditions(conditions); + + SearchManagerService service = new SearchManagerServiceImpl(); + List deviceWrappers = service.search(context); + + Gson gson = new Gson(); + String bbbb = gson.toJson(deviceWrappers); + log.info("Invalid Search " + bbbb); + + + for (DeviceWrapper dw : deviceWrappers) { + log.debug(dw.getDevice().getDescription()); + log.debug(dw.getDevice().getDeviceIdentifier()); + } + } }