From 26022daf03df6b3badd09a974b33468821926df7 Mon Sep 17 00:00:00 2001 From: Menaka Jayawardena Date: Wed, 11 Oct 2017 16:54:21 +0530 Subject: [PATCH] Added checks for other data types. --- .../core/search/mgt/impl/ProcessorImpl.java | 2 +- .../search/mgt/impl/QueryBuilderImpl.java | 64 +++++++++++++------ .../mgt/core/search/mgt/impl/Utils.java | 42 +++++++++--- 3 files changed, 77 insertions(+), 31 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/ProcessorImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/ProcessorImpl.java index cf7a1eb069..f96e2e4048 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/ProcessorImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/ProcessorImpl.java @@ -251,7 +251,7 @@ public class ProcessorImpl implements Processor { if (log.isDebugEnabled()) { log.debug("Query : " + queryHolder.getQuery()); } - Connection conn; + Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; List devices = new ArrayList<>(); 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 d5d0b1700a..65b6af46fa 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 @@ -23,7 +23,11 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.search.Condition; -import org.wso2.carbon.device.mgt.core.search.mgt.*; +import org.wso2.carbon.device.mgt.core.search.mgt.Constants; +import org.wso2.carbon.device.mgt.core.search.mgt.InvalidOperatorException; +import org.wso2.carbon.device.mgt.core.search.mgt.QueryBuilder; +import org.wso2.carbon.device.mgt.core.search.mgt.QueryHolder; +import org.wso2.carbon.device.mgt.core.search.mgt.ValueType; import java.util.ArrayList; import java.util.HashMap; @@ -93,8 +97,8 @@ public class QueryBuilderImpl implements QueryBuilder { intArr[0] = 1; //int x = 1; String query = this.getGenericQueryPart(valueTypeArray) + - this.processAND(andColumns, valueTypeArray, intArr) + - this.processOR(orColumns, valueTypeArray, intArr); + this.processAND(andColumns, valueTypeArray, intArr) + + this.processOR(orColumns, valueTypeArray, intArr); List queryHolders = new ArrayList<>(); QueryHolder queryHolder = new QueryHolder(); queryHolder.setQuery(query); @@ -135,20 +139,13 @@ public class QueryBuilderImpl implements QueryBuilder { + " LIKE ? "; ValueType type = new ValueType(); type.setColumnType(ValueType.columnType.STRING); - type.setStringValue("%"+con.getValue()+"%"); + type.setStringValue("%" + con.getValue() + "%"); valueType[x] = type; x++; } else { querySuffix = querySuffix + " AND DD." + Utils.getDeviceDetailsColumnNames().get(con.getKey()) + con .getOperator() + " ? "; - ValueType type = new ValueType(); - if (Utils.checkColumnType(con.getKey())) { - type.setColumnType(ValueType.columnType.STRING); - type.setStringValue(con.getValue()); - } else { - type.setColumnType(ValueType.columnType.INTEGER); - type.setIntValue(Integer.parseInt(con.getValue())); - } + ValueType type = this.getValueType(con); valueType[x] = type; x++; } @@ -182,21 +179,15 @@ public class QueryBuilderImpl implements QueryBuilder { + " LIKE ? "; ValueType type = new ValueType(); type.setColumnType(ValueType.columnType.STRING); - type.setStringValue("%"+con.getValue()+"%"); + type.setStringValue("%" + con.getValue() + "%"); valueType[x] = type; x++; } else { querySuffix = querySuffix + " OR DD." + Utils.getDeviceDetailsColumnNames().get(con.getKey()) + con .getOperator() + " ? "; - ValueType type = new ValueType(); - if (Utils.checkColumnType(con.getKey())) { - type.setColumnType(ValueType.columnType.STRING); - type.setStringValue(con.getValue()); - } else { - type.setColumnType(ValueType.columnType.INTEGER); - type.setIntValue(Integer.parseInt(con.getValue())); - } + ValueType type = this.getValueType(con); + valueType[x] = type; x++; } @@ -386,4 +377,35 @@ public class QueryBuilderImpl implements QueryBuilder { throw new InvalidOperatorException("Error occurred while building the sql", e); } } + + /** + * Returns a Value type based on the Condition data. + * + * @param con : The condition that passed. + * @re + */ + private ValueType getValueType(Condition con) { + ValueType type = new ValueType(); + String colValue = Utils.checkColumnType(con.getKey()); + + switch (colValue) { + case "String": + type.setColumnType(ValueType.columnType.STRING); + type.setStringValue(con.getValue()); + break; + case "Double": + type.setColumnType(ValueType.columnType.DOUBLE); + type.setDoubleValue(Double.parseDouble(con.getValue())); + break; + case "Integer": + type.setColumnType(ValueType.columnType.INTEGER); + type.setIntValue(Integer.parseInt(con.getValue())); + break; + case "Long": + type.setColumnType(ValueType.columnType.STRING); + type.setLongValue(Long.parseLong(con.getValue())); + } + + return type; + } } \ No newline at end of file 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 9e0d044e1e..fb5251acf4 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 @@ -75,32 +75,56 @@ public class Utils { } - public static boolean checkColumnType(String column) { + public static String checkColumnType(String column) { - boolean bool = false; + String type; switch (column) { case "deviceModel": - bool = true; + type = "String"; break; case "vendor": - bool = true; + type = "String"; break; case "osVersion": - bool = true; + type = "String"; break; case "connectionType": - bool = true; + type = "String"; break; case "ssid": - bool = true; + type = "String"; + break; + case "imei": + type = "String"; + break; + case "imsi": + type = "String"; + break; + case "batteryLevel": + type = "Double"; + break; + case "internalTotalMemory": + type = "Double"; + break; + case "internalAvailableMemory": + type = "Double"; + break; + case "externalAvailableMemory": + type = "Double"; + break; + case "externalTotalMemory": + type = "Double"; + break; + case "cpuUsage": + type = "Double"; break; default: - bool = false; + type = "String"; break; } - return bool; + return type; } public static Map getDeviceDetailsColumnNames() {