From 796acbda1679ea3350d611ce9fc597636df0fa83 Mon Sep 17 00:00:00 2001 From: charitha Date: Wed, 11 Jul 2018 10:28:14 +0530 Subject: [PATCH] Add location extraction from device properties and deice type specific location retrieval --- .../service/api/GeoLocationBasedService.java | 4 ++ .../impl/GeoLocationBasedServiceImpl.java | 3 +- .../impl/GeoLocationBasedServiceImplTest.java | 9 ++-- .../carbon/device/mgt/core/dao/DeviceDAO.java | 10 ++-- .../core/dao/impl/AbstractDeviceDAOImpl.java | 12 +++-- .../DeviceManagementProviderService.java | 2 +- .../DeviceManagementProviderServiceImpl.java | 49 +++++++++++++++++-- .../impl/GeoLocationBasedServiceImpl.java | 2 +- .../impl/GeoLocationBasedServiceImplTest.java | 5 +- 9 files changed, 74 insertions(+), 22 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GeoLocationBasedService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GeoLocationBasedService.java index aa84f83071..0fbe424cdf 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GeoLocationBasedService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GeoLocationBasedService.java @@ -198,6 +198,10 @@ public interface GeoLocationBasedService { response = Response.class) }) Response getGeoDeviceLocations( + @ApiParam( + name = "deviceType", + value = "Optional Device type name.") + @QueryParam("deviceType") String deviceType, @ApiParam( name = "minLat", value = "Define the minimum latitude of the geofence.", diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java index 89f7c1aa7a..7244f00587 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java @@ -125,6 +125,7 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService { @Consumes("application/json") @Produces("application/json") public Response getGeoDeviceLocations( + @QueryParam("deviceType") String deviceType, @QueryParam("minLat") double minLat, @QueryParam("maxLat") double maxLat, @QueryParam("minLong") double minLong, @@ -138,7 +139,7 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService { DeviceManagementProviderService deviceManagementService = DeviceMgtAPIUtils.getDeviceManagementService(); List geoClusters; try { - geoClusters = deviceManagementService.findGeoClusters(southWest, northEast, geohashLength); + geoClusters = deviceManagementService.findGeoClusters(deviceType, southWest, northEast, geohashLength); } catch (DeviceManagementException e) { String msg = "Error occurred while retrieving geo clusters "; log.error(msg, e); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImplTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImplTest.java index ebd20ecbfe..b655b1095e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImplTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImplTest.java @@ -1,6 +1,5 @@ package org.wso2.carbon.device.mgt.jaxrs.service.impl; - import org.mockito.Mockito; import org.testng.Assert; import org.testng.annotations.BeforeClass; @@ -33,8 +32,8 @@ public class GeoLocationBasedServiceImplTest { "in the given map boundaries") public void testGetGeoDeviceLocations1() throws DeviceManagementException { Mockito.doReturn(new ArrayList()).when(deviceManagementProviderService) - .findGeoClusters(Mockito.any(GeoCoordinate.class), Mockito.any(GeoCoordinate.class), Mockito.anyInt()); - Response response = geoLocationBasedService.getGeoDeviceLocations(0.4, 15, 75.6, + .findGeoClusters(null, Mockito.any(GeoCoordinate.class), Mockito.any(GeoCoordinate.class), Mockito.anyInt()); + Response response = geoLocationBasedService.getGeoDeviceLocations(null, 0.4, 15, 75.6, 90.1, 6); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), "getGeoDeviceLocations request failed with valid parameters"); @@ -51,8 +50,8 @@ public class GeoLocationBasedServiceImplTest { new GeoCoordinate(9.8, 84.7), new GeoCoordinate(11.1, 88.1), 4, "t1gd", "swerty12s", "android", "1234")); Mockito.doReturn(geoClusters).when(deviceManagementProviderService) - .findGeoClusters(Mockito.any(GeoCoordinate.class), Mockito.any(GeoCoordinate.class), Mockito.anyInt()); - Response response = geoLocationBasedService.getGeoDeviceLocations(0.4, 15, 75.6, + .findGeoClusters(null, Mockito.any(GeoCoordinate.class), Mockito.any(GeoCoordinate.class), Mockito.anyInt()); + Response response = geoLocationBasedService.getGeoDeviceLocations(null, 0.4, 15, 75.6, 90.1, 6); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), "getGeoDeviceLocations request failed with valid parameters"); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java index b4247966f9..b1f2ffb043 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java @@ -402,11 +402,13 @@ public interface DeviceDAO { * This method is used to retrieve the details of geoclusters formed relatively to the zoom level and map * boundaries. * - * @param southWest the coordinates of southWest corner of the map. - * @param northEast the coordinates of northEast corner of the map. - * @param tenantId tenant id. + * @param deviceType Optional device type name. + * @param southWest the coordinates of southWest corner of the map. + * @param northEast the coordinates of northEast corner of the map. + * @param tenantId tenant id. * @return returns a list of enrolment info objects. */ - List findGeoClusters(GeoCoordinate southWest, GeoCoordinate northEast, int geohashLength,int tenantId) throws DeviceManagementDAOException; + List findGeoClusters(String deviceType, GeoCoordinate southWest, GeoCoordinate northEast, + int geohashLength,int tenantId) throws DeviceManagementDAOException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index 0e97c24d8b..2d0d877c03 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -1062,7 +1062,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { return tenants; } - public List findGeoClusters(GeoCoordinate southWest, GeoCoordinate northEast, + public List findGeoClusters(String deviceType, GeoCoordinate southWest, GeoCoordinate northEast, int geohashLength, int tenantId) throws DeviceManagementDAOException { Connection conn; PreparedStatement stmt = null; @@ -1082,8 +1082,11 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { "WHERE DEVICE_LOCATION.LATITUDE BETWEEN ? AND ? AND " + "DEVICE_LOCATION.LONGITUDE BETWEEN ? AND ? AND " + "DEVICE.TENANT_ID=? AND " + - "DEVICE.ID=DEVICE_LOCATION.DEVICE_ID AND DEVICE.DEVICE_TYPE_ID=DEVICE_TYPE.ID" + - " GROUP BY GEOHASH_PREFIX"; + "DEVICE.ID=DEVICE_LOCATION.DEVICE_ID AND DEVICE.DEVICE_TYPE_ID=DEVICE_TYPE.ID"; + if (deviceType != null && !deviceType.isEmpty()) { + sql += " AND DEVICE_TYPE.NAME=?"; + } + sql += " GROUP BY GEOHASH_PREFIX"; stmt = conn.prepareStatement(sql); stmt.setInt(1, geohashLength); stmt.setDouble(2, southWest.getLatitude()); @@ -1091,6 +1094,9 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { stmt.setDouble(4, southWest.getLongitude()); stmt.setDouble(5, northEast.getLongitude()); stmt.setDouble(6,tenantId); + if (deviceType != null && !deviceType.isEmpty()) { + stmt.setString(7, deviceType); + } rs = stmt.executeQuery(); while (rs.next()) { double latitude = rs.getDouble("LATITUDE"); 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 864b962528..7117b9bd18 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 @@ -625,6 +625,6 @@ public interface DeviceManagementProviderService { List getDeviceEnrolledTenants() throws DeviceManagementException; - List findGeoClusters(GeoCoordinate southWest, GeoCoordinate northEast, + List findGeoClusters(String deviceType, GeoCoordinate southWest, GeoCoordinate northEast, int geohashLength) 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 798b9b74de..e87711e7ab 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 @@ -67,8 +67,10 @@ import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO; import org.wso2.carbon.device.mgt.core.dao.EnrollmentDAO; +import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceInformationManager; import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsDAO; import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsMgtDAOException; +import org.wso2.carbon.device.mgt.core.device.details.mgt.impl.DeviceInformationManagerImpl; import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceTypeServiceIdentifier; import org.wso2.carbon.device.mgt.core.geo.GeoCluster; @@ -297,10 +299,10 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv addDeviceToGroups(deviceIdentifier, device.getEnrolmentInfo().getOwnership()); addInitialOperations(deviceIdentifier, device.getType()); } + extractDeviceLocationToUpdate(device); return status; } - @Override public boolean modifyEnrollment(Device device) throws DeviceManagementException { if (device == null) { @@ -352,6 +354,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } finally { DeviceManagementDAOFactory.closeConnection(); } + extractDeviceLocationToUpdate(device); return status; } @@ -2614,13 +2617,18 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } @Override - public List findGeoClusters(GeoCoordinate southWest, GeoCoordinate northEast, int geohashLength) throws DeviceManagementException { + public List findGeoClusters(String deviceType, GeoCoordinate southWest, GeoCoordinate northEast, + int geohashLength) throws DeviceManagementException { if (log.isDebugEnabled()) { - log.debug("get information about geo clusters"); + if (deviceType == null || deviceType.isEmpty()) { + log.debug("get information about geo clusters."); + } else { + log.debug("get information about geo clusters for device type: " + deviceType); + } } try { DeviceManagementDAOFactory.openConnection(); - return deviceDAO.findGeoClusters(southWest, northEast, geohashLength, this.getTenantId()); + return deviceDAO.findGeoClusters(deviceType, southWest, northEast, geohashLength, this.getTenantId()); } catch (DeviceManagementDAOException e) { String msg = "Error occurred while retrieving the geo clusters."; log.error(msg, e); @@ -2637,4 +2645,37 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.closeConnection(); } } + + private void extractDeviceLocationToUpdate(Device device) { + List properties = device.getProperties(); + if (properties != null) { + String latitude = null; + String longitude = null; + for (Device.Property p : properties) { + if (p.getName().equalsIgnoreCase("latitude")) { + latitude = p.getValue(); + } + if (p.getName().equalsIgnoreCase("longitude")) { + longitude = p.getValue(); + } + } + if (latitude != null && longitude != null && !latitude.isEmpty() && !longitude.isEmpty()) { + DeviceLocation deviceLocation = new DeviceLocation(); + deviceLocation.setDeviceId(device.getId()); + deviceLocation.setDeviceIdentifier(new DeviceIdentifier(device.getDeviceIdentifier(), + device.getType())); + try { + deviceLocation.setLatitude(Double.parseDouble(latitude)); + deviceLocation.setLongitude(Double.parseDouble(longitude)); + DeviceInformationManager deviceInformationManager = new DeviceInformationManagerImpl(); + deviceInformationManager.addDeviceLocation(deviceLocation); + } catch (Exception e) { + //We are not failing the execution since this is not critical for the functionality. But logging as + // an error for reference. + log.error("Exception occurred while trying to add device location.", e); + } + } + } + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.v09.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.v09.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java index 34fbaae517..7c865b9778 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.v09.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.v09.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java @@ -138,7 +138,7 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService { DeviceManagementProviderService deviceManagementService = DeviceMgtAPIUtils.getDeviceManagementService(); List geoClusters; try { - geoClusters = deviceManagementService.findGeoClusters(southWest, northEast, geohashLength); + geoClusters = deviceManagementService.findGeoClusters(null, southWest, northEast, geohashLength); } catch (DeviceManagementException e) { String msg = "Error occurred while retrieving geo clusters "; log.error(msg, e); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.v09.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImplTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.v09.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImplTest.java index 0d33938c09..d320a626b4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.v09.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImplTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.v09.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImplTest.java @@ -19,7 +19,6 @@ package org.wso2.carbon.device.mgt.jaxrs.service.impl; - import org.mockito.Mockito; import org.testng.Assert; import org.testng.annotations.BeforeClass; @@ -52,7 +51,7 @@ public class GeoLocationBasedServiceImplTest { "in the given map boundaries") public void testGetGeoDeviceLocations1() throws DeviceManagementException { Mockito.doReturn(new ArrayList()).when(deviceManagementProviderService) - .findGeoClusters(Mockito.any(GeoCoordinate.class), Mockito.any(GeoCoordinate.class), Mockito.anyInt()); + .findGeoClusters(null, Mockito.any(GeoCoordinate.class), Mockito.any(GeoCoordinate.class), Mockito.anyInt()); Response response = geoLocationBasedService.getGeoDeviceLocations(0.4, 15, 75.6, 90.1, 6); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), @@ -70,7 +69,7 @@ public class GeoLocationBasedServiceImplTest { new GeoCoordinate(9.8, 84.7), new GeoCoordinate(11.1, 88.1), 4, "t1gd", "swerty12s", "android", "1234")); Mockito.doReturn(geoClusters).when(deviceManagementProviderService) - .findGeoClusters(Mockito.any(GeoCoordinate.class), Mockito.any(GeoCoordinate.class), Mockito.anyInt()); + .findGeoClusters(null,Mockito.any(GeoCoordinate.class), Mockito.any(GeoCoordinate.class), Mockito.anyInt()); Response response = geoLocationBasedService.getGeoDeviceLocations(0.4, 15, 75.6, 90.1, 6); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(),