From 0d19e742bd5c904114e02bf47e49625e162681e9 Mon Sep 17 00:00:00 2001 From: Rajitha Kumara Date: Wed, 17 Jul 2024 12:15:50 +0530 Subject: [PATCH 01/16] Fix role base subscription --- .../mgt/common/dto/DeviceSubscriptionDTO.java | 14 + .../mgt/core/dao/SubscriptionDAO.java | 3 +- .../GenericSubscriptionDAOImpl.java | 101 ++++++- .../core/impl/SubscriptionManagerImpl.java | 246 ++++++++++++++++++ .../core/util/SubscriptionManagementUtil.java | 29 +++ ...bscriptionManagementHelperServiceImpl.java | 152 +++++++++++ .../mgt/bean/DeviceSubscriptionStatus.java | 24 ++ .../mgt/bean/RoleBasedSubscriptionInfo.java | 61 +++++ .../SubscriptionManagementHelperService.java | 39 +++ .../dao/impl/AbstractEnrollmentDAOImpl.java | 97 ++++++- .../DeviceManagementProviderServiceImpl.java | 56 ++-- 11 files changed, 793 insertions(+), 29 deletions(-) create mode 100644 components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/SubscriptionManagementUtil.java create mode 100644 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/RoleBasedSubscriptionManagementHelperServiceImpl.java create mode 100644 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/bean/DeviceSubscriptionStatus.java create mode 100644 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/bean/RoleBasedSubscriptionInfo.java create mode 100644 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/service/SubscriptionManagementHelperService.java 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/dto/DeviceSubscriptionDTO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/DeviceSubscriptionDTO.java index 3306256b19..44885312dc 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/DeviceSubscriptionDTO.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/DeviceSubscriptionDTO.java @@ -19,6 +19,7 @@ package io.entgra.device.mgt.core.application.mgt.common.dto; import java.sql.Timestamp; +import java.util.Objects; public class DeviceSubscriptionDTO { @@ -121,4 +122,17 @@ public class DeviceSubscriptionDTO { public void setAppUuid(String appUuid) { this.appUuid = appUuid; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + DeviceSubscriptionDTO that = (DeviceSubscriptionDTO) o; + return deviceId == that.deviceId; + } + + @Override + public int hashCode() { + return Objects.hash(deviceId); + } } 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/dao/SubscriptionDAO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java index ec3717391a..8308669743 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java @@ -399,7 +399,8 @@ public interface SubscriptionDAO { */ List getSubscriptionDetailsByDeviceIds(int appReleaseId, boolean unsubscribe, int tenantId, List deviceIds, String actionStatus, String actionType, - String actionTriggeredBy, String tabActionStatus) throws ApplicationManagementDAOException; + String actionTriggeredBy, String tabActionStatus, + int limit, int offset) throws ApplicationManagementDAOException; /** * This method is used to get the details of device subscriptions related to a 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/dao/impl/subscription/GenericSubscriptionDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java index 8ffb95f039..c1ab28e092 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java @@ -1913,7 +1913,8 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc @Override public List getSubscriptionDetailsByDeviceIds(int appReleaseId, boolean unsubscribe, int tenantId, List deviceIds, String actionStatus, String actionType, - String actionTriggeredBy, String tabActionStatus) throws ApplicationManagementDAOException { + String actionTriggeredBy, String tabActionStatus, + int offset, int limit) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { log.debug("Getting device subscriptions for the application release id " + appReleaseId + " and device ids " + deviceIds + " from the database"); @@ -1945,7 +1946,8 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc sql.append(" AND DS.SUBSCRIBED_BY LIKE ? "); } - sql.append("ORDER BY ").append(subscriptionStatusTime).append(" DESC"); + sql.append("ORDER BY ").append(subscriptionStatusTime). + append(" DESC ").append("LIMIT ? OFFSET ?"); try (PreparedStatement ps = conn.prepareStatement(sql.toString())) { int paramIdx = 1; @@ -1966,6 +1968,9 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc ps.setString(paramIdx++, "%" + actionTriggeredBy + "%"); } + ps.setInt(paramIdx++, limit); + ps.setInt(paramIdx++, offset); + try (ResultSet rs = ps.executeQuery()) { if (log.isDebugEnabled()) { log.debug("Successfully retrieved device subscriptions for application release id " @@ -2002,6 +2007,98 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } +// @Override +// public List getSubscriptionDetailsByDeviceIds(int appReleaseId, boolean unsubscribe, int tenantId, +// List deviceIds, String actionStatus, String actionType, +// String actionTriggeredBy, String tabActionStatus) throws ApplicationManagementDAOException { +// if (log.isDebugEnabled()) { +// log.debug("Getting device subscriptions for the application release id " + appReleaseId +// + " and device ids " + deviceIds + " from the database"); +// } +// try { +// Connection conn = this.getDBConnection(); +// String subscriptionStatusTime = unsubscribe ? "DS.UNSUBSCRIBED_TIMESTAMP" : "DS.SUBSCRIBED_TIMESTAMP"; +// StringBuilder sql = new StringBuilder("SELECT " +// + "DS.ID AS ID, " +// + "DS.SUBSCRIBED_BY AS SUBSCRIBED_BY, " +// + "DS.SUBSCRIBED_TIMESTAMP AS SUBSCRIBED_AT, " +// + "DS.UNSUBSCRIBED AS IS_UNSUBSCRIBED, " +// + "DS.UNSUBSCRIBED_BY AS UNSUBSCRIBED_BY, " +// + "DS.UNSUBSCRIBED_TIMESTAMP AS UNSUBSCRIBED_AT, " +// + "DS.ACTION_TRIGGERED_FROM AS ACTION_TRIGGERED_FROM, " +// + "DS.STATUS AS STATUS, " +// + "DS.DM_DEVICE_ID AS DEVICE_ID " +// + "FROM AP_DEVICE_SUBSCRIPTION DS " +// + "WHERE DS.AP_APP_RELEASE_ID = ? AND DS.UNSUBSCRIBED = ? AND DS.TENANT_ID = ? AND DS.DM_DEVICE_ID IN (" + +// deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ") "); +// +// if (actionStatus != null && !actionStatus.isEmpty()) { +// sql.append(" AND DS.STATUS = ? "); +// } +// if (actionType != null && !actionType.isEmpty()) { +// sql.append(" AND DS.ACTION_TRIGGERED_FROM = ? "); +// } +// if (actionTriggeredBy != null && !actionTriggeredBy.isEmpty()) { +// sql.append(" AND DS.SUBSCRIBED_BY LIKE ? "); +// } +// +// sql.append("ORDER BY ").append(subscriptionStatusTime).append(" DESC"); +// +// try (PreparedStatement ps = conn.prepareStatement(sql.toString())) { +// int paramIdx = 1; +// ps.setInt(paramIdx++, appReleaseId); +// ps.setBoolean(paramIdx++, unsubscribe); +// ps.setInt(paramIdx++, tenantId); +// for (int i = 0; i < deviceIds.size(); i++) { +// ps.setInt(paramIdx++, deviceIds.get(i)); +// } +// +// if (actionStatus != null && !actionStatus.isEmpty()) { +// ps.setString(paramIdx++, actionStatus); +// } +// if (actionType != null && !actionType.isEmpty()) { +// ps.setString(paramIdx++, actionType); +// } +// if (actionTriggeredBy != null && !actionTriggeredBy.isEmpty()) { +// ps.setString(paramIdx++, "%" + actionTriggeredBy + "%"); +// } +// +// try (ResultSet rs = ps.executeQuery()) { +// if (log.isDebugEnabled()) { +// log.debug("Successfully retrieved device subscriptions for application release id " +// + appReleaseId + " and device ids " + deviceIds); +// } +// List subscriptions = new ArrayList<>(); +// while (rs.next()) { +// DeviceSubscriptionDTO subscription = new DeviceSubscriptionDTO(); +// subscription.setId(rs.getInt("ID")); +// subscription.setSubscribedBy(rs.getString("SUBSCRIBED_BY")); +// subscription.setSubscribedTimestamp(rs.getTimestamp("SUBSCRIBED_AT")); +// subscription.setUnsubscribed(rs.getBoolean("IS_UNSUBSCRIBED")); +// subscription.setUnsubscribedBy(rs.getString("UNSUBSCRIBED_BY")); +// subscription.setUnsubscribedTimestamp(rs.getTimestamp("UNSUBSCRIBED_AT")); +// subscription.setActionTriggeredFrom(rs.getString("ACTION_TRIGGERED_FROM")); +// subscription.setStatus(rs.getString("STATUS")); +// subscription.setDeviceId(rs.getInt("DEVICE_ID")); +// subscriptions.add(subscription); +// } +// return subscriptions; +// } +// } catch (SQLException e) { +// String msg = "Error occurred while running SQL to get device subscription data for application ID: " + appReleaseId +// + " and device ids: " + deviceIds + "."; +// log.error(msg, e); +// throw new ApplicationManagementDAOException(msg, e); +// } +// } catch (DBConnectionException e) { +// String msg = "Error occurred while obtaining the DB connection for getting device subscriptions for " +// + "application Id: " + appReleaseId + " and device ids: " + deviceIds + "."; +// log.error(msg, e); +// throw new ApplicationManagementDAOException(msg, e); +// } +// +// } + @Override public List getAllSubscriptionsDetails(int appReleaseId, boolean unsubscribe, int tenantId, String actionStatus, String actionType, String actionTriggeredBy, 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 065c9576bd..64995450cc 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 @@ -2153,6 +2153,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } } + @Override public List getRoleSubscriptionsByUUID(String uuid, String subscriptionStatus, PaginationRequest request, int offset, int limit) @@ -2209,9 +2210,12 @@ public class SubscriptionManagerImpl implements SubscriptionManager { 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(), @@ -2221,6 +2225,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } List deviceIds = ownerDetailsWithDevices.getDeviceIds(); + // now for each device id for (Integer deviceId : deviceIds) { List subscribedDeviceSubscriptions = new ArrayList<>(); @@ -2229,12 +2234,18 @@ public class SubscriptionManagerImpl implements SubscriptionManager { 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( @@ -2388,6 +2399,241 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } } +// @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(); 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/SubscriptionManagementUtil.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/SubscriptionManagementUtil.java new file mode 100644 index 0000000000..9c45f57be4 --- /dev/null +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/SubscriptionManagementUtil.java @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package io.entgra.device.mgt.core.application.mgt.core.util; + +public class SubscriptionManagementUtil { + public static final class DeviceSubscriptionStatus { + public static final String COMPLETED = "COMPLETED"; + public static final String ERROR ="ERROR"; + public static final String NEW = "NEW"; + public static final String SUBSCRIBED = "SUBSCRIBED"; + } +} 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/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/RoleBasedSubscriptionManagementHelperServiceImpl.java new file mode 100644 index 0000000000..854a46fef5 --- /dev/null +++ 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/RoleBasedSubscriptionManagementHelperServiceImpl.java @@ -0,0 +1,152 @@ +/* + * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt; + +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.bean.DeviceSubscriptionStatus; +import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.bean.RoleBasedSubscriptionInfo; +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 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.List; +import java.util.Objects; + +public class RoleBasedSubscriptionManagementHelperServiceImpl implements SubscriptionManagementHelperService { + private static final Log log = LogFactory.getLog(RoleBasedSubscriptionManagementHelperServiceImpl.class); + private final RoleBasedSubscriptionInfo roleBasedSubscriptionInfo; + private final boolean isUnsubscribe; + public RoleBasedSubscriptionManagementHelperServiceImpl(RoleBasedSubscriptionInfo roleBasedSubscriptionInfo) { + this.roleBasedSubscriptionInfo = roleBasedSubscriptionInfo; + this.isUnsubscribe = "unsubscribed".equals(roleBasedSubscriptionInfo.getSubscriptionStatus()); + } + + @SuppressWarnings("unchecked") + @Override + public List getStatusBaseSubscriptions(int limit, int offset) throws ApplicationManagementException { + List deviceSubscriptionDTOS; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { + UserStoreManager userStoreManager = DataHolder.getInstance().getRealmService(). + getTenantUserRealm(tenantId).getUserStoreManager(); + String[] usersWithRole = + userStoreManager.getUserListOfRole(roleBasedSubscriptionInfo.getRoleName()); + 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 deviceIds = (List) deviceListOwnByRole.stream().map(Device::getId); + + ConnectionManagerUtil.openDBConnection(); + ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO. + getReleaseByUUID(roleBasedSubscriptionInfo.getApplicationUUID(), tenantId); + if (applicationReleaseDTO == null) { + String msg = "Couldn't find an application release for application release UUID: " + + roleBasedSubscriptionInfo.getApplicationUUID(); + log.error(msg); + throw new NotFoundException(msg); + } + + if (Objects.equals(DeviceSubscriptionStatus.NEW.toString(), + roleBasedSubscriptionInfo.getDeviceSubscriptionStatus().toString())) { + + deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), + isUnsubscribe, tenantId, deviceIds, null, RoleBasedSubscriptionInfo.TRIGGERED_FROM_VALUE, + null, null, limit, offset); + for (Integer deviceId : deviceIds) { + DeviceSubscriptionDTO deviceSubscriptionDTO = new DeviceSubscriptionDTO(); + deviceSubscriptionDTO.setDeviceId(deviceId); + deviceSubscriptionDTOS.remove(deviceSubscriptionDTO); + } + return deviceSubscriptionDTOS; + } + + deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), + isUnsubscribe, tenantId, deviceIds, roleBasedSubscriptionInfo.getDeviceSubscriptionStatus(). + toString(), RoleBasedSubscriptionInfo.TRIGGERED_FROM_VALUE, + null, null, limit, offset); + + } catch (UserStoreException e) { + String msg = "Error encountered while getting the user management store for tenant id " + tenantId; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (DeviceManagementException e) { + String msg = "Error encountered while getting device details"; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (ApplicationManagementDAOException | DBConnectionException e) { + String msg = "Error encountered while connecting to the database"; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } + return deviceSubscriptionDTOS; + } + + @Override + public List getSubscriptions(int limit, int offset) throws ApplicationManagementException { + final int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { + ConnectionManagerUtil.openDBConnection(); + ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO. + getReleaseByUUID(roleBasedSubscriptionInfo.getApplicationUUID(), tenantId); + if (applicationReleaseDTO == null) { + String msg = "Couldn't find an application release for application release UUID: " + + roleBasedSubscriptionInfo.getApplicationUUID(); + log.error(msg); + throw new NotFoundException(msg); + } + return subscriptionDAO. + getRoleSubscriptionsByAppReleaseID(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, offset, limit); + } catch (DBConnectionException | ApplicationManagementDAOException e) { + throw new RuntimeException(e); + } + } + + @Override + public void getSubscriptionStatistics() throws ApplicationManagementException { + + } +} 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/bean/DeviceSubscriptionStatus.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/bean/DeviceSubscriptionStatus.java new file mode 100644 index 0000000000..11cbb62386 --- /dev/null +++ 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/bean/DeviceSubscriptionStatus.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.bean; + +public enum DeviceSubscriptionStatus { + COMPLETED, ERROR, NEW, SUBSCRIBED +} 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/bean/RoleBasedSubscriptionInfo.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/bean/RoleBasedSubscriptionInfo.java new file mode 100644 index 0000000000..1e7385d173 --- /dev/null +++ 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/bean/RoleBasedSubscriptionInfo.java @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.bean; + +public class RoleBasedSubscriptionInfo { + + private String applicationUUID; + private String roleName; + private String subscriptionStatus; + public static String TRIGGERED_FROM_VALUE = "role"; + private DeviceSubscriptionStatus deviceSubscriptionStatus; + + public String getApplicationUUID() { + return applicationUUID; + } + + public void setApplicationUUID(String applicationUUID) { + this.applicationUUID = applicationUUID; + } + + public String getRoleName() { + return roleName; + } + + public void setRoleName(String roleName) { + this.roleName = roleName; + } + + public String getSubscriptionStatus() { + return subscriptionStatus; + } + + public void setSubscriptionStatus(String subscriptionStatus) { + this.subscriptionStatus = subscriptionStatus; + } + + public DeviceSubscriptionStatus getDeviceSubscriptionStatus() { + return deviceSubscriptionStatus; + } + + public void setDeviceSubscriptionStatus(DeviceSubscriptionStatus deviceSubscriptionStatus) { + this.deviceSubscriptionStatus = deviceSubscriptionStatus; + } +} 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/service/SubscriptionManagementHelperService.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/service/SubscriptionManagementHelperService.java new file mode 100644 index 0000000000..256ed05b46 --- /dev/null +++ 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/service/SubscriptionManagementHelperService.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.service; + +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.core.dao.ApplicationDAO; +import io.entgra.device.mgt.core.application.mgt.core.dao.ApplicationReleaseDAO; +import io.entgra.device.mgt.core.application.mgt.core.dao.SubscriptionDAO; +import io.entgra.device.mgt.core.application.mgt.core.dao.common.ApplicationManagementDAOFactory; + +import java.util.List; + +public interface SubscriptionManagementHelperService { + SubscriptionDAO subscriptionDAO = ApplicationManagementDAOFactory.getSubscriptionDAO(); + ApplicationDAO applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO(); + ApplicationReleaseDAO applicationReleaseDAO = ApplicationManagementDAOFactory.getApplicationReleaseDAO(); + List getStatusBaseSubscriptions(int limit, int offset) throws ApplicationManagementException; + List getSubscriptions(int limit, int offset) throws ApplicationManagementException; + void getSubscriptionStatistics() throws ApplicationManagementException; +} 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/AbstractEnrollmentDAOImpl.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/AbstractEnrollmentDAOImpl.java index 1160b4bb79..70456d3be6 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/AbstractEnrollmentDAOImpl.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/AbstractEnrollmentDAOImpl.java @@ -587,9 +587,9 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { "d.NAME AS DEVICE_NAME, " + "e.DEVICE_TYPE AS DEVICE_TYPE, " + "e.DEVICE_IDENTIFICATION AS DEVICE_IDENTIFICATION " + - "FROM DM_ENROLMENT e " + - "JOIN DM_DEVICE d ON e.DEVICE_ID = d.ID " + - "WHERE e.OWNER = ? AND e.TENANT_ID = ? AND d.DEVICE_TYPE_ID = ? AND e.STATUS IN (" + deviceFilters + ")"); + "FROM DM_ENROLMENT e " + + "JOIN DM_DEVICE d ON e.DEVICE_ID = d.ID " + + "WHERE e.OWNER = ? AND e.TENANT_ID = ? AND d.DEVICE_TYPE_ID = ? AND e.STATUS IN (" + deviceFilters + ")"); if (deviceOwner != null && !deviceOwner.isEmpty()) { sql.append(" AND e.OWNER LIKE ?"); @@ -624,13 +624,6 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { try (ResultSet rs = stmt.executeQuery()) { while (rs.next()) { - if (ownerDetails.getUserName() == null) { - ownerDetails.setUserName(rs.getString("OWNER")); - } - ownerDetails.setDeviceStatus(rs.getString("DEVICE_STATUS")); - ownerDetails.setDeviceNames(rs.getString("DEVICE_NAME")); - ownerDetails.setDeviceTypes(rs.getString("DEVICE_TYPE")); - ownerDetails.setDeviceIdentifiers(rs.getString("DEVICE_IDENTIFICATION")); deviceIds.add(rs.getInt("DEVICE_ID")); deviceCount++; } @@ -641,11 +634,95 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { log.error(msg, e); throw new DeviceManagementDAOException(msg, e); } + ownerDetails.setUserName(deviceOwner); ownerDetails.setDeviceIds(deviceIds); ownerDetails.setDeviceCount(deviceCount); return ownerDetails; } +// @Override +// public OwnerWithDeviceDTO getOwnersWithDevices(String owner, List allowingDeviceStatuses, int tenantId, +// int deviceTypeId, String deviceOwner, String deviceName, +// String deviceStatus) throws DeviceManagementDAOException { +// Connection conn = null; +// OwnerWithDeviceDTO ownerDetails = new OwnerWithDeviceDTO(); +// List deviceIds = new ArrayList<>(); +// int deviceCount = 0; +// +// StringBuilder deviceFilters = new StringBuilder(); +// for (int i = 0; i < allowingDeviceStatuses.size(); i++) { +// deviceFilters.append("?"); +// if (i < allowingDeviceStatuses.size() - 1) { +// deviceFilters.append(","); +// } +// } +// +// StringBuilder sql = new StringBuilder( +// "SELECT e.DEVICE_ID, " + +// "e.OWNER, " + +// "e.STATUS AS DEVICE_STATUS, " + +// "d.NAME AS DEVICE_NAME, " + +// "e.DEVICE_TYPE AS DEVICE_TYPE, " + +// "e.DEVICE_IDENTIFICATION AS DEVICE_IDENTIFICATION " + +// "FROM DM_ENROLMENT e " + +// "JOIN DM_DEVICE d ON e.DEVICE_ID = d.ID " + +// "WHERE e.OWNER = ? AND e.TENANT_ID = ? AND d.DEVICE_TYPE_ID = ? AND e.STATUS IN (" + deviceFilters + ")"); +// +// if (deviceOwner != null && !deviceOwner.isEmpty()) { +// sql.append(" AND e.OWNER LIKE ?"); +// } +// if (deviceName != null && !deviceName.isEmpty()) { +// sql.append(" AND d.NAME LIKE ?"); +// } +// if (deviceStatus != null && !deviceStatus.isEmpty()) { +// sql.append(" AND e.STATUS = ?"); +// } +// +// try { +// conn = this.getConnection(); +// try (PreparedStatement stmt = conn.prepareStatement(sql.toString())) { +// int index = 1; +// stmt.setString(index++, owner); +// stmt.setInt(index++, tenantId); +// stmt.setInt(index++, deviceTypeId); +// for (String status : allowingDeviceStatuses) { +// stmt.setString(index++, status); +// } +// +// if (deviceOwner != null && !deviceOwner.isEmpty()) { +// stmt.setString(index++, "%" + deviceOwner + "%"); +// } +// if (deviceName != null && !deviceName.isEmpty()) { +// stmt.setString(index++, "%" + deviceName + "%"); +// } +// if (deviceStatus != null && !deviceStatus.isEmpty()) { +// stmt.setString(index++, deviceStatus); +// } +// +// try (ResultSet rs = stmt.executeQuery()) { +// while (rs.next()) { +// if (ownerDetails.getUserName() == null) { +// ownerDetails.setUserName(rs.getString("OWNER")); +// } +// ownerDetails.setDeviceStatus(rs.getString("DEVICE_STATUS")); +// ownerDetails.setDeviceNames(rs.getString("DEVICE_NAME")); +// ownerDetails.setDeviceTypes(rs.getString("DEVICE_TYPE")); +// ownerDetails.setDeviceIdentifiers(rs.getString("DEVICE_IDENTIFICATION")); +// deviceIds.add(rs.getInt("DEVICE_ID")); +// deviceCount++; +// } +// } +// } +// } catch (SQLException e) { +// String msg = "Error occurred while retrieving owners and device IDs for owner: " + owner; +// log.error(msg, e); +// throw new DeviceManagementDAOException(msg, e); +// } +// ownerDetails.setDeviceIds(deviceIds); +// ownerDetails.setDeviceCount(deviceCount); +// return ownerDetails; +// } + @Override public OwnerWithDeviceDTO getOwnerWithDeviceByDeviceId(int deviceId, int tenantId, String deviceOwner, String deviceName, String deviceStatus) throws DeviceManagementDAOException { diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index b07f915e43..3192cefcd0 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -161,6 +161,7 @@ import javax.xml.bind.Marshaller; import java.io.IOException; import java.io.StringWriter; import java.lang.reflect.Type; +import java.sql.Array; import java.sql.SQLException; import java.sql.Timestamp; import java.time.LocalDateTime; @@ -5359,25 +5360,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); OwnerWithDeviceDTO ownerWithDeviceDTO; - List allowingDeviceStatuses = new ArrayList<>(); - allowingDeviceStatuses.add(EnrolmentInfo.Status.ACTIVE.toString()); - allowingDeviceStatuses.add(EnrolmentInfo.Status.INACTIVE.toString()); - allowingDeviceStatuses.add(EnrolmentInfo.Status.UNREACHABLE.toString()); + List allowingDeviceStatuses = Arrays.asList(EnrolmentInfo.Status.ACTIVE.toString(), + EnrolmentInfo.Status.INACTIVE.toString(), EnrolmentInfo.Status.UNREACHABLE.toString()); try { DeviceManagementDAOFactory.openConnection(); - ownerWithDeviceDTO = this.enrollmentDAO.getOwnersWithDevices(owner, allowingDeviceStatuses, tenantId, deviceTypeId, deviceOwner, deviceName, deviceStatus); - if (ownerWithDeviceDTO == null) { - String msg = "No data found for owner: " + owner; - log.error(msg); - throw new DeviceManagementDAOException(msg); - } - List deviceIds = ownerWithDeviceDTO.getDeviceIds(); - if (deviceIds != null) { - ownerWithDeviceDTO.setDeviceCount(deviceIds.size()); - } else { - ownerWithDeviceDTO.setDeviceCount(0); - } + ownerWithDeviceDTO = this.enrollmentDAO.getOwnersWithDevices(owner, allowingDeviceStatuses, + tenantId, deviceTypeId, deviceOwner, deviceName, deviceStatus); } catch (DeviceManagementDAOException | SQLException e) { String msg = "Error occurred while retrieving device IDs for owner: " + owner; log.error(msg, e); @@ -5388,6 +5377,41 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return ownerWithDeviceDTO; } +// @Override +// public OwnerWithDeviceDTO getOwnersWithDeviceIds(String owner, int deviceTypeId, String deviceOwner, String deviceName, String deviceStatus) +// throws DeviceManagementDAOException { +// int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); +// OwnerWithDeviceDTO ownerWithDeviceDTO; +// +// List allowingDeviceStatuses = new ArrayList<>(); +// allowingDeviceStatuses.add(EnrolmentInfo.Status.ACTIVE.toString()); +// allowingDeviceStatuses.add(EnrolmentInfo.Status.INACTIVE.toString()); +// allowingDeviceStatuses.add(EnrolmentInfo.Status.UNREACHABLE.toString()); +// +// try { +// DeviceManagementDAOFactory.openConnection(); +// ownerWithDeviceDTO = this.enrollmentDAO.getOwnersWithDevices(owner, allowingDeviceStatuses, tenantId, deviceTypeId, deviceOwner, deviceName, deviceStatus); +// if (ownerWithDeviceDTO == null) { +// String msg = "No data found for owner: " + owner; +// log.error(msg); +// throw new DeviceManagementDAOException(msg); +// } +// List deviceIds = ownerWithDeviceDTO.getDeviceIds(); +// if (deviceIds != null) { +// ownerWithDeviceDTO.setDeviceCount(deviceIds.size()); +// } else { +// ownerWithDeviceDTO.setDeviceCount(0); +// } +// } catch (DeviceManagementDAOException | SQLException e) { +// String msg = "Error occurred while retrieving device IDs for owner: " + owner; +// log.error(msg, e); +// throw new DeviceManagementDAOException(msg, e); +// } finally { +// DeviceManagementDAOFactory.closeConnection(); +// } +// return ownerWithDeviceDTO; +// } + @Override public OwnerWithDeviceDTO getOwnerWithDeviceByDeviceId(int deviceId, String deviceOwner, String deviceName, String deviceStatus) -- 2.36.3 From 4fc14edf1e4c7ca949364956b73ca28bcb2416de Mon Sep 17 00:00:00 2001 From: Rajitha Kumara Date: Thu, 18 Jul 2024 02:26:39 +0530 Subject: [PATCH 02/16] Add subscription helper services --- .../DeviceSubscriptionFilterCriteria.java | 68 + .../mgt/common/SubscriptionEntity.java | 88 ++ .../mgt/common/SubscriptionInfo.java | 77 + .../mgt/common/dto/DeviceSubscriptionDTO.java | 8 + .../common/services/SubscriptionManager.java | 13 +- .../mgt/core/dao/SubscriptionDAO.java | 5 +- .../GenericSubscriptionDAOImpl.java | 59 +- .../core/impl/SubscriptionManagerImpl.java | 1407 +++++++++-------- .../mgt/SubscriptionManagementHelperUtil.java | 48 + ...SubscriptionManagementServiceProvider.java | 49 + ...bscriptionManagementHelperServiceImpl.java | 47 + ...bscriptionManagementHelperServiceImpl.java | 157 ++ ...bscriptionManagementHelperServiceImpl.java | 81 +- ...bscriptionManagementHelperServiceImpl.java | 47 + .../SubscriptionManagementHelperService.java | 9 +- .../mgt/core/device/mgt/common/Device.java | 8 +- .../dao/impl/device/GenericDeviceDAOImpl.java | 1 + 17 files changed, 1418 insertions(+), 754 deletions(-) create mode 100644 components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/DeviceSubscriptionFilterCriteria.java create mode 100644 components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionEntity.java create mode 100644 components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionInfo.java create mode 100644 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 create mode 100644 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/SubscriptionManagementServiceProvider.java create mode 100644 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 create mode 100644 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 rename 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 (62%) create mode 100644 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 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/DeviceSubscriptionFilterCriteria.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/DeviceSubscriptionFilterCriteria.java new file mode 100644 index 0000000000..298a5221eb --- /dev/null +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/DeviceSubscriptionFilterCriteria.java @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package io.entgra.device.mgt.core.application.mgt.common; + +public class DeviceSubscriptionFilterCriteria { + private String name; + private String owner; + private String deviceStatus; + private String filteringDeviceSubscriptionStatus; // COMPLETE, PENDING ... + private String triggeredBy; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getOwner() { + return owner; + } + + public void setOwner(String owner) { + this.owner = owner; + } + + public String getDeviceStatus() { + return deviceStatus; + } + + public void setDeviceStatus(String deviceStatus) { + this.deviceStatus = deviceStatus; + } + + public String getFilteringDeviceSubscriptionStatus() { + return filteringDeviceSubscriptionStatus; + } + + public void setFilteringDeviceSubscriptionStatus(String filteringDeviceSubscriptionStatus) { + this.filteringDeviceSubscriptionStatus = filteringDeviceSubscriptionStatus; + } + + public String getTriggeredBy() { + return triggeredBy; + } + + public void setTriggeredBy(String triggeredBy) { + this.triggeredBy = triggeredBy; + } +} 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/SubscriptionEntity.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionEntity.java new file mode 100644 index 0000000000..40c1b2f4a7 --- /dev/null +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionEntity.java @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package io.entgra.device.mgt.core.application.mgt.common; + +import java.sql.Timestamp; + +public class SubscriptionEntity { + private String identity; + private String subscribedBy; + private Timestamp subscribedTimestamp; + private boolean unsubscribed; + private String unsubscribedBy; + private Timestamp unsubscribedTimestamp; + private int applicationReleaseId; + + public String getIdentity() { + return identity; + } + + public void setIdentity(String identity) { + this.identity = identity; + } + + public String getSubscribedBy() { + return subscribedBy; + } + + public void setSubscribedBy(String subscribedBy) { + this.subscribedBy = subscribedBy; + } + + public Timestamp getSubscribedTimestamp() { + return subscribedTimestamp; + } + + public void setSubscribedTimestamp(Timestamp subscribedTimestamp) { + this.subscribedTimestamp = subscribedTimestamp; + } + + public boolean getUnsubscribed() { + return unsubscribed; + } + + public void setUnsubscribed(boolean unsubscribed) { + this.unsubscribed = unsubscribed; + } + + public String getUnsubscribedBy() { + return unsubscribedBy; + } + + public void setUnsubscribedBy(String unsubscribedBy) { + this.unsubscribedBy = unsubscribedBy; + } + + public Timestamp getUnsubscribedTimestamp() { + return unsubscribedTimestamp; + } + + public void setUnsubscribedTimestamp(Timestamp unsubscribedTimestamp) { + this.unsubscribedTimestamp = unsubscribedTimestamp; + } + + public int getApplicationReleaseId() { + return applicationReleaseId; + } + + public void setApplicationReleaseId(int applicationReleaseId) { + this.applicationReleaseId = applicationReleaseId; + } +} 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/SubscriptionInfo.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionInfo.java new file mode 100644 index 0000000000..6af03aa61a --- /dev/null +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionInfo.java @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package io.entgra.device.mgt.core.application.mgt.common; + +public class SubscriptionInfo { + private String applicationUUID; + private String subscriptionType; + private String deviceSubscriptionStatus; + private String identifier; + private String subscriptionStatus; + private DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria; + + public String getApplicationUUID() { + return applicationUUID; + } + + public void setApplicationUUID(String applicationUUID) { + this.applicationUUID = applicationUUID; + } + + public String getSubscriptionType() { + return subscriptionType; + } + + public void setSubscriptionType(String subscriptionType) { + this.subscriptionType = subscriptionType; + } + + public String getDeviceSubscriptionStatus() { + return deviceSubscriptionStatus; + } + + public void setDeviceSubscriptionStatus(String deviceSubscriptionStatus) { + this.deviceSubscriptionStatus = deviceSubscriptionStatus; + } + + public String getIdentifier() { + return identifier; + } + + public void setIdentifier(String identifier) { + this.identifier = identifier; + } + + public String getSubscriptionStatus() { + return subscriptionStatus; + } + + public void setSubscriptionStatus(String subscriptionStatus) { + this.subscriptionStatus = subscriptionStatus; + } + + public DeviceSubscriptionFilterCriteria getDeviceSubscriptionFilterCriteria() { + return deviceSubscriptionFilterCriteria; + } + + public void setDeviceSubscriptionFilterCriteria(DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria) { + this.deviceSubscriptionFilterCriteria = deviceSubscriptionFilterCriteria; + } +} 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/dto/DeviceSubscriptionDTO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/DeviceSubscriptionDTO.java index 44885312dc..a475a75cd0 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/DeviceSubscriptionDTO.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/DeviceSubscriptionDTO.java @@ -35,6 +35,14 @@ public class DeviceSubscriptionDTO { private int appReleaseId; private String appUuid; + public DeviceSubscriptionDTO() { + + } + public DeviceSubscriptionDTO(int deviceId, String status) { + this.deviceId = deviceId; + this.status = status; + } + public int getId() { return id; } 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 c8ca40813a..7803c10ccb 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 @@ -19,9 +19,13 @@ package io.entgra.device.mgt.core.application.mgt.common.services; import io.entgra.device.mgt.core.application.mgt.common.ApplicationInstallResponse; import io.entgra.device.mgt.core.application.mgt.common.CategorizedSubscriptionResult; +import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionData; 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.SubscriptionType; 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.ScheduledSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionsDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceOperationDTO; @@ -260,8 +264,13 @@ public interface SubscriptionManager { * @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; +// List getRoleSubscriptionsByUUID(String uuid, String subscriptionStatus, PaginationRequest request, +// int offset, int limit) throws ApplicationManagementException; + List getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + throws ApplicationManagementException; + + List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + throws ApplicationManagementException; /** * Retrieves the Device Subscription details associated with a given app release 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/dao/SubscriptionDAO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java index 8308669743..c77267d26c 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java @@ -19,6 +19,7 @@ package io.entgra.device.mgt.core.application.mgt.core.dao; import io.entgra.device.mgt.core.application.mgt.common.ExecutionStatus; import io.entgra.device.mgt.core.application.mgt.common.dto.GroupSubscriptionDTO; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity; import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionsDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationReleaseDTO; @@ -327,7 +328,7 @@ public interface SubscriptionDAO { * @return {@link GroupSubscriptionDTO} which contains the details of group subscriptions. * @throws ApplicationManagementDAOException if connection establishment fails. */ - List getGroupsSubscriptionDetailsByAppReleaseID(int appReleaseId, boolean unsubscribe, int tenantId, int offset, int limit) + List getGroupsSubscriptionDetailsByAppReleaseID(int appReleaseId, boolean unsubscribe, int tenantId, int offset, int limit) throws ApplicationManagementDAOException; /** @@ -355,7 +356,7 @@ public interface SubscriptionDAO { * @return {@link SubscriptionsDTO} which contains the details of subscriptions. * @throws ApplicationManagementDAOException if connection establishment or SQL execution fails. */ - List getRoleSubscriptionsByAppReleaseID(int appReleaseId, boolean unsubscribe, int tenantId, int offset, int limit) + List getRoleSubscriptionsByAppReleaseID(int appReleaseId, boolean unsubscribe, int tenantId, int offset, int limit) throws ApplicationManagementDAOException; /** 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/dao/impl/subscription/GenericSubscriptionDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java index c1ab28e092..45d4cff175 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java @@ -19,6 +19,7 @@ package io.entgra.device.mgt.core.application.mgt.core.dao.impl.subscription; import io.entgra.device.mgt.core.application.mgt.common.dto.GroupSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceOperationDTO; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity; import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionsDTO; import io.entgra.device.mgt.core.application.mgt.core.dao.SubscriptionDAO; import io.entgra.device.mgt.core.application.mgt.core.dao.impl.AbstractDAOImpl; @@ -1641,14 +1642,14 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } @Override - public List getGroupsSubscriptionDetailsByAppReleaseID(int appReleaseId, boolean unsubscribe, int tenantId, int offset, int limit) + public List getGroupsSubscriptionDetailsByAppReleaseID(int appReleaseId, boolean unsubscribe, int tenantId, int offset, int limit) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { log.debug("Request received in DAO Layer to get groups related to the given AppReleaseID."); } try { Connection conn = this.getDBConnection(); - List groupDetails = new ArrayList<>(); + List subscriptionEntities = new ArrayList<>(); String subscriptionStatusTime = unsubscribe ? "GS.UNSUBSCRIBED_TIMESTAMP" : "GS.SUBSCRIBED_TIMESTAMP"; String sql = "SELECT GS.GROUP_NAME, GS.SUBSCRIBED_BY, GS.SUBSCRIBED_TIMESTAMP, GS.UNSUBSCRIBED, " + @@ -1665,21 +1666,21 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc ps.setInt(5, offset); try (ResultSet rs = ps.executeQuery()) { - GroupSubscriptionDTO groupDetail; + SubscriptionEntity subscriptionEntity; while (rs.next()) { - groupDetail = new GroupSubscriptionDTO(); - groupDetail.setGroupName(rs.getString("GROUP_NAME")); - groupDetail.setSubscribedBy(rs.getString("SUBSCRIBED_BY")); - groupDetail.setSubscribedTimestamp(rs.getTimestamp("SUBSCRIBED_TIMESTAMP")); - groupDetail.setUnsubscribed(rs.getBoolean("UNSUBSCRIBED")); - groupDetail.setUnsubscribedBy(rs.getString("UNSUBSCRIBED_BY")); - groupDetail.setUnsubscribedTimestamp(rs.getTimestamp("UNSUBSCRIBED_TIMESTAMP")); - groupDetail.setAppReleaseId(rs.getInt("AP_APP_RELEASE_ID")); - - groupDetails.add(groupDetail); + subscriptionEntity = new SubscriptionEntity(); + subscriptionEntity.setIdentity(rs.getString("GROUP_NAME")); + subscriptionEntity.setSubscribedBy(rs.getString("SUBSCRIBED_BY")); + subscriptionEntity.setSubscribedTimestamp(rs.getTimestamp("SUBSCRIBED_TIMESTAMP")); + subscriptionEntity.setUnsubscribed(rs.getBoolean("UNSUBSCRIBED")); + subscriptionEntity.setUnsubscribedBy(rs.getString("UNSUBSCRIBED_BY")); + subscriptionEntity.setUnsubscribedTimestamp(rs.getTimestamp("UNSUBSCRIBED_TIMESTAMP")); + subscriptionEntity.setApplicationReleaseId(rs.getInt("AP_APP_RELEASE_ID")); + + subscriptionEntities.add(subscriptionEntity); } } - return groupDetails; + return subscriptionEntities; } } catch (DBConnectionException e) { String msg = "Error occurred while obtaining the DB connection to get groups for the given UUID."; @@ -1744,14 +1745,14 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } @Override - public List getRoleSubscriptionsByAppReleaseID(int appReleaseId, boolean unsubscribe, int tenantId, int offset, - int limit) throws ApplicationManagementDAOException { + public List getRoleSubscriptionsByAppReleaseID(int appReleaseId, boolean unsubscribe, int tenantId, int offset, + int limit) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { log.debug("Request received in DAO Layer to get role subscriptions related to the given AppReleaseID."); } try { Connection conn = this.getDBConnection(); - List roleSubscriptions = new ArrayList<>(); + List subscriptionEntities = new ArrayList<>(); String subscriptionStatusTime = unsubscribe ? "ARS.UNSUBSCRIBED_TIMESTAMP" : "ARS.SUBSCRIBED_TIMESTAMP"; String sql = "SELECT ARS.ROLE_NAME, ARS.SUBSCRIBED_BY, ARS.SUBSCRIBED_TIMESTAMP, ARS.UNSUBSCRIBED, " + @@ -1767,21 +1768,21 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc ps.setInt(4, limit); ps.setInt(5, offset); try (ResultSet rs = ps.executeQuery()) { - SubscriptionsDTO roleSubscription; + SubscriptionEntity subscriptionEntity; while (rs.next()) { - roleSubscription = new SubscriptionsDTO(); - roleSubscription.setName(rs.getString("ROLE_NAME")); - roleSubscription.setSubscribedBy(rs.getString("SUBSCRIBED_BY")); - roleSubscription.setSubscribedTimestamp(rs.getTimestamp("SUBSCRIBED_TIMESTAMP")); - roleSubscription.setUnsubscribed(rs.getBoolean("UNSUBSCRIBED")); - roleSubscription.setUnsubscribedBy(rs.getString("UNSUBSCRIBED_BY")); - roleSubscription.setUnsubscribedTimestamp(rs.getTimestamp("UNSUBSCRIBED_TIMESTAMP")); - roleSubscription.setAppReleaseId(rs.getInt("AP_APP_RELEASE_ID")); - - roleSubscriptions.add(roleSubscription); + subscriptionEntity = new SubscriptionEntity(); + subscriptionEntity.setIdentity(rs.getString("ROLE_NAME")); + subscriptionEntity.setSubscribedBy(rs.getString("SUBSCRIBED_BY")); + subscriptionEntity.setSubscribedTimestamp(rs.getTimestamp("SUBSCRIBED_TIMESTAMP")); + subscriptionEntity.setUnsubscribed(rs.getBoolean("UNSUBSCRIBED")); + subscriptionEntity.setUnsubscribedBy(rs.getString("UNSUBSCRIBED_BY")); + subscriptionEntity.setUnsubscribedTimestamp(rs.getTimestamp("UNSUBSCRIBED_TIMESTAMP")); + subscriptionEntity.setApplicationReleaseId(rs.getInt("AP_APP_RELEASE_ID")); + + subscriptionEntities.add(subscriptionEntity); } } - return roleSubscriptions; + return subscriptionEntities; } } catch (DBConnectionException e) { String msg = "Error occurred while obtaining the DB connection to get role subscriptions for 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 64995450cc..0d01be1c1b 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 @@ -24,6 +24,8 @@ import io.entgra.device.mgt.core.application.mgt.common.ApplicationSubscriptionI 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.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.dto.CategorizedSubscriptionCountsDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionsDTO; @@ -45,6 +47,8 @@ import io.entgra.device.mgt.core.application.mgt.common.services.VPPApplicationM import io.entgra.device.mgt.core.application.mgt.core.dao.ApplicationReleaseDAO; import io.entgra.device.mgt.core.application.mgt.core.dao.VppApplicationDAO; import io.entgra.device.mgt.core.application.mgt.core.exception.BadRequestException; +import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.SubscriptionManagementServiceProvider; +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; @@ -1707,698 +1711,727 @@ public class SubscriptionManagerImpl implements SubscriptionManager { 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(); - } + return null; } @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(); - } + PaginationRequest request, int offset, int limit) throws ApplicationManagementException { + return null; } - @Override - public List getRoleSubscriptionsByUUID(String uuid, String subscriptionStatus, - PaginationRequest request, int offset, int limit) + public List getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) 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); - } + SubscriptionManagementHelperService subscriptionManagementHelperService = + SubscriptionManagementServiceProvider.getInstance().getSubscriptionManagementHelperService(subscriptionInfo); + return subscriptionManagementHelperService.getSubscriptions(subscriptionInfo, limit, offset); + } - 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 getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + throws ApplicationManagementException { + SubscriptionManagementHelperService subscriptionManagementHelperService = + SubscriptionManagementServiceProvider.getInstance().getSubscriptionManagementHelperService(subscriptionInfo); + 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) @@ -2699,12 +2732,12 @@ public class SubscriptionManagerImpl implements SubscriptionManager { Map allDevicesMap = allDevices.stream() .collect(Collectors.toMap(DeviceDetailsDTO::getDeviceId, Function.identity())); - List allSubscriptionsForUnSubscribed = - subscriptionDAO.getSubscriptionDetailsByDeviceIds(appReleaseId, !unsubscribe, tenantId, deviceIds, request.getActionStatus(), - request.getActionType(), request.getActionTriggeredBy(), request.getTabActionStatus()); - List allSubscriptionsForSubscribed = - subscriptionDAO.getSubscriptionDetailsByDeviceIds(appReleaseId, unsubscribe, tenantId, deviceIds, request.getActionStatus(), - request.getActionType(), request.getActionTriggeredBy(), request.getTabActionStatus()); + 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() 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 new file mode 100644 index 0000000000..3a369060f1 --- /dev/null +++ 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 @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt; + +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.SubscriptionInfo; +import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; + +import java.util.List; + +public class SubscriptionManagementHelperUtil { + public static List getDeviceSubscriptionData(List deviceSubscriptionDTOS, + DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria) { + // todo: filtering + for (DeviceSubscriptionDTO deviceSubscriptionDTO : deviceSubscriptionDTOS) { + + } + return null; + } + + public static String getDeviceSubscriptionStatus(SubscriptionInfo subscriptionInfo) { + return getDeviceSubscriptionStatus(subscriptionInfo.getDeviceSubscriptionFilterCriteria(). + getFilteringDeviceSubscriptionStatus(), subscriptionInfo.getDeviceSubscriptionStatus()); + } + + public static String getDeviceSubscriptionStatus(String deviceSubscriptionStatusFilter, String deviceSubscriptionStatus) { + return (deviceSubscriptionStatusFilter != null && !deviceSubscriptionStatusFilter.isEmpty()) ? + deviceSubscriptionStatusFilter : deviceSubscriptionStatus; + } +} 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/SubscriptionManagementServiceProvider.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/SubscriptionManagementServiceProvider.java new file mode 100644 index 0000000000..671f4fdeba --- /dev/null +++ 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/SubscriptionManagementServiceProvider.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt; + +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo; +import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.impl.GroupBasedSubscriptionManagementHelperServiceImpl; +import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.impl.RoleBasedSubscriptionManagementHelperServiceImpl; +import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.service.SubscriptionManagementHelperService; + +import java.util.Objects; + +public class SubscriptionManagementServiceProvider { + private SubscriptionManagementServiceProvider() {} + + private static class SubscriptionManagementProviderServiceHolder { + private static final SubscriptionManagementServiceProvider INSTANCE = new SubscriptionManagementServiceProvider(); + } + + public static SubscriptionManagementServiceProvider getInstance() { + return SubscriptionManagementProviderServiceHolder.INSTANCE; + } + + public SubscriptionManagementHelperService getSubscriptionManagementHelperService(SubscriptionInfo subscriptionInfo) { + return getSubscriptionManagementHelperService(subscriptionInfo.getSubscriptionType()); + } + + private SubscriptionManagementHelperService getSubscriptionManagementHelperService(String subscriptionType) { + if (Objects.equals(subscriptionType, "role")) return RoleBasedSubscriptionManagementHelperServiceImpl.getInstance(); + if (Objects.equals(subscriptionType, "group")) return GroupBasedSubscriptionManagementHelperServiceImpl.getInstance(); + throw new UnsupportedOperationException("Subscription type: " + subscriptionType + " not supports"); + } +} 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 new file mode 100644 index 0000000000..a130f78d7a --- /dev/null +++ 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 @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.impl; + +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.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.core.util.subscription.mgt.service.SubscriptionManagementHelperService; + +import java.util.List; + +public class DeviceBasedSubscriptionManagementHelperServiceImpl implements SubscriptionManagementHelperService { + @Override + public List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { + return null; + } + + @Override + public List getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { + return null; + } + + @Override + public void getSubscriptionStatistics() throws ApplicationManagementException { + + } +} 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 new file mode 100644 index 0000000000..d59cd455f2 --- /dev/null +++ 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 @@ -0,0 +1,157 @@ +/* + * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.impl; + +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.dto.ApplicationDTO; +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.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.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.group.mgt.GroupManagementException; +import io.entgra.device.mgt.core.device.mgt.core.dto.GroupDetailsDTO; +import io.entgra.device.mgt.core.device.mgt.core.service.GroupManagementProviderService; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.PrivilegedCarbonContext; + +import java.util.List; +import java.util.Objects; + +public class GroupBasedSubscriptionManagementHelperServiceImpl implements SubscriptionManagementHelperService { + private static final Log log = LogFactory.getLog(GroupBasedSubscriptionManagementHelperServiceImpl.class); + private GroupBasedSubscriptionManagementHelperServiceImpl() {} + private static class GroupBasedSubscriptionManagementHelperServiceImplHolder { + private static final GroupBasedSubscriptionManagementHelperServiceImpl INSTANCE + = new GroupBasedSubscriptionManagementHelperServiceImpl(); + } + + public static GroupBasedSubscriptionManagementHelperServiceImpl getInstance() { + return GroupBasedSubscriptionManagementHelperServiceImplHolder.INSTANCE; + } + + @Override + public List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + throws ApplicationManagementException { + + final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus()); + List deviceSubscriptionDTOS; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + + try { + ConnectionManagerUtil.openDBConnection(); + ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO. + getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId); + + if (applicationReleaseDTO == null) { + String msg = "Couldn't find an application release for application release UUID: " + + subscriptionInfo.getApplicationUUID(); + log.error(msg); + throw new NotFoundException(msg); + } + + ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(subscriptionInfo.getApplicationUUID(), tenantId); + if (applicationDTO == null) { + String msg = "Application not found for the release UUID: " + subscriptionInfo.getApplicationUUID(); + log.error(msg); + throw new NotFoundException(msg); + } + + String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); + DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); + + GroupManagementProviderService groupManagementProviderService = HelperUtil.getGroupManagementProviderService(); + GroupDetailsDTO groupDetailsDTO = groupManagementProviderService.getGroupDetailsWithDevices(subscriptionInfo.getIdentifier(), + applicationDTO.getDeviceTypeId(), deviceSubscriptionFilterCriteria.getOwner(), deviceSubscriptionFilterCriteria.getName(), + deviceSubscriptionFilterCriteria.getDeviceStatus(), offset, limit); + + List deviceIdsOwnByGroup = groupDetailsDTO.getDeviceIds(); + + if (Objects.equals("NEW", deviceSubscriptionStatus)) { + + deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), + isUnsubscribe, tenantId, deviceIdsOwnByGroup, null, + subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), + null, limit, offset); + for (DeviceSubscriptionDTO deviceSubscriptionDTO: deviceSubscriptionDTOS) { + deviceIdsOwnByGroup.remove(deviceSubscriptionDTO.getDeviceId()); + } + } + + deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), + isUnsubscribe, tenantId, deviceIdsOwnByGroup, deviceSubscriptionStatus, subscriptionInfo.getSubscriptionType(), + deviceSubscriptionFilterCriteria.getTriggeredBy(), deviceSubscriptionStatus, limit, offset); + + } catch (GroupManagementException e) { + String msg = "Error encountered while retrieving group details for group: " + subscriptionInfo.getIdentifier(); + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (ApplicationManagementDAOException | DBConnectionException e) { + String msg = "Error encountered while connecting to the database"; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + + return SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, + subscriptionInfo.getDeviceSubscriptionFilterCriteria()); + } + + @Override + public List getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + throws ApplicationManagementException { + final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus()); + final int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { + ConnectionManagerUtil.openDBConnection(); + ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO. + getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId); + if (applicationReleaseDTO == null) { + String msg = "Couldn't find an application release for application release UUID: " + + subscriptionInfo.getApplicationUUID(); + log.error(msg); + throw new NotFoundException(msg); + } + return subscriptionDAO. + getGroupsSubscriptionDetailsByAppReleaseID(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, offset, limit); + } catch (DBConnectionException | ApplicationManagementDAOException e) { + String msg = "Error encountered while connecting to the database"; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + @Override + public void getSubscriptionStatistics() throws ApplicationManagementException { + + } +} 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/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 similarity index 62% rename from 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/RoleBasedSubscriptionManagementHelperServiceImpl.java rename to 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 854a46fef5..0bcbc6628a 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/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 @@ -17,11 +17,14 @@ * */ -package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt; +package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.impl; +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.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; @@ -29,8 +32,7 @@ import io.entgra.device.mgt.core.application.mgt.core.exception.NotFoundExceptio 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.bean.DeviceSubscriptionStatus; -import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.bean.RoleBasedSubscriptionInfo; +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; @@ -46,26 +48,33 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Objects; +import java.util.stream.Collectors; public class RoleBasedSubscriptionManagementHelperServiceImpl implements SubscriptionManagementHelperService { private static final Log log = LogFactory.getLog(RoleBasedSubscriptionManagementHelperServiceImpl.class); - private final RoleBasedSubscriptionInfo roleBasedSubscriptionInfo; - private final boolean isUnsubscribe; - public RoleBasedSubscriptionManagementHelperServiceImpl(RoleBasedSubscriptionInfo roleBasedSubscriptionInfo) { - this.roleBasedSubscriptionInfo = roleBasedSubscriptionInfo; - this.isUnsubscribe = "unsubscribed".equals(roleBasedSubscriptionInfo.getSubscriptionStatus()); + private RoleBasedSubscriptionManagementHelperServiceImpl() {} + private static class RoleBasedSubscriptionManagementHelperServiceImplHolder { + private static final RoleBasedSubscriptionManagementHelperServiceImpl INSTANCE + = new RoleBasedSubscriptionManagementHelperServiceImpl(); + } + + public static RoleBasedSubscriptionManagementHelperServiceImpl getInstance() { + return RoleBasedSubscriptionManagementHelperServiceImplHolder.INSTANCE; } @SuppressWarnings("unchecked") @Override - public List getStatusBaseSubscriptions(int limit, int offset) throws ApplicationManagementException { + public List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + throws ApplicationManagementException { + final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus()); List deviceSubscriptionDTOS; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { UserStoreManager userStoreManager = DataHolder.getInstance().getRealmService(). getTenantUserRealm(tenantId).getUserStoreManager(); String[] usersWithRole = - userStoreManager.getUserListOfRole(roleBasedSubscriptionInfo.getRoleName()); + userStoreManager.getUserListOfRole(subscriptionInfo.getIdentifier()); List deviceListOwnByRole = new ArrayList<>(); for (String user : usersWithRole) { PaginationRequest paginationRequest = new PaginationRequest(offset, limit); @@ -78,36 +87,37 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri } } - List deviceIds = (List) deviceListOwnByRole.stream().map(Device::getId); + List deviceIdsOwnByRole = deviceListOwnByRole.stream().map(Device::getId).collect(Collectors.toList()); ConnectionManagerUtil.openDBConnection(); ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO. - getReleaseByUUID(roleBasedSubscriptionInfo.getApplicationUUID(), tenantId); + getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId); if (applicationReleaseDTO == null) { String msg = "Couldn't find an application release for application release UUID: " + - roleBasedSubscriptionInfo.getApplicationUUID(); + subscriptionInfo.getApplicationUUID(); log.error(msg); throw new NotFoundException(msg); } - if (Objects.equals(DeviceSubscriptionStatus.NEW.toString(), - roleBasedSubscriptionInfo.getDeviceSubscriptionStatus().toString())) { + String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); + DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); + + if (Objects.equals("NEW", deviceSubscriptionStatus)) { deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIds, null, RoleBasedSubscriptionInfo.TRIGGERED_FROM_VALUE, - null, null, limit, offset); - for (Integer deviceId : deviceIds) { - DeviceSubscriptionDTO deviceSubscriptionDTO = new DeviceSubscriptionDTO(); - deviceSubscriptionDTO.setDeviceId(deviceId); - deviceSubscriptionDTOS.remove(deviceSubscriptionDTO); + isUnsubscribe, tenantId, deviceIdsOwnByRole, null, + subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), + null, limit, offset); + for (DeviceSubscriptionDTO deviceSubscriptionDTO: deviceSubscriptionDTOS) { + deviceListOwnByRole.remove(new Device(deviceSubscriptionDTO.getId())); } - return deviceSubscriptionDTOS; + deviceIdsOwnByRole = deviceListOwnByRole.stream().map(Device::getId).collect(Collectors.toList()); + } deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIds, roleBasedSubscriptionInfo.getDeviceSubscriptionStatus(). - toString(), RoleBasedSubscriptionInfo.TRIGGERED_FROM_VALUE, - null, null, limit, offset); + isUnsubscribe, tenantId, deviceIdsOwnByRole, deviceSubscriptionStatus, subscriptionInfo.getSubscriptionType(), + deviceSubscriptionFilterCriteria.getTriggeredBy(), deviceSubscriptionStatus, limit, offset); } catch (UserStoreException e) { String msg = "Error encountered while getting the user management store for tenant id " + tenantId; @@ -121,27 +131,36 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri String msg = "Error encountered while connecting to the database"; log.error(msg, e); throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); } - return deviceSubscriptionDTOS; + return SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, + subscriptionInfo.getDeviceSubscriptionFilterCriteria()); } @Override - public List getSubscriptions(int limit, int offset) throws ApplicationManagementException { + public List getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + throws ApplicationManagementException { + final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus()); final int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { ConnectionManagerUtil.openDBConnection(); ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO. - getReleaseByUUID(roleBasedSubscriptionInfo.getApplicationUUID(), tenantId); + getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId); if (applicationReleaseDTO == null) { String msg = "Couldn't find an application release for application release UUID: " + - roleBasedSubscriptionInfo.getApplicationUUID(); + subscriptionInfo.getApplicationUUID(); log.error(msg); throw new NotFoundException(msg); } return subscriptionDAO. getRoleSubscriptionsByAppReleaseID(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, offset, limit); } catch (DBConnectionException | ApplicationManagementDAOException e) { - throw new RuntimeException(e); + String msg = "Error encountered while connecting to the database"; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); } } 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 new file mode 100644 index 0000000000..939b72101a --- /dev/null +++ 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 @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.impl; + +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.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.core.util.subscription.mgt.service.SubscriptionManagementHelperService; + +import java.util.List; + +public class UserBasedSubscriptionManagementHelperServiceImpl implements SubscriptionManagementHelperService { + @Override + public List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { + return null; + } + + @Override + public List getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { + return null; + } + + @Override + public void getSubscriptionStatistics() throws ApplicationManagementException { + + } +} 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/service/SubscriptionManagementHelperService.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/service/SubscriptionManagementHelperService.java index 256ed05b46..e205b0a58e 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/service/SubscriptionManagementHelperService.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/service/SubscriptionManagementHelperService.java @@ -19,6 +19,9 @@ package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.service; +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.dto.DeviceSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionsDTO; import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException; @@ -33,7 +36,9 @@ public interface SubscriptionManagementHelperService { SubscriptionDAO subscriptionDAO = ApplicationManagementDAOFactory.getSubscriptionDAO(); ApplicationDAO applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO(); ApplicationReleaseDAO applicationReleaseDAO = ApplicationManagementDAOFactory.getApplicationReleaseDAO(); - List getStatusBaseSubscriptions(int limit, int offset) throws ApplicationManagementException; - List getSubscriptions(int limit, int offset) throws ApplicationManagementException; + List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + throws ApplicationManagementException; + List getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + throws ApplicationManagementException; void getSubscriptionStatistics() throws ApplicationManagementException; } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/Device.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/Device.java index b75f557077..2819364327 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/Device.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/Device.java @@ -97,6 +97,10 @@ public class Device implements Serializable { public Device() { } + public Device(int id) { + this.id = id; + } + public Device(String name, String type, String description, String deviceId, EnrolmentInfo enrolmentInfo, List features, List properties) { this.name = name; @@ -268,7 +272,9 @@ public class Device implements Serializable { Device device = (Device) o; - return getDeviceIdentifier().equals(device.getDeviceIdentifier()); + if (getDeviceIdentifier().equals(device.getDeviceIdentifier())) return true; + + return getId() == device.getId(); } 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/device/GenericDeviceDAOImpl.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/device/GenericDeviceDAOImpl.java index e2063761db..bf539199c9 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/device/GenericDeviceDAOImpl.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/device/GenericDeviceDAOImpl.java @@ -457,6 +457,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { "e.ID AS ENROLMENT_ID " + "FROM DM_ENROLMENT e, " + "(SELECT d.ID, " + + "d.LAST_UPDATED_TIMESTAMP, " + "d.DEVICE_IDENTIFICATION " + "FROM DM_DEVICE d WHERE d.TENANT_ID = ?) d1 " + "WHERE d1.ID = e.DEVICE_ID AND e.TENANT_ID = ? "; -- 2.36.3 From 5f0e18f1bf774c15d799822bdf4f8b9106353fc8 Mon Sep 17 00:00:00 2001 From: Rajitha Kumara Date: Thu, 18 Jul 2024 21:55:16 +0530 Subject: [PATCH 03/16] Add device filtering capabilities --- .../mgt/common/DeviceSubscription.java | 77 ++++++ .../mgt/common/SubscriptionData.java | 61 +++++ .../mgt/common/dto/DeviceSubscriptionDTO.java | 5 + .../common/services/SubscriptionManager.java | 3 +- .../core/impl/SubscriptionManagerImpl.java | 3 +- .../mgt/SubscriptionManagementHelperUtil.java | 41 +++- ...bscriptionManagementHelperServiceImpl.java | 3 +- ...bscriptionManagementHelperServiceImpl.java | 12 +- ...bscriptionManagementHelperServiceImpl.java | 30 +-- ...bscriptionManagementHelperServiceImpl.java | 228 +++++++++++++++++- .../SubscriptionManagementHelperService.java | 3 +- .../core/device/mgt/core/dao/DeviceDAO.java | 6 + .../core/dao/impl/AbstractDeviceDAOImpl.java | 94 ++++++++ .../DeviceManagementProviderService.java | 6 + .../DeviceManagementProviderServiceImpl.java | 53 ++++ 15 files changed, 596 insertions(+), 29 deletions(-) create mode 100644 components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/DeviceSubscription.java create mode 100644 components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionData.java 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 new file mode 100644 index 0000000000..98c76c192c --- /dev/null +++ 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 @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package io.entgra.device.mgt.core.application.mgt.common; + +public class DeviceSubscription { + private String deviceIdentifier; + private String deviceStatus; + private String deviceOwner; + private String deviceType; + private String ownershipType; + private SubscriptionData subscriptionData; + + public String getDeviceIdentifier() { + return deviceIdentifier; + } + + public void setDeviceIdentifier(String deviceIdentifier) { + this.deviceIdentifier = deviceIdentifier; + } + + public String getDeviceStatus() { + return deviceStatus; + } + + public void setDeviceStatus(String deviceStatus) { + this.deviceStatus = deviceStatus; + } + + public String getDeviceOwner() { + return deviceOwner; + } + + public void setDeviceOwner(String deviceOwner) { + this.deviceOwner = deviceOwner; + } + + public String getDeviceType() { + return deviceType; + } + + public void setDeviceType(String deviceType) { + this.deviceType = deviceType; + } + + public String getOwnershipType() { + return ownershipType; + } + + public void setOwnershipType(String ownershipType) { + this.ownershipType = ownershipType; + } + + public SubscriptionData getSubscriptionData() { + return subscriptionData; + } + + public void setSubscriptionData(SubscriptionData subscriptionData) { + this.subscriptionData = subscriptionData; + } +} diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionData.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionData.java new file mode 100644 index 0000000000..16dad4467d --- /dev/null +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionData.java @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package io.entgra.device.mgt.core.application.mgt.common; + +import java.sql.Timestamp; + +public class SubscriptionData { + private String deviceSubscriptionStatus; + private String triggeredBy; + private String subscriptionType; + private Timestamp triggeredAt; + + public String getDeviceSubscriptionStatus() { + return deviceSubscriptionStatus; + } + + public void setDeviceSubscriptionStatus(String deviceSubscriptionStatus) { + this.deviceSubscriptionStatus = deviceSubscriptionStatus; + } + + public String getTriggeredBy() { + return triggeredBy; + } + + public void setTriggeredBy(String triggeredBy) { + this.triggeredBy = triggeredBy; + } + + public String getSubscriptionType() { + return subscriptionType; + } + + public void setSubscriptionType(String subscriptionType) { + this.subscriptionType = subscriptionType; + } + + public Timestamp getTriggeredAt() { + return triggeredAt; + } + + public void setTriggeredAt(Timestamp triggeredAt) { + this.triggeredAt = triggeredAt; + } +} 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/dto/DeviceSubscriptionDTO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/DeviceSubscriptionDTO.java index a475a75cd0..aa4f221ca3 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/DeviceSubscriptionDTO.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/DeviceSubscriptionDTO.java @@ -38,6 +38,11 @@ public class DeviceSubscriptionDTO { public DeviceSubscriptionDTO() { } + + public DeviceSubscriptionDTO(int deviceId) { + this.deviceId = deviceId; + } + public DeviceSubscriptionDTO(int deviceId, String status) { this.deviceId = deviceId; this.status = status; 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 7803c10ccb..7f73cd1d0e 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 @@ -19,6 +19,7 @@ package io.entgra.device.mgt.core.application.mgt.common.services; import io.entgra.device.mgt.core.application.mgt.common.ApplicationInstallResponse; 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.ExecutionStatus; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity; @@ -269,7 +270,7 @@ public interface SubscriptionManager { List getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException; - List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException; /** 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 0d01be1c1b..bb6c93d889 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,6 +23,7 @@ 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; @@ -1729,7 +1730,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } @Override - public List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + public List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { SubscriptionManagementHelperService subscriptionManagementHelperService = SubscriptionManagementServiceProvider.getInstance().getSubscriptionManagementHelperService(subscriptionInfo); diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/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 3a369060f1..93a5d6f066 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 @@ -19,21 +19,52 @@ package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt; +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.SubscriptionData; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; +import io.entgra.device.mgt.core.application.mgt.core.util.HelperUtil; +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.exceptions.DeviceManagementException; +import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; public class SubscriptionManagementHelperUtil { - public static List getDeviceSubscriptionData(List deviceSubscriptionDTOS, - DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria) { - // todo: filtering - for (DeviceSubscriptionDTO deviceSubscriptionDTO : deviceSubscriptionDTOS) { + public static List getDeviceSubscriptionData(List deviceSubscriptionDTOS, + DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria) throws DeviceManagementException { + List deviceIds = deviceSubscriptionDTOS.stream().map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); + PaginationRequest paginationRequest = new PaginationRequest(0, -1); + paginationRequest.setDeviceName(deviceSubscriptionFilterCriteria.getName()); + paginationRequest.setDeviceStatus(deviceSubscriptionFilterCriteria.getDeviceStatus()); + paginationRequest.setOwner(deviceSubscriptionFilterCriteria.getOwner()); + List devices = HelperUtil.getDeviceManagementProviderService().getDevicesByDeviceIds(paginationRequest, deviceIds); + return populateDeviceData(deviceSubscriptionDTOS, devices); + } + private static List populateDeviceData(List deviceSubscriptionDTOS, List devices) { + List deviceSubscriptions = new ArrayList<>(); + for (Device device : devices) { + int idx = deviceSubscriptionDTOS.indexOf(new DeviceSubscriptionDTO(device.getId())); + if (idx >= 0) { + DeviceSubscriptionDTO deviceSubscriptionDTO = deviceSubscriptionDTOS.get(idx); + DeviceSubscription deviceSubscription = new DeviceSubscription(); + deviceSubscription.setDeviceIdentifier(device.getDeviceIdentifier()); + deviceSubscription.setDeviceOwner(device.getEnrolmentInfo().getOwner()); + deviceSubscription.setDeviceType(device.getType()); + SubscriptionData subscriptionData = new SubscriptionData(); + subscriptionData.setTriggeredBy(deviceSubscriptionDTO.getActionTriggeredFrom()); + subscriptionData.setTriggeredAt(deviceSubscriptionDTO.getSubscribedTimestamp()); + subscriptionData.setSubscriptionType(deviceSubscriptionDTO.getStatus()); + deviceSubscription.setSubscriptionData(subscriptionData); + deviceSubscriptions.add(deviceSubscription); + } } - return null; + return deviceSubscriptions; } public static String getDeviceSubscriptionStatus(SubscriptionInfo subscriptionInfo) { diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/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 a130f78d7a..fad338b06f 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 @@ -19,6 +19,7 @@ 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.SubscriptionEntity; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo; @@ -31,7 +32,7 @@ import java.util.List; public class DeviceBasedSubscriptionManagementHelperServiceImpl implements SubscriptionManagementHelperService { @Override - public List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { + public List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) 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 d59cd455f2..6454fa8807 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 @@ -19,6 +19,7 @@ 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; @@ -34,6 +35,7 @@ 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.exceptions.DeviceManagementException; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupManagementException; import io.entgra.device.mgt.core.device.mgt.core.dto.GroupDetailsDTO; import io.entgra.device.mgt.core.device.mgt.core.service.GroupManagementProviderService; @@ -57,13 +59,13 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr } @Override - public List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + public List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus()); List deviceSubscriptionDTOS; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - + // todo: check and refactor try { ConnectionManagerUtil.openDBConnection(); ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO. @@ -107,6 +109,8 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, deviceIdsOwnByGroup, deviceSubscriptionStatus, subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), deviceSubscriptionStatus, limit, offset); + return SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, + subscriptionInfo.getDeviceSubscriptionFilterCriteria()); } catch (GroupManagementException e) { String msg = "Error encountered while retrieving group details for group: " + subscriptionInfo.getIdentifier(); @@ -116,12 +120,12 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr String msg = "Error encountered while connecting to the database"; log.error(msg, e); throw new ApplicationManagementException(msg, e); + } catch (DeviceManagementException e) { + throw new RuntimeException(e); } finally { ConnectionManagerUtil.closeDBConnection(); } - return SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria()); } @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/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 0bcbc6628a..5bf4ffcb20 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 @@ -19,6 +19,7 @@ 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; @@ -64,13 +65,14 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri @SuppressWarnings("unchecked") @Override - public List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + public List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus()); List deviceSubscriptionDTOS; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { + ConnectionManagerUtil.openDBConnection(); UserStoreManager userStoreManager = DataHolder.getInstance().getRealmService(). getTenantUserRealm(tenantId).getUserStoreManager(); String[] usersWithRole = @@ -89,7 +91,6 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri List deviceIdsOwnByRole = deviceListOwnByRole.stream().map(Device::getId).collect(Collectors.toList()); - ConnectionManagerUtil.openDBConnection(); ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO. getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId); if (applicationReleaseDTO == null) { @@ -102,22 +103,23 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); + deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), + isUnsubscribe, tenantId, deviceIdsOwnByRole, null, + subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), + null, limit, offset); + if (Objects.equals("NEW", deviceSubscriptionStatus)) { - deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIdsOwnByRole, null, - subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), - null, limit, offset); - for (DeviceSubscriptionDTO deviceSubscriptionDTO: deviceSubscriptionDTOS) { - deviceListOwnByRole.remove(new Device(deviceSubscriptionDTO.getId())); - } - deviceIdsOwnByRole = deviceListOwnByRole.stream().map(Device::getId).collect(Collectors.toList()); + List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). + map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); + List newDeviceIds = HelperUtil.getDeviceManagementProviderService(). + getDevicesNotInGivenIdList(deviceIdsOfSubscription, new PaginationRequest(offset, limit)); + deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); } - deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIdsOwnByRole, deviceSubscriptionStatus, subscriptionInfo.getSubscriptionType(), - deviceSubscriptionFilterCriteria.getTriggeredBy(), deviceSubscriptionStatus, limit, offset); + return SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, + subscriptionInfo.getDeviceSubscriptionFilterCriteria()); } catch (UserStoreException e) { String msg = "Error encountered while getting the user management store for tenant id " + tenantId; @@ -134,8 +136,6 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri } finally { ConnectionManagerUtil.closeDBConnection(); } - return SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria()); } @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/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 939b72101a..0180b9953d 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 @@ -19,6 +19,7 @@ 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.SubscriptionEntity; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo; @@ -29,9 +30,234 @@ import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.serv import java.util.List; +/* +// @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(); +// } +// } + */ public class UserBasedSubscriptionManagementHelperServiceImpl implements SubscriptionManagementHelperService { @Override - public List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { + public List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + 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/service/SubscriptionManagementHelperService.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/service/SubscriptionManagementHelperService.java index e205b0a58e..8b234f0256 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/service/SubscriptionManagementHelperService.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/service/SubscriptionManagementHelperService.java @@ -19,6 +19,7 @@ package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.service; +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; @@ -36,7 +37,7 @@ public interface SubscriptionManagementHelperService { SubscriptionDAO subscriptionDAO = ApplicationManagementDAOFactory.getSubscriptionDAO(); ApplicationDAO applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO(); ApplicationReleaseDAO applicationReleaseDAO = ApplicationManagementDAOFactory.getApplicationReleaseDAO(); - List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException; List getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/DeviceDAO.java index b71b6cd640..3bf7e2d620 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/DeviceDAO.java @@ -864,4 +864,10 @@ public interface DeviceDAO { * @throws DeviceManagementDAOException */ int getCountOfDevicesNotInGroup(PaginationRequest request, int tenantId) throws DeviceManagementDAOException; + + List getDevicesNotInGivenIdList(PaginationRequest request, List deviceIds, int tenantId) + throws DeviceManagementDAOException; + + List getDevicesByDeviceIds(PaginationRequest paginationRequest, List deviceIds, int tenantId) + throws DeviceManagementDAOException; } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index a04da1dd27..2034bcf33c 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -54,6 +54,7 @@ import java.util.List; import java.util.Map; import java.util.StringJoiner; import java.util.Random; +import java.util.stream.Collectors; public abstract class AbstractDeviceDAOImpl implements DeviceDAO { @@ -3298,4 +3299,97 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { throw new DeviceManagementDAOException(msg, e); } } + + @Override + public List getDevicesNotInGivenIdList(PaginationRequest request, List deviceIds, int tenantId) + throws DeviceManagementDAOException { + List filteredDeviceIds = new ArrayList<>(); + String deviceIdStringList = deviceIds.stream().map(String::valueOf).collect(Collectors.joining(",")); + try { + Connection connection = getConnection(); + String sql = "SELECT ID AS DEVICE_ID FROM DM_DEVICE WHERE ID NOT IN " + + "(" + deviceIdStringList + ") LIMIT ? OFFSET ?"; + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setInt(1, request.getRowCount()); + preparedStatement.setInt(2, request.getStartIndex()); + try (ResultSet resultSet = preparedStatement.executeQuery()) { + while (resultSet.next()) { + filteredDeviceIds.add(resultSet.getInt("DEVICE_ID")); + } + } + return filteredDeviceIds; + } + } catch (SQLException e) { + String msg = "Error occurred while retrieving device ids not in: " + filteredDeviceIds; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + } + + @Override + public List getDevicesByDeviceIds(PaginationRequest paginationRequest, List deviceIds, int tenantId) + throws DeviceManagementDAOException { + List devices = new ArrayList<>(); + String deviceIdStringList = deviceIds.stream().map(String::valueOf).collect(Collectors.joining(",")); + boolean isOwnerProvided = false; + boolean isDeviceStatusProvided = false; + boolean isDeviceNameProvided = false; + try { + Connection connection = getConnection(); + String sql = "SELECT e.DEVICE_ID, d.DEVICE_IDENTIFICATION, e.STATUS, e.OWNER, d.NAME AS DEVICE_NAME, " + + "e.DEVICE_TYPE FROM DM_DEVICE d INNER JOIN DM_ENROLMENT e " + + "WHERE d.ID = e.DEVICE_ID AND d.TENANT_ID = ? AND e.DEVICE_ID IN (" + deviceIdStringList+ ") " + + "AND e.STATUS NOT IN ('DELETED', 'REMOVED')"; + + if (paginationRequest.getOwner() != null) { + sql = sql + " AND e.OWNER = ?"; + isOwnerProvided = true; + } + + if (paginationRequest.getDeviceStatus() != null) { + sql = sql + " AND e.STATUS = ?"; + isDeviceStatusProvided = true; + } + + if (paginationRequest.getDeviceName() != null) { + sql = sql + " AND d.DEVICE_NAME = ?"; + isDeviceNameProvided = true; + } + + int parameterIdx = 1; + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + + preparedStatement.setInt(parameterIdx++, tenantId); + + if (isOwnerProvided) + preparedStatement.setString(parameterIdx++, paginationRequest.getOwner()); + if (isDeviceStatusProvided) + preparedStatement.setString(parameterIdx++, paginationRequest.getDeviceStatus()); + if (isDeviceNameProvided) + preparedStatement.setString(parameterIdx, paginationRequest.getDeviceName()); + + try(ResultSet resultSet = preparedStatement.executeQuery()) { + Device device; + while(resultSet.next()) { + device = new Device(); + device.setId(resultSet.getInt("DEVICE_ID")); + device.setDeviceIdentifier(resultSet.getString("DEVICE_IDENTIFICATION")); + device.setName(resultSet.getString("DEVICE_NAME")); + device.setType(resultSet.getString("DEVICE_TYPE")); + EnrolmentInfo enrolmentInfo = new EnrolmentInfo(); + enrolmentInfo.setStatus(EnrolmentInfo.Status.valueOf(resultSet.getString("STATUS"))); + enrolmentInfo.setOwner(resultSet.getString("OWNER")); + device.setEnrolmentInfo(enrolmentInfo); + devices.add(device); + } + } + } + return devices; + } catch (SQLException e) { + String msg = "Error occurred while retrieving devices for device ids in: " + deviceIds; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + } + } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderService.java index 10d3598ff5..8c04b48f68 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderService.java @@ -1152,4 +1152,10 @@ public interface DeviceManagementProviderService { */ Device updateDeviceName(Device device, String deviceType, String deviceId) throws DeviceManagementException, DeviceNotFoundException, ConflictException; + + List getDevicesNotInGivenIdList(List deviceIds, PaginationRequest paginationRequest) + throws DeviceManagementException; + + List getDevicesByDeviceIds(PaginationRequest paginationRequest, List deviceIds) + throws DeviceManagementException; } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 3192cefcd0..eb8b41b499 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -5592,4 +5592,57 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.closeConnection(); } } + + @Override + public List getDevicesNotInGivenIdList(List deviceIds, PaginationRequest paginationRequest) + throws DeviceManagementException { + + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + if (paginationRequest == null) { + String msg = "Received null for pagination request"; + log.error(msg); + throw new DeviceManagementException(msg); + } + + try { + DeviceManagementDAOFactory.openConnection(); + return deviceDAO.getDevicesNotInGivenIdList(paginationRequest, deviceIds, tenantId); + } catch (DeviceManagementDAOException e) { + String msg = "Error encountered while getting device ids"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (SQLException e) { + String msg = "Error encountered while getting the database connection"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Override + public List getDevicesByDeviceIds(PaginationRequest paginationRequest, List deviceIds) + throws DeviceManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + if (paginationRequest == null) { + String msg = "Received null for pagination request"; + log.error(msg); + throw new DeviceManagementException(msg); + } + + try { + DeviceManagementDAOFactory.openConnection(); + return deviceDAO.getDevicesByDeviceIds(paginationRequest, deviceIds, tenantId); + } catch (DeviceManagementDAOException e) { + String msg = "Error encountered while getting devices for device ids in " + deviceIds; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (SQLException e) { + String msg = "Error encountered while getting the database connection"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } } -- 2.36.3 From 359c052251ef967240585cf69b0729a58f33bc1a Mon Sep 17 00:00:00 2001 From: Rajitha Kumara Date: Fri, 19 Jul 2024 02:11:42 +0530 Subject: [PATCH 04/16] Add improvements to subscription helper services --- .../mgt/core/dao/SubscriptionDAO.java | 2 +- .../GenericSubscriptionDAOImpl.java | 53 +-- ...SubscriptionManagementServiceProvider.java | 4 + ...bscriptionManagementHelperServiceImpl.java | 93 ++++- ...bscriptionManagementHelperServiceImpl.java | 44 ++- ...bscriptionManagementHelperServiceImpl.java | 16 +- ...bscriptionManagementHelperServiceImpl.java | 348 ++++++------------ .../core/dao/impl/AbstractGroupDAOImpl.java | 11 +- 8 files changed, 289 insertions(+), 282 deletions(-) diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java index c77267d26c..c68eb96e1d 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java @@ -342,7 +342,7 @@ public interface SubscriptionDAO { * @return {@link SubscriptionsDTO} which contains the details of subscriptions. * @throws ApplicationManagementDAOException if connection establishment or SQL execution fails. */ - List getUserSubscriptionsByAppReleaseID(int appReleaseId, boolean unsubscribe, int tenantId, + List getUserSubscriptionsByAppReleaseID(int appReleaseId, boolean unsubscribe, int tenantId, int offset, int limit) throws ApplicationManagementDAOException; /** 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/dao/impl/subscription/GenericSubscriptionDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java index 45d4cff175..d645cf7ab1 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java @@ -1694,14 +1694,14 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } @Override - public List getUserSubscriptionsByAppReleaseID(int appReleaseId, boolean unsubscribe, int tenantId, + public List getUserSubscriptionsByAppReleaseID(int appReleaseId, boolean unsubscribe, int tenantId, int offset, int limit) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { log.debug("Request received in DAO Layer to get user subscriptions related to the given UUID."); } try { Connection conn = this.getDBConnection(); - List userSubscriptions = new ArrayList<>(); + List subscriptionEntities = new ArrayList<>(); String subscriptionStatusTime = unsubscribe ? "US.UNSUBSCRIBED_TIMESTAMP" : "US.SUBSCRIBED_TIMESTAMP"; String sql = "SELECT US.USER_NAME, US.SUBSCRIBED_BY, US.SUBSCRIBED_TIMESTAMP, US.UNSUBSCRIBED, " + @@ -1717,21 +1717,21 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc ps.setInt(4, limit); ps.setInt(5, offset); try (ResultSet rs = ps.executeQuery()) { + SubscriptionEntity subscriptionEntity; while (rs.next()) { - SubscriptionsDTO userSubscription; - userSubscription = new SubscriptionsDTO(); - userSubscription.setName(rs.getString("USER_NAME")); - userSubscription.setSubscribedBy(rs.getString("SUBSCRIBED_BY")); - userSubscription.setSubscribedTimestamp(rs.getTimestamp("SUBSCRIBED_TIMESTAMP")); - userSubscription.setUnsubscribed(rs.getBoolean("UNSUBSCRIBED")); - userSubscription.setUnsubscribedBy(rs.getString("UNSUBSCRIBED_BY")); - userSubscription.setUnsubscribedTimestamp(rs.getTimestamp("UNSUBSCRIBED_TIMESTAMP")); - userSubscription.setAppReleaseId(rs.getInt("AP_APP_RELEASE_ID")); - - userSubscriptions.add(userSubscription); + subscriptionEntity = new SubscriptionEntity(); + subscriptionEntity.setIdentity(rs.getString("USER_NAME")); + subscriptionEntity.setSubscribedBy(rs.getString("SUBSCRIBED_BY")); + subscriptionEntity.setSubscribedTimestamp(rs.getTimestamp("SUBSCRIBED_TIMESTAMP")); + subscriptionEntity.setUnsubscribed(rs.getBoolean("UNSUBSCRIBED")); + subscriptionEntity.setUnsubscribedBy(rs.getString("UNSUBSCRIBED_BY")); + subscriptionEntity.setUnsubscribedTimestamp(rs.getTimestamp("UNSUBSCRIBED_TIMESTAMP")); + subscriptionEntity.setApplicationReleaseId(rs.getInt("AP_APP_RELEASE_ID")); + + subscriptionEntities.add(subscriptionEntity); } } - return userSubscriptions; + return subscriptionEntities; } } catch (DBConnectionException e) { String msg = "Error occurred while obtaining the DB connection to get user subscriptions for the given UUID."; @@ -1948,7 +1948,11 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } sql.append("ORDER BY ").append(subscriptionStatusTime). - append(" DESC ").append("LIMIT ? OFFSET ?"); + append(" DESC "); + + if (offset >= 0 && limit >= 0) { + sql.append("LIMIT ? OFFSET ?"); + } try (PreparedStatement ps = conn.prepareStatement(sql.toString())) { int paramIdx = 1; @@ -1969,8 +1973,10 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc ps.setString(paramIdx++, "%" + actionTriggeredBy + "%"); } - ps.setInt(paramIdx++, limit); - ps.setInt(paramIdx++, offset); + if (offset >= 0 && limit >= 0) { + ps.setInt(paramIdx++, limit); + ps.setInt(paramIdx, offset); + } try (ResultSet rs = ps.executeQuery()) { if (log.isDebugEnabled()) { @@ -2134,8 +2140,11 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc sql.append(" AND ").append(actionTriggeredColumn).append(" LIKE ? "); } - sql.append("ORDER BY ").append(subscriptionStatusTime).append(" DESC ") - .append("LIMIT ? OFFSET ?"); + sql.append("ORDER BY ").append(subscriptionStatusTime).append(" DESC "); + + if (limit >= 0 && offset >= 0) { + sql.append("LIMIT ? OFFSET ?"); + } try { Connection conn = this.getDBConnection(); @@ -2155,8 +2164,10 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc ps.setString(paramIdx++, "%" + actionTriggeredBy + "%"); } - ps.setInt(paramIdx++, limit); - ps.setInt(paramIdx++, offset); + if (limit >= 0 && offset >= 0) { + ps.setInt(paramIdx++, limit); + ps.setInt(paramIdx, offset); + } try (ResultSet rs = ps.executeQuery()) { if (log.isDebugEnabled()) { 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/SubscriptionManagementServiceProvider.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/SubscriptionManagementServiceProvider.java index 671f4fdeba..2769278735 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/SubscriptionManagementServiceProvider.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/SubscriptionManagementServiceProvider.java @@ -20,8 +20,10 @@ package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo; +import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.impl.DeviceBasedSubscriptionManagementHelperServiceImpl; import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.impl.GroupBasedSubscriptionManagementHelperServiceImpl; import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.impl.RoleBasedSubscriptionManagementHelperServiceImpl; +import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.impl.UserBasedSubscriptionManagementHelperServiceImpl; import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.service.SubscriptionManagementHelperService; import java.util.Objects; @@ -44,6 +46,8 @@ public class SubscriptionManagementServiceProvider { private SubscriptionManagementHelperService getSubscriptionManagementHelperService(String subscriptionType) { if (Objects.equals(subscriptionType, "role")) return RoleBasedSubscriptionManagementHelperServiceImpl.getInstance(); if (Objects.equals(subscriptionType, "group")) return GroupBasedSubscriptionManagementHelperServiceImpl.getInstance(); + if (Objects.equals(subscriptionType, "user")) return UserBasedSubscriptionManagementHelperServiceImpl.getInstance(); + if (Objects.equals(subscriptionType, "device")) return DeviceBasedSubscriptionManagementHelperServiceImpl.getInstance(); throw new UnsupportedOperationException("Subscription type: " + subscriptionType + " not supports"); } } 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 fad338b06f..30d91338c3 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 @@ -21,28 +21,111 @@ package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.imp 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.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 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; +import java.util.stream.Collectors; public class DeviceBasedSubscriptionManagementHelperServiceImpl implements SubscriptionManagementHelperService { + private static final Log log = LogFactory.getLog(DeviceBasedSubscriptionManagementHelperServiceImpl.class); + private DeviceBasedSubscriptionManagementHelperServiceImpl() {} + private static class DeviceBasedSubscriptionManagementHelperServiceImplHolder { + private static final DeviceBasedSubscriptionManagementHelperServiceImpl INSTANCE + = new DeviceBasedSubscriptionManagementHelperServiceImpl(); + } + public static DeviceBasedSubscriptionManagementHelperServiceImpl getInstance() { + return DeviceBasedSubscriptionManagementHelperServiceImpl.DeviceBasedSubscriptionManagementHelperServiceImplHolder.INSTANCE; + } + @Override - public List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { - return null; + public List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + throws ApplicationManagementException { + final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus()); + List deviceSubscriptionDTOS; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + + try { + ConnectionManagerUtil.openDBConnection(); + ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO. + getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId); + if (applicationReleaseDTO == null) { + String msg = "Couldn't find an application release for application release UUID: " + + subscriptionInfo.getApplicationUUID(); + log.error(msg); + throw new NotFoundException(msg); + } + + String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); + DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); + + if (Objects.equals("NEW", deviceSubscriptionStatus)) { + deviceSubscriptionDTOS = subscriptionDAO.getAllSubscriptionsDetails(applicationReleaseDTO. + getId(),isUnsubscribe, tenantId, null, subscriptionInfo.getSubscriptionType(), + deviceSubscriptionFilterCriteria.getTriggeredBy(),-1, -1); + + List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). + map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); + + List newDeviceIds = HelperUtil.getDeviceManagementProviderService(). + getDevicesNotInGivenIdList(deviceIdsOfSubscription, new PaginationRequest(offset, limit)); + deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); + } else { + deviceSubscriptionDTOS = subscriptionDAO.getAllSubscriptionsDetails(applicationReleaseDTO. + getId(),isUnsubscribe, tenantId, null, subscriptionInfo.getSubscriptionType(), + deviceSubscriptionFilterCriteria.getTriggeredBy(), limit, offset); + } + + return SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, + subscriptionInfo.getDeviceSubscriptionFilterCriteria()); + + } catch (DeviceManagementException e) { + String msg = "Error encountered while getting device details"; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (ApplicationManagementDAOException | DBConnectionException e) { + String msg = "Error encountered while connecting to the database"; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } } @Override - public List getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { - return null; + public List getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + throws ApplicationManagementException { + return Collections.emptyList(); } @Override public void getSubscriptionStatistics() throws ApplicationManagementException { - + // todo: analytics engine } } 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 6454fa8807..9e05b6fc2e 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 @@ -35,6 +35,7 @@ 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.PaginationRequest; import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupManagementException; import io.entgra.device.mgt.core.device.mgt.core.dto.GroupDetailsDTO; @@ -45,6 +46,7 @@ import org.wso2.carbon.context.PrivilegedCarbonContext; import java.util.List; import java.util.Objects; +import java.util.stream.Collectors; public class GroupBasedSubscriptionManagementHelperServiceImpl implements SubscriptionManagementHelperService { private static final Log log = LogFactory.getLog(GroupBasedSubscriptionManagementHelperServiceImpl.class); @@ -65,7 +67,7 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus()); List deviceSubscriptionDTOS; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - // todo: check and refactor + try { ConnectionManagerUtil.openDBConnection(); ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO. @@ -89,28 +91,40 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); GroupManagementProviderService groupManagementProviderService = HelperUtil.getGroupManagementProviderService(); - GroupDetailsDTO groupDetailsDTO = groupManagementProviderService.getGroupDetailsWithDevices(subscriptionInfo.getIdentifier(), - applicationDTO.getDeviceTypeId(), deviceSubscriptionFilterCriteria.getOwner(), deviceSubscriptionFilterCriteria.getName(), - deviceSubscriptionFilterCriteria.getDeviceStatus(), offset, limit); - - List deviceIdsOwnByGroup = groupDetailsDTO.getDeviceIds(); + GroupDetailsDTO groupDetailsDTO; + List deviceIdsOwnByGroup; if (Objects.equals("NEW", deviceSubscriptionStatus)) { + groupDetailsDTO = groupManagementProviderService.getGroupDetailsWithDevices(subscriptionInfo.getIdentifier(), + applicationDTO.getDeviceTypeId(), deviceSubscriptionFilterCriteria.getOwner(), deviceSubscriptionFilterCriteria.getName(), + deviceSubscriptionFilterCriteria.getDeviceStatus(), -1, -1); + deviceIdsOwnByGroup = groupDetailsDTO.getDeviceIds(); + + deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), + isUnsubscribe, tenantId, deviceIdsOwnByGroup, null, + subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), + null, -1, -1); + + List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). + map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); + + List newDeviceIds = HelperUtil.getDeviceManagementProviderService(). + getDevicesNotInGivenIdList(deviceIdsOfSubscription, new PaginationRequest(offset, limit)); + deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); + } else { + groupDetailsDTO = groupManagementProviderService.getGroupDetailsWithDevices(subscriptionInfo.getIdentifier(), + applicationDTO.getDeviceTypeId(), deviceSubscriptionFilterCriteria.getOwner(), deviceSubscriptionFilterCriteria.getName(), + deviceSubscriptionFilterCriteria.getDeviceStatus(), offset, limit); + deviceIdsOwnByGroup = groupDetailsDTO.getDeviceIds(); deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, deviceIdsOwnByGroup, null, subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), null, limit, offset); - for (DeviceSubscriptionDTO deviceSubscriptionDTO: deviceSubscriptionDTOS) { - deviceIdsOwnByGroup.remove(deviceSubscriptionDTO.getDeviceId()); - } } - deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIdsOwnByGroup, deviceSubscriptionStatus, subscriptionInfo.getSubscriptionType(), - deviceSubscriptionFilterCriteria.getTriggeredBy(), deviceSubscriptionStatus, limit, offset); - return SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria()); + return SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, + subscriptionInfo.getDeviceSubscriptionFilterCriteria()); } catch (GroupManagementException e) { String msg = "Error encountered while retrieving group details for group: " + subscriptionInfo.getIdentifier(); @@ -156,6 +170,6 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr @Override public void getSubscriptionStatistics() throws ApplicationManagementException { - + // todo: analytics engine } } 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 5bf4ffcb20..72796a9cd4 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 @@ -103,12 +103,11 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); - deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIdsOwnByRole, null, - subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), - null, limit, offset); - if (Objects.equals("NEW", deviceSubscriptionStatus)) { + deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), + isUnsubscribe, tenantId, deviceIdsOwnByRole, null, + subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), + null, -1, -1); List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); @@ -116,6 +115,11 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri List newDeviceIds = HelperUtil.getDeviceManagementProviderService(). getDevicesNotInGivenIdList(deviceIdsOfSubscription, new PaginationRequest(offset, limit)); deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); + } else { + deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), + isUnsubscribe, tenantId, deviceIdsOwnByRole, null, + subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), + null, limit, offset); } return SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, @@ -166,6 +170,6 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri @Override public void getSubscriptionStatistics() throws ApplicationManagementException { - + // todo: analytics engine } } 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 0180b9953d..437bab449f 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 @@ -20,254 +20,142 @@ 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.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.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 org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; -/* -// @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(); -// } -// } - */ public class UserBasedSubscriptionManagementHelperServiceImpl implements SubscriptionManagementHelperService { + private static final Log log = LogFactory.getLog(UserBasedSubscriptionManagementHelperServiceImpl.class); + private UserBasedSubscriptionManagementHelperServiceImpl() {} + private static class UserBasedSubscriptionManagementHelperServiceImplHolder { + private static final UserBasedSubscriptionManagementHelperServiceImpl INSTANCE + = new UserBasedSubscriptionManagementHelperServiceImpl(); + } + public static UserBasedSubscriptionManagementHelperServiceImpl getInstance() { + return UserBasedSubscriptionManagementHelperServiceImpl.UserBasedSubscriptionManagementHelperServiceImplHolder.INSTANCE; + } + + @SuppressWarnings("unchecked") @Override public List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { - return null; + final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus()); + List deviceSubscriptionDTOS; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + + 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()); + + ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO. + getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId); + if (applicationReleaseDTO == null) { + String msg = "Couldn't find an application release for application release UUID: " + + subscriptionInfo.getApplicationUUID(); + log.error(msg); + throw new NotFoundException(msg); + } + + String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); + DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); + + if (Objects.equals("NEW", deviceSubscriptionStatus)) { + deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), + isUnsubscribe, tenantId, deviceIdsOwnByUser, null, + subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), + null, -1, -1); + + List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). + map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); + + List newDeviceIds = HelperUtil.getDeviceManagementProviderService(). + getDevicesNotInGivenIdList(deviceIdsOfSubscription, new PaginationRequest(offset, limit)); + deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); + } else { + deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), + isUnsubscribe, tenantId, deviceIdsOwnByUser, null, + subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), + null, limit, offset); + } + + + return SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, + subscriptionInfo.getDeviceSubscriptionFilterCriteria()); + + } catch (DeviceManagementException e) { + String msg = "Error encountered while getting device details"; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (ApplicationManagementDAOException | DBConnectionException e) { + String msg = "Error encountered while connecting to the database"; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } } @Override - public List getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { - return null; + public List getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + throws ApplicationManagementException { + final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus()); + final int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { + ConnectionManagerUtil.openDBConnection(); + ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO. + getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId); + if (applicationReleaseDTO == null) { + String msg = "Couldn't find an application release for application release UUID: " + + subscriptionInfo.getApplicationUUID(); + log.error(msg); + throw new NotFoundException(msg); + } + return subscriptionDAO. + getUserSubscriptionsByAppReleaseID(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, offset, limit); + } catch (DBConnectionException | ApplicationManagementDAOException e) { + String msg = "Error encountered while connecting to the database"; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } } @Override public void getSubscriptionStatistics() throws ApplicationManagementException { - + // todo: analytics engine } } 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 1eba6c1a7e..416b6e6efd 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 @@ -1494,8 +1494,9 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { if (deviceStatus != null && !deviceStatus.isEmpty()) { sql.append(" AND e.STATUS = ?"); } - - sql.append(" LIMIT ? OFFSET ?"); + if (limit >= 0 && offset >=0 ) { + sql.append(" LIMIT ? OFFSET ?"); + } Connection conn = null; try { @@ -1519,8 +1520,10 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { stmt.setString(index++, deviceStatus); } - stmt.setInt(index++, limit); - stmt.setInt(index++, offset); + if (limit >= 0 && offset >=0 ) { + stmt.setInt(index++, limit); + stmt.setInt(index++, offset); + } try (ResultSet rs = stmt.executeQuery()) { while (rs.next()) { -- 2.36.3 From d2317e47e0bf63fe393b8d3e43f12b83c076e5ea Mon Sep 17 00:00:00 2001 From: Rajitha Kumara Date: Fri, 19 Jul 2024 12:03:46 +0530 Subject: [PATCH 05/16] Fix missing properties in subscription data bean --- .../mgt/common/DeviceSubscription.java | 20 +++++++++++++++++++ .../mgt/SubscriptionManagementHelperUtil.java | 6 +++++- .../core/dao/impl/AbstractDeviceDAOImpl.java | 4 +++- 3 files changed, 28 insertions(+), 2 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 98c76c192c..f3209c53b6 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 @@ -19,14 +19,26 @@ package io.entgra.device.mgt.core.application.mgt.common; +import java.sql.Timestamp; + public class DeviceSubscription { + private String deviceName; private String deviceIdentifier; private String deviceStatus; private String deviceOwner; private String deviceType; private String ownershipType; + private Timestamp dateOfLastUpdate; private SubscriptionData subscriptionData; + public String getDeviceName() { + return deviceName; + } + + public void setDeviceName(String deviceName) { + this.deviceName = deviceName; + } + public String getDeviceIdentifier() { return deviceIdentifier; } @@ -67,6 +79,14 @@ public class DeviceSubscription { this.ownershipType = ownershipType; } + public Timestamp getDateOfLastUpdate() { + return dateOfLastUpdate; + } + + public void setDateOfLastUpdate(Timestamp dateOfLastUpdate) { + this.dateOfLastUpdate = dateOfLastUpdate; + } + public SubscriptionData getSubscriptionData() { return subscriptionData; } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/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 93a5d6f066..d20294535c 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 @@ -20,7 +20,6 @@ package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt; 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.SubscriptionData; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo; @@ -30,6 +29,7 @@ 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.exceptions.DeviceManagementException; +import java.sql.Timestamp; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @@ -56,6 +56,10 @@ public class SubscriptionManagementHelperUtil { deviceSubscription.setDeviceIdentifier(device.getDeviceIdentifier()); deviceSubscription.setDeviceOwner(device.getEnrolmentInfo().getOwner()); deviceSubscription.setDeviceType(device.getType()); + deviceSubscription.setDeviceName(device.getName()); + deviceSubscription.setDeviceStatus(device.getEnrolmentInfo().getStatus().name()); + deviceSubscription.setOwnershipType(device.getEnrolmentInfo().getOwnership().name()); + deviceSubscription.setDateOfLastUpdate(new Timestamp(device.getEnrolmentInfo().getDateOfLastUpdate())); SubscriptionData subscriptionData = new SubscriptionData(); subscriptionData.setTriggeredBy(deviceSubscriptionDTO.getActionTriggeredFrom()); subscriptionData.setTriggeredAt(deviceSubscriptionDTO.getSubscribedTimestamp()); diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index 2034bcf33c..7f77d3d3e2 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -3337,7 +3337,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { try { Connection connection = getConnection(); String sql = "SELECT e.DEVICE_ID, d.DEVICE_IDENTIFICATION, e.STATUS, e.OWNER, d.NAME AS DEVICE_NAME, " + - "e.DEVICE_TYPE FROM DM_DEVICE d INNER JOIN DM_ENROLMENT e " + + "e.DEVICE_TYPE, e.OWNERSHIP, e.DATE_OF_LAST_UPDATE FROM DM_DEVICE d INNER JOIN DM_ENROLMENT e " + "WHERE d.ID = e.DEVICE_ID AND d.TENANT_ID = ? AND e.DEVICE_ID IN (" + deviceIdStringList+ ") " + "AND e.STATUS NOT IN ('DELETED', 'REMOVED')"; @@ -3379,6 +3379,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { EnrolmentInfo enrolmentInfo = new EnrolmentInfo(); enrolmentInfo.setStatus(EnrolmentInfo.Status.valueOf(resultSet.getString("STATUS"))); enrolmentInfo.setOwner(resultSet.getString("OWNER")); + enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.valueOf(resultSet.getString("OWNERSHIP"))); + enrolmentInfo.setDateOfLastUpdate(resultSet.getTimestamp("DATE_OF_LAST_UPDATE").getTime()); device.setEnrolmentInfo(enrolmentInfo); devices.add(device); } -- 2.36.3 From 0e01b1c0ee885bf15c779c525192d4aec37b5828 Mon Sep 17 00:00:00 2001 From: Rajitha Kumara Date: Fri, 19 Jul 2024 19:51:42 +0530 Subject: [PATCH 06/16] Fix filtering issues --- .../mgt/common/SubscriptionResponse.java | 63 +++++++++++++++++++ .../common/services/SubscriptionManager.java | 5 +- .../mgt/core/dao/SubscriptionDAO.java | 2 + .../GenericSubscriptionDAOImpl.java | 60 +++++++++++++++++- .../core/impl/SubscriptionManagerImpl.java | 5 +- .../mgt/SubscriptionManagementHelperUtil.java | 12 ++-- ...bscriptionManagementHelperServiceImpl.java | 34 ++++++---- ...bscriptionManagementHelperServiceImpl.java | 30 ++++++--- ...bscriptionManagementHelperServiceImpl.java | 28 ++++++--- ...bscriptionManagementHelperServiceImpl.java | 30 ++++++--- .../SubscriptionManagementHelperService.java | 5 +- .../core/device/mgt/core/dao/DeviceDAO.java | 3 + .../core/dao/impl/AbstractDeviceDAOImpl.java | 34 ++++++++-- .../DeviceManagementProviderService.java | 1 + .../DeviceManagementProviderServiceImpl.java | 21 +++++++ 15 files changed, 277 insertions(+), 56 deletions(-) create mode 100644 components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionResponse.java 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/SubscriptionResponse.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionResponse.java new file mode 100644 index 0000000000..aeaeba70be --- /dev/null +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionResponse.java @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package io.entgra.device.mgt.core.application.mgt.common; + +import java.util.List; + +public class SubscriptionResponse { + private String applicationUUID; + private int count; + private List data; + + public SubscriptionResponse(String applicationUUID, int count, List data) { + this.applicationUUID = applicationUUID; + this.count = count; + this.data = data; + } + + public SubscriptionResponse(String applicationUUID, List data) { + this.applicationUUID = applicationUUID; + this.data = data; + } + + public String getApplicationUUID() { + return applicationUUID; + } + + public void setApplicationUUID(String applicationUUID) { + this.applicationUUID = applicationUUID; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } +} 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 7f73cd1d0e..bf5434283b 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 @@ -24,6 +24,7 @@ import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionData; 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.SubscriptionType; import io.entgra.device.mgt.core.application.mgt.common.dto.CategorizedSubscriptionCountsDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; @@ -267,10 +268,10 @@ public interface SubscriptionManager { */ // List getRoleSubscriptionsByUUID(String uuid, String subscriptionStatus, PaginationRequest request, // int offset, int limit) throws ApplicationManagementException; - List getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException; - List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException; /** 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/dao/SubscriptionDAO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java index c68eb96e1d..8924fa4733 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java @@ -402,6 +402,8 @@ public interface SubscriptionDAO { List deviceIds, String actionStatus, String actionType, String actionTriggeredBy, String tabActionStatus, int limit, int offset) throws ApplicationManagementDAOException; + int getDeviceSubscriptionCount(int appReleaseId, boolean unsubscribe, int tenantId, String actionStatus, String actionType, + String actionTriggeredBy) throws ApplicationManagementDAOException; /** * This method is used to get the details of device subscriptions related to a 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/dao/impl/subscription/GenericSubscriptionDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java index d645cf7ab1..b9a459635d 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java @@ -1915,7 +1915,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc public List getSubscriptionDetailsByDeviceIds(int appReleaseId, boolean unsubscribe, int tenantId, List deviceIds, String actionStatus, String actionType, String actionTriggeredBy, String tabActionStatus, - int offset, int limit) throws ApplicationManagementDAOException { + int limit, int offset) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { log.debug("Getting device subscriptions for the application release id " + appReleaseId + " and device ids " + deviceIds + " from the database"); @@ -2014,6 +2014,64 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } + @Override + public int getDeviceSubscriptionCount(int appReleaseId, boolean unsubscribe, int tenantId, String actionStatus, + String actionType, String actionTriggeredBy) throws ApplicationManagementDAOException { + int deviceCount = 0; + try { + Connection conn = this.getDBConnection(); + StringBuilder sql = new StringBuilder("SELECT " + + "COUNT(DS.ID) AS DEVICE_COUNT " + + "FROM AP_DEVICE_SUBSCRIPTION DS " + + "WHERE DS.AP_APP_RELEASE_ID = ? AND DS.UNSUBSCRIBED = ? AND DS.TENANT_ID = ?"); + + if (actionStatus != null && !actionStatus.isEmpty()) { + sql.append(" AND DS.STATUS = ? "); + } + if (actionType != null && !actionType.isEmpty()) { + sql.append(" AND DS.ACTION_TRIGGERED_FROM = ? "); + } + if (actionTriggeredBy != null && !actionTriggeredBy.isEmpty()) { + sql.append(" AND DS.SUBSCRIBED_BY LIKE ? "); + } + + try (PreparedStatement ps = conn.prepareStatement(sql.toString())) { + int paramIdx = 1; + ps.setInt(paramIdx++, appReleaseId); + ps.setBoolean(paramIdx++, unsubscribe); + ps.setInt(paramIdx++, tenantId); + + if (actionStatus != null && !actionStatus.isEmpty()) { + ps.setString(paramIdx++, actionStatus); + } + if (actionType != null && !actionType.isEmpty()) { + ps.setString(paramIdx++, actionType); + } + if (actionTriggeredBy != null && !actionTriggeredBy.isEmpty()) { + ps.setString(paramIdx, "%" + actionTriggeredBy + "%"); + } + + try (ResultSet rs = ps.executeQuery()) { + if (rs.next()) { + deviceCount = rs.getInt("DEVICE_COUNT"); + } + } + + return deviceCount; + + } catch (SQLException e) { + String msg = "Error occurred while running SQL to get device subscription data for application ID"; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection for getting device subscriptions for " + + "application Id: " + appReleaseId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } + // @Override // public List getSubscriptionDetailsByDeviceIds(int appReleaseId, boolean unsubscribe, int tenantId, // List deviceIds, String actionStatus, String actionType, 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 bb6c93d889..3b14c5d68e 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 @@ -27,6 +27,7 @@ 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.dto.CategorizedSubscriptionCountsDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionsDTO; @@ -1722,7 +1723,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } @Override - public List getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + public SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { SubscriptionManagementHelperService subscriptionManagementHelperService = SubscriptionManagementServiceProvider.getInstance().getSubscriptionManagementHelperService(subscriptionInfo); @@ -1730,7 +1731,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } @Override - public List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + public SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { SubscriptionManagementHelperService subscriptionManagementHelperService = SubscriptionManagementServiceProvider.getInstance().getSubscriptionManagementHelperService(subscriptionInfo); diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/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 d20294535c..ca1077633f 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 @@ -36,17 +36,20 @@ import java.util.stream.Collectors; public class SubscriptionManagementHelperUtil { public static List getDeviceSubscriptionData(List deviceSubscriptionDTOS, - DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria) throws DeviceManagementException { + DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria, + boolean isUnsubscribed) + throws DeviceManagementException { List deviceIds = deviceSubscriptionDTOS.stream().map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); PaginationRequest paginationRequest = new PaginationRequest(0, -1); paginationRequest.setDeviceName(deviceSubscriptionFilterCriteria.getName()); paginationRequest.setDeviceStatus(deviceSubscriptionFilterCriteria.getDeviceStatus()); paginationRequest.setOwner(deviceSubscriptionFilterCriteria.getOwner()); List devices = HelperUtil.getDeviceManagementProviderService().getDevicesByDeviceIds(paginationRequest, deviceIds); - return populateDeviceData(deviceSubscriptionDTOS, devices); + return populateDeviceData(deviceSubscriptionDTOS, devices, isUnsubscribed); } - private static List populateDeviceData(List deviceSubscriptionDTOS, List devices) { + private static List populateDeviceData(List deviceSubscriptionDTOS, + List devices, boolean isUnsubscribed) { List deviceSubscriptions = new ArrayList<>(); for (Device device : devices) { int idx = deviceSubscriptionDTOS.indexOf(new DeviceSubscriptionDTO(device.getId())); @@ -61,7 +64,8 @@ public class SubscriptionManagementHelperUtil { deviceSubscription.setOwnershipType(device.getEnrolmentInfo().getOwnership().name()); deviceSubscription.setDateOfLastUpdate(new Timestamp(device.getEnrolmentInfo().getDateOfLastUpdate())); SubscriptionData subscriptionData = new SubscriptionData(); - subscriptionData.setTriggeredBy(deviceSubscriptionDTO.getActionTriggeredFrom()); + subscriptionData.setTriggeredBy(isUnsubscribed ? deviceSubscriptionDTO.getUnsubscribedBy() : + deviceSubscriptionDTO.getSubscribedBy()); subscriptionData.setTriggeredAt(deviceSubscriptionDTO.getSubscribedTimestamp()); subscriptionData.setSubscriptionType(deviceSubscriptionDTO.getStatus()); deviceSubscription.setSubscriptionData(subscriptionData); diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/DeviceBasedSubscriptionManagementHelperServiceImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/DeviceBasedSubscriptionManagementHelperServiceImpl.java index 30d91338c3..a7edd0359e 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 @@ -24,6 +24,7 @@ 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.dto.ApplicationReleaseDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionsDTO; @@ -40,6 +41,7 @@ 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; @@ -65,10 +67,11 @@ public class DeviceBasedSubscriptionManagementHelperServiceImpl implements Subsc } @Override - public List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + public SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus()); List deviceSubscriptionDTOS; + int deviceCount = 0; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { @@ -84,27 +87,34 @@ public class DeviceBasedSubscriptionManagementHelperServiceImpl implements Subsc String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); + DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); if (Objects.equals("NEW", deviceSubscriptionStatus)) { deviceSubscriptionDTOS = subscriptionDAO.getAllSubscriptionsDetails(applicationReleaseDTO. - getId(),isUnsubscribe, tenantId, null, subscriptionInfo.getSubscriptionType(), + getId(),isUnsubscribe, tenantId, null, subscriptionInfo.getSubscriptionType().toUpperCase(), deviceSubscriptionFilterCriteria.getTriggeredBy(),-1, -1); List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); - List newDeviceIds = HelperUtil.getDeviceManagementProviderService(). - 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 { deviceSubscriptionDTOS = subscriptionDAO.getAllSubscriptionsDetails(applicationReleaseDTO. - getId(),isUnsubscribe, tenantId, null, subscriptionInfo.getSubscriptionType(), - deviceSubscriptionFilterCriteria.getTriggeredBy(), limit, offset); - } - - return SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria()); + getId(),isUnsubscribe, tenantId, subscriptionInfo.getDeviceSubscriptionStatus(), subscriptionInfo. + getSubscriptionType().toUpperCase(), deviceSubscriptionFilterCriteria.getTriggeredBy(), offset, limit); + deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, + subscriptionInfo.getDeviceSubscriptionStatus(), subscriptionInfo.getSubscriptionType().toUpperCase(), + deviceSubscriptionFilterCriteria.getTriggeredBy()); + } + List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe); + return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions); } catch (DeviceManagementException e) { String msg = "Error encountered while getting device details"; log.error(msg, e); @@ -119,9 +129,9 @@ public class DeviceBasedSubscriptionManagementHelperServiceImpl implements Subsc } @Override - public List getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + public SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { - return Collections.emptyList(); + return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), Collections.emptyList()); } @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/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 9e05b6fc2e..59240df4d5 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 @@ -24,6 +24,7 @@ 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.dto.ApplicationDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationReleaseDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; @@ -39,6 +40,7 @@ import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest; import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupManagementException; import io.entgra.device.mgt.core.device.mgt.core.dto.GroupDetailsDTO; +import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService; import io.entgra.device.mgt.core.device.mgt.core.service.GroupManagementProviderService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -61,11 +63,12 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr } @Override - public List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + public SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus()); List deviceSubscriptionDTOS; + int deviceCount = 0; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { @@ -89,6 +92,7 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); + DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); GroupManagementProviderService groupManagementProviderService = HelperUtil.getGroupManagementProviderService(); GroupDetailsDTO groupDetailsDTO; @@ -108,9 +112,11 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); - List newDeviceIds = HelperUtil.getDeviceManagementProviderService(). - 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 { groupDetailsDTO = groupManagementProviderService.getGroupDetailsWithDevices(subscriptionInfo.getIdentifier(), applicationDTO.getDeviceTypeId(), deviceSubscriptionFilterCriteria.getOwner(), deviceSubscriptionFilterCriteria.getName(), @@ -118,14 +124,17 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr deviceIdsOwnByGroup = groupDetailsDTO.getDeviceIds(); deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIdsOwnByGroup, null, + isUnsubscribe, tenantId, deviceIdsOwnByGroup, subscriptionInfo.getDeviceSubscriptionStatus(), subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), null, limit, offset); - } - - return SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria()); + deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, + subscriptionInfo.getDeviceSubscriptionStatus(), subscriptionInfo.getSubscriptionType(), + deviceSubscriptionFilterCriteria.getTriggeredBy()); + } + List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe); + return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions); } catch (GroupManagementException e) { String msg = "Error encountered while retrieving group details for group: " + subscriptionInfo.getIdentifier(); log.error(msg, e); @@ -143,7 +152,7 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr } @Override - public List getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + public SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus()); final int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); @@ -157,8 +166,9 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr log.error(msg); throw new NotFoundException(msg); } - return subscriptionDAO. + List subscriptionEntities = subscriptionDAO. getGroupsSubscriptionDetailsByAppReleaseID(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, offset, limit); + return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), 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 72796a9cd4..eb5c13fb4c 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 @@ -24,6 +24,7 @@ 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.dto.ApplicationReleaseDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException; @@ -39,6 +40,7 @@ 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; @@ -65,10 +67,11 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri @SuppressWarnings("unchecked") @Override - public List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + public SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus()); List deviceSubscriptionDTOS; + int deviceCount = 0; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { @@ -102,6 +105,7 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); + DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); if (Objects.equals("NEW", deviceSubscriptionStatus)) { deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), @@ -112,18 +116,23 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); - List newDeviceIds = HelperUtil.getDeviceManagementProviderService(). - 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 { deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIdsOwnByRole, null, + isUnsubscribe, tenantId, deviceIdsOwnByRole, subscriptionInfo.getDeviceSubscriptionStatus(), subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), null, limit, offset); - } - return SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria()); + deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, + subscriptionInfo.getDeviceSubscriptionStatus(), subscriptionInfo.getSubscriptionType(), + deviceSubscriptionFilterCriteria.getTriggeredBy()); + } + List deviceSubscriptions = SubscriptionManagementHelperUtil. + getDeviceSubscriptionData(deviceSubscriptionDTOS, + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe); + return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions); } catch (UserStoreException e) { String msg = "Error encountered while getting the user management store for tenant id " + tenantId; @@ -143,7 +152,7 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri } @Override - public List getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + public SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus()); final int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); @@ -157,8 +166,9 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri log.error(msg); throw new NotFoundException(msg); } - return subscriptionDAO. + List subscriptionEntities = subscriptionDAO. getRoleSubscriptionsByAppReleaseID(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, offset, limit); + return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), 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/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 437bab449f..ee982575d1 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 @@ -23,6 +23,7 @@ import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscription; 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.dto.ApplicationReleaseDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException; @@ -37,6 +38,7 @@ 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; @@ -60,10 +62,11 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri @SuppressWarnings("unchecked") @Override - public List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + public SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus()); List deviceSubscriptionDTOS; + int deviceCount = 0; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { @@ -91,6 +94,7 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); + DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); if (Objects.equals("NEW", deviceSubscriptionStatus)) { deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), @@ -101,20 +105,25 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); - List newDeviceIds = HelperUtil.getDeviceManagementProviderService(). - 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 { deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIdsOwnByUser, null, + isUnsubscribe, tenantId, deviceIdsOwnByUser, subscriptionInfo.getDeviceSubscriptionStatus(), subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), null, limit, offset); - } + deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, + subscriptionInfo.getDeviceSubscriptionStatus(), subscriptionInfo.getSubscriptionType(), + deviceSubscriptionFilterCriteria.getTriggeredBy()); + } - return SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria()); - + List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe); + return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions); } catch (DeviceManagementException e) { String msg = "Error encountered while getting device details"; log.error(msg, e); @@ -129,7 +138,7 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri } @Override - public List getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + public SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus()); final int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); @@ -143,8 +152,9 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri log.error(msg); throw new NotFoundException(msg); } - return subscriptionDAO. + List subscriptionEntities = subscriptionDAO. getUserSubscriptionsByAppReleaseID(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, offset, limit); + return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), 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/service/SubscriptionManagementHelperService.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/service/SubscriptionManagementHelperService.java index 8b234f0256..743f51b3bb 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/service/SubscriptionManagementHelperService.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/service/SubscriptionManagementHelperService.java @@ -23,6 +23,7 @@ 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.dto.DeviceSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionsDTO; import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException; @@ -37,9 +38,9 @@ public interface SubscriptionManagementHelperService { SubscriptionDAO subscriptionDAO = ApplicationManagementDAOFactory.getSubscriptionDAO(); ApplicationDAO applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO(); ApplicationReleaseDAO applicationReleaseDAO = ApplicationManagementDAOFactory.getApplicationReleaseDAO(); - List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException; - List getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException; void getSubscriptionStatistics() throws ApplicationManagementException; } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/DeviceDAO.java index 3bf7e2d620..a071b0f6eb 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/DeviceDAO.java @@ -868,6 +868,9 @@ public interface DeviceDAO { List getDevicesNotInGivenIdList(PaginationRequest request, List deviceIds, int tenantId) throws DeviceManagementDAOException; + int getDeviceCountNotInGivenIdList(List deviceIds, int tenantId) + throws DeviceManagementDAOException; + List getDevicesByDeviceIds(PaginationRequest paginationRequest, List deviceIds, int tenantId) throws DeviceManagementDAOException; } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index 7f77d3d3e2..3c08335e7a 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -3308,10 +3308,11 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { try { Connection connection = getConnection(); String sql = "SELECT ID AS DEVICE_ID FROM DM_DEVICE WHERE ID NOT IN " + - "(" + deviceIdStringList + ") LIMIT ? OFFSET ?"; + "(" + deviceIdStringList + ") AND TENANT_ID = ? LIMIT ? OFFSET ?"; try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { - preparedStatement.setInt(1, request.getRowCount()); - preparedStatement.setInt(2, request.getStartIndex()); + preparedStatement.setInt(1, tenantId); + preparedStatement.setInt(2, request.getRowCount()); + preparedStatement.setInt(3, request.getStartIndex()); try (ResultSet resultSet = preparedStatement.executeQuery()) { while (resultSet.next()) { filteredDeviceIds.add(resultSet.getInt("DEVICE_ID")); @@ -3326,6 +3327,31 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } } + @Override + public int getDeviceCountNotInGivenIdList(List deviceIds, int tenantId) + throws DeviceManagementDAOException { + int deviceCount = 0; + String deviceIdStringList = deviceIds.stream().map(String::valueOf).collect(Collectors.joining(",")); + try { + Connection connection = getConnection(); + String sql = "SELECT COUNT(ID) AS COUNT FROM DM_DEVICE WHERE ID NOT IN " + + "(" + deviceIdStringList + ") AND TENANT_ID = ?"; + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setInt(1, tenantId); + try (ResultSet resultSet = preparedStatement.executeQuery()) { + if (resultSet.next()) { + deviceCount = resultSet.getInt("COUNT"); + } + } + return deviceCount; + } + } catch (SQLException e) { + String msg = "Error occurred while retrieving device count"; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + } + @Override public List getDevicesByDeviceIds(PaginationRequest paginationRequest, List deviceIds, int tenantId) throws DeviceManagementDAOException { @@ -3352,7 +3378,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } if (paginationRequest.getDeviceName() != null) { - sql = sql + " AND d.DEVICE_NAME = ?"; + sql = sql + " AND d.NAME = ?"; isDeviceNameProvided = true; } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderService.java index 8c04b48f68..5a8439431f 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderService.java @@ -1155,6 +1155,7 @@ public interface DeviceManagementProviderService { List getDevicesNotInGivenIdList(List deviceIds, PaginationRequest paginationRequest) throws DeviceManagementException; + int getDeviceCountNotInGivenIdList(List deviceIds) throws DeviceManagementException; List getDevicesByDeviceIds(PaginationRequest paginationRequest, List deviceIds) throws DeviceManagementException; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index eb8b41b499..c5353ab84e 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -5620,6 +5620,27 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } } + @Override + public int getDeviceCountNotInGivenIdList(List deviceIds) + throws DeviceManagementException { + + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { + DeviceManagementDAOFactory.openConnection(); + return deviceDAO.getDeviceCountNotInGivenIdList(deviceIds, tenantId); + } catch (DeviceManagementDAOException e) { + String msg = "Error encountered while getting device ids"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (SQLException e) { + String msg = "Error encountered while getting the database connection"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + @Override public List getDevicesByDeviceIds(PaginationRequest paginationRequest, List deviceIds) throws DeviceManagementException { -- 2.36.3 From 6b6787e0835907d48782e425eda5635c677271b6 Mon Sep 17 00:00:00 2001 From: Rajitha Kumara Date: Sat, 20 Jul 2024 00:37:16 +0530 Subject: [PATCH 07/16] Add statistics to group based supscription mgt helper --- .../mgt/common/SubscriptionStatistics.java | 68 +++++++++++++++++++ .../common/dto/SubscriptionStatisticDTO.java | 50 ++++++++++++++ .../mgt/core/dao/SubscriptionDAO.java | 5 ++ .../GenericSubscriptionDAOImpl.java | 52 ++++++++++++++ .../mgt/SubscriptionManagementHelperUtil.java | 37 ++++++++-- ...bscriptionManagementHelperServiceImpl.java | 6 +- ...bscriptionManagementHelperServiceImpl.java | 28 +++++++- ...bscriptionManagementHelperServiceImpl.java | 5 +- ...bscriptionManagementHelperServiceImpl.java | 4 +- .../SubscriptionManagementHelperService.java | 4 +- .../core/device/mgt/core/dao/GroupDAO.java | 1 + .../core/dao/impl/AbstractGroupDAOImpl.java | 25 +++++++ .../GroupManagementProviderService.java | 3 + .../GroupManagementProviderServiceImpl.java | 14 ++++ 14 files changed, 288 insertions(+), 14 deletions(-) create mode 100644 components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionStatistics.java create mode 100644 components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/SubscriptionStatisticDTO.java 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/SubscriptionStatistics.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionStatistics.java new file mode 100644 index 0000000000..80a10e98c5 --- /dev/null +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionStatistics.java @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package io.entgra.device.mgt.core.application.mgt.common; + +public class SubscriptionStatistics { + private float completedPercentage; + private float failedPercentage; + private float pendingPercentage; + private float newDevicesPercentage; + + public SubscriptionStatistics() {} + public SubscriptionStatistics(float completedPercentage, float failedPercentage, float pendingPercentage, + float newDevicesPercentage) { + this.completedPercentage = completedPercentage; + this.failedPercentage = failedPercentage; + this.pendingPercentage = pendingPercentage; + this.newDevicesPercentage = newDevicesPercentage; + } + + public float getCompletedPercentage() { + return completedPercentage; + } + + public void setCompletedPercentage(float completedPercentage) { + this.completedPercentage = completedPercentage; + } + + public float getFailedPercentage() { + return failedPercentage; + } + + public void setFailedPercentage(float failedPercentage) { + this.failedPercentage = failedPercentage; + } + + public float getPendingPercentage() { + return pendingPercentage; + } + + public void setPendingPercentage(float pendingPercentage) { + this.pendingPercentage = pendingPercentage; + } + + public float getNewDevicesPercentage() { + return newDevicesPercentage; + } + + public void setNewDevicesPercentage(float newDevicesPercentage) { + this.newDevicesPercentage = newDevicesPercentage; + } +} 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/dto/SubscriptionStatisticDTO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/SubscriptionStatisticDTO.java new file mode 100644 index 0000000000..1f02402fae --- /dev/null +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/SubscriptionStatisticDTO.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package io.entgra.device.mgt.core.application.mgt.common.dto; + +public class SubscriptionStatisticDTO { + private int completedDeviceCount; + private int pendingDevicesCount; + private int failedDevicesCount; + + public int getCompletedDeviceCount() { + return completedDeviceCount; + } + + public void setCompletedDeviceCount(int completedDeviceCount) { + this.completedDeviceCount = completedDeviceCount; + } + + public int getPendingDevicesCount() { + return pendingDevicesCount; + } + + public void setPendingDevicesCount(int pendingDevicesCount) { + this.pendingDevicesCount = pendingDevicesCount; + } + + public int getFailedDevicesCount() { + return failedDevicesCount; + } + + public void setFailedDevicesCount(int failedDevicesCount) { + this.failedDevicesCount = failedDevicesCount; + } +} 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/dao/SubscriptionDAO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java index 8924fa4733..e4b893f6ea 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java @@ -20,6 +20,7 @@ package io.entgra.device.mgt.core.application.mgt.core.dao; import io.entgra.device.mgt.core.application.mgt.common.ExecutionStatus; import io.entgra.device.mgt.core.application.mgt.common.dto.GroupSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity; +import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionStatisticDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionsDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationReleaseDTO; @@ -521,4 +522,8 @@ public interface SubscriptionDAO { * @throws ApplicationManagementDAOException if connection establishment or SQL execution fails. */ int getUserUnsubscriptionCount(int appReleaseId, int tenantId) throws ApplicationManagementDAOException; + + SubscriptionStatisticDTO getSubscriptionStatistic(String subscriptionType, boolean isUnsubscribed, int tenantId) + throws ApplicationManagementDAOException; + } 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/dao/impl/subscription/GenericSubscriptionDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java index b9a459635d..86a401b064 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java @@ -20,6 +20,7 @@ package io.entgra.device.mgt.core.application.mgt.core.dao.impl.subscription; import io.entgra.device.mgt.core.application.mgt.common.dto.GroupSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceOperationDTO; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity; +import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionStatisticDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionsDTO; import io.entgra.device.mgt.core.application.mgt.core.dao.SubscriptionDAO; import io.entgra.device.mgt.core.application.mgt.core.dao.impl.AbstractDAOImpl; @@ -47,6 +48,7 @@ import java.util.Calendar; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.StringJoiner; import java.util.stream.Collectors; @@ -2654,4 +2656,54 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc throw new ApplicationManagementDAOException(msg, e); } } + + @Override + public SubscriptionStatisticDTO getSubscriptionStatistic(String subscriptionType, boolean isUnsubscribed, int tenantId) + throws ApplicationManagementDAOException { + SubscriptionStatisticDTO subscriptionStatisticDTO = new SubscriptionStatisticDTO(); + boolean doesAllEntriesRequired = true; + try { + Connection connection = getDBConnection(); + String sql = "SELECT COUNT(DISTINCT ID) AS COUNT, " + + "STATUS FROM AP_DEVICE_SUBSCRIPTION WHERE " + + "TENANT_ID = ? AND UNSUBSCRIBED = ?"; + if (!Objects.equals(subscriptionType, "DEVICE")) { + sql = sql + " AND ACTION_TRIGGERED_FROM = ?"; + doesAllEntriesRequired = false; + } + sql = sql + " GROUP BY (STATUS)"; + + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + int idx = 1; + + preparedStatement.setInt(idx++, tenantId); + preparedStatement.setBoolean(idx++, isUnsubscribed); + if (!doesAllEntriesRequired) { + preparedStatement.setString(idx, subscriptionType); + } + try (ResultSet resultSet = preparedStatement.executeQuery()) { + while (resultSet.next()) { + int count = resultSet.getInt("COUNT"); + String status = resultSet.getString("STATUS"); + if (Objects.equals(status, "COMPLETED")) { + subscriptionStatisticDTO.setCompletedDeviceCount(count); + } else if (Objects.equals(status, "PENDING")) { + subscriptionStatisticDTO.setPendingDevicesCount(count); + } else { + subscriptionStatisticDTO.setFailedDevicesCount(count); + } + } + } + } + return subscriptionStatisticDTO; + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection for getting subscription statistics"; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while running SQL for getting subscription statistics"; + log.error(msg, e); + throw new ApplicationManagementDAOException(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/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 ca1077633f..61a6613249 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 @@ -23,11 +23,14 @@ import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscription; import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionFilterCriteria; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionData; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionStatistics; 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.core.util.HelperUtil; 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.exceptions.DeviceManagementException; +import org.jetbrains.annotations.NotNull; import java.sql.Timestamp; import java.util.ArrayList; @@ -63,11 +66,7 @@ public class SubscriptionManagementHelperUtil { deviceSubscription.setDeviceStatus(device.getEnrolmentInfo().getStatus().name()); deviceSubscription.setOwnershipType(device.getEnrolmentInfo().getOwnership().name()); deviceSubscription.setDateOfLastUpdate(new Timestamp(device.getEnrolmentInfo().getDateOfLastUpdate())); - SubscriptionData subscriptionData = new SubscriptionData(); - subscriptionData.setTriggeredBy(isUnsubscribed ? deviceSubscriptionDTO.getUnsubscribedBy() : - deviceSubscriptionDTO.getSubscribedBy()); - subscriptionData.setTriggeredAt(deviceSubscriptionDTO.getSubscribedTimestamp()); - subscriptionData.setSubscriptionType(deviceSubscriptionDTO.getStatus()); + SubscriptionData subscriptionData = getSubscriptionData(isUnsubscribed, deviceSubscriptionDTO); deviceSubscription.setSubscriptionData(subscriptionData); deviceSubscriptions.add(deviceSubscription); } @@ -75,6 +74,16 @@ public class SubscriptionManagementHelperUtil { return deviceSubscriptions; } + private static SubscriptionData getSubscriptionData(boolean isUnsubscribed, DeviceSubscriptionDTO deviceSubscriptionDTO) { + SubscriptionData subscriptionData = new SubscriptionData(); + subscriptionData.setTriggeredBy(isUnsubscribed ? deviceSubscriptionDTO.getUnsubscribedBy() : + deviceSubscriptionDTO.getSubscribedBy()); + subscriptionData.setTriggeredAt(deviceSubscriptionDTO.getSubscribedTimestamp()); + subscriptionData.setDeviceSubscriptionStatus(deviceSubscriptionDTO.getStatus()); + subscriptionData.setSubscriptionType(deviceSubscriptionDTO.getActionTriggeredFrom()); + return subscriptionData; + } + public static String getDeviceSubscriptionStatus(SubscriptionInfo subscriptionInfo) { return getDeviceSubscriptionStatus(subscriptionInfo.getDeviceSubscriptionFilterCriteria(). getFilteringDeviceSubscriptionStatus(), subscriptionInfo.getDeviceSubscriptionStatus()); @@ -84,4 +93,22 @@ public class SubscriptionManagementHelperUtil { return (deviceSubscriptionStatusFilter != null && !deviceSubscriptionStatusFilter.isEmpty()) ? deviceSubscriptionStatusFilter : deviceSubscriptionStatus; } + + public static SubscriptionStatistics getSubscriptionStatistics(SubscriptionStatisticDTO subscriptionStatisticDTO, int allDeviceCount) { + SubscriptionStatistics subscriptionStatistics = new SubscriptionStatistics(); + subscriptionStatistics.setCompletedPercentage( + getPercentage(subscriptionStatisticDTO.getCompletedDeviceCount(), allDeviceCount)); + subscriptionStatistics.setPendingPercentage( + getPercentage(subscriptionStatisticDTO.getPendingDevicesCount(), allDeviceCount)); + subscriptionStatistics.setFailedPercentage( + getPercentage(subscriptionStatisticDTO.getFailedDevicesCount(), allDeviceCount)); + return subscriptionStatistics; + } + + public static float getPercentage(int numerator, int denominator) { + if (denominator <= 0) { + return 0.0f; + } + return (float) numerator/denominator; + } } 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 a7edd0359e..a58dcad547 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 @@ -25,6 +25,7 @@ import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionFilter 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; @@ -135,7 +136,8 @@ public class DeviceBasedSubscriptionManagementHelperServiceImpl implements Subsc } @Override - public void getSubscriptionStatistics() throws ApplicationManagementException { - // todo: analytics engine + 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 59240df4d5..cd4e92d623 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 @@ -25,9 +25,11 @@ import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionFilter 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.ApplicationDTO; 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; @@ -44,6 +46,7 @@ import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProvide import io.entgra.device.mgt.core.device.mgt.core.service.GroupManagementProviderService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.jetbrains.annotations.NotNull; import org.wso2.carbon.context.PrivilegedCarbonContext; import java.util.List; @@ -126,7 +129,7 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, deviceIdsOwnByGroup, subscriptionInfo.getDeviceSubscriptionStatus(), subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), - null, limit, offset); + null, -1, -1); deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, subscriptionInfo.getDeviceSubscriptionStatus(), subscriptionInfo.getSubscriptionType(), @@ -179,7 +182,26 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr } @Override - public void getSubscriptionStatistics() throws ApplicationManagementException { - // todo: analytics engine + public SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo) + throws ApplicationManagementException { + 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 = HelperUtil.getGroupManagementProviderService().getDeviceCount(subscriptionInfo.getIdentifier()); + return SubscriptionManagementHelperUtil.getSubscriptionStatistics(subscriptionStatisticDTO, allDeviceCount); + } catch (ApplicationManagementDAOException e) { + String msg = "Error encountered while getting subscription statistics for group: " + subscriptionInfo.getIdentifier(); + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (GroupManagementException e) { + String msg = "Error encountered while getting device subscription for group: " + subscriptionInfo.getIdentifier(); + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } } } 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 eb5c13fb4c..2daad13bf2 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 @@ -25,6 +25,7 @@ import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionFilter 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.exception.ApplicationManagementException; @@ -179,7 +180,7 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri } @Override - public void getSubscriptionStatistics() throws ApplicationManagementException { - // todo: analytics engine + 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/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 ee982575d1..26c9134499 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 @@ -24,6 +24,7 @@ import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionFilter 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.exception.ApplicationManagementException; @@ -165,7 +166,8 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri } @Override - public void getSubscriptionStatistics() throws ApplicationManagementException { + public SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo) throws ApplicationManagementException { // todo: analytics engine + 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/service/SubscriptionManagementHelperService.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/service/SubscriptionManagementHelperService.java index 743f51b3bb..437fc6d9fa 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/service/SubscriptionManagementHelperService.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/service/SubscriptionManagementHelperService.java @@ -24,6 +24,7 @@ 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.DeviceSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionsDTO; import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException; @@ -42,5 +43,6 @@ public interface SubscriptionManagementHelperService { throws ApplicationManagementException; SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException; - void getSubscriptionStatistics() throws ApplicationManagementException; + SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo) + throws ApplicationManagementException; } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/GroupDAO.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/GroupDAO.java index c5d2d700cd..5da24ad91d 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/GroupDAO.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/GroupDAO.java @@ -488,4 +488,5 @@ public interface GroupDAO { int tenantId, String deviceOwner, String deviceName, String deviceStatus, int offset, int limit) throws GroupManagementDAOException; + int getDeviceCount(String groupName, int tenantId) throws GroupManagementDAOException; } \ No newline at end of file diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java index 416b6e6efd..5410aa1960 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 @@ -1556,4 +1556,29 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { throw new GroupManagementDAOException(msg, e); } } + + @Override + public int getDeviceCount(String groupName, int tenantId) throws GroupManagementDAOException { + int deviceCount = 0; + try { + 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 = ?"; + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setInt(1, tenantId); + preparedStatement.setString(2, groupName); + try (ResultSet resultSet = preparedStatement.executeQuery()) { + if (resultSet.next()) { + deviceCount = resultSet.getInt("COUNT"); + } + } + } + return deviceCount; + } catch (SQLException e) { + String msg = "Error occurred while retrieving device count for the group: " + groupName; + log.error(msg, e); + throw new GroupManagementDAOException(msg, e); + } + } } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderService.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderService.java index 3104cff169..89af7b66b8 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderService.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderService.java @@ -30,6 +30,7 @@ import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupAlreadyExistEx import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupManagementException; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupNotExistException; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.RoleDoesNotExistException; +import io.entgra.device.mgt.core.device.mgt.core.dao.GroupManagementDAOException; import io.entgra.device.mgt.core.device.mgt.core.dto.GroupDetailsDTO; import org.wso2.carbon.user.api.AuthorizationManager; import org.wso2.carbon.user.api.UserStoreManager; @@ -389,4 +390,6 @@ public interface GroupManagementProviderService { GroupDetailsDTO getGroupDetailsWithDevices(String groupName, int deviceTypeId, String deviceOwner, String deviceName, String deviceStatus, int offset, int limit) throws GroupManagementException; + int getDeviceCount(String groupName) throws GroupManagementException; + } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderServiceImpl.java index c2c00d55c0..36b79f150f 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderServiceImpl.java @@ -1725,4 +1725,18 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid return groupDetailsWithDevices; } + @Override + public int getDeviceCount(String groupName) throws GroupManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { + GroupManagementDAOFactory.openConnection(); + return groupDAO.getDeviceCount(groupName, tenantId); + } catch (SQLException | GroupManagementDAOException e) { + String msg = "Error occurred while retrieving device count."; + log.error(msg, e); + throw new GroupManagementException(msg, e); + } finally { + GroupManagementDAOFactory.closeConnection(); + } + } } -- 2.36.3 From 41d8c8ade89390694923c71c2ce04c9db27b1898 Mon Sep 17 00:00:00 2001 From: Rajitha Kumara Date: Sat, 20 Jul 2024 20:06:15 +0530 Subject: [PATCH 08/16] 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); -- 2.36.3 From 3596de1b2820b367ad0c8f787554552a070a8b00 Mon Sep 17 00:00:00 2001 From: Rajitha Kumara Date: Sun, 21 Jul 2024 19:27:33 +0530 Subject: [PATCH 09/16] Fix device count issues --- .../mgt/common/SubscriptionMetadata.java} | 18 ++- .../common/services/SubscriptionManager.java | 22 ++++ .../mgt/core/dao/SubscriptionDAO.java | 14 ++- .../GenericSubscriptionDAOImpl.java | 109 +++++++++++++++--- .../core/impl/SubscriptionManagerImpl.java | 28 ++++- .../mgt/SubscriptionManagementHelperUtil.java | 3 +- ...SubscriptionManagementServiceProvider.java | 22 ++-- .../mgt/bean/RoleBasedSubscriptionInfo.java | 61 ---------- ...bscriptionManagementHelperServiceImpl.java | 29 +++-- ...bscriptionManagementHelperServiceImpl.java | 58 +++++----- ...bscriptionManagementHelperServiceImpl.java | 45 +++++--- ...bscriptionManagementHelperServiceImpl.java | 43 ++++--- .../SubscriptionManagementHelperService.java | 10 +- .../core/device/mgt/core/dao/DeviceDAO.java | 3 + .../core/dao/impl/AbstractDeviceDAOImpl.java | 27 +++++ .../DeviceManagementProviderService.java | 3 + .../DeviceManagementProviderServiceImpl.java | 27 +++++ 17 files changed, 339 insertions(+), 183 deletions(-) rename 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/bean/DeviceSubscriptionStatus.java => io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionMetadata.java} (52%) delete mode 100644 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/bean/RoleBasedSubscriptionInfo.java 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/bean/DeviceSubscriptionStatus.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionMetadata.java similarity index 52% rename from 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/bean/DeviceSubscriptionStatus.java rename to components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionMetadata.java index 11cbb62386..6d1b128f42 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/bean/DeviceSubscriptionStatus.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionMetadata.java @@ -17,8 +17,20 @@ * */ -package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.bean; +package io.entgra.device.mgt.core.application.mgt.common; -public enum DeviceSubscriptionStatus { - COMPLETED, ERROR, NEW, SUBSCRIBED +public class SubscriptionMetadata { + public static final class DeviceSubscriptionStatus { + public static final String NEW = "NEW"; + public static final String PENDING = "PENDING"; + public static final String COMPLETED = "COMPLETED"; + public static final String FAILED = "FAILED"; + } + + public static final class SubscriptionTypes { + public static final String ROLE = "role"; + public static final String DEVICE = "device"; + public static final String GROUP = "group"; + public static final String USER = "user"; + } } 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 63274700fc..1e70f1b9d6 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 @@ -230,12 +230,34 @@ public interface SubscriptionManager { */ Activity getOperationAppDetails(String id) throws SubscriptionManagementException; + /** + * Get subscription data describes by {@link SubscriptionInfo} entity + * @param subscriptionInfo {@link SubscriptionInfo} + * @param limit Limit value + * @param offset Offset value + * @return {@link SubscriptionResponse} + * @throws ApplicationManagementException Throws when error encountered while getting subscription data + */ SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException; + /** + * Get status based subscription data describes by {@link SubscriptionInfo} entity + * @param subscriptionInfo {@link SubscriptionInfo} + * @param limit Limit value + * @param offset Offset value + * @return {@link SubscriptionResponse} + * @throws ApplicationManagementException Throws when error encountered while getting subscription data + */ SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException; + /** + * Get subscription statistics related data describes by the {@link SubscriptionInfo} + * @param subscriptionInfo {@link SubscriptionInfo} + * @return {@link SubscriptionStatistics} + * @throws ApplicationManagementException Throws when error encountered while getting statistics + */ SubscriptionStatistics getStatistics(SubscriptionInfo subscriptionInfo) throws ApplicationManagementException; /** 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/dao/SubscriptionDAO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java index e4b893f6ea..e7098c37a5 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java @@ -401,9 +401,9 @@ public interface SubscriptionDAO { */ List getSubscriptionDetailsByDeviceIds(int appReleaseId, boolean unsubscribe, int tenantId, List deviceIds, String actionStatus, String actionType, - String actionTriggeredBy, String tabActionStatus, - int limit, int offset) throws ApplicationManagementDAOException; - int getDeviceSubscriptionCount(int appReleaseId, boolean unsubscribe, int tenantId, String actionStatus, String actionType, + String actionTriggeredBy, int limit, int offset) throws ApplicationManagementDAOException; + int getDeviceSubscriptionCount(int appReleaseId, boolean unsubscribe, int tenantId, + List deviceIds, String actionStatus, String actionType, String actionTriggeredBy) throws ApplicationManagementDAOException; /** @@ -423,6 +423,10 @@ public interface SubscriptionDAO { List getAllSubscriptionsDetails(int appReleaseId, boolean unsubscribe, int tenantId, String actionStatus, String actionType, String actionTriggeredBy, int offset, int limit) throws ApplicationManagementDAOException; + int getAllSubscriptionsCount(int appReleaseId, boolean unsubscribe, int tenantId, + String actionStatus, String actionType, String actionTriggeredBy) + throws ApplicationManagementDAOException; + /** * This method is used to get the counts of all subscription types related to a UUID. * @@ -523,7 +527,7 @@ public interface SubscriptionDAO { */ int getUserUnsubscriptionCount(int appReleaseId, int tenantId) throws ApplicationManagementDAOException; - SubscriptionStatisticDTO getSubscriptionStatistic(String subscriptionType, boolean isUnsubscribed, int tenantId) - throws ApplicationManagementDAOException; + SubscriptionStatisticDTO getSubscriptionStatistic(List deviceIds, String subscriptionType, boolean isUnsubscribed, + int tenantId) throws ApplicationManagementDAOException; } 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/dao/impl/subscription/GenericSubscriptionDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java index 86a401b064..53e4409911 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java @@ -17,6 +17,7 @@ */ package io.entgra.device.mgt.core.application.mgt.core.dao.impl.subscription; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionMetadata; import io.entgra.device.mgt.core.application.mgt.common.dto.GroupSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceOperationDTO; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity; @@ -1916,8 +1917,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc @Override public List getSubscriptionDetailsByDeviceIds(int appReleaseId, boolean unsubscribe, int tenantId, List deviceIds, String actionStatus, String actionType, - String actionTriggeredBy, String tabActionStatus, - int limit, int offset) throws ApplicationManagementDAOException { + String actionTriggeredBy, int limit, int offset) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { log.debug("Getting device subscriptions for the application release id " + appReleaseId + " and device ids " + deviceIds + " from the database"); @@ -2017,15 +2017,16 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } @Override - public int getDeviceSubscriptionCount(int appReleaseId, boolean unsubscribe, int tenantId, String actionStatus, - String actionType, String actionTriggeredBy) throws ApplicationManagementDAOException { + public int getDeviceSubscriptionCount(int appReleaseId, boolean unsubscribe, int tenantId, + List deviceIds, String actionStatus, String actionType, + String actionTriggeredBy) throws ApplicationManagementDAOException { int deviceCount = 0; try { Connection conn = this.getDBConnection(); - StringBuilder sql = new StringBuilder("SELECT " - + "COUNT(DS.ID) AS DEVICE_COUNT " + StringBuilder sql = new StringBuilder("SELECT COUNT(DISTINCT DS.DM_DEVICE_ID) AS COUNT " + "FROM AP_DEVICE_SUBSCRIPTION DS " - + "WHERE DS.AP_APP_RELEASE_ID = ? AND DS.UNSUBSCRIBED = ? AND DS.TENANT_ID = ?"); + + "WHERE DS.AP_APP_RELEASE_ID = ? AND DS.UNSUBSCRIBED = ? AND DS.TENANT_ID = ? AND DS.DM_DEVICE_ID IN (" + + deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ") "); if (actionStatus != null && !actionStatus.isEmpty()) { sql.append(" AND DS.STATUS = ? "); @@ -2034,7 +2035,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc sql.append(" AND DS.ACTION_TRIGGERED_FROM = ? "); } if (actionTriggeredBy != null && !actionTriggeredBy.isEmpty()) { - sql.append(" AND DS.SUBSCRIBED_BY LIKE ? "); + sql.append(" AND DS.SUBSCRIBED_BY LIKE ?"); } try (PreparedStatement ps = conn.prepareStatement(sql.toString())) { @@ -2042,6 +2043,9 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc ps.setInt(paramIdx++, appReleaseId); ps.setBoolean(paramIdx++, unsubscribe); ps.setInt(paramIdx++, tenantId); + for (int i = 0; i < deviceIds.size(); i++) { + ps.setInt(paramIdx++, deviceIds.get(i)); + } if (actionStatus != null && !actionStatus.isEmpty()) { ps.setString(paramIdx++, actionStatus); @@ -2054,21 +2058,24 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } try (ResultSet rs = ps.executeQuery()) { + if (log.isDebugEnabled()) { + log.debug("Successfully retrieved device subscriptions for application release id " + + appReleaseId + " and device ids " + deviceIds); + } if (rs.next()) { - deviceCount = rs.getInt("DEVICE_COUNT"); + deviceCount = rs.getInt("COUNT"); } + return deviceCount; } - - return deviceCount; - } catch (SQLException e) { - String msg = "Error occurred while running SQL to get device subscription data for application ID"; + String msg = "Error occurred while running SQL to get device subscription data for application ID: " + appReleaseId + + " and device ids: " + deviceIds + "."; log.error(msg, e); throw new ApplicationManagementDAOException(msg, e); } } catch (DBConnectionException e) { String msg = "Error occurred while obtaining the DB connection for getting device subscriptions for " - + "application Id: " + appReleaseId; + + "application Id: " + appReleaseId + " and device ids: " + deviceIds + "."; log.error(msg, e); throw new ApplicationManagementDAOException(msg, e); } @@ -2265,6 +2272,72 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } } + @Override + public int getAllSubscriptionsCount(int appReleaseId, boolean unsubscribe, int tenantId, + String actionStatus, String actionType, String actionTriggeredBy) + throws ApplicationManagementDAOException { + int deviceCount = 0; + if (log.isDebugEnabled()) { + log.debug("Getting device subscriptions for the application release id " + appReleaseId + + " from the database"); + } + + String actionTriggeredColumn = unsubscribe ? "DS.UNSUBSCRIBED_BY" : "DS.SUBSCRIBED_BY"; + StringBuilder sql = new StringBuilder("SELECT COUNT(DISTINCT DS.DM_DEVICE_ID) AS COUNT " + + "FROM AP_DEVICE_SUBSCRIPTION DS " + + "WHERE DS.AP_APP_RELEASE_ID = ? AND DS.UNSUBSCRIBED = ? AND DS.TENANT_ID = ? "); + + if (actionStatus != null && !actionStatus.isEmpty()) { + sql.append(" AND DS.STATUS = ? "); + } + if (actionType != null && !actionType.isEmpty()) { + sql.append(" AND DS.ACTION_TRIGGERED_FROM = ? "); + } + if (actionTriggeredBy != null && !actionTriggeredBy.isEmpty()) { + sql.append(" AND ").append(actionTriggeredColumn).append(" LIKE ?"); + } + + try { + Connection conn = this.getDBConnection(); + try (PreparedStatement ps = conn.prepareStatement(sql.toString())) { + int paramIdx = 1; + ps.setInt(paramIdx++, appReleaseId); + ps.setBoolean(paramIdx++, unsubscribe); + ps.setInt(paramIdx++, tenantId); + + if (actionStatus != null && !actionStatus.isEmpty()) { + ps.setString(paramIdx++, actionStatus); + } + if (actionType != null && !actionType.isEmpty()) { + ps.setString(paramIdx++, actionType); + } + if (actionTriggeredBy != null && !actionTriggeredBy.isEmpty()) { + ps.setString(paramIdx++, "%" + actionTriggeredBy + "%"); + } + + try (ResultSet rs = ps.executeQuery()) { + if (log.isDebugEnabled()) { + log.debug("Successfully retrieved device subscriptions for application release id " + + appReleaseId); + } + if (rs.next()) { + deviceCount = rs.getInt("COUNT"); + } + } + return deviceCount; + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection for getting device subscription for " + + "application Id: " + appReleaseId + "."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while running SQL to get device subscription data for application ID: " + appReleaseId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } + @Override public int getAllSubscriptionCount(int appReleaseId, int tenantId) throws ApplicationManagementDAOException { @@ -2658,16 +2731,18 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } @Override - public SubscriptionStatisticDTO getSubscriptionStatistic(String subscriptionType, boolean isUnsubscribed, int tenantId) + public SubscriptionStatisticDTO getSubscriptionStatistic(List deviceIds, String subscriptionType, + boolean isUnsubscribed, int tenantId) throws ApplicationManagementDAOException { SubscriptionStatisticDTO subscriptionStatisticDTO = new SubscriptionStatisticDTO(); + String deviceIdsString = deviceIds.stream().map(String::valueOf).collect(Collectors.joining(",")); boolean doesAllEntriesRequired = true; try { Connection connection = getDBConnection(); String sql = "SELECT COUNT(DISTINCT ID) AS COUNT, " + "STATUS FROM AP_DEVICE_SUBSCRIPTION WHERE " + - "TENANT_ID = ? AND UNSUBSCRIBED = ?"; - if (!Objects.equals(subscriptionType, "DEVICE")) { + "TENANT_ID = ? AND UNSUBSCRIBED = ? AND DM_DEVICE_ID IN ("+ deviceIdsString + ")"; + if (!Objects.equals(subscriptionType, SubscriptionMetadata.SubscriptionTypes.DEVICE)) { sql = sql + " AND ACTION_TRIGGERED_FROM = ?"; doesAllEntriesRequired = false; } 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 b8ae361f6e..49552ea153 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 @@ -1698,6 +1698,14 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } } + /** + * Get subscription data describes by {@link SubscriptionInfo} entity + * @param subscriptionInfo {@link SubscriptionInfo} + * @param limit Limit value + * @param offset Offset value + * @return {@link SubscriptionResponse} + * @throws ApplicationManagementException Throws when error encountered while getting subscription data + */ @Override public SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { @@ -1706,6 +1714,14 @@ public class SubscriptionManagerImpl implements SubscriptionManager { return subscriptionManagementHelperService.getSubscriptions(subscriptionInfo, limit, offset); } + /** + * Get status based subscription data describes by {@link SubscriptionInfo} entity + * @param subscriptionInfo {@link SubscriptionInfo} + * @param limit Limit value + * @param offset Offset value + * @return {@link SubscriptionResponse} + * @throws ApplicationManagementException Throws when error encountered while getting subscription data + */ @Override public SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { @@ -1714,6 +1730,12 @@ public class SubscriptionManagerImpl implements SubscriptionManager { return subscriptionManagementHelperService.getStatusBaseSubscriptions(subscriptionInfo, limit, offset); } + /** + * Get subscription statistics related data describes by the {@link SubscriptionInfo} + * @param subscriptionInfo {@link SubscriptionInfo} + * @return {@link SubscriptionStatistics} + * @throws ApplicationManagementException Throws when error encountered while getting statistics + */ @Override public SubscriptionStatistics getStatistics(SubscriptionInfo subscriptionInfo) throws ApplicationManagementException { return SubscriptionManagementServiceProvider.getInstance().getSubscriptionManagementHelperService(subscriptionInfo). @@ -1791,13 +1813,9 @@ public class SubscriptionManagerImpl implements SubscriptionManager { List subscriptionCounts = new ArrayList<>(); subscriptionCounts.add(new CategorizedSubscriptionCountsDTO( - "All", + "Device", subscriptionDAO.getAllSubscriptionCount(appReleaseId, tenantId), subscriptionDAO.getAllUnsubscriptionCount(appReleaseId, tenantId))); - subscriptionCounts.add(new CategorizedSubscriptionCountsDTO( - "Device", - subscriptionDAO.getDeviceSubscriptionCount(appReleaseId, tenantId), - subscriptionDAO.getDeviceUnsubscriptionCount(appReleaseId, tenantId))); subscriptionCounts.add(new CategorizedSubscriptionCountsDTO( "Group", subscriptionDAO.getGroupSubscriptionCount(appReleaseId, tenantId), 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 9488181f91..e75ec5dd94 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 @@ -30,7 +30,6 @@ import io.entgra.device.mgt.core.application.mgt.core.util.HelperUtil; 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.exceptions.DeviceManagementException; -import org.jetbrains.annotations.NotNull; import java.sql.Timestamp; import java.util.ArrayList; @@ -87,7 +86,7 @@ public class SubscriptionManagementHelperUtil { public static String getDeviceSubscriptionStatus(SubscriptionInfo subscriptionInfo) { return getDeviceSubscriptionStatus(subscriptionInfo.getDeviceSubscriptionFilterCriteria(). - getFilteringDeviceSubscriptionStatus(), subscriptionInfo.getDeviceSubscriptionStatus()); + getFilteringDeviceSubscriptionStatus(), subscriptionInfo.getDeviceSubscriptionStatus()); } public static String getDeviceSubscriptionStatus(String deviceSubscriptionStatusFilter, String deviceSubscriptionStatus) { 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/SubscriptionManagementServiceProvider.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/SubscriptionManagementServiceProvider.java index 2769278735..61602d7317 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/SubscriptionManagementServiceProvider.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/SubscriptionManagementServiceProvider.java @@ -20,6 +20,7 @@ package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionMetadata; import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.impl.DeviceBasedSubscriptionManagementHelperServiceImpl; import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.impl.GroupBasedSubscriptionManagementHelperServiceImpl; import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.impl.RoleBasedSubscriptionManagementHelperServiceImpl; @@ -29,10 +30,7 @@ import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.serv import java.util.Objects; public class SubscriptionManagementServiceProvider { - private SubscriptionManagementServiceProvider() {} - - private static class SubscriptionManagementProviderServiceHolder { - private static final SubscriptionManagementServiceProvider INSTANCE = new SubscriptionManagementServiceProvider(); + private SubscriptionManagementServiceProvider() { } public static SubscriptionManagementServiceProvider getInstance() { @@ -44,10 +42,18 @@ public class SubscriptionManagementServiceProvider { } private SubscriptionManagementHelperService getSubscriptionManagementHelperService(String subscriptionType) { - if (Objects.equals(subscriptionType, "role")) return RoleBasedSubscriptionManagementHelperServiceImpl.getInstance(); - if (Objects.equals(subscriptionType, "group")) return GroupBasedSubscriptionManagementHelperServiceImpl.getInstance(); - if (Objects.equals(subscriptionType, "user")) return UserBasedSubscriptionManagementHelperServiceImpl.getInstance(); - if (Objects.equals(subscriptionType, "device")) return DeviceBasedSubscriptionManagementHelperServiceImpl.getInstance(); + if (Objects.equals(subscriptionType, SubscriptionMetadata.SubscriptionTypes.ROLE)) + return RoleBasedSubscriptionManagementHelperServiceImpl.getInstance(); + if (Objects.equals(subscriptionType, SubscriptionMetadata.SubscriptionTypes.GROUP)) + return GroupBasedSubscriptionManagementHelperServiceImpl.getInstance(); + if (Objects.equals(subscriptionType, SubscriptionMetadata.SubscriptionTypes.USER)) + return UserBasedSubscriptionManagementHelperServiceImpl.getInstance(); + if (Objects.equals(subscriptionType, SubscriptionMetadata.SubscriptionTypes.DEVICE)) + return DeviceBasedSubscriptionManagementHelperServiceImpl.getInstance(); throw new UnsupportedOperationException("Subscription type: " + subscriptionType + " not supports"); } + + private static class SubscriptionManagementProviderServiceHolder { + private static final SubscriptionManagementServiceProvider INSTANCE = new SubscriptionManagementServiceProvider(); + } } 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/bean/RoleBasedSubscriptionInfo.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/bean/RoleBasedSubscriptionInfo.java deleted file mode 100644 index 1e7385d173..0000000000 --- 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/bean/RoleBasedSubscriptionInfo.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. - * - * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.bean; - -public class RoleBasedSubscriptionInfo { - - private String applicationUUID; - private String roleName; - private String subscriptionStatus; - public static String TRIGGERED_FROM_VALUE = "role"; - private DeviceSubscriptionStatus deviceSubscriptionStatus; - - public String getApplicationUUID() { - return applicationUUID; - } - - public void setApplicationUUID(String applicationUUID) { - this.applicationUUID = applicationUUID; - } - - public String getRoleName() { - return roleName; - } - - public void setRoleName(String roleName) { - this.roleName = roleName; - } - - public String getSubscriptionStatus() { - return subscriptionStatus; - } - - public void setSubscriptionStatus(String subscriptionStatus) { - this.subscriptionStatus = subscriptionStatus; - } - - public DeviceSubscriptionStatus getDeviceSubscriptionStatus() { - return deviceSubscriptionStatus; - } - - public void setDeviceSubscriptionStatus(DeviceSubscriptionStatus deviceSubscriptionStatus) { - this.deviceSubscriptionStatus = deviceSubscriptionStatus; - } -} 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 584ed8a4ac..1963e8dcd7 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 @@ -22,6 +22,7 @@ package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.imp import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscription; import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionFilterCriteria; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionMetadata; 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; @@ -48,11 +49,10 @@ import java.util.stream.Collectors; public class DeviceBasedSubscriptionManagementHelperServiceImpl implements SubscriptionManagementHelperService { private static final Log log = LogFactory.getLog(DeviceBasedSubscriptionManagementHelperServiceImpl.class); - private DeviceBasedSubscriptionManagementHelperServiceImpl() {} - private static class DeviceBasedSubscriptionManagementHelperServiceImplHolder { - private static final DeviceBasedSubscriptionManagementHelperServiceImpl INSTANCE - = new DeviceBasedSubscriptionManagementHelperServiceImpl(); + + private DeviceBasedSubscriptionManagementHelperServiceImpl() { } + public static DeviceBasedSubscriptionManagementHelperServiceImpl getInstance() { return DeviceBasedSubscriptionManagementHelperServiceImpl.DeviceBasedSubscriptionManagementHelperServiceImplHolder.INSTANCE; } @@ -80,10 +80,10 @@ public class DeviceBasedSubscriptionManagementHelperServiceImpl implements Subsc DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); - if (Objects.equals("NEW", deviceSubscriptionStatus)) { + if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) { deviceSubscriptionDTOS = subscriptionDAO.getAllSubscriptionsDetails(applicationReleaseDTO. - getId(),isUnsubscribe, tenantId, null, subscriptionInfo.getSubscriptionType().toUpperCase(), - deviceSubscriptionFilterCriteria.getTriggeredBy(),-1, -1); + getId(), isUnsubscribe, tenantId, null, null, + deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); @@ -96,14 +96,14 @@ public class DeviceBasedSubscriptionManagementHelperServiceImpl implements Subsc deviceCount = deviceManagementProviderService.getDeviceCountNotInGivenIdList(deviceIdsOfSubscription); } else { deviceSubscriptionDTOS = subscriptionDAO.getAllSubscriptionsDetails(applicationReleaseDTO. - getId(),isUnsubscribe, tenantId, subscriptionInfo.getDeviceSubscriptionStatus(), subscriptionInfo. - getSubscriptionType().toUpperCase(), deviceSubscriptionFilterCriteria.getTriggeredBy(), offset, limit); + getId(), isUnsubscribe, tenantId, subscriptionInfo.getDeviceSubscriptionStatus(), null, + deviceSubscriptionFilterCriteria.getTriggeredBy(), offset, limit); - deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, - subscriptionInfo.getDeviceSubscriptionStatus(), subscriptionInfo.getSubscriptionType().toUpperCase(), + deviceCount = subscriptionDAO.getAllSubscriptionsCount(applicationReleaseDTO. + getId(), isUnsubscribe, tenantId, subscriptionInfo.getDeviceSubscriptionStatus(), null, deviceSubscriptionFilterCriteria.getTriggeredBy()); } - List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, + List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe); return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions); } catch (DeviceManagementException e) { @@ -131,4 +131,9 @@ public class DeviceBasedSubscriptionManagementHelperServiceImpl implements Subsc return null; } + private static class DeviceBasedSubscriptionManagementHelperServiceImplHolder { + private static final DeviceBasedSubscriptionManagementHelperServiceImpl INSTANCE + = new DeviceBasedSubscriptionManagementHelperServiceImpl(); + } + } 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 deaec45e43..3da06cc787 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 @@ -20,10 +20,10 @@ 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.SubscriptionMetadata; 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.ApplicationDTO; @@ -38,6 +38,7 @@ 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.exceptions.DeviceManagementException; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupManagementException; @@ -46,7 +47,6 @@ import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProvide import io.entgra.device.mgt.core.device.mgt.core.service.GroupManagementProviderService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.jetbrains.annotations.NotNull; import org.wso2.carbon.context.PrivilegedCarbonContext; import java.util.List; @@ -55,10 +55,8 @@ import java.util.stream.Collectors; public class GroupBasedSubscriptionManagementHelperServiceImpl implements SubscriptionManagementHelperService { private static final Log log = LogFactory.getLog(GroupBasedSubscriptionManagementHelperServiceImpl.class); - private GroupBasedSubscriptionManagementHelperServiceImpl() {} - private static class GroupBasedSubscriptionManagementHelperServiceImplHolder { - private static final GroupBasedSubscriptionManagementHelperServiceImpl INSTANCE - = new GroupBasedSubscriptionManagementHelperServiceImpl(); + + private GroupBasedSubscriptionManagementHelperServiceImpl() { } public static GroupBasedSubscriptionManagementHelperServiceImpl getInstance() { @@ -99,41 +97,41 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr GroupManagementProviderService groupManagementProviderService = HelperUtil.getGroupManagementProviderService(); GroupDetailsDTO groupDetailsDTO; - List deviceIdsOwnByGroup; - if (Objects.equals("NEW", deviceSubscriptionStatus)) { - groupDetailsDTO = groupManagementProviderService.getGroupDetailsWithDevices(subscriptionInfo.getIdentifier(), - applicationDTO.getDeviceTypeId(), deviceSubscriptionFilterCriteria.getOwner(), deviceSubscriptionFilterCriteria.getName(), - deviceSubscriptionFilterCriteria.getDeviceStatus(), -1, -1); - deviceIdsOwnByGroup = groupDetailsDTO.getDeviceIds(); + List allDeviceIdsOwnByGroup = groupManagementProviderService.getGroupDetailsWithDevices(subscriptionInfo.getIdentifier(), + applicationDTO.getDeviceTypeId(), deviceSubscriptionFilterCriteria.getOwner(), deviceSubscriptionFilterCriteria.getName(), + deviceSubscriptionFilterCriteria.getDeviceStatus(), -1, -1).getDeviceIds(); + if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) { deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIdsOwnByGroup, null, - subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), - null, -1, -1); + isUnsubscribe, tenantId, allDeviceIdsOwnByGroup, null, + subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); - List newDeviceIds = deviceManagementProviderService.getDevicesNotInGivenIdList(deviceIdsOfSubscription, + for (Integer deviceId : deviceIdsOfSubscription) { + allDeviceIdsOwnByGroup.remove(deviceId); + } + + List paginatedNewDeviceIds = deviceManagementProviderService.getDevicesInGivenIdList(allDeviceIdsOwnByGroup, new PaginationRequest(offset, limit)); - deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); + deviceSubscriptionDTOS = paginatedNewDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); - deviceCount = deviceManagementProviderService.getDeviceCountNotInGivenIdList(deviceIdsOfSubscription); + deviceCount = allDeviceIdsOwnByGroup.size(); } else { groupDetailsDTO = groupManagementProviderService.getGroupDetailsWithDevices(subscriptionInfo.getIdentifier(), applicationDTO.getDeviceTypeId(), deviceSubscriptionFilterCriteria.getOwner(), deviceSubscriptionFilterCriteria.getName(), deviceSubscriptionFilterCriteria.getDeviceStatus(), offset, limit); - deviceIdsOwnByGroup = groupDetailsDTO.getDeviceIds(); + List paginatedDeviceIdsOwnByGroup = groupDetailsDTO.getDeviceIds(); deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIdsOwnByGroup, subscriptionInfo.getDeviceSubscriptionStatus(), - subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), - null, -1, -1); + isUnsubscribe, tenantId, paginatedDeviceIdsOwnByGroup, subscriptionInfo.getDeviceSubscriptionStatus(), + subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); - deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, - subscriptionInfo.getDeviceSubscriptionStatus(), subscriptionInfo.getSubscriptionType(), - deviceSubscriptionFilterCriteria.getTriggeredBy()); + deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), + isUnsubscribe, tenantId, allDeviceIdsOwnByGroup, subscriptionInfo.getDeviceSubscriptionStatus(), + subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy()); } List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe); @@ -190,8 +188,11 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { ConnectionManagerUtil.openDBConnection(); + List devices = HelperUtil.getGroupManagementProviderService(). + getAllDevicesOfGroup(subscriptionInfo.getIdentifier(), false); + List deviceIdsOwnByGroup = devices.stream().map(Device::getId).collect(Collectors.toList()); SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO. - getSubscriptionStatistic(subscriptionInfo.getSubscriptionType(), isUnsubscribe, tenantId); + getSubscriptionStatistic(deviceIdsOwnByGroup, subscriptionInfo.getSubscriptionType(), isUnsubscribe, tenantId); int allDeviceCount = HelperUtil.getGroupManagementProviderService().getDeviceCount(subscriptionInfo.getIdentifier()); return SubscriptionManagementHelperUtil.getSubscriptionStatistics(subscriptionStatisticDTO, allDeviceCount); } catch (ApplicationManagementDAOException e) { @@ -206,4 +207,9 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr ConnectionManagerUtil.closeDBConnection(); } } + + private static class GroupBasedSubscriptionManagementHelperServiceImplHolder { + private static final GroupBasedSubscriptionManagementHelperServiceImpl INSTANCE + = new GroupBasedSubscriptionManagementHelperServiceImpl(); + } } 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 17f223e9d3..939b7227ff 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 @@ -23,6 +23,7 @@ import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscription; 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.SubscriptionMetadata; 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; @@ -56,10 +57,8 @@ import java.util.stream.Collectors; public class RoleBasedSubscriptionManagementHelperServiceImpl implements SubscriptionManagementHelperService { private static final Log log = LogFactory.getLog(RoleBasedSubscriptionManagementHelperServiceImpl.class); - private RoleBasedSubscriptionManagementHelperServiceImpl() {} - private static class RoleBasedSubscriptionManagementHelperServiceImplHolder { - private static final RoleBasedSubscriptionManagementHelperServiceImpl INSTANCE - = new RoleBasedSubscriptionManagementHelperServiceImpl(); + + private RoleBasedSubscriptionManagementHelperServiceImpl() { } public static RoleBasedSubscriptionManagementHelperServiceImpl getInstance() { @@ -91,28 +90,30 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); - if (Objects.equals("NEW", deviceSubscriptionStatus)) { + if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) { deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, deviceIdsOwnByRole, null, - subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), - null, -1, -1); + subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); - List newDeviceIds = deviceManagementProviderService.getDevicesNotInGivenIdList(deviceIdsOfSubscription, + for (Integer deviceId : deviceIdsOfSubscription) { + deviceIdsOwnByRole.remove(deviceId); + } + + List paginatedNewDeviceIds = deviceManagementProviderService.getDevicesInGivenIdList(deviceIdsOwnByRole, new PaginationRequest(offset, limit)); - deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); - deviceCount = deviceManagementProviderService.getDeviceCountNotInGivenIdList(deviceIdsOfSubscription); + deviceSubscriptionDTOS = paginatedNewDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); + deviceCount = deviceIdsOwnByRole.size(); } else { deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, deviceIdsOwnByRole, subscriptionInfo.getDeviceSubscriptionStatus(), - subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), - null, limit, offset); + subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), limit, offset); - deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, - subscriptionInfo.getDeviceSubscriptionStatus(), subscriptionInfo.getSubscriptionType(), - deviceSubscriptionFilterCriteria.getTriggeredBy()); + deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), + isUnsubscribe, tenantId, deviceIdsOwnByRole, subscriptionInfo.getDeviceSubscriptionStatus(), + subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy()); } List deviceSubscriptions = SubscriptionManagementHelperUtil. getDeviceSubscriptionData(deviceSubscriptionDTOS, @@ -151,7 +152,7 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri log.error(msg); throw new NotFoundException(msg); } - List subscriptionEntities = subscriptionDAO. + List subscriptionEntities = subscriptionDAO. getRoleSubscriptionsByAppReleaseID(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, offset, limit); int subscriptionCount = isUnsubscribe ? subscriptionDAO.getRoleUnsubscriptionCount(applicationReleaseDTO.getId(), tenantId) : subscriptionDAO.getRoleSubscriptionCount(applicationReleaseDTO.getId(), tenantId); @@ -171,9 +172,10 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { ConnectionManagerUtil.openDBConnection(); + List deviceIdsOwnByRole = getDeviceIdsOwnByRole(subscriptionInfo.getIdentifier(), tenantId); SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO. - getSubscriptionStatistic(subscriptionInfo.getSubscriptionType(), isUnsubscribe, tenantId); - int allDeviceCount = getDeviceIdsOwnByRole(subscriptionInfo.getIdentifier(), tenantId).size(); + getSubscriptionStatistic(deviceIdsOwnByRole, subscriptionInfo.getSubscriptionType(), isUnsubscribe, tenantId); + int allDeviceCount = deviceIdsOwnByRole.size(); return SubscriptionManagementHelperUtil.getSubscriptionStatistics(subscriptionStatisticDTO, allDeviceCount); } catch (DeviceManagementException | ApplicationManagementDAOException | UserStoreException e) { String msg = "Error encountered while getting subscription statistics for role: " + subscriptionInfo.getIdentifier(); @@ -198,9 +200,14 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri PaginationResult ownDeviceIds = HelperUtil.getDeviceManagementProviderService(). getAllDevicesIdList(paginationRequest); if (ownDeviceIds.getData() != null) { - deviceListOwnByRole.addAll((List)ownDeviceIds.getData()); + deviceListOwnByRole.addAll((List) ownDeviceIds.getData()); } } return deviceListOwnByRole.stream().map(Device::getId).collect(Collectors.toList()); } + + private static class RoleBasedSubscriptionManagementHelperServiceImplHolder { + private static final RoleBasedSubscriptionManagementHelperServiceImpl INSTANCE + = new RoleBasedSubscriptionManagementHelperServiceImpl(); + } } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/UserBasedSubscriptionManagementHelperServiceImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/UserBasedSubscriptionManagementHelperServiceImpl.java index 8b41019faa..1e67916804 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 @@ -23,6 +23,7 @@ import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscription; 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.SubscriptionMetadata; 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; @@ -53,11 +54,10 @@ import java.util.stream.Collectors; public class UserBasedSubscriptionManagementHelperServiceImpl implements SubscriptionManagementHelperService { private static final Log log = LogFactory.getLog(UserBasedSubscriptionManagementHelperServiceImpl.class); - private UserBasedSubscriptionManagementHelperServiceImpl() {} - private static class UserBasedSubscriptionManagementHelperServiceImplHolder { - private static final UserBasedSubscriptionManagementHelperServiceImpl INSTANCE - = new UserBasedSubscriptionManagementHelperServiceImpl(); + + private UserBasedSubscriptionManagementHelperServiceImpl() { } + public static UserBasedSubscriptionManagementHelperServiceImpl getInstance() { return UserBasedSubscriptionManagementHelperServiceImpl.UserBasedSubscriptionManagementHelperServiceImplHolder.INSTANCE; } @@ -87,29 +87,30 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); - if (Objects.equals("NEW", deviceSubscriptionStatus)) { + if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) { deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, deviceIdsOwnByUser, null, - subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), - null, -1, -1); + subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); - List newDeviceIds = deviceManagementProviderService.getDevicesNotInGivenIdList(deviceIdsOfSubscription, + for (Integer deviceId : deviceIdsOfSubscription) { + deviceIdsOwnByUser.remove(deviceId); + } + List paginatedNewDeviceIds = deviceManagementProviderService.getDevicesInGivenIdList(deviceIdsOwnByUser, new PaginationRequest(offset, limit)); - deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); + deviceSubscriptionDTOS = paginatedNewDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList()); - deviceCount = deviceManagementProviderService.getDeviceCountNotInGivenIdList(deviceIdsOfSubscription); + deviceCount = deviceIdsOwnByUser.size(); } else { deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, deviceIdsOwnByUser, subscriptionInfo.getDeviceSubscriptionStatus(), - subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), - null, limit, offset); + subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), limit, offset); - deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, - subscriptionInfo.getDeviceSubscriptionStatus(), subscriptionInfo.getSubscriptionType(), - deviceSubscriptionFilterCriteria.getTriggeredBy()); + deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), + isUnsubscribe, tenantId, deviceIdsOwnByUser, subscriptionInfo.getDeviceSubscriptionStatus(), + subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy()); } List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, @@ -163,9 +164,10 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { ConnectionManagerUtil.openDBConnection(); + List deviceIdsOwnByUser = getDeviceIdsOwnByUser(subscriptionInfo.getIdentifier()); SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO. - getSubscriptionStatistic(subscriptionInfo.getSubscriptionType(), isUnsubscribe, tenantId); - int allDeviceCount = getDeviceIdsOwnByUser(subscriptionInfo.getIdentifier()).size(); + getSubscriptionStatistic(deviceIdsOwnByUser, subscriptionInfo.getSubscriptionType(), isUnsubscribe, tenantId); + int allDeviceCount = deviceIdsOwnByUser.size(); return SubscriptionManagementHelperUtil.getSubscriptionStatistics(subscriptionStatisticDTO, allDeviceCount); } catch (DeviceManagementException | ApplicationManagementDAOException e) { String msg = "Error encountered while getting subscription statistics for user: " + subscriptionInfo.getIdentifier(); @@ -185,8 +187,13 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri PaginationResult ownDeviceIds = HelperUtil.getDeviceManagementProviderService(). getAllDevicesIdList(paginationRequest); if (ownDeviceIds.getData() != null) { - deviceListOwnByUser.addAll((List)ownDeviceIds.getData()); + deviceListOwnByUser.addAll((List) ownDeviceIds.getData()); } return deviceListOwnByUser.stream().map(Device::getId).collect(Collectors.toList()); } + + private static class UserBasedSubscriptionManagementHelperServiceImplHolder { + private static final UserBasedSubscriptionManagementHelperServiceImpl INSTANCE + = new UserBasedSubscriptionManagementHelperServiceImpl(); + } } 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/service/SubscriptionManagementHelperService.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/service/SubscriptionManagementHelperService.java index 437fc6d9fa..f869e282dd 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/service/SubscriptionManagementHelperService.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/service/SubscriptionManagementHelperService.java @@ -19,30 +19,26 @@ package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.service; -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.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.core.dao.ApplicationDAO; import io.entgra.device.mgt.core.application.mgt.core.dao.ApplicationReleaseDAO; import io.entgra.device.mgt.core.application.mgt.core.dao.SubscriptionDAO; import io.entgra.device.mgt.core.application.mgt.core.dao.common.ApplicationManagementDAOFactory; -import java.util.List; - public interface SubscriptionManagementHelperService { SubscriptionDAO subscriptionDAO = ApplicationManagementDAOFactory.getSubscriptionDAO(); ApplicationDAO applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO(); ApplicationReleaseDAO applicationReleaseDAO = ApplicationManagementDAOFactory.getApplicationReleaseDAO(); + SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException; + SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException; + SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo) throws ApplicationManagementException; } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/DeviceDAO.java index a071b0f6eb..422395e38e 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/DeviceDAO.java @@ -868,6 +868,9 @@ public interface DeviceDAO { List getDevicesNotInGivenIdList(PaginationRequest request, List deviceIds, int tenantId) throws DeviceManagementDAOException; + List getDevicesInGivenIdList(PaginationRequest request, List deviceIds, int tenantId) + throws DeviceManagementDAOException; + int getDeviceCountNotInGivenIdList(List deviceIds, int tenantId) throws DeviceManagementDAOException; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index 3c08335e7a..93d6c75fb0 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -3327,6 +3327,33 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } } + @Override + public List getDevicesInGivenIdList(PaginationRequest request, List deviceIds, int tenantId) + throws DeviceManagementDAOException { + List filteredDeviceIds = new ArrayList<>(); + String deviceIdStringList = deviceIds.stream().map(String::valueOf).collect(Collectors.joining(",")); + try { + Connection connection = getConnection(); + String sql = "SELECT ID AS DEVICE_ID FROM DM_DEVICE WHERE ID IN " + + "(" + deviceIdStringList + ") AND TENANT_ID = ? LIMIT ? OFFSET ?"; + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setInt(1, tenantId); + preparedStatement.setInt(2, request.getRowCount()); + preparedStatement.setInt(3, request.getStartIndex()); + try (ResultSet resultSet = preparedStatement.executeQuery()) { + while (resultSet.next()) { + filteredDeviceIds.add(resultSet.getInt("DEVICE_ID")); + } + } + return filteredDeviceIds; + } + } catch (SQLException e) { + String msg = "Error occurred while retrieving device ids in: " + filteredDeviceIds; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + } + @Override public int getDeviceCountNotInGivenIdList(List deviceIds, int tenantId) throws DeviceManagementDAOException { diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderService.java index 5a8439431f..fd43de69fd 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderService.java @@ -1155,6 +1155,9 @@ public interface DeviceManagementProviderService { List getDevicesNotInGivenIdList(List deviceIds, PaginationRequest paginationRequest) throws DeviceManagementException; + + List getDevicesInGivenIdList(List deviceIds, PaginationRequest paginationRequest) + throws DeviceManagementException; int getDeviceCountNotInGivenIdList(List deviceIds) throws DeviceManagementException; List getDevicesByDeviceIds(PaginationRequest paginationRequest, List deviceIds) diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index c5353ab84e..086bc4e509 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -5620,6 +5620,33 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } } + @Override + public List getDevicesInGivenIdList(List deviceIds, PaginationRequest paginationRequest) + throws DeviceManagementException { + + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + if (paginationRequest == null) { + String msg = "Received null for pagination request"; + log.error(msg); + throw new DeviceManagementException(msg); + } + + try { + DeviceManagementDAOFactory.openConnection(); + return deviceDAO.getDevicesInGivenIdList(paginationRequest, deviceIds, tenantId); + } catch (DeviceManagementDAOException e) { + String msg = "Error encountered while getting device ids"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (SQLException e) { + String msg = "Error encountered while getting the database connection"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + @Override public int getDeviceCountNotInGivenIdList(List deviceIds) throws DeviceManagementException { -- 2.36.3 From 023ce0f9ec3e8d1d576f256e403151d91dce5917 Mon Sep 17 00:00:00 2001 From: Rajitha Kumara Date: Mon, 22 Jul 2024 13:22:00 +0530 Subject: [PATCH 10/16] Fix issues in services --- .../mgt/common/SubscriptionMetadata.java | 34 ++++++- .../common/dto/SubscriptionStatisticDTO.java | 18 +++- .../mgt/core/dao/SubscriptionDAO.java | 8 +- .../GenericSubscriptionDAOImpl.java | 97 ++++++++++++++----- .../mgt/SubscriptionManagementHelperUtil.java | 5 + ...bscriptionManagementHelperServiceImpl.java | 5 +- ...bscriptionManagementHelperServiceImpl.java | 11 ++- ...bscriptionManagementHelperServiceImpl.java | 7 +- ...bscriptionManagementHelperServiceImpl.java | 11 ++- .../core/dao/impl/AbstractDeviceDAOImpl.java | 70 +++++++++---- .../DeviceManagementProviderServiceImpl.java | 1 - 11 files changed, 200 insertions(+), 67 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/SubscriptionMetadata.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionMetadata.java index 6d1b128f42..9e9a87d9a1 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionMetadata.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionMetadata.java @@ -19,12 +19,22 @@ package io.entgra.device.mgt.core.application.mgt.common; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + public class SubscriptionMetadata { public static final class DeviceSubscriptionStatus { public static final String NEW = "NEW"; public static final String PENDING = "PENDING"; public static final String COMPLETED = "COMPLETED"; - public static final String FAILED = "FAILED"; + public static final String ERROR = "ERROR"; + public static final String INVALID = "INVALID"; + public static final String UNAUTHORIZED = "UNAUTHORIZED"; + public static final String IN_PROGRESS = "IN_PROGRESS"; + public static final String REPEAT = "REPEAT"; } public static final class SubscriptionTypes { @@ -33,4 +43,26 @@ public class SubscriptionMetadata { public static final String GROUP = "group"; public static final String USER = "user"; } + + public static final class DBSubscriptionStatus { + public static final List COMPLETED_STATUS_LIST = + Collections.singletonList(DeviceSubscriptionStatus.COMPLETED); + public static final List ERROR_STATUS_LIST = + Arrays.asList(DeviceSubscriptionStatus.ERROR, DeviceSubscriptionStatus.INVALID, DeviceSubscriptionStatus.UNAUTHORIZED); + public static final List PENDING_STATUS_LIST = + Arrays.asList(DeviceSubscriptionStatus.PENDING, DeviceSubscriptionStatus.IN_PROGRESS, DeviceSubscriptionStatus.REPEAT); + } + + public static Map> deviceSubscriptionStatusToDBSubscriptionStatusMap; + static { + Map> statusMap = new HashMap<>(); + statusMap.put(DeviceSubscriptionStatus.COMPLETED, DBSubscriptionStatus.COMPLETED_STATUS_LIST); + statusMap.put(DeviceSubscriptionStatus.PENDING, DBSubscriptionStatus.PENDING_STATUS_LIST); + statusMap.put(DeviceSubscriptionStatus.IN_PROGRESS, DBSubscriptionStatus.PENDING_STATUS_LIST); + statusMap.put(DeviceSubscriptionStatus.REPEAT, DBSubscriptionStatus.PENDING_STATUS_LIST); + statusMap.put(DeviceSubscriptionStatus.ERROR, DBSubscriptionStatus.ERROR_STATUS_LIST); + statusMap.put(DeviceSubscriptionStatus.INVALID, DBSubscriptionStatus.ERROR_STATUS_LIST); + statusMap.put(DeviceSubscriptionStatus.UNAUTHORIZED, DBSubscriptionStatus.ERROR_STATUS_LIST); + deviceSubscriptionStatusToDBSubscriptionStatusMap = Collections.unmodifiableMap(statusMap); + } } 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/dto/SubscriptionStatisticDTO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/SubscriptionStatisticDTO.java index 1f02402fae..c50e47a5ac 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/SubscriptionStatisticDTO.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/SubscriptionStatisticDTO.java @@ -20,9 +20,21 @@ package io.entgra.device.mgt.core.application.mgt.common.dto; public class SubscriptionStatisticDTO { - private int completedDeviceCount; - private int pendingDevicesCount; - private int failedDevicesCount; + private int completedDeviceCount = 0; + private int pendingDevicesCount = 0; + private int failedDevicesCount = 0; + + public void addToComplete(int count) { + completedDeviceCount += count; + } + + public void addToPending(int count) { + pendingDevicesCount += count; + } + + public void addToFailed(int count) { + failedDevicesCount += count ; + } public int getCompletedDeviceCount() { return completedDeviceCount; 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/dao/SubscriptionDAO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java index e7098c37a5..d7a1d37d4a 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java @@ -400,10 +400,10 @@ public interface SubscriptionDAO { * @throws ApplicationManagementDAOException if connection establishment or SQL execution fails. */ List getSubscriptionDetailsByDeviceIds(int appReleaseId, boolean unsubscribe, int tenantId, - List deviceIds, String actionStatus, String actionType, + List deviceIds, List actionStatus, String actionType, String actionTriggeredBy, int limit, int offset) throws ApplicationManagementDAOException; int getDeviceSubscriptionCount(int appReleaseId, boolean unsubscribe, int tenantId, - List deviceIds, String actionStatus, String actionType, + List deviceIds, List actionStatus, String actionType, String actionTriggeredBy) throws ApplicationManagementDAOException; /** @@ -420,11 +420,11 @@ public interface SubscriptionDAO { * @return {@link DeviceOperationDTO} which contains the details of device subscriptions. * @throws ApplicationManagementDAOException if connection establishment or SQL execution fails. */ - List getAllSubscriptionsDetails(int appReleaseId, boolean unsubscribe, int tenantId, String actionStatus, String actionType, + List getAllSubscriptionsDetails(int appReleaseId, boolean unsubscribe, int tenantId, List actionStatus, String actionType, String actionTriggeredBy, int offset, int limit) throws ApplicationManagementDAOException; int getAllSubscriptionsCount(int appReleaseId, boolean unsubscribe, int tenantId, - String actionStatus, String actionType, String actionTriggeredBy) + List actionStatus, String actionType, String actionTriggeredBy) throws ApplicationManagementDAOException; /** 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/dao/impl/subscription/GenericSubscriptionDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java index 53e4409911..e2fd221943 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java @@ -46,6 +46,7 @@ import java.sql.Timestamp; import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -1914,14 +1915,20 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } } + // passed the required list for the action status @Override public List getSubscriptionDetailsByDeviceIds(int appReleaseId, boolean unsubscribe, int tenantId, - List deviceIds, String actionStatus, String actionType, + List deviceIds, List actionStatus, String actionType, String actionTriggeredBy, int limit, int offset) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { log.debug("Getting device subscriptions for the application release id " + appReleaseId + " and device ids " + deviceIds + " from the database"); } + + if (deviceIds == null || deviceIds.isEmpty()) { + return Collections.emptyList(); + } + try { Connection conn = this.getDBConnection(); String subscriptionStatusTime = unsubscribe ? "DS.UNSUBSCRIBED_TIMESTAMP" : "DS.SUBSCRIBED_TIMESTAMP"; @@ -1940,7 +1947,8 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ") "); if (actionStatus != null && !actionStatus.isEmpty()) { - sql.append(" AND DS.STATUS = ? "); + sql.append(" AND DS.STATUS IN ("). + append(actionStatus.stream().map(status -> "?").collect(Collectors.joining(","))).append(") "); } if (actionType != null && !actionType.isEmpty()) { sql.append(" AND DS.ACTION_TRIGGERED_FROM = ? "); @@ -1961,16 +1969,20 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc ps.setInt(paramIdx++, appReleaseId); ps.setBoolean(paramIdx++, unsubscribe); ps.setInt(paramIdx++, tenantId); - for (int i = 0; i < deviceIds.size(); i++) { - ps.setInt(paramIdx++, deviceIds.get(i)); + for (Integer deviceId : deviceIds) { + ps.setInt(paramIdx++, deviceId); } if (actionStatus != null && !actionStatus.isEmpty()) { - ps.setString(paramIdx++, actionStatus); + for (String status : actionStatus) { + ps.setString(paramIdx++, status); + } } + if (actionType != null && !actionType.isEmpty()) { ps.setString(paramIdx++, actionType); } + if (actionTriggeredBy != null && !actionTriggeredBy.isEmpty()) { ps.setString(paramIdx++, "%" + actionTriggeredBy + "%"); } @@ -2018,9 +2030,12 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc @Override public int getDeviceSubscriptionCount(int appReleaseId, boolean unsubscribe, int tenantId, - List deviceIds, String actionStatus, String actionType, + List deviceIds, List actionStatus, String actionType, String actionTriggeredBy) throws ApplicationManagementDAOException { int deviceCount = 0; + + if (deviceIds == null || deviceIds.isEmpty()) return deviceCount; + try { Connection conn = this.getDBConnection(); StringBuilder sql = new StringBuilder("SELECT COUNT(DISTINCT DS.DM_DEVICE_ID) AS COUNT " @@ -2029,8 +2044,10 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ") "); if (actionStatus != null && !actionStatus.isEmpty()) { - sql.append(" AND DS.STATUS = ? "); + sql.append(" AND DS.STATUS IN ("). + append(actionStatus.stream().map(status -> "?").collect(Collectors.joining(","))).append(") "); } + if (actionType != null && !actionType.isEmpty()) { sql.append(" AND DS.ACTION_TRIGGERED_FROM = ? "); } @@ -2043,16 +2060,20 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc ps.setInt(paramIdx++, appReleaseId); ps.setBoolean(paramIdx++, unsubscribe); ps.setInt(paramIdx++, tenantId); - for (int i = 0; i < deviceIds.size(); i++) { - ps.setInt(paramIdx++, deviceIds.get(i)); + for (Integer deviceId : deviceIds) { + ps.setInt(paramIdx++, deviceId); } if (actionStatus != null && !actionStatus.isEmpty()) { - ps.setString(paramIdx++, actionStatus); + for (String status : actionStatus) { + ps.setString(paramIdx++, status); + } } + if (actionType != null && !actionType.isEmpty()) { ps.setString(paramIdx++, actionType); } + if (actionTriggeredBy != null && !actionTriggeredBy.isEmpty()) { ps.setString(paramIdx, "%" + actionTriggeredBy + "%"); } @@ -2175,7 +2196,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc @Override public List getAllSubscriptionsDetails(int appReleaseId, boolean unsubscribe, int tenantId, - String actionStatus, String actionType, String actionTriggeredBy, + List actionStatus, String actionType, String actionTriggeredBy, int offset, int limit) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { log.debug("Getting device subscriptions for the application release id " + appReleaseId @@ -2198,8 +2219,10 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc + "WHERE DS.AP_APP_RELEASE_ID = ? AND DS.UNSUBSCRIBED = ? AND DS.TENANT_ID = ? "); if (actionStatus != null && !actionStatus.isEmpty()) { - sql.append(" AND DS.STATUS = ? "); + sql.append(" AND DS.STATUS IN ("). + append(actionStatus.stream().map(status -> "?").collect(Collectors.joining(","))).append(") "); } + if (actionType != null && !actionType.isEmpty()) { sql.append(" AND DS.ACTION_TRIGGERED_FROM = ? "); } @@ -2222,11 +2245,15 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc ps.setInt(paramIdx++, tenantId); if (actionStatus != null && !actionStatus.isEmpty()) { - ps.setString(paramIdx++, actionStatus); + for (String status : actionStatus) { + ps.setString(paramIdx++, status); + } } + if (actionType != null && !actionType.isEmpty()) { ps.setString(paramIdx++, actionType); } + if (actionTriggeredBy != null && !actionTriggeredBy.isEmpty()) { ps.setString(paramIdx++, "%" + actionTriggeredBy + "%"); } @@ -2274,7 +2301,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc @Override public int getAllSubscriptionsCount(int appReleaseId, boolean unsubscribe, int tenantId, - String actionStatus, String actionType, String actionTriggeredBy) + List actionStatus, String actionType, String actionTriggeredBy) throws ApplicationManagementDAOException { int deviceCount = 0; if (log.isDebugEnabled()) { @@ -2288,7 +2315,8 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc + "WHERE DS.AP_APP_RELEASE_ID = ? AND DS.UNSUBSCRIBED = ? AND DS.TENANT_ID = ? "); if (actionStatus != null && !actionStatus.isEmpty()) { - sql.append(" AND DS.STATUS = ? "); + sql.append(" AND DS.STATUS IN ("). + append(actionStatus.stream().map(status -> "?").collect(Collectors.joining(","))).append(") "); } if (actionType != null && !actionType.isEmpty()) { sql.append(" AND DS.ACTION_TRIGGERED_FROM = ? "); @@ -2306,8 +2334,11 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc ps.setInt(paramIdx++, tenantId); if (actionStatus != null && !actionStatus.isEmpty()) { - ps.setString(paramIdx++, actionStatus); + for (String status : actionStatus) { + ps.setString(paramIdx++, status); + } } + if (actionType != null && !actionType.isEmpty()) { ps.setString(paramIdx++, actionType); } @@ -2730,42 +2761,56 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } } + // todo: fixed the status @Override public SubscriptionStatisticDTO getSubscriptionStatistic(List deviceIds, String subscriptionType, boolean isUnsubscribed, int tenantId) throws ApplicationManagementDAOException { SubscriptionStatisticDTO subscriptionStatisticDTO = new SubscriptionStatisticDTO(); - String deviceIdsString = deviceIds.stream().map(String::valueOf).collect(Collectors.joining(",")); + if (deviceIds == null || deviceIds.isEmpty()) return subscriptionStatisticDTO; + boolean doesAllEntriesRequired = true; try { Connection connection = getDBConnection(); String sql = "SELECT COUNT(DISTINCT ID) AS COUNT, " + "STATUS FROM AP_DEVICE_SUBSCRIPTION WHERE " + - "TENANT_ID = ? AND UNSUBSCRIBED = ? AND DM_DEVICE_ID IN ("+ deviceIdsString + ")"; + "TENANT_ID = ? AND UNSUBSCRIBED = ? AND DM_DEVICE_ID IN ("+ + deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ")"; + if (!Objects.equals(subscriptionType, SubscriptionMetadata.SubscriptionTypes.DEVICE)) { - sql = sql + " AND ACTION_TRIGGERED_FROM = ?"; + sql += " AND ACTION_TRIGGERED_FROM = ?"; doesAllEntriesRequired = false; } - sql = sql + " GROUP BY (STATUS)"; + + sql += " GROUP BY (STATUS)"; try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { int idx = 1; preparedStatement.setInt(idx++, tenantId); preparedStatement.setBoolean(idx++, isUnsubscribed); + for (Integer deviceId : deviceIds) { + preparedStatement.setInt(idx++, deviceId); + } + if (!doesAllEntriesRequired) { preparedStatement.setString(idx, subscriptionType); } try (ResultSet resultSet = preparedStatement.executeQuery()) { while (resultSet.next()) { + // add the error and in progress int count = resultSet.getInt("COUNT"); String status = resultSet.getString("STATUS"); - if (Objects.equals(status, "COMPLETED")) { - subscriptionStatisticDTO.setCompletedDeviceCount(count); - } else if (Objects.equals(status, "PENDING")) { - subscriptionStatisticDTO.setPendingDevicesCount(count); - } else { - subscriptionStatisticDTO.setFailedDevicesCount(count); + + if (SubscriptionMetadata.DBSubscriptionStatus.COMPLETED_STATUS_LIST.contains(status)) { + subscriptionStatisticDTO.addToComplete(count); + } + + if (SubscriptionMetadata.DBSubscriptionStatus.PENDING_STATUS_LIST.contains(status)) { + subscriptionStatisticDTO.addToPending(count); + } + if (SubscriptionMetadata.DBSubscriptionStatus.ERROR_STATUS_LIST.contains(status)) { + subscriptionStatisticDTO.addToFailed(count); } } } 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 e75ec5dd94..0028e71581 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 @@ -23,6 +23,7 @@ import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscription; import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionFilterCriteria; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionData; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionMetadata; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionStatistics; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionStatisticDTO; @@ -115,4 +116,8 @@ public class SubscriptionManagementHelperUtil { } return ((float) numerator / (float) denominator) * 100; } + + public static List getDBSubscriptionStatus(String deviceSubscriptionStatus) { + return SubscriptionMetadata.deviceSubscriptionStatusToDBSubscriptionStatusMap.get(deviceSubscriptionStatus); + } } 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 1963e8dcd7..13ccecc0ee 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 @@ -79,6 +79,7 @@ public class DeviceBasedSubscriptionManagementHelperServiceImpl implements Subsc String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); + List dbSubscriptionStatus = SubscriptionManagementHelperUtil.getDBSubscriptionStatus(subscriptionInfo.getDeviceSubscriptionStatus()); if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) { deviceSubscriptionDTOS = subscriptionDAO.getAllSubscriptionsDetails(applicationReleaseDTO. @@ -96,11 +97,11 @@ public class DeviceBasedSubscriptionManagementHelperServiceImpl implements Subsc deviceCount = deviceManagementProviderService.getDeviceCountNotInGivenIdList(deviceIdsOfSubscription); } else { deviceSubscriptionDTOS = subscriptionDAO.getAllSubscriptionsDetails(applicationReleaseDTO. - getId(), isUnsubscribe, tenantId, subscriptionInfo.getDeviceSubscriptionStatus(), null, + getId(), isUnsubscribe, tenantId, dbSubscriptionStatus, null, deviceSubscriptionFilterCriteria.getTriggeredBy(), offset, limit); deviceCount = subscriptionDAO.getAllSubscriptionsCount(applicationReleaseDTO. - getId(), isUnsubscribe, tenantId, subscriptionInfo.getDeviceSubscriptionStatus(), null, + getId(), isUnsubscribe, tenantId, dbSubscriptionStatus, null, deviceSubscriptionFilterCriteria.getTriggeredBy()); } List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, 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 3da06cc787..0756495098 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 @@ -97,6 +97,7 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr GroupManagementProviderService groupManagementProviderService = HelperUtil.getGroupManagementProviderService(); GroupDetailsDTO groupDetailsDTO; + List dbSubscriptionStatus = SubscriptionManagementHelperUtil.getDBSubscriptionStatus(subscriptionInfo.getDeviceSubscriptionStatus()); List allDeviceIdsOwnByGroup = groupManagementProviderService.getGroupDetailsWithDevices(subscriptionInfo.getIdentifier(), applicationDTO.getDeviceTypeId(), deviceSubscriptionFilterCriteria.getOwner(), deviceSubscriptionFilterCriteria.getName(), @@ -105,7 +106,7 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) { deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, allDeviceIdsOwnByGroup, null, - subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); + null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); @@ -126,12 +127,12 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr List paginatedDeviceIdsOwnByGroup = groupDetailsDTO.getDeviceIds(); deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, paginatedDeviceIdsOwnByGroup, subscriptionInfo.getDeviceSubscriptionStatus(), - subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); + isUnsubscribe, tenantId, paginatedDeviceIdsOwnByGroup, dbSubscriptionStatus, + null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, allDeviceIdsOwnByGroup, subscriptionInfo.getDeviceSubscriptionStatus(), - subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy()); + isUnsubscribe, tenantId, allDeviceIdsOwnByGroup, dbSubscriptionStatus, + null, deviceSubscriptionFilterCriteria.getTriggeredBy()); } List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe); 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 939b7227ff..30a1dd38a6 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 @@ -89,11 +89,12 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); + List dbSubscriptionStatus = SubscriptionManagementHelperUtil.getDBSubscriptionStatus(subscriptionInfo.getDeviceSubscriptionStatus()); if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) { deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, deviceIdsOwnByRole, null, - subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); + null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); @@ -108,11 +109,11 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri deviceCount = deviceIdsOwnByRole.size(); } else { deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIdsOwnByRole, subscriptionInfo.getDeviceSubscriptionStatus(), + isUnsubscribe, tenantId, deviceIdsOwnByRole, dbSubscriptionStatus, subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), limit, offset); deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIdsOwnByRole, subscriptionInfo.getDeviceSubscriptionStatus(), + isUnsubscribe, tenantId, deviceIdsOwnByRole, dbSubscriptionStatus, subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy()); } List deviceSubscriptions = SubscriptionManagementHelperUtil. 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 1e67916804..830153d142 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 @@ -86,11 +86,12 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); + List dbSubscriptionStatus = SubscriptionManagementHelperUtil.getDBSubscriptionStatus(subscriptionInfo.getDeviceSubscriptionStatus()); if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) { deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, deviceIdsOwnByUser, null, - subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); + null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); @@ -105,12 +106,12 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri deviceCount = deviceIdsOwnByUser.size(); } else { deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIdsOwnByUser, subscriptionInfo.getDeviceSubscriptionStatus(), - subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), limit, offset); + isUnsubscribe, tenantId, deviceIdsOwnByUser, dbSubscriptionStatus, + null, deviceSubscriptionFilterCriteria.getTriggeredBy(), limit, offset); deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIdsOwnByUser, subscriptionInfo.getDeviceSubscriptionStatus(), - subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy()); + isUnsubscribe, tenantId, deviceIdsOwnByUser, dbSubscriptionStatus, + null, deviceSubscriptionFilterCriteria.getTriggeredBy()); } List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index 93d6c75fb0..946d1ec3c6 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -3304,15 +3304,28 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { public List getDevicesNotInGivenIdList(PaginationRequest request, List deviceIds, int tenantId) throws DeviceManagementDAOException { List filteredDeviceIds = new ArrayList<>(); - String deviceIdStringList = deviceIds.stream().map(String::valueOf).collect(Collectors.joining(",")); try { Connection connection = getConnection(); - String sql = "SELECT ID AS DEVICE_ID FROM DM_DEVICE WHERE ID NOT IN " + - "(" + deviceIdStringList + ") AND TENANT_ID = ? LIMIT ? OFFSET ?"; + String sql = "SELECT ID AS DEVICE_ID FROM DM_DEVICE WHERE TENANT_ID = ?"; + + if (deviceIds != null && !deviceIds.isEmpty()) { + sql += " AND ID NOT IN ( " + deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ")"; + } + + sql += " LIMIT ? OFFSET ?"; + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { - preparedStatement.setInt(1, tenantId); - preparedStatement.setInt(2, request.getRowCount()); - preparedStatement.setInt(3, request.getStartIndex()); + int paraIdx = 1; + preparedStatement.setInt(paraIdx++, tenantId); + + if (deviceIds != null && !deviceIds.isEmpty()) { + for (Integer deviceId : deviceIds) { + preparedStatement.setInt(paraIdx++, deviceId); + } + } + + preparedStatement.setInt(paraIdx++, request.getRowCount()); + preparedStatement.setInt(paraIdx, request.getStartIndex()); try (ResultSet resultSet = preparedStatement.executeQuery()) { while (resultSet.next()) { filteredDeviceIds.add(resultSet.getInt("DEVICE_ID")); @@ -3331,15 +3344,22 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { public List getDevicesInGivenIdList(PaginationRequest request, List deviceIds, int tenantId) throws DeviceManagementDAOException { List filteredDeviceIds = new ArrayList<>(); - String deviceIdStringList = deviceIds.stream().map(String::valueOf).collect(Collectors.joining(",")); + if (deviceIds == null || deviceIds.isEmpty()) return filteredDeviceIds; + + String deviceIdStringList = deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")); try { Connection connection = getConnection(); String sql = "SELECT ID AS DEVICE_ID FROM DM_DEVICE WHERE ID IN " + "(" + deviceIdStringList + ") AND TENANT_ID = ? LIMIT ? OFFSET ?"; try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { - preparedStatement.setInt(1, tenantId); - preparedStatement.setInt(2, request.getRowCount()); - preparedStatement.setInt(3, request.getStartIndex()); + int paraIdx = 1; + for (Integer deviceId : deviceIds) { + preparedStatement.setInt(paraIdx++, deviceId); + } + + preparedStatement.setInt(paraIdx++, tenantId); + preparedStatement.setInt(paraIdx++, request.getRowCount()); + preparedStatement.setInt(paraIdx, request.getStartIndex()); try (ResultSet resultSet = preparedStatement.executeQuery()) { while (resultSet.next()) { filteredDeviceIds.add(resultSet.getInt("DEVICE_ID")); @@ -3358,13 +3378,24 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { public int getDeviceCountNotInGivenIdList(List deviceIds, int tenantId) throws DeviceManagementDAOException { int deviceCount = 0; - String deviceIdStringList = deviceIds.stream().map(String::valueOf).collect(Collectors.joining(",")); try { Connection connection = getConnection(); - String sql = "SELECT COUNT(ID) AS COUNT FROM DM_DEVICE WHERE ID NOT IN " + - "(" + deviceIdStringList + ") AND TENANT_ID = ?"; + String sql = "SELECT COUNT(ID) AS COUNT FROM DM_DEVICE WHERE TENANT_ID = ?"; + + if (deviceIds != null && !deviceIds.isEmpty()) { + sql += " AND ID NOT IN ( " + deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ")"; + } + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { - preparedStatement.setInt(1, tenantId); + int paraIdx = 1; + preparedStatement.setInt(paraIdx++, tenantId); + + if (deviceIds != null && !deviceIds.isEmpty()) { + for (Integer deviceId : deviceIds) { + preparedStatement.setInt(paraIdx++, deviceId); + } + } + try (ResultSet resultSet = preparedStatement.executeQuery()) { if (resultSet.next()) { deviceCount = resultSet.getInt("COUNT"); @@ -3383,7 +3414,9 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { public List getDevicesByDeviceIds(PaginationRequest paginationRequest, List deviceIds, int tenantId) throws DeviceManagementDAOException { List devices = new ArrayList<>(); - String deviceIdStringList = deviceIds.stream().map(String::valueOf).collect(Collectors.joining(",")); + if (deviceIds == null || deviceIds.isEmpty()) return devices; + + String deviceIdStringList = deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")); boolean isOwnerProvided = false; boolean isDeviceStatusProvided = false; boolean isDeviceNameProvided = false; @@ -3409,11 +3442,14 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { isDeviceNameProvided = true; } - int parameterIdx = 1; try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { - + int parameterIdx = 1; preparedStatement.setInt(parameterIdx++, tenantId); + for (Integer deviceId : deviceIds) { + preparedStatement.setInt(parameterIdx++, deviceId); + } + if (isOwnerProvided) preparedStatement.setString(parameterIdx++, paginationRequest.getOwner()); if (isDeviceStatusProvided) diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 086bc4e509..6de1167704 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -5596,7 +5596,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public List getDevicesNotInGivenIdList(List deviceIds, PaginationRequest paginationRequest) throws DeviceManagementException { - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); if (paginationRequest == null) { String msg = "Received null for pagination request"; -- 2.36.3 From cd2ffa7cc5e989c5e9bacd77adb838fee7675759 Mon Sep 17 00:00:00 2001 From: Rajitha Kumara Date: Mon, 22 Jul 2024 15:12:54 +0530 Subject: [PATCH 11/16] Fix device count related issues --- .../mgt/common/DeviceSubscription.java | 9 +++ .../mgt/common/SubscriptionData.java | 9 +++ .../mgt/SubscriptionManagementHelperUtil.java | 16 +++- ...bscriptionManagementHelperServiceImpl.java | 9 +-- ...bscriptionManagementHelperServiceImpl.java | 17 ++--- ...bscriptionManagementHelperServiceImpl.java | 9 +-- ...bscriptionManagementHelperServiceImpl.java | 11 ++- .../core/device/mgt/core/dao/DeviceDAO.java | 3 + .../core/dao/impl/AbstractDeviceDAOImpl.java | 73 ++++++++++++++++++- .../DeviceManagementProviderService.java | 2 + .../DeviceManagementProviderServiceImpl.java | 26 +++++++ 11 files changed, 153 insertions(+), 31 deletions(-) diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/DeviceSubscription.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/DeviceSubscription.java index 7d4efd22d9..16e1d7553f 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/DeviceSubscription.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/DeviceSubscription.java @@ -23,6 +23,7 @@ import java.sql.Timestamp; public class DeviceSubscription { private int deviceId; + private int subscriptionId; private String deviceName; private String deviceIdentifier; private String deviceStatus; @@ -32,6 +33,14 @@ public class DeviceSubscription { private Timestamp dateOfLastUpdate; private SubscriptionData subscriptionData; + public int getSubscriptionId() { + return subscriptionId; + } + + public void setSubscriptionId(int subscriptionId) { + this.subscriptionId = subscriptionId; + } + public int getDeviceId() { return deviceId; } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionData.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionData.java index 16dad4467d..cef9a12508 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionData.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionData.java @@ -26,6 +26,7 @@ public class SubscriptionData { private String triggeredBy; private String subscriptionType; private Timestamp triggeredAt; + private int subscriptionId; public String getDeviceSubscriptionStatus() { return deviceSubscriptionStatus; @@ -58,4 +59,12 @@ public class SubscriptionData { public void setTriggeredAt(Timestamp triggeredAt) { this.triggeredAt = triggeredAt; } + + public int getSubscriptionId() { + return subscriptionId; + } + + public void setSubscriptionId(int subscriptionId) { + this.subscriptionId = subscriptionId; + } } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/SubscriptionManagementHelperUtil.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/SubscriptionManagementHelperUtil.java index 0028e71581..ebc2e7f7cc 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/SubscriptionManagementHelperUtil.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/SubscriptionManagementHelperUtil.java @@ -40,10 +40,10 @@ import java.util.stream.Collectors; public class SubscriptionManagementHelperUtil { public static List getDeviceSubscriptionData(List deviceSubscriptionDTOS, DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria, - boolean isUnsubscribed) + boolean isUnsubscribed, int limit, int offset) throws DeviceManagementException { List deviceIds = deviceSubscriptionDTOS.stream().map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); - PaginationRequest paginationRequest = new PaginationRequest(0, -1); + PaginationRequest paginationRequest = new PaginationRequest(offset, limit); paginationRequest.setDeviceName(deviceSubscriptionFilterCriteria.getName()); paginationRequest.setDeviceStatus(deviceSubscriptionFilterCriteria.getDeviceStatus()); paginationRequest.setOwner(deviceSubscriptionFilterCriteria.getOwner()); @@ -51,6 +51,17 @@ public class SubscriptionManagementHelperUtil { return populateDeviceData(deviceSubscriptionDTOS, devices, isUnsubscribed); } + public static int getTotalDeviceSubscriptionCount(List deviceSubscriptionDTOS, + DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria) + throws DeviceManagementException { + List deviceIds = deviceSubscriptionDTOS.stream().map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); + PaginationRequest paginationRequest = new PaginationRequest(-1, -1); + paginationRequest.setDeviceName(deviceSubscriptionFilterCriteria.getName()); + paginationRequest.setDeviceStatus(deviceSubscriptionFilterCriteria.getDeviceStatus()); + paginationRequest.setOwner(deviceSubscriptionFilterCriteria.getOwner()); + return HelperUtil.getDeviceManagementProviderService().getDeviceCountByDeviceIds(paginationRequest, deviceIds); + } + private static List populateDeviceData(List deviceSubscriptionDTOS, List devices, boolean isUnsubscribed) { List deviceSubscriptions = new ArrayList<>(); @@ -82,6 +93,7 @@ public class SubscriptionManagementHelperUtil { subscriptionData.setTriggeredAt(deviceSubscriptionDTO.getSubscribedTimestamp()); subscriptionData.setDeviceSubscriptionStatus(deviceSubscriptionDTO.getStatus()); subscriptionData.setSubscriptionType(deviceSubscriptionDTO.getActionTriggeredFrom()); + subscriptionData.setSubscriptionId(deviceSubscriptionDTO.getId()); return subscriptionData; } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/DeviceBasedSubscriptionManagementHelperServiceImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/DeviceBasedSubscriptionManagementHelperServiceImpl.java index 13ccecc0ee..51871d28fc 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/DeviceBasedSubscriptionManagementHelperServiceImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/DeviceBasedSubscriptionManagementHelperServiceImpl.java @@ -98,14 +98,13 @@ public class DeviceBasedSubscriptionManagementHelperServiceImpl implements Subsc } else { deviceSubscriptionDTOS = subscriptionDAO.getAllSubscriptionsDetails(applicationReleaseDTO. getId(), isUnsubscribe, tenantId, dbSubscriptionStatus, null, - deviceSubscriptionFilterCriteria.getTriggeredBy(), offset, limit); + deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); - deviceCount = subscriptionDAO.getAllSubscriptionsCount(applicationReleaseDTO. - getId(), isUnsubscribe, tenantId, dbSubscriptionStatus, null, - deviceSubscriptionFilterCriteria.getTriggeredBy()); + deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS, + subscriptionInfo.getDeviceSubscriptionFilterCriteria()); } List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe); + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, limit, offset); return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions); } catch (DeviceManagementException e) { String msg = "Error encountered while getting device details"; diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/GroupBasedSubscriptionManagementHelperServiceImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/GroupBasedSubscriptionManagementHelperServiceImpl.java index 0756495098..cac63111a6 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/GroupBasedSubscriptionManagementHelperServiceImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/GroupBasedSubscriptionManagementHelperServiceImpl.java @@ -99,11 +99,11 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr GroupDetailsDTO groupDetailsDTO; List dbSubscriptionStatus = SubscriptionManagementHelperUtil.getDBSubscriptionStatus(subscriptionInfo.getDeviceSubscriptionStatus()); - List allDeviceIdsOwnByGroup = groupManagementProviderService.getGroupDetailsWithDevices(subscriptionInfo.getIdentifier(), - applicationDTO.getDeviceTypeId(), deviceSubscriptionFilterCriteria.getOwner(), deviceSubscriptionFilterCriteria.getName(), - deviceSubscriptionFilterCriteria.getDeviceStatus(), -1, -1).getDeviceIds(); - if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) { + List allDeviceIdsOwnByGroup = groupManagementProviderService.getGroupDetailsWithDevices(subscriptionInfo.getIdentifier(), + applicationDTO.getDeviceTypeId(), deviceSubscriptionFilterCriteria.getOwner(), deviceSubscriptionFilterCriteria.getName(), + deviceSubscriptionFilterCriteria.getDeviceStatus(), -1, -1).getDeviceIds(); + deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, allDeviceIdsOwnByGroup, null, null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); @@ -130,12 +130,11 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr isUnsubscribe, tenantId, paginatedDeviceIdsOwnByGroup, dbSubscriptionStatus, null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); - deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, allDeviceIdsOwnByGroup, dbSubscriptionStatus, - null, deviceSubscriptionFilterCriteria.getTriggeredBy()); + deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS, + subscriptionInfo.getDeviceSubscriptionFilterCriteria()); } List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe); + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, limit, offset); return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions); } catch (GroupManagementException e) { String msg = "Error encountered while retrieving group details for group: " + subscriptionInfo.getIdentifier(); @@ -193,7 +192,7 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr getAllDevicesOfGroup(subscriptionInfo.getIdentifier(), false); List deviceIdsOwnByGroup = devices.stream().map(Device::getId).collect(Collectors.toList()); SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO. - getSubscriptionStatistic(deviceIdsOwnByGroup, subscriptionInfo.getSubscriptionType(), isUnsubscribe, tenantId); + getSubscriptionStatistic(deviceIdsOwnByGroup, null, isUnsubscribe, tenantId); int allDeviceCount = HelperUtil.getGroupManagementProviderService().getDeviceCount(subscriptionInfo.getIdentifier()); return SubscriptionManagementHelperUtil.getSubscriptionStatistics(subscriptionStatisticDTO, allDeviceCount); } catch (ApplicationManagementDAOException e) { diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/RoleBasedSubscriptionManagementHelperServiceImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/RoleBasedSubscriptionManagementHelperServiceImpl.java index 30a1dd38a6..9ed856c09e 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/RoleBasedSubscriptionManagementHelperServiceImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/RoleBasedSubscriptionManagementHelperServiceImpl.java @@ -112,13 +112,12 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri isUnsubscribe, tenantId, deviceIdsOwnByRole, dbSubscriptionStatus, subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), limit, offset); - deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIdsOwnByRole, dbSubscriptionStatus, - subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy()); + deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS, + subscriptionInfo.getDeviceSubscriptionFilterCriteria()); } List deviceSubscriptions = SubscriptionManagementHelperUtil. getDeviceSubscriptionData(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe); + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, limit, offset); return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions); } catch (UserStoreException e) { @@ -175,7 +174,7 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri ConnectionManagerUtil.openDBConnection(); List deviceIdsOwnByRole = getDeviceIdsOwnByRole(subscriptionInfo.getIdentifier(), tenantId); SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO. - getSubscriptionStatistic(deviceIdsOwnByRole, subscriptionInfo.getSubscriptionType(), isUnsubscribe, tenantId); + getSubscriptionStatistic(deviceIdsOwnByRole, null, isUnsubscribe, tenantId); int allDeviceCount = deviceIdsOwnByRole.size(); return SubscriptionManagementHelperUtil.getSubscriptionStatistics(subscriptionStatisticDTO, allDeviceCount); } catch (DeviceManagementException | ApplicationManagementDAOException | UserStoreException e) { diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/UserBasedSubscriptionManagementHelperServiceImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/UserBasedSubscriptionManagementHelperServiceImpl.java index 830153d142..8b548b2fbb 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/UserBasedSubscriptionManagementHelperServiceImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/UserBasedSubscriptionManagementHelperServiceImpl.java @@ -107,15 +107,14 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri } else { deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, deviceIdsOwnByUser, dbSubscriptionStatus, - null, deviceSubscriptionFilterCriteria.getTriggeredBy(), limit, offset); + null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); - deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIdsOwnByUser, dbSubscriptionStatus, - null, deviceSubscriptionFilterCriteria.getTriggeredBy()); + deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS, + subscriptionInfo.getDeviceSubscriptionFilterCriteria()); } List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe); + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, limit, offset); return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions); } catch (DeviceManagementException e) { String msg = "Error encountered while getting device details"; @@ -167,7 +166,7 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri ConnectionManagerUtil.openDBConnection(); List deviceIdsOwnByUser = getDeviceIdsOwnByUser(subscriptionInfo.getIdentifier()); SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO. - getSubscriptionStatistic(deviceIdsOwnByUser, subscriptionInfo.getSubscriptionType(), isUnsubscribe, tenantId); + getSubscriptionStatistic(deviceIdsOwnByUser, null, isUnsubscribe, tenantId); int allDeviceCount = deviceIdsOwnByUser.size(); return SubscriptionManagementHelperUtil.getSubscriptionStatistics(subscriptionStatisticDTO, allDeviceCount); } catch (DeviceManagementException | ApplicationManagementDAOException e) { diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/DeviceDAO.java index 422395e38e..3d646768fb 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/DeviceDAO.java @@ -876,4 +876,7 @@ public interface DeviceDAO { List getDevicesByDeviceIds(PaginationRequest paginationRequest, List deviceIds, int tenantId) throws DeviceManagementDAOException; + + int getDeviceCountByDeviceIds(PaginationRequest paginationRequest, List deviceIds, int tenantId) + throws DeviceManagementDAOException; } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index 946d1ec3c6..654977cc62 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -3428,7 +3428,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { "AND e.STATUS NOT IN ('DELETED', 'REMOVED')"; if (paginationRequest.getOwner() != null) { - sql = sql + " AND e.OWNER = ?"; + sql = sql + " AND e.OWNER LIKE ?"; isOwnerProvided = true; } @@ -3438,10 +3438,12 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } if (paginationRequest.getDeviceName() != null) { - sql = sql + " AND d.NAME = ?"; + sql = sql + " AND d.NAME LIKE ?"; isDeviceNameProvided = true; } + sql = sql + " LIMIT ? OFFSET ?"; + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { int parameterIdx = 1; preparedStatement.setInt(parameterIdx++, tenantId); @@ -3451,11 +3453,14 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } if (isOwnerProvided) - preparedStatement.setString(parameterIdx++, paginationRequest.getOwner()); + preparedStatement.setString(parameterIdx++, "%" + paginationRequest.getOwner() + "%"); if (isDeviceStatusProvided) preparedStatement.setString(parameterIdx++, paginationRequest.getDeviceStatus()); if (isDeviceNameProvided) - preparedStatement.setString(parameterIdx, paginationRequest.getDeviceName()); + preparedStatement.setString(parameterIdx++, "%" + paginationRequest.getDeviceName() + "%"); + + preparedStatement.setInt(parameterIdx++, paginationRequest.getRowCount()); + preparedStatement.setInt(parameterIdx, paginationRequest.getStartIndex()); try(ResultSet resultSet = preparedStatement.executeQuery()) { Device device; @@ -3483,4 +3488,64 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } } + @Override + public int getDeviceCountByDeviceIds(PaginationRequest paginationRequest, List deviceIds, int tenantId) + throws DeviceManagementDAOException { + int deviceCount = 0; + if (deviceIds == null || deviceIds.isEmpty()) return deviceCount; + + String deviceIdStringList = deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")); + boolean isOwnerProvided = false; + boolean isDeviceStatusProvided = false; + boolean isDeviceNameProvided = false; + try { + Connection connection = getConnection(); + String sql = "SELECT COUNT(DISTINCT e.DEVICE_ID) AS COUNT FROM DM_DEVICE d INNER JOIN DM_ENROLMENT e " + + "WHERE d.ID = e.DEVICE_ID AND d.TENANT_ID = ? AND e.DEVICE_ID IN (" + deviceIdStringList+ ") " + + "AND e.STATUS NOT IN ('DELETED', 'REMOVED')"; + + if (paginationRequest.getOwner() != null) { + sql = sql + " AND e.OWNER LIKE ?"; + isOwnerProvided = true; + } + + if (paginationRequest.getDeviceStatus() != null) { + sql = sql + " AND e.STATUS = ?"; + isDeviceStatusProvided = true; + } + + if (paginationRequest.getDeviceName() != null) { + sql = sql + " AND d.NAME LIKE ?"; + isDeviceNameProvided = true; + } + + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + int parameterIdx = 1; + preparedStatement.setInt(parameterIdx++, tenantId); + + for (Integer deviceId : deviceIds) { + preparedStatement.setInt(parameterIdx++, deviceId); + } + + if (isOwnerProvided) + preparedStatement.setString(parameterIdx++, "%" + paginationRequest.getOwner() + "%"); + if (isDeviceStatusProvided) + preparedStatement.setString(parameterIdx++, paginationRequest.getDeviceStatus()); + if (isDeviceNameProvided) + preparedStatement.setString(parameterIdx, "%" + paginationRequest.getDeviceName() + "%"); + + try(ResultSet resultSet = preparedStatement.executeQuery()) { + if (resultSet.next()) { + deviceCount = resultSet.getInt("COUNT"); + } + } + } + return deviceCount; + } catch (SQLException e) { + String msg = "Error occurred while retrieving device count for device ids in: " + deviceIds; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + } + } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderService.java index fd43de69fd..f95bbd3294 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderService.java @@ -1162,4 +1162,6 @@ public interface DeviceManagementProviderService { List getDevicesByDeviceIds(PaginationRequest paginationRequest, List deviceIds) throws DeviceManagementException; + public int getDeviceCountByDeviceIds(PaginationRequest paginationRequest, List deviceIds) + throws DeviceManagementException; } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 6de1167704..3fdeb263ea 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -5692,4 +5692,30 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.closeConnection(); } } + + @Override + public int getDeviceCountByDeviceIds(PaginationRequest paginationRequest, List deviceIds) + throws DeviceManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + if (paginationRequest == null) { + String msg = "Received null for pagination request"; + log.error(msg); + throw new DeviceManagementException(msg); + } + + try { + DeviceManagementDAOFactory.openConnection(); + return deviceDAO.getDeviceCountByDeviceIds(paginationRequest, deviceIds, tenantId); + } catch (DeviceManagementDAOException e) { + String msg = "Error encountered while getting devices for device ids in " + deviceIds; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (SQLException e) { + String msg = "Error encountered while getting the database connection"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } } -- 2.36.3 From 4fae9cf2e30e870a499c207cedfcec1c5aa016dc Mon Sep 17 00:00:00 2001 From: Rajitha Kumara Date: Mon, 22 Jul 2024 16:38:28 +0530 Subject: [PATCH 12/16] Add todo for device count issue --- .../core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index 654977cc62..99b942ca3a 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -3488,6 +3488,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } } + // todo: fix the join query @Override public int getDeviceCountByDeviceIds(PaginationRequest paginationRequest, List deviceIds, int tenantId) throws DeviceManagementDAOException { @@ -3500,8 +3501,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { boolean isDeviceNameProvided = false; try { Connection connection = getConnection(); - String sql = "SELECT COUNT(DISTINCT e.DEVICE_ID) AS COUNT FROM DM_DEVICE d INNER JOIN DM_ENROLMENT e " + - "WHERE d.ID = e.DEVICE_ID AND d.TENANT_ID = ? AND e.DEVICE_ID IN (" + deviceIdStringList+ ") " + + String sql = "SELECT COUNT(DISTINCT e.DEVICE_ID) FROM DM_ENROLMENT e " + + "WHERE e.TENANT_ID = ? AND e.DEVICE_ID IN (" + deviceIdStringList+ ") " + "AND e.STATUS NOT IN ('DELETED', 'REMOVED')"; if (paginationRequest.getOwner() != null) { -- 2.36.3 From 57199a5d893413978935d20ab33ec04af7a2def2 Mon Sep 17 00:00:00 2001 From: Rajitha Kumara Date: Mon, 22 Jul 2024 20:20:04 +0530 Subject: [PATCH 13/16] Fix device type filtering issue --- .../mgt/SubscriptionManagementHelperUtil.java | 6 ++++-- ...sedSubscriptionManagementHelperServiceImpl.java | 12 ++++++++++-- ...sedSubscriptionManagementHelperServiceImpl.java | 4 ++-- ...sedSubscriptionManagementHelperServiceImpl.java | 12 ++++++++++-- ...sedSubscriptionManagementHelperServiceImpl.java | 13 ++++++++++--- .../core/device/mgt/common/PaginationRequest.java | 9 +++++++++ .../mgt/core/dao/impl/AbstractDeviceDAOImpl.java | 14 +++++++++++--- 7 files changed, 56 insertions(+), 14 deletions(-) diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/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 ebc2e7f7cc..e5c3969329 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/SubscriptionManagementHelperUtil.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/SubscriptionManagementHelperUtil.java @@ -40,25 +40,27 @@ import java.util.stream.Collectors; public class SubscriptionManagementHelperUtil { public static List getDeviceSubscriptionData(List deviceSubscriptionDTOS, DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria, - boolean isUnsubscribed, int limit, int offset) + boolean isUnsubscribed, int deviceTypeId, int limit, int offset) throws DeviceManagementException { List deviceIds = deviceSubscriptionDTOS.stream().map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); PaginationRequest paginationRequest = new PaginationRequest(offset, limit); paginationRequest.setDeviceName(deviceSubscriptionFilterCriteria.getName()); paginationRequest.setDeviceStatus(deviceSubscriptionFilterCriteria.getDeviceStatus()); paginationRequest.setOwner(deviceSubscriptionFilterCriteria.getOwner()); + paginationRequest.setDeviceTypeId(deviceTypeId); List devices = HelperUtil.getDeviceManagementProviderService().getDevicesByDeviceIds(paginationRequest, deviceIds); return populateDeviceData(deviceSubscriptionDTOS, devices, isUnsubscribed); } public static int getTotalDeviceSubscriptionCount(List deviceSubscriptionDTOS, - DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria) + DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria, int deviceTypeId) throws DeviceManagementException { List deviceIds = deviceSubscriptionDTOS.stream().map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); PaginationRequest paginationRequest = new PaginationRequest(-1, -1); paginationRequest.setDeviceName(deviceSubscriptionFilterCriteria.getName()); paginationRequest.setDeviceStatus(deviceSubscriptionFilterCriteria.getDeviceStatus()); paginationRequest.setOwner(deviceSubscriptionFilterCriteria.getOwner()); + paginationRequest.setDeviceTypeId(deviceTypeId); return HelperUtil.getDeviceManagementProviderService().getDeviceCountByDeviceIds(paginationRequest, deviceIds); } 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 51871d28fc..b0fc7c97bd 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 @@ -25,6 +25,7 @@ import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionMetadata; 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.ApplicationDTO; 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.exception.ApplicationManagementException; @@ -76,6 +77,13 @@ public class DeviceBasedSubscriptionManagementHelperServiceImpl implements Subsc throw new NotFoundException(msg); } + ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(subscriptionInfo.getApplicationUUID(), tenantId); + if (applicationDTO == null) { + String msg = "Application not found for the release UUID: " + subscriptionInfo.getApplicationUUID(); + log.error(msg); + throw new NotFoundException(msg); + } + String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); @@ -101,10 +109,10 @@ public class DeviceBasedSubscriptionManagementHelperServiceImpl implements Subsc deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria()); + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), applicationDTO.getDeviceTypeId()); } List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, limit, offset); + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, applicationDTO.getDeviceTypeId(), limit, offset); return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions); } catch (DeviceManagementException e) { String msg = "Error encountered while getting device details"; diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/GroupBasedSubscriptionManagementHelperServiceImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/GroupBasedSubscriptionManagementHelperServiceImpl.java index cac63111a6..1a78df119c 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 @@ -131,10 +131,10 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria()); + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), applicationDTO.getDeviceTypeId()); } List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, limit, offset); + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, applicationDTO.getDeviceTypeId(), limit, offset); return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions); } catch (GroupManagementException e) { String msg = "Error encountered while retrieving group details for group: " + subscriptionInfo.getIdentifier(); diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/RoleBasedSubscriptionManagementHelperServiceImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/RoleBasedSubscriptionManagementHelperServiceImpl.java index 9ed856c09e..b5d527676e 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 @@ -26,6 +26,7 @@ import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionMetadata; 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.ApplicationDTO; 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; @@ -86,6 +87,13 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri throw new NotFoundException(msg); } + ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(subscriptionInfo.getApplicationUUID(), tenantId); + if (applicationDTO == null) { + String msg = "Application not found for the release UUID: " + subscriptionInfo.getApplicationUUID(); + log.error(msg); + throw new NotFoundException(msg); + } + String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); @@ -113,11 +121,11 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), limit, offset); deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria()); + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), applicationDTO.getDeviceTypeId()); } List deviceSubscriptions = SubscriptionManagementHelperUtil. getDeviceSubscriptionData(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, limit, offset); + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, applicationDTO.getDeviceTypeId(), limit, offset); return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions); } catch (UserStoreException e) { diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/UserBasedSubscriptionManagementHelperServiceImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/UserBasedSubscriptionManagementHelperServiceImpl.java index 8b548b2fbb..83bc8c08e2 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 @@ -26,6 +26,7 @@ import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionMetadata; 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.ApplicationDTO; 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; @@ -83,6 +84,13 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri throw new NotFoundException(msg); } + ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(subscriptionInfo.getApplicationUUID(), tenantId); + if (applicationDTO == null) { + String msg = "Application not found for the release UUID: " + subscriptionInfo.getApplicationUUID(); + log.error(msg); + throw new NotFoundException(msg); + } + String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); @@ -110,11 +118,10 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria()); + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), applicationDTO.getDeviceTypeId()); } - List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, limit, offset); + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, applicationDTO.getDeviceTypeId(), limit, offset); return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions); } catch (DeviceManagementException e) { String msg = "Error encountered while getting device details"; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/PaginationRequest.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/PaginationRequest.java index 95b8a92c6c..c4e01eb282 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/PaginationRequest.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/PaginationRequest.java @@ -54,6 +54,7 @@ public class PaginationRequest { private List statusList = new ArrayList<>(); private OperationLogFilters operationLogFilters = new OperationLogFilters(); private List sortColumn = new ArrayList<>(); + private int deviceTypeId; public OperationLogFilters getOperationLogFilters() { return operationLogFilters; } @@ -292,4 +293,12 @@ public class PaginationRequest { public void setTabActionStatus(String tabActionStatus) { this.tabActionStatus = tabActionStatus; } + + public int getDeviceTypeId() { + return deviceTypeId; + } + + public void setDeviceTypeId(int deviceTypeId) { + this.deviceTypeId = deviceTypeId; + } } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index 99b942ca3a..7ae29f29d8 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -3424,7 +3424,10 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { Connection connection = getConnection(); String sql = "SELECT e.DEVICE_ID, d.DEVICE_IDENTIFICATION, e.STATUS, e.OWNER, d.NAME AS DEVICE_NAME, " + "e.DEVICE_TYPE, e.OWNERSHIP, e.DATE_OF_LAST_UPDATE FROM DM_DEVICE d INNER JOIN DM_ENROLMENT e " + - "WHERE d.ID = e.DEVICE_ID AND d.TENANT_ID = ? AND e.DEVICE_ID IN (" + deviceIdStringList+ ") " + + "ON d.ID = e.DEVICE_ID " + + "WHERE d.DEVICE_TYPE_ID = ? " + + "AND d.TENANT_ID = ? " + + "AND e.DEVICE_ID IN (" + deviceIdStringList+ ") " + "AND e.STATUS NOT IN ('DELETED', 'REMOVED')"; if (paginationRequest.getOwner() != null) { @@ -3446,6 +3449,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { int parameterIdx = 1; + preparedStatement.setInt(parameterIdx++, paginationRequest.getDeviceTypeId()); preparedStatement.setInt(parameterIdx++, tenantId); for (Integer deviceId : deviceIds) { @@ -3501,8 +3505,11 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { boolean isDeviceNameProvided = false; try { Connection connection = getConnection(); - String sql = "SELECT COUNT(DISTINCT e.DEVICE_ID) FROM DM_ENROLMENT e " + - "WHERE e.TENANT_ID = ? AND e.DEVICE_ID IN (" + deviceIdStringList+ ") " + + String sql = "SELECT COUNT(DISTINCT e.DEVICE_ID) FROM DM_DEVICE d INNER JOIN DM_ENROLMENT e " + + "ON d.ID = e.DEVICE_ID " + + "WHERE e.TENANT_ID = ? " + + "AND e.DEVICE_ID IN (" + deviceIdStringList+ ") " + + "AND d.DEVICE_TYPE_ID = ? " + "AND e.STATUS NOT IN ('DELETED', 'REMOVED')"; if (paginationRequest.getOwner() != null) { @@ -3528,6 +3535,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { preparedStatement.setInt(parameterIdx++, deviceId); } + preparedStatement.setInt(parameterIdx++, paginationRequest.getDeviceTypeId()); if (isOwnerProvided) preparedStatement.setString(parameterIdx++, "%" + paginationRequest.getOwner() + "%"); if (isDeviceStatusProvided) -- 2.36.3 From 56139b708e1dadf61287c4b42acc6d10fab57a60 Mon Sep 17 00:00:00 2001 From: Rajitha Kumara Date: Mon, 22 Jul 2024 22:11:43 +0530 Subject: [PATCH 14/16] Fix syntax error --- .../core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index 7ae29f29d8..2565f2c2f8 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -3505,7 +3505,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { boolean isDeviceNameProvided = false; try { Connection connection = getConnection(); - String sql = "SELECT COUNT(DISTINCT e.DEVICE_ID) FROM DM_DEVICE d INNER JOIN DM_ENROLMENT e " + + String sql = "SELECT COUNT(DISTINCT e.DEVICE_ID) AS COUNT " + + "FROM DM_DEVICE d INNER JOIN DM_ENROLMENT e " + "ON d.ID = e.DEVICE_ID " + "WHERE e.TENANT_ID = ? " + "AND e.DEVICE_ID IN (" + deviceIdStringList+ ") " + -- 2.36.3 From 86db650952609cefc773b314d5e67e7aea94cbec Mon Sep 17 00:00:00 2001 From: Rajitha Kumara Date: Mon, 22 Jul 2024 22:42:55 +0530 Subject: [PATCH 15/16] Fix role pagination issue --- .../impl/RoleBasedSubscriptionManagementHelperServiceImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 b5d527676e..b3498db662 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 @@ -118,7 +118,7 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri } else { deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, deviceIdsOwnByRole, dbSubscriptionStatus, - subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), limit, offset); + subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS, subscriptionInfo.getDeviceSubscriptionFilterCriteria(), applicationDTO.getDeviceTypeId()); -- 2.36.3 From c1fa382335482a289b9f8b8f110b515fe326eb85 Mon Sep 17 00:00:00 2001 From: nipuni Date: Tue, 23 Jul 2024 00:29:26 +0530 Subject: [PATCH 16/16] Fix review comments. --- .../GenericSubscriptionDAOImpl.java | 79 ++-- .../OracleSubscriptionDAOImpl.java | 379 ++++++++++++++++++ .../mgt/SubscriptionManagementHelperUtil.java | 70 ++++ ...SubscriptionManagementServiceProvider.java | 16 +- .../core/dao/impl/AbstractDeviceDAOImpl.java | 32 +- .../dao/impl/device/GenericDeviceDAOImpl.java | 153 +++++++ .../dao/impl/device/OracleDeviceDAOImpl.java | 161 ++++++++ 7 files changed, 852 insertions(+), 38 deletions(-) diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java index e2fd221943..acd3f3906b 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java @@ -1656,10 +1656,16 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc List subscriptionEntities = new ArrayList<>(); String subscriptionStatusTime = unsubscribe ? "GS.UNSUBSCRIBED_TIMESTAMP" : "GS.SUBSCRIBED_TIMESTAMP"; - String sql = "SELECT GS.GROUP_NAME, GS.SUBSCRIBED_BY, GS.SUBSCRIBED_TIMESTAMP, GS.UNSUBSCRIBED, " + - "GS.UNSUBSCRIBED_BY, GS.UNSUBSCRIBED_TIMESTAMP, GS.AP_APP_RELEASE_ID " + + String sql = "SELECT GS.GROUP_NAME, " + + "GS.SUBSCRIBED_BY, " + + "GS.SUBSCRIBED_TIMESTAMP, " + + "GS.UNSUBSCRIBED, " + + "GS.UNSUBSCRIBED_BY, " + + "GS.UNSUBSCRIBED_TIMESTAMP, " + + "GS.AP_APP_RELEASE_ID " + "FROM AP_GROUP_SUBSCRIPTION GS " + - "WHERE GS.AP_APP_RELEASE_ID = ? AND GS.UNSUBSCRIBED = ? AND GS.TENANT_ID = ? " + + "WHERE GS.AP_APP_RELEASE_ID = ? " + + "AND GS.UNSUBSCRIBED = ? AND GS.TENANT_ID = ? " + "ORDER BY " + subscriptionStatusTime + " DESC " + "LIMIT ? OFFSET ?"; try (PreparedStatement ps = conn.prepareStatement(sql)) { @@ -1708,10 +1714,17 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc List subscriptionEntities = new ArrayList<>(); String subscriptionStatusTime = unsubscribe ? "US.UNSUBSCRIBED_TIMESTAMP" : "US.SUBSCRIBED_TIMESTAMP"; - String sql = "SELECT US.USER_NAME, US.SUBSCRIBED_BY, US.SUBSCRIBED_TIMESTAMP, US.UNSUBSCRIBED, " + - "US.UNSUBSCRIBED_BY, US.UNSUBSCRIBED_TIMESTAMP, US.AP_APP_RELEASE_ID " + + String sql = "SELECT US.USER_NAME, " + + "US.SUBSCRIBED_BY, " + + "US.SUBSCRIBED_TIMESTAMP, " + + "US.UNSUBSCRIBED, " + + "US.UNSUBSCRIBED_BY, " + + "US.UNSUBSCRIBED_TIMESTAMP, " + + "US.AP_APP_RELEASE_ID " + "FROM AP_USER_SUBSCRIPTION US " + - "WHERE US.AP_APP_RELEASE_ID = ? AND US.UNSUBSCRIBED = ? AND US.TENANT_ID = ? " + + "WHERE US.AP_APP_RELEASE_ID = ? " + + "AND US.UNSUBSCRIBED = ? " + + "AND US.TENANT_ID = ? " + "ORDER BY " + subscriptionStatusTime + " DESC " + "LIMIT ? OFFSET ?"; try (PreparedStatement ps = conn.prepareStatement(sql)) { @@ -1759,10 +1772,17 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc List subscriptionEntities = new ArrayList<>(); String subscriptionStatusTime = unsubscribe ? "ARS.UNSUBSCRIBED_TIMESTAMP" : "ARS.SUBSCRIBED_TIMESTAMP"; - String sql = "SELECT ARS.ROLE_NAME, ARS.SUBSCRIBED_BY, ARS.SUBSCRIBED_TIMESTAMP, ARS.UNSUBSCRIBED, " + - "ARS.UNSUBSCRIBED_BY, ARS.UNSUBSCRIBED_TIMESTAMP, ARS.AP_APP_RELEASE_ID " + + String sql = "SELECT ARS.ROLE_NAME, " + + "ARS.SUBSCRIBED_BY, " + + "ARS.SUBSCRIBED_TIMESTAMP, " + + "ARS.UNSUBSCRIBED, " + + "ARS.UNSUBSCRIBED_BY, " + + "ARS.UNSUBSCRIBED_TIMESTAMP, " + + "ARS.AP_APP_RELEASE_ID " + "FROM AP_ROLE_SUBSCRIPTION ARS " + - "WHERE ARS.AP_APP_RELEASE_ID = ? AND ARS.UNSUBSCRIBED = ? AND ARS.TENANT_ID = ? " + + "WHERE ARS.AP_APP_RELEASE_ID = ? " + + "AND ARS.UNSUBSCRIBED = ? " + + "AND ARS.TENANT_ID = ? " + "ORDER BY " + subscriptionStatusTime + " DESC " + "LIMIT ? OFFSET ?"; try (PreparedStatement ps = conn.prepareStatement(sql)) { @@ -1943,8 +1963,11 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc + "DS.STATUS AS STATUS, " + "DS.DM_DEVICE_ID AS DEVICE_ID " + "FROM AP_DEVICE_SUBSCRIPTION DS " - + "WHERE DS.AP_APP_RELEASE_ID = ? AND DS.UNSUBSCRIBED = ? AND DS.TENANT_ID = ? AND DS.DM_DEVICE_ID IN (" + - deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ") "); + + "WHERE DS.AP_APP_RELEASE_ID = ? " + + "AND DS.UNSUBSCRIBED = ? " + + "AND DS.TENANT_ID = ? " + + "AND DS.DM_DEVICE_ID IN (" + + deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ") "); if (actionStatus != null && !actionStatus.isEmpty()) { sql.append(" AND DS.STATUS IN ("). @@ -2040,8 +2063,11 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc Connection conn = this.getDBConnection(); StringBuilder sql = new StringBuilder("SELECT COUNT(DISTINCT DS.DM_DEVICE_ID) AS COUNT " + "FROM AP_DEVICE_SUBSCRIPTION DS " - + "WHERE DS.AP_APP_RELEASE_ID = ? AND DS.UNSUBSCRIBED = ? AND DS.TENANT_ID = ? AND DS.DM_DEVICE_ID IN (" + - deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ") "); + + "WHERE DS.AP_APP_RELEASE_ID = ? " + + "AND DS.UNSUBSCRIBED = ? " + + "AND DS.TENANT_ID = ? " + + "AND DS.DM_DEVICE_ID IN (" + + deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ") "); if (actionStatus != null && !actionStatus.isEmpty()) { sql.append(" AND DS.STATUS IN ("). @@ -2216,7 +2242,9 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc + "DS.STATUS AS STATUS, " + "DS.DM_DEVICE_ID AS DEVICE_ID " + "FROM AP_DEVICE_SUBSCRIPTION DS " - + "WHERE DS.AP_APP_RELEASE_ID = ? AND DS.UNSUBSCRIBED = ? AND DS.TENANT_ID = ? "); + + "WHERE DS.AP_APP_RELEASE_ID = ? " + + "AND DS.UNSUBSCRIBED = ? " + + "AND DS.TENANT_ID = ? "); if (actionStatus != null && !actionStatus.isEmpty()) { sql.append(" AND DS.STATUS IN ("). @@ -2301,18 +2329,18 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc @Override public int getAllSubscriptionsCount(int appReleaseId, boolean unsubscribe, int tenantId, - List actionStatus, String actionType, String actionTriggeredBy) + List actionStatus, String actionType, String actionTriggeredBy) throws ApplicationManagementDAOException { - int deviceCount = 0; if (log.isDebugEnabled()) { log.debug("Getting device subscriptions for the application release id " + appReleaseId + " from the database"); } - String actionTriggeredColumn = unsubscribe ? "DS.UNSUBSCRIBED_BY" : "DS.SUBSCRIBED_BY"; StringBuilder sql = new StringBuilder("SELECT COUNT(DISTINCT DS.DM_DEVICE_ID) AS COUNT " + "FROM AP_DEVICE_SUBSCRIPTION DS " - + "WHERE DS.AP_APP_RELEASE_ID = ? AND DS.UNSUBSCRIBED = ? AND DS.TENANT_ID = ? "); + + "WHERE DS.AP_APP_RELEASE_ID = ? " + + "AND DS.UNSUBSCRIBED = ? " + + "AND DS.TENANT_ID = ? "); if (actionStatus != null && !actionStatus.isEmpty()) { sql.append(" AND DS.STATUS IN ("). @@ -2324,7 +2352,6 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc if (actionTriggeredBy != null && !actionTriggeredBy.isEmpty()) { sql.append(" AND ").append(actionTriggeredColumn).append(" LIKE ?"); } - try { Connection conn = this.getDBConnection(); try (PreparedStatement ps = conn.prepareStatement(sql.toString())) { @@ -2338,24 +2365,19 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc ps.setString(paramIdx++, status); } } - if (actionType != null && !actionType.isEmpty()) { ps.setString(paramIdx++, actionType); } if (actionTriggeredBy != null && !actionTriggeredBy.isEmpty()) { ps.setString(paramIdx++, "%" + actionTriggeredBy + "%"); } - try (ResultSet rs = ps.executeQuery()) { if (log.isDebugEnabled()) { log.debug("Successfully retrieved device subscriptions for application release id " + appReleaseId); } - if (rs.next()) { - deviceCount = rs.getInt("COUNT"); - } + return rs.next() ? rs.getInt("COUNT") : 0; } - return deviceCount; } } catch (DBConnectionException e) { String msg = "Error occurred while obtaining the DB connection for getting device subscription for " @@ -2773,8 +2795,11 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc try { Connection connection = getDBConnection(); String sql = "SELECT COUNT(DISTINCT ID) AS COUNT, " + - "STATUS FROM AP_DEVICE_SUBSCRIPTION WHERE " + - "TENANT_ID = ? AND UNSUBSCRIBED = ? AND DM_DEVICE_ID IN ("+ + "STATUS " + + "FROM AP_DEVICE_SUBSCRIPTION " + + "WHERE TENANT_ID = ? " + + "AND UNSUBSCRIBED = ? " + + "AND DM_DEVICE_ID IN ("+ deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ")"; if (!Objects.equals(subscriptionType, SubscriptionMetadata.SubscriptionTypes.DEVICE)) { 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/dao/impl/subscription/OracleSubscriptionDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/OracleSubscriptionDAOImpl.java index 9181b9b4de..71bd17bbc9 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/OracleSubscriptionDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/OracleSubscriptionDAOImpl.java @@ -18,6 +18,8 @@ package io.entgra.device.mgt.core.application.mgt.core.dao.impl.subscription; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity; +import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import io.entgra.device.mgt.core.application.mgt.common.exception.DBConnectionException; @@ -28,7 +30,9 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; /** * This handles Application subscribing operations which are specific to Oracle. @@ -157,4 +161,379 @@ public class OracleSubscriptionDAOImpl extends GenericSubscriptionDAOImpl { throw new ApplicationManagementDAOException(msg, e); } } + + // passed the required list for the action status + @Override + public List getSubscriptionDetailsByDeviceIds(int appReleaseId, boolean unsubscribe, int tenantId, + List deviceIds, List actionStatus, String actionType, + String actionTriggeredBy, int limit, int offset) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Getting device subscriptions for the application release id " + appReleaseId + + " and device ids " + deviceIds + " from the database"); + } + if (deviceIds == null || deviceIds.isEmpty()) { + return Collections.emptyList(); + } + try { + Connection conn = this.getDBConnection(); + String subscriptionStatusTime = unsubscribe ? "DS.UNSUBSCRIBED_TIMESTAMP" : "DS.SUBSCRIBED_TIMESTAMP"; + StringBuilder sql = new StringBuilder("SELECT " + + "DS.ID AS ID, " + + "DS.SUBSCRIBED_BY AS SUBSCRIBED_BY, " + + "DS.SUBSCRIBED_TIMESTAMP AS SUBSCRIBED_AT, " + + "DS.UNSUBSCRIBED AS IS_UNSUBSCRIBED, " + + "DS.UNSUBSCRIBED_BY AS UNSUBSCRIBED_BY, " + + "DS.UNSUBSCRIBED_TIMESTAMP AS UNSUBSCRIBED_AT, " + + "DS.ACTION_TRIGGERED_FROM AS ACTION_TRIGGERED_FROM, " + + "DS.STATUS AS STATUS, " + + "DS.DM_DEVICE_ID AS DEVICE_ID " + + "FROM AP_DEVICE_SUBSCRIPTION DS " + + "WHERE DS.AP_APP_RELEASE_ID = ? " + + "AND DS.UNSUBSCRIBED = ? " + + "AND DS.TENANT_ID = ? " + + "AND DS.DM_DEVICE_ID IN (" + + deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ") "); + if (actionStatus != null && !actionStatus.isEmpty()) { + sql.append(" AND DS.STATUS IN ("). + append(actionStatus.stream().map(status -> "?").collect(Collectors.joining(","))).append(") "); + } + if (actionType != null && !actionType.isEmpty()) { + sql.append(" AND DS.ACTION_TRIGGERED_FROM = ? "); + } + if (actionTriggeredBy != null && !actionTriggeredBy.isEmpty()) { + sql.append(" AND DS.SUBSCRIBED_BY LIKE ? "); + } + sql.append("ORDER BY ").append(subscriptionStatusTime). + append(" DESC "); + if (offset >= 0 && limit >= 0) { + sql.append("OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"); + } + try (PreparedStatement ps = conn.prepareStatement(sql.toString())) { + int paramIdx = 1; + ps.setInt(paramIdx++, appReleaseId); + ps.setBoolean(paramIdx++, unsubscribe); + ps.setInt(paramIdx++, tenantId); + for (Integer deviceId : deviceIds) { + ps.setInt(paramIdx++, deviceId); + } + if (actionStatus != null && !actionStatus.isEmpty()) { + for (String status : actionStatus) { + ps.setString(paramIdx++, status); + } + } + if (actionType != null && !actionType.isEmpty()) { + ps.setString(paramIdx++, actionType); + } + if (actionTriggeredBy != null && !actionTriggeredBy.isEmpty()) { + ps.setString(paramIdx++, "%" + actionTriggeredBy + "%"); + } + if (offset >= 0 && limit >= 0) { + ps.setInt(paramIdx++, offset); + ps.setInt(paramIdx, limit); + } + try (ResultSet rs = ps.executeQuery()) { + if (log.isDebugEnabled()) { + log.debug("Successfully retrieved device subscriptions for application release id " + + appReleaseId + " and device ids " + deviceIds); + } + List subscriptions = new ArrayList<>(); + while (rs.next()) { + DeviceSubscriptionDTO subscription = new DeviceSubscriptionDTO(); + subscription.setId(rs.getInt("ID")); + subscription.setSubscribedBy(rs.getString("SUBSCRIBED_BY")); + subscription.setSubscribedTimestamp(rs.getTimestamp("SUBSCRIBED_AT")); + subscription.setUnsubscribed(rs.getBoolean("IS_UNSUBSCRIBED")); + subscription.setUnsubscribedBy(rs.getString("UNSUBSCRIBED_BY")); + subscription.setUnsubscribedTimestamp(rs.getTimestamp("UNSUBSCRIBED_AT")); + subscription.setActionTriggeredFrom(rs.getString("ACTION_TRIGGERED_FROM")); + subscription.setStatus(rs.getString("STATUS")); + subscription.setDeviceId(rs.getInt("DEVICE_ID")); + subscriptions.add(subscription); + } + return subscriptions; + } + } catch (SQLException e) { + String msg = "Error occurred while running SQL to get device subscription data for application ID: " + appReleaseId + + " and device ids: " + deviceIds + "."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection for getting device subscriptions for " + + "application Id: " + appReleaseId + " and device ids: " + deviceIds + "."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } + + @Override + public List getRoleSubscriptionsByAppReleaseID(int appReleaseId, boolean unsubscribe, int tenantId, int offset, + int limit) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to get role subscriptions related to the given AppReleaseID."); + } + try { + Connection conn = this.getDBConnection(); + List subscriptionEntities = new ArrayList<>(); + + String subscriptionStatusTime = unsubscribe ? "ARS.UNSUBSCRIBED_TIMESTAMP" : "ARS.SUBSCRIBED_TIMESTAMP"; + String sql = "SELECT ARS.ROLE_NAME, " + + "ARS.SUBSCRIBED_BY, " + + "ARS.SUBSCRIBED_TIMESTAMP, " + + "ARS.UNSUBSCRIBED, " + + "ARS.UNSUBSCRIBED_BY, " + + "ARS.UNSUBSCRIBED_TIMESTAMP, " + + "ARS.AP_APP_RELEASE_ID " + + "FROM AP_ROLE_SUBSCRIPTION ARS " + + "WHERE ARS.AP_APP_RELEASE_ID = ? " + + "AND ARS.UNSUBSCRIBED = ? " + + "AND ARS.TENANT_ID = ? " + + "ORDER BY " + subscriptionStatusTime + " DESC " + + "OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + + try (PreparedStatement ps = conn.prepareStatement(sql)) { + ps.setInt(1, appReleaseId); + ps.setBoolean(2, unsubscribe); + ps.setInt(3, tenantId); + ps.setInt(4, offset); + ps.setInt(5, limit); + try (ResultSet rs = ps.executeQuery()) { + SubscriptionEntity subscriptionEntity; + while (rs.next()) { + subscriptionEntity = new SubscriptionEntity(); + subscriptionEntity.setIdentity(rs.getString("ROLE_NAME")); + subscriptionEntity.setSubscribedBy(rs.getString("SUBSCRIBED_BY")); + subscriptionEntity.setSubscribedTimestamp(rs.getTimestamp("SUBSCRIBED_TIMESTAMP")); + subscriptionEntity.setUnsubscribed(rs.getBoolean("UNSUBSCRIBED")); + subscriptionEntity.setUnsubscribedBy(rs.getString("UNSUBSCRIBED_BY")); + subscriptionEntity.setUnsubscribedTimestamp(rs.getTimestamp("UNSUBSCRIBED_TIMESTAMP")); + subscriptionEntity.setApplicationReleaseId(rs.getInt("AP_APP_RELEASE_ID")); + + subscriptionEntities.add(subscriptionEntity); + } + } + return subscriptionEntities; + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection to get role subscriptions for the given UUID."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "SQL Error occurred while getting role subscriptions for the given UUID."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } + + @Override + public List getUserSubscriptionsByAppReleaseID(int appReleaseId, boolean unsubscribe, int tenantId, + int offset, int limit) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to get user subscriptions related to the given UUID."); + } + try { + Connection conn = this.getDBConnection(); + List subscriptionEntities = new ArrayList<>(); + + String subscriptionStatusTime = unsubscribe ? "US.UNSUBSCRIBED_TIMESTAMP" : "US.SUBSCRIBED_TIMESTAMP"; + String sql = "SELECT US.USER_NAME, " + + "US.SUBSCRIBED_BY, " + + "US.SUBSCRIBED_TIMESTAMP, " + + "US.UNSUBSCRIBED, " + + "US.UNSUBSCRIBED_BY, " + + "US.UNSUBSCRIBED_TIMESTAMP, " + + "US.AP_APP_RELEASE_ID " + + "FROM AP_USER_SUBSCRIPTION US " + + "WHERE US.AP_APP_RELEASE_ID = ? " + + "AND US.UNSUBSCRIBED = ? " + + "AND US.TENANT_ID = ? " + + "ORDER BY " + subscriptionStatusTime + " DESC " + + "OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + + try (PreparedStatement ps = conn.prepareStatement(sql)) { + ps.setInt(1, appReleaseId); + ps.setBoolean(2, unsubscribe); + ps.setInt(3, tenantId); + ps.setInt(4, offset); + ps.setInt(5, limit); + try (ResultSet rs = ps.executeQuery()) { + SubscriptionEntity subscriptionEntity; + while (rs.next()) { + subscriptionEntity = new SubscriptionEntity(); + subscriptionEntity.setIdentity(rs.getString("USER_NAME")); + subscriptionEntity.setSubscribedBy(rs.getString("SUBSCRIBED_BY")); + subscriptionEntity.setSubscribedTimestamp(rs.getTimestamp("SUBSCRIBED_TIMESTAMP")); + subscriptionEntity.setUnsubscribed(rs.getBoolean("UNSUBSCRIBED")); + subscriptionEntity.setUnsubscribedBy(rs.getString("UNSUBSCRIBED_BY")); + subscriptionEntity.setUnsubscribedTimestamp(rs.getTimestamp("UNSUBSCRIBED_TIMESTAMP")); + subscriptionEntity.setApplicationReleaseId(rs.getInt("AP_APP_RELEASE_ID")); + + subscriptionEntities.add(subscriptionEntity); + } + } + return subscriptionEntities; + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection to get user subscriptions for the given UUID."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "SQL Error occurred while getting user subscriptions for the given UUID."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } + + @Override + public List getGroupsSubscriptionDetailsByAppReleaseID(int appReleaseId, boolean unsubscribe, int tenantId, int offset, int limit) + throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to get groups related to the given AppReleaseID."); + } + try { + Connection conn = this.getDBConnection(); + List subscriptionEntities = new ArrayList<>(); + + String subscriptionStatusTime = unsubscribe ? "GS.UNSUBSCRIBED_TIMESTAMP" : "GS.SUBSCRIBED_TIMESTAMP"; + String sql = "SELECT GS.GROUP_NAME, " + + "GS.SUBSCRIBED_BY, " + + "GS.SUBSCRIBED_TIMESTAMP, " + + "GS.UNSUBSCRIBED, " + + "GS.UNSUBSCRIBED_BY, " + + "GS.UNSUBSCRIBED_TIMESTAMP, " + + "GS.AP_APP_RELEASE_ID " + + "FROM AP_GROUP_SUBSCRIPTION GS " + + "WHERE GS.AP_APP_RELEASE_ID = ? " + + "AND GS.UNSUBSCRIBED = ? AND GS.TENANT_ID = ? " + + "ORDER BY " + subscriptionStatusTime + " DESC " + + "OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + + try (PreparedStatement ps = conn.prepareStatement(sql)) { + ps.setInt(1, appReleaseId); + ps.setBoolean(2, unsubscribe); + ps.setInt(3, tenantId); + ps.setInt(4, offset); + ps.setInt(5, limit); + + try (ResultSet rs = ps.executeQuery()) { + SubscriptionEntity subscriptionEntity; + while (rs.next()) { + subscriptionEntity = new SubscriptionEntity(); + subscriptionEntity.setIdentity(rs.getString("GROUP_NAME")); + subscriptionEntity.setSubscribedBy(rs.getString("SUBSCRIBED_BY")); + subscriptionEntity.setSubscribedTimestamp(rs.getTimestamp("SUBSCRIBED_TIMESTAMP")); + subscriptionEntity.setUnsubscribed(rs.getBoolean("UNSUBSCRIBED")); + subscriptionEntity.setUnsubscribedBy(rs.getString("UNSUBSCRIBED_BY")); + subscriptionEntity.setUnsubscribedTimestamp(rs.getTimestamp("UNSUBSCRIBED_TIMESTAMP")); + subscriptionEntity.setApplicationReleaseId(rs.getInt("AP_APP_RELEASE_ID")); + + subscriptionEntities.add(subscriptionEntity); + } + } + return subscriptionEntities; + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection to get groups for the given UUID."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "SQL Error occurred while getting groups for the given UUID."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } + + @Override + public List getAllSubscriptionsDetails(int appReleaseId, boolean unsubscribe, int tenantId, + List actionStatus, String actionType, String actionTriggeredBy, + int offset, int limit) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Getting device subscriptions for the application release id " + appReleaseId + " from the database"); + } + String subscriptionStatusTime = unsubscribe ? "DS.UNSUBSCRIBED_TIMESTAMP" : "DS.SUBSCRIBED_TIMESTAMP"; + String actionTriggeredColumn = unsubscribe ? "DS.UNSUBSCRIBED_BY" : "DS.SUBSCRIBED_BY"; + StringBuilder sql = new StringBuilder("SELECT " + + "DS.ID AS ID, " + + "DS.SUBSCRIBED_BY AS SUBSCRIBED_BY, " + + "DS.SUBSCRIBED_TIMESTAMP AS SUBSCRIBED_AT, " + + "DS.UNSUBSCRIBED AS IS_UNSUBSCRIBED, " + + "DS.UNSUBSCRIBED_BY AS UNSUBSCRIBED_BY, " + + "DS.UNSUBSCRIBED_TIMESTAMP AS UNSUBSCRIBED_AT, " + + "DS.ACTION_TRIGGERED_FROM AS ACTION_TRIGGERED_FROM, " + + "DS.STATUS AS STATUS, " + + "DS.DM_DEVICE_ID AS DEVICE_ID " + + "FROM AP_DEVICE_SUBSCRIPTION DS " + + "WHERE DS.AP_APP_RELEASE_ID = ? " + + "AND DS.UNSUBSCRIBED = ? " + + "AND DS.TENANT_ID = ? "); + if (actionStatus != null && !actionStatus.isEmpty()) { + sql.append(" AND DS.STATUS IN (") + .append(actionStatus.stream().map(status -> "?").collect(Collectors.joining(","))).append(") "); + } + if (actionType != null && !actionType.isEmpty()) { + sql.append(" AND DS.ACTION_TRIGGERED_FROM = ? "); + } + if (actionTriggeredBy != null && !actionTriggeredBy.isEmpty()) { + sql.append(" AND ").append(actionTriggeredColumn).append(" LIKE ? "); + } + sql.append("ORDER BY ").append(subscriptionStatusTime).append(" DESC "); + if (limit >= 0 && offset >= 0) { + sql.append("OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"); + } + try { + Connection conn = this.getDBConnection(); + try (PreparedStatement ps = conn.prepareStatement(sql.toString())) { + int paramIdx = 1; + ps.setInt(paramIdx++, appReleaseId); + ps.setBoolean(paramIdx++, unsubscribe); + ps.setInt(paramIdx++, tenantId); + if (actionStatus != null && !actionStatus.isEmpty()) { + for (String status : actionStatus) { + ps.setString(paramIdx++, status); + } + } + if (actionType != null && !actionType.isEmpty()) { + ps.setString(paramIdx++, actionType); + } + if (actionTriggeredBy != null && !actionTriggeredBy.isEmpty()) { + ps.setString(paramIdx++, "%" + actionTriggeredBy + "%"); + } + if (limit >= 0 && offset >= 0) { + ps.setInt(paramIdx++, offset); + ps.setInt(paramIdx, limit); + } + try (ResultSet rs = ps.executeQuery()) { + if (log.isDebugEnabled()) { + log.debug("Successfully retrieved device subscriptions for application release id " + + appReleaseId); + } + List deviceSubscriptions = new ArrayList<>(); + while (rs.next()) { + DeviceSubscriptionDTO subscription = new DeviceSubscriptionDTO(); + subscription.setId(rs.getInt("ID")); + subscription.setSubscribedBy(rs.getString("SUBSCRIBED_BY")); + subscription.setSubscribedTimestamp(rs.getTimestamp("SUBSCRIBED_AT")); + subscription.setUnsubscribed(rs.getBoolean("IS_UNSUBSCRIBED")); + subscription.setUnsubscribedBy(rs.getString("UNSUBSCRIBED_BY")); + subscription.setUnsubscribedTimestamp(rs.getTimestamp("UNSUBSCRIBED_AT")); + subscription.setActionTriggeredFrom(rs.getString("ACTION_TRIGGERED_FROM")); + subscription.setStatus(rs.getString("STATUS")); + subscription.setDeviceId(rs.getInt("DEVICE_ID")); + + deviceSubscriptions.add(subscription); + } + return deviceSubscriptions; + } + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection for getting device subscription for application Id: " + + appReleaseId + "."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while running SQL to get device subscription data for application ID: " + appReleaseId; + log.error(msg, e); + throw new ApplicationManagementDAOException(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/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 e5c3969329..0fbc32b986 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 @@ -38,6 +38,19 @@ import java.util.List; import java.util.stream.Collectors; public class SubscriptionManagementHelperUtil { + + /** + * Retrieves device subscription data based on the provided filters. + * + * @param deviceSubscriptionDTOS List of DeviceSubscriptionDTO objects. + * @param deviceSubscriptionFilterCriteria Filter criteria for device subscription. + * @param isUnsubscribed Boolean indicating whether to filter unsubscribed devices. + * @param deviceTypeId Device type ID. + * @param limit Limit for pagination. + * @param offset Offset for pagination. + * @return List of DeviceSubscription objects. + * @throws DeviceManagementException If an error occurs during device management. + */ public static List getDeviceSubscriptionData(List deviceSubscriptionDTOS, DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria, boolean isUnsubscribed, int deviceTypeId, int limit, int offset) @@ -52,6 +65,15 @@ public class SubscriptionManagementHelperUtil { return populateDeviceData(deviceSubscriptionDTOS, devices, isUnsubscribed); } + /** + * Retrieves the total count of device subscriptions based on the provided filters. + * + * @param deviceSubscriptionDTOS List of DeviceSubscriptionDTO objects. + * @param deviceSubscriptionFilterCriteria Filter criteria for device subscription. + * @param deviceTypeId Device type ID. + * @return int Total count of device subscriptions. + * @throws DeviceManagementException If an error occurs during device management. + */ public static int getTotalDeviceSubscriptionCount(List deviceSubscriptionDTOS, DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria, int deviceTypeId) throws DeviceManagementException { @@ -64,6 +86,14 @@ public class SubscriptionManagementHelperUtil { return HelperUtil.getDeviceManagementProviderService().getDeviceCountByDeviceIds(paginationRequest, deviceIds); } + /** + * Populates device subscription data based on the provided devices and subscription DTOs. + * + * @param deviceSubscriptionDTOS List of DeviceSubscriptionDTO objects. + * @param devices List of Device objects. + * @param isUnsubscribed Boolean indicating whether to filter unsubscribed devices. + * @return List of DeviceSubscription objects. + */ private static List populateDeviceData(List deviceSubscriptionDTOS, List devices, boolean isUnsubscribed) { List deviceSubscriptions = new ArrayList<>(); @@ -88,6 +118,13 @@ public class SubscriptionManagementHelperUtil { return deviceSubscriptions; } + /** + * Creates a SubscriptionData object based on the provided subscription DTO. + * + * @param isUnsubscribed Boolean indicating whether to filter unsubscribed devices. + * @param deviceSubscriptionDTO DeviceSubscriptionDTO object. + * @return SubscriptionData object. + */ private static SubscriptionData getSubscriptionData(boolean isUnsubscribed, DeviceSubscriptionDTO deviceSubscriptionDTO) { SubscriptionData subscriptionData = new SubscriptionData(); subscriptionData.setTriggeredBy(isUnsubscribed ? deviceSubscriptionDTO.getUnsubscribedBy() : @@ -99,16 +136,36 @@ public class SubscriptionManagementHelperUtil { return subscriptionData; } + /** + * Retrieves the device subscription status based on the provided subscription info. + * + * @param subscriptionInfo SubscriptionInfo object. + * @return Device subscription status. + */ public static String getDeviceSubscriptionStatus(SubscriptionInfo subscriptionInfo) { return getDeviceSubscriptionStatus(subscriptionInfo.getDeviceSubscriptionFilterCriteria(). getFilteringDeviceSubscriptionStatus(), subscriptionInfo.getDeviceSubscriptionStatus()); } + /** + * Retrieves the device subscription status based on the provided filter and status. + * + * @param deviceSubscriptionStatusFilter Filtered device subscription status. + * @param deviceSubscriptionStatus Device subscription status. + * @return Device subscription status. + */ public static String getDeviceSubscriptionStatus(String deviceSubscriptionStatusFilter, String deviceSubscriptionStatus) { return (deviceSubscriptionStatusFilter != null && !deviceSubscriptionStatusFilter.isEmpty()) ? deviceSubscriptionStatusFilter : deviceSubscriptionStatus; } + /** + * Retrieves subscription statistics based on the provided subscription statistics DTO and device count. + * + * @param subscriptionStatisticDTO SubscriptionStatisticDTO object. + * @param allDeviceCount Total count of all devices. + * @return SubscriptionStatistics object. + */ public static SubscriptionStatistics getSubscriptionStatistics(SubscriptionStatisticDTO subscriptionStatisticDTO, int allDeviceCount) { SubscriptionStatistics subscriptionStatistics = new SubscriptionStatistics(); subscriptionStatistics.setCompletedPercentage( @@ -124,6 +181,13 @@ public class SubscriptionManagementHelperUtil { return subscriptionStatistics; } + /** + * Calculates the percentages. + * + * @param numerator Numerator value. + * @param denominator Denominator value. + * @return Calculated percentage. + */ public static float getPercentage(int numerator, int denominator) { if (denominator <= 0) { return 0.0f; @@ -131,6 +195,12 @@ public class SubscriptionManagementHelperUtil { return ((float) numerator / (float) denominator) * 100; } + /** + * Retrieves database subscription statuses based on the provided device subscription status. + * + * @param deviceSubscriptionStatus Device subscription status. + * @return List of database subscription statuses. + */ public static List getDBSubscriptionStatus(String deviceSubscriptionStatus) { return SubscriptionMetadata.deviceSubscriptionStatusToDBSubscriptionStatusMap.get(deviceSubscriptionStatus); } 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/SubscriptionManagementServiceProvider.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/SubscriptionManagementServiceProvider.java index 61602d7317..c2fd88b25e 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/SubscriptionManagementServiceProvider.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/SubscriptionManagementServiceProvider.java @@ -33,14 +33,22 @@ public class SubscriptionManagementServiceProvider { private SubscriptionManagementServiceProvider() { } - public static SubscriptionManagementServiceProvider getInstance() { - return SubscriptionManagementProviderServiceHolder.INSTANCE; - } - + /** + * Retrieves the appropriate SubscriptionManagementHelperService based on the provided SubscriptionInfo. + * + * @param subscriptionInfo SubscriptionInfo object containing the subscription type. + * @return SubscriptionManagementHelperService implementation based on the subscription type. + */ public SubscriptionManagementHelperService getSubscriptionManagementHelperService(SubscriptionInfo subscriptionInfo) { return getSubscriptionManagementHelperService(subscriptionInfo.getSubscriptionType()); } + /** + * Retrieves the appropriate SubscriptionManagementHelperService based on the subscription type. + * + * @param subscriptionType Type of the subscription. + * @return SubscriptionManagementHelperService implementation based on the subscription type. + */ private SubscriptionManagementHelperService getSubscriptionManagementHelperService(String subscriptionType) { if (Objects.equals(subscriptionType, SubscriptionMetadata.SubscriptionTypes.ROLE)) return RoleBasedSubscriptionManagementHelperServiceImpl.getInstance(); diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index 7ae29f29d8..52feae35d4 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -3306,7 +3306,9 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { List filteredDeviceIds = new ArrayList<>(); try { Connection connection = getConnection(); - String sql = "SELECT ID AS DEVICE_ID FROM DM_DEVICE WHERE TENANT_ID = ?"; + String sql = "SELECT ID AS DEVICE_ID " + + "FROM DM_DEVICE " + + "WHERE TENANT_ID = ?"; if (deviceIds != null && !deviceIds.isEmpty()) { sql += " AND ID NOT IN ( " + deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ")"; @@ -3349,8 +3351,12 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { String deviceIdStringList = deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")); try { Connection connection = getConnection(); - String sql = "SELECT ID AS DEVICE_ID FROM DM_DEVICE WHERE ID IN " + - "(" + deviceIdStringList + ") AND TENANT_ID = ? LIMIT ? OFFSET ?"; + String sql = "SELECT ID AS DEVICE_ID " + + "FROM DM_DEVICE " + + "WHERE ID IN (" + deviceIdStringList + ")" + + " AND TENANT_ID = ? " + + "LIMIT ? " + + "OFFSET ?"; try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { int paraIdx = 1; for (Integer deviceId : deviceIds) { @@ -3380,7 +3386,9 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { int deviceCount = 0; try { Connection connection = getConnection(); - String sql = "SELECT COUNT(ID) AS COUNT FROM DM_DEVICE WHERE TENANT_ID = ?"; + String sql = "SELECT COUNT(ID) AS COUNT " + + "FROM DM_DEVICE " + + "WHERE TENANT_ID = ?"; if (deviceIds != null && !deviceIds.isEmpty()) { sql += " AND ID NOT IN ( " + deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ")"; @@ -3422,8 +3430,16 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { boolean isDeviceNameProvided = false; try { Connection connection = getConnection(); - String sql = "SELECT e.DEVICE_ID, d.DEVICE_IDENTIFICATION, e.STATUS, e.OWNER, d.NAME AS DEVICE_NAME, " + - "e.DEVICE_TYPE, e.OWNERSHIP, e.DATE_OF_LAST_UPDATE FROM DM_DEVICE d INNER JOIN DM_ENROLMENT e " + + String sql = "SELECT e.DEVICE_ID, " + + "d.DEVICE_IDENTIFICATION, " + + "e.STATUS, " + + "e.OWNER, " + + "d.NAME AS DEVICE_NAME, " + + "e.DEVICE_TYPE, " + + "e.OWNERSHIP, " + + "e.DATE_OF_LAST_UPDATE " + + "FROM DM_DEVICE d " + + "INNER JOIN DM_ENROLMENT e " + "ON d.ID = e.DEVICE_ID " + "WHERE d.DEVICE_TYPE_ID = ? " + "AND d.TENANT_ID = ? " + @@ -3505,7 +3521,9 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { boolean isDeviceNameProvided = false; try { Connection connection = getConnection(); - String sql = "SELECT COUNT(DISTINCT e.DEVICE_ID) FROM DM_DEVICE d INNER JOIN DM_ENROLMENT e " + + String sql = "SELECT COUNT(DISTINCT e.DEVICE_ID) " + + "FROM DM_DEVICE d " + + "INNER JOIN DM_ENROLMENT e " + "ON d.ID = e.DEVICE_ID " + "WHERE e.TENANT_ID = ? " + "AND e.DEVICE_ID IN (" + deviceIdStringList+ ") " + 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/device/GenericDeviceDAOImpl.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/device/GenericDeviceDAOImpl.java index bf539199c9..e116df7490 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/device/GenericDeviceDAOImpl.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/device/GenericDeviceDAOImpl.java @@ -42,6 +42,7 @@ import java.util.Date; import java.util.List; import java.util.StringJoiner; import java.util.Map; +import java.util.stream.Collectors; /** * This class holds the generic implementation of DeviceDAO which can be used to support ANSI db syntax. @@ -1858,4 +1859,156 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { } } + @Override + public List getDevicesByDeviceIds(PaginationRequest paginationRequest, List deviceIds, int tenantId) + throws DeviceManagementDAOException { + List devices = new ArrayList<>(); + if (deviceIds == null || deviceIds.isEmpty()) return devices; + + String deviceIdStringList = deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")); + boolean isOwnerProvided = false; + boolean isDeviceStatusProvided = false; + boolean isDeviceNameProvided = false; + try { + Connection connection = getConnection(); + String sql = "SELECT e.DEVICE_ID, " + + "d.DEVICE_IDENTIFICATION, " + + "e.STATUS, " + + "e.OWNER, " + + "d.NAME AS DEVICE_NAME, " + + "e.DEVICE_TYPE, " + + "e.OWNERSHIP, " + + "e.DATE_OF_LAST_UPDATE " + + "FROM DM_DEVICE d " + + "INNER JOIN DM_ENROLMENT e " + + "WHERE d.ID = e.DEVICE_ID " + + "AND d.TENANT_ID = ? " + + "AND e.DEVICE_ID IN (" + deviceIdStringList+ ") " + + "AND e.STATUS NOT IN ('DELETED', 'REMOVED')"; + if (paginationRequest.getOwner() != null) { + sql = sql + " AND e.OWNER LIKE ?"; + isOwnerProvided = true; + } + if (paginationRequest.getDeviceStatus() != null) { + sql = sql + " AND e.STATUS = ?"; + isDeviceStatusProvided = true; + } + if (paginationRequest.getDeviceName() != null) { + sql = sql + " AND d.NAME LIKE ?"; + isDeviceNameProvided = true; + } + sql = sql + " LIMIT ? OFFSET ?"; + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + int parameterIdx = 1; + preparedStatement.setInt(parameterIdx++, tenantId); + for (Integer deviceId : deviceIds) { + preparedStatement.setInt(parameterIdx++, deviceId); + } + if (isOwnerProvided) + preparedStatement.setString(parameterIdx++, "%" + paginationRequest.getOwner() + "%"); + if (isDeviceStatusProvided) + preparedStatement.setString(parameterIdx++, paginationRequest.getDeviceStatus()); + if (isDeviceNameProvided) + preparedStatement.setString(parameterIdx++, "%" + paginationRequest.getDeviceName() + "%"); + preparedStatement.setInt(parameterIdx++, paginationRequest.getRowCount()); + preparedStatement.setInt(parameterIdx, paginationRequest.getStartIndex()); + try(ResultSet resultSet = preparedStatement.executeQuery()) { + Device device; + while(resultSet.next()) { + device = new Device(); + device.setId(resultSet.getInt("DEVICE_ID")); + device.setDeviceIdentifier(resultSet.getString("DEVICE_IDENTIFICATION")); + device.setName(resultSet.getString("DEVICE_NAME")); + device.setType(resultSet.getString("DEVICE_TYPE")); + EnrolmentInfo enrolmentInfo = new EnrolmentInfo(); + enrolmentInfo.setStatus(EnrolmentInfo.Status.valueOf(resultSet.getString("STATUS"))); + enrolmentInfo.setOwner(resultSet.getString("OWNER")); + enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.valueOf(resultSet.getString("OWNERSHIP"))); + enrolmentInfo.setDateOfLastUpdate(resultSet.getTimestamp("DATE_OF_LAST_UPDATE").getTime()); + device.setEnrolmentInfo(enrolmentInfo); + devices.add(device); + } + } + } + return devices; + } catch (SQLException e) { + String msg = "Error occurred while retrieving devices for device ids in: " + deviceIds; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + } + + @Override + public List getDevicesInGivenIdList(PaginationRequest request, List deviceIds, int tenantId) + throws DeviceManagementDAOException { + List filteredDeviceIds = new ArrayList<>(); + if (deviceIds == null || deviceIds.isEmpty()) return filteredDeviceIds; + String deviceIdStringList = deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")); + try { + Connection connection = getConnection(); + String sql = "SELECT ID AS DEVICE_ID " + + "FROM DM_DEVICE WHERE ID IN " + + "(" + deviceIdStringList + ") " + + "AND TENANT_ID = ? " + + "LIMIT ? " + + "OFFSET ?"; + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + int paraIdx = 1; + for (Integer deviceId : deviceIds) { + preparedStatement.setInt(paraIdx++, deviceId); + } + preparedStatement.setInt(paraIdx++, tenantId); + preparedStatement.setInt(paraIdx++, request.getRowCount()); + preparedStatement.setInt(paraIdx, request.getStartIndex()); + try (ResultSet resultSet = preparedStatement.executeQuery()) { + while (resultSet.next()) { + filteredDeviceIds.add(resultSet.getInt("DEVICE_ID")); + } + } + return filteredDeviceIds; + } + } catch (SQLException e) { + String msg = "Error occurred while retrieving device ids in: " + filteredDeviceIds; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + } + + @Override + public List getDevicesNotInGivenIdList(PaginationRequest request, List deviceIds, int tenantId) + throws DeviceManagementDAOException { + List filteredDeviceIds = new ArrayList<>(); + try { + Connection connection = getConnection(); + String sql = "SELECT ID AS DEVICE_ID " + + "FROM DM_DEVICE" + + " WHERE TENANT_ID = ?"; + + if (deviceIds != null && !deviceIds.isEmpty()) { + sql += " AND ID NOT IN ( " + deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ")"; + } + sql += " LIMIT ? OFFSET ?"; + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + int paraIdx = 1; + preparedStatement.setInt(paraIdx++, tenantId); + if (deviceIds != null && !deviceIds.isEmpty()) { + for (Integer deviceId : deviceIds) { + preparedStatement.setInt(paraIdx++, deviceId); + } + } + preparedStatement.setInt(paraIdx++, request.getRowCount()); + preparedStatement.setInt(paraIdx, request.getStartIndex()); + try (ResultSet resultSet = preparedStatement.executeQuery()) { + while (resultSet.next()) { + filteredDeviceIds.add(resultSet.getInt("DEVICE_ID")); + } + } + return filteredDeviceIds; + } + } catch (SQLException e) { + String msg = "Error occurred while retrieving device ids not in: " + filteredDeviceIds; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + } } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.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/device/OracleDeviceDAOImpl.java index 3effd783c1..d1649f8528 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/device/OracleDeviceDAOImpl.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/device/OracleDeviceDAOImpl.java @@ -20,6 +20,7 @@ package io.entgra.device.mgt.core.device.mgt.core.dao.impl.device; import io.entgra.device.mgt.core.device.mgt.common.Device; import io.entgra.device.mgt.core.device.mgt.common.EnrolmentInfo; +import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest; import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -28,7 +29,9 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; /** * This class holds the generic implementation of DeviceDAO which can be used to support ANSI db syntax. @@ -69,4 +72,162 @@ public class OracleDeviceDAOImpl extends SQLServerDeviceDAOImpl { } } + @Override + public List getDevicesByDeviceIds(PaginationRequest paginationRequest, List deviceIds, int tenantId) + throws DeviceManagementDAOException { + List devices = new ArrayList<>(); + if (deviceIds == null || deviceIds.isEmpty()) return devices; + + String deviceIdStringList = deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")); + boolean isOwnerProvided = false; + boolean isDeviceStatusProvided = false; + boolean isDeviceNameProvided = false; + try { + Connection connection = getConnection(); + String sql = "SELECT e.DEVICE_ID, " + + "d.DEVICE_IDENTIFICATION, " + + "e.STATUS, " + + "e.OWNER, " + + "d.NAME AS DEVICE_NAME, " + + "e.DEVICE_TYPE, " + + "e.OWNERSHIP, " + + "e.DATE_OF_LAST_UPDATE " + + "FROM DM_DEVICE d " + + "INNER JOIN DM_ENROLMENT e " + + "ON d.ID = e.DEVICE_ID " + + "WHERE d.TENANT_ID = ? " + + "AND e.DEVICE_ID IN (" + deviceIdStringList + ") " + + "AND e.STATUS NOT IN ('DELETED', 'REMOVED')"; + if (paginationRequest.getOwner() != null) { + sql += " AND e.OWNER LIKE ?"; + isOwnerProvided = true; + } + if (paginationRequest.getDeviceStatus() != null) { + sql += " AND e.STATUS = ?"; + isDeviceStatusProvided = true; + } + if (paginationRequest.getDeviceName() != null) { + sql += " AND d.NAME LIKE ?"; + isDeviceNameProvided = true; + } + sql += " OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + int parameterIdx = 1; + preparedStatement.setInt(parameterIdx++, tenantId); + for (Integer deviceId : deviceIds) { + preparedStatement.setInt(parameterIdx++, deviceId); + } + if (isOwnerProvided) { + preparedStatement.setString(parameterIdx++, "%" + paginationRequest.getOwner() + "%"); + } + if (isDeviceStatusProvided) { + preparedStatement.setString(parameterIdx++, paginationRequest.getDeviceStatus()); + } + if (isDeviceNameProvided) { + preparedStatement.setString(parameterIdx++, "%" + paginationRequest.getDeviceName() + "%"); + } + preparedStatement.setInt(parameterIdx++, paginationRequest.getStartIndex()); + preparedStatement.setInt(parameterIdx, paginationRequest.getRowCount()); + try (ResultSet resultSet = preparedStatement.executeQuery()) { + while (resultSet.next()) { + Device device = new Device(); + device.setId(resultSet.getInt("DEVICE_ID")); + device.setDeviceIdentifier(resultSet.getString("DEVICE_IDENTIFICATION")); + device.setName(resultSet.getString("DEVICE_NAME")); + device.setType(resultSet.getString("DEVICE_TYPE")); + EnrolmentInfo enrolmentInfo = new EnrolmentInfo(); + enrolmentInfo.setStatus(EnrolmentInfo.Status.valueOf(resultSet.getString("STATUS"))); + enrolmentInfo.setOwner(resultSet.getString("OWNER")); + enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.valueOf(resultSet.getString("OWNERSHIP"))); + enrolmentInfo.setDateOfLastUpdate(resultSet.getTimestamp("DATE_OF_LAST_UPDATE").getTime()); + device.setEnrolmentInfo(enrolmentInfo); + devices.add(device); + } + } + } + return devices; + } catch (SQLException e) { + String msg = "Error occurred while retrieving devices for device ids in: " + deviceIds; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + } + + @Override + public List getDevicesInGivenIdList(PaginationRequest request, List deviceIds, int tenantId) + throws DeviceManagementDAOException { + List filteredDeviceIds = new ArrayList<>(); + if (deviceIds == null || deviceIds.isEmpty()) return filteredDeviceIds; + + String deviceIdStringList = deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")); + try { + Connection connection = getConnection(); + String sql = "SELECT ID AS DEVICE_ID " + + "FROM DM_DEVICE WHERE ID IN " + + "(" + deviceIdStringList + ") " + + "AND TENANT_ID = ? " + + "OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + int paraIdx = 1; + for (Integer deviceId : deviceIds) { + preparedStatement.setInt(paraIdx++, deviceId); + } + preparedStatement.setInt(paraIdx++, tenantId); + preparedStatement.setInt(paraIdx++, request.getStartIndex()); + preparedStatement.setInt(paraIdx, request.getRowCount()); + + try (ResultSet resultSet = preparedStatement.executeQuery()) { + while (resultSet.next()) { + filteredDeviceIds.add(resultSet.getInt("DEVICE_ID")); + } + } + return filteredDeviceIds; + } + } catch (SQLException e) { + String msg = "Error occurred while retrieving device ids in: " + deviceIds; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + } + + @Override + public List getDevicesNotInGivenIdList(PaginationRequest request, List deviceIds, int tenantId) + throws DeviceManagementDAOException { + List filteredDeviceIds = new ArrayList<>(); + try { + Connection connection = getConnection(); + String sql = "SELECT ID AS DEVICE_ID " + + "FROM DM_DEVICE " + + "WHERE TENANT_ID = ?"; + + if (deviceIds != null && !deviceIds.isEmpty()) { + sql += " AND ID NOT IN (" + deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ")"; + } + + sql += "OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + int paraIdx = 1; + preparedStatement.setInt(paraIdx++, tenantId); + if (deviceIds != null && !deviceIds.isEmpty()) { + for (Integer deviceId : deviceIds) { + preparedStatement.setInt(paraIdx++, deviceId); + } + } + preparedStatement.setInt(paraIdx++, request.getStartIndex()); + preparedStatement.setInt(paraIdx, request.getRowCount()); + + try (ResultSet resultSet = preparedStatement.executeQuery()) { + while (resultSet.next()) { + filteredDeviceIds.add(resultSet.getInt("DEVICE_ID")); + } + } + return filteredDeviceIds; + } + } catch (SQLException e) { + String msg = "Error occurred while retrieving device ids not in: " + deviceIds; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + } } -- 2.36.3