Merge branch 'geofence-api' into 'corrective-policy'

Add Geo Json and fence shape attributes

See merge request entgra/carbon-device-mgt!648
corrective-policy
Inosh Perara 4 years ago
commit 691d6c6d7d

@ -41,22 +41,29 @@ public class GeofenceWrapper {
@ApiModelProperty( @ApiModelProperty(
name = "latitude", name = "latitude",
value = "Latitude of center of the geo fence", value = "Latitude of center of the geo fence")
required = true)
private double latitude; private double latitude;
@ApiModelProperty( @ApiModelProperty(
name = "longitude", name = "longitude",
value = "Longitude of center of the geo fence", value = "Longitude of center of the geo fence")
required = true)
private double longitude; private double longitude;
@ApiModelProperty( @ApiModelProperty(
name = "radius", name = "radius",
value = "Radius from the center", value = "Radius from the center")
required = true)
private float radius; 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() { public int getId() {
return id; return id;
} }
@ -104,4 +111,20 @@ public class GeofenceWrapper {
public void setRadius(float radius) { public void setRadius(float radius) {
this.radius = 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;
}
} }

@ -597,6 +597,8 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService {
geofenceData.setLatitude(geofenceWrapper.getLatitude()); geofenceData.setLatitude(geofenceWrapper.getLatitude());
geofenceData.setLongitude(geofenceWrapper.getLongitude()); geofenceData.setLongitude(geofenceWrapper.getLongitude());
geofenceData.setRadius(geofenceWrapper.getRadius()); geofenceData.setRadius(geofenceWrapper.getRadius());
geofenceData.setGeoJson(geofenceWrapper.getGeoJson());
geofenceData.setFenceShape(geofenceWrapper.getFenceShape());
GeoLocationProviderService geoService = DeviceMgtAPIUtils.getGeoService(); GeoLocationProviderService geoService = DeviceMgtAPIUtils.getGeoService();
if (!geoService.createGeofence(geofenceData)) { if (!geoService.createGeofence(geofenceData)) {
@ -644,6 +646,8 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService {
geofenceWrapper.setLatitude(geofenceData.getLatitude()); geofenceWrapper.setLatitude(geofenceData.getLatitude());
geofenceWrapper.setLongitude(geofenceData.getLongitude()); geofenceWrapper.setLongitude(geofenceData.getLongitude());
geofenceWrapper.setRadius(geofenceData.getRadius()); geofenceWrapper.setRadius(geofenceData.getRadius());
geofenceWrapper.setGeoJson(geofenceData.getGeoJson());
geofenceWrapper.setFenceShape(geofenceData.getFenceShape());
return geofenceWrapper; return geofenceWrapper;
} }

@ -526,29 +526,39 @@ public class RequestValidationUtil {
} }
public static void validateGeofenceData(GeofenceWrapper geofenceWrapper) { public static void validateGeofenceData(GeofenceWrapper geofenceWrapper) {
boolean isGeoJsonExists = false;
if (geofenceWrapper.getFenceName() == null || geofenceWrapper.getFenceName().trim().isEmpty()) { if (geofenceWrapper.getFenceName() == null || geofenceWrapper.getFenceName().trim().isEmpty()) {
String msg = "Geofence name should not be null or empty"; String msg = "Geofence name should not be null or empty";
log.error(msg); log.error(msg);
throw new InputValidationException( throw new InputValidationException(
new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatus.SC_BAD_REQUEST).setMessage(msg).build()); 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"; String msg = "Latitude should be a value between -90 and 90";
log.error(msg); log.error(msg);
throw new InputValidationException( throw new InputValidationException(
new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatus.SC_BAD_REQUEST).setMessage(msg).build()); 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"; String msg = "Longitude should be a value between -180 and 180";
log.error(msg); log.error(msg);
throw new InputValidationException( throw new InputValidationException(
new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatus.SC_BAD_REQUEST).setMessage(msg).build()); 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"; String msg = "Minimum radius of the fence should be 1m";
log.error(msg); log.error(msg);
throw new InputValidationException( throw new InputValidationException(
new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatus.SC_BAD_REQUEST).setMessage(msg).build()); 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());
}
} }
} }

@ -18,8 +18,6 @@
package org.wso2.carbon.device.mgt.common.geo.service; package org.wso2.carbon.device.mgt.common.geo.service;
import org.apache.commons.lang.StringUtils;
public class GeofenceData { public class GeofenceData {
private int id; private int id;
private String fenceName; private String fenceName;
@ -29,6 +27,8 @@ public class GeofenceData {
private float radius; private float radius;
private int tenantId; private int tenantId;
private String owner; private String owner;
private String geoJson;
private String fenceShape;
public int getId() { public int getId() {
return id; return id;
@ -93,4 +93,20 @@ public class GeofenceData {
public void setOwner(String owner) { public void setOwner(String owner) {
this.owner = 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;
}
} }

@ -48,6 +48,8 @@ public class GeofenceDAOImpl implements GeofenceDAO {
"LATITUDE, " + "LATITUDE, " +
"LONGITUDE, " + "LONGITUDE, " +
"RADIUS, " + "RADIUS, " +
"GEO_JSON, " +
"FENCE_SHAPE, " +
"CREATED_TIMESTAMP, " + "CREATED_TIMESTAMP, " +
"OWNER, " + "OWNER, " +
"TENANT_ID) " + "TENANT_ID) " +
@ -58,9 +60,11 @@ public class GeofenceDAOImpl implements GeofenceDAO {
stmt.setDouble(3, geofenceData.getLatitude()); stmt.setDouble(3, geofenceData.getLatitude());
stmt.setDouble(4, geofenceData.getLongitude()); stmt.setDouble(4, geofenceData.getLongitude());
stmt.setFloat(5, geofenceData.getRadius()); stmt.setFloat(5, geofenceData.getRadius());
stmt.setTimestamp(6, new Timestamp(new Date().getTime())); stmt.setString(6, geofenceData.getGeoJson());
stmt.setString(7, geofenceData.getOwner()); stmt.setString(7, geofenceData.getFenceShape());
stmt.setInt(8, geofenceData.getTenantId()); stmt.setTimestamp(8, new Timestamp(new Date().getTime()));
stmt.setString(9, geofenceData.getOwner());
stmt.setInt(10, geofenceData.getTenantId());
return stmt.executeUpdate(); return stmt.executeUpdate();
} }
} catch (SQLException e) { } catch (SQLException e) {
@ -82,6 +86,8 @@ public class GeofenceDAOImpl implements GeofenceDAO {
"LATITUDE, " + "LATITUDE, " +
"LONGITUDE, " + "LONGITUDE, " +
"RADIUS, " + "RADIUS, " +
"GEO_JSON, " +
"FENCE_SHAPE, " +
"OWNER, " + "OWNER, " +
"TENANT_ID " + "TENANT_ID " +
"FROM DM_GEOFENCE " + "FROM DM_GEOFENCE " +
@ -116,6 +122,8 @@ public class GeofenceDAOImpl implements GeofenceDAO {
"LATITUDE, " + "LATITUDE, " +
"LONGITUDE, " + "LONGITUDE, " +
"RADIUS, " + "RADIUS, " +
"GEO_JSON, " +
"FENCE_SHAPE, " +
"OWNER, " + "OWNER, " +
"TENANT_ID " + "TENANT_ID " +
"FROM DM_GEOFENCE " + "FROM DM_GEOFENCE " +
@ -163,6 +171,8 @@ public class GeofenceDAOImpl implements GeofenceDAO {
"LATITUDE = ?, " + "LATITUDE = ?, " +
"LONGITUDE = ?, " + "LONGITUDE = ?, " +
"RADIUS = ? " + "RADIUS = ? " +
"GEO_JSON = ? " +
"FENCE_SHAPE = ? " +
"WHERE ID = ?"; "WHERE ID = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, geofenceData.getFenceName()); stmt.setString(1, geofenceData.getFenceName());
@ -170,7 +180,9 @@ public class GeofenceDAOImpl implements GeofenceDAO {
stmt.setDouble(3, geofenceData.getLatitude()); stmt.setDouble(3, geofenceData.getLatitude());
stmt.setDouble(4, geofenceData.getLongitude()); stmt.setDouble(4, geofenceData.getLongitude());
stmt.setFloat(5, geofenceData.getRadius()); 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(); return stmt.executeUpdate();
} }
} catch (SQLException e) { } catch (SQLException e) {
@ -194,6 +206,8 @@ public class GeofenceDAOImpl implements GeofenceDAO {
geofenceData.setLatitude(rst.getDouble("LATITUDE")); geofenceData.setLatitude(rst.getDouble("LATITUDE"));
geofenceData.setLongitude(rst.getDouble("LONGITUDE")); geofenceData.setLongitude(rst.getDouble("LONGITUDE"));
geofenceData.setRadius(rst.getFloat("RADIUS")); geofenceData.setRadius(rst.getFloat("RADIUS"));
geofenceData.setGeoJson(rst.getString("GEO_JSON"));
geofenceData.setFenceShape(rst.getString("FENCE_SHAPE"));
geofenceData.setOwner(rst.getString("OWNER")); geofenceData.setOwner(rst.getString("OWNER"));
geofenceData.setTenantId(rst.getInt("TENANT_ID")); geofenceData.setTenantId(rst.getInt("TENANT_ID"));
geofenceDataList.add(geofenceData); geofenceDataList.add(geofenceData);

@ -658,6 +658,8 @@ CREATE TABLE IF NOT EXISTS DM_GEOFENCE (
LATITUDE DOUBLE NULL, LATITUDE DOUBLE NULL,
LONGITUDE DOUBLE NULL, LONGITUDE DOUBLE NULL,
RADIUS DOUBLE NULL, RADIUS DOUBLE NULL,
GEO_JSON TEXT DEFAULT NULL,
FENCE_SHAPE VARCHAR(100) DEFAULT NULL,
CREATED_TIMESTAMP TIMESTAMP NOT NULL, CREATED_TIMESTAMP TIMESTAMP NOT NULL,
OWNER VARCHAR(255) NOT NULL, OWNER VARCHAR(255) NOT NULL,
TENANT_ID INTEGER DEFAULT 0, TENANT_ID INTEGER DEFAULT 0,

@ -703,9 +703,11 @@ CREATE TABLE DM_GEOFENCE (
ID INT IDENTITY NOT NULL, ID INT IDENTITY NOT NULL,
FENCE_NAME VARCHAR(255) NOT NULL, FENCE_NAME VARCHAR(255) NOT NULL,
DESCRIPTION VARCHAR(MAX) DEFAULT NULL, DESCRIPTION VARCHAR(MAX) DEFAULT NULL,
LATITUDE DECIMAL(3,5) NOT NULL, LATITUDE DECIMAL(3,5) DEFAULT NULL,
LONGITUDE DECIMAL(3,5) NOT NULL, LONGITUDE DECIMAL(3,5) DEFAULT NULL,
RADIUS DECIMAL(30,4) NOT NULL, RADIUS DECIMAL(30,4) DEFAULT NULL,
GEO_JSON VARCHAR(MAX) DEFAULT NULL,
FENCE_SHAPE VARCHAR(100) DEFAULT NULL,
CREATED_TIMESTAMP DATETIME2 NOT NULL, CREATED_TIMESTAMP DATETIME2 NOT NULL,
OWNER VARCHAR(255) NOT NULL, OWNER VARCHAR(255) NOT NULL,
TENANT_ID INTEGER DEFAULT 0, TENANT_ID INTEGER DEFAULT 0,

@ -719,9 +719,11 @@ CREATE TABLE IF NOT EXISTS DM_GEOFENCE (
ID INT NOT NULL AUTO_INCREMENT, ID INT NOT NULL AUTO_INCREMENT,
FENCE_NAME VARCHAR(255) NOT NULL, FENCE_NAME VARCHAR(255) NOT NULL,
DESCRIPTION TEXT DEFAULT NULL, DESCRIPTION TEXT DEFAULT NULL,
LATITUDE DOUBLE NOT NULL, LATITUDE DOUBLE DEFAULT NULL,
LONGITUDE DOUBLE NOT NULL, LONGITUDE DOUBLE DEFAULT NULL,
RADIUS DOUBLE NOT NULL, RADIUS DOUBLE DEFAULT NULL,
GEO_JSON TEXT DEFAULT NULL,
FENCE_SHAPE VARCHAR(100) DEFAULT NULL,
CREATED_TIMESTAMP TIMESTAMP NOT NULL, CREATED_TIMESTAMP TIMESTAMP NOT NULL,
OWNER VARCHAR(255) NOT NULL, OWNER VARCHAR(255) NOT NULL,
TENANT_ID INTEGER DEFAULT 0, TENANT_ID INTEGER DEFAULT 0,

@ -1072,9 +1072,11 @@ CREATE TABLE DM_GEOFENCE (
ID NUMBER(10) NOT NULL, ID NUMBER(10) NOT NULL,
FENCE_NAME VARCHAR2(255) NOT NULL, FENCE_NAME VARCHAR2(255) NOT NULL,
DESCRIPTION CLOB DEFAULT NULL, DESCRIPTION CLOB DEFAULT NULL,
LATITUDE BINARY_DOUBLE NOT NULL, LATITUDE BINARY_DOUBLE DEFAULT NULL,
LONGITUDE BINARY_DOUBLE NOT NULL, LONGITUDE BINARY_DOUBLE DEFAULT NULL,
RADIUS BINARY_DOUBLE NOT NULL, RADIUS BINARY_DOUBLE DEFAULT NULL,
GEO_JSON CLOB DEFAULT NULL,
FENCE_SHAPE VARCHAR2(100) DEFAULT NULL,
CREATED_TIMESTAMP TIMESTAMP(0) NOT NULL, CREATED_TIMESTAMP TIMESTAMP(0) NOT NULL,
OWNER VARCHAR2(255) NOT NULL, OWNER VARCHAR2(255) NOT NULL,
TENANT_ID NUMBER(10) DEFAULT 0, TENANT_ID NUMBER(10) DEFAULT 0,

@ -721,9 +721,11 @@ CREATE TABLE IF NOT EXISTS DM_GEOFENCE (
ID INTEGER DEFAULT NEXTVAL ('DM_GEOFENCE_seq') NOT NULL, ID INTEGER DEFAULT NEXTVAL ('DM_GEOFENCE_seq') NOT NULL,
FENCE_NAME VARCHAR(255) NOT NULL, FENCE_NAME VARCHAR(255) NOT NULL,
DESCRIPTION TEXT DEFAULT NULL, DESCRIPTION TEXT DEFAULT NULL,
LATITUDE DECIMAL(3,5) NOT NULL, LATITUDE DECIMAL(3,5) DEFAULT NULL,
LONGITUDE DECIMAL(3,5) NOT NULL, LONGITUDE DECIMAL(3,5) DEFAULT NULL,
RADIUS DECIMAL(35,3) NOT NULL, RADIUS DECIMAL(35,3) DEFAULT NULL,
GEO_JSON TEXT DEFAULT NULL,
FENCE_SHAPE VARCHAR(100) DEFAULT NULL,
CREATED_TIMESTAMP TIMESTAMP(0) NOT NULL, CREATED_TIMESTAMP TIMESTAMP(0) NOT NULL,
OWNER VARCHAR(255) NOT NULL, OWNER VARCHAR(255) NOT NULL,
TENANT_ID INTEGER DEFAULT 0, TENANT_ID INTEGER DEFAULT 0,

Loading…
Cancel
Save