From 6263a550ffc5f7b550751237ced5f67f2bd18e4d Mon Sep 17 00:00:00 2001 From: manoj Date: Thu, 25 Jun 2015 20:25:08 +0530 Subject: [PATCH] Set device enrolment status --- .../device/mgt/common/EnrolmentInfo.java | 4 ++-- .../mgt/core/dao/impl/DeviceDAOImpl.java | 10 +++++--- .../DeviceManagementProviderServiceImpl.java | 24 +++++++++++++++++-- .../mgt/core/util/DeviceManagerUtil.java | 1 + 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EnrolmentInfo.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EnrolmentInfo.java index f002c617b1..997c445271 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EnrolmentInfo.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EnrolmentInfo.java @@ -26,11 +26,11 @@ public class EnrolmentInfo { private Status status; private String owner; - public enum Status { + public static enum Status { CREATED, ACTIVE, INACTIVE, UNCLAIMED, SUSPENDED, BLOCKED, REMOVED } - public enum OwnerShip { + public static enum OwnerShip { BYOD, COPE } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java index 9ca8317ac1..0d64e67e6e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java @@ -76,12 +76,16 @@ public class DeviceDAOImpl implements DeviceDAO { PreparedStatement stmt = null; try { conn = this.getConnection(); - String sql = "UPDATE DM_DEVICE SET STATUS = ?, OWNER = ? WHERE DEVICE_IDENTIFICATION = ? AND TENANT_ID = ?"; + String sql = "UPDATE DM_DEVICE SET STATUS = ?, OWNER = ?, DATE_OF_ENROLLMENT=?, " + + "DATE_OF_LAST_UPDATE=? WHERE DEVICE_IDENTIFICATION = ? AND TENANT_ID = ? AND DEVICE_TYPE_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, device.getEnrolmentInfo().getStatus().toString()); stmt.setString(2, device.getEnrolmentInfo().getOwner()); - stmt.setString(3, device.getDeviceIdentifier()); - stmt.setInt(4, tenantId); + stmt.setLong(3, device.getEnrolmentInfo().getDateOfEnrolment()); + stmt.setLong(4, device.getEnrolmentInfo().getDateOfLastUpdate()); + stmt.setString(5, device.getDeviceIdentifier()); + stmt.setInt(6, typeId); + stmt.setInt(7, tenantId); stmt.executeUpdate(); } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while enrolling device '" + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 38d6a6f8f2..5b768e7ae8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -44,6 +44,7 @@ import java.io.IOException; import java.net.URLDecoder; import java.net.URLEncoder; import java.util.ArrayList; +import java.util.Date; import java.util.List; public class DeviceManagementProviderServiceImpl implements DeviceManagementProviderService, PluginInitializationListener { @@ -100,6 +101,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.beginTransaction(); DeviceType type = deviceTypeDAO.getDeviceType(device.getType()); + device.getEnrolmentInfo().setDateOfEnrolment(new Date().getTime()); + device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); deviceDAO.addDevice(type.getId(), device, tenantId); DeviceManagementDAOFactory.commitTransaction(); @@ -121,6 +124,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return status; } + + @Override public boolean modifyEnrollment(Device device) throws DeviceManagementException { DeviceManager dms = @@ -130,9 +135,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); DeviceManagementDAOFactory.beginTransaction(); - DeviceType type = deviceTypeDAO.getDeviceType(device.getType()); - deviceDAO.updateDevice(type.getId(), device, tenantId); + deviceDAO.updateDevice(type.getId(),device, tenantId); DeviceManagementDAOFactory.commitTransaction(); } catch (DeviceManagementDAOException e) { @@ -155,8 +159,24 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException { + + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); DeviceManager dms = this.getPluginRepository().getDeviceManagementService(deviceId.getType()); + try { + Device device = deviceDAO.getDevice(deviceId,tenantId); + DeviceType deviceType = deviceTypeDAO.getDeviceType(device.getType()); + + device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); + device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.REMOVED); + deviceDAO.updateDevice(deviceType.getId(), device, tenantId); + + } catch (DeviceManagementDAOException e) { + String errorMsg = "Error occurred while fetch device for device Identifier:"; + log.error(errorMsg + deviceId.toString(),e); + throw new DeviceManagementException(errorMsg, e); + + } return dms.disenrollDevice(deviceId); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java index 126d60b543..4824537db4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java @@ -30,6 +30,7 @@ import org.wso2.carbon.apimgt.impl.APIConstants; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.core.api.mgt.APIConfig; import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig; import org.wso2.carbon.device.mgt.core.config.datasource.JNDILookupDefinition;