From 43a6c710add55462a23efcef4ed30b2a85c721b5 Mon Sep 17 00:00:00 2001 From: inoshperera Date: Fri, 17 Jul 2020 08:11:44 +0530 Subject: [PATCH] Handle change of device name --- .../mgt/core/DeviceManagementConstants.java | 9 +++++++++ .../impl/DeviceInformationManagerImpl.java | 20 +++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java index 7f7610126b..1b402560b7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java @@ -169,4 +169,13 @@ public final class DeviceManagementConstants { public static final String DEVICE_INFO_PARAM = "device-info"; public static final String APP_USAGE_ENDPOINT = REPORTING_CONTEXT + "/app-usage"; } + + public static final class Payload { + private Payload() { + throw new AssertionError(); + } + public static final String DEVICE_INFO_DEVICE_NAME = "DEVICE_NAME"; + public static final String DEVICE_INFO_IMEI = "IMEI"; + public static final String DEVICE_INFO_IMSI = "IMSI";; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java index 2b4756d0f5..c111f02f3b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java @@ -134,6 +134,20 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { deviceDetailsDAO.addDeviceProperties(injectableProps, device.getId(), device.getEnrolmentInfo().getId()); } + + if (deviceInfo.getDeviceDetailsMap().containsKey(DeviceManagementConstants + .Payload.DEVICE_INFO_DEVICE_NAME) && + StringUtils.isNotEmpty(deviceInfo.getDeviceDetailsMap() + .get(DeviceManagementConstants.Payload.DEVICE_INFO_DEVICE_NAME)) + && !device.getName().equals(deviceInfo.getDeviceDetailsMap() + .get(DeviceManagementConstants.Payload.DEVICE_INFO_DEVICE_NAME))) { + String name = deviceInfo.getDeviceDetailsMap() + .get(DeviceManagementConstants.Payload.DEVICE_INFO_DEVICE_NAME); + log.info("Device identifier " + device.getDeviceIdentifier() + ", Device name " + + "changed by user from " + device.getName() + " to " + name); + device.setName(name); + } + deviceDAO.updateDevice(device, CarbonContext.getThreadLocalCarbonContext().getTenantId()); DeviceManagementDAOFactory.commitTransaction(); @@ -142,8 +156,10 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { Object[] metaData = {device.getDeviceIdentifier(), device.getType()}; Object[] payload = new Object[]{ Calendar.getInstance().getTimeInMillis(), - deviceInfo.getDeviceDetailsMap().get("IMEI"), - deviceInfo.getDeviceDetailsMap().get("IMSI"), + deviceInfo.getDeviceDetailsMap().get(DeviceManagementConstants.Payload + .DEVICE_INFO_IMEI), + deviceInfo.getDeviceDetailsMap().get(DeviceManagementConstants.Payload + .DEVICE_INFO_IMSI), deviceInfo.getDeviceModel(), deviceInfo.getVendor(), deviceInfo.getOsVersion(),