From 41d8c8ade89390694923c71c2ce04c9db27b1898 Mon Sep 17 00:00:00 2001 From: Rajitha Kumara Date: Sat, 20 Jul 2024 20:06:15 +0530 Subject: [PATCH] Fix issues in statistics --- .../mgt/common/DeviceSubscription.java | 9 + .../common/services/SubscriptionManager.java | 68 +- .../core/impl/SubscriptionManagerImpl.java | 1339 +---------------- .../mgt/SubscriptionManagementHelperUtil.java | 7 +- ...bscriptionManagementHelperServiceImpl.java | 13 +- ...bscriptionManagementHelperServiceImpl.java | 4 +- ...bscriptionManagementHelperServiceImpl.java | 64 +- ...bscriptionManagementHelperServiceImpl.java | 49 +- .../core/dao/impl/AbstractGroupDAOImpl.java | 2 +- 9 files changed, 103 insertions(+), 1452 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 f3209c53b6..7d4efd22d9 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 @@ -22,6 +22,7 @@ package io.entgra.device.mgt.core.application.mgt.common; import java.sql.Timestamp; public class DeviceSubscription { + private int deviceId; private String deviceName; private String deviceIdentifier; private String deviceStatus; @@ -31,6 +32,14 @@ public class DeviceSubscription { private Timestamp dateOfLastUpdate; private SubscriptionData subscriptionData; + public int getDeviceId() { + return deviceId; + } + + public void setDeviceId(int deviceId) { + this.deviceId = deviceId; + } + public String getDeviceName() { return deviceName; } 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 bf5434283b..63274700fc 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 @@ -25,6 +25,7 @@ import io.entgra.device.mgt.core.application.mgt.common.ExecutionStatus; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionResponse; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionStatistics; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionType; import io.entgra.device.mgt.core.application.mgt.common.dto.CategorizedSubscriptionCountsDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; @@ -229,78 +230,13 @@ public interface SubscriptionManager { */ Activity getOperationAppDetails(String id) throws SubscriptionManagementException; - /** - * Retrieves the group details associated with a given app release UUID. - * - * @param uuid the UUID of the app release - * @param subscriptionStatus the status of the subscription (subscribed or unsubscribed) - * @param offset the offset for the data set - * @param limit the limit for the data set - * @return {@link SubscriptionsDTO} which contains the details of subscriptions. - * @throws ApplicationManagementException if an error occurs while fetching the group details - */ - public List getGroupsSubscriptionDetailsByUUID(String uuid, String subscriptionStatus, - PaginationRequest request, int offset, - int limit) throws ApplicationManagementException; - - /** - * Retrieves the user details associated with a given app release UUID. - * - * @param uuid the UUID of the app release - * @param subscriptionStatus the status of the subscription (subscribed or unsubscribed) - * @param offset the offset for the data set - * @param limit the limit for the data set - * @return {@link SubscriptionsDTO} which contains the details of subscriptions. - * @throws ApplicationManagementException if an error occurs while fetching the user details - */ - List getUserSubscriptionsByUUID(String uuid, String subscriptionStatus, PaginationRequest request, - int offset, int limit) throws ApplicationManagementException; - - /** - * Retrieves the Role details associated with a given app release UUID. - * - * @param uuid the UUID of the app release - * @param subscriptionStatus the status of the subscription (subscribed or unsubscribed) - * @param offset the offset for the data set - * @param limit the limit for the data set - * @return {@link SubscriptionsDTO} which contains the details of subscriptions. - * @throws ApplicationManagementException if an error occurs while fetching the role details - */ -// List getRoleSubscriptionsByUUID(String uuid, String subscriptionStatus, PaginationRequest request, -// int offset, int limit) throws ApplicationManagementException; SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException; SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException; - /** - * Retrieves the Device Subscription details associated with a given app release UUID. - * - * @param uuid the UUID of the app release - * @param subscriptionStatus the status of the subscription (subscribed or unsubscribed) - * @param offset the offset for the data set - * @param limit the limit for the data set - * @return {@link DeviceSubscriptionResponseDTO} which contains the details of device subscriptions. - * @throws ApplicationManagementException if an error occurs while fetching the device subscription details - */ - DeviceSubscriptionResponseDTO getDeviceSubscriptionsDetailsByUUID(String uuid, String subscriptionStatus, - PaginationRequest request, int offset, - int limit) throws ApplicationManagementException; - - /** - * Retrieves the All Device details associated with a given app release UUID. - * - * @param uuid the UUID of the app release - * @param subscriptionStatus the status of the subscription (subscribed or unsubscribed) - * @param offset the offset for the data set - * @param limit the limit for the data set - * @return {@link DeviceSubscriptionResponseDTO} which contains the details of device subscriptions. - * @throws ApplicationManagementException if an error occurs while fetching the subscription details - */ - DeviceSubscriptionResponseDTO getAllSubscriptionDetailsByUUID(String uuid, String subscriptionStatus, - PaginationRequest request, int offset, - int limit) throws ApplicationManagementException; + SubscriptionStatistics getStatistics(SubscriptionInfo subscriptionInfo) throws ApplicationManagementException; /** * This method is responsible for retrieving device subscription details related to the given UUID. 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 3b14c5d68e..b8ae361f6e 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 @@ -23,22 +23,18 @@ import io.entgra.device.mgt.core.application.mgt.common.ApplicationInstallRespon import io.entgra.device.mgt.core.application.mgt.common.ApplicationSubscriptionInfo; import io.entgra.device.mgt.core.application.mgt.common.ApplicationType; import io.entgra.device.mgt.core.application.mgt.common.CategorizedSubscriptionResult; -import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscription; import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionData; -import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionResponse; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionStatistics; import io.entgra.device.mgt.core.application.mgt.common.dto.CategorizedSubscriptionCountsDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; -import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionsDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceOperationDTO; -import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionResponseDTO; import io.entgra.device.mgt.core.application.mgt.common.DeviceTypes; import io.entgra.device.mgt.core.application.mgt.common.ExecutionStatus; import io.entgra.device.mgt.core.application.mgt.common.SubAction; import io.entgra.device.mgt.core.application.mgt.common.SubscribingDeviceIdHolder; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionType; -import io.entgra.device.mgt.core.application.mgt.common.dto.GroupSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationReleaseDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.ScheduledSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationDTO; @@ -53,13 +49,9 @@ import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.Subs import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.service.SubscriptionManagementHelperService; 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.core.dto.DeviceDetailsDTO; -import io.entgra.device.mgt.core.device.mgt.core.dto.GroupDetailsDTO; import io.entgra.device.mgt.core.device.mgt.core.DeviceManagementConstants; import io.entgra.device.mgt.core.application.mgt.core.exception.UnexpectedServerErrorException; -import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOException; import io.entgra.device.mgt.core.device.mgt.core.dto.OperationDTO; -import io.entgra.device.mgt.core.device.mgt.core.dto.OwnerWithDeviceDTO; import io.entgra.device.mgt.core.device.mgt.extensions.logger.spi.EntgraLogger; import io.entgra.device.mgt.core.notification.logger.AppInstallLogContext; import io.entgra.device.mgt.core.notification.logger.impl.EntgraAppInstallLoggerImpl; @@ -118,7 +110,6 @@ import io.entgra.device.mgt.core.device.mgt.core.util.MDMIOSOperationUtil; import io.entgra.device.mgt.core.device.mgt.core.util.MDMWindowsOperationUtil; import io.entgra.device.mgt.core.identity.jwt.client.extension.dto.AccessTokenInfo; import org.wso2.carbon.user.api.UserStoreException; -import org.wso2.carbon.user.api.UserStoreManager; import javax.ws.rs.core.MediaType; import java.io.BufferedReader; @@ -133,7 +124,6 @@ import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -141,7 +131,6 @@ import java.util.Map; import java.util.Properties; import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; -import java.util.function.Function; import java.util.stream.Collectors; /** @@ -1709,19 +1698,6 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } } - @Override - public List getGroupsSubscriptionDetailsByUUID( - String uuid, String subscriptionStatus, PaginationRequest request, int offset, int limit) - throws ApplicationManagementException { - return null; - } - - @Override - public List getUserSubscriptionsByUUID(String uuid, String subscriptionStatus, - PaginationRequest request, int offset, int limit) throws ApplicationManagementException { - return null; - } - @Override public SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { @@ -1738,1317 +1714,10 @@ public class SubscriptionManagerImpl implements SubscriptionManager { return subscriptionManagementHelperService.getStatusBaseSubscriptions(subscriptionInfo, limit, offset); } -// @Override -// public List getGroupsSubscriptionDetailsByUUID( -// String uuid, String subscriptionStatus, PaginationRequest request, int offset, int limit) -// throws ApplicationManagementException { -// -// int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); -// boolean unsubscribe = subscriptionStatus.equals("unsubscribed"); -// -// try { -// ConnectionManagerUtil.openDBConnection(); -// -// ApplicationReleaseDTO applicationReleaseDTO = this.applicationReleaseDAO.getReleaseByUUID(uuid, tenantId); -// if (applicationReleaseDTO == null) { -// String msg = "Couldn't find an application release for application release UUID: " + uuid; -// log.error(msg); -// throw new NotFoundException(msg); -// } -// ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(uuid, tenantId); -// int appReleaseId = applicationReleaseDTO.getId(); -// List groupDetailsWithDevices = new ArrayList<>(); -// -// List groupDetails = -// subscriptionDAO.getGroupsSubscriptionDetailsByAppReleaseID(appReleaseId, unsubscribe, tenantId, offset, limit); -// if (groupDetails == null) { -// throw new ApplicationManagementException("Group details not found for appReleaseId: " + appReleaseId); -// } -// -// GroupManagementProviderService groupManagementProviderService = HelperUtil.getGroupManagementProviderService(); -// -// for (GroupSubscriptionDTO groupDetail : groupDetails) { -// -// if (StringUtils.isNotBlank(request.getGroupName()) && !request.getGroupName().equals(groupDetail.getGroupName())) { -// continue; -// } -// -// String groupName = StringUtils.isNotBlank(request.getGroupName()) ? request.getGroupName() : groupDetail.getGroupName(); -// -// // Retrieve group details and device IDs for the group using the service layer -// GroupDetailsDTO groupDetailWithDevices = -// groupManagementProviderService.getGroupDetailsWithDevices( -// groupName, applicationDTO.getDeviceTypeId(), request.getOwner(), -// request.getDeviceName(), request.getDeviceStatus(), offset, limit); -// -// SubscriptionsDTO groupDetailDTO = new SubscriptionsDTO(); -// groupDetailDTO.setId(groupDetailWithDevices.getGroupId()); -// groupDetailDTO.setName(groupDetail.getGroupName()); -// groupDetailDTO.setOwner(groupDetailWithDevices.getGroupOwner()); -// groupDetailDTO.setSubscribedBy(groupDetail.getSubscribedBy()); -// groupDetailDTO.setSubscribedTimestamp(groupDetail.getSubscribedTimestamp()); -// groupDetailDTO.setUnsubscribed(groupDetail.isUnsubscribed()); -// groupDetailDTO.setUnsubscribedBy(groupDetail.getUnsubscribedBy()); -// groupDetailDTO.setUnsubscribedTimestamp(groupDetail.getUnsubscribedTimestamp()); -// groupDetailDTO.setAppReleaseId(groupDetail.getAppReleaseId()); -// groupDetailDTO.setDeviceCount(groupDetailWithDevices.getDeviceCount()); -// -// // Fetch device subscriptions for each device ID in the group -// List pendingDevices = new ArrayList<>(); -// List installedDevices = new ArrayList<>(); -// List errorDevices = new ArrayList<>(); -// List newDevices = new ArrayList<>(); -// List subscribedDevices = new ArrayList<>(); -// -// List deviceIds = groupDetailWithDevices.getDeviceIds(); -// Map statusCounts = new HashMap<>(); -// statusCounts.put("COMPLETED", 0); -// statusCounts.put("ERROR", 0); -// statusCounts.put("PENDING", 0); -// statusCounts.put("NEW", 0); -// statusCounts.put("SUBSCRIBED", 0); -// -// for (Integer deviceId : deviceIds) { -// // Get subscribed devices if unsubscribed devices are requested -// List deviceSubscriptions; -// if (unsubscribe) { -// deviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds( -// appReleaseId, !unsubscribe, tenantId, deviceIds, -// request.getActionStatus(), request.getActionType(), request.getActionTriggeredBy(), request.getTabActionStatus()); -// } else { -// deviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds( -// groupDetail.getAppReleaseId(), false, tenantId, deviceIds, -// request.getActionStatus(), request.getActionType(), request.getActionTriggeredBy(), request.getTabActionStatus()); -// } -// List filteredDeviceSubscriptions = deviceSubscriptions.stream() -// .filter(subscription -> StringUtils.isBlank(request.getTabActionStatus()) || subscription.getStatus().equals(request.getTabActionStatus())) -// .collect(Collectors.toList()); -// boolean isNewDevice = true; -// for (DeviceSubscriptionDTO subscription : filteredDeviceSubscriptions) { -// if (subscription.getDeviceId() == deviceId) { -// DeviceSubscriptionData deviceDetail = new DeviceSubscriptionData(); -// deviceDetail.setDeviceId(subscription.getDeviceId()); -// deviceDetail.setStatus(subscription.getStatus()); -// deviceDetail.setActionType(subscription.getActionTriggeredFrom()); -// deviceDetail.setDeviceOwner(groupDetailWithDevices.getDeviceOwners().get(deviceId)); -// deviceDetail.setDeviceStatus(groupDetailWithDevices.getDeviceStatuses().get(deviceId)); -// deviceDetail.setDeviceName(groupDetailWithDevices.getDeviceNames().get(deviceId)); -// deviceDetail.setSubId(subscription.getId()); -// deviceDetail.setActionTriggeredBy(subscription.getSubscribedBy()); -// deviceDetail.setActionTriggeredTimestamp(subscription.getSubscribedTimestamp()); -// deviceDetail.setUnsubscribed(subscription.isUnsubscribed()); -// deviceDetail.setUnsubscribedBy(subscription.getUnsubscribedBy()); -// deviceDetail.setUnsubscribedTimestamp(subscription.getUnsubscribedTimestamp()); -// deviceDetail.setType(groupDetailWithDevices.getDeviceTypes().get(deviceId)); -// deviceDetail.setDeviceIdentifier(groupDetailWithDevices.getDeviceIdentifiers().get(deviceId)); -// -// String status = subscription.getStatus(); -// switch (status) { -// case "COMPLETED": -// installedDevices.add(deviceDetail); -// statusCounts.put("COMPLETED", statusCounts.get("COMPLETED") + 1); -// break; -// case "ERROR": -// case "INVALID": -// case "UNAUTHORIZED": -// errorDevices.add(deviceDetail); -// statusCounts.put("ERROR", statusCounts.get("ERROR") + 1); -// break; -// case "IN_PROGRESS": -// case "PENDING": -// case "REPEATED": -// pendingDevices.add(deviceDetail); -// statusCounts.put("PENDING", statusCounts.get("PENDING") + 1); -// break; -// default: -// newDevices.add(deviceDetail); -// statusCounts.put("NEW", statusCounts.get("NEW") + 1); -// break; -// } -// isNewDevice = false; -// } -// } -// if (isNewDevice) { -// boolean isSubscribedDevice = false; -// for (DeviceSubscriptionDTO subscribedDevice : deviceSubscriptions) { -// if (subscribedDevice.getDeviceId() == deviceId) { -// DeviceSubscriptionData subscribedDeviceDetail = new DeviceSubscriptionData(); -// subscribedDeviceDetail.setDeviceId(subscribedDevice.getDeviceId()); -// subscribedDeviceDetail.setDeviceOwner(groupDetailWithDevices.getDeviceOwners().get(deviceId)); -// subscribedDeviceDetail.setDeviceStatus(groupDetailWithDevices.getDeviceStatuses().get(deviceId)); -// subscribedDeviceDetail.setDeviceName(groupDetailWithDevices.getDeviceNames().get(deviceId)); -// subscribedDeviceDetail.setSubId(subscribedDevice.getId()); -// subscribedDeviceDetail.setActionTriggeredBy(subscribedDevice.getSubscribedBy()); -// subscribedDeviceDetail.setActionTriggeredTimestamp(subscribedDevice.getSubscribedTimestamp()); -// subscribedDeviceDetail.setActionType(subscribedDevice.getActionTriggeredFrom()); -// subscribedDeviceDetail.setStatus(subscribedDevice.getStatus()); -// subscribedDeviceDetail.setType(groupDetailWithDevices.getDeviceTypes().get(deviceId)); -// subscribedDeviceDetail.setDeviceIdentifier(groupDetailWithDevices.getDeviceIdentifiers().get(deviceId)); -// subscribedDevices.add(subscribedDeviceDetail); -// statusCounts.put("SUBSCRIBED", statusCounts.get("SUBSCRIBED") + 1); -// isSubscribedDevice = true; -// break; -// } -// } -// if (!isSubscribedDevice) { -// DeviceSubscriptionData newDeviceDetail = new DeviceSubscriptionData(); -// newDeviceDetail.setDeviceId(deviceId); -// newDeviceDetail.setDeviceOwner(groupDetailWithDevices.getDeviceOwners().get(deviceId)); -// newDeviceDetail.setDeviceStatus(groupDetailWithDevices.getDeviceStatuses().get(deviceId)); -// newDeviceDetail.setDeviceName(groupDetailWithDevices.getDeviceNames().get(deviceId)); -// newDeviceDetail.setType(groupDetailWithDevices.getDeviceTypes().get(deviceId)); -// newDeviceDetail.setDeviceIdentifier(groupDetailWithDevices.getDeviceIdentifiers().get(deviceId)); -// newDevices.add(newDeviceDetail); -// statusCounts.put("NEW", statusCounts.get("NEW") + 1); -// } -// } -// } -// -// int totalDevices = deviceIds.size(); -// Map statusPercentages = new HashMap<>(); -// for (Map.Entry entry : statusCounts.entrySet()) { -// double percentage = ((double) entry.getValue() / totalDevices) * 100; -// String formattedPercentage = String.format("%.2f", percentage); -// statusPercentages.put(entry.getKey(), Double.valueOf(formattedPercentage)); -// } -// -// List requestedDevices = new ArrayList<>(); -// if (StringUtils.isNotBlank(request.getTabActionStatus())) { -// switch (request.getTabActionStatus()) { -// case "COMPLETED": -// requestedDevices = installedDevices; -// break; -// case "PENDING": -// requestedDevices = pendingDevices; -// break; -// case "ERROR": -// requestedDevices = errorDevices; -// break; -// case "NEW": -// requestedDevices = newDevices; -// break; -// case "SUBSCRIBED": -// requestedDevices = subscribedDevices; -// break; -// } -// groupDetailDTO.setDevices(new CategorizedSubscriptionResult(requestedDevices, request.getTabActionStatus())); -// } else { -// CategorizedSubscriptionResult categorizedSubscriptionResult; -// if (subscribedDevices.isEmpty()) { -// categorizedSubscriptionResult = -// new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices); -// } else { -// categorizedSubscriptionResult = -// new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices, subscribedDevices); -// } -// groupDetailDTO.setDevices(categorizedSubscriptionResult); -// } -// groupDetailDTO.setStatusPercentages(statusPercentages); -// groupDetailsWithDevices.add(groupDetailDTO); -// } -// -// return groupDetailsWithDevices; -// } catch (ApplicationManagementDAOException e) { -// String msg = "Error occurred while fetching groups and devices for UUID: " + uuid; -// log.error(msg, e); -// throw new ApplicationManagementException(msg, e); -// } catch (DBConnectionException e) { -// String msg = "DB Connection error occurred while fetching groups and devices for UUID: " + uuid; -// log.error(msg, e); -// throw new ApplicationManagementException(msg, e); -// } catch (GroupManagementException e) { -// String msg = "Error occurred while fetching group details and device IDs: " + e.getMessage(); -// log.error(msg, e); -// throw new ApplicationManagementException(msg, e); -// } finally { -// ConnectionManagerUtil.closeDBConnection(); -// } -// } -// -// @Override -// public List getUserSubscriptionsByUUID(String uuid, String subscriptionStatus, -// PaginationRequest request, int offset, int limit) -// throws ApplicationManagementException { -// int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); -// boolean unsubscribe = subscriptionStatus.equals("unsubscribed"); -// String status; -// -// try { -// ConnectionManagerUtil.openDBConnection(); -// -// ApplicationReleaseDTO applicationReleaseDTO = this.applicationReleaseDAO.getReleaseByUUID(uuid, tenantId); -// if (applicationReleaseDTO == null) { -// String msg = "Couldn't find an application release for application release UUID: " + uuid; -// log.error(msg); -// throw new NotFoundException(msg); -// } -// ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(uuid, tenantId); -// int appReleaseId = applicationReleaseDTO.getId(); -// List userSubscriptionsWithDevices = new ArrayList<>(); -// -// List userSubscriptions = -// subscriptionDAO.getUserSubscriptionsByAppReleaseID(appReleaseId, unsubscribe, tenantId, offset, limit); -// if (userSubscriptions == null) { -// throw new ApplicationManagementException("User details not found for appReleaseId: " + appReleaseId); -// } -// -// DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); -// -// for (SubscriptionsDTO userSubscription : userSubscriptions) { -// -// if (StringUtils.isNotBlank(request.getUserName()) && !request.getUserName().equals(userSubscription.getName())) { -// continue; -// } -// -// String userName = StringUtils.isNotBlank(request.getUserName()) ? request.getUserName() : userSubscription.getName(); -// -// // Retrieve owner details and device IDs for the user using the service layer -// OwnerWithDeviceDTO ownerDetailsWithDevices = -// deviceManagementProviderService.getOwnersWithDeviceIds(userName, applicationDTO.getDeviceTypeId(), -// request.getOwner(), request.getDeviceName(), request.getDeviceStatus()); -// -// SubscriptionsDTO userSubscriptionDTO = new SubscriptionsDTO(); -// userSubscriptionDTO.setName(userSubscription.getName()); -// userSubscriptionDTO.setSubscribedBy(userSubscription.getSubscribedBy()); -// userSubscriptionDTO.setSubscribedTimestamp(userSubscription.getSubscribedTimestamp()); -// userSubscriptionDTO.setUnsubscribed(userSubscription.getUnsubscribed()); -// userSubscriptionDTO.setUnsubscribedBy(userSubscription.getUnsubscribedBy()); -// userSubscriptionDTO.setUnsubscribedTimestamp(userSubscription.getUnsubscribedTimestamp()); -// userSubscriptionDTO.setAppReleaseId(userSubscription.getAppReleaseId()); -// -// userSubscriptionDTO.setDeviceCount(ownerDetailsWithDevices.getDeviceCount()); -// -// // Fetch device subscriptions for each device ID associated with the user -// List pendingDevices = new ArrayList<>(); -// List installedDevices = new ArrayList<>(); -// List errorDevices = new ArrayList<>(); -// List newDevices = new ArrayList<>(); -// List subscribedDevices = new ArrayList<>(); -// -// List deviceIds = ownerDetailsWithDevices.getDeviceIds(); -// Map statusCounts = new HashMap<>(); -// statusCounts.put("PENDING", 0); -// statusCounts.put("COMPLETED", 0); -// statusCounts.put("ERROR", 0); -// statusCounts.put("NEW", 0); -// statusCounts.put("SUBSCRIBED", 0); -// -// List subscribedDeviceSubscriptions = new ArrayList<>(); -// if (unsubscribe) { -// subscribedDeviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds( -// appReleaseId, !unsubscribe, tenantId, deviceIds, request.getActionStatus(), request.getActionType(), -// request.getActionTriggeredBy(), request.getTabActionStatus()); -// } -// -// for (Integer deviceId : deviceIds) { -// List deviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds( -// userSubscription.getAppReleaseId(), unsubscribe, tenantId, deviceIds, request.getActionStatus(), request.getActionType(), -// request.getActionTriggeredBy(), request.getTabActionStatus()); -// OwnerWithDeviceDTO ownerWithDeviceByDeviceId = -// deviceManagementProviderService.getOwnerWithDeviceByDeviceId(deviceId, request.getOwner(), request.getDeviceName(), -// request.getDeviceStatus()); -// if (ownerWithDeviceByDeviceId == null) { -// continue; -// } -// boolean isNewDevice = true; -// for (DeviceSubscriptionDTO subscription : deviceSubscriptions) { -// if (subscription.getDeviceId() == deviceId) { -// DeviceSubscriptionData deviceDetail = new DeviceSubscriptionData(); -// deviceDetail.setDeviceId(subscription.getDeviceId()); -// deviceDetail.setSubId(subscription.getId()); -// deviceDetail.setDeviceOwner(ownerWithDeviceByDeviceId.getUserName()); -// deviceDetail.setDeviceStatus(ownerWithDeviceByDeviceId.getDeviceStatus()); -// deviceDetail.setDeviceName(ownerWithDeviceByDeviceId.getDeviceNames()); -// deviceDetail.setActionType(subscription.getActionTriggeredFrom()); -// deviceDetail.setStatus(subscription.getStatus()); -// deviceDetail.setActionType(subscription.getActionTriggeredFrom()); -// deviceDetail.setActionTriggeredBy(subscription.getSubscribedBy()); -// deviceDetail.setActionTriggeredTimestamp(subscription.getSubscribedTimestamp()); -// deviceDetail.setUnsubscribed(subscription.isUnsubscribed()); -// deviceDetail.setUnsubscribedBy(subscription.getUnsubscribedBy()); -// deviceDetail.setUnsubscribedTimestamp(subscription.getUnsubscribedTimestamp()); -// deviceDetail.setType(ownerWithDeviceByDeviceId.getDeviceTypes()); -// deviceDetail.setDeviceIdentifier(ownerWithDeviceByDeviceId.getDeviceIdentifiers()); -// -// status = subscription.getStatus(); -// switch (status) { -// case "COMPLETED": -// installedDevices.add(deviceDetail); -// statusCounts.put("COMPLETED", statusCounts.get("COMPLETED") + 1); -// break; -// case "ERROR": -// case "INVALID": -// case "UNAUTHORIZED": -// errorDevices.add(deviceDetail); -// statusCounts.put("ERROR", statusCounts.get("ERROR") + 1); -// break; -// case "IN_PROGRESS": -// case "PENDING": -// case "REPEATED": -// pendingDevices.add(deviceDetail); -// statusCounts.put("PENDING", statusCounts.get("PENDING") + 1); -// break; -// } -// isNewDevice = false; -// } -// } -// if (isNewDevice) { -// boolean isSubscribedDevice = false; -// for (DeviceSubscriptionDTO subscribedDevice : subscribedDeviceSubscriptions) { -// if (subscribedDevice.getDeviceId() == deviceId) { -// DeviceSubscriptionData subscribedDeviceDetail = new DeviceSubscriptionData(); -// subscribedDeviceDetail.setDeviceId(subscribedDevice.getDeviceId()); -// subscribedDeviceDetail.setDeviceName(ownerWithDeviceByDeviceId.getDeviceNames()); -// subscribedDeviceDetail.setDeviceOwner(ownerWithDeviceByDeviceId.getUserName()); -// subscribedDeviceDetail.setDeviceStatus(ownerWithDeviceByDeviceId.getDeviceStatus()); -// subscribedDeviceDetail.setSubId(subscribedDevice.getId()); -// subscribedDeviceDetail.setActionTriggeredBy(subscribedDevice.getSubscribedBy()); -// subscribedDeviceDetail.setActionTriggeredTimestamp(subscribedDevice.getSubscribedTimestamp()); -// subscribedDeviceDetail.setActionType(subscribedDevice.getActionTriggeredFrom()); -// subscribedDeviceDetail.setStatus(subscribedDevice.getStatus()); -// subscribedDeviceDetail.setType(ownerWithDeviceByDeviceId.getDeviceTypes()); -// subscribedDeviceDetail.setDeviceIdentifier(ownerWithDeviceByDeviceId.getDeviceIdentifiers()); -// subscribedDevices.add(subscribedDeviceDetail); -// statusCounts.put("SUBSCRIBED", statusCounts.get("SUBSCRIBED") + 1); -// isSubscribedDevice = true; -// break; -// } -// } -// if (!isSubscribedDevice) { -// DeviceSubscriptionData newDeviceDetail = new DeviceSubscriptionData(); -// newDeviceDetail.setDeviceId(deviceId); -// newDeviceDetail.setDeviceOwner(ownerWithDeviceByDeviceId.getUserName()); -// newDeviceDetail.setDeviceStatus(ownerWithDeviceByDeviceId.getDeviceStatus()); -// newDeviceDetail.setDeviceName(ownerWithDeviceByDeviceId.getDeviceNames()); -// newDeviceDetail.setType(ownerWithDeviceByDeviceId.getDeviceTypes()); -// newDeviceDetail.setDeviceIdentifier(ownerWithDeviceByDeviceId.getDeviceIdentifiers()); -// newDevices.add(newDeviceDetail); -// statusCounts.put("NEW", statusCounts.get("NEW") + 1); -// } -// } -// } -// -// int totalDevices = deviceIds.size(); -// Map statusPercentages = new HashMap<>(); -// for (Map.Entry entry : statusCounts.entrySet()) { -// double percentage = ((double) entry.getValue() / totalDevices) * 100; -// String formattedPercentage = String.format("%.2f", percentage); -// statusPercentages.put(entry.getKey(), Double.valueOf(formattedPercentage)); -// } -// -// List requestedDevices = new ArrayList<>(); -// if (StringUtils.isNotBlank(request.getTabActionStatus())) { -// switch (request.getTabActionStatus()) { -// case "COMPLETED": -// requestedDevices = installedDevices; -// break; -// case "PENDING": -// requestedDevices = pendingDevices; -// break; -// case "ERROR": -// requestedDevices = errorDevices; -// break; -// case "NEW": -// requestedDevices = newDevices; -// break; -// case "SUBSCRIBED": -// requestedDevices = subscribedDevices; -// break; -// } -// userSubscriptionDTO.setDevices(new CategorizedSubscriptionResult(requestedDevices, request.getTabActionStatus())); -// } else { -// CategorizedSubscriptionResult categorizedSubscriptionResult; -// if (subscribedDevices.isEmpty()) { -// categorizedSubscriptionResult = -// new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices); -// } else { -// categorizedSubscriptionResult = -// new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices, -// subscribedDevices); -// } -// userSubscriptionDTO.setDevices(categorizedSubscriptionResult); -// userSubscriptionDTO.setStatusPercentages(statusPercentages); -// -// } -// userSubscriptionsWithDevices.add(userSubscriptionDTO); -// } -// return userSubscriptionsWithDevices; -// } catch (ApplicationManagementDAOException e) { -// String msg = "Error occurred while getting user subscriptions for the application release UUID: " + uuid; -// log.error(msg, e); -// throw new ApplicationManagementException(msg, e); -// } catch (DBConnectionException e) { -// String msg = "DB Connection error occurred while getting user subscriptions for UUID: " + uuid; -// log.error(msg, e); -// throw new ApplicationManagementException(msg, e); -// } catch (DeviceManagementDAOException e) { -// throw new RuntimeException(e); -// } finally { -// ConnectionManagerUtil.closeDBConnection(); -// } -// } -// -// -// @Override -// public List getRoleSubscriptionsByUUID(String uuid, String subscriptionStatus, -// PaginationRequest request, int offset, int limit) -// throws ApplicationManagementException { -// int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); -// boolean unsubscribe = subscriptionStatus.equals("unsubscribed"); -// String roleName; -// String status; -// -// try { -// ConnectionManagerUtil.openDBConnection(); -// -// ApplicationReleaseDTO applicationReleaseDTO = this.applicationReleaseDAO.getReleaseByUUID(uuid, tenantId); -// if (applicationReleaseDTO == null) { -// String msg = "Couldn't find an application release for application release UUID: " + uuid; -// log.error(msg); -// throw new NotFoundException(msg); -// } -// ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(uuid, tenantId); -// int appReleaseId = applicationReleaseDTO.getId(); -// List roleSubscriptionsWithDevices = new ArrayList<>(); -// -// List roleSubscriptions = -// subscriptionDAO.getRoleSubscriptionsByAppReleaseID(appReleaseId, unsubscribe, tenantId, offset, limit); -// if (roleSubscriptions == null) { -// throw new ApplicationManagementException("Role details not found for appReleaseId: " + appReleaseId); -// } -// - DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); -// -// for (SubscriptionsDTO roleSubscription : roleSubscriptions) { -// -// roleName = StringUtils.isNotBlank(request.getRoleName()) ? request.getRoleName() : roleSubscription.getName(); -// -// SubscriptionsDTO roleSubscriptionDTO = new SubscriptionsDTO(); -// roleSubscriptionDTO.setName(roleSubscription.getName()); -// roleSubscriptionDTO.setSubscribedBy(roleSubscription.getSubscribedBy()); -// roleSubscriptionDTO.setSubscribedTimestamp(roleSubscription.getSubscribedTimestamp()); -// roleSubscriptionDTO.setUnsubscribed(roleSubscription.getUnsubscribed()); -// roleSubscriptionDTO.setUnsubscribedBy(roleSubscription.getUnsubscribedBy()); -// roleSubscriptionDTO.setUnsubscribedTimestamp(roleSubscription.getUnsubscribedTimestamp()); -// roleSubscriptionDTO.setAppReleaseId(roleSubscription.getAppReleaseId()); -// -// List pendingDevices = new ArrayList<>(); -// List installedDevices = new ArrayList<>(); -// List errorDevices = new ArrayList<>(); -// List newDevices = new ArrayList<>(); -// List subscribedDevices = new ArrayList<>(); -// -// Map statusCounts = new HashMap<>(); -// statusCounts.put("PENDING", 0); -// statusCounts.put("COMPLETED", 0); -// statusCounts.put("ERROR", 0); -// statusCounts.put("NEW", 0); -// statusCounts.put("SUBSCRIBED", 0); -// -// // getting the user list for the role -// List users = this.getUsersForRole(roleName); -// -// for (String user : users) { -// -// // for each user get the device info and device ids -// OwnerWithDeviceDTO ownerDetailsWithDevices; -// try { -// ownerDetailsWithDevices = deviceManagementProviderService.getOwnersWithDeviceIds(user, applicationDTO.getDeviceTypeId(), -// request.getOwner(), request.getDeviceName(), request.getDeviceStatus()); -// } catch (DeviceManagementDAOException e) { -// throw new ApplicationManagementException("Error retrieving owner details with devices for user: " + user, e); -// } -// -// List deviceIds = ownerDetailsWithDevices.getDeviceIds(); -// // now for each device id -// for (Integer deviceId : deviceIds) { -// -// List subscribedDeviceSubscriptions = new ArrayList<>(); -// if (unsubscribe) { -// subscribedDeviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds( -// appReleaseId, !unsubscribe, tenantId, deviceIds, request.getActionStatus(), request.getActionType(), -// request.getActionTriggeredBy(), request.getTabActionStatus()); -// } -// -// // why the fuck is this here -// OwnerWithDeviceDTO ownerWithDeviceByDeviceId = -// deviceManagementProviderService.getOwnerWithDeviceByDeviceId(deviceId, request.getOwner(), request.getDeviceName(), -// request.getDeviceStatus()); -// -// -// if (ownerWithDeviceByDeviceId == null) { -// continue; -// } -// -// -// List deviceSubscriptions; -// try { -// deviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds( -// roleSubscription.getAppReleaseId(), unsubscribe, tenantId, deviceIds, request.getActionStatus(), -// request.getActionType(), request.getActionTriggeredBy(), request.getTabActionStatus()); -// } catch (ApplicationManagementDAOException e) { -// throw new ApplicationManagementException("Error retrieving device subscriptions", e); -// } -// -// boolean isNewDevice = true; -// for (DeviceSubscriptionDTO deviceSubscription : deviceSubscriptions) { -// if (deviceSubscription.getDeviceId() == deviceId) { -// DeviceSubscriptionData deviceDetail = new DeviceSubscriptionData(); -// deviceDetail.setDeviceId(deviceSubscription.getDeviceId()); -// deviceDetail.setDeviceName(ownerWithDeviceByDeviceId.getDeviceNames()); -// deviceDetail.setDeviceOwner(ownerWithDeviceByDeviceId.getUserName()); -// deviceDetail.setDeviceStatus(ownerWithDeviceByDeviceId.getDeviceStatus()); -// deviceDetail.setActionType(deviceSubscription.getActionTriggeredFrom()); -// deviceDetail.setStatus(deviceSubscription.getStatus()); -// deviceDetail.setActionType(deviceSubscription.getActionTriggeredFrom()); -// deviceDetail.setActionTriggeredBy(deviceSubscription.getSubscribedBy()); -// deviceDetail.setSubId(deviceSubscription.getId()); -// deviceDetail.setActionTriggeredTimestamp(deviceSubscription.getSubscribedTimestamp()); -// deviceDetail.setUnsubscribed(deviceSubscription.isUnsubscribed()); -// deviceDetail.setUnsubscribedBy(deviceSubscription.getUnsubscribedBy()); -// deviceDetail.setUnsubscribedTimestamp(deviceSubscription.getUnsubscribedTimestamp()); -// deviceDetail.setType(ownerWithDeviceByDeviceId.getDeviceTypes()); -// deviceDetail.setDeviceIdentifier(ownerWithDeviceByDeviceId.getDeviceIdentifiers()); -// -// status = deviceSubscription.getStatus(); -// switch (status) { -// case "COMPLETED": -// installedDevices.add(deviceDetail); -// statusCounts.put("COMPLETED", statusCounts.get("COMPLETED") + 1); -// break; -// case "ERROR": -// case "INVALID": -// case "UNAUTHORIZED": -// errorDevices.add(deviceDetail); -// statusCounts.put("ERROR", statusCounts.get("ERROR") + 1); -// break; -// case "IN_PROGRESS": -// case "PENDING": -// case "REPEATED": -// pendingDevices.add(deviceDetail); -// statusCounts.put("PENDING", statusCounts.get("PENDING") + 1); -// break; -// } -// isNewDevice = false; -// } -// } -// if (isNewDevice) { -// boolean isSubscribedDevice = false; -// for (DeviceSubscriptionDTO subscribedDevice : subscribedDeviceSubscriptions) { -// if (subscribedDevice.getDeviceId() == deviceId) { -// DeviceSubscriptionData subscribedDeviceDetail = new DeviceSubscriptionData(); -// subscribedDeviceDetail.setDeviceId(subscribedDevice.getDeviceId()); -// subscribedDeviceDetail.setDeviceName(ownerWithDeviceByDeviceId.getDeviceNames()); -// subscribedDeviceDetail.setDeviceOwner(ownerWithDeviceByDeviceId.getUserName()); -// subscribedDeviceDetail.setDeviceStatus(ownerWithDeviceByDeviceId.getDeviceStatus()); -// subscribedDeviceDetail.setSubId(subscribedDevice.getId()); -// subscribedDeviceDetail.setActionTriggeredBy(subscribedDevice.getSubscribedBy()); -// subscribedDeviceDetail.setActionTriggeredTimestamp(subscribedDevice.getSubscribedTimestamp()); -// subscribedDeviceDetail.setActionType(subscribedDevice.getActionTriggeredFrom()); -// subscribedDeviceDetail.setStatus(subscribedDevice.getStatus()); -// subscribedDeviceDetail.setType(ownerWithDeviceByDeviceId.getDeviceTypes()); -// subscribedDeviceDetail.setDeviceIdentifier(ownerWithDeviceByDeviceId.getDeviceIdentifiers()); -// subscribedDevices.add(subscribedDeviceDetail); -// statusCounts.put("SUBSCRIBED", statusCounts.get("SUBSCRIBED") + 1); -// isSubscribedDevice = true; -// break; -// } -// } -// if (!isSubscribedDevice) { -// DeviceSubscriptionData newDeviceDetail = new DeviceSubscriptionData(); -// newDeviceDetail.setDeviceId(deviceId); -// newDeviceDetail.setDeviceName(ownerWithDeviceByDeviceId.getDeviceNames()); -// newDeviceDetail.setDeviceOwner(ownerWithDeviceByDeviceId.getUserName()); -// newDeviceDetail.setDeviceStatus(ownerWithDeviceByDeviceId.getDeviceStatus()); -// newDeviceDetail.setType(ownerWithDeviceByDeviceId.getDeviceTypes()); -// newDeviceDetail.setDeviceIdentifier(ownerWithDeviceByDeviceId.getDeviceIdentifiers()); -// newDevices.add(newDeviceDetail); -// statusCounts.put("NEW", statusCounts.get("NEW") + 1); -// } -// } -// } -// } -// -// int totalDevices = -// pendingDevices.size() + installedDevices.size() + errorDevices.size() + newDevices.size() + subscribedDevices.size(); -// Map statusPercentages = new HashMap<>(); -// for (Map.Entry entry : statusCounts.entrySet()) { -// double percentage = totalDevices == 0 ? 0.0 : ((double) entry.getValue() / totalDevices) * 100; -// String formattedPercentage = String.format("%.2f", percentage); -// statusPercentages.put(entry.getKey(), Double.valueOf(formattedPercentage)); -// } -// -// List requestedDevices = new ArrayList<>(); -// if (StringUtils.isNotBlank(request.getTabActionStatus())) { -// switch (request.getTabActionStatus()) { -// case "COMPLETED": -// requestedDevices = installedDevices; -// break; -// case "PENDING": -// requestedDevices = pendingDevices; -// break; -// case "ERROR": -// requestedDevices = errorDevices; -// break; -// case "NEW": -// requestedDevices = newDevices; -// break; -// case "SUBSCRIBED": -// requestedDevices = subscribedDevices; -// break; -// } -// roleSubscriptionDTO.setDevices(new CategorizedSubscriptionResult(requestedDevices, request.getTabActionStatus())); -// -// } else { -// CategorizedSubscriptionResult categorizedSubscriptionResult; -// if (subscribedDevices.isEmpty()) { -// categorizedSubscriptionResult = -// new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices); -// } else { -// categorizedSubscriptionResult = -// new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices, -// subscribedDevices); -// } -// roleSubscriptionDTO.setDevices(categorizedSubscriptionResult); -// roleSubscriptionDTO.setStatusPercentages(statusPercentages); -// roleSubscriptionDTO.setDeviceCount(totalDevices); -// } -// roleSubscriptionsWithDevices.add(roleSubscriptionDTO); -// } -// -// return roleSubscriptionsWithDevices; -// } catch (ApplicationManagementDAOException | DeviceManagementDAOException e) { -// String msg = "Error occurred in retrieving role subscriptions with devices"; -// log.error(msg, e); -// throw new ApplicationManagementException(msg, e); -// } catch (DBConnectionException e) { -// String msg = "Error occurred while retrieving the database connection"; -// log.error(msg, e); -// throw new ApplicationManagementException(msg, e); -// } catch (UserStoreException e) { -// String msg = "Error occurred while retrieving users for role"; -// log.error(msg, e); -// throw new ApplicationManagementException(msg, e); -// } finally { -// ConnectionManagerUtil.closeDBConnection(); -// } -// } - -// @Override -// public List getRoleSubscriptionsByUUID(String uuid, String subscriptionStatus, -// PaginationRequest request, int offset, int limit) -// throws ApplicationManagementException { -// int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); -// boolean unsubscribe = subscriptionStatus.equals("unsubscribed"); -// String roleName; -// String status; -// -// try { -// ConnectionManagerUtil.openDBConnection(); -// -// ApplicationReleaseDTO applicationReleaseDTO = this.applicationReleaseDAO.getReleaseByUUID(uuid, tenantId); -// if (applicationReleaseDTO == null) { -// String msg = "Couldn't find an application release for application release UUID: " + uuid; -// log.error(msg); -// throw new NotFoundException(msg); -// } -// ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(uuid, tenantId); -// int appReleaseId = applicationReleaseDTO.getId(); -// List roleSubscriptionsWithDevices = new ArrayList<>(); -// -// List roleSubscriptions = -// subscriptionDAO.getRoleSubscriptionsByAppReleaseID(appReleaseId, unsubscribe, tenantId, offset, limit); -// if (roleSubscriptions == null) { -// throw new ApplicationManagementException("Role details not found for appReleaseId: " + appReleaseId); -// } -// -// DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); -// -// for (SubscriptionsDTO roleSubscription : roleSubscriptions) { -// -// roleName = StringUtils.isNotBlank(request.getRoleName()) ? request.getRoleName() : roleSubscription.getName(); -// -// SubscriptionsDTO roleSubscriptionDTO = new SubscriptionsDTO(); -// roleSubscriptionDTO.setName(roleSubscription.getName()); -// roleSubscriptionDTO.setSubscribedBy(roleSubscription.getSubscribedBy()); -// roleSubscriptionDTO.setSubscribedTimestamp(roleSubscription.getSubscribedTimestamp()); -// roleSubscriptionDTO.setUnsubscribed(roleSubscription.getUnsubscribed()); -// roleSubscriptionDTO.setUnsubscribedBy(roleSubscription.getUnsubscribedBy()); -// roleSubscriptionDTO.setUnsubscribedTimestamp(roleSubscription.getUnsubscribedTimestamp()); -// roleSubscriptionDTO.setAppReleaseId(roleSubscription.getAppReleaseId()); -// -// List pendingDevices = new ArrayList<>(); -// List installedDevices = new ArrayList<>(); -// List errorDevices = new ArrayList<>(); -// List newDevices = new ArrayList<>(); -// List subscribedDevices = new ArrayList<>(); -// -// Map statusCounts = new HashMap<>(); -// statusCounts.put("PENDING", 0); -// statusCounts.put("COMPLETED", 0); -// statusCounts.put("ERROR", 0); -// statusCounts.put("NEW", 0); -// statusCounts.put("SUBSCRIBED", 0); -// -// List users = this.getUsersForRole(roleName); -// -// for (String user : users) { -// OwnerWithDeviceDTO ownerDetailsWithDevices; -// try { -// ownerDetailsWithDevices = deviceManagementProviderService.getOwnersWithDeviceIds(user, applicationDTO.getDeviceTypeId(), -// request.getOwner(), request.getDeviceName(), request.getDeviceStatus()); -// } catch (DeviceManagementDAOException e) { -// throw new ApplicationManagementException("Error retrieving owner details with devices for user: " + user, e); -// } -// -// List deviceIds = ownerDetailsWithDevices.getDeviceIds(); -// for (Integer deviceId : deviceIds) { -// -// List subscribedDeviceSubscriptions = new ArrayList<>(); -// if (unsubscribe) { -// subscribedDeviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds( -// appReleaseId, !unsubscribe, tenantId, deviceIds, request.getActionStatus(), request.getActionType(), -// request.getActionTriggeredBy(), request.getTabActionStatus()); -// } -// OwnerWithDeviceDTO ownerWithDeviceByDeviceId = -// deviceManagementProviderService.getOwnerWithDeviceByDeviceId(deviceId, request.getOwner(), request.getDeviceName(), -// request.getDeviceStatus()); -// if (ownerWithDeviceByDeviceId == null) { -// continue; -// } -// List deviceSubscriptions; -// try { -// deviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds( -// roleSubscription.getAppReleaseId(), unsubscribe, tenantId, deviceIds, request.getActionStatus(), -// request.getActionType(), request.getActionTriggeredBy(), request.getTabActionStatus()); -// } catch (ApplicationManagementDAOException e) { -// throw new ApplicationManagementException("Error retrieving device subscriptions", e); -// } -// -// boolean isNewDevice = true; -// for (DeviceSubscriptionDTO deviceSubscription : deviceSubscriptions) { -// if (deviceSubscription.getDeviceId() == deviceId) { -// DeviceSubscriptionData deviceDetail = new DeviceSubscriptionData(); -// deviceDetail.setDeviceId(deviceSubscription.getDeviceId()); -// deviceDetail.setDeviceName(ownerWithDeviceByDeviceId.getDeviceNames()); -// deviceDetail.setDeviceOwner(ownerWithDeviceByDeviceId.getUserName()); -// deviceDetail.setDeviceStatus(ownerWithDeviceByDeviceId.getDeviceStatus()); -// deviceDetail.setActionType(deviceSubscription.getActionTriggeredFrom()); -// deviceDetail.setStatus(deviceSubscription.getStatus()); -// deviceDetail.setActionType(deviceSubscription.getActionTriggeredFrom()); -// deviceDetail.setActionTriggeredBy(deviceSubscription.getSubscribedBy()); -// deviceDetail.setSubId(deviceSubscription.getId()); -// deviceDetail.setActionTriggeredTimestamp(deviceSubscription.getSubscribedTimestamp()); -// deviceDetail.setUnsubscribed(deviceSubscription.isUnsubscribed()); -// deviceDetail.setUnsubscribedBy(deviceSubscription.getUnsubscribedBy()); -// deviceDetail.setUnsubscribedTimestamp(deviceSubscription.getUnsubscribedTimestamp()); -// deviceDetail.setType(ownerWithDeviceByDeviceId.getDeviceTypes()); -// deviceDetail.setDeviceIdentifier(ownerWithDeviceByDeviceId.getDeviceIdentifiers()); -// -// status = deviceSubscription.getStatus(); -// switch (status) { -// case "COMPLETED": -// installedDevices.add(deviceDetail); -// statusCounts.put("COMPLETED", statusCounts.get("COMPLETED") + 1); -// break; -// case "ERROR": -// case "INVALID": -// case "UNAUTHORIZED": -// errorDevices.add(deviceDetail); -// statusCounts.put("ERROR", statusCounts.get("ERROR") + 1); -// break; -// case "IN_PROGRESS": -// case "PENDING": -// case "REPEATED": -// pendingDevices.add(deviceDetail); -// statusCounts.put("PENDING", statusCounts.get("PENDING") + 1); -// break; -// } -// isNewDevice = false; -// } -// } -// if (isNewDevice) { -// boolean isSubscribedDevice = false; -// for (DeviceSubscriptionDTO subscribedDevice : subscribedDeviceSubscriptions) { -// if (subscribedDevice.getDeviceId() == deviceId) { -// DeviceSubscriptionData subscribedDeviceDetail = new DeviceSubscriptionData(); -// subscribedDeviceDetail.setDeviceId(subscribedDevice.getDeviceId()); -// subscribedDeviceDetail.setDeviceName(ownerWithDeviceByDeviceId.getDeviceNames()); -// subscribedDeviceDetail.setDeviceOwner(ownerWithDeviceByDeviceId.getUserName()); -// subscribedDeviceDetail.setDeviceStatus(ownerWithDeviceByDeviceId.getDeviceStatus()); -// subscribedDeviceDetail.setSubId(subscribedDevice.getId()); -// subscribedDeviceDetail.setActionTriggeredBy(subscribedDevice.getSubscribedBy()); -// subscribedDeviceDetail.setActionTriggeredTimestamp(subscribedDevice.getSubscribedTimestamp()); -// subscribedDeviceDetail.setActionType(subscribedDevice.getActionTriggeredFrom()); -// subscribedDeviceDetail.setStatus(subscribedDevice.getStatus()); -// subscribedDeviceDetail.setType(ownerWithDeviceByDeviceId.getDeviceTypes()); -// subscribedDeviceDetail.setDeviceIdentifier(ownerWithDeviceByDeviceId.getDeviceIdentifiers()); -// subscribedDevices.add(subscribedDeviceDetail); -// statusCounts.put("SUBSCRIBED", statusCounts.get("SUBSCRIBED") + 1); -// isSubscribedDevice = true; -// break; -// } -// } -// if (!isSubscribedDevice) { -// DeviceSubscriptionData newDeviceDetail = new DeviceSubscriptionData(); -// newDeviceDetail.setDeviceId(deviceId); -// newDeviceDetail.setDeviceName(ownerWithDeviceByDeviceId.getDeviceNames()); -// newDeviceDetail.setDeviceOwner(ownerWithDeviceByDeviceId.getUserName()); -// newDeviceDetail.setDeviceStatus(ownerWithDeviceByDeviceId.getDeviceStatus()); -// newDeviceDetail.setType(ownerWithDeviceByDeviceId.getDeviceTypes()); -// newDeviceDetail.setDeviceIdentifier(ownerWithDeviceByDeviceId.getDeviceIdentifiers()); -// newDevices.add(newDeviceDetail); -// statusCounts.put("NEW", statusCounts.get("NEW") + 1); -// } -// } -// } -// } -// -// int totalDevices = -// pendingDevices.size() + installedDevices.size() + errorDevices.size() + newDevices.size() + subscribedDevices.size(); -// Map statusPercentages = new HashMap<>(); -// for (Map.Entry entry : statusCounts.entrySet()) { -// double percentage = totalDevices == 0 ? 0.0 : ((double) entry.getValue() / totalDevices) * 100; -// String formattedPercentage = String.format("%.2f", percentage); -// statusPercentages.put(entry.getKey(), Double.valueOf(formattedPercentage)); -// } -// -// List requestedDevices = new ArrayList<>(); -// if (StringUtils.isNotBlank(request.getTabActionStatus())) { -// switch (request.getTabActionStatus()) { -// case "COMPLETED": -// requestedDevices = installedDevices; -// break; -// case "PENDING": -// requestedDevices = pendingDevices; -// break; -// case "ERROR": -// requestedDevices = errorDevices; -// break; -// case "NEW": -// requestedDevices = newDevices; -// break; -// case "SUBSCRIBED": -// requestedDevices = subscribedDevices; -// break; -// } -// roleSubscriptionDTO.setDevices(new CategorizedSubscriptionResult(requestedDevices, request.getTabActionStatus())); -// -// } else { -// CategorizedSubscriptionResult categorizedSubscriptionResult; -// if (subscribedDevices.isEmpty()) { -// categorizedSubscriptionResult = -// new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices); -// } else { -// categorizedSubscriptionResult = -// new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices, -// subscribedDevices); -// } -// roleSubscriptionDTO.setDevices(categorizedSubscriptionResult); -// roleSubscriptionDTO.setStatusPercentages(statusPercentages); -// roleSubscriptionDTO.setDeviceCount(totalDevices); -// } -// roleSubscriptionsWithDevices.add(roleSubscriptionDTO); -// } -// -// return roleSubscriptionsWithDevices; -// } catch (ApplicationManagementDAOException | DeviceManagementDAOException e) { -// String msg = "Error occurred in retrieving role subscriptions with devices"; -// log.error(msg, e); -// throw new ApplicationManagementException(msg, e); -// } catch (DBConnectionException e) { -// String msg = "Error occurred while retrieving the database connection"; -// log.error(msg, e); -// throw new ApplicationManagementException(msg, e); -// } catch (UserStoreException e) { -// String msg = "Error occurred while retrieving users for role"; -// log.error(msg, e); -// throw new ApplicationManagementException(msg, e); -// } finally { -// ConnectionManagerUtil.closeDBConnection(); -// } -// } - - // Get user list for each role - public List getUsersForRole(String roleName) throws UserStoreException { - PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); - int tenantId = ctx.getTenantId(); - UserStoreManager userStoreManager = DataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId).getUserStoreManager(); - String[] users = userStoreManager.getUserListOfRole(roleName); - return Arrays.asList(users); - } - @Override - public DeviceSubscriptionResponseDTO getDeviceSubscriptionsDetailsByUUID(String uuid, String subscriptionStatus, PaginationRequest request, int offset, - int limit) throws ApplicationManagementException { - - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - boolean unsubscribe = subscriptionStatus.equals("unsubscribed"); - - try { - ConnectionManagerUtil.openDBConnection(); - - ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO.getReleaseByUUID(uuid, tenantId); - if (applicationReleaseDTO == null) { - String msg = "Couldn't find an application release for application release UUID: " + uuid; - log.error(msg); - throw new NotFoundException(msg); - } - ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(uuid, tenantId); - int appReleaseId = applicationReleaseDTO.getId(); - - DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); - List deviceSubscriptions = - subscriptionDAO.getDeviceSubscriptionsByAppReleaseID(appReleaseId, unsubscribe, tenantId, offset, limit); - - // empty response for no device subscriptions - if (deviceSubscriptions.isEmpty()) { - return new DeviceSubscriptionResponseDTO(0, Collections.emptyMap(), - new CategorizedSubscriptionResult(Collections.emptyList(), - Collections.emptyList(), Collections.emptyList(), Collections.emptyList())); - } - - List allDevices = - deviceManagementProviderService.getDevicesByTenantId(tenantId, applicationDTO.getDeviceTypeId(), - request.getOwner(), request.getDeviceStatus()); - - List deviceIds = allDevices.stream() - .map(DeviceDetailsDTO::getDeviceId) - .collect(Collectors.toList()); - - Map statusCounts = new HashMap<>(); - statusCounts.put("PENDING", 0); - statusCounts.put("COMPLETED", 0); - statusCounts.put("ERROR", 0); - statusCounts.put("NEW", 0); - statusCounts.put("SUBSCRIBED", 0); - - List installedDevices = new ArrayList<>(); - List pendingDevices = new ArrayList<>(); - List errorDevices = new ArrayList<>(); - List newDevices = new ArrayList<>(); - List subscribedDevices = new ArrayList<>(); - - Map deviceSubscriptionMap = deviceSubscriptions.stream() - .collect(Collectors.toMap(DeviceSubscriptionDTO::getDeviceId, Function.identity())); - Map allDevicesMap = allDevices.stream() - .collect(Collectors.toMap(DeviceDetailsDTO::getDeviceId, Function.identity())); - - List allSubscriptionsForUnSubscribed = null; -// subscriptionDAO.getSubscriptionDetailsByDeviceIds(appReleaseId, !unsubscribe, tenantId, deviceIds, request.getActionStatus(), -// request.getActionType(), request.getActionTriggeredBy(), request.getTabActionStatus()); - List allSubscriptionsForSubscribed = null; -// subscriptionDAO.getSubscriptionDetailsByDeviceIds(appReleaseId, unsubscribe, tenantId, deviceIds, request.getActionStatus(), -// request.getActionType(), request.getActionTriggeredBy(), request.getTabActionStatus()); - Map allSubscriptionForUnSubscribedMap = allSubscriptionsForUnSubscribed.stream() - .collect(Collectors.toMap(DeviceSubscriptionDTO::getDeviceId, Function.identity())); - Map allSubscriptionForSubscribedMap = allSubscriptionsForSubscribed.stream() - .collect(Collectors.toMap(DeviceSubscriptionDTO::getDeviceId, Function.identity())); - - for (DeviceDetailsDTO device : allDevices) { - Integer deviceId = device.getDeviceId(); - OwnerWithDeviceDTO ownerWithDevice = - deviceManagementProviderService.getOwnerWithDeviceByDeviceId(deviceId, request.getOwner(), request.getDeviceName(), - request.getDeviceStatus()); - if (ownerWithDevice == null || (request.getDeviceName() != null && !request.getDeviceName().isEmpty() && - (ownerWithDevice.getDeviceNames() == null || !ownerWithDevice.getDeviceNames().contains(request.getDeviceName())))) { - continue; - } - if (deviceSubscriptionMap.containsKey(deviceId)) { - DeviceSubscriptionDTO subscription = deviceSubscriptionMap.get(deviceId); - DeviceSubscriptionData deviceDetail = new DeviceSubscriptionData(); - deviceDetail.setDeviceId(subscription.getDeviceId()); - deviceDetail.setSubId(subscription.getId()); - deviceDetail.setDeviceName(ownerWithDevice.getDeviceNames()); - deviceDetail.setDeviceOwner(ownerWithDevice.getUserName()); - deviceDetail.setDeviceStatus(ownerWithDevice.getDeviceStatus()); - deviceDetail.setActionType(subscription.getActionTriggeredFrom()); - deviceDetail.setStatus(subscription.getStatus()); - deviceDetail.setActionTriggeredBy(subscription.getSubscribedBy()); - deviceDetail.setActionTriggeredTimestamp(subscription.getSubscribedTimestamp()); - deviceDetail.setUnsubscribed(subscription.isUnsubscribed()); - deviceDetail.setUnsubscribedBy(subscription.getUnsubscribedBy()); - deviceDetail.setUnsubscribedTimestamp(subscription.getUnsubscribedTimestamp()); - deviceDetail.setType(ownerWithDevice.getDeviceTypes()); - deviceDetail.setDeviceIdentifier(ownerWithDevice.getDeviceIdentifiers()); - - String status = subscription.getStatus(); - switch (status) { - case "COMPLETED": - installedDevices.add(deviceDetail); - statusCounts.put("COMPLETED", statusCounts.get("COMPLETED") + 1); - break; - case "ERROR": - case "INVALID": - case "UNAUTHORIZED": - errorDevices.add(deviceDetail); - statusCounts.put("ERROR", statusCounts.get("ERROR") + 1); - break; - case "IN_PROGRESS": - case "PENDING": - case "REPEATED": - pendingDevices.add(deviceDetail); - statusCounts.put("PENDING", statusCounts.get("PENDING") + 1); - break; - } - } else if (unsubscribe && allSubscriptionForUnSubscribedMap.containsKey(deviceId) && !deviceSubscriptionMap.containsKey(deviceId)) { - // Check if the device subscription has unsubscribed status set to false - DeviceSubscriptionDTO allSubscription = allSubscriptionForUnSubscribedMap.get(deviceId); - if (!allSubscription.isUnsubscribed()) { - DeviceSubscriptionData subscribedDeviceDetail = new DeviceSubscriptionData(); - subscribedDeviceDetail.setDeviceId(allSubscription.getDeviceId()); - subscribedDeviceDetail.setDeviceName(ownerWithDevice.getDeviceNames()); - subscribedDeviceDetail.setDeviceOwner(ownerWithDevice.getUserName()); - subscribedDeviceDetail.setDeviceStatus(ownerWithDevice.getDeviceStatus()); - subscribedDeviceDetail.setSubId(allSubscription.getId()); - subscribedDeviceDetail.setActionTriggeredBy(allSubscription.getSubscribedBy()); - subscribedDeviceDetail.setActionTriggeredTimestamp(allSubscription.getSubscribedTimestamp()); - subscribedDeviceDetail.setActionType(allSubscription.getActionTriggeredFrom()); - subscribedDeviceDetail.setStatus(allSubscription.getStatus()); - subscribedDeviceDetail.setType(ownerWithDevice.getDeviceTypes()); - subscribedDeviceDetail.setDeviceIdentifier(ownerWithDevice.getDeviceIdentifiers()); - subscribedDevices.add(subscribedDeviceDetail); - statusCounts.put("SUBSCRIBED", statusCounts.get("SUBSCRIBED") + 1); - } - } else if (unsubscribe && !allSubscriptionForUnSubscribedMap.containsKey(deviceId) && !deviceSubscriptionMap.containsKey(deviceId) - && (allDevicesMap.containsKey(deviceId))) { - DeviceSubscriptionData newDeviceDetail = new DeviceSubscriptionData(); - newDeviceDetail.setDeviceId(deviceId); - newDeviceDetail.setDeviceOwner(ownerWithDevice.getUserName()); - newDeviceDetail.setDeviceStatus(ownerWithDevice.getDeviceStatus()); - newDeviceDetail.setType(ownerWithDevice.getDeviceTypes()); - newDeviceDetail.setDeviceIdentifier(ownerWithDevice.getDeviceIdentifiers()); - newDevices.add(newDeviceDetail); - statusCounts.put("NEW", statusCounts.get("NEW") + 1); - } else if (!unsubscribe && !allSubscriptionForSubscribedMap.containsKey(deviceId) && !deviceSubscriptionMap.containsKey(deviceId) - && (allDevicesMap.containsKey(deviceId))) { - DeviceSubscriptionData newDeviceDetail = new DeviceSubscriptionData(); - newDeviceDetail.setDeviceId(deviceId); - newDeviceDetail.setDeviceName(ownerWithDevice.getDeviceNames()); - newDeviceDetail.setDeviceOwner(ownerWithDevice.getUserName()); - newDeviceDetail.setDeviceStatus(ownerWithDevice.getDeviceStatus()); - newDeviceDetail.setType(ownerWithDevice.getDeviceTypes()); - newDeviceDetail.setDeviceIdentifier(ownerWithDevice.getDeviceIdentifiers()); - newDevices.add(newDeviceDetail); - statusCounts.put("NEW", statusCounts.get("NEW") + 1); - } - } - - int totalDevices = allDevices.size(); - Map statusPercentages = new HashMap<>(); - for (Map.Entry entry : statusCounts.entrySet()) { - double percentage = ((double) entry.getValue() / totalDevices) * 100; - String formattedPercentage = String.format("%.2f", percentage); - statusPercentages.put(entry.getKey(), Double.valueOf(formattedPercentage)); - } - - List requestedDevices = new ArrayList<>(); - if (StringUtils.isNotBlank(request.getTabActionStatus())) { - switch (request.getTabActionStatus()) { - case "COMPLETED": - requestedDevices = installedDevices; - break; - case "PENDING": - requestedDevices = pendingDevices; - break; - case "ERROR": - requestedDevices = errorDevices; - break; - case "NEW": - requestedDevices = newDevices; - break; - case "SUBSCRIBED": - requestedDevices = subscribedDevices; - break; - } - } else { - requestedDevices.addAll(installedDevices); - requestedDevices.addAll(pendingDevices); - requestedDevices.addAll(errorDevices); - requestedDevices.addAll(newDevices); - requestedDevices.addAll(subscribedDevices); - } - - CategorizedSubscriptionResult categorizedSubscriptionResult = - new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices, subscribedDevices); - DeviceSubscriptionResponseDTO deviceSubscriptionResponse = - new DeviceSubscriptionResponseDTO(totalDevices, statusPercentages, categorizedSubscriptionResult); - - return deviceSubscriptionResponse; - - } catch (ApplicationManagementDAOException e) { - String msg = "Error occurred while getting device subscriptions for the application release UUID: " + uuid; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } catch (DBConnectionException e) { - String msg = "DB Connection error occurred while getting device subscriptions for UUID: " + uuid; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } catch (DeviceManagementDAOException e) { - throw new RuntimeException(e); - } finally { - ConnectionManagerUtil.closeDBConnection(); - } - } - - @Override - public DeviceSubscriptionResponseDTO getAllSubscriptionDetailsByUUID(String uuid, String subscriptionStatus, PaginationRequest request, - int offset, int limit) throws ApplicationManagementException { - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - boolean unsubscribe = subscriptionStatus.equals("unsubscribed"); - - try { - ConnectionManagerUtil.openDBConnection(); - - ApplicationReleaseDTO applicationReleaseDTO = this.applicationReleaseDAO.getReleaseByUUID(uuid, tenantId); - if (applicationReleaseDTO == null) { - String msg = "Couldn't find an application release for application release UUID: " + uuid; - log.error(msg); - throw new NotFoundException(msg); - } - ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(uuid, tenantId); - int appReleaseId = applicationReleaseDTO.getId(); - - List allSubscriptions = - subscriptionDAO.getAllSubscriptionsDetails(appReleaseId, unsubscribe, tenantId, request.getActionStatus(), - request.getActionType(), request.getActionTriggeredBy(), offset, limit); - - // empty response for no subscriptions - if (allSubscriptions.isEmpty()) { - return new DeviceSubscriptionResponseDTO(0, Collections.emptyMap(), - new CategorizedSubscriptionResult(Collections.emptyList(), - Collections.emptyList(), Collections.emptyList(), Collections.emptyList())); - } - - DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); - - Map allSubscriptionMap = allSubscriptions.stream() - .collect(Collectors.toMap(DeviceSubscriptionDTO::getDeviceId, Function.identity())); - - List pendingDevices = new ArrayList<>(); - List installedDevices = new ArrayList<>(); - List errorDevices = new ArrayList<>(); - List newDevices = new ArrayList<>(); - - Map statusCounts = new HashMap<>(); - statusCounts.put("PENDING", 0); - statusCounts.put("COMPLETED", 0); - statusCounts.put("ERROR", 0); - statusCounts.put("NEW", 0); - - List allDevices = - deviceManagementProviderService.getDevicesByTenantId(tenantId, applicationDTO.getDeviceTypeId(), request.getOwner(), - request.getDeviceStatus()); - - for (DeviceDetailsDTO device : allDevices) { - Integer deviceId = device.getDeviceId(); - OwnerWithDeviceDTO ownerWithDevice = - deviceManagementProviderService.getOwnerWithDeviceByDeviceId(deviceId, request.getOwner(), request.getDeviceName(), - request.getDeviceStatus()); - if (ownerWithDevice == null || (request.getDeviceName() != null && !request.getDeviceName().isEmpty() && - (ownerWithDevice.getDeviceNames() == null || !ownerWithDevice.getDeviceNames().contains(request.getDeviceName())))) { - continue; - } - if (allSubscriptionMap.containsKey(deviceId)) { - DeviceSubscriptionDTO subscription = allSubscriptionMap.get(deviceId); - DeviceSubscriptionData deviceDetail = new DeviceSubscriptionData(); - deviceDetail.setDeviceId(subscription.getDeviceId()); - deviceDetail.setDeviceName(ownerWithDevice.getDeviceNames()); - deviceDetail.setSubId(subscription.getId()); - deviceDetail.setDeviceOwner(ownerWithDevice.getUserName()); - deviceDetail.setDeviceStatus(ownerWithDevice.getDeviceStatus()); - deviceDetail.setActionType(subscription.getActionTriggeredFrom()); - deviceDetail.setStatus(subscription.getStatus()); - deviceDetail.setActionTriggeredBy(subscription.getSubscribedBy()); - deviceDetail.setActionTriggeredTimestamp(subscription.getSubscribedTimestamp()); - deviceDetail.setUnsubscribed(subscription.isUnsubscribed()); - deviceDetail.setUnsubscribedBy(subscription.getUnsubscribedBy()); - deviceDetail.setUnsubscribedTimestamp(subscription.getUnsubscribedTimestamp()); - deviceDetail.setType(ownerWithDevice.getDeviceTypes()); - deviceDetail.setDeviceIdentifier(ownerWithDevice.getDeviceIdentifiers()); - - String status = subscription.getStatus(); - switch (status) { - case "COMPLETED": - installedDevices.add(deviceDetail); - statusCounts.put("COMPLETED", statusCounts.get("COMPLETED") + 1); - break; - case "ERROR": - case "INVALID": - case "UNAUTHORIZED": - errorDevices.add(deviceDetail); - statusCounts.put("ERROR", statusCounts.get("ERROR") + 1); - break; - case "IN_PROGRESS": - case "PENDING": - case "REPEATED": - pendingDevices.add(deviceDetail); - statusCounts.put("PENDING", statusCounts.get("PENDING") + 1); - break; - } - } else { - DeviceSubscriptionData newDeviceDetail = new DeviceSubscriptionData(); - newDeviceDetail.setDeviceId(deviceId); - newDeviceDetail.setDeviceName(ownerWithDevice.getDeviceNames()); - newDeviceDetail.setDeviceOwner(ownerWithDevice.getUserName()); - newDeviceDetail.setDeviceStatus(ownerWithDevice.getDeviceStatus()); - newDeviceDetail.setType(ownerWithDevice.getDeviceTypes()); - newDeviceDetail.setDeviceIdentifier(ownerWithDevice.getDeviceIdentifiers()); - newDevices.add(newDeviceDetail); - statusCounts.put("NEW", statusCounts.get("NEW") + 1); - } - } - - int totalDevices = allDevices.size(); - Map statusPercentages = new HashMap<>(); - for (Map.Entry entry : statusCounts.entrySet()) { - double percentage = ((double) entry.getValue() / totalDevices) * 100; - String formattedPercentage = String.format("%.2f", percentage); - statusPercentages.put(entry.getKey(), Double.valueOf(formattedPercentage)); - } - - List requestedDevices = new ArrayList<>(); - if (StringUtils.isNotBlank(request.getTabActionStatus())) { - switch (request.getTabActionStatus()) { - case "COMPLETED": - requestedDevices = installedDevices; - break; - case "PENDING": - requestedDevices = pendingDevices; - break; - case "ERROR": - requestedDevices = errorDevices; - break; - case "NEW": - requestedDevices = newDevices; - break; - } - } else { - requestedDevices.addAll(installedDevices); - requestedDevices.addAll(pendingDevices); - requestedDevices.addAll(errorDevices); - requestedDevices.addAll(newDevices); - } - - CategorizedSubscriptionResult categorizedSubscriptionResult = - new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices); - DeviceSubscriptionResponseDTO result = - new DeviceSubscriptionResponseDTO(totalDevices, statusPercentages, categorizedSubscriptionResult); - - return result; - } catch (ApplicationManagementDAOException e) { - String msg = "Error occurred while getting user subscriptions for the application release UUID: " + uuid; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } catch (DBConnectionException e) { - String msg = "DB Connection error occurred while getting user subscriptions for UUID: " + uuid; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } catch (DeviceManagementDAOException e) { - throw new RuntimeException(e); - } finally { - ConnectionManagerUtil.closeDBConnection(); - } + public SubscriptionStatistics getStatistics(SubscriptionInfo subscriptionInfo) throws ApplicationManagementException { + return SubscriptionManagementServiceProvider.getInstance().getSubscriptionManagementHelperService(subscriptionInfo). + getSubscriptionStatistics(subscriptionInfo); } @Override 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 61a6613249..9488181f91 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 @@ -59,6 +59,7 @@ public class SubscriptionManagementHelperUtil { if (idx >= 0) { DeviceSubscriptionDTO deviceSubscriptionDTO = deviceSubscriptionDTOS.get(idx); DeviceSubscription deviceSubscription = new DeviceSubscription(); + deviceSubscription.setDeviceId(device.getId()); deviceSubscription.setDeviceIdentifier(device.getDeviceIdentifier()); deviceSubscription.setDeviceOwner(device.getEnrolmentInfo().getOwner()); deviceSubscription.setDeviceType(device.getType()); @@ -102,6 +103,10 @@ public class SubscriptionManagementHelperUtil { getPercentage(subscriptionStatisticDTO.getPendingDevicesCount(), allDeviceCount)); subscriptionStatistics.setFailedPercentage( getPercentage(subscriptionStatisticDTO.getFailedDevicesCount(), allDeviceCount)); + subscriptionStatistics.setNewDevicesPercentage(getPercentage((allDeviceCount - + subscriptionStatisticDTO.getCompletedDeviceCount() - + subscriptionStatisticDTO.getPendingDevicesCount() - + subscriptionStatisticDTO.getFailedDevicesCount()), allDeviceCount)); return subscriptionStatistics; } @@ -109,6 +114,6 @@ public class SubscriptionManagementHelperUtil { if (denominator <= 0) { return 0.0f; } - return (float) numerator/denominator; + return ((float) numerator / (float) denominator) * 100; } } 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 a58dcad547..584ed8a4ac 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 @@ -20,37 +20,27 @@ package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.impl; import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscription; -import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionData; import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionFilterCriteria; -import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionResponse; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionStatistics; import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationReleaseDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; -import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionsDTO; import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException; import io.entgra.device.mgt.core.application.mgt.common.exception.DBConnectionException; import io.entgra.device.mgt.core.application.mgt.core.exception.ApplicationManagementDAOException; import io.entgra.device.mgt.core.application.mgt.core.exception.NotFoundException; -import io.entgra.device.mgt.core.application.mgt.core.internal.DataHolder; import io.entgra.device.mgt.core.application.mgt.core.util.ConnectionManagerUtil; 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.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.service.DeviceManagementProviderService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; -import org.wso2.carbon.user.api.UserStoreException; -import org.wso2.carbon.user.api.UserStoreManager; -import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Objects; @@ -136,7 +126,8 @@ public class DeviceBasedSubscriptionManagementHelperServiceImpl implements Subsc } @Override - public SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo) throws ApplicationManagementException { + public SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo) + throws ApplicationManagementException { return 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/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 cd4e92d623..deaec45e43 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 @@ -171,7 +171,9 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr } List subscriptionEntities = subscriptionDAO. getGroupsSubscriptionDetailsByAppReleaseID(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, offset, limit); - return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), subscriptionEntities); + int subscriptionCount = isUnsubscribe ? subscriptionDAO.getGroupUnsubscriptionCount(applicationReleaseDTO.getId(), tenantId) : + subscriptionDAO.getGroupSubscriptionCount(applicationReleaseDTO.getId(), tenantId); + return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), subscriptionCount, subscriptionEntities); } catch (DBConnectionException | ApplicationManagementDAOException e) { String msg = "Error encountered while connecting to the database"; log.error(msg, 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 2daad13bf2..17f223e9d3 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 @@ -20,7 +20,6 @@ package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.impl; import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscription; -import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionData; import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionFilterCriteria; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo; @@ -28,6 +27,7 @@ import io.entgra.device.mgt.core.application.mgt.common.SubscriptionResponse; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionStatistics; import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationReleaseDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; +import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionStatisticDTO; import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException; import io.entgra.device.mgt.core.application.mgt.common.exception.DBConnectionException; import io.entgra.device.mgt.core.application.mgt.core.exception.ApplicationManagementDAOException; @@ -66,7 +66,6 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri return RoleBasedSubscriptionManagementHelperServiceImplHolder.INSTANCE; } - @SuppressWarnings("unchecked") @Override public SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { @@ -77,23 +76,7 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri try { ConnectionManagerUtil.openDBConnection(); - UserStoreManager userStoreManager = DataHolder.getInstance().getRealmService(). - getTenantUserRealm(tenantId).getUserStoreManager(); - String[] usersWithRole = - userStoreManager.getUserListOfRole(subscriptionInfo.getIdentifier()); - List deviceListOwnByRole = new ArrayList<>(); - for (String user : usersWithRole) { - PaginationRequest paginationRequest = new PaginationRequest(offset, limit); - 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()); - } - } - - List deviceIdsOwnByRole = deviceListOwnByRole.stream().map(Device::getId).collect(Collectors.toList()); + List deviceIdsOwnByRole = getDeviceIdsOwnByRole(subscriptionInfo.getIdentifier(), tenantId); ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO. getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId); @@ -117,7 +100,8 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); - List newDeviceIds = deviceManagementProviderService.getDevicesNotInGivenIdList(deviceIdsOfSubscription, new PaginationRequest(offset, limit)); + List newDeviceIds = deviceManagementProviderService.getDevicesNotInGivenIdList(deviceIdsOfSubscription, + new PaginationRequest(offset, limit)); deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); deviceCount = deviceManagementProviderService.getDeviceCountNotInGivenIdList(deviceIdsOfSubscription); } else { @@ -169,7 +153,9 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri } List subscriptionEntities = subscriptionDAO. getRoleSubscriptionsByAppReleaseID(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, offset, limit); - return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), subscriptionEntities); + int subscriptionCount = isUnsubscribe ? subscriptionDAO.getRoleUnsubscriptionCount(applicationReleaseDTO.getId(), tenantId) : + subscriptionDAO.getRoleSubscriptionCount(applicationReleaseDTO.getId(), tenantId); + return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), subscriptionCount, subscriptionEntities); } catch (DBConnectionException | ApplicationManagementDAOException e) { String msg = "Error encountered while connecting to the database"; log.error(msg, e); @@ -181,6 +167,40 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri @Override public SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo) throws ApplicationManagementException { - return null; + final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus()); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { + ConnectionManagerUtil.openDBConnection(); + SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO. + getSubscriptionStatistic(subscriptionInfo.getSubscriptionType(), isUnsubscribe, tenantId); + int allDeviceCount = getDeviceIdsOwnByRole(subscriptionInfo.getIdentifier(), tenantId).size(); + return SubscriptionManagementHelperUtil.getSubscriptionStatistics(subscriptionStatisticDTO, allDeviceCount); + } catch (DeviceManagementException | ApplicationManagementDAOException | UserStoreException e) { + String msg = "Error encountered while getting subscription statistics for role: " + subscriptionInfo.getIdentifier(); + 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()); } } 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 26c9134499..8b41019faa 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 @@ -27,6 +27,7 @@ import io.entgra.device.mgt.core.application.mgt.common.SubscriptionResponse; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionStatistics; import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationReleaseDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; +import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionStatisticDTO; import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException; import io.entgra.device.mgt.core.application.mgt.common.exception.DBConnectionException; import io.entgra.device.mgt.core.application.mgt.core.exception.ApplicationManagementDAOException; @@ -61,7 +62,6 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri return UserBasedSubscriptionManagementHelperServiceImpl.UserBasedSubscriptionManagementHelperServiceImplHolder.INSTANCE; } - @SuppressWarnings("unchecked") @Override public SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { @@ -72,17 +72,7 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri try { ConnectionManagerUtil.openDBConnection(); - List deviceListOwnByUser = new ArrayList<>(); - PaginationRequest paginationRequest = new PaginationRequest(offset, limit); - paginationRequest.setOwner(subscriptionInfo.getIdentifier()); - paginationRequest.setStatusList(Arrays.asList("ACTIVE", "INACTIVE", "UNREACHABLE")); - PaginationResult ownDeviceIds = HelperUtil.getDeviceManagementProviderService(). - getAllDevicesIdList(paginationRequest); - if (ownDeviceIds.getData() != null) { - deviceListOwnByUser.addAll((List)ownDeviceIds.getData()); - } - - List deviceIdsOwnByUser = deviceListOwnByUser.stream().map(Device::getId).collect(Collectors.toList()); + List deviceIdsOwnByUser = getDeviceIdsOwnByUser(subscriptionInfo.getIdentifier()); ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO. getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId); @@ -155,7 +145,9 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri } List subscriptionEntities = subscriptionDAO. getUserSubscriptionsByAppReleaseID(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, offset, limit); - return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), subscriptionEntities); + int subscriptionCount = isUnsubscribe ? subscriptionDAO.getUserUnsubscriptionCount(applicationReleaseDTO.getId(), tenantId) : + subscriptionDAO.getUserSubscriptionCount(applicationReleaseDTO.getId(), tenantId); + return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), subscriptionCount, subscriptionEntities); } catch (DBConnectionException | ApplicationManagementDAOException e) { String msg = "Error encountered while connecting to the database"; log.error(msg, e); @@ -167,7 +159,34 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri @Override public SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo) throws ApplicationManagementException { - // todo: analytics engine - return null; + final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus()); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { + ConnectionManagerUtil.openDBConnection(); + SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO. + getSubscriptionStatistic(subscriptionInfo.getSubscriptionType(), isUnsubscribe, tenantId); + int allDeviceCount = getDeviceIdsOwnByUser(subscriptionInfo.getIdentifier()).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); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + @SuppressWarnings("unchecked") + private List getDeviceIdsOwnByUser(String username) throws DeviceManagementException { + List deviceListOwnByUser = new ArrayList<>(); + PaginationRequest paginationRequest = new PaginationRequest(-1, -1); + paginationRequest.setOwner(username); + paginationRequest.setStatusList(Arrays.asList("ACTIVE", "INACTIVE", "UNREACHABLE")); + PaginationResult ownDeviceIds = HelperUtil.getDeviceManagementProviderService(). + getAllDevicesIdList(paginationRequest); + if (ownDeviceIds.getData() != null) { + deviceListOwnByUser.addAll((List)ownDeviceIds.getData()); + } + return deviceListOwnByUser.stream().map(Device::getId).collect(Collectors.toList()); } } 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 5410aa1960..65baf5a1d3 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 @@ -1564,7 +1564,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { Connection connection = GroupManagementDAOFactory.getConnection(); String sql = "SELECT COUNT(d.ID) AS COUNT FROM DM_GROUP d INNER JOIN " + "DM_DEVICE_GROUP_MAP m ON " + - "d.ID = m.GROUP_ID WHERE TENANT_ID = ? AND d.GROUP_NAME = ?"; + "d.ID = m.GROUP_ID WHERE d.TENANT_ID = ? AND d.GROUP_NAME = ?"; try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { preparedStatement.setInt(1, tenantId); preparedStatement.setString(2, groupName);