From 9e4d84dfb2bf60666c5a6740eaca90ddd7018f42 Mon Sep 17 00:00:00 2001 From: Harshan Liyanage Date: Tue, 28 Mar 2017 12:37:28 +0530 Subject: [PATCH] Fix for Advanced Search Issue #670 --- .../device/mgt/core/search/mgt/impl/QueryBuilderImpl.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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 6291f9cb16..253e95fdd0 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 @@ -35,6 +35,7 @@ public class QueryBuilderImpl implements QueryBuilder { private static final Log log = LogFactory.getLog(QueryBuilderImpl.class); private final String WILDCARD_OPERATOR = "%"; + private final String OR_OPERATOR = "OR"; private String current_username; private boolean isDeviceAdminUser; @@ -131,7 +132,7 @@ public class QueryBuilderImpl implements QueryBuilder { for (Condition con : conditions) { if (Utils.checkDeviceDetailsColumns(con.getKey())) { if (con.operator.equals(WILDCARD_OPERATOR)) { - querySuffix = querySuffix + " OR DD." + Utils.getDeviceDetailsColumnNames().get(con.getKey()) + querySuffix = querySuffix + " AND DD." + Utils.getDeviceDetailsColumnNames().get(con.getKey()) + " LIKE ? "; ValueType type = new ValueType(); type.setColumnType(ValueType.columnType.STRING); @@ -215,6 +216,11 @@ public class QueryBuilderImpl implements QueryBuilder { } catch (Exception e) { throw new InvalidOperatorException("Error occurred while building the sql", e); } + if (!querySuffix.isEmpty()) { + //Replacing the first OR operator as it's unnecessary + querySuffix = querySuffix.replaceFirst(OR_OPERATOR, ""); + querySuffix = " AND (" + querySuffix + ")"; + } return querySuffix; }