diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java index e2fd221943..acd3f3906b 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java @@ -1656,10 +1656,16 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc List subscriptionEntities = new ArrayList<>(); String subscriptionStatusTime = unsubscribe ? "GS.UNSUBSCRIBED_TIMESTAMP" : "GS.SUBSCRIBED_TIMESTAMP"; - String sql = "SELECT GS.GROUP_NAME, GS.SUBSCRIBED_BY, GS.SUBSCRIBED_TIMESTAMP, GS.UNSUBSCRIBED, " + - "GS.UNSUBSCRIBED_BY, GS.UNSUBSCRIBED_TIMESTAMP, GS.AP_APP_RELEASE_ID " + + String sql = "SELECT GS.GROUP_NAME, " + + "GS.SUBSCRIBED_BY, " + + "GS.SUBSCRIBED_TIMESTAMP, " + + "GS.UNSUBSCRIBED, " + + "GS.UNSUBSCRIBED_BY, " + + "GS.UNSUBSCRIBED_TIMESTAMP, " + + "GS.AP_APP_RELEASE_ID " + "FROM AP_GROUP_SUBSCRIPTION GS " + - "WHERE GS.AP_APP_RELEASE_ID = ? AND GS.UNSUBSCRIBED = ? AND GS.TENANT_ID = ? " + + "WHERE GS.AP_APP_RELEASE_ID = ? " + + "AND GS.UNSUBSCRIBED = ? AND GS.TENANT_ID = ? " + "ORDER BY " + subscriptionStatusTime + " DESC " + "LIMIT ? OFFSET ?"; try (PreparedStatement ps = conn.prepareStatement(sql)) { @@ -1708,10 +1714,17 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc List subscriptionEntities = new ArrayList<>(); String subscriptionStatusTime = unsubscribe ? "US.UNSUBSCRIBED_TIMESTAMP" : "US.SUBSCRIBED_TIMESTAMP"; - String sql = "SELECT US.USER_NAME, US.SUBSCRIBED_BY, US.SUBSCRIBED_TIMESTAMP, US.UNSUBSCRIBED, " + - "US.UNSUBSCRIBED_BY, US.UNSUBSCRIBED_TIMESTAMP, US.AP_APP_RELEASE_ID " + + String sql = "SELECT US.USER_NAME, " + + "US.SUBSCRIBED_BY, " + + "US.SUBSCRIBED_TIMESTAMP, " + + "US.UNSUBSCRIBED, " + + "US.UNSUBSCRIBED_BY, " + + "US.UNSUBSCRIBED_TIMESTAMP, " + + "US.AP_APP_RELEASE_ID " + "FROM AP_USER_SUBSCRIPTION US " + - "WHERE US.AP_APP_RELEASE_ID = ? AND US.UNSUBSCRIBED = ? AND US.TENANT_ID = ? " + + "WHERE US.AP_APP_RELEASE_ID = ? " + + "AND US.UNSUBSCRIBED = ? " + + "AND US.TENANT_ID = ? " + "ORDER BY " + subscriptionStatusTime + " DESC " + "LIMIT ? OFFSET ?"; try (PreparedStatement ps = conn.prepareStatement(sql)) { @@ -1759,10 +1772,17 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc List subscriptionEntities = new ArrayList<>(); String subscriptionStatusTime = unsubscribe ? "ARS.UNSUBSCRIBED_TIMESTAMP" : "ARS.SUBSCRIBED_TIMESTAMP"; - String sql = "SELECT ARS.ROLE_NAME, ARS.SUBSCRIBED_BY, ARS.SUBSCRIBED_TIMESTAMP, ARS.UNSUBSCRIBED, " + - "ARS.UNSUBSCRIBED_BY, ARS.UNSUBSCRIBED_TIMESTAMP, ARS.AP_APP_RELEASE_ID " + + String sql = "SELECT ARS.ROLE_NAME, " + + "ARS.SUBSCRIBED_BY, " + + "ARS.SUBSCRIBED_TIMESTAMP, " + + "ARS.UNSUBSCRIBED, " + + "ARS.UNSUBSCRIBED_BY, " + + "ARS.UNSUBSCRIBED_TIMESTAMP, " + + "ARS.AP_APP_RELEASE_ID " + "FROM AP_ROLE_SUBSCRIPTION ARS " + - "WHERE ARS.AP_APP_RELEASE_ID = ? AND ARS.UNSUBSCRIBED = ? AND ARS.TENANT_ID = ? " + + "WHERE ARS.AP_APP_RELEASE_ID = ? " + + "AND ARS.UNSUBSCRIBED = ? " + + "AND ARS.TENANT_ID = ? " + "ORDER BY " + subscriptionStatusTime + " DESC " + "LIMIT ? OFFSET ?"; try (PreparedStatement ps = conn.prepareStatement(sql)) { @@ -1943,8 +1963,11 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc + "DS.STATUS AS STATUS, " + "DS.DM_DEVICE_ID AS DEVICE_ID " + "FROM AP_DEVICE_SUBSCRIPTION DS " - + "WHERE DS.AP_APP_RELEASE_ID = ? AND DS.UNSUBSCRIBED = ? AND DS.TENANT_ID = ? AND DS.DM_DEVICE_ID IN (" + - deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ") "); + + "WHERE DS.AP_APP_RELEASE_ID = ? " + + "AND DS.UNSUBSCRIBED = ? " + + "AND DS.TENANT_ID = ? " + + "AND DS.DM_DEVICE_ID IN (" + + deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ") "); if (actionStatus != null && !actionStatus.isEmpty()) { sql.append(" AND DS.STATUS IN ("). @@ -2040,8 +2063,11 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc Connection conn = this.getDBConnection(); StringBuilder sql = new StringBuilder("SELECT COUNT(DISTINCT DS.DM_DEVICE_ID) AS COUNT " + "FROM AP_DEVICE_SUBSCRIPTION DS " - + "WHERE DS.AP_APP_RELEASE_ID = ? AND DS.UNSUBSCRIBED = ? AND DS.TENANT_ID = ? AND DS.DM_DEVICE_ID IN (" + - deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ") "); + + "WHERE DS.AP_APP_RELEASE_ID = ? " + + "AND DS.UNSUBSCRIBED = ? " + + "AND DS.TENANT_ID = ? " + + "AND DS.DM_DEVICE_ID IN (" + + deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ") "); if (actionStatus != null && !actionStatus.isEmpty()) { sql.append(" AND DS.STATUS IN ("). @@ -2216,7 +2242,9 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc + "DS.STATUS AS STATUS, " + "DS.DM_DEVICE_ID AS DEVICE_ID " + "FROM AP_DEVICE_SUBSCRIPTION DS " - + "WHERE DS.AP_APP_RELEASE_ID = ? AND DS.UNSUBSCRIBED = ? AND DS.TENANT_ID = ? "); + + "WHERE DS.AP_APP_RELEASE_ID = ? " + + "AND DS.UNSUBSCRIBED = ? " + + "AND DS.TENANT_ID = ? "); if (actionStatus != null && !actionStatus.isEmpty()) { sql.append(" AND DS.STATUS IN ("). @@ -2301,18 +2329,18 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc @Override public int getAllSubscriptionsCount(int appReleaseId, boolean unsubscribe, int tenantId, - List actionStatus, String actionType, String actionTriggeredBy) + List actionStatus, String actionType, String actionTriggeredBy) throws ApplicationManagementDAOException { - int deviceCount = 0; if (log.isDebugEnabled()) { log.debug("Getting device subscriptions for the application release id " + appReleaseId + " from the database"); } - String actionTriggeredColumn = unsubscribe ? "DS.UNSUBSCRIBED_BY" : "DS.SUBSCRIBED_BY"; StringBuilder sql = new StringBuilder("SELECT COUNT(DISTINCT DS.DM_DEVICE_ID) AS COUNT " + "FROM AP_DEVICE_SUBSCRIPTION DS " - + "WHERE DS.AP_APP_RELEASE_ID = ? AND DS.UNSUBSCRIBED = ? AND DS.TENANT_ID = ? "); + + "WHERE DS.AP_APP_RELEASE_ID = ? " + + "AND DS.UNSUBSCRIBED = ? " + + "AND DS.TENANT_ID = ? "); if (actionStatus != null && !actionStatus.isEmpty()) { sql.append(" AND DS.STATUS IN ("). @@ -2324,7 +2352,6 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc if (actionTriggeredBy != null && !actionTriggeredBy.isEmpty()) { sql.append(" AND ").append(actionTriggeredColumn).append(" LIKE ?"); } - try { Connection conn = this.getDBConnection(); try (PreparedStatement ps = conn.prepareStatement(sql.toString())) { @@ -2338,24 +2365,19 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc ps.setString(paramIdx++, status); } } - if (actionType != null && !actionType.isEmpty()) { ps.setString(paramIdx++, actionType); } if (actionTriggeredBy != null && !actionTriggeredBy.isEmpty()) { ps.setString(paramIdx++, "%" + actionTriggeredBy + "%"); } - try (ResultSet rs = ps.executeQuery()) { if (log.isDebugEnabled()) { log.debug("Successfully retrieved device subscriptions for application release id " + appReleaseId); } - if (rs.next()) { - deviceCount = rs.getInt("COUNT"); - } + return rs.next() ? rs.getInt("COUNT") : 0; } - return deviceCount; } } catch (DBConnectionException e) { String msg = "Error occurred while obtaining the DB connection for getting device subscription for " @@ -2773,8 +2795,11 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc try { Connection connection = getDBConnection(); String sql = "SELECT COUNT(DISTINCT ID) AS COUNT, " + - "STATUS FROM AP_DEVICE_SUBSCRIPTION WHERE " + - "TENANT_ID = ? AND UNSUBSCRIBED = ? AND DM_DEVICE_ID IN ("+ + "STATUS " + + "FROM AP_DEVICE_SUBSCRIPTION " + + "WHERE TENANT_ID = ? " + + "AND UNSUBSCRIBED = ? " + + "AND DM_DEVICE_ID IN ("+ deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ")"; if (!Objects.equals(subscriptionType, SubscriptionMetadata.SubscriptionTypes.DEVICE)) { diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/OracleSubscriptionDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/OracleSubscriptionDAOImpl.java index 9181b9b4de..71bd17bbc9 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/OracleSubscriptionDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/OracleSubscriptionDAOImpl.java @@ -18,6 +18,8 @@ package io.entgra.device.mgt.core.application.mgt.core.dao.impl.subscription; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity; +import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import io.entgra.device.mgt.core.application.mgt.common.exception.DBConnectionException; @@ -28,7 +30,9 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; /** * This handles Application subscribing operations which are specific to Oracle. @@ -157,4 +161,379 @@ public class OracleSubscriptionDAOImpl extends GenericSubscriptionDAOImpl { throw new ApplicationManagementDAOException(msg, e); } } + + // passed the required list for the action status + @Override + public List getSubscriptionDetailsByDeviceIds(int appReleaseId, boolean unsubscribe, int tenantId, + List deviceIds, List actionStatus, String actionType, + String actionTriggeredBy, int limit, int offset) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Getting device subscriptions for the application release id " + appReleaseId + + " and device ids " + deviceIds + " from the database"); + } + if (deviceIds == null || deviceIds.isEmpty()) { + return Collections.emptyList(); + } + try { + Connection conn = this.getDBConnection(); + String subscriptionStatusTime = unsubscribe ? "DS.UNSUBSCRIBED_TIMESTAMP" : "DS.SUBSCRIBED_TIMESTAMP"; + StringBuilder sql = new StringBuilder("SELECT " + + "DS.ID AS ID, " + + "DS.SUBSCRIBED_BY AS SUBSCRIBED_BY, " + + "DS.SUBSCRIBED_TIMESTAMP AS SUBSCRIBED_AT, " + + "DS.UNSUBSCRIBED AS IS_UNSUBSCRIBED, " + + "DS.UNSUBSCRIBED_BY AS UNSUBSCRIBED_BY, " + + "DS.UNSUBSCRIBED_TIMESTAMP AS UNSUBSCRIBED_AT, " + + "DS.ACTION_TRIGGERED_FROM AS ACTION_TRIGGERED_FROM, " + + "DS.STATUS AS STATUS, " + + "DS.DM_DEVICE_ID AS DEVICE_ID " + + "FROM AP_DEVICE_SUBSCRIPTION DS " + + "WHERE DS.AP_APP_RELEASE_ID = ? " + + "AND DS.UNSUBSCRIBED = ? " + + "AND DS.TENANT_ID = ? " + + "AND DS.DM_DEVICE_ID IN (" + + deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ") "); + if (actionStatus != null && !actionStatus.isEmpty()) { + sql.append(" AND DS.STATUS IN ("). + append(actionStatus.stream().map(status -> "?").collect(Collectors.joining(","))).append(") "); + } + if (actionType != null && !actionType.isEmpty()) { + sql.append(" AND DS.ACTION_TRIGGERED_FROM = ? "); + } + if (actionTriggeredBy != null && !actionTriggeredBy.isEmpty()) { + sql.append(" AND DS.SUBSCRIBED_BY LIKE ? "); + } + sql.append("ORDER BY ").append(subscriptionStatusTime). + append(" DESC "); + if (offset >= 0 && limit >= 0) { + sql.append("OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"); + } + try (PreparedStatement ps = conn.prepareStatement(sql.toString())) { + int paramIdx = 1; + ps.setInt(paramIdx++, appReleaseId); + ps.setBoolean(paramIdx++, unsubscribe); + ps.setInt(paramIdx++, tenantId); + for (Integer deviceId : deviceIds) { + ps.setInt(paramIdx++, deviceId); + } + if (actionStatus != null && !actionStatus.isEmpty()) { + for (String status : actionStatus) { + ps.setString(paramIdx++, status); + } + } + if (actionType != null && !actionType.isEmpty()) { + ps.setString(paramIdx++, actionType); + } + if (actionTriggeredBy != null && !actionTriggeredBy.isEmpty()) { + ps.setString(paramIdx++, "%" + actionTriggeredBy + "%"); + } + if (offset >= 0 && limit >= 0) { + ps.setInt(paramIdx++, offset); + ps.setInt(paramIdx, limit); + } + try (ResultSet rs = ps.executeQuery()) { + if (log.isDebugEnabled()) { + log.debug("Successfully retrieved device subscriptions for application release id " + + appReleaseId + " and device ids " + deviceIds); + } + List subscriptions = new ArrayList<>(); + while (rs.next()) { + DeviceSubscriptionDTO subscription = new DeviceSubscriptionDTO(); + subscription.setId(rs.getInt("ID")); + subscription.setSubscribedBy(rs.getString("SUBSCRIBED_BY")); + subscription.setSubscribedTimestamp(rs.getTimestamp("SUBSCRIBED_AT")); + subscription.setUnsubscribed(rs.getBoolean("IS_UNSUBSCRIBED")); + subscription.setUnsubscribedBy(rs.getString("UNSUBSCRIBED_BY")); + subscription.setUnsubscribedTimestamp(rs.getTimestamp("UNSUBSCRIBED_AT")); + subscription.setActionTriggeredFrom(rs.getString("ACTION_TRIGGERED_FROM")); + subscription.setStatus(rs.getString("STATUS")); + subscription.setDeviceId(rs.getInt("DEVICE_ID")); + subscriptions.add(subscription); + } + return subscriptions; + } + } catch (SQLException e) { + String msg = "Error occurred while running SQL to get device subscription data for application ID: " + appReleaseId + + " and device ids: " + deviceIds + "."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection for getting device subscriptions for " + + "application Id: " + appReleaseId + " and device ids: " + deviceIds + "."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } + + @Override + public List getRoleSubscriptionsByAppReleaseID(int appReleaseId, boolean unsubscribe, int tenantId, int offset, + int limit) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to get role subscriptions related to the given AppReleaseID."); + } + try { + Connection conn = this.getDBConnection(); + List subscriptionEntities = new ArrayList<>(); + + String subscriptionStatusTime = unsubscribe ? "ARS.UNSUBSCRIBED_TIMESTAMP" : "ARS.SUBSCRIBED_TIMESTAMP"; + String sql = "SELECT ARS.ROLE_NAME, " + + "ARS.SUBSCRIBED_BY, " + + "ARS.SUBSCRIBED_TIMESTAMP, " + + "ARS.UNSUBSCRIBED, " + + "ARS.UNSUBSCRIBED_BY, " + + "ARS.UNSUBSCRIBED_TIMESTAMP, " + + "ARS.AP_APP_RELEASE_ID " + + "FROM AP_ROLE_SUBSCRIPTION ARS " + + "WHERE ARS.AP_APP_RELEASE_ID = ? " + + "AND ARS.UNSUBSCRIBED = ? " + + "AND ARS.TENANT_ID = ? " + + "ORDER BY " + subscriptionStatusTime + " DESC " + + "OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + + try (PreparedStatement ps = conn.prepareStatement(sql)) { + ps.setInt(1, appReleaseId); + ps.setBoolean(2, unsubscribe); + ps.setInt(3, tenantId); + ps.setInt(4, offset); + ps.setInt(5, limit); + try (ResultSet rs = ps.executeQuery()) { + SubscriptionEntity subscriptionEntity; + while (rs.next()) { + subscriptionEntity = new SubscriptionEntity(); + subscriptionEntity.setIdentity(rs.getString("ROLE_NAME")); + subscriptionEntity.setSubscribedBy(rs.getString("SUBSCRIBED_BY")); + subscriptionEntity.setSubscribedTimestamp(rs.getTimestamp("SUBSCRIBED_TIMESTAMP")); + subscriptionEntity.setUnsubscribed(rs.getBoolean("UNSUBSCRIBED")); + subscriptionEntity.setUnsubscribedBy(rs.getString("UNSUBSCRIBED_BY")); + subscriptionEntity.setUnsubscribedTimestamp(rs.getTimestamp("UNSUBSCRIBED_TIMESTAMP")); + subscriptionEntity.setApplicationReleaseId(rs.getInt("AP_APP_RELEASE_ID")); + + subscriptionEntities.add(subscriptionEntity); + } + } + return subscriptionEntities; + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection to get role subscriptions for the given UUID."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "SQL Error occurred while getting role subscriptions for the given UUID."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } + + @Override + public List getUserSubscriptionsByAppReleaseID(int appReleaseId, boolean unsubscribe, int tenantId, + int offset, int limit) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to get user subscriptions related to the given UUID."); + } + try { + Connection conn = this.getDBConnection(); + List subscriptionEntities = new ArrayList<>(); + + String subscriptionStatusTime = unsubscribe ? "US.UNSUBSCRIBED_TIMESTAMP" : "US.SUBSCRIBED_TIMESTAMP"; + String sql = "SELECT US.USER_NAME, " + + "US.SUBSCRIBED_BY, " + + "US.SUBSCRIBED_TIMESTAMP, " + + "US.UNSUBSCRIBED, " + + "US.UNSUBSCRIBED_BY, " + + "US.UNSUBSCRIBED_TIMESTAMP, " + + "US.AP_APP_RELEASE_ID " + + "FROM AP_USER_SUBSCRIPTION US " + + "WHERE US.AP_APP_RELEASE_ID = ? " + + "AND US.UNSUBSCRIBED = ? " + + "AND US.TENANT_ID = ? " + + "ORDER BY " + subscriptionStatusTime + " DESC " + + "OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + + try (PreparedStatement ps = conn.prepareStatement(sql)) { + ps.setInt(1, appReleaseId); + ps.setBoolean(2, unsubscribe); + ps.setInt(3, tenantId); + ps.setInt(4, offset); + ps.setInt(5, limit); + try (ResultSet rs = ps.executeQuery()) { + SubscriptionEntity subscriptionEntity; + while (rs.next()) { + subscriptionEntity = new SubscriptionEntity(); + subscriptionEntity.setIdentity(rs.getString("USER_NAME")); + subscriptionEntity.setSubscribedBy(rs.getString("SUBSCRIBED_BY")); + subscriptionEntity.setSubscribedTimestamp(rs.getTimestamp("SUBSCRIBED_TIMESTAMP")); + subscriptionEntity.setUnsubscribed(rs.getBoolean("UNSUBSCRIBED")); + subscriptionEntity.setUnsubscribedBy(rs.getString("UNSUBSCRIBED_BY")); + subscriptionEntity.setUnsubscribedTimestamp(rs.getTimestamp("UNSUBSCRIBED_TIMESTAMP")); + subscriptionEntity.setApplicationReleaseId(rs.getInt("AP_APP_RELEASE_ID")); + + subscriptionEntities.add(subscriptionEntity); + } + } + return subscriptionEntities; + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection to get user subscriptions for the given UUID."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "SQL Error occurred while getting user subscriptions for the given UUID."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } + + @Override + public List getGroupsSubscriptionDetailsByAppReleaseID(int appReleaseId, boolean unsubscribe, int tenantId, int offset, int limit) + throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to get groups related to the given AppReleaseID."); + } + try { + Connection conn = this.getDBConnection(); + List subscriptionEntities = new ArrayList<>(); + + String subscriptionStatusTime = unsubscribe ? "GS.UNSUBSCRIBED_TIMESTAMP" : "GS.SUBSCRIBED_TIMESTAMP"; + String sql = "SELECT GS.GROUP_NAME, " + + "GS.SUBSCRIBED_BY, " + + "GS.SUBSCRIBED_TIMESTAMP, " + + "GS.UNSUBSCRIBED, " + + "GS.UNSUBSCRIBED_BY, " + + "GS.UNSUBSCRIBED_TIMESTAMP, " + + "GS.AP_APP_RELEASE_ID " + + "FROM AP_GROUP_SUBSCRIPTION GS " + + "WHERE GS.AP_APP_RELEASE_ID = ? " + + "AND GS.UNSUBSCRIBED = ? AND GS.TENANT_ID = ? " + + "ORDER BY " + subscriptionStatusTime + " DESC " + + "OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + + try (PreparedStatement ps = conn.prepareStatement(sql)) { + ps.setInt(1, appReleaseId); + ps.setBoolean(2, unsubscribe); + ps.setInt(3, tenantId); + ps.setInt(4, offset); + ps.setInt(5, limit); + + try (ResultSet rs = ps.executeQuery()) { + SubscriptionEntity subscriptionEntity; + while (rs.next()) { + subscriptionEntity = new SubscriptionEntity(); + subscriptionEntity.setIdentity(rs.getString("GROUP_NAME")); + subscriptionEntity.setSubscribedBy(rs.getString("SUBSCRIBED_BY")); + subscriptionEntity.setSubscribedTimestamp(rs.getTimestamp("SUBSCRIBED_TIMESTAMP")); + subscriptionEntity.setUnsubscribed(rs.getBoolean("UNSUBSCRIBED")); + subscriptionEntity.setUnsubscribedBy(rs.getString("UNSUBSCRIBED_BY")); + subscriptionEntity.setUnsubscribedTimestamp(rs.getTimestamp("UNSUBSCRIBED_TIMESTAMP")); + subscriptionEntity.setApplicationReleaseId(rs.getInt("AP_APP_RELEASE_ID")); + + subscriptionEntities.add(subscriptionEntity); + } + } + return subscriptionEntities; + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection to get groups for the given UUID."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "SQL Error occurred while getting groups for the given UUID."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } + + @Override + public List getAllSubscriptionsDetails(int appReleaseId, boolean unsubscribe, int tenantId, + List actionStatus, String actionType, String actionTriggeredBy, + int offset, int limit) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Getting device subscriptions for the application release id " + appReleaseId + " from the database"); + } + String subscriptionStatusTime = unsubscribe ? "DS.UNSUBSCRIBED_TIMESTAMP" : "DS.SUBSCRIBED_TIMESTAMP"; + String actionTriggeredColumn = unsubscribe ? "DS.UNSUBSCRIBED_BY" : "DS.SUBSCRIBED_BY"; + StringBuilder sql = new StringBuilder("SELECT " + + "DS.ID AS ID, " + + "DS.SUBSCRIBED_BY AS SUBSCRIBED_BY, " + + "DS.SUBSCRIBED_TIMESTAMP AS SUBSCRIBED_AT, " + + "DS.UNSUBSCRIBED AS IS_UNSUBSCRIBED, " + + "DS.UNSUBSCRIBED_BY AS UNSUBSCRIBED_BY, " + + "DS.UNSUBSCRIBED_TIMESTAMP AS UNSUBSCRIBED_AT, " + + "DS.ACTION_TRIGGERED_FROM AS ACTION_TRIGGERED_FROM, " + + "DS.STATUS AS STATUS, " + + "DS.DM_DEVICE_ID AS DEVICE_ID " + + "FROM AP_DEVICE_SUBSCRIPTION DS " + + "WHERE DS.AP_APP_RELEASE_ID = ? " + + "AND DS.UNSUBSCRIBED = ? " + + "AND DS.TENANT_ID = ? "); + if (actionStatus != null && !actionStatus.isEmpty()) { + sql.append(" AND DS.STATUS IN (") + .append(actionStatus.stream().map(status -> "?").collect(Collectors.joining(","))).append(") "); + } + if (actionType != null && !actionType.isEmpty()) { + sql.append(" AND DS.ACTION_TRIGGERED_FROM = ? "); + } + if (actionTriggeredBy != null && !actionTriggeredBy.isEmpty()) { + sql.append(" AND ").append(actionTriggeredColumn).append(" LIKE ? "); + } + sql.append("ORDER BY ").append(subscriptionStatusTime).append(" DESC "); + if (limit >= 0 && offset >= 0) { + sql.append("OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"); + } + try { + Connection conn = this.getDBConnection(); + try (PreparedStatement ps = conn.prepareStatement(sql.toString())) { + int paramIdx = 1; + ps.setInt(paramIdx++, appReleaseId); + ps.setBoolean(paramIdx++, unsubscribe); + ps.setInt(paramIdx++, tenantId); + if (actionStatus != null && !actionStatus.isEmpty()) { + for (String status : actionStatus) { + ps.setString(paramIdx++, status); + } + } + if (actionType != null && !actionType.isEmpty()) { + ps.setString(paramIdx++, actionType); + } + if (actionTriggeredBy != null && !actionTriggeredBy.isEmpty()) { + ps.setString(paramIdx++, "%" + actionTriggeredBy + "%"); + } + if (limit >= 0 && offset >= 0) { + ps.setInt(paramIdx++, offset); + ps.setInt(paramIdx, limit); + } + try (ResultSet rs = ps.executeQuery()) { + if (log.isDebugEnabled()) { + log.debug("Successfully retrieved device subscriptions for application release id " + + appReleaseId); + } + List deviceSubscriptions = new ArrayList<>(); + while (rs.next()) { + DeviceSubscriptionDTO subscription = new DeviceSubscriptionDTO(); + subscription.setId(rs.getInt("ID")); + subscription.setSubscribedBy(rs.getString("SUBSCRIBED_BY")); + subscription.setSubscribedTimestamp(rs.getTimestamp("SUBSCRIBED_AT")); + subscription.setUnsubscribed(rs.getBoolean("IS_UNSUBSCRIBED")); + subscription.setUnsubscribedBy(rs.getString("UNSUBSCRIBED_BY")); + subscription.setUnsubscribedTimestamp(rs.getTimestamp("UNSUBSCRIBED_AT")); + subscription.setActionTriggeredFrom(rs.getString("ACTION_TRIGGERED_FROM")); + subscription.setStatus(rs.getString("STATUS")); + subscription.setDeviceId(rs.getInt("DEVICE_ID")); + + deviceSubscriptions.add(subscription); + } + return deviceSubscriptions; + } + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection for getting device subscription for application Id: " + + appReleaseId + "."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while running SQL to get device subscription data for application ID: " + appReleaseId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/SubscriptionManagementHelperUtil.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/SubscriptionManagementHelperUtil.java index e5c3969329..0fbc32b986 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/SubscriptionManagementHelperUtil.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/SubscriptionManagementHelperUtil.java @@ -38,6 +38,19 @@ import java.util.List; import java.util.stream.Collectors; public class SubscriptionManagementHelperUtil { + + /** + * Retrieves device subscription data based on the provided filters. + * + * @param deviceSubscriptionDTOS List of DeviceSubscriptionDTO objects. + * @param deviceSubscriptionFilterCriteria Filter criteria for device subscription. + * @param isUnsubscribed Boolean indicating whether to filter unsubscribed devices. + * @param deviceTypeId Device type ID. + * @param limit Limit for pagination. + * @param offset Offset for pagination. + * @return List of DeviceSubscription objects. + * @throws DeviceManagementException If an error occurs during device management. + */ public static List getDeviceSubscriptionData(List deviceSubscriptionDTOS, DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria, boolean isUnsubscribed, int deviceTypeId, int limit, int offset) @@ -52,6 +65,15 @@ public class SubscriptionManagementHelperUtil { return populateDeviceData(deviceSubscriptionDTOS, devices, isUnsubscribed); } + /** + * Retrieves the total count of device subscriptions based on the provided filters. + * + * @param deviceSubscriptionDTOS List of DeviceSubscriptionDTO objects. + * @param deviceSubscriptionFilterCriteria Filter criteria for device subscription. + * @param deviceTypeId Device type ID. + * @return int Total count of device subscriptions. + * @throws DeviceManagementException If an error occurs during device management. + */ public static int getTotalDeviceSubscriptionCount(List deviceSubscriptionDTOS, DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria, int deviceTypeId) throws DeviceManagementException { @@ -64,6 +86,14 @@ public class SubscriptionManagementHelperUtil { return HelperUtil.getDeviceManagementProviderService().getDeviceCountByDeviceIds(paginationRequest, deviceIds); } + /** + * Populates device subscription data based on the provided devices and subscription DTOs. + * + * @param deviceSubscriptionDTOS List of DeviceSubscriptionDTO objects. + * @param devices List of Device objects. + * @param isUnsubscribed Boolean indicating whether to filter unsubscribed devices. + * @return List of DeviceSubscription objects. + */ private static List populateDeviceData(List deviceSubscriptionDTOS, List devices, boolean isUnsubscribed) { List deviceSubscriptions = new ArrayList<>(); @@ -88,6 +118,13 @@ public class SubscriptionManagementHelperUtil { return deviceSubscriptions; } + /** + * Creates a SubscriptionData object based on the provided subscription DTO. + * + * @param isUnsubscribed Boolean indicating whether to filter unsubscribed devices. + * @param deviceSubscriptionDTO DeviceSubscriptionDTO object. + * @return SubscriptionData object. + */ private static SubscriptionData getSubscriptionData(boolean isUnsubscribed, DeviceSubscriptionDTO deviceSubscriptionDTO) { SubscriptionData subscriptionData = new SubscriptionData(); subscriptionData.setTriggeredBy(isUnsubscribed ? deviceSubscriptionDTO.getUnsubscribedBy() : @@ -99,16 +136,36 @@ public class SubscriptionManagementHelperUtil { return subscriptionData; } + /** + * Retrieves the device subscription status based on the provided subscription info. + * + * @param subscriptionInfo SubscriptionInfo object. + * @return Device subscription status. + */ public static String getDeviceSubscriptionStatus(SubscriptionInfo subscriptionInfo) { return getDeviceSubscriptionStatus(subscriptionInfo.getDeviceSubscriptionFilterCriteria(). getFilteringDeviceSubscriptionStatus(), subscriptionInfo.getDeviceSubscriptionStatus()); } + /** + * Retrieves the device subscription status based on the provided filter and status. + * + * @param deviceSubscriptionStatusFilter Filtered device subscription status. + * @param deviceSubscriptionStatus Device subscription status. + * @return Device subscription status. + */ public static String getDeviceSubscriptionStatus(String deviceSubscriptionStatusFilter, String deviceSubscriptionStatus) { return (deviceSubscriptionStatusFilter != null && !deviceSubscriptionStatusFilter.isEmpty()) ? deviceSubscriptionStatusFilter : deviceSubscriptionStatus; } + /** + * Retrieves subscription statistics based on the provided subscription statistics DTO and device count. + * + * @param subscriptionStatisticDTO SubscriptionStatisticDTO object. + * @param allDeviceCount Total count of all devices. + * @return SubscriptionStatistics object. + */ public static SubscriptionStatistics getSubscriptionStatistics(SubscriptionStatisticDTO subscriptionStatisticDTO, int allDeviceCount) { SubscriptionStatistics subscriptionStatistics = new SubscriptionStatistics(); subscriptionStatistics.setCompletedPercentage( @@ -124,6 +181,13 @@ public class SubscriptionManagementHelperUtil { return subscriptionStatistics; } + /** + * Calculates the percentages. + * + * @param numerator Numerator value. + * @param denominator Denominator value. + * @return Calculated percentage. + */ public static float getPercentage(int numerator, int denominator) { if (denominator <= 0) { return 0.0f; @@ -131,6 +195,12 @@ public class SubscriptionManagementHelperUtil { return ((float) numerator / (float) denominator) * 100; } + /** + * Retrieves database subscription statuses based on the provided device subscription status. + * + * @param deviceSubscriptionStatus Device subscription status. + * @return List of database subscription statuses. + */ public static List getDBSubscriptionStatus(String deviceSubscriptionStatus) { return SubscriptionMetadata.deviceSubscriptionStatusToDBSubscriptionStatusMap.get(deviceSubscriptionStatus); } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/SubscriptionManagementServiceProvider.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/SubscriptionManagementServiceProvider.java index 61602d7317..c2fd88b25e 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/SubscriptionManagementServiceProvider.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/SubscriptionManagementServiceProvider.java @@ -33,14 +33,22 @@ public class SubscriptionManagementServiceProvider { private SubscriptionManagementServiceProvider() { } - public static SubscriptionManagementServiceProvider getInstance() { - return SubscriptionManagementProviderServiceHolder.INSTANCE; - } - + /** + * Retrieves the appropriate SubscriptionManagementHelperService based on the provided SubscriptionInfo. + * + * @param subscriptionInfo SubscriptionInfo object containing the subscription type. + * @return SubscriptionManagementHelperService implementation based on the subscription type. + */ public SubscriptionManagementHelperService getSubscriptionManagementHelperService(SubscriptionInfo subscriptionInfo) { return getSubscriptionManagementHelperService(subscriptionInfo.getSubscriptionType()); } + /** + * Retrieves the appropriate SubscriptionManagementHelperService based on the subscription type. + * + * @param subscriptionType Type of the subscription. + * @return SubscriptionManagementHelperService implementation based on the subscription type. + */ private SubscriptionManagementHelperService getSubscriptionManagementHelperService(String subscriptionType) { if (Objects.equals(subscriptionType, SubscriptionMetadata.SubscriptionTypes.ROLE)) return RoleBasedSubscriptionManagementHelperServiceImpl.getInstance(); diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index 7ae29f29d8..52feae35d4 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -3306,7 +3306,9 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { List filteredDeviceIds = new ArrayList<>(); try { Connection connection = getConnection(); - String sql = "SELECT ID AS DEVICE_ID FROM DM_DEVICE WHERE TENANT_ID = ?"; + String sql = "SELECT ID AS DEVICE_ID " + + "FROM DM_DEVICE " + + "WHERE TENANT_ID = ?"; if (deviceIds != null && !deviceIds.isEmpty()) { sql += " AND ID NOT IN ( " + deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ")"; @@ -3349,8 +3351,12 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { String deviceIdStringList = deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")); try { Connection connection = getConnection(); - String sql = "SELECT ID AS DEVICE_ID FROM DM_DEVICE WHERE ID IN " + - "(" + deviceIdStringList + ") AND TENANT_ID = ? LIMIT ? OFFSET ?"; + String sql = "SELECT ID AS DEVICE_ID " + + "FROM DM_DEVICE " + + "WHERE ID IN (" + deviceIdStringList + ")" + + " AND TENANT_ID = ? " + + "LIMIT ? " + + "OFFSET ?"; try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { int paraIdx = 1; for (Integer deviceId : deviceIds) { @@ -3380,7 +3386,9 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { int deviceCount = 0; try { Connection connection = getConnection(); - String sql = "SELECT COUNT(ID) AS COUNT FROM DM_DEVICE WHERE TENANT_ID = ?"; + String sql = "SELECT COUNT(ID) AS COUNT " + + "FROM DM_DEVICE " + + "WHERE TENANT_ID = ?"; if (deviceIds != null && !deviceIds.isEmpty()) { sql += " AND ID NOT IN ( " + deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ")"; @@ -3422,8 +3430,16 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { boolean isDeviceNameProvided = false; try { Connection connection = getConnection(); - String sql = "SELECT e.DEVICE_ID, d.DEVICE_IDENTIFICATION, e.STATUS, e.OWNER, d.NAME AS DEVICE_NAME, " + - "e.DEVICE_TYPE, e.OWNERSHIP, e.DATE_OF_LAST_UPDATE FROM DM_DEVICE d INNER JOIN DM_ENROLMENT e " + + String sql = "SELECT e.DEVICE_ID, " + + "d.DEVICE_IDENTIFICATION, " + + "e.STATUS, " + + "e.OWNER, " + + "d.NAME AS DEVICE_NAME, " + + "e.DEVICE_TYPE, " + + "e.OWNERSHIP, " + + "e.DATE_OF_LAST_UPDATE " + + "FROM DM_DEVICE d " + + "INNER JOIN DM_ENROLMENT e " + "ON d.ID = e.DEVICE_ID " + "WHERE d.DEVICE_TYPE_ID = ? " + "AND d.TENANT_ID = ? " + @@ -3505,7 +3521,9 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { boolean isDeviceNameProvided = false; try { Connection connection = getConnection(); - String sql = "SELECT COUNT(DISTINCT e.DEVICE_ID) FROM DM_DEVICE d INNER JOIN DM_ENROLMENT e " + + String sql = "SELECT COUNT(DISTINCT e.DEVICE_ID) " + + "FROM DM_DEVICE d " + + "INNER JOIN DM_ENROLMENT e " + "ON d.ID = e.DEVICE_ID " + "WHERE e.TENANT_ID = ? " + "AND e.DEVICE_ID IN (" + deviceIdStringList+ ") " + diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java index bf539199c9..e116df7490 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java @@ -42,6 +42,7 @@ import java.util.Date; import java.util.List; import java.util.StringJoiner; import java.util.Map; +import java.util.stream.Collectors; /** * This class holds the generic implementation of DeviceDAO which can be used to support ANSI db syntax. @@ -1858,4 +1859,156 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { } } + @Override + public List getDevicesByDeviceIds(PaginationRequest paginationRequest, List deviceIds, int tenantId) + throws DeviceManagementDAOException { + List devices = new ArrayList<>(); + if (deviceIds == null || deviceIds.isEmpty()) return devices; + + String deviceIdStringList = deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")); + boolean isOwnerProvided = false; + boolean isDeviceStatusProvided = false; + boolean isDeviceNameProvided = false; + try { + Connection connection = getConnection(); + String sql = "SELECT e.DEVICE_ID, " + + "d.DEVICE_IDENTIFICATION, " + + "e.STATUS, " + + "e.OWNER, " + + "d.NAME AS DEVICE_NAME, " + + "e.DEVICE_TYPE, " + + "e.OWNERSHIP, " + + "e.DATE_OF_LAST_UPDATE " + + "FROM DM_DEVICE d " + + "INNER JOIN DM_ENROLMENT e " + + "WHERE d.ID = e.DEVICE_ID " + + "AND d.TENANT_ID = ? " + + "AND e.DEVICE_ID IN (" + deviceIdStringList+ ") " + + "AND e.STATUS NOT IN ('DELETED', 'REMOVED')"; + if (paginationRequest.getOwner() != null) { + sql = sql + " AND e.OWNER LIKE ?"; + isOwnerProvided = true; + } + if (paginationRequest.getDeviceStatus() != null) { + sql = sql + " AND e.STATUS = ?"; + isDeviceStatusProvided = true; + } + if (paginationRequest.getDeviceName() != null) { + sql = sql + " AND d.NAME LIKE ?"; + isDeviceNameProvided = true; + } + sql = sql + " LIMIT ? OFFSET ?"; + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + int parameterIdx = 1; + preparedStatement.setInt(parameterIdx++, tenantId); + for (Integer deviceId : deviceIds) { + preparedStatement.setInt(parameterIdx++, deviceId); + } + if (isOwnerProvided) + preparedStatement.setString(parameterIdx++, "%" + paginationRequest.getOwner() + "%"); + if (isDeviceStatusProvided) + preparedStatement.setString(parameterIdx++, paginationRequest.getDeviceStatus()); + if (isDeviceNameProvided) + preparedStatement.setString(parameterIdx++, "%" + paginationRequest.getDeviceName() + "%"); + preparedStatement.setInt(parameterIdx++, paginationRequest.getRowCount()); + preparedStatement.setInt(parameterIdx, paginationRequest.getStartIndex()); + try(ResultSet resultSet = preparedStatement.executeQuery()) { + Device device; + while(resultSet.next()) { + device = new Device(); + device.setId(resultSet.getInt("DEVICE_ID")); + device.setDeviceIdentifier(resultSet.getString("DEVICE_IDENTIFICATION")); + device.setName(resultSet.getString("DEVICE_NAME")); + device.setType(resultSet.getString("DEVICE_TYPE")); + EnrolmentInfo enrolmentInfo = new EnrolmentInfo(); + enrolmentInfo.setStatus(EnrolmentInfo.Status.valueOf(resultSet.getString("STATUS"))); + enrolmentInfo.setOwner(resultSet.getString("OWNER")); + enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.valueOf(resultSet.getString("OWNERSHIP"))); + enrolmentInfo.setDateOfLastUpdate(resultSet.getTimestamp("DATE_OF_LAST_UPDATE").getTime()); + device.setEnrolmentInfo(enrolmentInfo); + devices.add(device); + } + } + } + return devices; + } catch (SQLException e) { + String msg = "Error occurred while retrieving devices for device ids in: " + deviceIds; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + } + + @Override + public List getDevicesInGivenIdList(PaginationRequest request, List deviceIds, int tenantId) + throws DeviceManagementDAOException { + List filteredDeviceIds = new ArrayList<>(); + if (deviceIds == null || deviceIds.isEmpty()) return filteredDeviceIds; + String deviceIdStringList = deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")); + try { + Connection connection = getConnection(); + String sql = "SELECT ID AS DEVICE_ID " + + "FROM DM_DEVICE WHERE ID IN " + + "(" + deviceIdStringList + ") " + + "AND TENANT_ID = ? " + + "LIMIT ? " + + "OFFSET ?"; + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + int paraIdx = 1; + for (Integer deviceId : deviceIds) { + preparedStatement.setInt(paraIdx++, deviceId); + } + preparedStatement.setInt(paraIdx++, tenantId); + preparedStatement.setInt(paraIdx++, request.getRowCount()); + preparedStatement.setInt(paraIdx, request.getStartIndex()); + try (ResultSet resultSet = preparedStatement.executeQuery()) { + while (resultSet.next()) { + filteredDeviceIds.add(resultSet.getInt("DEVICE_ID")); + } + } + return filteredDeviceIds; + } + } catch (SQLException e) { + String msg = "Error occurred while retrieving device ids in: " + filteredDeviceIds; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + } + + @Override + public List getDevicesNotInGivenIdList(PaginationRequest request, List deviceIds, int tenantId) + throws DeviceManagementDAOException { + List filteredDeviceIds = new ArrayList<>(); + try { + Connection connection = getConnection(); + String sql = "SELECT ID AS DEVICE_ID " + + "FROM DM_DEVICE" + + " WHERE TENANT_ID = ?"; + + if (deviceIds != null && !deviceIds.isEmpty()) { + sql += " AND ID NOT IN ( " + deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ")"; + } + sql += " LIMIT ? OFFSET ?"; + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + int paraIdx = 1; + preparedStatement.setInt(paraIdx++, tenantId); + if (deviceIds != null && !deviceIds.isEmpty()) { + for (Integer deviceId : deviceIds) { + preparedStatement.setInt(paraIdx++, deviceId); + } + } + preparedStatement.setInt(paraIdx++, request.getRowCount()); + preparedStatement.setInt(paraIdx, request.getStartIndex()); + try (ResultSet resultSet = preparedStatement.executeQuery()) { + while (resultSet.next()) { + filteredDeviceIds.add(resultSet.getInt("DEVICE_ID")); + } + } + return filteredDeviceIds; + } + } catch (SQLException e) { + String msg = "Error occurred while retrieving device ids not in: " + filteredDeviceIds; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + } } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java index 3effd783c1..d1649f8528 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java @@ -20,6 +20,7 @@ package io.entgra.device.mgt.core.device.mgt.core.dao.impl.device; import io.entgra.device.mgt.core.device.mgt.common.Device; import io.entgra.device.mgt.core.device.mgt.common.EnrolmentInfo; +import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest; import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -28,7 +29,9 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; /** * This class holds the generic implementation of DeviceDAO which can be used to support ANSI db syntax. @@ -69,4 +72,162 @@ public class OracleDeviceDAOImpl extends SQLServerDeviceDAOImpl { } } + @Override + public List getDevicesByDeviceIds(PaginationRequest paginationRequest, List deviceIds, int tenantId) + throws DeviceManagementDAOException { + List devices = new ArrayList<>(); + if (deviceIds == null || deviceIds.isEmpty()) return devices; + + String deviceIdStringList = deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")); + boolean isOwnerProvided = false; + boolean isDeviceStatusProvided = false; + boolean isDeviceNameProvided = false; + try { + Connection connection = getConnection(); + String sql = "SELECT e.DEVICE_ID, " + + "d.DEVICE_IDENTIFICATION, " + + "e.STATUS, " + + "e.OWNER, " + + "d.NAME AS DEVICE_NAME, " + + "e.DEVICE_TYPE, " + + "e.OWNERSHIP, " + + "e.DATE_OF_LAST_UPDATE " + + "FROM DM_DEVICE d " + + "INNER JOIN DM_ENROLMENT e " + + "ON d.ID = e.DEVICE_ID " + + "WHERE d.TENANT_ID = ? " + + "AND e.DEVICE_ID IN (" + deviceIdStringList + ") " + + "AND e.STATUS NOT IN ('DELETED', 'REMOVED')"; + if (paginationRequest.getOwner() != null) { + sql += " AND e.OWNER LIKE ?"; + isOwnerProvided = true; + } + if (paginationRequest.getDeviceStatus() != null) { + sql += " AND e.STATUS = ?"; + isDeviceStatusProvided = true; + } + if (paginationRequest.getDeviceName() != null) { + sql += " AND d.NAME LIKE ?"; + isDeviceNameProvided = true; + } + sql += " OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + int parameterIdx = 1; + preparedStatement.setInt(parameterIdx++, tenantId); + for (Integer deviceId : deviceIds) { + preparedStatement.setInt(parameterIdx++, deviceId); + } + if (isOwnerProvided) { + preparedStatement.setString(parameterIdx++, "%" + paginationRequest.getOwner() + "%"); + } + if (isDeviceStatusProvided) { + preparedStatement.setString(parameterIdx++, paginationRequest.getDeviceStatus()); + } + if (isDeviceNameProvided) { + preparedStatement.setString(parameterIdx++, "%" + paginationRequest.getDeviceName() + "%"); + } + preparedStatement.setInt(parameterIdx++, paginationRequest.getStartIndex()); + preparedStatement.setInt(parameterIdx, paginationRequest.getRowCount()); + try (ResultSet resultSet = preparedStatement.executeQuery()) { + while (resultSet.next()) { + Device device = new Device(); + device.setId(resultSet.getInt("DEVICE_ID")); + device.setDeviceIdentifier(resultSet.getString("DEVICE_IDENTIFICATION")); + device.setName(resultSet.getString("DEVICE_NAME")); + device.setType(resultSet.getString("DEVICE_TYPE")); + EnrolmentInfo enrolmentInfo = new EnrolmentInfo(); + enrolmentInfo.setStatus(EnrolmentInfo.Status.valueOf(resultSet.getString("STATUS"))); + enrolmentInfo.setOwner(resultSet.getString("OWNER")); + enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.valueOf(resultSet.getString("OWNERSHIP"))); + enrolmentInfo.setDateOfLastUpdate(resultSet.getTimestamp("DATE_OF_LAST_UPDATE").getTime()); + device.setEnrolmentInfo(enrolmentInfo); + devices.add(device); + } + } + } + return devices; + } catch (SQLException e) { + String msg = "Error occurred while retrieving devices for device ids in: " + deviceIds; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + } + + @Override + public List getDevicesInGivenIdList(PaginationRequest request, List deviceIds, int tenantId) + throws DeviceManagementDAOException { + List filteredDeviceIds = new ArrayList<>(); + if (deviceIds == null || deviceIds.isEmpty()) return filteredDeviceIds; + + String deviceIdStringList = deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")); + try { + Connection connection = getConnection(); + String sql = "SELECT ID AS DEVICE_ID " + + "FROM DM_DEVICE WHERE ID IN " + + "(" + deviceIdStringList + ") " + + "AND TENANT_ID = ? " + + "OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + int paraIdx = 1; + for (Integer deviceId : deviceIds) { + preparedStatement.setInt(paraIdx++, deviceId); + } + preparedStatement.setInt(paraIdx++, tenantId); + preparedStatement.setInt(paraIdx++, request.getStartIndex()); + preparedStatement.setInt(paraIdx, request.getRowCount()); + + try (ResultSet resultSet = preparedStatement.executeQuery()) { + while (resultSet.next()) { + filteredDeviceIds.add(resultSet.getInt("DEVICE_ID")); + } + } + return filteredDeviceIds; + } + } catch (SQLException e) { + String msg = "Error occurred while retrieving device ids in: " + deviceIds; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + } + + @Override + public List getDevicesNotInGivenIdList(PaginationRequest request, List deviceIds, int tenantId) + throws DeviceManagementDAOException { + List filteredDeviceIds = new ArrayList<>(); + try { + Connection connection = getConnection(); + String sql = "SELECT ID AS DEVICE_ID " + + "FROM DM_DEVICE " + + "WHERE TENANT_ID = ?"; + + if (deviceIds != null && !deviceIds.isEmpty()) { + sql += " AND ID NOT IN (" + deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ")"; + } + + sql += "OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + int paraIdx = 1; + preparedStatement.setInt(paraIdx++, tenantId); + if (deviceIds != null && !deviceIds.isEmpty()) { + for (Integer deviceId : deviceIds) { + preparedStatement.setInt(paraIdx++, deviceId); + } + } + preparedStatement.setInt(paraIdx++, request.getStartIndex()); + preparedStatement.setInt(paraIdx, request.getRowCount()); + + try (ResultSet resultSet = preparedStatement.executeQuery()) { + while (resultSet.next()) { + filteredDeviceIds.add(resultSet.getInt("DEVICE_ID")); + } + } + return filteredDeviceIds; + } + } catch (SQLException e) { + String msg = "Error occurred while retrieving device ids not in: " + deviceIds; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + } }