From 2338a6745d9b6211fde928b9ace1116a444da261 Mon Sep 17 00:00:00 2001 From: pramilaniroshan Date: Thu, 7 Nov 2024 23:32:09 +0530 Subject: [PATCH] Add serial number and custom property filters to device count query --- .../core/dao/impl/AbstractDeviceDAOImpl.java | 27 +++++++++++++++++++ 1 file changed, 27 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..077c4de39f 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 @@ -1319,6 +1319,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 +1372,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 +1421,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()) {