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/services/SubscriptionManager.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/SubscriptionManager.java index 93060a2f2d..15cf23af56 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/SubscriptionManager.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/SubscriptionManager.java @@ -252,14 +252,12 @@ public interface SubscriptionManager { SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException; - SubscriptionResponse getStatusBaseSubscriptionsUser(SubscriptionInfo subscriptionInfo, int limit, int offset) - throws ApplicationManagementException; +// SubscriptionResponse getStatusBaseSubscriptionsGroup(SubscriptionInfo subscriptionInfo, int limit, int offset) +// throws ApplicationManagementException; - SubscriptionResponse getStatusBaseSubscriptionsRole(SubscriptionInfo subscriptionInfo, int limit, int offset) - throws ApplicationManagementException; +// SubscriptionResponse getStatusBaseSubscriptionsAll(SubscriptionInfo subscriptionInfo, int limit, int offset) +// throws ApplicationManagementException; - SubscriptionResponse getStatusBaseSubscriptionsGroup(SubscriptionInfo subscriptionInfo, int limit, int offset) - throws ApplicationManagementException; /** * Get subscription statistics related data describes by the {@link SubscriptionInfo} 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/impl/SubscriptionManagerImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/SubscriptionManagerImpl.java index 33a06aa923..e52b375e97 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/SubscriptionManagerImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/SubscriptionManagerImpl.java @@ -1713,7 +1713,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { public SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo) throws ApplicationManagementException { - if (subscriptionInfo.getSubscriptionType()==SubscriptionMetadata.SubscriptionTypes.DEVICE){ + if (Objects.equals(subscriptionInfo.getSubscriptionType(), SubscriptionMetadata.SubscriptionTypes.DEVICE)){ return null; } final boolean isUnsubscribe = Objects.equals(SubscriptionMetadata.SUBSCRIPTION_STATUS_UNSUBSCRIBED, subscriptionInfo.getSubscriptionStatus()); @@ -1759,7 +1759,183 @@ public class SubscriptionManagerImpl implements SubscriptionManager { public SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { - + if (Objects.equals(subscriptionInfo.getSubscriptionType(), SubscriptionMetadata.SubscriptionTypes.DEVICE)) { + return getStatusBaseSubscriptionsDevice(subscriptionInfo, limit, offset); + } else if (Objects.equals(subscriptionInfo.getSubscriptionType(), SubscriptionMetadata.SubscriptionTypes.GROUP)) { + return getStatusBaseSubscriptionsGroup(subscriptionInfo, limit, offset); + } else { + return getStatusBaseSubscriptionsRoleUser(subscriptionInfo, limit, offset); + } + } +// +// final boolean isUnsubscribe = Objects.equals(SubscriptionMetadata.SUBSCRIPTION_STATUS_UNSUBSCRIBED, subscriptionInfo.getSubscriptionStatus()); +// List deviceSubscriptionDTOS; +// int deviceCount = 0; +// int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); +// +// try { +// ConnectionManagerUtil.openDBConnection(); +// ApplicationReleaseDTO applicationReleaseDTO = getApplicationRelease(subscriptionInfo.getApplicationUUID(), tenantId); +// ApplicationDTO applicationDTO = getApplication(subscriptionInfo.getApplicationUUID(), tenantId); +// +// String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); +// DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); +// +// if (Objects.equals(subscriptionInfo.getSubscriptionType(), SubscriptionMetadata.SubscriptionTypes.GROUP)){ +// deviceSubscriptionDTOS = handleGroupSubscription(subscriptionInfo, limit, offset, isUnsubscribe, applicationReleaseDTO, applicationDTO, deviceSubscriptionStatus, deviceSubscriptionFilterCriteria); +// } if (Objects.equals(subscriptionInfo.getSubscriptionType(), SubscriptionMetadata.SubscriptionTypes.DEVICE)) { +// deviceSubscriptionDTOS = handleDeviceSubscription(subscriptionInfo, isUnsubscribe, applicationReleaseDTO, deviceSubscriptionStatus, deviceSubscriptionFilterCriteria); +// } else { +// getStatusBaseSubscriptionsRoleUser(subscriptionInfo, limit, offset); +// } +// +// List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, +// subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, applicationDTO.getDeviceTypeId(), limit, offset); +// return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions); +// } catch (Exception e) { +// handleException(e, subscriptionInfo.getIdentifier()); +// return null; // This line will not be reached due to exception handling, but is required for compilation. +// } finally { +// ConnectionManagerUtil.closeDBConnection(); +// } +// } +// +// private ApplicationReleaseDTO getApplicationRelease(String applicationUUID, int tenantId) throws ApplicationManagementException { +// ApplicationReleaseDTO applicationReleaseDTO = null; +// try { +// applicationReleaseDTO = applicationReleaseDAO.getReleaseByUUID(applicationUUID, tenantId); +// } catch (ApplicationManagementDAOException e) { +// throw new RuntimeException(e); +// } +// if (applicationReleaseDTO == null) { +// String msg = "Couldn't find an application release for application release UUID: " + applicationUUID; +// log.error(msg); +// throw new NotFoundException(msg); +// } +// return applicationReleaseDTO; +// } +// +// private ApplicationDTO getApplication(String applicationUUID, int tenantId) throws ApplicationManagementException { +// ApplicationDTO applicationDTO = null; +// try { +// applicationDTO = this.applicationDAO.getAppWithRelatedRelease(applicationUUID, tenantId); +// } catch (ApplicationManagementDAOException e) { +// throw new RuntimeException(e); +// } +// if (applicationDTO == null) { +// String msg = "Application not found for the release UUID: " + applicationUUID; +// log.error(msg); +// throw new NotFoundException(msg); +// } +// return applicationDTO; +// } +// +// private List handleDeviceSubscription(SubscriptionInfo subscriptionInfo, boolean isUnsubscribe, ApplicationReleaseDTO applicationReleaseDTO, +// String deviceSubscriptionStatus, DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria) throws DeviceManagementException { +// DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); +// List deviceSubscriptionDTOS; +// List dbSubscriptionStatus = SubscriptionManagementHelperUtil.getDBSubscriptionStatus(subscriptionInfo.getDeviceSubscriptionStatus()); +// +// if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) { +// try { +// deviceSubscriptionDTOS = subscriptionDAO.getAllSubscriptionsDetails(applicationReleaseDTO.getId(), +// isUnsubscribe, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(), null, null, +// deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); +// } catch (ApplicationManagementDAOException e) { +// throw new RuntimeException(e); +// } +// +// List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream() +// .map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); +// +// List newDeviceIds = deviceManagementProviderService.getDevicesNotInGivenIdList(deviceIdsOfSubscription); +// +// deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); +// } else { +// try { +// deviceSubscriptionDTOS = subscriptionDAO.getAllSubscriptionsDetails(applicationReleaseDTO.getId(), +// isUnsubscribe, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(), dbSubscriptionStatus, null, +// deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); +// } catch (ApplicationManagementDAOException e) { +// throw new RuntimeException(e); +// } +// } +// return deviceSubscriptionDTOS; +// } +// +// private List handleGroupSubscription(SubscriptionInfo subscriptionInfo, int limit, int offset, boolean isUnsubscribe, +// ApplicationReleaseDTO applicationReleaseDTO, ApplicationDTO applicationDTO, String deviceSubscriptionStatus, DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria) +// throws GroupManagementException, DeviceManagementException { +// GroupManagementProviderService groupManagementProviderService = HelperUtil.getGroupManagementProviderService(); +// DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); +// List deviceSubscriptionDTOS; +// List dbSubscriptionStatus = SubscriptionManagementHelperUtil.getDBSubscriptionStatus(subscriptionInfo.getDeviceSubscriptionStatus()); +// +// if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) { +// List allDeviceIdsOwnByGroup = groupManagementProviderService.getGroupDetailsWithDevices(subscriptionInfo.getIdentifier(), +// applicationDTO.getDeviceTypeId(), deviceSubscriptionFilterCriteria.getOwner(), deviceSubscriptionFilterCriteria.getName(), +// deviceSubscriptionFilterCriteria.getDeviceStatus(), -1, -1).getDeviceIds(); +// +// try { +// deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), +// isUnsubscribe, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(), allDeviceIdsOwnByGroup, null, +// null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); +// } catch (ApplicationManagementDAOException e) { +// throw new RuntimeException(e); +// } +// +// List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream() +// .map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); +// +// for (Integer deviceId : deviceIdsOfSubscription) { +// allDeviceIdsOwnByGroup.remove(deviceId); +// } +// +// List newDeviceIds = deviceManagementProviderService.getDevicesInGivenIdList(allDeviceIdsOwnByGroup); +// deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); +// } else { +// GroupDetailsDTO groupDetailsDTO = groupManagementProviderService.getGroupDetailsWithDevices(subscriptionInfo.getIdentifier(), +// applicationDTO.getDeviceTypeId(), deviceSubscriptionFilterCriteria.getOwner(), deviceSubscriptionFilterCriteria.getName(), +// deviceSubscriptionFilterCriteria.getDeviceStatus(), offset, limit); +// List paginatedDeviceIdsOwnByGroup = groupDetailsDTO.getDeviceIds(); +// +// try { +// deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), +// isUnsubscribe, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(), paginatedDeviceIdsOwnByGroup, dbSubscriptionStatus, +// null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); +// } catch (ApplicationManagementDAOException e) { +// throw new RuntimeException(e); +// } +// } +// return deviceSubscriptionDTOS; +// } +// +// private void handleException(Exception e, String identifier) throws ApplicationManagementException { +// if (e instanceof GroupManagementException) { +// String msg = "Error encountered while retrieving group details for group: " + identifier; +// log.error(msg, e); +// throw new ApplicationManagementException(msg, e); +// } else if (e instanceof ApplicationManagementDAOException || e instanceof DBConnectionException) { +// String msg = "Error encountered while connecting to the database"; +// log.error(msg, e); +// throw new ApplicationManagementException(msg, e); +// } else if (e instanceof DeviceManagementException) { +// throw new RuntimeException(e); +// } else { +// String msg = "Unexpected error occurred"; +// log.error(msg, e); +// throw new ApplicationManagementException(msg, e); +// } +// } +// + + + + public SubscriptionResponse getStatusBaseSubscriptionsDevice(SubscriptionInfo subscriptionInfo, int limit, int offset) + throws ApplicationManagementException { +// if (Objects.equals(subscriptionInfo.getSubscriptionType(), SubscriptionMetadata.SubscriptionTypes.DEVICE)){ +// return null; +// } final boolean isUnsubscribe = Objects.equals(SubscriptionMetadata.SUBSCRIPTION_STATUS_UNSUBSCRIBED, subscriptionInfo.getSubscriptionStatus()); List deviceSubscriptionDTOS; int deviceCount = 0; @@ -1822,105 +1998,6 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } } - - public SubscriptionResponse getStatusBaseSubscriptionsRole(SubscriptionInfo subscriptionInfo, int limit, int offset) - throws ApplicationManagementException { - final boolean isUnsubscribe = Objects.equals(SubscriptionMetadata.SUBSCRIPTION_STATUS_UNSUBSCRIBED, subscriptionInfo.getSubscriptionStatus()); - List deviceSubscriptionDTOS; - int deviceCount = 0; - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - - try { - ConnectionManagerUtil.openDBConnection(); - List deviceIdsOwnByRole = getDeviceIdsOwnByRole(subscriptionInfo.getIdentifier(), tenantId); - - ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO. - getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId); - if (applicationReleaseDTO == null) { - String msg = "Couldn't find an application release for application release UUID: " + - subscriptionInfo.getApplicationUUID(); - log.error(msg); - throw new NotFoundException(msg); - } - - ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(subscriptionInfo.getApplicationUUID(), tenantId); - if (applicationDTO == null) { - String msg = "Application not found for the release UUID: " + subscriptionInfo.getApplicationUUID(); - log.error(msg); - throw new NotFoundException(msg); - } - - String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); - DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); - DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); - List dbSubscriptionStatus = SubscriptionManagementHelperUtil.getDBSubscriptionStatus(subscriptionInfo.getDeviceSubscriptionStatus()); - - if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) { - deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIdsOwnByRole, null, - null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); - - List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). - map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); - - for (Integer deviceId : deviceIdsOfSubscription) { - deviceIdsOwnByRole.remove(deviceId); - } - - List newDeviceIds = deviceManagementProviderService.getDevicesInGivenIdList(deviceIdsOwnByRole); - deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); - } else { - deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIdsOwnByRole, dbSubscriptionStatus, - subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); - } - deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria(), applicationDTO.getDeviceTypeId()); - - List deviceSubscriptions = SubscriptionManagementHelperUtil. - getDeviceSubscriptionData(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, applicationDTO.getDeviceTypeId(), limit, offset); - return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions); - - } catch (UserStoreException e) { - String msg = "Error encountered while getting the user management store for tenant id " + tenantId; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } catch (DeviceManagementException e) { - String msg = "Error encountered while getting device details"; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } catch (ApplicationManagementDAOException | DBConnectionException e) { - String msg = "Error encountered while connecting to the database"; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } finally { - ConnectionManagerUtil.closeDBConnection(); - } - } - - - @SuppressWarnings("unchecked") - private List getDeviceIdsOwnByRole(String roleName, int tenantId) throws UserStoreException, DeviceManagementException { - UserStoreManager userStoreManager = DataHolder.getInstance().getRealmService(). - getTenantUserRealm(tenantId).getUserStoreManager(); - String[] usersWithRole = - userStoreManager.getUserListOfRole(roleName); - List deviceListOwnByRole = new ArrayList<>(); - for (String user : usersWithRole) { - PaginationRequest paginationRequest = new PaginationRequest(-1, -1); - paginationRequest.setOwner(user); - paginationRequest.setStatusList(Arrays.asList("ACTIVE", "INACTIVE", "UNREACHABLE")); - PaginationResult ownDeviceIds = HelperUtil.getDeviceManagementProviderService(). - getAllDevicesIdList(paginationRequest); - if (ownDeviceIds.getData() != null) { - deviceListOwnByRole.addAll((List) ownDeviceIds.getData()); - } - } - return deviceListOwnByRole.stream().map(Device::getId).collect(Collectors.toList()); - } - - public SubscriptionResponse getStatusBaseSubscriptionsGroup(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { @@ -2004,10 +2081,10 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } finally { ConnectionManagerUtil.closeDBConnection(); } - } - public SubscriptionResponse getStatusBaseSubscriptionsUser(SubscriptionInfo subscriptionInfo, int limit, int offset) + + public SubscriptionResponse getStatusBaseSubscriptionsRoleUser(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { final boolean isUnsubscribe = Objects.equals(SubscriptionMetadata.SUBSCRIPTION_STATUS_UNSUBSCRIBED, subscriptionInfo.getSubscriptionStatus()); List deviceSubscriptionDTOS; @@ -2016,7 +2093,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { try { ConnectionManagerUtil.openDBConnection(); - List deviceIdsOwnByUser = getDeviceIdsOwnByUser(subscriptionInfo.getIdentifier()); + List deviceIdsOwnByRole = getAllDeviceIds(subscriptionInfo); ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO. getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId); @@ -2041,29 +2118,35 @@ public class SubscriptionManagerImpl implements SubscriptionManager { if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) { deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIdsOwnByUser, null, + isUnsubscribe, tenantId, deviceIdsOwnByRole, null, null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); for (Integer deviceId : deviceIdsOfSubscription) { - deviceIdsOwnByUser.remove(deviceId); + deviceIdsOwnByRole.remove(deviceId); } - List newDeviceIds = deviceManagementProviderService.getDevicesInGivenIdList(deviceIdsOwnByUser); - deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); + List newDeviceIds = deviceManagementProviderService.getDevicesInGivenIdList(deviceIdsOwnByRole); + deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); } else { deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIdsOwnByUser, dbSubscriptionStatus, - null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); + isUnsubscribe, tenantId, deviceIdsOwnByRole, dbSubscriptionStatus, + subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); } deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS, subscriptionInfo.getDeviceSubscriptionFilterCriteria(), applicationDTO.getDeviceTypeId()); - List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, applicationDTO.getDeviceTypeId(), limit, offset); + List deviceSubscriptions = SubscriptionManagementHelperUtil. + getDeviceSubscriptionData(deviceSubscriptionDTOS, + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, applicationDTO.getDeviceTypeId(), limit, offset); return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions); + + } catch (UserStoreException e) { + String msg = "Error encountered while getting the user management store for tenant id " + tenantId; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); } catch (DeviceManagementException e) { String msg = "Error encountered while getting device details"; log.error(msg, e); @@ -2077,6 +2160,46 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } } + + @SuppressWarnings("unchecked") + private List getDeviceIdsOwnByRole(String roleName, int tenantId) throws UserStoreException, DeviceManagementException { + UserStoreManager userStoreManager = DataHolder.getInstance().getRealmService(). + getTenantUserRealm(tenantId).getUserStoreManager(); + String[] usersWithRole = + userStoreManager.getUserListOfRole(roleName); + List deviceListOwnByRole = new ArrayList<>(); + for (String user : usersWithRole) { + PaginationRequest paginationRequest = new PaginationRequest(-1, -1); + paginationRequest.setOwner(user); + paginationRequest.setStatusList(Arrays.asList("ACTIVE", "INACTIVE", "UNREACHABLE")); + PaginationResult ownDeviceIds = HelperUtil.getDeviceManagementProviderService(). + getAllDevicesIdList(paginationRequest); + if (ownDeviceIds.getData() != null) { + deviceListOwnByRole.addAll((List) ownDeviceIds.getData()); + } + } + return deviceListOwnByRole.stream().map(Device::getId).collect(Collectors.toList()); + } + + + + private List getAllDeviceIds (SubscriptionInfo subscriptionInfo) throws DeviceManagementException, UserStoreException { + String type = subscriptionInfo.getSubscriptionType(); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + if (Objects.equals(type, SubscriptionMetadata.SubscriptionTypes.ROLE)){ + return getDeviceIdsOwnByRole(subscriptionInfo.getIdentifier(),tenantId); + } +// if (Objects.equals(type, SubscriptionMetadata.SubscriptionTypes.GROUP)){ +// return getDeviceIdsOwnByGroup(subscriptionInfo.getIdentifier(),tenantId); +// } + if (Objects.equals(type,SubscriptionMetadata.SubscriptionTypes.USER)){ + return getDeviceIdsOwnByUser(subscriptionInfo.getIdentifier()); + }else + return null; + } + + + @SuppressWarnings("unchecked") private List getDeviceIdsOwnByUser(String username) throws DeviceManagementException { List deviceListOwnByUser = new ArrayList<>();