From 643acd78276472bdb89bd97f277a7ff63716c89d Mon Sep 17 00:00:00 2001 From: Ace Date: Tue, 10 Jan 2017 16:16:31 +0530 Subject: [PATCH] Adding api for renaming devices --- .../org.wso2.carbon.device.mgt.api/pom.xml | 4 +- .../service/api/DeviceManagementService.java | 77 +++++++++++++++++++ .../impl/DeviceManagementServiceImpl.java | 26 +++++++ .../devicemgt/app/conf/config.json | 1 + .../cdmf.page.devices/public/js/listing.js | 6 +- 5 files changed, 110 insertions(+), 4 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.api/pom.xml index 27b01bd554d..ce88139cb6c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/pom.xml @@ -201,8 +201,8 @@ - com.google.code.gson - gson + org.json.wso2 + json commons-codec.wso2 diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java index 3c96e96a50f..8574b4088d5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java @@ -29,6 +29,7 @@ import io.swagger.annotations.ApiParam; import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; import io.swagger.annotations.ResponseHeader; +import org.json.JSONObject; import org.wso2.carbon.apimgt.annotations.api.Scope; import org.wso2.carbon.apimgt.annotations.api.Scopes; import org.wso2.carbon.device.mgt.common.Device; @@ -401,6 +402,82 @@ public interface DeviceManagementService { String deviceId); + //device rename request would looks like follows + //POST devices/type/virtual_firealarm/id/us06ww93auzp/rename + @POST + @Path("/type/{device-type}/id/{device-id}/rename") + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + consumes = MediaType.APPLICATION_JSON, + httpMethod = "POST", + value = "Update the device specified by device id", + notes = "Returns the status of the updated device operation.", + tags = "Device Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = "perm:devices:update") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully fetched information of the device.", + response = Device.class, + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body"), + @ResponseHeader( + name = "ETag", + description = "Entity Tag of the response resource.\n" + + "Used by caches, or in conditional requests."), + @ResponseHeader( + name = "Last-Modified", + description = "Date and time the resource has been modified the last time.\n" + + "Used by caches, or in conditional requests."), + }), + @ApiResponse( + code = 304, + message = "Not Modified. Empty body because the client already has the latest " + + "version of the requested resource."), + @ApiResponse( + code = 400, + message = "Bad Request. \n Invalid request or validation error.", + response = ErrorResponse.class), + @ApiResponse( + code = 404, + message = "Not Found. \n No device is found under the provided type and id.", + response = ErrorResponse.class), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n " + + "Server error occurred while retrieving information requested device.", + response = ErrorResponse.class) + }) + Response renameDevice( + @ApiParam( + name = "device", + value = "The payload containing new name for device with updated name.", + required = true) + Device device, + @ApiParam( + name = "device-type", + value = "The device type, such as ios, android or windows.", + required = true) + @PathParam("device-type") + @Size(max = 45) + String deviceType, + @ApiParam( + name = "device-id", + value = "The device identifier of the device.", + required = true) + @PathParam("device-id") + @Size(max = 45) + String deviceId); + + @GET @Path("/{type}/{id}/features") @ApiOperation( diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index 013a04c350d..dd4c6041625 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -18,8 +18,11 @@ */ package org.wso2.carbon.device.mgt.jaxrs.service.impl; +import io.swagger.annotations.ApiParam; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.json.JSONException; +import org.json.JSONObject; import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.device.mgt.common.*; import org.wso2.carbon.device.mgt.common.app.mgt.Application; @@ -30,6 +33,7 @@ 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.search.SearchContext; import org.wso2.carbon.device.mgt.core.app.mgt.ApplicationManagementProviderService; +import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.search.mgt.SearchManagerService; import org.wso2.carbon.device.mgt.core.search.mgt.SearchMgtException; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; @@ -236,6 +240,28 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { } } + @POST + @Override + @Path("/type/{device-type}/id/{device-id}/rename") + public Response renameDevice(Device device, @PathParam("device-type") String deviceType, + @PathParam("device-id") String deviceId) { + DeviceManagementProviderService deviceManagementProviderService = DeviceMgtAPIUtils.getDeviceManagementService(); + try { + Device persistedDevice = deviceManagementProviderService.getDevice(new DeviceIdentifier + (deviceId, deviceType)); + persistedDevice.setName(device.getName()); + boolean response = deviceManagementProviderService.modifyEnrollment(persistedDevice); + return Response.status(Response.Status.CREATED).entity(response).build(); + + } catch (DeviceManagementException e) { + log.error("Error encountered while updating device of type : " + deviceType + " and " + + "ID : " + deviceId); + return Response.status(Response.Status.BAD_REQUEST).entity( + new ErrorResponse.ErrorResponseBuilder().setMessage("Error while updating " + + "device of type " + deviceType + " and ID : " + deviceId).build()).build(); + } + } + @GET @Path("/{type}/{id}") @Override diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/conf/config.json b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/conf/config.json index 8b923a9b825..e9259819aae 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/conf/config.json +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/conf/config.json @@ -90,6 +90,7 @@ "perm:devices:operations", "perm:devices:search", "perm:devices:details", + "perm:devices:update", "perm:devices:view", "perm:view-configuration", "perm:manage-configuration", diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/public/js/listing.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/public/js/listing.js index 978d85e804b..9fe08fa365f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/public/js/listing.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/public/js/listing.js @@ -730,7 +730,7 @@ function attachDeviceEvents() { var deviceId = $(this).data("deviceid"); var deviceType = $(this).data("devicetype"); var deviceName = $(this).data("devicename"); - var serviceURL = "/api/device-mgt/v1.0/devices/type/" + deviceType + "/id/" + deviceId; + var serviceURL = "/api/device-mgt/v1.0/devices/type/" + deviceType + "/id/" + deviceId + "/rename"; $(modalPopupContent).html($('#edit-device-modal-content').html()); $('#edit-device-name').val(deviceName); @@ -738,7 +738,9 @@ function attachDeviceEvents() { $("a#edit-device-yes-link").click(function () { var newDeviceName = $('#edit-device-name').val(); - invokerUtil.put(serviceURL, {"name": newDeviceName}, function (message) { + var request = {}; + request['name'] = newDeviceName; + invokerUtil.put(serviceURL, request, function (message) { $(modalPopupContent).html($('#edit-device-200-content').html()); setTimeout(function () { hidePopup();