mirror of
https://repository.entgra.net/community/device-mgt-core.git
synced 2025-10-06 02:01:45 +00:00
Fixed the device status changing issue when there are multiple enrollments for the same device
This commit is contained in:
parent
b1e140b58e
commit
f9ba45f439
@ -34,7 +34,7 @@ public interface EnrollmentDAO {
|
||||
|
||||
int removeEnrollment(int deviceId, String currentOwner, int tenantId) throws DeviceManagementDAOException;
|
||||
|
||||
boolean setStatus(int deviceId, String currentOwner, Status status,
|
||||
boolean setStatus(int enrolmentId, String currentOwner, Status status,
|
||||
int tenantId) throws DeviceManagementDAOException;
|
||||
|
||||
Status getStatus(int deviceId, String currentOwner, int tenantId) throws DeviceManagementDAOException;
|
||||
|
||||
@ -323,8 +323,10 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
||||
rs = stmt.executeQuery();
|
||||
devices = new ArrayList<>();
|
||||
while (rs.next()) {
|
||||
Device device = DeviceManagementDAOUtil.loadDevice(rs);
|
||||
devices.add(device);
|
||||
Device device = DeviceManagementDAOUtil.loadActiveDevice(rs, false);
|
||||
if (device != null) {
|
||||
devices.add(device);
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while listing devices for type '" + type + "'", e);
|
||||
@ -780,7 +782,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
||||
stmt.setInt(5, tenantId);
|
||||
rs = stmt.executeQuery();
|
||||
if (rs.next()) {
|
||||
enrolmentInfo = DeviceManagementDAOUtil.loadEnrolment(rs);
|
||||
enrolmentInfo = DeviceManagementDAOUtil.loadMatchingEnrolment(rs);
|
||||
}
|
||||
return enrolmentInfo;
|
||||
} catch (SQLException e) {
|
||||
|
||||
@ -145,16 +145,16 @@ public class EnrollmentDAOImpl implements EnrollmentDAO {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setStatus(int deviceId, String currentOwner, EnrolmentInfo.Status status,
|
||||
public boolean setStatus(int enrolmentID, String currentOwner, EnrolmentInfo.Status status,
|
||||
int tenantId) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
String sql = "UPDATE DM_ENROLMENT SET STATUS = ? WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?";
|
||||
String sql = "UPDATE DM_ENROLMENT SET STATUS = ? WHERE ID = ? AND OWNER = ? AND TENANT_ID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setString(1, status.toString());
|
||||
stmt.setInt(2, deviceId);
|
||||
stmt.setInt(2, enrolmentID);
|
||||
stmt.setString(3, currentOwner);
|
||||
stmt.setInt(4, tenantId);
|
||||
stmt.executeUpdate();
|
||||
|
||||
@ -147,6 +147,40 @@ public final class DeviceManagementDAOUtil {
|
||||
return enrolmentInfo;
|
||||
}
|
||||
|
||||
public static EnrolmentInfo loadMatchingEnrolment(ResultSet rs) throws SQLException {
|
||||
Map<EnrolmentInfo.Status, EnrolmentInfo> enrolmentInfos = new HashMap<>();
|
||||
EnrolmentInfo enrolmentInfo = loadEnrolment(rs);
|
||||
if (EnrolmentInfo.Status.ACTIVE.equals(enrolmentInfo.getStatus())) {
|
||||
return enrolmentInfo;
|
||||
}
|
||||
enrolmentInfos.put(enrolmentInfo.getStatus(), enrolmentInfo);
|
||||
while (rs.next()) {
|
||||
enrolmentInfo = loadEnrolment(rs);
|
||||
if (EnrolmentInfo.Status.ACTIVE.equals(enrolmentInfo.getStatus())) {
|
||||
return enrolmentInfo;
|
||||
}
|
||||
enrolmentInfos.put(enrolmentInfo.getStatus(), enrolmentInfo);
|
||||
}
|
||||
if (enrolmentInfos.containsKey(EnrolmentInfo.Status.UNREACHABLE)) {
|
||||
return enrolmentInfos.get(EnrolmentInfo.Status.UNREACHABLE);
|
||||
} else if (enrolmentInfos.containsKey(EnrolmentInfo.Status.INACTIVE)) {
|
||||
return enrolmentInfos.get(EnrolmentInfo.Status.INACTIVE);
|
||||
} else if (enrolmentInfos.containsKey(EnrolmentInfo.Status.DISENROLLMENT_REQUESTED)) {
|
||||
return enrolmentInfos.get(EnrolmentInfo.Status.DISENROLLMENT_REQUESTED);
|
||||
} else if (enrolmentInfos.containsKey(EnrolmentInfo.Status.CREATED)) {
|
||||
return enrolmentInfos.get(EnrolmentInfo.Status.CREATED);
|
||||
} else if (enrolmentInfos.containsKey(EnrolmentInfo.Status.REMOVED)) {
|
||||
return enrolmentInfos.get(EnrolmentInfo.Status.REMOVED);
|
||||
} else if (enrolmentInfos.containsKey(EnrolmentInfo.Status.UNCLAIMED)) {
|
||||
return enrolmentInfos.get(EnrolmentInfo.Status.UNCLAIMED);
|
||||
} else if (enrolmentInfos.containsKey(EnrolmentInfo.Status.SUSPENDED)) {
|
||||
return enrolmentInfos.get(EnrolmentInfo.Status.SUSPENDED);
|
||||
} else if (enrolmentInfos.containsKey(EnrolmentInfo.Status.BLOCKED)) {
|
||||
return enrolmentInfos.get(EnrolmentInfo.Status.BLOCKED);
|
||||
}
|
||||
return enrolmentInfo;
|
||||
}
|
||||
|
||||
public static Device loadDevice(ResultSet rs) throws SQLException {
|
||||
Device device = new Device();
|
||||
device.setId(rs.getInt("DEVICE_ID"));
|
||||
@ -158,6 +192,43 @@ public final class DeviceManagementDAOUtil {
|
||||
return device;
|
||||
}
|
||||
|
||||
//This method will retrieve most appropriate device information when there are multiple device enrollments for
|
||||
//a single device. Here we'll consider only active status.
|
||||
public static Device loadActiveDevice(ResultSet rs, boolean deviceInfoIncluded) throws SQLException {
|
||||
Map<EnrolmentInfo.Status, Device> deviceMap = new HashMap<>();
|
||||
Device device = loadDevice(rs);
|
||||
if (deviceInfoIncluded) {
|
||||
device.setDeviceInfo(loadDeviceInfo(rs));
|
||||
}
|
||||
|
||||
if (EnrolmentInfo.Status.ACTIVE.equals(device.getEnrolmentInfo().getStatus())) {
|
||||
return device;
|
||||
}
|
||||
deviceMap.put(device.getEnrolmentInfo().getStatus(), device);
|
||||
while (rs.next()) {
|
||||
device = loadDevice(rs);
|
||||
if (deviceInfoIncluded) {
|
||||
device.setDeviceInfo(loadDeviceInfo(rs));
|
||||
}
|
||||
if (EnrolmentInfo.Status.ACTIVE.equals(device.getEnrolmentInfo().getStatus())) {
|
||||
return device;
|
||||
}
|
||||
if (device.getEnrolmentInfo() != null) {
|
||||
deviceMap.put(device.getEnrolmentInfo().getStatus(), device);
|
||||
}
|
||||
}
|
||||
if (deviceMap.containsKey(EnrolmentInfo.Status.UNREACHABLE)) {
|
||||
return deviceMap.get(EnrolmentInfo.Status.UNREACHABLE);
|
||||
} else if (deviceMap.containsKey(EnrolmentInfo.Status.INACTIVE)) {
|
||||
return deviceMap.get(EnrolmentInfo.Status.INACTIVE);
|
||||
} else if (deviceMap.containsKey(EnrolmentInfo.Status.CREATED)) {
|
||||
return deviceMap.get(EnrolmentInfo.Status.CREATED);
|
||||
} else if (deviceMap.containsKey(EnrolmentInfo.Status.UNCLAIMED)) {
|
||||
return deviceMap.get(EnrolmentInfo.Status.UNCLAIMED);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
//This method will retrieve most appropriate device information when there are multiple device enrollments for
|
||||
//a single device. We'll give the highest priority to active devices.
|
||||
public static Device loadMatchingDevice(ResultSet rs, boolean deviceInfoIncluded) throws SQLException {
|
||||
@ -182,10 +253,10 @@ public final class DeviceManagementDAOUtil {
|
||||
deviceMap.put(device.getEnrolmentInfo().getStatus(), device);
|
||||
}
|
||||
}
|
||||
if (deviceMap.containsKey(EnrolmentInfo.Status.INACTIVE)) {
|
||||
return deviceMap.get(EnrolmentInfo.Status.INACTIVE);
|
||||
} else if (deviceMap.containsKey(EnrolmentInfo.Status.UNREACHABLE)) {
|
||||
if (deviceMap.containsKey(EnrolmentInfo.Status.UNREACHABLE)) {
|
||||
return deviceMap.get(EnrolmentInfo.Status.UNREACHABLE);
|
||||
} else if (deviceMap.containsKey(EnrolmentInfo.Status.INACTIVE)) {
|
||||
return deviceMap.get(EnrolmentInfo.Status.INACTIVE);
|
||||
} else if (deviceMap.containsKey(EnrolmentInfo.Status.DISENROLLMENT_REQUESTED)) {
|
||||
return deviceMap.get(EnrolmentInfo.Status.DISENROLLMENT_REQUESTED);
|
||||
} else if (deviceMap.containsKey(EnrolmentInfo.Status.CREATED)) {
|
||||
|
||||
@ -34,6 +34,7 @@ import org.wso2.carbon.device.mgt.core.config.task.TaskConfiguration;
|
||||
import org.wso2.carbon.device.mgt.core.dao.DeviceDAO;
|
||||
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
|
||||
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
|
||||
import org.wso2.carbon.device.mgt.core.dao.EnrollmentDAO;
|
||||
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
|
||||
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationDAO;
|
||||
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException;
|
||||
@ -68,6 +69,7 @@ public class OperationManagerImpl implements OperationManager {
|
||||
private OperationMappingDAO operationMappingDAO;
|
||||
private OperationDAO operationDAO;
|
||||
private DeviceDAO deviceDAO;
|
||||
private EnrollmentDAO enrollmentDAO;
|
||||
private NotificationStrategy notificationStrategy;
|
||||
|
||||
public OperationManagerImpl() {
|
||||
@ -78,6 +80,7 @@ public class OperationManagerImpl implements OperationManager {
|
||||
operationMappingDAO = OperationManagementDAOFactory.getOperationMappingDAO();
|
||||
operationDAO = OperationManagementDAOFactory.getOperationDAO();
|
||||
deviceDAO = DeviceManagementDAOFactory.getDeviceDAO();
|
||||
enrollmentDAO = DeviceManagementDAOFactory.getEnrollmentDAO();
|
||||
}
|
||||
|
||||
public OperationManagerImpl(NotificationStrategy notificationStrategy) {
|
||||
@ -373,7 +376,7 @@ public class OperationManagerImpl implements OperationManager {
|
||||
case INACTIVE:
|
||||
case UNREACHABLE:
|
||||
this.resetAttemptCount(enrolmentId);
|
||||
this.setEnrolmentStatus(deviceId, EnrolmentInfo.Status.ACTIVE);
|
||||
this.setEnrolmentStatus(enrolmentId, EnrolmentInfo.Status.ACTIVE);
|
||||
break;
|
||||
}
|
||||
|
||||
@ -919,19 +922,18 @@ public class OperationManagerImpl implements OperationManager {
|
||||
return enrolmentInfo;
|
||||
}
|
||||
|
||||
private boolean setEnrolmentStatus(DeviceIdentifier deviceId, EnrolmentInfo.Status status) throws OperationManagementException {
|
||||
private boolean setEnrolmentStatus(int enrolmentId, EnrolmentInfo.Status status) throws OperationManagementException {
|
||||
boolean updateStatus;
|
||||
try {
|
||||
DeviceManagementDAOFactory.beginTransaction();
|
||||
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
|
||||
String user = this.getUser();
|
||||
updateStatus = deviceDAO.setEnrolmentStatus(deviceId, user, status, tenantId);
|
||||
updateStatus = enrollmentDAO.setStatus(enrolmentId, user, status, tenantId);
|
||||
DeviceManagementDAOFactory.commitTransaction();
|
||||
} catch (DeviceManagementDAOException e) {
|
||||
DeviceManagementDAOFactory.rollbackTransaction();
|
||||
throw new OperationManagementException("Error occurred while updating enrollment status of '" +
|
||||
deviceId.getType() + "' device carrying the identifier '" +
|
||||
deviceId.getId() + "'", e);
|
||||
throw new OperationManagementException("Error occurred while updating enrollment status of device of " +
|
||||
"enrolment-id '" + enrolmentId + "'", e);
|
||||
} catch (TransactionManagementException e) {
|
||||
throw new OperationManagementException("Error occurred while initiating a transaction", e);
|
||||
} finally {
|
||||
|
||||
@ -240,8 +240,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
|
||||
try {
|
||||
int tenantId = this.getTenantId();
|
||||
DeviceManagementDAOFactory.beginTransaction();
|
||||
|
||||
DeviceType type = deviceTypeDAO.getDeviceType(device.getType(), tenantId);
|
||||
Device currentDevice = deviceDAO.getDevice(deviceIdentifier, tenantId);
|
||||
device.setId(currentDevice.getId());
|
||||
device.getEnrolmentInfo().setId(currentDevice.getEnrolmentInfo().getId());
|
||||
@ -1019,10 +1017,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
|
||||
public boolean setStatus(DeviceIdentifier deviceId, String currentOwner,
|
||||
EnrolmentInfo.Status status) throws DeviceManagementException {
|
||||
try {
|
||||
boolean success = false;
|
||||
DeviceManagementDAOFactory.beginTransaction();
|
||||
int tenantId = this.getTenantId();
|
||||
Device device = deviceDAO.getDevice(deviceId, tenantId);
|
||||
boolean success = enrollmentDAO.setStatus(device.getId(), currentOwner, status, tenantId);
|
||||
EnrolmentInfo enrolmentInfo = device.getEnrolmentInfo();
|
||||
if (enrolmentInfo != null) {
|
||||
success = enrollmentDAO.setStatus(enrolmentInfo.getId(), currentOwner, status, tenantId);
|
||||
}
|
||||
DeviceManagementDAOFactory.commitTransaction();
|
||||
return success;
|
||||
} catch (DeviceManagementDAOException e) {
|
||||
@ -1032,6 +1034,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
|
||||
throw new DeviceManagementException("Error occurred while initiating transaction", e);
|
||||
} finally {
|
||||
DeviceManagementDAOFactory.closeConnection();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -23,16 +23,12 @@ import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.device.mgt.common.Device;
|
||||
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
|
||||
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
|
||||
import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO;
|
||||
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
|
||||
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
|
||||
import org.wso2.carbon.ntask.core.Task;
|
||||
import org.wso2.carbon.policy.mgt.common.monitor.PolicyComplianceException;
|
||||
import org.wso2.carbon.policy.mgt.common.spi.PolicyMonitoringService;
|
||||
import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder;
|
||||
import org.wso2.carbon.policy.mgt.core.mgt.MonitoringManager;
|
||||
import org.wso2.carbon.policy.mgt.core.mgt.impl.MonitoringManagerImpl;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@ -62,35 +58,25 @@ public class MonitoringTask implements Task {
|
||||
}
|
||||
|
||||
MonitoringManager monitoringManager = PolicyManagementDataHolder.getInstance().getMonitoringManager();
|
||||
|
||||
List<String> deviceTypes = new ArrayList<>();
|
||||
try {
|
||||
deviceTypes = monitoringManager.getDeviceTypes();
|
||||
} catch (PolicyComplianceException e) {
|
||||
log.error("Error occurred while getting the device types.");
|
||||
}
|
||||
|
||||
if (!deviceTypes.isEmpty()) {
|
||||
try {
|
||||
|
||||
|
||||
DeviceManagementProviderService deviceManagementProviderService =
|
||||
PolicyManagementDataHolder.getInstance().getDeviceManagementService();
|
||||
|
||||
for (String deviceType : deviceTypes) {
|
||||
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Running task for device type : " + deviceType);
|
||||
}
|
||||
|
||||
PolicyMonitoringService monitoringService =
|
||||
PolicyManagementDataHolder.getInstance().getPolicyMonitoringService(deviceType);
|
||||
List<Device> devices = deviceManagementProviderService.getAllDevices(deviceType);
|
||||
if (monitoringService != null && !devices.isEmpty()) {
|
||||
|
||||
|
||||
List<Device> notifiableDevices = new ArrayList<>();
|
||||
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Removing inactive and blocked devices from the list for the device type : " +
|
||||
deviceType);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user