Added checks for other data types.

revert-70aa11f8
Menaka Jayawardena 7 years ago
parent 485a0d3721
commit 26022daf03

@ -251,7 +251,7 @@ public class ProcessorImpl implements Processor {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Query : " + queryHolder.getQuery()); log.debug("Query : " + queryHolder.getQuery());
} }
Connection conn; Connection conn = null;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
List<Device> devices = new ArrayList<>(); List<Device> devices = new ArrayList<>();

@ -23,7 +23,11 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.search.Condition; 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.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@ -93,8 +97,8 @@ public class QueryBuilderImpl implements QueryBuilder {
intArr[0] = 1; intArr[0] = 1;
//int x = 1; //int x = 1;
String query = this.getGenericQueryPart(valueTypeArray) + String query = this.getGenericQueryPart(valueTypeArray) +
this.processAND(andColumns, valueTypeArray, intArr) + this.processAND(andColumns, valueTypeArray, intArr) +
this.processOR(orColumns, valueTypeArray, intArr); this.processOR(orColumns, valueTypeArray, intArr);
List<QueryHolder> queryHolders = new ArrayList<>(); List<QueryHolder> queryHolders = new ArrayList<>();
QueryHolder queryHolder = new QueryHolder(); QueryHolder queryHolder = new QueryHolder();
queryHolder.setQuery(query); queryHolder.setQuery(query);
@ -135,20 +139,13 @@ public class QueryBuilderImpl implements QueryBuilder {
+ " LIKE ? "; + " LIKE ? ";
ValueType type = new ValueType(); ValueType type = new ValueType();
type.setColumnType(ValueType.columnType.STRING); type.setColumnType(ValueType.columnType.STRING);
type.setStringValue("%"+con.getValue()+"%"); type.setStringValue("%" + con.getValue() + "%");
valueType[x] = type; valueType[x] = type;
x++; x++;
} else { } else {
querySuffix = querySuffix + " AND DD." + Utils.getDeviceDetailsColumnNames().get(con.getKey()) + con querySuffix = querySuffix + " AND DD." + Utils.getDeviceDetailsColumnNames().get(con.getKey()) + con
.getOperator() + " ? "; .getOperator() + " ? ";
ValueType type = new ValueType(); ValueType type = this.getValueType(con);
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[x] = type; valueType[x] = type;
x++; x++;
} }
@ -182,21 +179,15 @@ public class QueryBuilderImpl implements QueryBuilder {
+ " LIKE ? "; + " LIKE ? ";
ValueType type = new ValueType(); ValueType type = new ValueType();
type.setColumnType(ValueType.columnType.STRING); type.setColumnType(ValueType.columnType.STRING);
type.setStringValue("%"+con.getValue()+"%"); type.setStringValue("%" + con.getValue() + "%");
valueType[x] = type; valueType[x] = type;
x++; x++;
} else { } else {
querySuffix = querySuffix + " OR DD." + Utils.getDeviceDetailsColumnNames().get(con.getKey()) + con querySuffix = querySuffix + " OR DD." + Utils.getDeviceDetailsColumnNames().get(con.getKey()) + con
.getOperator() + " ? "; .getOperator() + " ? ";
ValueType type = new ValueType(); ValueType type = this.getValueType(con);
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[x] = type; valueType[x] = type;
x++; x++;
} }
@ -386,4 +377,35 @@ public class QueryBuilderImpl implements QueryBuilder {
throw new InvalidOperatorException("Error occurred while building the sql", e); 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;
}
} }

@ -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) { switch (column) {
case "deviceModel": case "deviceModel":
bool = true; type = "String";
break; break;
case "vendor": case "vendor":
bool = true; type = "String";
break; break;
case "osVersion": case "osVersion":
bool = true; type = "String";
break; break;
case "connectionType": case "connectionType":
bool = true; type = "String";
break; break;
case "ssid": 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; break;
default: default:
bool = false; type = "String";
break; break;
} }
return bool; return type;
} }
public static Map<String, String> getDeviceDetailsColumnNames() { public static Map<String, String> getDeviceDetailsColumnNames() {

Loading…
Cancel
Save