From f84a8cd55ccd9162b0dfa7f6ceba4676dfe40360 Mon Sep 17 00:00:00 2001 From: pramilaniroshan Date: Fri, 8 Nov 2024 00:07:45 +0530 Subject: [PATCH] Add serial number and custom property filters to device count query --- .../core/dao/impl/AbstractDeviceDAOImpl.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index ffebacf1e0..5780ca3b3f 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -1032,6 +1032,10 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { boolean isSinceProvided = false; List tagList = request.getTags(); boolean isTagsProvided = false; + String serial = request.getSerialNumber(); + boolean isSerialProvided = false; + Map customProperties = request.getCustomProperty(); + boolean isCustomPropertiesProvided = !customProperties.isEmpty(); try { Connection conn = getConnection(); @@ -1095,6 +1099,19 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { sql += ") GROUP BY dtm.ENROLMENT_ID HAVING COUNT(DISTINCT t.NAME) = ? )"; 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)) { int paramIdx = 1; @@ -1129,6 +1146,14 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } 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()) { if (rs.next()) { @@ -1319,6 +1344,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { boolean isSinceProvided = false; List tagList = request.getTags(); boolean isTagsProvided = false; + String serial = request.getSerialNumber(); + boolean isSerialProvided = false; try { Connection conn = getConnection(); @@ -1370,6 +1397,23 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { sql += ") GROUP BY dtm.ENROLMENT_ID HAVING COUNT(DISTINCT t.NAME) = ? )"; 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 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)) { int paramIdx = 1; @@ -1402,6 +1446,14 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } stmt.setInt(paramIdx++, tagList.size()); } + if (isSerialProvided) { + stmt.setString(paramIdx++, "%" + serial + "%"); + } + if (!request.getCustomProperty().isEmpty()) { + for (Map.Entry entry : request.getCustomProperty().entrySet()) { + stmt.setString(paramIdx++, "%" + entry.getValue() + "%"); + } + } try (ResultSet rs = stmt.executeQuery()) { if (rs.next()) {