Add serial number and custom property filters to device count query

pull/549/head
Pramila Niroshan 2 weeks ago
parent a10bdd1d05
commit f84a8cd55c

@ -1032,6 +1032,10 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
boolean isSinceProvided = false; boolean isSinceProvided = false;
List<String> tagList = request.getTags(); List<String> tagList = request.getTags();
boolean isTagsProvided = false; boolean isTagsProvided = false;
String serial = request.getSerialNumber();
boolean isSerialProvided = false;
Map<String, String> customProperties = request.getCustomProperty();
boolean isCustomPropertiesProvided = !customProperties.isEmpty();
try { try {
Connection conn = getConnection(); Connection conn = getConnection();
@ -1095,6 +1099,19 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
sql += ") GROUP BY dtm.ENROLMENT_ID HAVING COUNT(DISTINCT t.NAME) = ? )"; sql += ") GROUP BY dtm.ENROLMENT_ID HAVING COUNT(DISTINCT t.NAME) = ? )";
isTagsProvided = true; isTagsProvided = true;
} }
if (serial != null && !serial.isEmpty()) {
sql += " AND EXISTS (SELECT VALUE_FIELD FROM DM_DEVICE_INFO di " +
"WHERE di.DEVICE_ID = d1.DEVICE_ID AND di.KEY_FIELD = 'serial' " +
"AND di.VALUE_FIELD LIKE ?)";
isSerialProvided = true;
}
if (isCustomPropertiesProvided) {
for (String key : customProperties.keySet()) {
sql += " AND EXISTS (SELECT VALUE_FIELD FROM DM_DEVICE_INFO di2 " +
"WHERE di2.DEVICE_ID = d1.DEVICE_ID AND di2.KEY_FIELD = '" + key + "' " +
"AND di2.VALUE_FIELD LIKE ?)";
}
}
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
int paramIdx = 1; int paramIdx = 1;
@ -1129,6 +1146,14 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
} }
stmt.setInt(paramIdx++, tagList.size()); stmt.setInt(paramIdx++, tagList.size());
} }
if (isSerialProvided) {
stmt.setString(paramIdx++, "%" + serial + "%");
}
if (isCustomPropertiesProvided) {
for (String value : customProperties.values()) {
stmt.setString(paramIdx++, "%" + value + "%");
}
}
try (ResultSet rs = stmt.executeQuery()) { try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) { if (rs.next()) {
@ -1319,6 +1344,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
boolean isSinceProvided = false; boolean isSinceProvided = false;
List<String> tagList = request.getTags(); List<String> tagList = request.getTags();
boolean isTagsProvided = false; boolean isTagsProvided = false;
String serial = request.getSerialNumber();
boolean isSerialProvided = false;
try { try {
Connection conn = getConnection(); Connection conn = getConnection();
@ -1370,6 +1397,23 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
sql += ") GROUP BY dtm.ENROLMENT_ID HAVING COUNT(DISTINCT t.NAME) = ? )"; sql += ") GROUP BY dtm.ENROLMENT_ID HAVING COUNT(DISTINCT t.NAME) = ? )";
isTagsProvided = true; isTagsProvided = true;
} }
if (serial != null && !serial.isEmpty()) {
sql += " AND EXISTS (" +
"SELECT VALUE_FIELD FROM DM_DEVICE_INFO di " +
"WHERE di.DEVICE_ID = d1.ID " +
"AND di.KEY_FIELD = 'serial' " +
"AND di.VALUE_FIELD LIKE ?)";
isSerialProvided = true;
}
if (!request.getCustomProperty().isEmpty()) {
for (Map.Entry<String, String> entry : request.getCustomProperty().entrySet()) {
sql += " AND EXISTS (" +
"SELECT VALUE_FIELD FROM DM_DEVICE_INFO di2 " +
"WHERE di2.DEVICE_ID = d1.ID " +
"AND di2.KEY_FIELD = '" + entry.getKey() + "' " +
"AND di2.VALUE_FIELD LIKE ?)";
}
}
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
int paramIdx = 1; int paramIdx = 1;
@ -1402,6 +1446,14 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
} }
stmt.setInt(paramIdx++, tagList.size()); stmt.setInt(paramIdx++, tagList.size());
} }
if (isSerialProvided) {
stmt.setString(paramIdx++, "%" + serial + "%");
}
if (!request.getCustomProperty().isEmpty()) {
for (Map.Entry<String, String> entry : request.getCustomProperty().entrySet()) {
stmt.setString(paramIdx++, "%" + entry.getValue() + "%");
}
}
try (ResultSet rs = stmt.executeQuery()) { try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) { if (rs.next()) {

Loading…
Cancel
Save