Add group data into geofence response, Format code

corrective-policy
Pahansith 4 years ago
parent 8c80185610
commit a3629d5932

@ -21,6 +21,7 @@ package org.wso2.carbon.device.mgt.jaxrs.beans;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import java.util.List; import java.util.List;
import java.util.Map;
public class GeofenceWrapper { public class GeofenceWrapper {
@ -78,6 +79,8 @@ public class GeofenceWrapper {
required = true) required = true)
private List<Integer> groupIds; private List<Integer> groupIds;
private Map<Integer, String> groupNames;
public int getId() { public int getId() {
return id; return id;
} }
@ -157,4 +160,12 @@ public class GeofenceWrapper {
public void setGroupIds(List<Integer> groupIds) { public void setGroupIds(List<Integer> groupIds) {
this.groupIds = groupIds; this.groupIds = groupIds;
} }
public Map<Integer, String> getGroupNames() {
return groupNames;
}
public void setGroupNames(Map<Integer, String> groupNames) {
this.groupNames = groupNames;
}
} }

@ -680,7 +680,12 @@ 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.getGroupIds() != null && !geofenceData.getGroupIds().isEmpty()) {
geofenceWrapper.setGroupIds(geofenceData.getGroupIds());
}
if (geofenceData.getGroupData() != null && !geofenceData.getGroupData().isEmpty()) {
geofenceWrapper.setGroupNames(geofenceData.getGroupData());
}
if (geofenceData.getEventConfig() != null) { if (geofenceData.getEventConfig() != null) {
geofenceWrapper.setEventConfig(getEventConfigBean(geofenceData.getEventConfig())); geofenceWrapper.setEventConfig(getEventConfigBean(geofenceData.getEventConfig()));
} }
@ -725,7 +730,7 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService {
request.setProperty(DeviceManagementConstants.GeoServices.FENCE_NAME, name); request.setProperty(DeviceManagementConstants.GeoServices.FENCE_NAME, name);
} }
List<GeofenceData> geoFences = geoService.getGeoFences(request); List<GeofenceData> geoFences = geoService.getGeoFences(request);
if (requireEventData) { if (!geoFences.isEmpty() && requireEventData) {
geoFences = geoService.attachEventObjects(geoFences); geoFences = geoService.attachEventObjects(geoFences);
} }
return getResponse(geoFences); return getResponse(geoFences);

