From 47dc8ec708892ebf9d4e8b60e2f9384389b98a13 Mon Sep 17 00:00:00 2001 From: Pahansith Date: Wed, 30 Sep 2020 14:05:06 +0530 Subject: [PATCH] Add Geo Json and fence shape attributes --- .../mgt/jaxrs/beans/GeofenceWrapper.java | 35 +++++++++++++++---- .../impl/GeoLocationBasedServiceImpl.java | 4 +++ .../impl/util/RequestValidationUtil.java | 16 +++++++-- .../mgt/common/geo/service/GeofenceData.java | 20 +++++++++-- .../mgt/core/dao/impl/GeofenceDAOImpl.java | 22 +++++++++--- .../src/main/resources/dbscripts/cdm/h2.sql | 2 ++ .../main/resources/dbscripts/cdm/mssql.sql | 8 +++-- .../main/resources/dbscripts/cdm/mysql.sql | 8 +++-- .../main/resources/dbscripts/cdm/oracle.sql | 8 +++-- .../resources/dbscripts/cdm/postgresql.sql | 8 +++-- 10 files changed, 104 insertions(+), 27 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/GeofenceWrapper.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/GeofenceWrapper.java index 51d14baf11..533775b73b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/GeofenceWrapper.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/GeofenceWrapper.java @@ -41,22 +41,29 @@ public class GeofenceWrapper { @ApiModelProperty( name = "latitude", - value = "Latitude of center of the geo fence", - required = true) + value = "Latitude of center of the geo fence") private double latitude; @ApiModelProperty( name = "longitude", - value = "Longitude of center of the geo fence", - required = true) + value = "Longitude of center of the geo fence") private double longitude; @ApiModelProperty( name = "radius", - value = "Radius from the center", - required = true) + value = "Radius from the center") private float radius; + @ApiModelProperty( + name = "geoJson", + value = "JSON data set of the polygon fence") + private String geoJson; + + @ApiModelProperty( + name = "fenceShape", + value = "Shape of the fence") + private String fenceShape; + public int getId() { return id; } @@ -104,4 +111,20 @@ public class GeofenceWrapper { public void setRadius(float radius) { this.radius = radius; } + + public String getGeoJson() { + return geoJson; + } + + public void setGeoJson(String geoJson) { + this.geoJson = geoJson; + } + + public String getFenceShape() { + return fenceShape; + } + + public void setFenceShape(String fenceShape) { + this.fenceShape = fenceShape; + } } 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 8c0c56fc8c..ad8ce0829b 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 @@ -597,6 +597,8 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService { geofenceData.setLatitude(geofenceWrapper.getLatitude()); geofenceData.setLongitude(geofenceWrapper.getLongitude()); geofenceData.setRadius(geofenceWrapper.getRadius()); + geofenceData.setGeoJson(geofenceWrapper.getGeoJson()); + geofenceData.setFenceShape(geofenceWrapper.getFenceShape()); GeoLocationProviderService geoService = DeviceMgtAPIUtils.getGeoService(); if (!geoService.createGeofence(geofenceData)) { @@ -644,6 +646,8 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService { geofenceWrapper.setLatitude(geofenceData.getLatitude()); geofenceWrapper.setLongitude(geofenceData.getLongitude()); geofenceWrapper.setRadius(geofenceData.getRadius()); + geofenceWrapper.setGeoJson(geofenceData.getGeoJson()); + geofenceWrapper.setFenceShape(geofenceData.getFenceShape()); return geofenceWrapper; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/RequestValidationUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/RequestValidationUtil.java index 8bafe1b846..0c2157cb53 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/RequestValidationUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/RequestValidationUtil.java @@ -526,29 +526,39 @@ public class RequestValidationUtil { } public static void validateGeofenceData(GeofenceWrapper geofenceWrapper) { + boolean isGeoJsonExists = false; if (geofenceWrapper.getFenceName() == null || geofenceWrapper.getFenceName().trim().isEmpty()) { String msg = "Geofence name should not be null or empty"; log.error(msg); throw new InputValidationException( new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatus.SC_BAD_REQUEST).setMessage(msg).build()); } - if (geofenceWrapper.getLatitude() < -90 || geofenceWrapper.getLatitude() > 90) { + if (!geofenceWrapper.getGeoJson().trim().isEmpty()) { + isGeoJsonExists = true; + } + if ((geofenceWrapper.getLatitude() < -90 || geofenceWrapper.getLatitude() > 90) && !isGeoJsonExists) { String msg = "Latitude should be a value between -90 and 90"; log.error(msg); throw new InputValidationException( new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatus.SC_BAD_REQUEST).setMessage(msg).build()); } - if (geofenceWrapper.getLongitude() < -180 || geofenceWrapper.getLongitude() > 180) { + if ((geofenceWrapper.getLongitude() < -180 || geofenceWrapper.getLongitude() > 180) && !isGeoJsonExists) { String msg = "Longitude should be a value between -180 and 180"; log.error(msg); throw new InputValidationException( new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatus.SC_BAD_REQUEST).setMessage(msg).build()); } - if (geofenceWrapper.getRadius() < 1) { + if (geofenceWrapper.getRadius() < 1 && !isGeoJsonExists) { String msg = "Minimum radius of the fence should be 1m"; log.error(msg); throw new InputValidationException( new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatus.SC_BAD_REQUEST).setMessage(msg).build()); } + if (geofenceWrapper.getFenceShape().trim().isEmpty()) { + String msg = "Fence shape should not be empty"; + log.error(msg); + throw new InputValidationException( + new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatus.SC_BAD_REQUEST).setMessage(msg).build()); + } } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/GeofenceData.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/GeofenceData.java index 5c0ee4293a..0dbc4a4294 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/GeofenceData.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/GeofenceData.java @@ -18,8 +18,6 @@ package org.wso2.carbon.device.mgt.common.geo.service; -import org.apache.commons.lang.StringUtils; - public class GeofenceData { private int id; private String fenceName; @@ -29,6 +27,8 @@ public class GeofenceData { private float radius; private int tenantId; private String owner; + private String geoJson; + private String fenceShape; public int getId() { return id; @@ -93,4 +93,20 @@ public class GeofenceData { public void setOwner(String owner) { this.owner = owner; } + + public String getGeoJson() { + return geoJson; + } + + public void setGeoJson(String geoJson) { + this.geoJson = geoJson; + } + + public String getFenceShape() { + return fenceShape; + } + + public void setFenceShape(String fenceShape) { + this.fenceShape = fenceShape; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GeofenceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GeofenceDAOImpl.java index 15d0ee859c..7f64c04923 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GeofenceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GeofenceDAOImpl.java @@ -48,6 +48,8 @@ public class GeofenceDAOImpl implements GeofenceDAO { "LATITUDE, " + "LONGITUDE, " + "RADIUS, " + + "GEO_JSON, " + + "FENCE_SHAPE, " + "CREATED_TIMESTAMP, " + "OWNER, " + "TENANT_ID) " + @@ -58,9 +60,11 @@ public class GeofenceDAOImpl implements GeofenceDAO { stmt.setDouble(3, geofenceData.getLatitude()); stmt.setDouble(4, geofenceData.getLongitude()); stmt.setFloat(5, geofenceData.getRadius()); - stmt.setTimestamp(6, new Timestamp(new Date().getTime())); - stmt.setString(7, geofenceData.getOwner()); - stmt.setInt(8, geofenceData.getTenantId()); + stmt.setString(6, geofenceData.getGeoJson()); + stmt.setString(7, geofenceData.getFenceShape()); + stmt.setTimestamp(8, new Timestamp(new Date().getTime())); + stmt.setString(9, geofenceData.getOwner()); + stmt.setInt(10, geofenceData.getTenantId()); return stmt.executeUpdate(); } } catch (SQLException e) { @@ -82,6 +86,8 @@ public class GeofenceDAOImpl implements GeofenceDAO { "LATITUDE, " + "LONGITUDE, " + "RADIUS, " + + "GEO_JSON, " + + "FENCE_SHAPE, " + "OWNER, " + "TENANT_ID " + "FROM DM_GEOFENCE " + @@ -116,6 +122,8 @@ public class GeofenceDAOImpl implements GeofenceDAO { "LATITUDE, " + "LONGITUDE, " + "RADIUS, " + + "GEO_JSON, " + + "FENCE_SHAPE, " + "OWNER, " + "TENANT_ID " + "FROM DM_GEOFENCE " + @@ -163,6 +171,8 @@ public class GeofenceDAOImpl implements GeofenceDAO { "LATITUDE = ?, " + "LONGITUDE = ?, " + "RADIUS = ? " + + "GEO_JSON = ? " + + "FENCE_SHAPE = ? " + "WHERE ID = ?"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setString(1, geofenceData.getFenceName()); @@ -170,7 +180,9 @@ public class GeofenceDAOImpl implements GeofenceDAO { stmt.setDouble(3, geofenceData.getLatitude()); stmt.setDouble(4, geofenceData.getLongitude()); stmt.setFloat(5, geofenceData.getRadius()); - stmt.setInt(6, fenceId); + stmt.setString(6, geofenceData.getGeoJson()); + stmt.setString(7, geofenceData.getFenceShape()); + stmt.setInt(8, fenceId); return stmt.executeUpdate(); } } catch (SQLException e) { @@ -194,6 +206,8 @@ public class GeofenceDAOImpl implements GeofenceDAO { geofenceData.setLatitude(rst.getDouble("LATITUDE")); geofenceData.setLongitude(rst.getDouble("LONGITUDE")); geofenceData.setRadius(rst.getFloat("RADIUS")); + geofenceData.setGeoJson(rst.getString("GEO_JSON")); + geofenceData.setFenceShape(rst.getString("FENCE_SHAPE")); geofenceData.setOwner(rst.getString("OWNER")); geofenceData.setTenantId(rst.getInt("TENANT_ID")); geofenceDataList.add(geofenceData); diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql index 1a5e038c68..4d754cd3f9 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -658,6 +658,8 @@ CREATE TABLE IF NOT EXISTS DM_GEOFENCE ( LATITUDE DOUBLE NULL, LONGITUDE DOUBLE NULL, RADIUS DOUBLE NULL, + GEO_JSON TEXT DEFAULT NULL, + FENCE_SHAPE VARCHAR(100) DEFAULT NULL, CREATED_TIMESTAMP TIMESTAMP NOT NULL, OWNER VARCHAR(255) NOT NULL, TENANT_ID INTEGER DEFAULT 0, diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql index 97c78d27e7..28f36b63ff 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql @@ -703,9 +703,11 @@ CREATE TABLE DM_GEOFENCE ( ID INT IDENTITY NOT NULL, FENCE_NAME VARCHAR(255) NOT NULL, DESCRIPTION VARCHAR(MAX) DEFAULT NULL, - LATITUDE DECIMAL(3,5) NOT NULL, - LONGITUDE DECIMAL(3,5) NOT NULL, - RADIUS DECIMAL(30,4) NOT NULL, + LATITUDE DECIMAL(3,5) DEFAULT NULL, + LONGITUDE DECIMAL(3,5) DEFAULT NULL, + RADIUS DECIMAL(30,4) DEFAULT NULL, + GEO_JSON VARCHAR(MAX) DEFAULT NULL, + FENCE_SHAPE VARCHAR(100) DEFAULT NULL, CREATED_TIMESTAMP DATETIME2 NOT NULL, OWNER VARCHAR(255) NOT NULL, TENANT_ID INTEGER DEFAULT 0, diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql index f4ca10da8c..63b751823b 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql @@ -719,9 +719,11 @@ CREATE TABLE IF NOT EXISTS DM_GEOFENCE ( ID INT NOT NULL AUTO_INCREMENT, FENCE_NAME VARCHAR(255) NOT NULL, DESCRIPTION TEXT DEFAULT NULL, - LATITUDE DOUBLE NOT NULL, - LONGITUDE DOUBLE NOT NULL, - RADIUS DOUBLE NOT NULL, + LATITUDE DOUBLE DEFAULT NULL, + LONGITUDE DOUBLE DEFAULT NULL, + RADIUS DOUBLE DEFAULT NULL, + GEO_JSON TEXT DEFAULT NULL, + FENCE_SHAPE VARCHAR(100) DEFAULT NULL, CREATED_TIMESTAMP TIMESTAMP NOT NULL, OWNER VARCHAR(255) NOT NULL, TENANT_ID INTEGER DEFAULT 0, diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql index d20da813c6..42b32d5ab2 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql @@ -1072,9 +1072,11 @@ CREATE TABLE DM_GEOFENCE ( ID NUMBER(10) NOT NULL, FENCE_NAME VARCHAR2(255) NOT NULL, DESCRIPTION CLOB DEFAULT NULL, - LATITUDE BINARY_DOUBLE NOT NULL, - LONGITUDE BINARY_DOUBLE NOT NULL, - RADIUS BINARY_DOUBLE NOT NULL, + LATITUDE BINARY_DOUBLE DEFAULT NULL, + LONGITUDE BINARY_DOUBLE DEFAULT NULL, + RADIUS BINARY_DOUBLE DEFAULT NULL, + GEO_JSON CLOB DEFAULT NULL, + FENCE_SHAPE VARCHAR2(100) DEFAULT NULL, CREATED_TIMESTAMP TIMESTAMP(0) NOT NULL, OWNER VARCHAR2(255) NOT NULL, TENANT_ID NUMBER(10) DEFAULT 0, diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql index 778da5d6fc..9e3408577a 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql @@ -721,9 +721,11 @@ CREATE TABLE IF NOT EXISTS DM_GEOFENCE ( ID INTEGER DEFAULT NEXTVAL ('DM_GEOFENCE_seq') NOT NULL, FENCE_NAME VARCHAR(255) NOT NULL, DESCRIPTION TEXT DEFAULT NULL, - LATITUDE DECIMAL(3,5) NOT NULL, - LONGITUDE DECIMAL(3,5) NOT NULL, - RADIUS DECIMAL(35,3) NOT NULL, + LATITUDE DECIMAL(3,5) DEFAULT NULL, + LONGITUDE DECIMAL(3,5) DEFAULT NULL, + RADIUS DECIMAL(35,3) DEFAULT NULL, + GEO_JSON TEXT DEFAULT NULL, + FENCE_SHAPE VARCHAR(100) DEFAULT NULL, CREATED_TIMESTAMP TIMESTAMP(0) NOT NULL, OWNER VARCHAR(255) NOT NULL, TENANT_ID INTEGER DEFAULT 0,