From d9a449dd265e922ce5bc9b3aa7a39975f44e7be7 Mon Sep 17 00:00:00 2001 From: inoshperera Date: Wed, 23 Nov 2016 12:36:05 +0530 Subject: [PATCH] adding modify enrollment capability to save chnages in enrollment such as GCM token update --- .../impl/DeviceManagementServiceImpl.java | 59 ++++++++++++++----- .../impl/dao/impl/AndroidDeviceDAOImpl.java | 6 +- .../impl/util/AndroidPluginConstants.java | 2 +- 3 files changed, 47 insertions(+), 20 deletions(-) diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/services/impl/DeviceManagementServiceImpl.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/services/impl/DeviceManagementServiceImpl.java index ddcf2a49d..2117cccec 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/services/impl/DeviceManagementServiceImpl.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/services/impl/DeviceManagementServiceImpl.java @@ -276,25 +276,52 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { @Path("/{id}") @Override public Response modifyEnrollment(@PathParam("id") String id, @Valid AndroidDevice androidDevice) { - Device device = new Device(); - String msg = ""; - device.setType(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID); - if(androidDevice.getEnrolmentInfo().getDateOfEnrolment() <= 0){ - msg = "Invalid Enrollment date."; - return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); + Device device; + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(androidDevice.getDeviceIdentifier()); + deviceIdentifier.setType(AndroidConstants.DEVICE_TYPE_ANDROID); + try { + device = AndroidAPIUtils.getDeviceManagementService().getDevice(deviceIdentifier); + } catch (DeviceManagementException e) { + String msg = "Error occurred while getting enrollment details of the Android device that carries the id '" + + id + "'"; + log.error(msg, e); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } - if(androidDevice.getEnrolmentInfo().getDateOfLastUpdate() <= 0){ - msg = "Invalid Last Updated date."; - return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); + + if (androidDevice == null) { + String errorMessage = "The payload of the android device enrollment is incorrect."; + log.error(errorMessage); + throw new org.wso2.carbon.mdm.services.android.exception.BadRequestException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(errorMessage).build()); + } + if (device == null) { + String errorMessage = "The device to be modified doesn't exist."; + log.error(errorMessage); + throw new org.wso2.carbon.mdm.services.android.exception.BadRequestException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(errorMessage).build()); + } + if(androidDevice.getEnrolmentInfo() != null){ + device.setEnrolmentInfo(device.getEnrolmentInfo()); } - device.setEnrolmentInfo(androidDevice.getEnrolmentInfo()); device.getEnrolmentInfo().setOwner(AndroidAPIUtils.getAuthenticatedUser()); - device.setDeviceInfo(androidDevice.getDeviceInfo()); + if(androidDevice.getDeviceInfo() != null) { + device.setDeviceInfo(androidDevice.getDeviceInfo()); + } device.setDeviceIdentifier(androidDevice.getDeviceIdentifier()); - device.setDescription(androidDevice.getDescription()); - device.setName(androidDevice.getName()); - device.setFeatures(androidDevice.getFeatures()); - device.setProperties(androidDevice.getProperties()); + if(androidDevice.getDescription() != null) { + device.setDescription(androidDevice.getDescription()); + } + if(androidDevice.getName() != null) { + device.setName(androidDevice.getName()); + } + if(androidDevice.getFeatures() != null) { + device.setFeatures(androidDevice.getFeatures()); + } + if(androidDevice.getProperties() != null) { + device.setProperties(androidDevice.getProperties()); + } boolean result; try { device.setType(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID); @@ -313,7 +340,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { return Response.status(Response.Status.NOT_MODIFIED).entity(responseMessage).build(); } } catch (DeviceManagementException e) { - msg = "Error occurred while modifying enrollment of the Android device that carries the id '" + + String msg = "Error occurred while modifying enrollment of the Android device that carries the id '" + id + "'"; log.error(msg, e); throw new UnexpectedServerErrorException( diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/impl/dao/impl/AndroidDeviceDAOImpl.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/impl/dao/impl/AndroidDeviceDAOImpl.java index 2fa63e5bc..c033a8c47 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/impl/dao/impl/AndroidDeviceDAOImpl.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/impl/dao/impl/AndroidDeviceDAOImpl.java @@ -159,7 +159,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{ stmt.setString(2, properties.get(AndroidPluginConstants.DEVICE_INFO)); stmt.setString(3, mobileDevice.getSerial()); stmt.setString(4, mobileDevice.getVendor()); - stmt.setString(5, mobileDevice.getMobileDeviceId()); + stmt.setString(5, properties.get(AndroidPluginConstants.MAC_ADDRESS)); stmt.setString(6, properties.get(AndroidPluginConstants.DEVICE_NAME)); stmt.setString(7, mobileDevice.getLatitude()); stmt.setString(8, mobileDevice.getLongitude()); @@ -167,8 +167,8 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{ stmt.setString(10, mobileDevice.getImsi()); stmt.setString(11, mobileDevice.getOsVersion()); stmt.setString(12, mobileDevice.getModel()); - stmt.setString(13, mobileDevice.getMobileDeviceId()); - stmt.setString(14, mobileDevice.getOsBuildDate()); + stmt.setString(13, mobileDevice.getOsBuildDate()); + stmt.setString(14, mobileDevice.getMobileDeviceId()); int rows = stmt.executeUpdate(); if (rows > 0) { status = true; diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/impl/util/AndroidPluginConstants.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/impl/util/AndroidPluginConstants.java index 376932449..8c79cdd9f 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/impl/util/AndroidPluginConstants.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android/src/main/java/org/wso2/carbon/device/mgt/mobile/android/impl/util/AndroidPluginConstants.java @@ -37,7 +37,7 @@ public final class AndroidPluginConstants { public static final String VENDOR = "VENDOR"; public static final String OS_VERSION = "OS_VERSION"; public static final String OS_BUILD_DATE = "OS_BUILD_DATE"; - public static final String MAC_ADDRESS = "MAC_ADDRESS"; + public static final String MAC_ADDRESS = "MAC"; //Properties related to AD_FEATURE table public static final String ANDROID_FEATURE_ID = "ID";