From 3596de1b2820b367ad0c8f787554552a070a8b00 Mon Sep 17 00:00:00 2001 From: Rajitha Kumara Date: Sun, 21 Jul 2024 19:27:33 +0530 Subject: [PATCH] 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 {