Fix all,device,group removed devices

geofence-revoke-fix
prathabanKavin 5 months ago
parent f9cea050ae
commit 68a4de92d6

@ -126,5 +126,5 @@ public interface EnrollmentDAO {
* @return {@link OwnerWithDeviceDTO} which contains a list of devices related to a user
* @throws DeviceManagementDAOException if an error occurs while fetching the data
*/
List<DeviceDetailsDTO> getDevicesByTenantId(int tenantId) throws DeviceManagementDAOException;
List<DeviceDetailsDTO> getDevicesByTenantId(int tenantId, List<String> allowingDeviceStatuses) throws DeviceManagementDAOException;
}

@ -479,7 +479,7 @@ public interface GroupDAO {
* @return {@link GroupDetailsDTO} which containing group details and a list of device IDs
* @throws GroupManagementDAOException if an error occurs while retrieving the group details and devices
*/
GroupDetailsDTO getGroupDetailsWithDevices(String groupName, int tenantId, int offset, int limit)
GroupDetailsDTO getGroupDetailsWithDevices(String groupName, List<String> allowingDeviceStatuses, int tenantId, int offset, int limit)
throws GroupManagementDAOException;
}

@ -654,18 +654,34 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO {
}
@Override
public List<DeviceDetailsDTO> getDevicesByTenantId(int tenantId)
public List<DeviceDetailsDTO> getDevicesByTenantId(int tenantId, List<String> allowingDeviceStatuses)
throws DeviceManagementDAOException {
List<DeviceDetailsDTO> devices = new ArrayList<>();
if (allowingDeviceStatuses.isEmpty()) {
return devices;
}
StringBuilder deviceFilters = new StringBuilder();
for (int i = 0; i < allowingDeviceStatuses.size(); i++) {
deviceFilters.append("?");
if (i < allowingDeviceStatuses.size() - 1) {
deviceFilters.append(",");
}
}
String sql = "SELECT DEVICE_ID, OWNER, STATUS, DEVICE_TYPE, DEVICE_IDENTIFICATION " +
"FROM DM_ENROLMENT " +
"WHERE TENANT_ID = ?";
"WHERE TENANT_ID = ? AND STATUS IN (" + deviceFilters.toString() + ")";
Connection conn = null;
try {
conn = this.getConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, tenantId);
int index = 1;
stmt.setInt(index++, tenantId);
for (String status : allowingDeviceStatuses) {
stmt.setString(index++, status);
}
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
@ -687,4 +703,5 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO {
return devices;
}
}

@ -1441,7 +1441,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
}
@Override
public GroupDetailsDTO getGroupDetailsWithDevices(String groupName, int tenantId, int offset, int limit)
public GroupDetailsDTO getGroupDetailsWithDevices(String groupName, List<String> allowedStatuses, int tenantId, int offset, int limit)
throws GroupManagementDAOException {
if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to get group details and device IDs for group: " + groupName);
@ -1454,6 +1454,14 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
Map<Integer, String> deviceTypes = new HashMap<>();
Map<Integer, String> deviceIdentifiers = new HashMap<>();
StringBuilder deviceFilters = new StringBuilder();
for (int i = 0; i < allowedStatuses.size(); i++) {
deviceFilters.append("?");
if (i < allowedStatuses.size() - 1) {
deviceFilters.append(",");
}
}
String sql =
"SELECT " +
" g.ID AS GROUP_ID, " +
@ -1473,16 +1481,21 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
"WHERE " +
" g.GROUP_NAME = ? " +
" AND g.TENANT_ID = ? " +
" AND e.STATUS IN (" + deviceFilters.toString() + ") " +
"LIMIT ? OFFSET ?";
Connection conn = null;
try {
conn = GroupManagementDAOFactory.getConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, groupName);
stmt.setInt(2, tenantId);
stmt.setInt(3, limit);
stmt.setInt(4, offset);
int index = 1;
stmt.setString(index++, groupName);
stmt.setInt(index++, tenantId);
for (String status : allowedStatuses) {
stmt.setString(index++, status);
}
stmt.setInt(index++, limit);
stmt.setInt(index++, offset);
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
@ -1500,19 +1513,19 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
deviceIdentifiers.put(deviceId, rs.getString("DEVICE_IDENTIFICATION"));
}
}
groupDetails.setDeviceIds(deviceIds);
groupDetails.setDeviceCount(deviceIds.size());
groupDetails.setDeviceOwners(deviceOwners);
groupDetails.setDeviceStatuses(deviceStatuses);
groupDetails.setDeviceNames(deviceNames);
groupDetails.setDeviceTypes(deviceTypes);
groupDetails.setDeviceIdentifiers(deviceIdentifiers);
return groupDetails;
groupDetails.setDeviceIds(deviceIds);
groupDetails.setDeviceCount(deviceIds.size());
groupDetails.setDeviceOwners(deviceOwners);
groupDetails.setDeviceStatuses(deviceStatuses);
groupDetails.setDeviceNames(deviceNames);
groupDetails.setDeviceTypes(deviceTypes);
groupDetails.setDeviceIdentifiers(deviceIdentifiers);
return groupDetails;
}
} catch (SQLException e) {
String msg = "Error occurred while retrieving group details and device IDs for group: " + groupName;
log.error(msg, e);
throw new GroupManagementDAOException(msg, e);
}
} catch (SQLException e) {
String msg = "Error occurred while retrieving group details and device IDs for group: " + groupName;
log.error(msg, e);
throw new GroupManagementDAOException(msg, e);
}
}
}

