From 8a2425508976e47eeaf65b7dd19ba2927f302c39 Mon Sep 17 00:00:00 2001 From: prathabanKavin Date: Wed, 24 Jul 2024 00:53:57 +0530 Subject: [PATCH] Fix device count issues in subscription view --- .../GenericSubscriptionDAOImpl.java | 68 +++++++++++++++---- .../core/device/mgt/core/dao/DeviceDAO.java | 10 +++ .../core/dao/impl/AbstractDeviceDAOImpl.java | 36 ++++++++++ .../DeviceManagementProviderService.java | 8 +++ .../DeviceManagementProviderServiceImpl.java | 24 +++++++ 5 files changed, 132 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/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 c459acd7ad..c80e10d064 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 @@ -25,7 +25,11 @@ import io.entgra.device.mgt.core.application.mgt.core.dao.SubscriptionDAO; import io.entgra.device.mgt.core.application.mgt.core.dao.impl.AbstractDAOImpl; import io.entgra.device.mgt.core.application.mgt.core.exception.UnexpectedServerErrorException; import io.entgra.device.mgt.core.application.mgt.core.util.DAOUtil; +import io.entgra.device.mgt.core.application.mgt.core.util.HelperUtil; +import io.entgra.device.mgt.core.device.mgt.common.EnrolmentInfo; +import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException; import io.entgra.device.mgt.core.device.mgt.common.operation.mgt.Activity; +import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import io.entgra.device.mgt.core.application.mgt.common.ExecutionStatus; @@ -2399,23 +2403,41 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } @Override - public int getAllSubscriptionCount(int appReleaseId, int tenantId) - throws ApplicationManagementDAOException { + public int getAllSubscriptionCount(int appReleaseId, int tenantId) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { - log.debug("Getting all subscriptions count for the application appReleaseId " + appReleaseId - + " from the database"); + log.debug("Getting all subscriptions count for the application appReleaseId " + appReleaseId + " from the database"); } + List allowingDeviceStatuses = new ArrayList<>(); + allowingDeviceStatuses.add(EnrolmentInfo.Status.ACTIVE.toString()); + allowingDeviceStatuses.add(EnrolmentInfo.Status.INACTIVE.toString()); + allowingDeviceStatuses.add(EnrolmentInfo.Status.UNREACHABLE.toString()); + + DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); try { Connection conn = this.getDBConnection(); + List deviceIds = deviceManagementProviderService.getDeviceIdsByStatus(allowingDeviceStatuses); + if (deviceIds.isEmpty()) { + return 0; + } + StringBuilder idList = new StringBuilder(); + for (int i = 0; i < deviceIds.size(); i++) { + idList.append("?"); + if (i < deviceIds.size() - 1) { + idList.append(","); + } + } String sql = "SELECT COUNT(*) AS count " + "FROM AP_DEVICE_SUBSCRIPTION " + "WHERE AP_APP_RELEASE_ID = ? " + "AND TENANT_ID = ? " + - "AND UNSUBSCRIBED = FALSE"; - + "AND UNSUBSCRIBED = FALSE " + + "AND DM_DEVICE_ID IN (" + idList.toString() + ")"; try (PreparedStatement ps = conn.prepareStatement(sql)) { ps.setInt(1, appReleaseId); ps.setInt(2, tenantId); + for (int i = 0; i < deviceIds.size(); i++) { + ps.setInt(3 + i, deviceIds.get(i)); + } try (ResultSet rs = ps.executeQuery()) { if (rs.next()) { @@ -2429,7 +2451,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc log.error(msg, e); throw new ApplicationManagementDAOException(msg, e); } - } catch (DBConnectionException e) { + } catch (DBConnectionException | DeviceManagementException e) { String msg = "Error occurred while obtaining the DB connection for getting all subscriptions count for appReleaseId: " + appReleaseId + "."; log.error(msg, e); @@ -2438,23 +2460,41 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } @Override - public int getAllUnsubscriptionCount(int appReleaseId, int tenantId) - throws ApplicationManagementDAOException { + public int getAllUnsubscriptionCount(int appReleaseId, int tenantId) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { - log.debug("Getting all unsubscription count for the application appReleaseId " + appReleaseId - + " from the database"); + log.debug("Getting all unsubscription count for the application appReleaseId " + appReleaseId + " from the database"); } + List allowingDeviceStatuses = new ArrayList<>(); + allowingDeviceStatuses.add(EnrolmentInfo.Status.ACTIVE.toString()); + allowingDeviceStatuses.add(EnrolmentInfo.Status.INACTIVE.toString()); + allowingDeviceStatuses.add(EnrolmentInfo.Status.UNREACHABLE.toString()); + + DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); try { Connection conn = this.getDBConnection(); + List deviceIds = deviceManagementProviderService.getDeviceIdsByStatus(allowingDeviceStatuses); + if (deviceIds.isEmpty()) { + return 0; + } + StringBuilder idList = new StringBuilder(); + for (int i = 0; i < deviceIds.size(); i++) { + idList.append("?"); + if (i < deviceIds.size() - 1) { + idList.append(","); + } + } String sql = "SELECT COUNT(*) AS count " + "FROM AP_DEVICE_SUBSCRIPTION " + "WHERE AP_APP_RELEASE_ID = ? " + "AND TENANT_ID = ? " + - "AND UNSUBSCRIBED = TRUE"; - + "AND UNSUBSCRIBED = TRUE " + + "AND DM_DEVICE_ID IN (" + idList.toString() + ")"; try (PreparedStatement ps = conn.prepareStatement(sql)) { ps.setInt(1, appReleaseId); ps.setInt(2, tenantId); + for (int i = 0; i < deviceIds.size(); i++) { + ps.setInt(3 + i, deviceIds.get(i)); + } try (ResultSet rs = ps.executeQuery()) { if (rs.next()) { @@ -2468,7 +2508,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc log.error(msg, e); throw new ApplicationManagementDAOException(msg, e); } - } catch (DBConnectionException e) { + } catch (DBConnectionException | DeviceManagementException e) { String msg = "Error occurred while obtaining the DB connection for getting all unsubscription count for appReleaseId: " + appReleaseId + "."; log.error(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/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 606a1eab5c..5189068242 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 @@ -18,6 +18,7 @@ package io.entgra.device.mgt.core.device.mgt.core.dao; +import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException; import org.apache.commons.collections.map.SingletonMap; import io.entgra.device.mgt.core.device.mgt.common.*; import io.entgra.device.mgt.core.device.mgt.common.EnrolmentInfo.Status; @@ -879,4 +880,13 @@ public interface DeviceDAO { int getDeviceCountByDeviceIds(PaginationRequest paginationRequest, List deviceIds, int tenantId) throws DeviceManagementDAOException; + + /** + * This method is used to get device count that are not within a specific group. + * + * @param statuses Device statuses to be filtered + * @return deviceIds + * @throws DeviceManagementException + */ + List getDeviceIdsByStatus(List statuses) 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/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 bb0bb97b62..3b8aa18647 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 @@ -18,6 +18,7 @@ package io.entgra.device.mgt.core.device.mgt.core.dao.impl; +import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException; import org.apache.commons.collections.map.SingletonMap; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.LogFactory; @@ -3484,4 +3485,39 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } } + @Override + public List getDeviceIdsByStatus(List statuses) throws DeviceManagementException { + StringBuilder deviceFilters = new StringBuilder(); + for (int i = 0; i < statuses.size(); i++) { + deviceFilters.append("?"); + if (i < statuses.size() - 1) { + deviceFilters.append(","); + } + } + + try { + Connection conn = getConnection(); + String sql = "SELECT DEVICE_ID " + + "FROM DM_ENROLMENT " + + "WHERE STATUS IN (" + deviceFilters.toString() + ")"; + + try (PreparedStatement ps = conn.prepareStatement(sql)) { + for (int i = 0; i < statuses.size(); i++) { + ps.setString(i + 1, statuses.get(i)); + } + + try (ResultSet rs = ps.executeQuery()) { + List deviceIds = new ArrayList<>(); + while (rs.next()) { + deviceIds.add(rs.getInt("DEVICE_ID")); + } + return deviceIds; + } + } + } catch (SQLException e) { + String msg = "Error occurred while running SQL to get device IDs by status."; + log.error(msg, e); + throw new DeviceManagementException(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 56f95d3f44..682e4e5aac 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 @@ -1163,4 +1163,12 @@ public interface DeviceManagementProviderService { throws DeviceManagementException; int getDeviceCountByDeviceIds(PaginationRequest paginationRequest, List deviceIds) throws DeviceManagementException; + + /** + * This method is to get Device ids by statuses + * @param statuses statuses to be filtered. + * @return deviceIds + * @throws DeviceManagementException if any service level or DAO level error occurs. + */ + List getDeviceIdsByStatus(List statuses) 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 341714dd10..851153de16 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 @@ -5706,4 +5706,28 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.closeConnection(); } } + + @Override + public List getDeviceIdsByStatus(List statuses) throws DeviceManagementException { + if (statuses == null || statuses.isEmpty()) { + String msg = "Received null or empty list for statuses"; + log.error(msg); + throw new DeviceManagementException(msg); + } + + try { + DeviceManagementDAOFactory.openConnection(); + return deviceDAO.getDeviceIdsByStatus(statuses); + } catch (DeviceManagementException e) { + String msg = "Error encountered while getting device IDs for statuses: " + statuses; + 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(); + } + } }