From 2da8cdd05feedfb2f540342382570288df096e22 Mon Sep 17 00:00:00 2001 From: ruwin Date: Tue, 10 Sep 2024 07:25:08 +0530 Subject: [PATCH 1/6] Fixed the incorrect device statistics issue happening due to REMOVED and DELETED devices. --- ...BasedSubscriptionManagementHelperServiceImpl.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) 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 16834bf2a1..e78ab631f2 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 @@ -195,10 +195,18 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr } List devices = HelperUtil.getGroupManagementProviderService(). getAllDevicesOfGroup(subscriptionInfo.getIdentifier(), false); - List deviceIdsOwnByGroup = devices.stream().map(Device::getId).collect(Collectors.toList()); + List removedIds = devices.stream() + .filter(device -> {String status = String.valueOf(device.getEnrolmentInfo().getStatus()); + return "REMOVED".equalsIgnoreCase(status) || "DELETED".equalsIgnoreCase(status);}) + .map(device -> device.getEnrolmentInfo().getId()).collect(Collectors.toList()); + List enrollmentIdsOwnByGroup = devices.stream().map(device -> device.getEnrolmentInfo().getId()).collect(Collectors.toList()); + enrollmentIdsOwnByGroup.removeAll(removedIds); + List deviceIdsOwnByGroup = devices.stream() + .filter(device -> enrollmentIdsOwnByGroup.contains(device.getEnrolmentInfo().getId())) + .map(Device::getId).collect(Collectors.toList()); SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO. getSubscriptionStatistic(deviceIdsOwnByGroup, isUnsubscribe, tenantId, applicationReleaseDTO.getId()); - int allDeviceCount = HelperUtil.getGroupManagementProviderService().getDeviceCount(subscriptionInfo.getIdentifier()); + int allDeviceCount= deviceIdsOwnByGroup.size(); return SubscriptionManagementHelperUtil.getSubscriptionStatistics(subscriptionStatisticDTO, allDeviceCount); } catch (ApplicationManagementDAOException e) { String msg = "Error encountered while getting subscription statistics for group: " + subscriptionInfo.getIdentifier(); From 6934827920cf94ac499c904564fb2df486837c15 Mon Sep 17 00:00:00 2001 From: ruwin Date: Tue, 10 Sep 2024 07:28:29 +0530 Subject: [PATCH 2/6] Fixed the incorrect device statistics issue happening due to REMOVED and DELETED devices. --- ...GroupBasedSubscriptionManagementHelperServiceImpl.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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 e78ab631f2..d67706e575 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 @@ -196,8 +196,10 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr List devices = HelperUtil.getGroupManagementProviderService(). getAllDevicesOfGroup(subscriptionInfo.getIdentifier(), false); List removedIds = devices.stream() - .filter(device -> {String status = String.valueOf(device.getEnrolmentInfo().getStatus()); - return "REMOVED".equalsIgnoreCase(status) || "DELETED".equalsIgnoreCase(status);}) + .filter(device -> { + String status = String.valueOf(device.getEnrolmentInfo().getStatus()); + return "REMOVED".equalsIgnoreCase(status) || "DELETED".equalsIgnoreCase(status); + }) .map(device -> device.getEnrolmentInfo().getId()).collect(Collectors.toList()); List enrollmentIdsOwnByGroup = devices.stream().map(device -> device.getEnrolmentInfo().getId()).collect(Collectors.toList()); enrollmentIdsOwnByGroup.removeAll(removedIds); @@ -206,7 +208,7 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr .map(Device::getId).collect(Collectors.toList()); SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO. getSubscriptionStatistic(deviceIdsOwnByGroup, isUnsubscribe, tenantId, applicationReleaseDTO.getId()); - int allDeviceCount= deviceIdsOwnByGroup.size(); + int allDeviceCount = deviceIdsOwnByGroup.size(); return SubscriptionManagementHelperUtil.getSubscriptionStatistics(subscriptionStatisticDTO, allDeviceCount); } catch (ApplicationManagementDAOException e) { String msg = "Error encountered while getting subscription statistics for group: " + subscriptionInfo.getIdentifier(); From 0609b3a9c2f6b48975bbccd33bdae77ead491ec9 Mon Sep 17 00:00:00 2001 From: ruwin Date: Wed, 11 Sep 2024 07:21:33 +0530 Subject: [PATCH 3/6] Used constants instead of hardcoding --- .../GroupBasedSubscriptionManagementHelperServiceImpl.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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 d67706e575..9b5dc445de 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 @@ -39,6 +39,7 @@ import io.entgra.device.mgt.core.application.mgt.core.util.HelperUtil; import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.SubscriptionManagementHelperUtil; import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.service.SubscriptionManagementHelperService; import io.entgra.device.mgt.core.device.mgt.common.Device; +import io.entgra.device.mgt.core.device.mgt.common.EnrolmentInfo; import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupManagementException; import io.entgra.device.mgt.core.device.mgt.core.dto.GroupDetailsDTO; @@ -197,8 +198,8 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr getAllDevicesOfGroup(subscriptionInfo.getIdentifier(), false); List removedIds = devices.stream() .filter(device -> { - String status = String.valueOf(device.getEnrolmentInfo().getStatus()); - return "REMOVED".equalsIgnoreCase(status) || "DELETED".equalsIgnoreCase(status); + EnrolmentInfo.Status status = device.getEnrolmentInfo().getStatus(); + return status == EnrolmentInfo.Status.REMOVED || status == EnrolmentInfo.Status.DELETED; }) .map(device -> device.getEnrolmentInfo().getId()).collect(Collectors.toList()); List enrollmentIdsOwnByGroup = devices.stream().map(device -> device.getEnrolmentInfo().getId()).collect(Collectors.toList()); From a161791fc07eb9636dd2b24517a9a2dd1563fe36 Mon Sep 17 00:00:00 2001 From: ruwin Date: Thu, 19 Sep 2024 12:43:39 +0530 Subject: [PATCH 4/6] Added a method to retrieve the device count based on application type --- ...bscriptionManagementHelperServiceImpl.java | 26 +++++---------- ...bscriptionManagementHelperServiceImpl.java | 4 ++- ...bscriptionManagementHelperServiceImpl.java | 4 ++- .../core/device/mgt/core/dao/GroupDAO.java | 2 ++ .../core/dao/impl/AbstractGroupDAOImpl.java | 33 +++++++++++++++++++ .../GroupManagementProviderService.java | 2 ++ .../GroupManagementProviderServiceImpl.java | 16 ++++++++- 7 files changed, 67 insertions(+), 20 deletions(-) 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 9b5dc445de..02628d3ef6 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 @@ -49,6 +49,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; +import java.util.Arrays; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; @@ -147,7 +148,6 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr } finally { ConnectionManagerUtil.closeDBConnection(); } - } @Override @@ -194,22 +194,14 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr log.error(msg); throw new NotFoundException(msg); } - List devices = HelperUtil.getGroupManagementProviderService(). - getAllDevicesOfGroup(subscriptionInfo.getIdentifier(), false); - List removedIds = devices.stream() - .filter(device -> { - EnrolmentInfo.Status status = device.getEnrolmentInfo().getStatus(); - return status == EnrolmentInfo.Status.REMOVED || status == EnrolmentInfo.Status.DELETED; - }) - .map(device -> device.getEnrolmentInfo().getId()).collect(Collectors.toList()); - List enrollmentIdsOwnByGroup = devices.stream().map(device -> device.getEnrolmentInfo().getId()).collect(Collectors.toList()); - enrollmentIdsOwnByGroup.removeAll(removedIds); - List deviceIdsOwnByGroup = devices.stream() - .filter(device -> enrollmentIdsOwnByGroup.contains(device.getEnrolmentInfo().getId())) - .map(Device::getId).collect(Collectors.toList()); - SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO. - getSubscriptionStatistic(deviceIdsOwnByGroup, isUnsubscribe, tenantId, applicationReleaseDTO.getId()); - int allDeviceCount = deviceIdsOwnByGroup.size(); + List deviceStatuses = Arrays.asList(EnrolmentInfo.Status.ACTIVE.name(), + EnrolmentInfo.Status.INACTIVE.name(), EnrolmentInfo.Status.UNREACHABLE.name()); + List devices = HelperUtil.getGroupManagementProviderService().getAllDevicesOfGroup(subscriptionInfo.getIdentifier(), deviceStatuses, false); + List deviceIdsOwnByGroup = devices.stream().map(Device::getId).collect(Collectors.toList()); + SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO.getSubscriptionStatistic + (deviceIdsOwnByGroup, isUnsubscribe, tenantId, applicationReleaseDTO.getId()); + int allDeviceCount = HelperUtil.getGroupManagementProviderService().getDeviceCountWithGroup(subscriptionInfo.getIdentifier(), + applicationDAO.getApplication(applicationReleaseDTO.getUuid(), tenantId).getDeviceTypeId()); return SubscriptionManagementHelperUtil.getSubscriptionStatistics(subscriptionStatisticDTO, allDeviceCount); } catch (ApplicationManagementDAOException e) { String msg = "Error encountered while getting subscription statistics for group: " + subscriptionInfo.getIdentifier(); 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 b667198fd5..c78c27d261 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 @@ -40,6 +40,7 @@ import io.entgra.device.mgt.core.application.mgt.core.util.HelperUtil; import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.SubscriptionManagementHelperUtil; import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.service.SubscriptionManagementHelperService; import io.entgra.device.mgt.core.device.mgt.common.Device; +import io.entgra.device.mgt.core.device.mgt.common.EnrolmentInfo; import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest; import io.entgra.device.mgt.core.device.mgt.common.PaginationResult; import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException; @@ -210,7 +211,8 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri for (String user : usersWithRole) { PaginationRequest paginationRequest = new PaginationRequest(-1, -1); paginationRequest.setOwner(user); - paginationRequest.setStatusList(Arrays.asList("ACTIVE", "INACTIVE", "UNREACHABLE")); + paginationRequest.setStatusList(Arrays.asList(EnrolmentInfo.Status.ACTIVE.name(), + EnrolmentInfo.Status.INACTIVE.name(),EnrolmentInfo.Status.UNREACHABLE.name())); PaginationResult ownDeviceIds = HelperUtil.getDeviceManagementProviderService(). getAllDevicesIdList(paginationRequest); if (ownDeviceIds.getData() != null) { 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 b8978b1c7a..9b51d51ecd 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 @@ -39,6 +39,7 @@ import io.entgra.device.mgt.core.application.mgt.core.util.HelperUtil; import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.SubscriptionManagementHelperUtil; import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.service.SubscriptionManagementHelperService; import io.entgra.device.mgt.core.device.mgt.common.Device; +import io.entgra.device.mgt.core.device.mgt.common.EnrolmentInfo; import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest; import io.entgra.device.mgt.core.device.mgt.common.PaginationResult; import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException; @@ -196,7 +197,8 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri List deviceListOwnByUser = new ArrayList<>(); PaginationRequest paginationRequest = new PaginationRequest(-1, -1); paginationRequest.setOwner(username); - paginationRequest.setStatusList(Arrays.asList("ACTIVE", "INACTIVE", "UNREACHABLE")); + paginationRequest.setStatusList(Arrays.asList(EnrolmentInfo.Status.ACTIVE.name(), + EnrolmentInfo.Status.INACTIVE.name(),EnrolmentInfo.Status.UNREACHABLE.name())); PaginationResult ownDeviceIds = HelperUtil.getDeviceManagementProviderService(). getAllDevicesIdList(paginationRequest); if (ownDeviceIds.getData() != null) { 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/GroupDAO.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/GroupDAO.java index 5da24ad91d..513995fab1 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/GroupDAO.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/GroupDAO.java @@ -489,4 +489,6 @@ public interface GroupDAO { throws GroupManagementDAOException; int getDeviceCount(String groupName, int tenantId) throws GroupManagementDAOException; + + int getDeviceCountWithGroup(String groupName, int deviceTypeId, int tenantId) throws GroupManagementDAOException; } \ No newline at end of file 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/AbstractGroupDAOImpl.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/AbstractGroupDAOImpl.java index 21f831e712..94b675d723 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/AbstractGroupDAOImpl.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/AbstractGroupDAOImpl.java @@ -1584,4 +1584,37 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { throw new GroupManagementDAOException(msg, e); } } + + @Override + public int getDeviceCountWithGroup(String groupName, int deviceTypeId, int tenantId) throws GroupManagementDAOException { + int deviceCount = 0; + try { + Connection connection = GroupManagementDAOFactory.getConnection(); + String sql = "SELECT COUNT(e.ID) AS COUNT " + + "FROM DM_GROUP d " + + "INNER JOIN DM_DEVICE_GROUP_MAP m ON d.ID = m.GROUP_ID " + + "INNER JOIN DM_ENROLMENT e ON m.DEVICE_ID = e.DEVICE_ID " + + "INNER JOIN DM_DEVICE r ON e.DEVICE_ID = r.ID " + + "WHERE d.TENANT_ID = ? " + + "AND d.GROUP_NAME = ? " + + "AND r.DEVICE_TYPE_ID = ? " + + "AND e.STATUS NOT IN ('REMOVED', 'DELETED')"; + + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setInt(1, tenantId); + preparedStatement.setString(2, groupName); + preparedStatement.setInt(3, deviceTypeId); + 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 the group: " + groupName; + log.error(msg, e); + throw new GroupManagementDAOException(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/GroupManagementProviderService.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/GroupManagementProviderService.java index 89af7b66b8..90049beef9 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/GroupManagementProviderService.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/GroupManagementProviderService.java @@ -392,4 +392,6 @@ public interface GroupManagementProviderService { int getDeviceCount(String groupName) throws GroupManagementException; + + int getDeviceCountWithGroup(String groupName, int deviceTypeId) throws GroupManagementException; } 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/GroupManagementProviderServiceImpl.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/GroupManagementProviderServiceImpl.java index 1549b3c7b7..f8aa6477a1 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/GroupManagementProviderServiceImpl.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/GroupManagementProviderServiceImpl.java @@ -77,7 +77,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid private final GroupDAO groupDAO; private final DeviceDAO deviceDAO; - /** * Set groupDAO from GroupManagementDAOFactory when class instantiate. */ @@ -1746,4 +1745,19 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid GroupManagementDAOFactory.closeConnection(); } } + + @Override + public int getDeviceCountWithGroup(String groupName,int deviceTypeId) throws GroupManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { + GroupManagementDAOFactory.openConnection(); + return groupDAO.getDeviceCountWithGroup(groupName,deviceTypeId, tenantId); + } catch (SQLException | GroupManagementDAOException e) { + String msg = "Error occurred while retrieving device count."; + log.error(msg, e); + throw new GroupManagementException(msg, e); + } finally { + GroupManagementDAOFactory.closeConnection(); + } + } } From 9107efb3a672e46406fda8fba4851097e999b41a Mon Sep 17 00:00:00 2001 From: ruwin Date: Thu, 19 Sep 2024 19:13:31 +0530 Subject: [PATCH 5/6] Added a method to get the device count based on app os type with group --- .../mgt/core/service/GroupManagementProviderServiceImpl.java | 1 + 1 file changed, 1 insertion(+) 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/GroupManagementProviderServiceImpl.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/GroupManagementProviderServiceImpl.java index f8aa6477a1..825d89184d 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/GroupManagementProviderServiceImpl.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/GroupManagementProviderServiceImpl.java @@ -77,6 +77,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid private final GroupDAO groupDAO; private final DeviceDAO deviceDAO; + /** * Set groupDAO from GroupManagementDAOFactory when class instantiate. */ From 230192f2d9d6b926b1661c863aaf2ac06f1105a9 Mon Sep 17 00:00:00 2001 From: ruwin Date: Thu, 19 Sep 2024 22:51:12 +0530 Subject: [PATCH 6/6] Completed the requested changes --- .../device/mgt/core/device/mgt/core/dao/GroupDAO.java | 9 +++++++++ .../mgt/core/service/GroupManagementProviderService.java | 9 ++++++++- .../core/service/GroupManagementProviderServiceImpl.java | 8 ++++++-- 3 files changed, 23 insertions(+), 3 deletions(-) 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/GroupDAO.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/GroupDAO.java index 513995fab1..c883e2790e 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/GroupDAO.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/GroupDAO.java @@ -490,5 +490,14 @@ public interface GroupDAO { int getDeviceCount(String groupName, int tenantId) throws GroupManagementDAOException; + /** + * Retrieves the count of devices for a specific group, device type, and tenant. + * + * @param groupName the name of the group + * @param deviceTypeId the ID of the device type (e.g., Android, iOS, Windows) + * @param tenantId the ID of the tenant + * @return the count of devices for the given group, device type, and tenant + * @throws GroupManagementDAOException if an error occurs during the retrieval of the device count + */ int getDeviceCountWithGroup(String groupName, int deviceTypeId, int tenantId) throws GroupManagementDAOException; } \ No newline at end of file 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/GroupManagementProviderService.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/GroupManagementProviderService.java index 90049beef9..9750b7471e 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/GroupManagementProviderService.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/GroupManagementProviderService.java @@ -392,6 +392,13 @@ public interface GroupManagementProviderService { int getDeviceCount(String groupName) throws GroupManagementException; - + /** + * Retrieves the count of devices for a specific group and device type. + * + * @param groupName the name of the group + * @param deviceTypeId the ID of the device type (e.g., Android, iOS, Windows) + * @return the count of devices for the given group and device type + * @throws GroupManagementException if an error occurs during the retrieval of the device count + */ int getDeviceCountWithGroup(String groupName, int deviceTypeId) throws GroupManagementException; } 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/GroupManagementProviderServiceImpl.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/GroupManagementProviderServiceImpl.java index 825d89184d..575f2b3fa1 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/GroupManagementProviderServiceImpl.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/GroupManagementProviderServiceImpl.java @@ -1753,8 +1753,12 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid try { GroupManagementDAOFactory.openConnection(); return groupDAO.getDeviceCountWithGroup(groupName,deviceTypeId, tenantId); - } catch (SQLException | GroupManagementDAOException e) { - String msg = "Error occurred while retrieving device count."; + } catch (SQLException e) { + String msg = "SQL error occurred while retrieving device count."; + log.error(msg, e); + throw new GroupManagementException(msg, e); + } catch (GroupManagementDAOException e) { + String msg = "DAO error occurred while retrieving device count."; log.error(msg, e); throw new GroupManagementException(msg, e); } finally {