forked from community/device-mgt-core
Create geofence APIs See merge request entgra/carbon-device-mgt!641corrective-policy
commit
de4f3f64fe
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.jaxrs.beans;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class GeofenceList extends BasePaginatedResult {
|
||||
private List<GeofenceWrapper> geofenceList = new ArrayList<>();
|
||||
|
||||
@ApiModelProperty(value = "List of geofences returned")
|
||||
@JsonProperty("geofences")
|
||||
public List<GeofenceWrapper> getGeofenceList() {
|
||||
return geofenceList;
|
||||
}
|
||||
|
||||
public void setGeofenceList(List<GeofenceWrapper> geofenceList) {
|
||||
this.geofenceList = geofenceList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("{\n");
|
||||
sb.append(" count: ").append(getCount()).append(",\n");
|
||||
sb.append(" geofences: [").append(geofenceList).append("\n");
|
||||
sb.append("]}\n");
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
@ -0,0 +1,107 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.jaxrs.beans;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
|
||||
public class GeofenceWrapper {
|
||||
|
||||
@ApiModelProperty(
|
||||
name = "id",
|
||||
value = "Id of the geo fence")
|
||||
private int id;
|
||||
|
||||
@ApiModelProperty(
|
||||
name = "fenceName",
|
||||
value = "Name of the geo fence",
|
||||
required = true)
|
||||
private String fenceName;
|
||||
|
||||
@ApiModelProperty(
|
||||
name = "description",
|
||||
value = "Description of the geo fence",
|
||||
required = true)
|
||||
private String description;
|
||||
|
||||
@ApiModelProperty(
|
||||
name = "latitude",
|
||||
value = "Latitude of center of the geo fence",
|
||||
required = true)
|
||||
private double latitude;
|
||||
|
||||
@ApiModelProperty(
|
||||
name = "longitude",
|
||||
value = "Longitude of center of the geo fence",
|
||||
required = true)
|
||||
private double longitude;
|
||||
|
||||
@ApiModelProperty(
|
||||
name = "radius",
|
||||
value = "Radius from the center",
|
||||
required = true)
|
||||
private float radius;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getFenceName() {
|
||||
return fenceName;
|
||||
}
|
||||
|
||||
public void setFenceName(String fenceName) {
|
||||
this.fenceName = fenceName;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public double getLatitude() {
|
||||
return latitude;
|
||||
}
|
||||
|
||||
public void setLatitude(double latitude) {
|
||||
this.latitude = latitude;
|
||||
}
|
||||
|
||||
public double getLongitude() {
|
||||
return longitude;
|
||||
}
|
||||
|
||||
public void setLongitude(double longitude) {
|
||||
this.longitude = longitude;
|
||||
}
|
||||
|
||||
public float getRadius() {
|
||||
return radius;
|
||||
}
|
||||
|
||||
public void setRadius(float radius) {
|
||||
this.radius = radius;
|
||||
}
|
||||
}
|
@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.common.geo.service;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
public class GeofenceData {
|
||||
private int id;
|
||||
private String fenceName;
|
||||
private String description;
|
||||
private double latitude;
|
||||
private double longitude;
|
||||
private float radius;
|
||||
private int tenantId;
|
||||
private String owner;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getFenceName() {
|
||||
return fenceName;
|
||||
}
|
||||
|
||||
public void setFenceName(String fenceName) {
|
||||
this.fenceName = fenceName;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public double getLatitude() {
|
||||
return latitude;
|
||||
}
|
||||
|
||||
public void setLatitude(double latitude) {
|
||||
this.latitude = latitude;
|
||||
}
|
||||
|
||||
public double getLongitude() {
|
||||
return longitude;
|
||||
}
|
||||
|
||||
public void setLongitude(double longitude) {
|
||||
this.longitude = longitude;
|
||||
}
|
||||
|
||||
public float getRadius() {
|
||||
return radius;
|
||||
}
|
||||
|
||||
public void setRadius(float radius) {
|
||||
this.radius = radius;
|
||||
}
|
||||
|
||||
public int getTenantId() {
|
||||
return tenantId;
|
||||
}
|
||||
|
||||
public void setTenantId(int tenantId) {
|
||||
this.tenantId = tenantId;
|
||||
}
|
||||
|
||||
public String getOwner() {
|
||||
return owner;
|
||||
}
|
||||
|
||||
public void setOwner(String owner) {
|
||||
this.owner = owner;
|
||||
}
|
||||
}
|
@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.core.dao;
|
||||
|
||||
import org.wso2.carbon.device.mgt.common.PaginationRequest;
|
||||
import org.wso2.carbon.device.mgt.common.geo.service.GeofenceData;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Use to manage geofence data in DB
|
||||
*/
|
||||
public interface GeofenceDAO {
|
||||
|
||||
/**
|
||||
* Create new record of GeoFence
|
||||
* @param geofenceData GeoFence record data
|
||||
* @return created row count
|
||||
* @throws DeviceManagementDAOException error occurs while saving the data
|
||||
*/
|
||||
int saveGeofence(GeofenceData geofenceData) throws DeviceManagementDAOException;
|
||||
|
||||
/**
|
||||
* Retrieve a geofence record for specified Id
|
||||
* @param fenceId Id of the fence which should be queried
|
||||
* @return Retrieved geofence data with tenant and owner info
|
||||
* @throws DeviceManagementDAOException error occurs while reading the data
|
||||
*/
|
||||
GeofenceData getGeofence(int fenceId) throws DeviceManagementDAOException;
|
||||
|
||||
/**
|
||||
* Retrieve a paginated list of geofence data for a specific tenant
|
||||
* @param request pagination request with offset and limit
|
||||
* @param tenantId Id of the tenant which fences owned
|
||||
* @return List of geofences retrieved
|
||||
* @throws DeviceManagementDAOException error occurs while reading the data
|
||||
*/
|
||||
List<GeofenceData> getGeofencesOfTenant(PaginationRequest request, int tenantId)
|
||||
throws DeviceManagementDAOException;
|
||||
|
||||
/**
|
||||
* Delete a geofence using the geofence Id
|
||||
* @param fenceId Id of the fence which should be deleted
|
||||
* @return Affected row count
|
||||
* @throws DeviceManagementDAOException error occurs while deleting the data
|
||||
*/
|
||||
int deleteGeofenceById(int fenceId) throws DeviceManagementDAOException;
|
||||
|
||||
/**
|
||||
* Update a geofence record using fence id
|
||||
* @param geofenceData updated geofence data
|
||||
* @param fenceId id of the fence which should be updated
|
||||
* @return affected row count
|
||||
* @throws DeviceManagementDAOException error occurs while updating the data
|
||||
*/
|
||||
int updateGeofence(GeofenceData geofenceData, int fenceId) throws DeviceManagementDAOException;
|
||||
}
|
@ -0,0 +1,203 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.core.dao.impl;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.device.mgt.common.PaginationRequest;
|
||||
import org.wso2.carbon.device.mgt.common.geo.service.GeoLocationBasedServiceException;
|
||||
import org.wso2.carbon.device.mgt.common.geo.service.GeofenceData;
|
||||
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.GeofenceDAO;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
public class GeofenceDAOImpl implements GeofenceDAO {
|
||||
private static final Log log = LogFactory.getLog(GeofenceDAOImpl.class);
|
||||
@Override
|
||||
public int saveGeofence(GeofenceData geofenceData) throws DeviceManagementDAOException {
|
||||
try {
|
||||
Connection conn = this.getConnection();
|
||||
String sql = "INSERT INTO DM_GEOFENCE(" +
|
||||
"FENCE_NAME, " +
|
||||
"DESCRIPTION, " +
|
||||
"LATITUDE, " +
|
||||
"LONGITUDE, " +
|
||||
"RADIUS, " +
|
||||
"CREATED_TIMESTAMP, " +
|
||||
"OWNER, " +
|
||||
"TENANT_ID) " +
|
||||
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setString(1, geofenceData.getFenceName());
|
||||
stmt.setString(2, geofenceData.getDescription());
|
||||
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());
|
||||
return stmt.executeUpdate();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while creating Geofence for the tenant id "+geofenceData.getTenantId();
|
||||
log.error(msg, e);
|
||||
throw new DeviceManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public GeofenceData getGeofence(int fenceId) throws DeviceManagementDAOException {
|
||||
try {
|
||||
GeofenceData geofenceData = null;
|
||||
Connection conn = this.getConnection();
|
||||
String sql = "SELECT " +
|
||||
"ID, " +
|
||||
"FENCE_NAME, " +
|
||||
"DESCRIPTION, " +
|
||||
"LATITUDE, " +
|
||||
"LONGITUDE, " +
|
||||
"RADIUS, " +
|
||||
"OWNER, " +
|
||||
"TENANT_ID " +
|
||||
"FROM DM_GEOFENCE " +
|
||||
"WHERE ID = ?";
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setInt(1, fenceId);
|
||||
try (ResultSet rst = stmt.executeQuery()) {
|
||||
List<GeofenceData> geofenceDataList = extractGeofenceData(rst);
|
||||
if (!geofenceDataList.isEmpty()) {
|
||||
geofenceData = geofenceDataList.get(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
return geofenceData;
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while retrieving Geofence with id "+fenceId;
|
||||
log.error(msg, e);
|
||||
throw new DeviceManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<GeofenceData> getGeofencesOfTenant(PaginationRequest request, int tenantId)
|
||||
throws DeviceManagementDAOException {
|
||||
try {
|
||||
List<GeofenceData> geofenceData;
|
||||
Connection conn = this.getConnection();
|
||||
String sql = "SELECT " +
|
||||
"ID, " +
|
||||
"FENCE_NAME, " +
|
||||
"DESCRIPTION, " +
|
||||
"LATITUDE, " +
|
||||
"LONGITUDE, " +
|
||||
"RADIUS, " +
|
||||
"OWNER, " +
|
||||
"TENANT_ID " +
|
||||
"FROM DM_GEOFENCE " +
|
||||
"WHERE TENANT_ID = ? " +
|
||||
"LIMIT ? OFFSET ?";
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setInt(1, tenantId);
|
||||
stmt.setInt(2, request.getRowCount());
|
||||
stmt.setInt(3, request.getStartIndex());
|
||||
try (ResultSet rst = stmt.executeQuery()) {
|
||||
geofenceData = extractGeofenceData(rst);
|
||||
}
|
||||
}
|
||||
return geofenceData;
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while retrieving Geofence of the tenant " + tenantId;
|
||||
log.error(msg, e);
|
||||
throw new DeviceManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int deleteGeofenceById(int fenceId) throws DeviceManagementDAOException {
|
||||
try {
|
||||
Connection conn = this.getConnection();
|
||||
String sql = "DELETE FROM DM_GEOFENCE WHERE ID = ?";
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setInt(1, fenceId);
|
||||
return stmt.executeUpdate();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while deleting Geofence with ID " + fenceId;
|
||||
log.error(msg, e);
|
||||
throw new DeviceManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int updateGeofence(GeofenceData geofenceData, int fenceId) throws DeviceManagementDAOException {
|
||||
try {
|
||||
Connection conn = this.getConnection();
|
||||
String sql = "UPDATE DM_GEOFENCE SET " +
|
||||
"FENCE_NAME = ?, " +
|
||||
"DESCRIPTION = ?, " +
|
||||
"LATITUDE = ?, " +
|
||||
"LONGITUDE = ?, " +
|
||||
"RADIUS = ? " +
|
||||
"WHERE ID = ?";
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setString(1, geofenceData.getFenceName());
|
||||
stmt.setString(2, geofenceData.getDescription());
|
||||
stmt.setDouble(3, geofenceData.getLatitude());
|
||||
stmt.setDouble(4, geofenceData.getLongitude());
|
||||
stmt.setFloat(5, geofenceData.getRadius());
|
||||
stmt.setInt(6, fenceId);
|
||||
return stmt.executeUpdate();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while updating Geofence record with id " + fenceId;
|
||||
log.error(msg, e);
|
||||
throw new DeviceManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
private Connection getConnection() throws SQLException {
|
||||
return DeviceManagementDAOFactory.getConnection();
|
||||
}
|
||||
|
||||
private List<GeofenceData> extractGeofenceData(ResultSet rst) throws SQLException {
|
||||
List <GeofenceData> geofenceDataList = new ArrayList<>();
|
||||
while (rst.next()) {
|
||||
GeofenceData geofenceData = new GeofenceData();
|
||||
geofenceData.setId(rst.getInt("ID"));
|
||||
geofenceData.setFenceName(rst.getString("FENCE_NAME"));
|
||||
geofenceData.setDescription(rst.getString("DESCRIPTION"));
|
||||
geofenceData.setLatitude(rst.getDouble("LATITUDE"));
|
||||
geofenceData.setLongitude(rst.getDouble("LONGITUDE"));
|
||||
geofenceData.setRadius(rst.getFloat("RADIUS"));
|
||||
geofenceData.setOwner(rst.getString("OWNER"));
|
||||
geofenceData.setTenantId(rst.getInt("TENANT_ID"));
|
||||
geofenceDataList.add(geofenceData);
|
||||
}
|
||||
return geofenceDataList;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue