Fix all,device,group removed devices

issue-10462/secure-pending-operation-6.2
prathabanKavin 4 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 * @return {@link OwnerWithDeviceDTO} which contains a list of devices related to a user
* @throws DeviceManagementDAOException if an error occurs while fetching the data * @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 * @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 * @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; throws GroupManagementDAOException;
} }

@ -654,18 +654,34 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO {
} }
@Override @Override
public List<DeviceDetailsDTO> getDevicesByTenantId(int tenantId) public List<DeviceDetailsDTO> getDevicesByTenantId(int tenantId, List<String> allowingDeviceStatuses)
throws DeviceManagementDAOException { throws DeviceManagementDAOException {
List<DeviceDetailsDTO> devices = new ArrayList<>(); 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 " + String sql = "SELECT DEVICE_ID, OWNER, STATUS, DEVICE_TYPE, DEVICE_IDENTIFICATION " +
"FROM DM_ENROLMENT " + "FROM DM_ENROLMENT " +
"WHERE TENANT_ID = ?"; "WHERE TENANT_ID = ? AND STATUS IN (" + deviceFilters.toString() + ")";
Connection conn = null; Connection conn = null;
try { try {
conn = this.getConnection(); conn = this.getConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) { 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()) { try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) { while (rs.next()) {
@ -687,4 +703,5 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO {
return devices; return devices;
} }
} }

@ -1441,7 +1441,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
} }
@Override @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 { throws GroupManagementDAOException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to get group details and device IDs for group: " + groupName); 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> deviceTypes = new HashMap<>();
Map<Integer, String> deviceIdentifiers = 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 = String sql =
"SELECT " + "SELECT " +
" g.ID AS GROUP_ID, " + " g.ID AS GROUP_ID, " +
@ -1473,16 +1481,21 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
"WHERE " + "WHERE " +
" g.GROUP_NAME = ? " + " g.GROUP_NAME = ? " +
" AND g.TENANT_ID = ? " + " AND g.TENANT_ID = ? " +
" AND e.STATUS IN (" + deviceFilters.toString() + ") " +
"LIMIT ? OFFSET ?"; "LIMIT ? OFFSET ?";
Connection conn = null; Connection conn = null;
try { try {
conn = GroupManagementDAOFactory.getConnection(); conn = GroupManagementDAOFactory.getConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, groupName); int index = 1;
stmt.setInt(2, tenantId); stmt.setString(index++, groupName);
stmt.setInt(3, limit); stmt.setInt(index++, tenantId);
stmt.setInt(4, offset); for (String status : allowedStatuses) {
stmt.setString(index++, status);
}
stmt.setInt(index++, limit);
stmt.setInt(index++, offset);
try (ResultSet rs = stmt.executeQuery()) { try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) { while (rs.next()) {

@ -5418,9 +5418,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
@Override @Override
public List<DeviceDetailsDTO> getDevicesByTenantId(int tenantId) throws DeviceManagementDAOException { public List<DeviceDetailsDTO> getDevicesByTenantId(int tenantId) throws DeviceManagementDAOException {
List<DeviceDetailsDTO> devices; 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 { try {
DeviceManagementDAOFactory.openConnection(); DeviceManagementDAOFactory.openConnection();
devices = enrollmentDAO.getDevicesByTenantId(tenantId); devices = enrollmentDAO.getDevicesByTenantId(tenantId, allowingDeviceStatuses);
if (devices == null || devices.isEmpty()) { if (devices == null || devices.isEmpty()) {
String msg = "No devices found for tenant ID: " + tenantId; String msg = "No devices found for tenant ID: " + tenantId;
log.error(msg); log.error(msg);

@ -18,6 +18,7 @@
package io.entgra.device.mgt.core.device.mgt.core.service; 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.DeviceGroup;
import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroupConstants; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroupConstants;
import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroupRoleWrapper; 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.CarbonConstants;
import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext; 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.DeviceManagementException;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceNotFoundException; 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.common.exceptions.TransactionManagementException;
import io.entgra.device.mgt.core.device.mgt.core.event.config.GroupAssignmentEventOperationExecutor; import io.entgra.device.mgt.core.device.mgt.core.event.config.GroupAssignmentEventOperationExecutor;
import io.entgra.device.mgt.core.device.mgt.core.geo.task.GeoFenceEventOperationManager; 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(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
GroupDetailsDTO groupDetailsWithDevices; 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 { try {
GroupManagementDAOFactory.openConnection(); GroupManagementDAOFactory.openConnection();
groupDetailsWithDevices = this.groupDAO.getGroupDetailsWithDevices(groupName, tenantId, offset, limit); groupDetailsWithDevices = this.groupDAO.getGroupDetailsWithDevices(groupName, allowingDeviceStatuses, tenantId, offset, limit);
} catch (GroupManagementDAOException | SQLException e) { } catch (GroupManagementDAOException | SQLException e) {
String msg = "Error occurred while retrieving group details and device IDs for group: " + groupName; String msg = "Error occurred while retrieving group details and device IDs for group: " + groupName;
log.error(msg, e); log.error(msg, e);

Loading…
Cancel
Save