From 320d5215de079200ac06475251dad999e38ad152 Mon Sep 17 00:00:00 2001 From: nipuni Date: Fri, 30 Aug 2024 13:39:52 +0530 Subject: [PATCH] Implement the search API for Subscription details view --- .../common/services/SubscriptionManager.java | 5 +- .../mgt/core/dao/SubscriptionDAO.java | 71 +++++- .../GenericSubscriptionDAOImpl.java | 232 ++++++++++++------ .../OracleSubscriptionDAOImpl.java | 106 +++++--- .../SQLServerSubscriptionDAOImpl.java | 107 +++++--- .../core/impl/SubscriptionManagerImpl.java | 27 +- 6 files changed, 381 insertions(+), 167 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/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 1e70f1b9d6..fd16e70872 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 @@ -187,11 +187,14 @@ public interface SubscriptionManager { * @param subType subscription type of the application. * @param offsetValue offset value for get paginated request. * @param limitValue limit value for get paginated request. + * @param uninstalled a Boolean flag indicating the filter criteria for retrieve subscription data + * @param searchName an optional search term to filter the results by name. If null or empty, no filtering by name is applied. * @return {@link PaginationResult} pagination result of the category details. * @throws {@link ApplicationManagementException} Exception of the application management */ PaginationResult getAppInstalledSubscribers(int offsetValue, int limitValue, String appUUID, - String subType) throws ApplicationManagementException; + String subType, Boolean uninstalled, String searchName) + throws ApplicationManagementException; /** * This method is responsible to provide application subscription data for given application 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 c35c4a5251..cd1755fc21 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 @@ -198,14 +198,33 @@ public interface SubscriptionDAO { * @param offsetValue offset value for get paginated result * @param limitValue limit value for get paginated result * @param appReleaseId id of the application release. + * @param uninstalled a Boolean flag indicating the filter criteria for getting users: + * - `true` to get only unsubscribed users, + * - `false` to get only subscribed users, + * - `null` to get all users regardless of their unsubscription status. + * @param searchName an optional search term to filter the results by username. * @return subscribedUsers - list of app subscribed users. * @throws {@link ApplicationManagementDAOException} if connections establishment fails. */ List getAppSubscribedUsers(int offsetValue, int limitValue, int appReleaseId, - int tenantId) + int tenantId, Boolean uninstalled, String searchName) throws ApplicationManagementDAOException; - int getSubscribedUserCount(int appReleaseId, int tenantId) throws ApplicationManagementDAOException; + /** + * This method is used to get the count of users who are subscribed or unsubscribed to a specific application release. + * + * @param appReleaseId the ID of the application release for which the user count is to be retrieved. + * @param tenantId the ID of the current tenant. + * @param uninstalled a Boolean flag indicating the filter criteria for counting users: + * - `true` to count only unsubscribed users, + * - `false` to count only subscribed users, + * - `null` to count all users regardless of their unsubscription status. + * @param searchName an optional search term to filter the results by username. + * @return the count of users based on the specified criteria. + * @throws ApplicationManagementDAOException if an error occurs while establishing a database connection or executing the query. + */ + int getSubscribedUserCount(int appReleaseId, int tenantId, Boolean uninstalled, String searchName) + throws ApplicationManagementDAOException; /** * This method is used to get the details of roles @@ -214,14 +233,34 @@ public interface SubscriptionDAO { * @param offsetValue offset value for get paginated request. * @param limitValue limit value for get paginated request. * @param appReleaseId id of the application release. + * @param uninstalled a Boolean flag indicating the filter criteria for getting roles: + * - `true` to get only unsubscribed roles, + * - `false` to get only subscribed roles, + * - `null` to get all roles regardless of their unsubscription status. + * @param searchName an optional search term to filter the results by role name. * @return subscribedRoles - list of app subscribed roles. * @throws {@link ApplicationManagementDAOException} if connections establishment fails. */ List getAppSubscribedRoles(int offsetValue, int limitValue, int appReleaseId, - int tenantId) + int tenantId, Boolean uninstalled, String searchName) throws ApplicationManagementDAOException; - int getSubscribedRoleCount(int appReleaseId, int tenantId) throws ApplicationManagementDAOException; + /** + * This method retrieves the count of roles subscribed to a given application release. + * The count can be filtered based on the unsubscription status. + * + * @param appReleaseId the ID of the application release for which the subscribed roles are counted. + * @param tenantId the ID of the current tenant. + * @param uninstalled a Boolean flag indicating the filter criteria for counting roles: + * - `true` to count only unsubscribed roles, + * - `false` to count only subscribed roles, + * - `null` to count all roles regardless of their unsubscription status. + * @param searchName an optional search term to filter the results by role name. + * @return the count of roles that match the specified criteria. + * @throws ApplicationManagementDAOException if there is an error while accessing the database or processing the request. + */ + int getSubscribedRoleCount(int appReleaseId, int tenantId, Boolean uninstalled, String searchName) + throws ApplicationManagementDAOException; /** * This method is used to get the details of subscribed groups @@ -230,13 +269,33 @@ public interface SubscriptionDAO { * @param offsetValue offset value for get paginated request. * @param limitValue limit value for get paginated request. * @param appReleaseId id of the application release. + * @param uninstalled a Boolean flag indicating the filter criteria for getting groups: + * - `true` to get only unsubscribed groups, + * - `false` to get only subscribed groups, + * - `null` to get all groups regardless of their unsubscription status. + * @param searchName an optional search term to filter the results by group name. * @return subscribedGroups - list of app subscribed groups. * @throws {@link ApplicationManagementDAOException} if connections establishment fails. */ - List getAppSubscribedGroups(int offsetValue, int limitValue, int appReleaseId, int tenantId) + List getAppSubscribedGroups(int offsetValue, int limitValue, int appReleaseId, int tenantId, + Boolean uninstalled, String searchName) throws ApplicationManagementDAOException; - int getSubscribedGroupCount(int appReleaseId, int tenantId) throws ApplicationManagementDAOException; + /** + * This method is used to get the count of subscribed groups + * + * @param tenantId id of the current tenant + * @param appReleaseId id of the application release. + * @param uninstalled a Boolean flag indicating the filter criteria for counting groups: + * - `true` to count only unsubscribed groups, + * - `false` to count only subscribed groups, + * - `null` to count all groups regardless of their unsubscription status. + * @param searchName an optional search term to filter the results by group name. + * @return subscribedGroups - list of app subscribed groups. + * @throws {@link ApplicationManagementDAOException} if connections establishment fails. + */ + int getSubscribedGroupCount(int appReleaseId, int tenantId,Boolean uninstalled, String searchName) + throws ApplicationManagementDAOException; /** * This method is used to get the details of subscribed groups 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 66aec68576..a5701cfdfb 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 @@ -967,25 +967,37 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc @Override public List getAppSubscribedUsers(int offsetValue, int limitValue, int appReleaseId, - int tenantId) + int tenantId, Boolean uninstalled, String searchName) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { - log.debug("Request received in DAO Layer to get already subscribed users for " + - "given app release id."); + log.debug("Request received in DAO Layer to get subscribed/unsubscribed users for the given app release ID."); } try { Connection conn = this.getDBConnection(); List subscribedUsers = new ArrayList<>(); - String sql = "SELECT " - + "US.USER_NAME AS USER_NAME " - + "FROM AP_USER_SUBSCRIPTION US " - + "WHERE " - + "AP_APP_RELEASE_ID = ? AND TENANT_ID = ? LIMIT ? OFFSET ?"; + String sql = "SELECT US.USER_NAME AS USER_NAME " + + "FROM AP_USER_SUBSCRIPTION US " + + "WHERE AP_APP_RELEASE_ID = ? " + + "AND TENANT_ID = ? "; + if (uninstalled != null) { + sql += "AND UNSUBSCRIBED = ? "; + } + if (searchName != null && !searchName.trim().isEmpty()) { + sql += "AND US.USER_NAME LIKE ? "; + } + sql += "LIMIT ? OFFSET ?"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { - stmt.setInt(1, appReleaseId); - stmt.setInt(2, tenantId); - stmt.setInt(3, limitValue); - stmt.setInt(4, offsetValue); + int index = 1; + stmt.setInt(index++, appReleaseId); + stmt.setInt(index++, tenantId); + if (uninstalled != null) { + stmt.setBoolean(index++, uninstalled); + } + if (searchName != null && !searchName.trim().isEmpty()) { + stmt.setString(index++, "%" + searchName + "%"); + } + stmt.setInt(index++, limitValue); + stmt.setInt(index, offsetValue); try (ResultSet rs = stmt.executeQuery()) { while (rs.next()) { subscribedUsers.add(rs.getString("USER_NAME")); @@ -994,50 +1006,62 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc return subscribedUsers; } } catch (DBConnectionException e) { - String msg = "Error occurred while obtaining the DB connection to get already " + - "subscribed users for given app release id."; + String msg = "Error occurred while obtaining the DB connection to get subscribed/unsubscribed users for the " + + "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."; + String msg = "SQL Error occurred while getting subscribed/unsubscribed users for the given app release ID."; log.error(msg, e); throw new ApplicationManagementDAOException(msg, e); } } @Override - public int getSubscribedUserCount(int appReleaseId, int tenantId) + public int getSubscribedUserCount(int appReleaseId, int tenantId, Boolean uninstalled, String searchName) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { - log.debug("Request received in DAO Layer to get already subscribed users for " + + log.debug("Request received in DAO Layer to get already subscribed/unsubscribed 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 = ?"; + String sql = "SELECT COUNT(US.USER_NAME) AS USER_COUNT " + + "FROM AP_USER_SUBSCRIPTION US " + + "WHERE AP_APP_RELEASE_ID = ? " + + "AND TENANT_ID = ?"; + if (uninstalled != null) { + sql += " AND UNSUBSCRIBED = ?"; + } + if (searchName != null && !searchName.trim().isEmpty()) { + sql += " AND US.USER_NAME LIKE ?"; + } try (PreparedStatement stmt = conn.prepareStatement(sql)) { - stmt.setInt(1, appReleaseId); - stmt.setInt(2, tenantId); + int index = 1; + stmt.setInt(index++, appReleaseId); + stmt.setInt(index++, tenantId); + if (uninstalled != null) { + stmt.setBoolean(index++, uninstalled); + } + if (searchName != null && !searchName.trim().isEmpty()) { + stmt.setString(index++, "%" + searchName + "%"); + } try (ResultSet rs = stmt.executeQuery()) { if (rs.next()) { - return rs.getInt("USER_NAME"); + return rs.getInt("USER_COUNT"); } } 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."; + "subscribed/unsubscribed 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."; + String msg = "SQL Error occurred while getting subscribed/unsubscribed users count for given app release id."; log.error(msg, e); throw new ApplicationManagementDAOException(msg, e); } @@ -1151,25 +1175,40 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc @Override public List getAppSubscribedRoles(int offsetValue, int limitValue, int appReleaseId, - int tenantId) + int tenantId, Boolean uninstalled, String searchName) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { - log.debug("Request received in DAO Layer to get already subscribed roles for " + + log.debug("Request received in DAO Layer to get already subscribed/unsubscribed roles for " + "given app release id."); } try { Connection conn = this.getDBConnection(); List subscribedRoles = new ArrayList<>(); String sql = "SELECT " - + "RS.ROLE_NAME AS ROLE " - + "FROM AP_ROLE_SUBSCRIPTION RS " - + "WHERE " - + "AP_APP_RELEASE_ID = ? AND TENANT_ID = ? LIMIT ? OFFSET ?"; + + "RS.ROLE_NAME AS ROLE " + + "FROM AP_ROLE_SUBSCRIPTION RS " + + "WHERE AP_APP_RELEASE_ID = ? " + + "AND TENANT_ID = ?"; + if (uninstalled != null) { + sql += " AND UNSUBSCRIBED = ?"; + } + if (searchName != null && !searchName.trim().isEmpty()) { + sql += " AND RS.ROLE_NAME LIKE ?"; + } + sql += " LIMIT ? OFFSET ?"; try (PreparedStatement ps = conn.prepareStatement(sql)) { - ps.setInt(1, appReleaseId); - ps.setInt(2, tenantId); - ps.setInt(3, limitValue); - ps.setInt(4, offsetValue); + int paramIndex = 1; + ps.setInt(paramIndex++, appReleaseId); + ps.setInt(paramIndex++, tenantId); + + if (uninstalled != null) { + ps.setBoolean(paramIndex++, uninstalled); + } + if (searchName != null && !searchName.trim().isEmpty()) { + ps.setString(paramIndex++, "%" + searchName + "%"); + } + ps.setInt(paramIndex++, limitValue); + ps.setInt(paramIndex, offsetValue); try (ResultSet rs = ps.executeQuery()) { while (rs.next()) { subscribedRoles.add(rs.getString("ROLE")); @@ -1179,49 +1218,61 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } } catch (DBConnectionException e) { String msg = "Error occurred while obtaining the DB connection to get already " + - "subscribed roles for given app release id."; + "subscribed/unsubscribed roles 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."; + String msg = "SQL Error occurred while getting subscribed/unsubscribed roles for given app release id."; log.error(msg, e); throw new ApplicationManagementDAOException(msg, e); } } @Override - public int getSubscribedRoleCount(int appReleaseId, int tenantId) + public int getSubscribedRoleCount(int appReleaseId, int tenantId, Boolean uninstalled, String searchName) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { - log.debug("Request received in DAO Layer to get already subscribed roles for " + + log.debug("Request received in DAO Layer to get already subscribed/unsubscribed 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 = ?"; + + "COUNT(RS.ROLE_NAME) AS ROLE_COUNT " + + "FROM AP_ROLE_SUBSCRIPTION RS " + + "WHERE AP_APP_RELEASE_ID = ? " + + "AND TENANT_ID = ?"; + if (uninstalled != null) { + sql += " AND UNSUBSCRIBED = ?"; + } + if (searchName != null && !searchName.trim().isEmpty()) { + sql += " AND RS.ROLE_NAME LIKE ?"; + } try (PreparedStatement stmt = conn.prepareStatement(sql)) { - stmt.setInt(1, appReleaseId); - stmt.setInt(2, tenantId); - + int paramIndex = 1; + stmt.setInt(paramIndex++, appReleaseId); + stmt.setInt(paramIndex++, tenantId); + if (uninstalled != null) { + stmt.setBoolean(paramIndex++, uninstalled); + } + if (searchName != null && !searchName.trim().isEmpty()) { + stmt.setString(paramIndex++, "%" + searchName + "%"); + } try (ResultSet rs = stmt.executeQuery()) { if (rs.next()) { - return rs.getInt("ROLE_NAME"); + return rs.getInt("ROLE_COUNT"); } + return 0; } - 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."; + "subscribed/unsubscribed 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."; + String msg = "SQL Error occurred while getting subscribed/unsubscribed roles count for given app release id."; log.error(msg, e); throw new ApplicationManagementDAOException(msg, e); } @@ -1269,25 +1320,37 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc @Override public List getAppSubscribedGroups(int offsetValue, int limitValue, int appReleaseId, - int tenantId) + int tenantId, Boolean uninstalled, String searchName) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { - log.debug("Request received in DAO Layer to get already subscribed groups for " + + log.debug("Request received in DAO Layer to get already subscribed/unsubscribed groups for " + "given app release id."); } try { Connection conn = this.getDBConnection(); List subscribedGroups = new ArrayList<>(); - String sql = "SELECT " - + "GS.GROUP_NAME AS APP_GROUPS " - + "FROM AP_GROUP_SUBSCRIPTION GS " - + "WHERE " - + "AP_APP_RELEASE_ID = ? AND TENANT_ID = ? LIMIT ? OFFSET ?"; + String sql = "SELECT GS.GROUP_NAME AS APP_GROUPS " + + "FROM AP_GROUP_SUBSCRIPTION GS " + + "WHERE AP_APP_RELEASE_ID = ? AND TENANT_ID = ?"; + if (uninstalled != null) { + sql += " AND UNSUBSCRIBED = ?"; + } + if (searchName != null && !searchName.trim().isEmpty()) { + sql += " AND GS.GROUP_NAME LIKE ?"; + } + sql += " LIMIT ? OFFSET ?"; try (PreparedStatement ps = conn.prepareStatement(sql)) { - ps.setInt(1, appReleaseId); - ps.setInt(2, tenantId); - ps.setInt(3, limitValue); - ps.setInt(4, offsetValue); + int paramIndex = 1; + ps.setInt(paramIndex++, appReleaseId); + ps.setInt(paramIndex++, tenantId); + if (uninstalled != null) { + ps.setBoolean(paramIndex++, uninstalled); + } + if (searchName != null && !searchName.trim().isEmpty()) { + ps.setString(paramIndex++, "%" + searchName + "%"); + } + ps.setInt(paramIndex++, limitValue); + ps.setInt(paramIndex, offsetValue); try (ResultSet rs = ps.executeQuery()) { while (rs.next()) { subscribedGroups.add(rs.getString("APP_GROUPS")); @@ -1297,11 +1360,11 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } } catch (DBConnectionException e) { String msg = "Error occurred while obtaining the DB connection to get already " + - "subscribed groups for given app release id."; + "subscribed/unsubscribed groups 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 " + + String msg = "SQL Error occurred while getting subscribed/unsubscribed groups for given " + "app release id."; log.error(msg, e); throw new ApplicationManagementDAOException(msg, e); @@ -1309,24 +1372,34 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } @Override - public int getSubscribedGroupCount(int appReleaseId, int tenantId) + public int getSubscribedGroupCount(int appReleaseId, int tenantId, Boolean uninstalled, String searchName) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { - log.debug("Request received in DAO Layer to get already subscribed groups for " + - "given app release id."); + log.debug("Request received in DAO Layer to get the count of subscribed/unsubscribed groups for " + + "given app release id."); } try { Connection conn = this.getDBConnection(); - String sql = "SELECT " - + "COUNT(GS.GROUP_NAME) AS APP_GROUPS_COUNT " - + "FROM AP_GROUP_SUBSCRIPTION GS " - + "WHERE " - + "AP_APP_RELEASE_ID = ? AND TENANT_ID = ?"; + String sql = "SELECT COUNT(GS.GROUP_NAME) AS APP_GROUPS_COUNT " + + "FROM AP_GROUP_SUBSCRIPTION GS " + + "WHERE AP_APP_RELEASE_ID = ? AND TENANT_ID = ?"; + if (uninstalled != null) { + sql += " AND UNSUBSCRIBED = ?"; + } + if (searchName != null && !searchName.trim().isEmpty()) { + sql += " AND GS.GROUP_NAME LIKE ?"; + } try (PreparedStatement stmt = conn.prepareStatement(sql)) { - stmt.setInt(1, appReleaseId); - stmt.setInt(2, tenantId); - + int paramIndex = 1; + stmt.setInt(paramIndex++, appReleaseId); + stmt.setInt(paramIndex++, tenantId); + if (uninstalled != null) { + stmt.setBoolean(paramIndex++, uninstalled); + } + if (searchName != null && !searchName.trim().isEmpty()) { + stmt.setString(paramIndex++, "%" + searchName + "%"); + } try (ResultSet rs = stmt.executeQuery()) { if (rs.next()) { return rs.getInt("APP_GROUPS_COUNT"); @@ -1335,12 +1408,13 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc 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."; + String msg = "Error occurred while obtaining the DB connection to get the count of " + + "subscribed/unsubscribed groups 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."; + String msg = "SQL Error occurred while getting the count of subscribed/unsubscribed groups for given " + + "app release id."; 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/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 f66051e308..a074672a55 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 @@ -43,10 +43,10 @@ public class OracleSubscriptionDAOImpl extends GenericSubscriptionDAOImpl { @Override public List getAppSubscribedUsers(int offsetValue, int limitValue, int appReleaseId, - int tenantId) + int tenantId, Boolean uninstalled, String searchName) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { - log.debug("Request received in DAO Layer to get already subscribed users for " + + log.debug("Request received in DAO Layer to get already subscribed/unsubscribed users for " + "given app release id."); } try { @@ -55,13 +55,28 @@ public class OracleSubscriptionDAOImpl extends GenericSubscriptionDAOImpl { String sql = "SELECT " + "US.USER_NAME AS USER " + "FROM AP_USER_SUBSCRIPTION US " - + "WHERE " - + "AP_APP_RELEASE_ID = ? AND TENANT_ID = ? ORDER BY US.ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + + "WHERE AP_APP_RELEASE_ID = ? " + + "AND TENANT_ID = ? "; + if (uninstalled != null) { + sql += "AND UNSUBSCRIBED = ? "; + } + if (searchName != null && !searchName.trim().isEmpty()) { + sql += "AND US.USER_NAME LIKE ? "; + } + sql += "ORDER BY US.ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { - stmt.setInt(1, appReleaseId); - stmt.setInt(2, tenantId); - stmt.setInt(3, offsetValue); - stmt.setInt(4, limitValue); + int index = 1; + stmt.setInt(index++, appReleaseId); + stmt.setInt(index++, tenantId); + if (uninstalled != null) { + stmt.setBoolean(index++, uninstalled); + } + if (searchName != null && !searchName.trim().isEmpty()) { + stmt.setString(index++, "%" + searchName + "%"); + } + stmt.setInt(index++, offsetValue); + stmt.setInt(index, limitValue); + try (ResultSet rs = stmt.executeQuery()) { while (rs.next()) { subscribedUsers.add(rs.getString("USER")); @@ -71,11 +86,11 @@ public class OracleSubscriptionDAOImpl extends GenericSubscriptionDAOImpl { } } catch (DBConnectionException e) { String msg = "Error occurred while obtaining the DB connection to get already " + - "subscribed users for given app release id."; + "subscribed/unsubscribed users 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."; + String msg = "SQL Error occurred while getting subscribed/unsubscribed users for given app release id."; log.error(msg, e); throw new ApplicationManagementDAOException(msg, e); } @@ -83,10 +98,10 @@ public class OracleSubscriptionDAOImpl extends GenericSubscriptionDAOImpl { @Override public List getAppSubscribedRoles(int offsetValue, int limitValue, int appReleaseId, - int tenantId) + int tenantId, Boolean uninstalled, String searchName) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { - log.debug("Request received in DAO Layer to get already subscribed roles for " + + log.debug("Request received in DAO Layer to get already subscribed/unsubscribed roles for " + "given app release id."); } try { @@ -95,13 +110,27 @@ public class OracleSubscriptionDAOImpl extends GenericSubscriptionDAOImpl { String sql = "SELECT " + "RS.ROLE_NAME AS ROLE " + "FROM AP_ROLE_SUBSCRIPTION RS " - + "WHERE " - + "AP_APP_RELEASE_ID = ? AND TENANT_ID = ? ORDER BY RS.ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + + "WHERE AP_APP_RELEASE_ID = ? " + + "AND TENANT_ID = ? "; + if (uninstalled != null) { + sql += "AND UNSUBSCRIBED = ? "; + } + if (searchName != null && !searchName.trim().isEmpty()) { + sql += "AND RS.ROLE_NAME LIKE ? "; + } + sql += "ORDER BY RS.ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; try (PreparedStatement ps = conn.prepareStatement(sql)) { - ps.setInt(1, appReleaseId); - ps.setInt(2, tenantId); - ps.setInt(3, offsetValue); - ps.setInt(4, limitValue); + int paramIndex = 1; + ps.setInt(paramIndex++, appReleaseId); + ps.setInt(paramIndex++, tenantId); + if (uninstalled != null) { + ps.setBoolean(paramIndex++, uninstalled); + } + if (searchName != null && !searchName.trim().isEmpty()) { + ps.setString(paramIndex++, "%" + searchName + "%"); + } + ps.setInt(paramIndex++, offsetValue); + ps.setInt(paramIndex, limitValue); try (ResultSet rs = ps.executeQuery()) { while (rs.next()) { subscribedRoles.add(rs.getString("ROLE")); @@ -111,11 +140,11 @@ public class OracleSubscriptionDAOImpl extends GenericSubscriptionDAOImpl { } } catch (DBConnectionException e) { String msg = "Error occurred while obtaining the DB connection to get already " + - "subscribed roles for given app release id."; + "subscribed/unsubscribed roles 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."; + String msg = "SQL Error occurred while getting subscribed/unsubscribed roles for given app release id."; log.error(msg, e); throw new ApplicationManagementDAOException(msg, e); } @@ -123,11 +152,10 @@ public class OracleSubscriptionDAOImpl extends GenericSubscriptionDAOImpl { @Override public List getAppSubscribedGroups(int offsetValue, int limitValue, int appReleaseId, - int tenantId) + int tenantId, Boolean uninstalled, String searchName) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { - log.debug("Request received in DAO Layer to get already subscribed groups for " + - "given app release id."); + log.debug("Request received in DAO Layer to get subscribed/unsubscribed groups for the given app release ID."); } try { Connection conn = this.getDBConnection(); @@ -135,13 +163,26 @@ public class OracleSubscriptionDAOImpl extends GenericSubscriptionDAOImpl { String sql = "SELECT " + "GS.GROUP_NAME AS GROUPS " + "FROM AP_GROUP_SUBSCRIPTION GS " - + "WHERE " - + "AP_APP_RELEASE_ID = ? AND TENANT_ID = ? ORDER BY GS.ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + + "WHERE AP_APP_RELEASE_ID = ? AND TENANT_ID = ?"; + if (uninstalled != null) { + sql += " AND UNSUBSCRIBED = ?"; + } + if (searchName != null && !searchName.trim().isEmpty()) { + sql += " AND GS.GROUP_NAME LIKE ?"; + } + sql += " ORDER BY GS.ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; try (PreparedStatement ps = conn.prepareStatement(sql)) { - ps.setInt(1, appReleaseId); - ps.setInt(2, tenantId); - ps.setInt(3, offsetValue); - ps.setInt(4, limitValue); + int paramIndex = 1; + ps.setInt(paramIndex++, appReleaseId); + ps.setInt(paramIndex++, tenantId); + if (uninstalled != null) { + ps.setBoolean(paramIndex++, uninstalled); + } + if (searchName != null && !searchName.trim().isEmpty()) { + ps.setString(paramIndex++, "%" + searchName + "%"); + } + ps.setInt(paramIndex++, offsetValue); + ps.setInt(paramIndex, limitValue); try (ResultSet rs = ps.executeQuery()) { while (rs.next()) { subscribedGroups.add(rs.getString("GROUPS")); @@ -150,13 +191,12 @@ public class OracleSubscriptionDAOImpl extends GenericSubscriptionDAOImpl { return subscribedGroups; } } catch (DBConnectionException e) { - String msg = "Error occurred while obtaining the DB connection to get already " + - "subscribed groups for given app release id."; + String msg = "Error occurred while obtaining the DB connection to get subscribed/unsubscribed groups" + + " for the 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."; + String msg = "SQL Error occurred while getting subscribed/unsubscribed groups for the given app release ID."; 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/dao/impl/subscription/SQLServerSubscriptionDAOImpl.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/SQLServerSubscriptionDAOImpl.java index f715a244c5..622ce0c399 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/SQLServerSubscriptionDAOImpl.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/SQLServerSubscriptionDAOImpl.java @@ -39,10 +39,10 @@ public class SQLServerSubscriptionDAOImpl extends GenericSubscriptionDAOImpl { @Override public List getAppSubscribedUsers(int offsetValue, int limitValue, int appReleaseId, - int tenantId) + int tenantId, Boolean uninstalled, String searchName) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { - log.debug("Request received in DAO Layer to get already subscribed users for " + + log.debug("Request received in DAO Layer to get already subscribed/unsubscribed users for " + "given app release id."); } try { @@ -51,13 +51,28 @@ public class SQLServerSubscriptionDAOImpl extends GenericSubscriptionDAOImpl { String sql = "SELECT " + "US.USER_NAME AS USER_NAME " + "FROM AP_USER_SUBSCRIPTION US " - + "WHERE " - + "AP_APP_RELEASE_ID = ? AND TENANT_ID = ? ORDER BY US.ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + + "WHERE AP_APP_RELEASE_ID = ? " + + "AND TENANT_ID = ? "; + if (uninstalled != null) { + sql += "AND UNSUBSCRIBED = ? "; + } + if (searchName != null && !searchName.trim().isEmpty()) { + sql += "AND US.USER_NAME LIKE ? "; + } + sql += "ORDER BY US.ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { - stmt.setInt(1, appReleaseId); - stmt.setInt(2, tenantId); - stmt.setInt(3, offsetValue); - stmt.setInt(4, limitValue); + int index = 1; + stmt.setInt(index++, appReleaseId); + stmt.setInt(index++, tenantId); + if (uninstalled != null) { + stmt.setBoolean(index++, uninstalled); + } + if (searchName != null && !searchName.trim().isEmpty()) { + stmt.setString(index++, "%" + searchName + "%"); + } + stmt.setInt(index++, offsetValue); + stmt.setInt(index, limitValue); + try (ResultSet rs = stmt.executeQuery()) { while (rs.next()) { subscribedUsers.add(rs.getString("USER_NAME")); @@ -67,11 +82,11 @@ public class SQLServerSubscriptionDAOImpl extends GenericSubscriptionDAOImpl { } } catch (DBConnectionException e) { String msg = "Error occurred while obtaining the DB connection to get already " + - "subscribed users for given app release id."; + "subscribed/unsubscribed users 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."; + String msg = "SQL Error occurred while getting subscribed/unsubscribed users for given app release id."; log.error(msg, e); throw new ApplicationManagementDAOException(msg, e); } @@ -79,10 +94,10 @@ public class SQLServerSubscriptionDAOImpl extends GenericSubscriptionDAOImpl { @Override public List getAppSubscribedRoles(int offsetValue, int limitValue, int appReleaseId, - int tenantId) + int tenantId, Boolean uninstalled, String searchName) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { - log.debug("Request received in DAO Layer to get already subscribed roles for " + + log.debug("Request received in DAO Layer to get already subscribed/unsubscribed roles for " + "given app release id."); } try { @@ -91,13 +106,29 @@ public class SQLServerSubscriptionDAOImpl extends GenericSubscriptionDAOImpl { String sql = "SELECT " + "RS.ROLE_NAME AS ROLE " + "FROM AP_ROLE_SUBSCRIPTION RS " - + "WHERE " - + "AP_APP_RELEASE_ID = ? AND TENANT_ID = ? ORDER BY RS.ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + + "WHERE AP_APP_RELEASE_ID = ? " + + "AND TENANT_ID = ? "; + if (uninstalled != null) { + sql += "AND UNSUBSCRIBED = ? "; + } + if (searchName != null && !searchName.trim().isEmpty()) { + sql += "AND RS.ROLE_NAME LIKE ? "; + } + sql += "ORDER BY RS.ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + try (PreparedStatement ps = conn.prepareStatement(sql)) { - ps.setInt(1, appReleaseId); - ps.setInt(2, tenantId); - ps.setInt(3, offsetValue); - ps.setInt(4, limitValue); + int paramIndex = 1; + ps.setInt(paramIndex++, appReleaseId); + ps.setInt(paramIndex++, tenantId); + if (uninstalled != null) { + ps.setBoolean(paramIndex++, uninstalled); + } + if (searchName != null && !searchName.trim().isEmpty()) { + ps.setString(paramIndex++, "%" + searchName + "%"); + } + ps.setInt(paramIndex++, offsetValue); + ps.setInt(paramIndex, limitValue); + try (ResultSet rs = ps.executeQuery()) { while (rs.next()) { subscribedRoles.add(rs.getString("ROLE")); @@ -107,11 +138,11 @@ public class SQLServerSubscriptionDAOImpl extends GenericSubscriptionDAOImpl { } } catch (DBConnectionException e) { String msg = "Error occurred while obtaining the DB connection to get already " + - "subscribed roles for given app release id."; + "subscribed/unsubscribed roles 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."; + String msg = "SQL Error occurred while getting subscribed/unsubscribed roles for given app release id."; log.error(msg, e); throw new ApplicationManagementDAOException(msg, e); } @@ -119,11 +150,10 @@ public class SQLServerSubscriptionDAOImpl extends GenericSubscriptionDAOImpl { @Override public List getAppSubscribedGroups(int offsetValue, int limitValue, int appReleaseId, - int tenantId) + int tenantId, Boolean uninstalled, String searchName) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { - log.debug("Request received in DAO Layer to get already subscribed groups for " + - "given app release id."); + log.debug("Request received in DAO Layer to get subscribed/unsubscribed groups for the given app release ID."); } try { Connection conn = this.getDBConnection(); @@ -131,13 +161,26 @@ public class SQLServerSubscriptionDAOImpl extends GenericSubscriptionDAOImpl { String sql = "SELECT " + "GS.GROUP_NAME AS GROUPS " + "FROM AP_GROUP_SUBSCRIPTION GS " - + "WHERE " - + "AP_APP_RELEASE_ID = ? AND TENANT_ID = ? ORDER BY GS.ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + + "WHERE AP_APP_RELEASE_ID = ? AND TENANT_ID = ?"; + if (uninstalled != null) { + sql += " AND UNSUBSCRIBED = ?"; + } + if (searchName != null && !searchName.trim().isEmpty()) { + sql += " AND GS.GROUP_NAME LIKE ?"; + } + sql += " ORDER BY GS.ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; try (PreparedStatement ps = conn.prepareStatement(sql)) { - ps.setInt(1, appReleaseId); - ps.setInt(2, tenantId); - ps.setInt(3, offsetValue); - ps.setInt(4, limitValue); + int paramIndex = 1; + ps.setInt(paramIndex++, appReleaseId); + ps.setInt(paramIndex++, tenantId); + if (uninstalled != null) { + ps.setBoolean(paramIndex++, uninstalled); + } + if (searchName != null && !searchName.trim().isEmpty()) { + ps.setString(paramIndex++, "%" + searchName + "%"); + } + ps.setInt(paramIndex++, offsetValue); + ps.setInt(paramIndex, limitValue); try (ResultSet rs = ps.executeQuery()) { while (rs.next()) { subscribedGroups.add(rs.getString("GROUPS")); @@ -146,13 +189,11 @@ public class SQLServerSubscriptionDAOImpl extends GenericSubscriptionDAOImpl { return subscribedGroups; } } catch (DBConnectionException e) { - String msg = "Error occurred while obtaining the DB connection to get already " + - "subscribed groups for given app release id."; + String msg = "Error occurred while obtaining the DB connection to get subscribed/unsubscribed groups for the 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."; + String msg = "SQL Error occurred while getting subscribed/unsubscribed groups for the given app release ID."; 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/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 0c5b4057ff..92f0734c9f 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 @@ -1498,7 +1498,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } @Override - public PaginationResult getAppInstalledSubscribers(int offsetValue, int limitValue, String appUUID, String subType) + public PaginationResult getAppInstalledSubscribers(int offsetValue, int limitValue, String appUUID, String subType, + Boolean uninstalled, String searchName) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); @@ -1513,20 +1514,16 @@ public class SubscriptionManagerImpl implements SubscriptionManager { 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); - } - } + .getAppSubscribedUsers(offsetValue, limitValue, applicationReleaseId, tenantId, uninstalled, searchName); + count = subscriptionDAO.getSubscribedUserCount(applicationReleaseId, tenantId, uninstalled, searchName); + } else if (SubscriptionType.ROLE.toString().equalsIgnoreCase(subType)) { + subscriptionList = subscriptionDAO + .getAppSubscribedRoles(offsetValue, limitValue, applicationReleaseId, tenantId, uninstalled, searchName); + count = subscriptionDAO.getSubscribedRoleCount(applicationReleaseId, tenantId, uninstalled, searchName); + } else if (SubscriptionType.GROUP.toString().equalsIgnoreCase(subType)) { + subscriptionList = subscriptionDAO + .getAppSubscribedGroups(offsetValue, limitValue, applicationReleaseId, tenantId, uninstalled, searchName); + count = subscriptionDAO.getSubscribedGroupCount(applicationReleaseId, tenantId, uninstalled, searchName); } paginationResult.setData(subscriptionList);