@ -568,15 +568,6 @@ public class DeviceMgtAPIUtils {
return geoService; return geoService;
} }
public static EventConfigurationProviderService getEventConfigService() {
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
EventConfigurationProviderService eventService = (EventConfigurationProviderService) ctx.getOSGiService(EventConfigurationProviderService.class, null);
if (eventService == null) {
throw new IllegalStateException("Event configuration service has not been initialized.");
}
return eventService;
}
public static AnalyticsDataAPI getAnalyticsDataAPI() { public static AnalyticsDataAPI getAnalyticsDataAPI() {
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
AnalyticsDataAPI analyticsDataAPI = AnalyticsDataAPI analyticsDataAPI =

@ -22,6 +22,7 @@ import org.wso2.carbon.device.mgt.common.event.config.EventConfig;
import org.wso2.carbon.device.mgt.common.event.config.EventMetaData; import org.wso2.carbon.device.mgt.common.event.config.EventMetaData;
import java.util.List; import java.util.List;
import java.util.Map;
public class GeofenceData { public class GeofenceData {
private int id; private int id;
@ -36,6 +37,7 @@ public class GeofenceData {
private String fenceShape; private String fenceShape;
private List<EventConfig> eventConfig; private List<EventConfig> eventConfig;
private List<Integer> groupIds; private List<Integer> groupIds;
private Map<Integer, String> groupData;
public int getId() { public int getId() {
return id; return id;
@ -132,4 +134,12 @@ public class GeofenceData {
public void setGroupIds(List<Integer> groupIds) { public void setGroupIds(List<Integer> groupIds) {
this.groupIds = groupIds; this.groupIds = groupIds;
} }
public Map<Integer, String> getGroupData() {
return groupData;
}
public void setGroupData(Map<Integer, String> groupData) {
this.groupData = groupData;
}
} }

@ -23,6 +23,7 @@ import org.wso2.carbon.device.mgt.common.event.config.EventConfig;
import java.util.List; import java.util.List;
public interface EventConfigDAO { public interface EventConfigDAO {
/** /**
* Create event configuration entries of the db for a selected tenant * Create event configuration entries of the db for a selected tenant
* @param eventConfigList event list to be created * @param eventConfigList event list to be created
@ -50,6 +51,13 @@ public interface EventConfigDAO {
*/ */
List<EventConfig> getEventsOfGroups(List<Integer> groupIds, int tenantId) throws EventManagementDAOException; List<EventConfig> getEventsOfGroups(List<Integer> groupIds, int tenantId) throws EventManagementDAOException;
/**
* Get events of groups using group Id
* @param groupId id of the group
* @param tenantId id of the tenant
* @return EventConfig list of specific group
* @throws EventManagementDAOException errors occur while retrieving events of groups
*/
List<EventConfig> getEventsOfGroups(int groupId, int tenantId) throws EventManagementDAOException; List<EventConfig> getEventsOfGroups(int groupId, int tenantId) throws EventManagementDAOException;
/** /**
@ -61,7 +69,6 @@ public interface EventConfigDAO {
/** /**
* Update event records of the tenant * Update event records of the tenant
*
* @param eventsToUpdate updating event records * @param eventsToUpdate updating event records
* @throws EventManagementDAOException error occurred while updating events * @throws EventManagementDAOException error occurred while updating events
*/ */
@ -69,7 +76,6 @@ public interface EventConfigDAO {
/** /**
* Delete events using event ids * Delete events using event ids
*
* @param eventsIdsToDelete ids of the events which should be deleted * @param eventsIdsToDelete ids of the events which should be deleted
* @throws EventManagementDAOException error occurred while deleting event records * @throws EventManagementDAOException error occurred while deleting event records
*/ */

@ -21,9 +21,11 @@ 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.event.config.EventConfig;
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.dto.event.config.GeoFenceGroupMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
/** /**
* Use to manage geofence data in DB * Use to manage geofence data in DB
@ -153,7 +155,7 @@ public interface GeofenceDAO {
* @return GroupIds mapped with geofence id * @return GroupIds mapped with geofence id
* @throws DeviceManagementDAOException thrown errors while retrieving group Ids of geo fence * @throws DeviceManagementDAOException thrown errors while retrieving group Ids of geo fence
*/ */
Map<Integer, List<Integer>> getGroupIdsOfGeoFences(List<Integer> fenceIds) throws DeviceManagementDAOException; Set<GeoFenceGroupMap> getGroupIdsOfGeoFences(List<Integer> fenceIds) throws DeviceManagementDAOException;
/** /**
* Get geo fences of the specific group and tenant * Get geo fences of the specific group and tenant

@ -27,6 +27,7 @@ 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;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.GeofenceDAO; import org.wso2.carbon.device.mgt.core.dao.GeofenceDAO;
import org.wso2.carbon.device.mgt.core.dto.event.config.GeoFenceGroupMap;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
@ -38,8 +39,10 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
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);
@ -170,8 +173,8 @@ public class GeofenceDAOImpl implements GeofenceDAO {
public List<GeofenceData> getGeoFencesOfTenant(String fenceName, int tenantId) public List<GeofenceData> getGeoFencesOfTenant(String fenceName, int tenantId)
throws DeviceManagementDAOException { throws DeviceManagementDAOException {
try { try {
List<GeofenceData> geofenceData;
Connection conn = this.getConnection(); Connection conn = this.getConnection();
List<GeofenceData> geofenceData;
String sql = "SELECT " + String sql = "SELECT " +
"ID, " + "ID, " +
"FENCE_NAME, " + "FENCE_NAME, " +
@ -184,7 +187,8 @@ public class GeofenceDAOImpl implements GeofenceDAO {
"OWNER, " + "OWNER, " +
"TENANT_ID " + "TENANT_ID " +
"FROM DM_GEOFENCE " + "FROM DM_GEOFENCE " +
"WHERE FENCE_NAME LIKE ? AND TENANT_ID = ? "; "WHERE FENCE_NAME LIKE ?" +
"AND TENANT_ID = ? ";
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, fenceName + "%"); stmt.setString(1, fenceName + "%");
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
@ -482,15 +486,17 @@ public class GeofenceDAOImpl implements GeofenceDAO {
} }
@Override @Override
public Map<Integer, List<Integer>> getGroupIdsOfGeoFences(List<Integer> fenceIds) throws DeviceManagementDAOException { public Set<GeoFenceGroupMap> getGroupIdsOfGeoFences(List<Integer> fenceIds) throws DeviceManagementDAOException {
try { try {
Map<Integer, List<Integer>> fenceGroupMap = new HashMap<>(); Set<GeoFenceGroupMap> geoFenceGroupSet = new HashSet<>();
Connection conn = this.getConnection(); Connection conn = this.getConnection();
String sql = "SELECT " + String sql = "SELECT " +
"FENCE_ID, " + "FENCE_ID, " +
"GROUP_ID " + "M.GROUP_ID, " +
"FROM DM_GEOFENCE_GROUP_MAPPING " + "G.GROUP_NAME " +
"WHERE FENCE_ID IN (%s) "; "FROM DM_GEOFENCE_GROUP_MAPPING M, DM_GROUP G " +
"WHERE M.GROUP_ID = G.ID " +
"AND FENCE_ID IN (%s)";
String inClause = String.join(", ", Collections.nCopies(fenceIds.size(), "?")); String inClause = String.join(", ", Collections.nCopies(fenceIds.size(), "?"));
sql = String.format(sql, inClause); sql = String.format(sql, inClause);
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
@ -500,16 +506,14 @@ public class GeofenceDAOImpl implements GeofenceDAO {
} }
ResultSet rst = stmt.executeQuery(); ResultSet rst = stmt.executeQuery();
while (rst.next()) { while (rst.next()) {
int fenceId = rst.getInt("FENCE_ID"); GeoFenceGroupMap geoFenceGroupMap = new GeoFenceGroupMap();
List<Integer> groupIdList = fenceGroupMap.get(fenceId); geoFenceGroupMap.setFenceId(rst.getInt("FENCE_ID"));
if (groupIdList == null) { geoFenceGroupMap.setGroupId(rst.getInt("GROUP_ID"));
groupIdList = new ArrayList<>(); geoFenceGroupMap.setGroupName(rst.getString("GROUP_NAME"));
} geoFenceGroupSet.add(geoFenceGroupMap);
groupIdList.add(rst.getInt("GROUP_ID"));
fenceGroupMap.put(fenceId, groupIdList);
} }
} }
return fenceGroupMap; return geoFenceGroupSet;
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while fetching group IDs of the fences"; String msg = "Error occurred while fetching group IDs of the fences";
log.error(msg, e); log.error(msg, e);
@ -601,10 +605,10 @@ public class GeofenceDAOImpl implements GeofenceDAO {
try (PreparedStatement stmt = con.prepareStatement(sql)){ try (PreparedStatement stmt = con.prepareStatement(sql)){
stmt.setInt(1, fenceId); stmt.setInt(1, fenceId);
ResultSet rst = stmt.executeQuery(); ResultSet rst = stmt.executeQuery();
List<Integer> groupIdList = new ArrayList<>(); Map<Integer, String> groupMap = new HashMap<>();
GeofenceData geofenceData = null; GeofenceData geofenceData = null;
while (rst.next()) { while (rst.next()) {
groupIdList.add(rst.getInt("GROUP_ID")); groupMap.put(rst.getInt("GROUP_ID"), rst.getString("GROUP_NAME"));
if (rst.isLast()) { if (rst.isLast()) {
geofenceData = new GeofenceData(); geofenceData = new GeofenceData();
geofenceData.setId(rst.getInt("FENCE_ID")); geofenceData.setId(rst.getInt("FENCE_ID"));
@ -615,7 +619,7 @@ public class GeofenceDAOImpl implements GeofenceDAO {
geofenceData.setRadius(rst.getFloat("RADIUS")); geofenceData.setRadius(rst.getFloat("RADIUS"));
geofenceData.setGeoJson(rst.getString("GEO_JSON")); geofenceData.setGeoJson(rst.getString("GEO_JSON"));
geofenceData.setFenceShape(rst.getString("FENCE_SHAPE")); geofenceData.setFenceShape(rst.getString("FENCE_SHAPE"));
geofenceData.setGroupIds(groupIdList); geofenceData.setGroupData(groupMap);
} }
} }
return geofenceData; return geofenceData;

@ -0,0 +1,60 @@
/*
* 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.dto.event.config;
public class GeoFenceGroupMap {
private int fenceId;
private int groupId;
private String groupName;
public int getFenceId() {
return fenceId;
}
public void setFenceId(int fenceId) {
this.fenceId = fenceId;
}
public int getGroupId() {
return groupId;
}
public void setGroupId(int groupId) {
this.groupId = groupId;
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof GeoFenceGroupMap) {
GeoFenceGroupMap map = (GeoFenceGroupMap)obj;
return map.getFenceId() == this.getFenceId()
&& map.getGroupId() == this.getGroupId()
&& map.getGroupName().equals(this.getGroupName());
}
return false;
}
}

@ -54,6 +54,7 @@ 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.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.GeofenceDAO; import org.wso2.carbon.device.mgt.core.dao.GeofenceDAO;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.dto.event.config.GeoFenceGroupMap;
import org.wso2.carbon.device.mgt.core.geo.task.GeoFenceEventOperationManager; import org.wso2.carbon.device.mgt.core.geo.task.GeoFenceEventOperationManager;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import org.wso2.carbon.device.mgt.core.operation.mgt.OperationMgtConstants; import org.wso2.carbon.device.mgt.core.operation.mgt.OperationMgtConstants;
@ -89,6 +90,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.Collections; import java.util.Collections;
import java.util.Set;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -1376,7 +1378,7 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic
try { try {
DeviceManagementDAOFactory.openConnection(); DeviceManagementDAOFactory.openConnection();
GeofenceData geofence = geofenceDAO.getGeofence(fenceId); GeofenceData geofence = geofenceDAO.getGeofence(fenceId, true);
if (geofence != null) { if (geofence != null) {
GeoCacheManagerImpl.getInstance().addFenceToCache(geofence, fenceId, tenantId); GeoCacheManagerImpl.getInstance().addFenceToCache(geofence, fenceId, tenantId);
} }
@ -1677,11 +1679,22 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic
for (GeofenceData geoFence : geoFences) { for (GeofenceData geoFence : geoFences) {
fenceIds.add(geoFence.getId()); fenceIds.add(geoFence.getId());
} }
Map<Integer, List<EventConfig>> eventsOfGeoFences = geofenceDAO.getEventsOfGeoFences(fenceIds); if (!fenceIds.isEmpty()) {
Map<Integer, List<Integer>> groupIdsOfGeoFences = geofenceDAO.getGroupIdsOfGeoFences(fenceIds); Map<Integer, List<EventConfig>> eventsOfGeoFences = geofenceDAO.getEventsOfGeoFences(fenceIds);
for (GeofenceData geoFence : geoFences) { Set<GeoFenceGroupMap> groupIdsOfGeoFences = geofenceDAO.getGroupIdsOfGeoFences(fenceIds);
geoFence.setEventConfig(eventsOfGeoFences.get(geoFence.getId())); for (GeofenceData geoFence : geoFences) {
geoFence.setGroupIds(groupIdsOfGeoFences.get(geoFence.getId())); geoFence.setEventConfig(eventsOfGeoFences.get(geoFence.getId()));
for (GeoFenceGroupMap geoFenceGroupMap : groupIdsOfGeoFences) {
if (geoFenceGroupMap.getFenceId() == geoFence.getId()) {
Map<Integer, String> groupData = geoFence.getGroupData();
if (groupData == null) {
groupData = new HashMap<>();
}
groupData.put(geoFenceGroupMap.getGroupId(), geoFenceGroupMap.getGroupName());
geoFence.setGroupData(groupData);
}
}
}
} }
return geoFences; return geoFences;
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {

Loading…
Cancel
Save