From 03e87c558f73ed94c64969c62bfaf225e6effc26 Mon Sep 17 00:00:00 2001 From: Shamalka Navod Date: Tue, 19 May 2020 10:40:41 +0000 Subject: [PATCH] Get devices by identifier list API --- .../service/api/DeviceManagementService.java | 46 +++++++++++++++++++ .../impl/DeviceManagementServiceImpl.java | 21 +++++++++ .../DeviceManagementProviderService.java | 11 +++++ .../DeviceManagementProviderServiceImpl.java | 19 ++++++++ 4 files changed, 97 insertions(+) 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 9e786f1deb..02ded254df 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 @@ -629,6 +629,52 @@ public interface DeviceManagementService { @QueryParam("requireDeviceInfo") boolean requireDeviceInfo); + @POST + @Produces(MediaType.APPLICATION_JSON) + @Path("/type/any/list") + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "POST", + value = "Getting Details of Devices", + notes = "Get the details of devices by specifying the device identifiers.", + tags = "Device Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = "perm:devices:details") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully fetched the details 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 was last modified.\n" + + "Used by caches, or in conditional requests."), + }), + @ApiResponse( + code = 400, + message = "Bad Request. \n Invalid request or validation error.", + response = ErrorResponse.class), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n " + + "Server error occurred while retrieving the device details.", + response = ErrorResponse.class) + }) + Response getDeviceByIdList(List deviceIds); + @PUT @Produces(MediaType.APPLICATION_JSON) 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 baa9b4a2c4..3600ffb3ab 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 @@ -662,6 +662,27 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { return Response.status(Response.Status.OK).entity(device).build(); } + @POST + @Path("/type/any/list") + @Override + public Response getDeviceByIdList(List deviceIds) { + DeviceManagementProviderService deviceManagementProviderService = + DeviceMgtAPIUtils.getDeviceManagementService(); + if (deviceIds == null || deviceIds.isEmpty()) { + String msg = "Required values of device identifiers are not set.."; + log.error(msg); + return Response.status(Response.Status.BAD_REQUEST).build(); + } + try { + List devices = deviceManagementProviderService.getDeviceByIdList(deviceIds); + return Response.status(Response.Status.OK).entity(devices).build(); + } catch (DeviceManagementException e) { + String msg = "Error encountered while retrieving devices"; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } + @GET @Path("/{type}/{id}/location") @Override 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 5bb5c70f54..54c7c04805 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 @@ -916,4 +916,15 @@ public interface DeviceManagementProviderService { * @throws {@link OperationManagementException} */ boolean isOperationExist(DeviceIdentifier deviceId, int operationId) throws OperationManagementException; + + /** + * Get device list for a given device identifier list + * + * @param deviceIdentifiers A list of device identifiers + * @return A list of devices + * @throws {@link DeviceManagementException} + * @throws {@link InvalidDeviceException} + */ + List getDeviceByIdList(List deviceIdentifiers) + 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 e1776f3fa7..dd2d63df8d 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 @@ -4177,4 +4177,23 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return pluginRepository.getOperationManager(deviceId.getType(), this.getTenantId()) .isOperationExist(deviceId, operationId); } + + @Override + public List getDeviceByIdList(List deviceIdentifiers) throws DeviceManagementException { + int tenantId = this.getTenantId(); + try { + DeviceManagementDAOFactory.openConnection(); + return deviceDAO.getDevicesByIdentifiers(deviceIdentifiers, tenantId); + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while retrieving device list."; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } }