Fix device count issues in subscription view

remotes/1723019596460615494/tmp_refs/heads/master
prathabanKavin 4 months ago
parent 56231e5a7e
commit 8a24255089

@ -25,7 +25,11 @@ import io.entgra.device.mgt.core.application.mgt.core.dao.SubscriptionDAO;
import io.entgra.device.mgt.core.application.mgt.core.dao.impl.AbstractDAOImpl;
import io.entgra.device.mgt.core.application.mgt.core.exception.UnexpectedServerErrorException;
import io.entgra.device.mgt.core.application.mgt.core.util.DAOUtil;
import io.entgra.device.mgt.core.application.mgt.core.util.HelperUtil;
import io.entgra.device.mgt.core.device.mgt.common.EnrolmentInfo;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException;
import io.entgra.device.mgt.core.device.mgt.common.operation.mgt.Activity;
import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import io.entgra.device.mgt.core.application.mgt.common.ExecutionStatus;
@ -2399,23 +2403,41 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
}
@Override
public int getAllSubscriptionCount(int appReleaseId, int tenantId)
throws ApplicationManagementDAOException {
public int getAllSubscriptionCount(int appReleaseId, int tenantId) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) {
log.debug("Getting all subscriptions count for the application appReleaseId " + appReleaseId
+ " from the database");
log.debug("Getting all subscriptions count for the application appReleaseId " + appReleaseId + " from the database");
}
List<String> allowingDeviceStatuses = new ArrayList<>();
allowingDeviceStatuses.add(EnrolmentInfo.Status.ACTIVE.toString());
allowingDeviceStatuses.add(EnrolmentInfo.Status.INACTIVE.toString());
allowingDeviceStatuses.add(EnrolmentInfo.Status.UNREACHABLE.toString());
DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService();
try {
Connection conn = this.getDBConnection();
List<Integer> deviceIds = deviceManagementProviderService.getDeviceIdsByStatus(allowingDeviceStatuses);
if (deviceIds.isEmpty()) {
return 0;
}
StringBuilder idList = new StringBuilder();
for (int i = 0; i < deviceIds.size(); i++) {
idList.append("?");
if (i < deviceIds.size() - 1) {
idList.append(",");
}
}
String sql = "SELECT COUNT(*) AS count " +
"FROM AP_DEVICE_SUBSCRIPTION " +
"WHERE AP_APP_RELEASE_ID = ? " +
"AND TENANT_ID = ? " +
"AND UNSUBSCRIBED = FALSE";
"AND UNSUBSCRIBED = FALSE " +
"AND DM_DEVICE_ID IN (" + idList.toString() + ")";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setInt(1, appReleaseId);
ps.setInt(2, tenantId);
for (int i = 0; i < deviceIds.size(); i++) {
ps.setInt(3 + i, deviceIds.get(i));
}
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
@ -2429,7 +2451,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
log.error(msg, e);
throw new ApplicationManagementDAOException(msg, e);
}
} catch (DBConnectionException e) {
} catch (DBConnectionException | DeviceManagementException e) {
String msg = "Error occurred while obtaining the DB connection for getting all subscriptions count for appReleaseId: "
+ appReleaseId + ".";
log.error(msg, e);
@ -2438,23 +2460,41 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
}
@Override
public int getAllUnsubscriptionCount(int appReleaseId, int tenantId)
throws ApplicationManagementDAOException {
public int getAllUnsubscriptionCount(int appReleaseId, int tenantId) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) {
log.debug("Getting all unsubscription count for the application appReleaseId " + appReleaseId
+ " from the database");
log.debug("Getting all unsubscription count for the application appReleaseId " + appReleaseId + " from the database");
}
List<String> allowingDeviceStatuses = new ArrayList<>();
allowingDeviceStatuses.add(EnrolmentInfo.Status.ACTIVE.toString());
allowingDeviceStatuses.add(EnrolmentInfo.Status.INACTIVE.toString());
allowingDeviceStatuses.add(EnrolmentInfo.Status.UNREACHABLE.toString());
DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService();
try {
Connection conn = this.getDBConnection();
List<Integer> deviceIds = deviceManagementProviderService.getDeviceIdsByStatus(allowingDeviceStatuses);
if (deviceIds.isEmpty()) {
return 0;
}
StringBuilder idList = new StringBuilder();
for (int i = 0; i < deviceIds.size(); i++) {
idList.append("?");
if (i < deviceIds.size() - 1) {
idList.append(",");
}
}
String sql = "SELECT COUNT(*) AS count " +
"FROM AP_DEVICE_SUBSCRIPTION " +
"WHERE AP_APP_RELEASE_ID = ? " +
"AND TENANT_ID = ? " +
"AND UNSUBSCRIBED = TRUE";
"AND UNSUBSCRIBED = TRUE " +
"AND DM_DEVICE_ID IN (" + idList.toString() + ")";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setInt(1, appReleaseId);
ps.setInt(2, tenantId);
for (int i = 0; i < deviceIds.size(); i++) {
ps.setInt(3 + i, deviceIds.get(i));
}
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
@ -2468,7 +2508,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
log.error(msg, e);
throw new ApplicationManagementDAOException(msg, e);
}
} catch (DBConnectionException e) {
} catch (DBConnectionException | DeviceManagementException e) {
String msg = "Error occurred while obtaining the DB connection for getting all unsubscription count for appReleaseId: "
+ appReleaseId + ".";
log.error(msg, e);

@ -18,6 +18,7 @@
package io.entgra.device.mgt.core.device.mgt.core.dao;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException;
import org.apache.commons.collections.map.SingletonMap;
import io.entgra.device.mgt.core.device.mgt.common.*;
import io.entgra.device.mgt.core.device.mgt.common.EnrolmentInfo.Status;
@ -879,4 +880,13 @@ public interface DeviceDAO {
int getDeviceCountByDeviceIds(PaginationRequest paginationRequest, List<Integer> deviceIds, int tenantId)
throws DeviceManagementDAOException;
/**
* This method is used to get device count that are not within a specific group.
*
* @param statuses Device statuses to be filtered
* @return deviceIds
* @throws DeviceManagementException
*/
List<Integer> getDeviceIdsByStatus(List<String> statuses) throws DeviceManagementException;
}

@ -18,6 +18,7 @@
package io.entgra.device.mgt.core.device.mgt.core.dao.impl;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException;
import org.apache.commons.collections.map.SingletonMap;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.LogFactory;
@ -3484,4 +3485,39 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
}
}
@Override
public List<Integer> getDeviceIdsByStatus(List<String> statuses) throws DeviceManagementException {
StringBuilder deviceFilters = new StringBuilder();
for (int i = 0; i < statuses.size(); i++) {
deviceFilters.append("?");
if (i < statuses.size() - 1) {
deviceFilters.append(",");
}
}
try {
Connection conn = getConnection();
String sql = "SELECT DEVICE_ID " +
"FROM DM_ENROLMENT " +
"WHERE STATUS IN (" + deviceFilters.toString() + ")";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
for (int i = 0; i < statuses.size(); i++) {
ps.setString(i + 1, statuses.get(i));
}
try (ResultSet rs = ps.executeQuery()) {
List<Integer> deviceIds = new ArrayList<>();
while (rs.next()) {
deviceIds.add(rs.getInt("DEVICE_ID"));
}
return deviceIds;
}
}
} catch (SQLException e) {
String msg = "Error occurred while running SQL to get device IDs by status.";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
}
}
}

@ -1163,4 +1163,12 @@ public interface DeviceManagementProviderService {
throws DeviceManagementException;
int getDeviceCountByDeviceIds(PaginationRequest paginationRequest, List<Integer> deviceIds)
throws DeviceManagementException;
/**
* This method is to get Device ids by statuses
* @param statuses statuses to be filtered.
* @return deviceIds
* @throws DeviceManagementException if any service level or DAO level error occurs.
*/
List<Integer> getDeviceIdsByStatus(List<String> statuses) throws DeviceManagementException;
}

@ -5706,4 +5706,28 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
DeviceManagementDAOFactory.closeConnection();
}
}
@Override
public List<Integer> getDeviceIdsByStatus(List<String> statuses) throws DeviceManagementException {
if (statuses == null || statuses.isEmpty()) {
String msg = "Received null or empty list for statuses";
log.error(msg);
throw new DeviceManagementException(msg);
}
try {
DeviceManagementDAOFactory.openConnection();
return deviceDAO.getDeviceIdsByStatus(statuses);
} catch (DeviceManagementException e) {
String msg = "Error encountered while getting device IDs for statuses: " + statuses;
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} catch (SQLException e) {
String msg = "Error encountered while getting the database connection";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
}

Loading…
Cancel
Save