From 02217825335983418030ef316be56803d7cecf37 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Sat, 28 May 2016 01:57:36 +0530 Subject: [PATCH] Adding new apis for getting the device location when list of device identifiers are supplied --- .../mgt/jaxrs/api/DeviceInformation.java | 20 ++++++++++++++++ .../jaxrs/api/impl/DeviceInformationImpl.java | 17 ++++++++++++++ .../details/mgt/DeviceInformationManager.java | 8 +++++++ .../impl/DeviceInformationManagerImpl.java | 23 ++++++++++++++++++- 4 files changed, 67 insertions(+), 1 deletion(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/DeviceInformation.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/DeviceInformation.java index b10412b516a..355806ee9db 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/DeviceInformation.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/DeviceInformation.java @@ -105,4 +105,24 @@ public interface DeviceInformation { @ApiParam(name = "id", value = "Provide the device identifier", required = true) @PathParam("id") String id); + + @GET + @Path("location/list") + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "Get the locations of devices", + notes = "This will return the locations of devices including latitude and longitude as well the " + + "physical address for the supplied device identifiers", + response = DeviceLocation.class) + @ApiResponses(value = { + @ApiResponse(code = 200, message = ""), + @ApiResponse(code = 400, message = ""), + @ApiResponse(code = 400, message = ""), + @ApiResponse(code = 500, message = "Internal Server Error") + }) + @Permission(scope = "device-info", permissions = {"/permission/admin/device-mgt/admin/devices/list"}) + Response getDeviceLocations(@ApiParam(name = "deviceIdentifiers", value = "List of device identifiers", + required = true) List deviceIdentifiers); + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/impl/DeviceInformationImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/impl/DeviceInformationImpl.java index a59c81be542..3bbc90d4f4b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/impl/DeviceInformationImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/impl/DeviceInformationImpl.java @@ -19,6 +19,7 @@ package org.wso2.carbon.device.mgt.jaxrs.api.impl; +import io.swagger.annotations.ApiParam; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; @@ -96,5 +97,21 @@ public class DeviceInformationImpl implements DeviceInformation { } return Response.status(Response.Status.OK).entity(deviceLocation).build(); } + + @Override + public Response getDeviceLocations(@ApiParam(name = "deviceIdentifiers", value = "List of device identifiers", + required = true) List deviceIdentifiers) { + DeviceInformationManager informationManager; + List deviceLocations; + try { + informationManager = DeviceMgtAPIUtils.getDeviceInformationManagerService(); + deviceLocations = informationManager.getDeviceLocations(deviceIdentifiers); + } catch (DeviceDetailsMgtException e) { + String msg = "Error occurred while getting the device location."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + return Response.status(Response.Status.OK).entity(deviceLocations).build(); + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/DeviceInformationManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/DeviceInformationManager.java index 5b62a0f8dd2..1c947b63728 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/DeviceInformationManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/DeviceInformationManager.java @@ -72,6 +72,14 @@ public interface DeviceInformationManager { */ DeviceLocation getDeviceLocation(DeviceIdentifier deviceIdentifier) throws DeviceDetailsMgtException; + /** + * This method will return the device location with latitude, longitude, address etc.. of supplied devices. + * @param deviceIdentifiers - List of Device identifier and device type. + * @return Device Location list. + * @throws DeviceDetailsMgtException + */ + List getDeviceLocations(List deviceIdentifiers) throws DeviceDetailsMgtException; + // /** // * This method will manage the storing of device application list. // * @param deviceApplication - Device application list. 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 b2266d35cd8..96f8addaae5 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 @@ -121,7 +121,7 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { } } DeviceManagementDAOFactory.openConnection(); - for(Integer id : deviceIds) { + for (Integer id : deviceIds) { DeviceInfo deviceInfo = deviceDetailsDAO.getDeviceInformation(id); deviceInfo.setDeviceDetailsMap(deviceDetailsDAO.getDeviceProperties(id)); deviceInfos.add(deviceInfo); @@ -183,5 +183,26 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { } } + @Override + public List getDeviceLocations(List deviceIdentifiers) throws DeviceDetailsMgtException { + + try { + List devices = DeviceManagementDataHolder.getInstance(). + getDeviceManagementProvider().getAllDevices(deviceIdentifiers.get(0).getType()); + List deviceLocations = new ArrayList<>(); + DeviceManagementDAOFactory.openConnection(); + for (Device device : devices) { + deviceLocations.add(deviceDetailsDAO.getDeviceLocation(device.getId())); + } + return deviceLocations; + } catch (DeviceManagementException e) { + throw new DeviceDetailsMgtException("Exception occurred while retrieving the devices.", e); + } catch (SQLException e) { + throw new DeviceDetailsMgtException("SQL error occurred while retrieving device from database.", e); + } catch (DeviceDetailsMgtDAOException e) { + throw new DeviceDetailsMgtException("Exception occurred while retrieving device locations.", e); + } + } + }