Fixed the device status changing issue when there are multiple enrollments for the same device

4.x.x
harshanl 8 years ago
parent b1e140b58e
commit f9ba45f439

@ -34,7 +34,7 @@ public interface EnrollmentDAO {
int removeEnrollment(int deviceId, String currentOwner, int tenantId) throws DeviceManagementDAOException; 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; int tenantId) throws DeviceManagementDAOException;
Status getStatus(int deviceId, String currentOwner, int tenantId) throws DeviceManagementDAOException; Status getStatus(int deviceId, String currentOwner, int tenantId) throws DeviceManagementDAOException;

@ -323,9 +323,11 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
rs = stmt.executeQuery(); rs = stmt.executeQuery();
devices = new ArrayList<>(); devices = new ArrayList<>();
while (rs.next()) { while (rs.next()) {
Device device = DeviceManagementDAOUtil.loadDevice(rs); Device device = DeviceManagementDAOUtil.loadActiveDevice(rs, false);
if (device != null) {
devices.add(device); devices.add(device);
} }
}
} catch (SQLException e) { } catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while listing devices for type '" + type + "'", e); throw new DeviceManagementDAOException("Error occurred while listing devices for type '" + type + "'", e);
} finally { } finally {
@ -780,7 +782,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
stmt.setInt(5, tenantId); stmt.setInt(5, tenantId);
rs = stmt.executeQuery(); rs = stmt.executeQuery();
if (rs.next()) { if (rs.next()) {
enrolmentInfo = DeviceManagementDAOUtil.loadEnrolment(rs); enrolmentInfo = DeviceManagementDAOUtil.loadMatchingEnrolment(rs);
} }
return enrolmentInfo; return enrolmentInfo;
} catch (SQLException e) { } catch (SQLException e) {

@ -145,16 +145,16 @@ public class EnrollmentDAOImpl implements EnrollmentDAO {
} }
@Override @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 { int tenantId) throws DeviceManagementDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
conn = this.getConnection(); 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 = conn.prepareStatement(sql);
stmt.setString(1, status.toString()); stmt.setString(1, status.toString());
stmt.setInt(2, deviceId); stmt.setInt(2, enrolmentID);
stmt.setString(3, currentOwner); stmt.setString(3, currentOwner);
stmt.setInt(4, tenantId); stmt.setInt(4, tenantId);
stmt.executeUpdate(); stmt.executeUpdate();

@ -147,6 +147,40 @@ public final class DeviceManagementDAOUtil {
return enrolmentInfo; 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 { public static Device loadDevice(ResultSet rs) throws SQLException {
Device device = new Device(); Device device = new Device();
device.setId(rs.getInt("DEVICE_ID")); device.setId(rs.getInt("DEVICE_ID"));
@ -158,6 +192,43 @@ public final class DeviceManagementDAOUtil {
return device; 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 //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. //a single device. We'll give the highest priority to active devices.
public static Device loadMatchingDevice(ResultSet rs, boolean deviceInfoIncluded) throws SQLException { public static Device loadMatchingDevice(ResultSet rs, boolean deviceInfoIncluded) throws SQLException {
@ -182,10 +253,10 @@ public final class DeviceManagementDAOUtil {
deviceMap.put(device.getEnrolmentInfo().getStatus(), device); deviceMap.put(device.getEnrolmentInfo().getStatus(), device);
} }
} }
if (deviceMap.containsKey(EnrolmentInfo.Status.INACTIVE)) { if (deviceMap.containsKey(EnrolmentInfo.Status.UNREACHABLE)) {
return deviceMap.get(EnrolmentInfo.Status.INACTIVE);
} else if (deviceMap.containsKey(EnrolmentInfo.Status.UNREACHABLE)) {
return deviceMap.get(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)) { } else if (deviceMap.containsKey(EnrolmentInfo.Status.DISENROLLMENT_REQUESTED)) {
return deviceMap.get(EnrolmentInfo.Status.DISENROLLMENT_REQUESTED); return deviceMap.get(EnrolmentInfo.Status.DISENROLLMENT_REQUESTED);
} else if (deviceMap.containsKey(EnrolmentInfo.Status.CREATED)) { } 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.DeviceDAO;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; 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.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.internal.DeviceManagementDataHolder;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationDAO; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationDAO;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException;
@ -68,6 +69,7 @@ public class OperationManagerImpl implements OperationManager {
private OperationMappingDAO operationMappingDAO; private OperationMappingDAO operationMappingDAO;
private OperationDAO operationDAO; private OperationDAO operationDAO;
private DeviceDAO deviceDAO; private DeviceDAO deviceDAO;
private EnrollmentDAO enrollmentDAO;
private NotificationStrategy notificationStrategy; private NotificationStrategy notificationStrategy;
public OperationManagerImpl() { public OperationManagerImpl() {
@ -78,6 +80,7 @@ public class OperationManagerImpl implements OperationManager {
operationMappingDAO = OperationManagementDAOFactory.getOperationMappingDAO(); operationMappingDAO = OperationManagementDAOFactory.getOperationMappingDAO();
operationDAO = OperationManagementDAOFactory.getOperationDAO(); operationDAO = OperationManagementDAOFactory.getOperationDAO();
deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); deviceDAO = DeviceManagementDAOFactory.getDeviceDAO();
enrollmentDAO = DeviceManagementDAOFactory.getEnrollmentDAO();
} }
public OperationManagerImpl(NotificationStrategy notificationStrategy) { public OperationManagerImpl(NotificationStrategy notificationStrategy) {
@ -373,7 +376,7 @@ public class OperationManagerImpl implements OperationManager {
case INACTIVE: case INACTIVE:
case UNREACHABLE: case UNREACHABLE:
this.resetAttemptCount(enrolmentId); this.resetAttemptCount(enrolmentId);
this.setEnrolmentStatus(deviceId, EnrolmentInfo.Status.ACTIVE); this.setEnrolmentStatus(enrolmentId, EnrolmentInfo.Status.ACTIVE);
break; break;
} }
@ -919,19 +922,18 @@ public class OperationManagerImpl implements OperationManager {
return enrolmentInfo; return enrolmentInfo;
} }
private boolean setEnrolmentStatus(DeviceIdentifier deviceId, EnrolmentInfo.Status status) throws OperationManagementException { private boolean setEnrolmentStatus(int enrolmentId, EnrolmentInfo.Status status) throws OperationManagementException {
boolean updateStatus; boolean updateStatus;
try { try {
DeviceManagementDAOFactory.beginTransaction(); DeviceManagementDAOFactory.beginTransaction();
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
String user = this.getUser(); String user = this.getUser();
updateStatus = deviceDAO.setEnrolmentStatus(deviceId, user, status, tenantId); updateStatus = enrollmentDAO.setStatus(enrolmentId, user, status, tenantId);
DeviceManagementDAOFactory.commitTransaction(); DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
DeviceManagementDAOFactory.rollbackTransaction(); DeviceManagementDAOFactory.rollbackTransaction();
throw new OperationManagementException("Error occurred while updating enrollment status of '" + throw new OperationManagementException("Error occurred while updating enrollment status of device of " +
deviceId.getType() + "' device carrying the identifier '" + "enrolment-id '" + enrolmentId + "'", e);
deviceId.getId() + "'", e);
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
throw new OperationManagementException("Error occurred while initiating a transaction", e); throw new OperationManagementException("Error occurred while initiating a transaction", e);
} finally { } finally {

@ -240,8 +240,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
try { try {
int tenantId = this.getTenantId(); int tenantId = this.getTenantId();
DeviceManagementDAOFactory.beginTransaction(); DeviceManagementDAOFactory.beginTransaction();
DeviceType type = deviceTypeDAO.getDeviceType(device.getType(), tenantId);
Device currentDevice = deviceDAO.getDevice(deviceIdentifier, tenantId); Device currentDevice = deviceDAO.getDevice(deviceIdentifier, tenantId);
device.setId(currentDevice.getId()); device.setId(currentDevice.getId());
device.getEnrolmentInfo().setId(currentDevice.getEnrolmentInfo().getId()); device.getEnrolmentInfo().setId(currentDevice.getEnrolmentInfo().getId());
@ -1019,10 +1017,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
public boolean setStatus(DeviceIdentifier deviceId, String currentOwner, public boolean setStatus(DeviceIdentifier deviceId, String currentOwner,
EnrolmentInfo.Status status) throws DeviceManagementException { EnrolmentInfo.Status status) throws DeviceManagementException {
try { try {
boolean success = false;
DeviceManagementDAOFactory.beginTransaction(); DeviceManagementDAOFactory.beginTransaction();
int tenantId = this.getTenantId(); int tenantId = this.getTenantId();
Device device = deviceDAO.getDevice(deviceId, tenantId); 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(); DeviceManagementDAOFactory.commitTransaction();
return success; return success;
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
@ -1032,6 +1034,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
throw new DeviceManagementException("Error occurred while initiating transaction", e); throw new DeviceManagementException("Error occurred while initiating transaction", e);
} finally { } finally {
DeviceManagementDAOFactory.closeConnection(); DeviceManagementDAOFactory.closeConnection();
} }
} }

@ -23,16 +23,12 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo; 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.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.ntask.core.Task; import org.wso2.carbon.ntask.core.Task;
import org.wso2.carbon.policy.mgt.common.monitor.PolicyComplianceException; import org.wso2.carbon.policy.mgt.common.monitor.PolicyComplianceException;
import org.wso2.carbon.policy.mgt.common.spi.PolicyMonitoringService; 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.internal.PolicyManagementDataHolder;
import org.wso2.carbon.policy.mgt.core.mgt.MonitoringManager; 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.ArrayList;
import java.util.List; import java.util.List;
@ -62,35 +58,25 @@ public class MonitoringTask implements Task {
} }
MonitoringManager monitoringManager = PolicyManagementDataHolder.getInstance().getMonitoringManager(); MonitoringManager monitoringManager = PolicyManagementDataHolder.getInstance().getMonitoringManager();
List<String> deviceTypes = new ArrayList<>(); List<String> deviceTypes = new ArrayList<>();
try { try {
deviceTypes = monitoringManager.getDeviceTypes(); deviceTypes = monitoringManager.getDeviceTypes();
} catch (PolicyComplianceException e) { } catch (PolicyComplianceException e) {
log.error("Error occurred while getting the device types."); log.error("Error occurred while getting the device types.");
} }
if (!deviceTypes.isEmpty()) { if (!deviceTypes.isEmpty()) {
try { try {
DeviceManagementProviderService deviceManagementProviderService = DeviceManagementProviderService deviceManagementProviderService =
PolicyManagementDataHolder.getInstance().getDeviceManagementService(); PolicyManagementDataHolder.getInstance().getDeviceManagementService();
for (String deviceType : deviceTypes) { for (String deviceType : deviceTypes) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Running task for device type : " + deviceType); log.debug("Running task for device type : " + deviceType);
} }
PolicyMonitoringService monitoringService = PolicyMonitoringService monitoringService =
PolicyManagementDataHolder.getInstance().getPolicyMonitoringService(deviceType); PolicyManagementDataHolder.getInstance().getPolicyMonitoringService(deviceType);
List<Device> devices = deviceManagementProviderService.getAllDevices(deviceType); List<Device> devices = deviceManagementProviderService.getAllDevices(deviceType);
if (monitoringService != null && !devices.isEmpty()) { if (monitoringService != null && !devices.isEmpty()) {
List<Device> notifiableDevices = new ArrayList<>(); List<Device> notifiableDevices = new ArrayList<>();
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Removing inactive and blocked devices from the list for the device type : " + log.debug("Removing inactive and blocked devices from the list for the device type : " +
deviceType); deviceType);

Loading…
Cancel
Save