From f3dd402e4bde8a8070ebf0feeafc40096de1aff3 Mon Sep 17 00:00:00 2001 From: manoj Date: Thu, 25 Jun 2015 21:13:55 +0530 Subject: [PATCH] Set device status to active when poll operations --- .../operation/mgt/OperationManagerImpl.java | 34 +++++++++++++++++++ .../DeviceManagementProviderService.java | 1 + .../DeviceManagementProviderServiceImpl.java | 17 +++++++++- 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java index 148e32a124..da551394e3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -24,12 +24,15 @@ import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; 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.DeviceTypeDAO; +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; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; @@ -57,6 +60,7 @@ public class OperationManagerImpl implements OperationManager { private OperationMappingDAO operationMappingDAO; private OperationDAO operationDAO; private DeviceDAO deviceDAO; + private DeviceTypeDAO deviceTypeDAO; public OperationManagerImpl() { commandOperationDAO = OperationManagementDAOFactory.getCommandOperationDAO(); @@ -66,6 +70,7 @@ public class OperationManagerImpl implements OperationManager { operationMappingDAO = OperationManagementDAOFactory.getOperationMappingDAO(); operationDAO = OperationManagementDAOFactory.getOperationDAO(); deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); + deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); } @Override @@ -169,6 +174,21 @@ public class OperationManagerImpl implements OperationManager { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); device = deviceDAO.getDevice(deviceId, tenantId); + if (device.getEnrolmentInfo().getStatus() !=null && !device.getEnrolmentInfo().getStatus().equals( + EnrolmentInfo.Status.ACTIVE)){ + try { + DeviceManagementDataHolder.getInstance().getDeviceManagementProvider() + .updateDeviceEnrolmentInfo(device, + EnrolmentInfo.Status.ACTIVE); + }catch (DeviceManagementException deviceMgtEx){ + String errorMsg = "Error occurred while update enrol status: "+deviceId.toString(); + log.error(errorMsg, deviceMgtEx); + throw new OperationManagementException(errorMsg, deviceMgtEx); + } + } + + + if (device == null) { throw new OperationManagementException("Device not found for given device " + "Identifier:" + deviceId.getId() + " and given type:" + deviceId.getType()); @@ -225,6 +245,20 @@ public class OperationManagerImpl implements OperationManager { } org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO .getNextOperation(device.getId()); + + if (device.getEnrolmentInfo().getStatus() !=null && !device.getEnrolmentInfo().getStatus().equals( + EnrolmentInfo.Status.ACTIVE)){ + try { + DeviceManagementDataHolder.getInstance().getDeviceManagementProvider() + .updateDeviceEnrolmentInfo(device, + EnrolmentInfo.Status.ACTIVE); + }catch (DeviceManagementException deviceMgtEx){ + String errorMsg = "Error occurred while update enrol status: "+deviceId.toString(); + log.error(errorMsg, deviceMgtEx); + throw new OperationManagementException(errorMsg, deviceMgtEx); + } + } + if (dtoOperation != null) { if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND .equals(dtoOperation.getType())) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index b1f3e28947..9602ad390b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -87,4 +87,5 @@ public interface DeviceManagementProviderService extends DeviceManager, LicenseM */ List getApplicationListForDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementException; + void updateDeviceEnrolmentInfo(Device device, EnrolmentInfo.Status active) throws DeviceManagementException; } 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 5b768e7ae8..ad002b76cd 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 @@ -137,7 +137,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.beginTransaction(); DeviceType type = deviceTypeDAO.getDeviceType(device.getType()); deviceDAO.updateDevice(type.getId(),device, tenantId); - DeviceManagementDAOFactory.commitTransaction(); } catch (DeviceManagementDAOException e) { try { @@ -652,6 +651,22 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return null; } + @Override + public void updateDeviceEnrolmentInfo(Device device, EnrolmentInfo.Status status) throws DeviceManagementException { + + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { + DeviceType deviceType = deviceTypeDAO.getDeviceType(device.getType()); + device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); + device.getEnrolmentInfo().setStatus(status); + deviceDAO.updateDevice(deviceType.getId(), device, tenantId); + }catch (DeviceManagementDAOException deviceDaoEx){ + String errorMsg = "Error occured update device enrolment status:"+device.getId(); + log.error(errorMsg, deviceDaoEx); + throw new DeviceManagementException(errorMsg, deviceDaoEx); + } + } + @Override public void registerDeviceManagementService(DeviceManagementService deviceManagementService) { try {