@ -5418,9 +5418,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
@Override
public List<DeviceDetailsDTO> getDevicesByTenantId(int tenantId) throws DeviceManagementDAOException {
List<DeviceDetailsDTO> devices;
List<String> allowingDeviceStatuses = new ArrayList<>();
allowingDeviceStatuses.add(EnrolmentInfo.Status.ACTIVE.toString());
allowingDeviceStatuses.add(EnrolmentInfo.Status.INACTIVE.toString());
allowingDeviceStatuses.add(EnrolmentInfo.Status.UNREACHABLE.toString());
try {
DeviceManagementDAOFactory.openConnection();
devices = enrollmentDAO.getDevicesByTenantId(tenantId);
devices = enrollmentDAO.getDevicesByTenantId(tenantId, allowingDeviceStatuses);
if (devices == null || devices.isEmpty()) {
String msg = "No devices found for tenant ID: " + tenantId;
log.error(msg);

@ -18,6 +18,7 @@
package io.entgra.device.mgt.core.device.mgt.core.service;
import io.entgra.device.mgt.core.device.mgt.common.*;
import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroup;
import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroupConstants;
import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroupRoleWrapper;
@ -39,13 +40,8 @@ import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.CarbonConstants;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import io.entgra.device.mgt.core.device.mgt.common.Device;
import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier;
import io.entgra.device.mgt.core.device.mgt.common.DeviceManagementConstants;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceNotFoundException;
import io.entgra.device.mgt.core.device.mgt.common.GroupPaginationRequest;
import io.entgra.device.mgt.core.device.mgt.common.PaginationResult;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.TransactionManagementException;
import io.entgra.device.mgt.core.device.mgt.core.event.config.GroupAssignmentEventOperationExecutor;
import io.entgra.device.mgt.core.device.mgt.core.geo.task.GeoFenceEventOperationManager;
@ -1695,10 +1691,14 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
}
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
GroupDetailsDTO groupDetailsWithDevices;
List<String> allowingDeviceStatuses = new ArrayList<>();
allowingDeviceStatuses.add(EnrolmentInfo.Status.ACTIVE.toString());
allowingDeviceStatuses.add(EnrolmentInfo.Status.INACTIVE.toString());
allowingDeviceStatuses.add(EnrolmentInfo.Status.UNREACHABLE.toString());
try {
GroupManagementDAOFactory.openConnection();
groupDetailsWithDevices = this.groupDAO.getGroupDetailsWithDevices(groupName, tenantId, offset, limit);
groupDetailsWithDevices = this.groupDAO.getGroupDetailsWithDevices(groupName, allowingDeviceStatuses, tenantId, offset, limit);
} catch (GroupManagementDAOException | SQLException e) {
String msg = "Error occurred while retrieving group details and device IDs for group: " + groupName;
log.error(msg, e);

Loading…
Cancel
Save