From a3629d593217c873a8081b750bb1674187379bb3 Mon Sep 17 00:00:00 2001 From: Pahansith Date: Thu, 19 Nov 2020 22:56:51 +0530 Subject: [PATCH] Add group data into geofence response, Format code --- .../mgt/jaxrs/beans/GeofenceWrapper.java | 11 ++++ .../impl/GeoLocationBasedServiceImpl.java | 9 ++- .../mgt/jaxrs/util/DeviceMgtAPIUtils.java | 9 --- .../mgt/common/geo/service/GeofenceData.java | 10 ++++ .../device/mgt/core/dao/EventConfigDAO.java | 10 +++- .../device/mgt/core/dao/GeofenceDAO.java | 4 +- .../mgt/core/dao/impl/GeofenceDAOImpl.java | 40 +++++++------ .../dto/event/config/GeoFenceGroupMap.java | 60 +++++++++++++++++++ .../GeoLocationProviderServiceImpl.java | 25 ++++++-- 9 files changed, 140 insertions(+), 38 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/event/config/GeoFenceGroupMap.java 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 8e33462913e..e97323fd232 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 @@ -21,6 +21,7 @@ package org.wso2.carbon.device.mgt.jaxrs.beans; import io.swagger.annotations.ApiModelProperty; import java.util.List; +import java.util.Map; public class GeofenceWrapper { @@ -78,6 +79,8 @@ public class GeofenceWrapper { required = true) private List groupIds; + private Map groupNames; + public int getId() { return id; } @@ -157,4 +160,12 @@ public class GeofenceWrapper { public void setGroupIds(List groupIds) { this.groupIds = groupIds; } + + public Map getGroupNames() { + return groupNames; + } + + public void setGroupNames(Map groupNames) { + this.groupNames = groupNames; + } } 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 d41382cbfe6..be5592b3404 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 @@ -680,7 +680,12 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService { geofenceWrapper.setRadius(geofenceData.getRadius()); geofenceWrapper.setGeoJson(geofenceData.getGeoJson()); 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) { geofenceWrapper.setEventConfig(getEventConfigBean(geofenceData.getEventConfig())); } @@ -725,7 +730,7 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService { request.setProperty(DeviceManagementConstants.GeoServices.FENCE_NAME, name); } List geoFences = geoService.getGeoFences(request); - if (requireEventData) { + if (!geoFences.isEmpty() && requireEventData) { geoFences = geoService.attachEventObjects(geoFences); } return getResponse(geoFences); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java index 3065e39bc7c..073e8f44e39 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java @@ -568,15 +568,6 @@ public class DeviceMgtAPIUtils { 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() { PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); AnalyticsDataAPI analyticsDataAPI = 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 e4e0437bdd5..64471383f7b 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 @@ -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 java.util.List; +import java.util.Map; public class GeofenceData { private int id; @@ -36,6 +37,7 @@ public class GeofenceData { private String fenceShape; private List eventConfig; private List groupIds; + private Map groupData; public int getId() { return id; @@ -132,4 +134,12 @@ public class GeofenceData { public void setGroupIds(List groupIds) { this.groupIds = groupIds; } + + public Map getGroupData() { + return groupData; + } + + public void setGroupData(Map groupData) { + this.groupData = groupData; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/EventConfigDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/EventConfigDAO.java index c10e3a4d26e..288d854b146 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/EventConfigDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/EventConfigDAO.java @@ -23,6 +23,7 @@ import org.wso2.carbon.device.mgt.common.event.config.EventConfig; import java.util.List; public interface EventConfigDAO { + /** * Create event configuration entries of the db for a selected tenant * @param eventConfigList event list to be created @@ -50,6 +51,13 @@ public interface EventConfigDAO { */ List getEventsOfGroups(List 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 getEventsOfGroups(int groupId, int tenantId) throws EventManagementDAOException; /** @@ -61,7 +69,6 @@ public interface EventConfigDAO { /** * Update event records of the tenant - * * @param eventsToUpdate updating event records * @throws EventManagementDAOException error occurred while updating events */ @@ -69,7 +76,6 @@ public interface EventConfigDAO { /** * Delete events using event ids - * * @param eventsIdsToDelete ids of the events which should be deleted * @throws EventManagementDAOException error occurred while deleting event records */ diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/GeofenceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/GeofenceDAO.java index 1e369c6de63..8c309673676 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/GeofenceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/GeofenceDAO.java @@ -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.event.config.EventConfig; 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.Map; +import java.util.Set; /** * Use to manage geofence data in DB @@ -153,7 +155,7 @@ public interface GeofenceDAO { * @return GroupIds mapped with geofence id * @throws DeviceManagementDAOException thrown errors while retrieving group Ids of geo fence */ - Map> getGroupIdsOfGeoFences(List fenceIds) throws DeviceManagementDAOException; + Set getGroupIdsOfGeoFences(List fenceIds) throws DeviceManagementDAOException; /** * Get geo fences of the specific group and tenant 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 ffbfffd3893..b4e6ad8be29 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 @@ -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.DeviceManagementDAOFactory; 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.PreparedStatement; @@ -38,8 +39,10 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; public class GeofenceDAOImpl implements GeofenceDAO { private static final Log log = LogFactory.getLog(GeofenceDAOImpl.class); @@ -170,8 +173,8 @@ public class GeofenceDAOImpl implements GeofenceDAO { public List getGeoFencesOfTenant(String fenceName, int tenantId) throws DeviceManagementDAOException { try { - List geofenceData; Connection conn = this.getConnection(); + List geofenceData; String sql = "SELECT " + "ID, " + "FENCE_NAME, " + @@ -184,7 +187,8 @@ public class GeofenceDAOImpl implements GeofenceDAO { "OWNER, " + "TENANT_ID " + "FROM DM_GEOFENCE " + - "WHERE FENCE_NAME LIKE ? AND TENANT_ID = ? "; + "WHERE FENCE_NAME LIKE ?" + + "AND TENANT_ID = ? "; try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setString(1, fenceName + "%"); stmt.setInt(2, tenantId); @@ -482,15 +486,17 @@ public class GeofenceDAOImpl implements GeofenceDAO { } @Override - public Map> getGroupIdsOfGeoFences(List fenceIds) throws DeviceManagementDAOException { + public Set getGroupIdsOfGeoFences(List fenceIds) throws DeviceManagementDAOException { try { - Map> fenceGroupMap = new HashMap<>(); + Set geoFenceGroupSet = new HashSet<>(); Connection conn = this.getConnection(); String sql = "SELECT " + "FENCE_ID, " + - "GROUP_ID " + - "FROM DM_GEOFENCE_GROUP_MAPPING " + - "WHERE FENCE_ID IN (%s) "; + "M.GROUP_ID, " + + "G.GROUP_NAME " + + "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(), "?")); sql = String.format(sql, inClause); try (PreparedStatement stmt = conn.prepareStatement(sql)) { @@ -500,16 +506,14 @@ public class GeofenceDAOImpl implements GeofenceDAO { } ResultSet rst = stmt.executeQuery(); while (rst.next()) { - int fenceId = rst.getInt("FENCE_ID"); - List groupIdList = fenceGroupMap.get(fenceId); - if (groupIdList == null) { - groupIdList = new ArrayList<>(); - } - groupIdList.add(rst.getInt("GROUP_ID")); - fenceGroupMap.put(fenceId, groupIdList); + GeoFenceGroupMap geoFenceGroupMap = new GeoFenceGroupMap(); + geoFenceGroupMap.setFenceId(rst.getInt("FENCE_ID")); + geoFenceGroupMap.setGroupId(rst.getInt("GROUP_ID")); + geoFenceGroupMap.setGroupName(rst.getString("GROUP_NAME")); + geoFenceGroupSet.add(geoFenceGroupMap); } } - return fenceGroupMap; + return geoFenceGroupSet; } catch (SQLException e) { String msg = "Error occurred while fetching group IDs of the fences"; log.error(msg, e); @@ -601,10 +605,10 @@ public class GeofenceDAOImpl implements GeofenceDAO { try (PreparedStatement stmt = con.prepareStatement(sql)){ stmt.setInt(1, fenceId); ResultSet rst = stmt.executeQuery(); - List groupIdList = new ArrayList<>(); + Map groupMap = new HashMap<>(); GeofenceData geofenceData = null; while (rst.next()) { - groupIdList.add(rst.getInt("GROUP_ID")); + groupMap.put(rst.getInt("GROUP_ID"), rst.getString("GROUP_NAME")); if (rst.isLast()) { geofenceData = new GeofenceData(); geofenceData.setId(rst.getInt("FENCE_ID")); @@ -615,7 +619,7 @@ public class GeofenceDAOImpl implements GeofenceDAO { geofenceData.setRadius(rst.getFloat("RADIUS")); geofenceData.setGeoJson(rst.getString("GEO_JSON")); geofenceData.setFenceShape(rst.getString("FENCE_SHAPE")); - geofenceData.setGroupIds(groupIdList); + geofenceData.setGroupData(groupMap); } } return geofenceData; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/event/config/GeoFenceGroupMap.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/event/config/GeoFenceGroupMap.java new file mode 100644 index 00000000000..bac19e86782 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/event/config/GeoFenceGroupMap.java @@ -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; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java index 34badb30548..9dca01c56f4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java @@ -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.GeofenceDAO; 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.internal.DeviceManagementDataHolder; 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.Properties; import java.util.Collections; +import java.util.Set; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; @@ -1376,7 +1378,7 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic try { DeviceManagementDAOFactory.openConnection(); - GeofenceData geofence = geofenceDAO.getGeofence(fenceId); + GeofenceData geofence = geofenceDAO.getGeofence(fenceId, true); if (geofence != null) { GeoCacheManagerImpl.getInstance().addFenceToCache(geofence, fenceId, tenantId); } @@ -1677,11 +1679,22 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic for (GeofenceData geoFence : geoFences) { fenceIds.add(geoFence.getId()); } - Map> eventsOfGeoFences = geofenceDAO.getEventsOfGeoFences(fenceIds); - Map> groupIdsOfGeoFences = geofenceDAO.getGroupIdsOfGeoFences(fenceIds); - for (GeofenceData geoFence : geoFences) { - geoFence.setEventConfig(eventsOfGeoFences.get(geoFence.getId())); - geoFence.setGroupIds(groupIdsOfGeoFences.get(geoFence.getId())); + if (!fenceIds.isEmpty()) { + Map> eventsOfGeoFences = geofenceDAO.getEventsOfGeoFences(fenceIds); + Set groupIdsOfGeoFences = geofenceDAO.getGroupIdsOfGeoFences(fenceIds); + for (GeofenceData geoFence : geoFences) { + geoFence.setEventConfig(eventsOfGeoFences.get(geoFence.getId())); + for (GeoFenceGroupMap geoFenceGroupMap : groupIdsOfGeoFences) { + if (geoFenceGroupMap.getFenceId() == geoFence.getId()) { + Map groupData = geoFence.getGroupData(); + if (groupData == null) { + groupData = new HashMap<>(); + } + groupData.put(geoFenceGroupMap.getGroupId(), geoFenceGroupMap.getGroupName()); + geoFence.setGroupData(groupData); + } + } + } } return geoFences; } catch (DeviceManagementDAOException e) {