Modify geofence get API

corrective-policy
Pahansith 4 years ago
parent af85ca47e0
commit efe116d87e

@ -979,7 +979,11 @@ public interface GeoLocationBasedService {
@ApiParam( @ApiParam(
name = "name", name = "name",
value = "Geo Fence name") value = "Geo Fence name")
@QueryParam("name") String name); @QueryParam("name") String name,
@ApiParam(
name = "requireEventData",
value = "Require geofence event data")
@QueryParam("requireEventData") boolean requireEventData);
@DELETE @DELETE

@ -18,7 +18,9 @@
package org.wso2.carbon.device.mgt.jaxrs.service.impl; package org.wso2.carbon.device.mgt.jaxrs.service.impl;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.google.gson.Gson; import com.google.gson.Gson;
import jdk.nashorn.internal.parser.JSONParser; import jdk.nashorn.internal.parser.JSONParser;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -66,6 +68,7 @@ import org.wso2.carbon.utils.multitenancy.MultitenantUtils;
import javax.ws.rs.*; import javax.ws.rs.*;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -674,16 +677,43 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService {
geofenceWrapper.setRadius(geofenceData.getRadius()); geofenceWrapper.setRadius(geofenceData.getRadius());
geofenceWrapper.setGeoJson(geofenceData.getGeoJson()); geofenceWrapper.setGeoJson(geofenceData.getGeoJson());
geofenceWrapper.setFenceShape(geofenceData.getFenceShape()); geofenceWrapper.setFenceShape(geofenceData.getFenceShape());
geofenceWrapper.setGroupIds(geofenceData.getGroupIds());
if (geofenceData.getEventConfig() != null) {
geofenceWrapper.setEventConfig(getEventConfigBean(geofenceData.getEventConfig()));
}
return geofenceWrapper; return geofenceWrapper;
} }
private List<org.wso2.carbon.device.mgt.jaxrs.beans.EventConfig> getEventConfigBean(List<EventConfig> eventConfig) {
List<org.wso2.carbon.device.mgt.jaxrs.beans.EventConfig> eventList = new ArrayList<>();
org.wso2.carbon.device.mgt.jaxrs.beans.EventConfig eventData;
for (EventConfig event : eventConfig) {
eventData = new org.wso2.carbon.device.mgt.jaxrs.beans.EventConfig();
eventData.setId(event.getEventId());
eventData.setEventLogic(event.getEventLogic());
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
try {
List<EventAction> eventActions = mapper.readValue(event.getActions(), mapper.getTypeFactory().
constructCollectionType(List.class, EventAction.class));
eventData.setActions(eventActions);
} catch (IOException e) {
log.error("Error occurred while parsing event actions of the event with ID " + event.getEventId());
}
eventList.add(eventData);
}
return eventList;
}
@Path("/geo-fence") @Path("/geo-fence")
@GET @GET
@Consumes("application/json") @Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public Response getGeofence(@QueryParam("offset") int offset, public Response getGeofence(@QueryParam("offset") int offset,
@QueryParam("limit") int limit, @QueryParam("limit") int limit,
@QueryParam("name") String name) { @QueryParam("name") String name,
@QueryParam("requireEventData") boolean requireEventData) {
try { try {
GeoLocationProviderService geoService = DeviceMgtAPIUtils.getGeoService(); GeoLocationProviderService geoService = DeviceMgtAPIUtils.getGeoService();
if (offset >= 0 && limit != 0) { if (offset >= 0 && limit != 0) {
@ -691,10 +721,18 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService {
if (name != null && !name.isEmpty()) { if (name != null && !name.isEmpty()) {
request.setProperty(DeviceManagementConstants.GeoServices.FENCE_NAME, name); request.setProperty(DeviceManagementConstants.GeoServices.FENCE_NAME, name);
} }
return getResponse(geoService.getGeoFences(request)); List<GeofenceData> geoFences = geoService.getGeoFences(request);
if (requireEventData) {
geoFences = geoService.getGeoFenceEvents(geoFences);
}
return getResponse(geoFences);
} }
if (name != null && !name.isEmpty()) { if (name != null && !name.isEmpty()) {
return getResponse(geoService.getGeoFences(name)); List<GeofenceData> geoFences = geoService.getGeoFences(name);
if (requireEventData) {
geoFences = geoService.getGeoFenceEvents(geoFences);
}
return getResponse(geoFences);
} }
return getResponse(geoService.getGeoFences()); return getResponse(geoService.getGeoFences());
} catch (GeoLocationBasedServiceException e) { } catch (GeoLocationBasedServiceException e) {

@ -145,4 +145,6 @@ public interface GeoLocationProviderService {
*/ */
boolean updateGeoEventConfigurations(List<EventConfig> eventConfig, List<Integer> removedEventIdList, boolean updateGeoEventConfigurations(List<EventConfig> eventConfig, List<Integer> removedEventIdList,
List<Integer> groupIds, int fenceId) throws GeoLocationBasedServiceException; List<Integer> groupIds, int fenceId) throws GeoLocationBasedServiceException;
List<GeofenceData> getGeoFenceEvents(List<GeofenceData> geoFences) throws GeoLocationBasedServiceException;
} }

@ -19,9 +19,11 @@
package org.wso2.carbon.device.mgt.core.dao; package org.wso2.carbon.device.mgt.core.dao;
import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.event.config.EventConfig;
import org.wso2.carbon.device.mgt.common.geo.service.GeofenceData; import org.wso2.carbon.device.mgt.common.geo.service.GeofenceData;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* Use to manage geofence data in DB * Use to manage geofence data in DB
@ -128,4 +130,10 @@ public interface GeofenceDAO {
* @throws DeviceManagementDAOException error occurred deleting geofence event mapping * @throws DeviceManagementDAOException error occurred deleting geofence event mapping
*/ */
void deleteGeofenceEventMapping(List<Integer> removedEventIdList) throws DeviceManagementDAOException; void deleteGeofenceEventMapping(List<Integer> removedEventIdList) throws DeviceManagementDAOException;
Map<Integer, List<EventConfig>> getEventsOfGeoFences(List<Integer> geofenceIds) throws DeviceManagementDAOException;
List<EventConfig> getEventsOfGeoFence(int geofenceId) throws DeviceManagementDAOException;
Map<Integer, List<Integer>> getGroupIdsOfGeoFences(List<Integer> fenceIds) throws DeviceManagementDAOException;
} }

@ -22,6 +22,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.event.config.EventConfig;
import org.wso2.carbon.device.mgt.common.geo.service.GeoLocationBasedServiceException; 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.common.geo.service.GeofenceData;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
@ -35,8 +36,11 @@ import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
public class GeofenceDAOImpl implements GeofenceDAO { public class GeofenceDAOImpl implements GeofenceDAO {
private static final Log log = LogFactory.getLog(GeofenceDAOImpl.class); private static final Log log = LogFactory.getLog(GeofenceDAOImpl.class);
@ -408,4 +412,125 @@ public class GeofenceDAOImpl implements GeofenceDAO {
throw new DeviceManagementDAOException(msg, e); throw new DeviceManagementDAOException(msg, e);
} }
} }
@Override
public Map<Integer, List<EventConfig>> getEventsOfGeoFences(List<Integer> geofenceIds) throws DeviceManagementDAOException {
try {
Map<Integer, List<EventConfig>> geoFenceEventMap = new HashMap<>();
List<EventConfig> eventList = new ArrayList<>();
Connection conn = this.getConnection();
String sql = "SELECT " +
"E.ID AS EVENT_ID, " +
"M.FENCE_ID AS FENCE_ID, " +
"EVENT_SOURCE, " +
"EVENT_LOGIC, " +
"ACTIONS " +
"FROM DM_DEVICE_EVENT E, DM_GEOFENCE_EVENT_MAPPING M " +
"WHERE E.ID = M.EVENT_ID " +
"AND M.FENCE_ID IN (%s)";
String inClause = String.join(", ", Collections.nCopies(geofenceIds.size(), "?"));
sql = String.format(sql, inClause);
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
int index = 1;
for (Integer geofenceId : geofenceIds) {
stmt.setInt(index++, geofenceId);
}
ResultSet resultSet = stmt.executeQuery();
while (resultSet.next()) {
int fenceId = resultSet.getInt("FENCE_ID");
List<EventConfig> eventConfigList = geoFenceEventMap.get(fenceId);
if (eventConfigList == null) {
eventConfigList = new ArrayList<>();
}
EventConfig event = new EventConfig();
event.setEventId(resultSet.getInt("EVENT_ID"));
event.setEventSource(resultSet.getString("EVENT_SOURCE"));
event.setEventLogic(resultSet.getString("EVENT_LOGIC"));
event.setActions(resultSet.getString("ACTIONS"));
eventConfigList.add(event);
geoFenceEventMap.put(fenceId, eventConfigList);
}
return geoFenceEventMap;
}
} catch (SQLException e) {
String msg = "Error occurred while updating Geofence record with id ";
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
}
}
@Override
public List<EventConfig> getEventsOfGeoFence(int geofenceId) throws DeviceManagementDAOException {
try {
List<EventConfig> eventList = new ArrayList<>();
Connection conn = this.getConnection();
String sql = "SELECT " +
"ID AS EVENT_ID, " +
"EVENT_SOURCE, " +
"EVENT_LOGIC, " +
"ACTIONS " +
"FROM DM_DEVICE_EVENT " +
"WHERE ID IN (" +
"SELECT EVENT_ID FROM DM_GEOFENCE_EVENT_MAPPING " +
"WHERE FENCE_ID = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, geofenceId);
return getEventConfigs(eventList, stmt);
}
} catch (SQLException e) {
String msg = "Error occurred while updating Geofence record with id ";
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
}
}
@Override
public Map<Integer, List<Integer>> getGroupIdsOfGeoFences(List<Integer> fenceIds) throws DeviceManagementDAOException {
try {
Map<Integer, List<Integer>> fenceGroupMap = new HashMap<>();
Connection conn = this.getConnection();
String sql = "SELECT " +
"FENCE_ID, " +
"GROUP_ID " +
"FROM DM_GEOFENCE_GROUP_MAPPING " +
"WHERE FENCE_ID IN (%s) ";
String inClause = String.join(", ", Collections.nCopies(fenceIds.size(), "?"));
sql = String.format(sql, inClause);
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
int index = 1;
for (Integer fenceId : fenceIds) {
stmt.setInt(index++, fenceId);
}
ResultSet rst = stmt.executeQuery();
while (rst.next()) {
int fenceId = rst.getInt("FENCE_ID");
List<Integer> groupIdList = fenceGroupMap.get(fenceId);
if (groupIdList == null) {
groupIdList = new ArrayList<>();
}
groupIdList.add(rst.getInt("GROUP_ID"));
fenceGroupMap.put(fenceId, groupIdList);
}
}
return fenceGroupMap;
} catch (SQLException e) {
String msg = "Error occurred while fetching group IDs of the fences";
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
}
}
private List<EventConfig> getEventConfigs(List<EventConfig> eventList, PreparedStatement stmt) throws SQLException {
ResultSet resultSet = stmt.executeQuery();
EventConfig event;
while (resultSet.next()) {
event = new EventConfig();
event.setEventId(resultSet.getInt("EVENT_ID"));
event.setEventSource(resultSet.getString("EVENT_SOURCE"));
event.setEventLogic(resultSet.getString("EVENT_LOGIC"));
event.setActions(resultSet.getString("ACTIONS"));
eventList.add(event);
}
return eventList;
}
} }

@ -1613,4 +1613,32 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic
} }
return true; return true;
} }
@Override
public List<GeofenceData> getGeoFenceEvents(List<GeofenceData> geoFences) throws GeoLocationBasedServiceException {
try {
DeviceManagementDAOFactory.openConnection();
List<Integer> fenceIds = new ArrayList<>();
for (GeofenceData geoFence : geoFences) {
fenceIds.add(geoFence.getId());
}
Map<Integer, List<EventConfig>> eventsOfGeoFences = geofenceDAO.getEventsOfGeoFences(fenceIds);
Map<Integer, List<Integer>> groupIdsOfGeoFences = geofenceDAO.getGroupIdsOfGeoFences(fenceIds);
for (GeofenceData geoFence : geoFences) {
geoFence.setEventConfig(eventsOfGeoFences.get(geoFence.getId()));
geoFence.setGroupIds(groupIdsOfGeoFences.get(geoFence.getId()));
}
return geoFences;
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while retrieving geo fence events/groups data";
log.error(msg, e);
throw new GeoLocationBasedServiceException(msg, e);
} catch (SQLException e) {
String msg = "Failed open DB connection while getting geo fence event data";
log.error(msg, e);
throw new GeoLocationBasedServiceException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
} }

Loading…
Cancel
Save