From 3f582db051e8d06b21ee09debd7808361aeceecd Mon Sep 17 00:00:00 2001 From: Nipun Nadeen De Silva Date: Tue, 5 Nov 2019 02:35:06 +0000 Subject: [PATCH] Set the correct count value of the subscribers retrieving API --- .../mgt/core/dao/SubscriptionDAO.java | 6 + .../GenericSubscriptionDAOImpl.java | 114 ++++++++++++++++++ .../core/impl/SubscriptionManagerImpl.java | 6 +- 3 files changed, 125 insertions(+), 1 deletion(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java index 5b7f9a00cb..2f1170a77e 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java @@ -165,6 +165,8 @@ public interface SubscriptionDAO { int tenantId) throws ApplicationManagementDAOException; + int getSubscribedUserCount(int appReleaseId, int tenantId) throws ApplicationManagementDAOException; + /** * This method is used to get the details of roles * @@ -179,6 +181,8 @@ public interface SubscriptionDAO { int tenantId) throws ApplicationManagementDAOException; + int getSubscribedRoleCount(int appReleaseId, int tenantId) throws ApplicationManagementDAOException; + /** * This method is used to get the details of subscribed groups * @@ -191,4 +195,6 @@ public interface SubscriptionDAO { */ List getAppSubscribedGroups(int offsetValue, int limitValue, int appReleaseId, int tenantId) throws ApplicationManagementDAOException; + + int getSubscribedGroupCount(int appReleaseId, int tenantId) throws ApplicationManagementDAOException; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java index 46621cb9d4..38ab032ebd 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java @@ -877,6 +877,44 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } } + @Override + public int getSubscribedUserCount(int appReleaseId, int tenantId) + throws ApplicationManagementDAOException { + + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to get already subscribed users for " + + "given app release id."); + } + try { + Connection conn = this.getDBConnection(); + String sql = "SELECT " + + "COUNT(US.USER_NAME) AS USER_NAME " + + "FROM AP_USER_SUBSCRIPTION US " + + "WHERE " + + "AP_APP_RELEASE_ID = ? AND TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, appReleaseId); + stmt.setInt(2, tenantId); + + try (ResultSet rs = stmt.executeQuery()) { + if (rs.next()) { + return rs.getInt("USER_NAME"); + } + } + return 0; + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection to get already " + + "subscribed users count for given app release id."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "SQL Error occurred while getting subscribed users for given app release id."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } + @Override public List getScheduledSubscriptionByStatus(ExecutionStatus status, boolean deleted) throws ApplicationManagementDAOException { @@ -986,6 +1024,44 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } } + @Override + public int getSubscribedRoleCount(int appReleaseId, int tenantId) + throws ApplicationManagementDAOException { + + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to get already subscribed roles for " + + "given app release id."); + } + try { + Connection conn = this.getDBConnection(); + String sql = "SELECT " + + "COUNT(RS.ROLE_NAME) AS ROLE_NAME " + + "FROM AP_ROLE_SUBSCRIPTION RS " + + "WHERE " + + "AP_APP_RELEASE_ID = ? AND TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, appReleaseId); + stmt.setInt(2, tenantId); + + try (ResultSet rs = stmt.executeQuery()) { + if (rs.next()) { + return rs.getInt("ROLE_NAME"); + } + } + return 0; + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection to get already " + + "subscribed roles count for given app release id."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "SQL Error occurred while getting subscribed roles for given app release id."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } + @Override public ScheduledSubscriptionDTO getPendingScheduledSubscriptionByTaskName(String taskName) throws ApplicationManagementDAOException { @@ -1066,4 +1142,42 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc throw new ApplicationManagementDAOException(msg, e); } } + + @Override + public int getSubscribedGroupCount(int appReleaseId, int tenantId) + throws ApplicationManagementDAOException { + + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to get already subscribed groups for " + + "given app release id."); + } + try { + Connection conn = this.getDBConnection(); + String sql = "SELECT " + + "COUNT(GS.GROUP_NAME) AS GROUPS " + + "FROM AP_GROUP_SUBSCRIPTION GS " + + "WHERE " + + "AP_APP_RELEASE_ID = ? AND TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, appReleaseId); + stmt.setInt(2, tenantId); + + try (ResultSet rs = stmt.executeQuery()) { + if (rs.next()) { + return rs.getInt("GROUPS"); + } + } + return 0; + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection to get already " + + "subscribed groups count for given app release id."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "SQL Error occurred while getting subscribed groups for given app release id."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java index 6e0388c843..e8cc0a4d31 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java @@ -1137,18 +1137,22 @@ public class SubscriptionManagerImpl implements SubscriptionManager { int applicationReleaseId = applicationDTO.getApplicationReleaseDTOs().get(0).getId(); List subscriptionList = new ArrayList<>(); + int count = 0; if (SubscriptionType.USER.toString().equalsIgnoreCase(subType)) { subscriptionList = subscriptionDAO .getAppSubscribedUsers(offsetValue, limitValue, applicationReleaseId, tenantId); + count = subscriptionDAO.getSubscribedUserCount(applicationReleaseId, tenantId); } else if (SubscriptionType.ROLE.toString().equalsIgnoreCase(subType)) { subscriptionList = subscriptionDAO .getAppSubscribedRoles(offsetValue, limitValue, applicationReleaseId, tenantId); + count = subscriptionDAO.getSubscribedRoleCount(applicationReleaseId, tenantId); } else if (SubscriptionType.GROUP.toString().equalsIgnoreCase(subType)) { subscriptionList = subscriptionDAO .getAppSubscribedGroups(offsetValue, limitValue, applicationReleaseId, tenantId); + count = subscriptionDAO.getSubscribedGroupCount(applicationReleaseId, tenantId); } - int count = subscriptionList.size(); + paginationResult.setData(subscriptionList); paginationResult.setRecordsFiltered(count); paginationResult.setRecordsTotal(count);