From cd2ffa7cc5e989c5e9bacd77adb838fee7675759 Mon Sep 17 00:00:00 2001 From: Rajitha Kumara Date: Mon, 22 Jul 2024 15:12:54 +0530 Subject: [PATCH] Fix device count related issues --- .../mgt/common/DeviceSubscription.java | 9 +++ .../mgt/common/SubscriptionData.java | 9 +++ .../mgt/SubscriptionManagementHelperUtil.java | 16 +++- ...bscriptionManagementHelperServiceImpl.java | 9 +-- ...bscriptionManagementHelperServiceImpl.java | 17 ++--- ...bscriptionManagementHelperServiceImpl.java | 9 +-- ...bscriptionManagementHelperServiceImpl.java | 11 ++- .../core/device/mgt/core/dao/DeviceDAO.java | 3 + .../core/dao/impl/AbstractDeviceDAOImpl.java | 73 ++++++++++++++++++- .../DeviceManagementProviderService.java | 2 + .../DeviceManagementProviderServiceImpl.java | 26 +++++++ 11 files changed, 153 insertions(+), 31 deletions(-) diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/DeviceSubscription.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/DeviceSubscription.java index 7d4efd22d9..16e1d7553f 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/DeviceSubscription.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/DeviceSubscription.java @@ -23,6 +23,7 @@ import java.sql.Timestamp; public class DeviceSubscription { private int deviceId; + private int subscriptionId; private String deviceName; private String deviceIdentifier; private String deviceStatus; @@ -32,6 +33,14 @@ public class DeviceSubscription { private Timestamp dateOfLastUpdate; private SubscriptionData subscriptionData; + public int getSubscriptionId() { + return subscriptionId; + } + + public void setSubscriptionId(int subscriptionId) { + this.subscriptionId = subscriptionId; + } + public int getDeviceId() { return deviceId; } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionData.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionData.java index 16dad4467d..cef9a12508 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionData.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionData.java @@ -26,6 +26,7 @@ public class SubscriptionData { private String triggeredBy; private String subscriptionType; private Timestamp triggeredAt; + private int subscriptionId; public String getDeviceSubscriptionStatus() { return deviceSubscriptionStatus; @@ -58,4 +59,12 @@ public class SubscriptionData { public void setTriggeredAt(Timestamp triggeredAt) { this.triggeredAt = triggeredAt; } + + public int getSubscriptionId() { + return subscriptionId; + } + + public void setSubscriptionId(int subscriptionId) { + this.subscriptionId = subscriptionId; + } } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/SubscriptionManagementHelperUtil.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/SubscriptionManagementHelperUtil.java index 0028e71581..ebc2e7f7cc 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/SubscriptionManagementHelperUtil.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/SubscriptionManagementHelperUtil.java @@ -40,10 +40,10 @@ import java.util.stream.Collectors; public class SubscriptionManagementHelperUtil { public static List getDeviceSubscriptionData(List deviceSubscriptionDTOS, DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria, - boolean isUnsubscribed) + boolean isUnsubscribed, int limit, int offset) throws DeviceManagementException { List deviceIds = deviceSubscriptionDTOS.stream().map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); - PaginationRequest paginationRequest = new PaginationRequest(0, -1); + PaginationRequest paginationRequest = new PaginationRequest(offset, limit); paginationRequest.setDeviceName(deviceSubscriptionFilterCriteria.getName()); paginationRequest.setDeviceStatus(deviceSubscriptionFilterCriteria.getDeviceStatus()); paginationRequest.setOwner(deviceSubscriptionFilterCriteria.getOwner()); @@ -51,6 +51,17 @@ public class SubscriptionManagementHelperUtil { return populateDeviceData(deviceSubscriptionDTOS, devices, isUnsubscribed); } + public static int getTotalDeviceSubscriptionCount(List deviceSubscriptionDTOS, + DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria) + throws DeviceManagementException { + List deviceIds = deviceSubscriptionDTOS.stream().map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); + PaginationRequest paginationRequest = new PaginationRequest(-1, -1); + paginationRequest.setDeviceName(deviceSubscriptionFilterCriteria.getName()); + paginationRequest.setDeviceStatus(deviceSubscriptionFilterCriteria.getDeviceStatus()); + paginationRequest.setOwner(deviceSubscriptionFilterCriteria.getOwner()); + return HelperUtil.getDeviceManagementProviderService().getDeviceCountByDeviceIds(paginationRequest, deviceIds); + } + private static List populateDeviceData(List deviceSubscriptionDTOS, List devices, boolean isUnsubscribed) { List deviceSubscriptions = new ArrayList<>(); @@ -82,6 +93,7 @@ public class SubscriptionManagementHelperUtil { subscriptionData.setTriggeredAt(deviceSubscriptionDTO.getSubscribedTimestamp()); subscriptionData.setDeviceSubscriptionStatus(deviceSubscriptionDTO.getStatus()); subscriptionData.setSubscriptionType(deviceSubscriptionDTO.getActionTriggeredFrom()); + subscriptionData.setSubscriptionId(deviceSubscriptionDTO.getId()); return subscriptionData; } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/DeviceBasedSubscriptionManagementHelperServiceImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/DeviceBasedSubscriptionManagementHelperServiceImpl.java index 13ccecc0ee..51871d28fc 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/DeviceBasedSubscriptionManagementHelperServiceImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/DeviceBasedSubscriptionManagementHelperServiceImpl.java @@ -98,14 +98,13 @@ public class DeviceBasedSubscriptionManagementHelperServiceImpl implements Subsc } else { deviceSubscriptionDTOS = subscriptionDAO.getAllSubscriptionsDetails(applicationReleaseDTO. getId(), isUnsubscribe, tenantId, dbSubscriptionStatus, null, - deviceSubscriptionFilterCriteria.getTriggeredBy(), offset, limit); + deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); - deviceCount = subscriptionDAO.getAllSubscriptionsCount(applicationReleaseDTO. - getId(), isUnsubscribe, tenantId, dbSubscriptionStatus, null, - deviceSubscriptionFilterCriteria.getTriggeredBy()); + deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS, + subscriptionInfo.getDeviceSubscriptionFilterCriteria()); } List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe); + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, limit, offset); return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions); } catch (DeviceManagementException e) { String msg = "Error encountered while getting device details"; diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/GroupBasedSubscriptionManagementHelperServiceImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/GroupBasedSubscriptionManagementHelperServiceImpl.java index 0756495098..cac63111a6 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/GroupBasedSubscriptionManagementHelperServiceImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/GroupBasedSubscriptionManagementHelperServiceImpl.java @@ -99,11 +99,11 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr GroupDetailsDTO groupDetailsDTO; List dbSubscriptionStatus = SubscriptionManagementHelperUtil.getDBSubscriptionStatus(subscriptionInfo.getDeviceSubscriptionStatus()); - List allDeviceIdsOwnByGroup = groupManagementProviderService.getGroupDetailsWithDevices(subscriptionInfo.getIdentifier(), - applicationDTO.getDeviceTypeId(), deviceSubscriptionFilterCriteria.getOwner(), deviceSubscriptionFilterCriteria.getName(), - deviceSubscriptionFilterCriteria.getDeviceStatus(), -1, -1).getDeviceIds(); - if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) { + List allDeviceIdsOwnByGroup = groupManagementProviderService.getGroupDetailsWithDevices(subscriptionInfo.getIdentifier(), + applicationDTO.getDeviceTypeId(), deviceSubscriptionFilterCriteria.getOwner(), deviceSubscriptionFilterCriteria.getName(), + deviceSubscriptionFilterCriteria.getDeviceStatus(), -1, -1).getDeviceIds(); + deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, allDeviceIdsOwnByGroup, null, null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); @@ -130,12 +130,11 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr isUnsubscribe, tenantId, paginatedDeviceIdsOwnByGroup, dbSubscriptionStatus, null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); - deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, allDeviceIdsOwnByGroup, dbSubscriptionStatus, - null, deviceSubscriptionFilterCriteria.getTriggeredBy()); + deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS, + subscriptionInfo.getDeviceSubscriptionFilterCriteria()); } List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe); + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, limit, offset); return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions); } catch (GroupManagementException e) { String msg = "Error encountered while retrieving group details for group: " + subscriptionInfo.getIdentifier(); @@ -193,7 +192,7 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr getAllDevicesOfGroup(subscriptionInfo.getIdentifier(), false); List deviceIdsOwnByGroup = devices.stream().map(Device::getId).collect(Collectors.toList()); SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO. - getSubscriptionStatistic(deviceIdsOwnByGroup, subscriptionInfo.getSubscriptionType(), isUnsubscribe, tenantId); + getSubscriptionStatistic(deviceIdsOwnByGroup, null, isUnsubscribe, tenantId); int allDeviceCount = HelperUtil.getGroupManagementProviderService().getDeviceCount(subscriptionInfo.getIdentifier()); return SubscriptionManagementHelperUtil.getSubscriptionStatistics(subscriptionStatisticDTO, allDeviceCount); } catch (ApplicationManagementDAOException e) { diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/RoleBasedSubscriptionManagementHelperServiceImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/RoleBasedSubscriptionManagementHelperServiceImpl.java index 30a1dd38a6..9ed856c09e 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/RoleBasedSubscriptionManagementHelperServiceImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/RoleBasedSubscriptionManagementHelperServiceImpl.java @@ -112,13 +112,12 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri isUnsubscribe, tenantId, deviceIdsOwnByRole, dbSubscriptionStatus, subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), limit, offset); - deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIdsOwnByRole, dbSubscriptionStatus, - subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy()); + deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS, + subscriptionInfo.getDeviceSubscriptionFilterCriteria()); } List deviceSubscriptions = SubscriptionManagementHelperUtil. getDeviceSubscriptionData(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe); + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, limit, offset); return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions); } catch (UserStoreException e) { @@ -175,7 +174,7 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri ConnectionManagerUtil.openDBConnection(); List deviceIdsOwnByRole = getDeviceIdsOwnByRole(subscriptionInfo.getIdentifier(), tenantId); SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO. - getSubscriptionStatistic(deviceIdsOwnByRole, subscriptionInfo.getSubscriptionType(), isUnsubscribe, tenantId); + getSubscriptionStatistic(deviceIdsOwnByRole, null, isUnsubscribe, tenantId); int allDeviceCount = deviceIdsOwnByRole.size(); return SubscriptionManagementHelperUtil.getSubscriptionStatistics(subscriptionStatisticDTO, allDeviceCount); } catch (DeviceManagementException | ApplicationManagementDAOException | UserStoreException e) { diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/UserBasedSubscriptionManagementHelperServiceImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/UserBasedSubscriptionManagementHelperServiceImpl.java index 830153d142..8b548b2fbb 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/UserBasedSubscriptionManagementHelperServiceImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/UserBasedSubscriptionManagementHelperServiceImpl.java @@ -107,15 +107,14 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri } else { deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, deviceIdsOwnByUser, dbSubscriptionStatus, - null, deviceSubscriptionFilterCriteria.getTriggeredBy(), limit, offset); + null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); - deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIdsOwnByUser, dbSubscriptionStatus, - null, deviceSubscriptionFilterCriteria.getTriggeredBy()); + deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS, + subscriptionInfo.getDeviceSubscriptionFilterCriteria()); } List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe); + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, limit, offset); return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions); } catch (DeviceManagementException e) { String msg = "Error encountered while getting device details"; @@ -167,7 +166,7 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri ConnectionManagerUtil.openDBConnection(); List deviceIdsOwnByUser = getDeviceIdsOwnByUser(subscriptionInfo.getIdentifier()); SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO. - getSubscriptionStatistic(deviceIdsOwnByUser, subscriptionInfo.getSubscriptionType(), isUnsubscribe, tenantId); + getSubscriptionStatistic(deviceIdsOwnByUser, null, isUnsubscribe, tenantId); int allDeviceCount = deviceIdsOwnByUser.size(); return SubscriptionManagementHelperUtil.getSubscriptionStatistics(subscriptionStatisticDTO, allDeviceCount); } catch (DeviceManagementException | ApplicationManagementDAOException e) { 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/DeviceDAO.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/DeviceDAO.java index 422395e38e..3d646768fb 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/DeviceDAO.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/DeviceDAO.java @@ -876,4 +876,7 @@ public interface DeviceDAO { List getDevicesByDeviceIds(PaginationRequest paginationRequest, List deviceIds, int tenantId) throws DeviceManagementDAOException; + + int getDeviceCountByDeviceIds(PaginationRequest paginationRequest, List deviceIds, int tenantId) + throws DeviceManagementDAOException; } 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 946d1ec3c6..654977cc62 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 @@ -3428,7 +3428,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { "AND e.STATUS NOT IN ('DELETED', 'REMOVED')"; if (paginationRequest.getOwner() != null) { - sql = sql + " AND e.OWNER = ?"; + sql = sql + " AND e.OWNER LIKE ?"; isOwnerProvided = true; } @@ -3438,10 +3438,12 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } if (paginationRequest.getDeviceName() != null) { - sql = sql + " AND d.NAME = ?"; + sql = sql + " AND d.NAME LIKE ?"; isDeviceNameProvided = true; } + sql = sql + " LIMIT ? OFFSET ?"; + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { int parameterIdx = 1; preparedStatement.setInt(parameterIdx++, tenantId); @@ -3451,11 +3453,14 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } if (isOwnerProvided) - preparedStatement.setString(parameterIdx++, paginationRequest.getOwner()); + preparedStatement.setString(parameterIdx++, "%" + paginationRequest.getOwner() + "%"); if (isDeviceStatusProvided) preparedStatement.setString(parameterIdx++, paginationRequest.getDeviceStatus()); if (isDeviceNameProvided) - preparedStatement.setString(parameterIdx, paginationRequest.getDeviceName()); + preparedStatement.setString(parameterIdx++, "%" + paginationRequest.getDeviceName() + "%"); + + preparedStatement.setInt(parameterIdx++, paginationRequest.getRowCount()); + preparedStatement.setInt(parameterIdx, paginationRequest.getStartIndex()); try(ResultSet resultSet = preparedStatement.executeQuery()) { Device device; @@ -3483,4 +3488,64 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } } + @Override + public int getDeviceCountByDeviceIds(PaginationRequest paginationRequest, List deviceIds, int tenantId) + throws DeviceManagementDAOException { + int deviceCount = 0; + if (deviceIds == null || deviceIds.isEmpty()) return deviceCount; + + String deviceIdStringList = deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")); + boolean isOwnerProvided = false; + boolean isDeviceStatusProvided = false; + boolean isDeviceNameProvided = false; + try { + Connection connection = getConnection(); + String sql = "SELECT COUNT(DISTINCT e.DEVICE_ID) AS COUNT FROM DM_DEVICE d INNER JOIN DM_ENROLMENT e " + + "WHERE d.ID = e.DEVICE_ID AND d.TENANT_ID = ? AND e.DEVICE_ID IN (" + deviceIdStringList+ ") " + + "AND e.STATUS NOT IN ('DELETED', 'REMOVED')"; + + if (paginationRequest.getOwner() != null) { + sql = sql + " AND e.OWNER LIKE ?"; + isOwnerProvided = true; + } + + if (paginationRequest.getDeviceStatus() != null) { + sql = sql + " AND e.STATUS = ?"; + isDeviceStatusProvided = true; + } + + if (paginationRequest.getDeviceName() != null) { + sql = sql + " AND d.NAME LIKE ?"; + isDeviceNameProvided = true; + } + + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + int parameterIdx = 1; + preparedStatement.setInt(parameterIdx++, tenantId); + + for (Integer deviceId : deviceIds) { + preparedStatement.setInt(parameterIdx++, deviceId); + } + + if (isOwnerProvided) + preparedStatement.setString(parameterIdx++, "%" + paginationRequest.getOwner() + "%"); + if (isDeviceStatusProvided) + preparedStatement.setString(parameterIdx++, paginationRequest.getDeviceStatus()); + if (isDeviceNameProvided) + preparedStatement.setString(parameterIdx, "%" + paginationRequest.getDeviceName() + "%"); + + try(ResultSet resultSet = preparedStatement.executeQuery()) { + if (resultSet.next()) { + deviceCount = resultSet.getInt("COUNT"); + } + } + } + return deviceCount; + } catch (SQLException e) { + String msg = "Error occurred while retrieving device count for device ids in: " + deviceIds; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + } + } 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/service/DeviceManagementProviderService.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderService.java index fd43de69fd..f95bbd3294 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderService.java @@ -1162,4 +1162,6 @@ public interface DeviceManagementProviderService { List getDevicesByDeviceIds(PaginationRequest paginationRequest, List deviceIds) throws DeviceManagementException; + public int getDeviceCountByDeviceIds(PaginationRequest paginationRequest, List deviceIds) + throws DeviceManagementException; } 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/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 6de1167704..3fdeb263ea 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -5692,4 +5692,30 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.closeConnection(); } } + + @Override + public int getDeviceCountByDeviceIds(PaginationRequest paginationRequest, List deviceIds) + throws DeviceManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + if (paginationRequest == null) { + String msg = "Received null for pagination request"; + log.error(msg); + throw new DeviceManagementException(msg); + } + + try { + DeviceManagementDAOFactory.openConnection(); + return deviceDAO.getDeviceCountByDeviceIds(paginationRequest, deviceIds, tenantId); + } catch (DeviceManagementDAOException e) { + String msg = "Error encountered while getting devices for device ids in " + deviceIds; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (SQLException e) { + String msg = "Error encountered while getting the database connection"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } }