From 27b033c3f05c6e0692df4aedb677ba23efeea4e9 Mon Sep 17 00:00:00 2001 From: ruwin Date: Thu, 3 Oct 2024 23:47:11 +0530 Subject: [PATCH] Fix for incorrect statistics issue in User and Role Added a fix to resolve the incorrect statistics issue that occurred when application-unsupported devices were assigned to a specific user or role. This issue caused inaccurate device statistics to be displayed in such cases. This change ensures accurate reporting of device statistics by correctly handling unsupported device types. --- ...bscriptionManagementHelperServiceImpl.java | 38 ++++++++++++++++++- ...bscriptionManagementHelperServiceImpl.java | 8 +++- 2 files changed, 43 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/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 c78c27d261..d86534b073 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 @@ -44,6 +44,8 @@ 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; +import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOException; +import io.entgra.device.mgt.core.device.mgt.core.dto.DeviceDetailsDTO; import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -190,7 +192,7 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri List deviceIdsOwnByRole = getDeviceIdsOwnByRole(subscriptionInfo.getIdentifier(), tenantId); SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO. getSubscriptionStatistic(deviceIdsOwnByRole, isUnsubscribe, tenantId, applicationReleaseDTO.getId()); - int allDeviceCount = deviceIdsOwnByRole.size(); + int allDeviceCount = getDeviceIdsOwnByRoleWithType(subscriptionInfo.getIdentifier(), tenantId, applicationReleaseDTO); return SubscriptionManagementHelperUtil.getSubscriptionStatistics(subscriptionStatisticDTO, allDeviceCount); } catch (DeviceManagementException | ApplicationManagementDAOException | UserStoreException e) { String msg = "Error encountered while getting subscription statistics for role: " + subscriptionInfo.getIdentifier(); @@ -212,7 +214,7 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri PaginationRequest paginationRequest = new PaginationRequest(-1, -1); paginationRequest.setOwner(user); paginationRequest.setStatusList(Arrays.asList(EnrolmentInfo.Status.ACTIVE.name(), - EnrolmentInfo.Status.INACTIVE.name(),EnrolmentInfo.Status.UNREACHABLE.name())); + EnrolmentInfo.Status.INACTIVE.name(), EnrolmentInfo.Status.UNREACHABLE.name())); PaginationResult ownDeviceIds = HelperUtil.getDeviceManagementProviderService(). getAllDevicesIdList(paginationRequest); if (ownDeviceIds.getData() != null) { @@ -222,6 +224,38 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri return deviceListOwnByRole.stream().map(Device::getId).collect(Collectors.toList()); } + private int getDeviceIdsOwnByRoleWithType(String roleName, int tenantId, ApplicationReleaseDTO applicationReleaseDTO) + throws UserStoreException, DeviceManagementException { + UserStoreManager userStoreManager = DataHolder.getInstance().getRealmService() + .getTenantUserRealm(tenantId).getUserStoreManager(); + String[] usersWithRole = userStoreManager.getUserListOfRole(roleName); + int idCountOwnByRole = 0; + int deviceTypeId; + try { + deviceTypeId = applicationDAO.getApplication(applicationReleaseDTO.getUuid(), tenantId).getDeviceTypeId(); + } catch (ApplicationManagementDAOException e) { + String msg = "Error encountered while accessing application management data."; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } + for (String user : usersWithRole) { + try { + List idsOwnByRole = HelperUtil.getDeviceManagementProviderService() + .getDevicesByTenantId(tenantId, deviceTypeId, user, null); + if (idsOwnByRole != null) { + idCountOwnByRole += idsOwnByRole.size(); + } + } catch (DeviceManagementDAOException e) { + String msg = String.format("Error encountered while accessing device management data for user: %s", user); + log.error(msg, e); + } catch (Exception e) { + String msg = String.format("Unexpected error occurred for user: %s", user); + log.error(msg, e); + } + } + return idCountOwnByRole; + } + private static class RoleBasedSubscriptionManagementHelperServiceImplHolder { private static final RoleBasedSubscriptionManagementHelperServiceImpl INSTANCE = new RoleBasedSubscriptionManagementHelperServiceImpl(); 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 9b51d51ecd..021e829c4d 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 @@ -43,6 +43,8 @@ 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; +import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOException; +import io.entgra.device.mgt.core.device.mgt.core.dto.DeviceDetailsDTO; import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -181,12 +183,16 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri List deviceIdsOwnByUser = getDeviceIdsOwnByUser(subscriptionInfo.getIdentifier()); SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO. getSubscriptionStatistic(deviceIdsOwnByUser, isUnsubscribe, tenantId, applicationReleaseDTO.getId()); - int allDeviceCount = deviceIdsOwnByUser.size(); + List devices = HelperUtil.getDeviceManagementProviderService().getDevicesByTenantId(tenantId, + applicationDAO.getApplication(applicationReleaseDTO.getUuid(), tenantId).getDeviceTypeId(), subscriptionInfo.getIdentifier(), null); + int allDeviceCount = devices.size(); return SubscriptionManagementHelperUtil.getSubscriptionStatistics(subscriptionStatisticDTO, allDeviceCount); } catch (DeviceManagementException | ApplicationManagementDAOException e) { String msg = "Error encountered while getting subscription statistics for user: " + subscriptionInfo.getIdentifier(); log.error(msg, e); throw new ApplicationManagementException(msg, e); + } catch (DeviceManagementDAOException e) { + throw new RuntimeException(e); } finally { ConnectionManagerUtil.closeDBConnection(); }