diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.cert.admin.api/src/main/java/org/wso2/carbon/certificate/mgt/cert/jaxrs/api/CertificateManagementAdminService.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.cert.admin.api/src/main/java/org/wso2/carbon/certificate/mgt/cert/jaxrs/api/CertificateManagementAdminService.java index a2b90633ea..6fed010f65 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.cert.admin.api/src/main/java/org/wso2/carbon/certificate/mgt/cert/jaxrs/api/CertificateManagementAdminService.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.cert.admin.api/src/main/java/org/wso2/carbon/certificate/mgt/cert/jaxrs/api/CertificateManagementAdminService.java @@ -35,7 +35,7 @@ import javax.ws.rs.core.Response; } ), tags = { - @Tag(name = "devicemgt_admin", description = "") + @Tag(name = "device_management", description = "") } ) @Api(value = "Certificate Management", description = "This API includes all the certificate management related operations") diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/QueryBuilder.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/QueryBuilder.java index c3e912fcb8..6e3803f2d8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/QueryBuilder.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/QueryBuilder.java @@ -26,18 +26,18 @@ import java.util.Map; public interface QueryBuilder { - Map> buildQueries(List conditions) throws InvalidOperatorException; + Map> buildQueries(List conditions) throws InvalidOperatorException; - String processAND(List conditions) throws InvalidOperatorException; + String processAND(List conditions, ValueType[] valueType, Integer intArr[]) throws InvalidOperatorException; - String processOR(List conditions) throws InvalidOperatorException; + String processOR(List conditions, ValueType[] valueType, Integer intArr[]) throws InvalidOperatorException; - List processLocation(Condition condition) throws InvalidOperatorException; + List processLocation(Condition condition) throws InvalidOperatorException; - List processANDProperties(List conditions) throws InvalidOperatorException; + List processANDProperties(List conditions) throws InvalidOperatorException; - List processORProperties(List conditions) throws InvalidOperatorException; + List processORProperties(List conditions) throws InvalidOperatorException; - String processUpdatedDevices(long epochTime) throws InvalidOperatorException; + QueryHolder processUpdatedDevices(long epochTime) throws InvalidOperatorException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/QueryHolder.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/QueryHolder.java new file mode 100644 index 0000000000..6bc6b1190d --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/QueryHolder.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + + +package org.wso2.carbon.device.mgt.core.search.mgt; + +public class QueryHolder { + + private String query; + private ValueType[] types; + + public String getQuery() { + return query; + } + + public void setQuery(String query) { + this.query = query; + } + + public ValueType[] getTypes() { + return types; + } + + public void setTypes(ValueType[] types) { + this.types = types; + } +} + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/ValueType.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/ValueType.java new file mode 100644 index 0000000000..ea432068ec --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/ValueType.java @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + + +package org.wso2.carbon.device.mgt.core.search.mgt; + +public class ValueType { + + public enum columnType { + STRING, + INTEGER, + DOUBLE, + LONG + } + + private String stringValue; + private int intValue; + private Double doubleValue; + private long longValue; + + private columnType columnType; + + public String getStringValue() { + return stringValue; + } + + public void setStringValue(String stringValue) { + this.stringValue = stringValue; + } + + public int getIntValue() { + return intValue; + } + + public void setIntValue(int intValue) { + this.intValue = intValue; + } + + public Double getDoubleValue() { + return doubleValue; + } + + public void setDoubleValue(Double doubleValue) { + this.doubleValue = doubleValue; + } + + public ValueType.columnType getColumnType() { + return columnType; + } + + public void setColumnType(ValueType.columnType columnType) { + this.columnType = columnType; + } + + public long getLongValue() { + return longValue; + } + + public void setLongValue(long longValue) { + this.longValue = longValue; + } +} + 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 7fe10ba8c4..b2c779c037 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 @@ -62,27 +62,32 @@ public class ProcessorImpl implements Processor { @Override public List execute(SearchContext searchContext) throws SearchMgtException { + if(!Utils.validateOperators(searchContext.getConditions())){ + throw new SearchMgtException("Invalid validator is provided."); + } + QueryBuilder queryBuilder = new QueryBuilderImpl(); List generalDevices = new ArrayList<>(); List> allANDDevices = new ArrayList<>(); List> allORDevices = new ArrayList<>(); List locationDevices = new ArrayList<>(); try { - Map> queries = queryBuilder.buildQueries(searchContext.getConditions()); DeviceManagementDAOFactory.openConnection(); + Map> queries = queryBuilder.buildQueries(searchContext.getConditions()); + if (queries.containsKey(Constants.GENERAL)) { generalDevices = searchDeviceDetailsTable(queries.get(Constants.GENERAL).get(0)); } if (queries.containsKey(Constants.PROP_AND)) { - for (String query : queries.get(Constants.PROP_AND)) { - List andDevices = searchDeviceDetailsTable(query); + for (QueryHolder queryHolder : queries.get(Constants.PROP_AND)) { + List andDevices = searchDeviceDetailsTable(queryHolder); allANDDevices.add(andDevices); } } if (queries.containsKey(Constants.PROP_OR)) { - for (String query : queries.get(Constants.PROP_OR)) { - List orDevices = searchDeviceDetailsTable(query); + for (QueryHolder queryHolder : queries.get(Constants.PROP_OR)) { + List orDevices = searchDeviceDetailsTable(queryHolder); allORDevices.add(orDevices); } } @@ -141,12 +146,12 @@ public class ProcessorImpl implements Processor { @Override public List getUpdatedDevices(long epochTime) throws SearchMgtException { - if((1 + (int)Math.floor(Math.log10(epochTime))) <=10 ) { + if ((1 + (int) Math.floor(Math.log10(epochTime))) <= 10) { epochTime = epochTime * 1000; } QueryBuilder queryBuilder = new QueryBuilderImpl(); try { - String query = queryBuilder.processUpdatedDevices(epochTime); + QueryHolder query = queryBuilder.processUpdatedDevices(epochTime); DeviceManagementDAOFactory.openConnection(); return searchDeviceDetailsTable(query); } catch (InvalidOperatorException e) { @@ -218,7 +223,7 @@ public class ProcessorImpl implements Processor { for (List devices : deLists) { Map deviceMap = new HashMap<>(); - for (Device device: devices) { + for (Device device : devices) { deviceMap.put(device.getId(), device); } maps.add(deviceMap); @@ -241,9 +246,9 @@ public class ProcessorImpl implements Processor { } } - private List searchDeviceDetailsTable(String query) throws SearchDAOException { + private List searchDeviceDetailsTable(QueryHolder queryHolder) throws SearchDAOException { if (log.isDebugEnabled()) { - log.debug("Query : " + query); + log.debug("Query : " + queryHolder.getQuery()); } Connection conn; PreparedStatement stmt = null; @@ -252,7 +257,26 @@ public class ProcessorImpl implements Processor { Map devs = new HashMap<>(); try { conn = this.getConnection(); - stmt = conn.prepareStatement(query); + stmt = conn.prepareStatement(queryHolder.getQuery()); + + int x = 1; + ValueType[] types = queryHolder.getTypes(); + for (ValueType type : types) { + if (type.getColumnType().equals(ValueType.columnType.STRING)) { + stmt.setString(x, type.getStringValue()); + x++; + } else if (type.getColumnType().equals(ValueType.columnType.INTEGER)) { + stmt.setInt(x, type.getIntValue()); + x++; + } else if (type.getColumnType().equals(ValueType.columnType.LONG)){ + stmt.setLong(x, type.getLongValue()); + x++; + } else if(type.getColumnType().equals(ValueType.columnType.DOUBLE)){ + stmt.setDouble(x, type.getDoubleValue()); + x++; + } + } + rs = stmt.executeQuery(); while (rs.next()) { if (!devs.containsKey(rs.getInt("ID"))) { @@ -362,8 +386,8 @@ public class ProcessorImpl implements Processor { } } catch (SQLException e) { throw new SearchDAOException("Error occurred while retrieving the device properties.", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt,rs); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); } 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 251b773123..6291f9cb16 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,9 +23,7 @@ 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.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.*; import java.util.ArrayList; import java.util.HashMap; @@ -41,7 +39,7 @@ public class QueryBuilderImpl implements QueryBuilder { private boolean isDeviceAdminUser; @Override - public Map> buildQueries(List conditions) throws InvalidOperatorException { + public Map> buildQueries(List conditions) throws InvalidOperatorException { List andColumns = new ArrayList<>(); List orColumns = new ArrayList<>(); List otherANDColumns = new ArrayList<>(); @@ -82,10 +80,27 @@ public class QueryBuilderImpl implements QueryBuilder { } } - Map> queries = new HashMap<>(); + Map> queries = new HashMap<>(); if ((!andColumns.isEmpty()) || (!orColumns.isEmpty())) { - queries.put(Constants.GENERAL, Utils.convertStringToList(this.getGenericQueryPart() + this.processAND(andColumns) + - this.processOR(orColumns))); + // Size is taken as the sum of both columns and for tenant id. + ValueType valueTypeArray[] = new ValueType[andColumns.size() + orColumns.size() + 1]; + +// String query =Utils.convertStringToList( + + // passing the integer value to the x so that array is correctly passed. + Integer intArr[] = new Integer[1]; + intArr[0] = 1; + //int x = 1; + String query = this.getGenericQueryPart(valueTypeArray) + + this.processAND(andColumns, valueTypeArray, intArr) + + this.processOR(orColumns, valueTypeArray, intArr); + List queryHolders = new ArrayList<>(); + QueryHolder queryHolder = new QueryHolder(); + queryHolder.setQuery(query); + queryHolder.setTypes(valueTypeArray); + queryHolders.add(queryHolder); + + queries.put(Constants.GENERAL, queryHolders); } if (!otherANDColumns.isEmpty()) { queries.put(Constants.PROP_AND, this.processANDProperties(otherANDColumns)); @@ -108,124 +123,262 @@ public class QueryBuilderImpl implements QueryBuilder { } @Override - public String processAND(List conditions) throws InvalidOperatorException { + public String processAND(List conditions, ValueType[] valueType, Integer intArr[]) throws InvalidOperatorException { String querySuffix = ""; - for (Condition con : conditions) { - if (Utils.checkDeviceDetailsColumns(con.getKey())) { - if (con.operator.equals(WILDCARD_OPERATOR)){ - querySuffix = querySuffix + " OR DD." + Utils.getDeviceDetailsColumnNames().get(con.getKey()) - + " LIKE \'%" + con.getValue() + "%\'"; - } else { - querySuffix = querySuffix + " AND DD." + Utils.getDeviceDetailsColumnNames().get(con.getKey()) + con - .getOperator() + Utils.getConvertedValue(con.getKey(), con.getValue()); + try { + // TODO: find upto what address location of the array has filled. + int x = intArr[0]; + for (Condition con : conditions) { + if (Utils.checkDeviceDetailsColumns(con.getKey())) { + if (con.operator.equals(WILDCARD_OPERATOR)) { + querySuffix = querySuffix + " OR DD." + Utils.getDeviceDetailsColumnNames().get(con.getKey()) + + " LIKE ? "; + ValueType type = new ValueType(); + type.setColumnType(ValueType.columnType.STRING); + 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(Utils.getConvertedValue(con.getKey(), con.getValue())); + } else { + type.setColumnType(ValueType.columnType.INTEGER); + type.setIntValue(Integer.parseInt(Utils.getConvertedValue(con.getKey(), con.getValue()))); + } + valueType[x] = type; + x++; + } + } else if (Utils.checkDeviceLocationColumns(con.getKey().toLowerCase())) { + querySuffix = querySuffix + " AND DL." + Utils.getDeviceLocationColumnNames().get(con.getKey().toLowerCase()) + + con.getOperator() + " ? "; + ValueType type = new ValueType(); + type.setColumnType(ValueType.columnType.STRING); + type.setStringValue(con.getValue()); + valueType[x] = type; + x++; } - } else if (Utils.checkDeviceLocationColumns(con.getKey().toLowerCase())) { - querySuffix = querySuffix + " AND DL." + Utils.getDeviceLocationColumnNames().get(con.getKey().toLowerCase()) + - con.getOperator() + con.getValue(); } + intArr[0] = x; + } catch (Exception e) { + throw new InvalidOperatorException("Error occurred while building the sql", e); } return querySuffix; } @Override - public String processOR(List conditions) throws InvalidOperatorException { + public String processOR(List conditions, ValueType[] valueType, Integer intArr[]) throws InvalidOperatorException { String querySuffix = ""; - for (Condition con : conditions) { - if (Utils.checkDeviceDetailsColumns(con.getKey())) { - if (con.operator.equals(WILDCARD_OPERATOR)) { - querySuffix = querySuffix + " OR DD." + Utils.getDeviceDetailsColumnNames().get(con.getKey()) - + " LIKE \'%" + con.getValue() + "%\'"; - } else { - querySuffix = querySuffix + " OR DD." + Utils.getDeviceDetailsColumnNames().get(con.getKey()) + con - .getOperator() + Utils.getConvertedValue(con.getKey(), con.getValue()); + // TODO: find upto what address location of the array has filled. + try { + int x = intArr[0]; + for (Condition con : conditions) { + if (Utils.checkDeviceDetailsColumns(con.getKey())) { + if (con.operator.equals(WILDCARD_OPERATOR)) { + querySuffix = querySuffix + " OR DD." + Utils.getDeviceDetailsColumnNames().get(con.getKey()) + + " LIKE ? "; + ValueType type = new ValueType(); + type.setColumnType(ValueType.columnType.STRING); + 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(Utils.getConvertedValue(con.getKey(), con.getValue())); + } else { + type.setColumnType(ValueType.columnType.INTEGER); + type.setIntValue(Integer.parseInt(Utils.getConvertedValue(con.getKey(), con.getValue()))); + } + valueType[x] = type; + x++; + } + } else if (Utils.checkDeviceLocationColumns(con.getKey().toLowerCase())) { + querySuffix = + querySuffix + " OR DL." + Utils.getDeviceLocationColumnNames().get(con.getKey().toLowerCase()) + + con.getOperator() + " ? "; + ValueType type = new ValueType(); + type.setColumnType(ValueType.columnType.STRING); + type.setStringValue(con.getValue()); + valueType[x] = type; + x++; } - } else if (Utils.checkDeviceLocationColumns(con.getKey().toLowerCase())) { - querySuffix = - querySuffix + " OR DL." + Utils.getDeviceLocationColumnNames().get(con.getKey().toLowerCase()) - + con.getOperator() + con.getValue(); } + intArr[0] = x; + } catch (Exception e) { + throw new InvalidOperatorException("Error occurred while building the sql", e); } return querySuffix; } @Override - public List processLocation(Condition condition) throws InvalidOperatorException { - List queryList = new ArrayList<>(); - queryList.add(this.buildLocationQuery(condition.getValue())); - return queryList; + public List processLocation(Condition condition) throws InvalidOperatorException { + List queryHolders = new ArrayList<>(); + queryHolders.add(this.buildLocationQuery(condition.getValue())); + return queryHolders; } @Override - public List processANDProperties(List conditions) throws InvalidOperatorException { + public List processANDProperties(List conditions) throws InvalidOperatorException { return this.getQueryList(conditions); } @Override - public List processORProperties(List conditions) throws InvalidOperatorException { + public List processORProperties(List conditions) throws InvalidOperatorException { return this.getQueryList(conditions); } @Override - public String processUpdatedDevices(long epochTime) throws InvalidOperatorException { - return this.getGenericQueryPart() + " AND ( DD.UPDATE_TIMESTAMP > " + epochTime + - " OR DL.UPDATE_TIMESTAMP > " + epochTime + " )"; + public QueryHolder processUpdatedDevices(long epochTime) throws InvalidOperatorException { + try { + ValueType valueTypeArray[] = new ValueType[3]; + String query = this.getGenericQueryPart(valueTypeArray) + " AND ( DD.UPDATE_TIMESTAMP > ? " + + "OR DL.UPDATE_TIMESTAMP > ? )"; + + ValueType val1 = new ValueType(); + val1.setColumnType(ValueType.columnType.LONG); + val1.setLongValue(epochTime); + valueTypeArray[1] = val1; + + ValueType val2 = new ValueType(); + val2.setColumnType(ValueType.columnType.LONG); + val2.setLongValue(epochTime); + valueTypeArray[2] = val2; + + QueryHolder queryHolder = new QueryHolder(); + queryHolder.setQuery(query); + queryHolder.setTypes(valueTypeArray); + + return queryHolder; + } catch (Exception e) { + throw new InvalidOperatorException("Error occurred while building the for the updated devices.", e); + } } - private List getQueryList(List conditions) { - List queryList = new ArrayList<>(); - for (Condition con : conditions) { + private List getQueryList(List conditions) throws InvalidOperatorException { + try { + List queryHolders = new ArrayList<>(); + for (Condition con : conditions) { + + QueryHolder query = new QueryHolder(); + ValueType valueTypeArray[] = new ValueType[3]; + + String querySuffix = this.getPropertyQueryPart(valueTypeArray) + " AND DI.KEY_FIELD = " + " ? " + + " AND DI.VALUE_FIELD " + con.getOperator() + " ? "; + ValueType key = new ValueType(); + key.setColumnType(ValueType.columnType.STRING); + key.setStringValue(con.getKey()); + valueTypeArray[1] = key; + + ValueType value = new ValueType(); + value.setColumnType(ValueType.columnType.STRING); + value.setStringValue(con.getValue()); + valueTypeArray[2] = value; - String querySuffix = this.getPropertyQueryPart() + " AND DI.KEY_FIELD = " + "\'" + con.getKey() + "\'" + - " AND DI.VALUE_FIELD " + con.getOperator() + "\'" + con.getValue() + "\'"; - queryList.add(querySuffix); + query.setQuery(querySuffix); + query.setTypes(valueTypeArray); + + queryHolders.add(query); + } + return queryHolders; + } catch (Exception e) { + throw new InvalidOperatorException("Error occurred while building the sql", e); } - return queryList; } - private String buildLocationQuery(String location) { + private QueryHolder buildLocationQuery(String location) throws InvalidOperatorException { + try { + ValueType valueTypeArray[] = new ValueType[7]; + String query = this.getGenericQueryPart(valueTypeArray); + query = query + " AND (DL.STREET1 LIKE ? "; + query = query + " OR DL.STREET2 LIKE ? "; + query = query + " OR DL.CITY LIKE ? "; + query = query + " OR DL.STATE LIKE ? "; + query = query + " OR DL.COUNTRY LIKE ? "; + query = query + " OR DL.ZIP LIKE ? )"; + + ValueType value = new ValueType(); + value.setColumnType(ValueType.columnType.STRING); + value.setStringValue("%" + location + "%"); + + // Same location is passed to each place + valueTypeArray[1] = value; + valueTypeArray[2] = value; + valueTypeArray[3] = value; + valueTypeArray[4] = value; + valueTypeArray[5] = value; + valueTypeArray[6] = value; - String query = this.getGenericQueryPart(); - query = query + " AND (DL.STREET1 LIKE \'%" + location + "%\'"; - query = query + " OR DL.STREET2 LIKE \'%" + location + "%\'"; - query = query + " OR DL.CITY LIKE \'%" + location + "%\'"; - query = query + " OR DL.STATE LIKE \'%" + location + "%\'"; - query = query + " OR DL.COUNTRY LIKE \'%" + location + "%\'"; - query = query + " OR DL.ZIP LIKE \'%" + location + "%\')"; - return query; + QueryHolder queryHolder = new QueryHolder(); + queryHolder.setQuery(query); + queryHolder.setTypes(valueTypeArray); + + return queryHolder; + } catch (Exception e) { + throw new InvalidOperatorException("Error occurred while building the sql for location.", e); + } } - 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" + - "DT.NAME AS DEVICE_TYPE_NAME, DD.DEVICE_ID, DD.DEVICE_MODEL, DD.VENDOR, \n" + - "DD.OS_VERSION, DD.OS_BUILD_DATE, 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, DE.OWNER, DE.OWNERSHIP, DE.STATUS " + - "AS DE_STATUS 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" + - "INNER JOIN DM_ENROLMENT AS DE ON D.ID=DE.DEVICE_ID\n" + - "WHERE D.TENANT_ID = " + PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + private String getGenericQueryPart(ValueType[] valueTypeArray) throws InvalidOperatorException { + try { + String query = "SELECT D.ID, D.DESCRIPTION, D.NAME, \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.OS_BUILD_DATE, 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, DE.OWNER, DE.OWNERSHIP, DE.STATUS " + + "AS DE_STATUS 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" + + "INNER JOIN DM_ENROLMENT AS DE ON D.ID=DE.DEVICE_ID\n" + + "WHERE D.TENANT_ID = ? "; + + ValueType type = new ValueType(); + type.setIntValue(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); + type.setColumnType(ValueType.columnType.INTEGER); + valueTypeArray[0] = type; + return query; + + } catch (Exception e) { + throw new InvalidOperatorException("Error occurred while building the sql", e); + } } - 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" + - "DT.NAME AS DEVICE_TYPE_NAME, DD.DEVICE_ID, DD.DEVICE_MODEL, DD.VENDOR, \n" + - "DD.OS_VERSION, DD.OS_BUILD_DATE, 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, \n" + - "DE.OWNER, DE.OWNERSHIP, DE.STATUS AS DE_STATUS " + - "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" + - "INNER JOIN DM_ENROLMENT AS DE ON D.ID=DE.DEVICE_ID\n" + - "LEFT JOIN DM_DEVICE_INFO AS DI ON DI.DEVICE_ID=D.ID\n" + - "WHERE D.TENANT_ID = " + - PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + private String getPropertyQueryPart(ValueType[] valueTypeArray) throws InvalidOperatorException { + try { + String query = "SELECT D.ID, D.DESCRIPTION, D.NAME, \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.OS_BUILD_DATE, 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, \n" + + "DE.OWNER, DE.OWNERSHIP, DE.STATUS AS DE_STATUS " + + "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" + + "INNER JOIN DM_ENROLMENT AS DE ON D.ID=DE.DEVICE_ID\n" + + "LEFT JOIN DM_DEVICE_INFO AS DI ON DI.DEVICE_ID=D.ID\n" + + "WHERE D.TENANT_ID = ? "; + + ValueType type = new ValueType(); + type.setIntValue(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); + type.setColumnType(ValueType.columnType.INTEGER); + valueTypeArray[0] = type; + return query; + + } catch (Exception e) { + throw new InvalidOperatorException("Error occurred while building the sql", e); + } } } \ 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 d28666ba88..10e12f24ce 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 @@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.core.search.mgt.impl; import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.search.Condition; import java.util.ArrayList; import java.util.HashMap; @@ -31,6 +32,8 @@ public class Utils { private static Map genericColumnsMap = new HashMap<>(); private static Map locationColumnsMap = new HashMap<>(); + private static Map operators = new HashMap<>(); + static { genericColumnsMap.put("deviceModel", "DEVICE_MODEL"); genericColumnsMap.put("vendor", "VENDOR"); @@ -58,6 +61,18 @@ public class Utils { locationColumnsMap.put("zip", "STATE"); locationColumnsMap.put("country", "COUNTRY"); + //=, >, <, >=, <=, <>, !=, !>, !< + operators.put("=", "="); + operators.put(">", ">"); + operators.put("<", "<"); + operators.put(">=", ">="); + operators.put("<=", "<="); + operators.put("<>", "<>"); + operators.put("!=", "!="); + operators.put("!>", "!>"); + operators.put("!<", "!<"); + operators.put("%", "%"); + } public static boolean checkColumnType(String column) { @@ -142,5 +157,15 @@ public class Utils { return str.substring(0, str.length() - 1); } + + public static boolean validateOperators(List conditions) { + for (Condition con : conditions) { + if (!operators.containsKey(con.getOperator())) { + return false; + } + } + return true; + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.device.search/public/js/bottomJs.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.device.search/public/js/bottomJs.js index c885566db8..68a4292ea8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.device.search/public/js/bottomJs.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.device.search/public/js/bottomJs.js @@ -47,7 +47,7 @@ var dynamicForm = '
' + + 'Internal Available Memory ' + ' '; } else { return ''; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/uuf-template-app/lib/utils.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/uuf-template-app/lib/utils.js index c108e1e5bf..a7e006682d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/uuf-template-app/lib/utils.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/uuf-template-app/lib/utils.js @@ -365,6 +365,23 @@ var utils = {}; content = content.replace(/\$\{server\.http_port}/g, getProperty("carbon.http.port")); content = content.replace(/\$\{server\.https_port}/g, getProperty("carbon.https.port")); + //parsing system params + var paramPattern = new RegExp("%(.*?)%", "g"); + var out = content; + while ((matches = paramPattern.exec(content)) !== null) { + // This is necessary to avoid infinite loops with zero-width matches + if (matches.index === paramPattern.lastIndex) { + paramPattern.lastIndex++; + } + if (matches.length == 2) { + var property = process.getProperty(matches[1]); + if (property) { + out = out.replace(new RegExp("%" + matches[1] + "%", "g"), property); + } + } + } + content = out; + var appConf = parse(content); application.put(constants.CACHE_KEY_APP_CONF, appConf); application.put(constants.CACHE_KEY_APP_CONF_FILE_LMD,