From a0451f1f4923877b8faa2c1cfa6ee367cccb1de7 Mon Sep 17 00:00:00 2001 From: waruni Date: Tue, 16 Jul 2024 08:35:37 +0530 Subject: [PATCH] Add EVENT REVOKE operation for groupIds to delete --- .../EventConfigurationProviderService.java | 7 ++ ...EventConfigurationProviderServiceImpl.java | 21 ++++++ .../GeoLocationProviderServiceImpl.java | 66 ++++++++++++------- 3 files changed, 70 insertions(+), 24 deletions(-) diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/event/config/EventConfigurationProviderService.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/event/config/EventConfigurationProviderService.java index dca2d843b8..a51dd14f7b 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/event/config/EventConfigurationProviderService.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/event/config/EventConfigurationProviderService.java @@ -72,6 +72,13 @@ public interface EventConfigurationProviderService { */ void deleteEvents(List eventList) throws EventConfigurationException; + /** + * + * @param groupIdsToDelete group list to be deleted + * @throws EventConfigurationException error thrown while deleting group records + */ + void deleteGroups(List groupIdsToDelete) throws EventConfigurationException; + /** * Create event operation and add them into the corresponding devices * @param eventType EVENT_REVOKE / EVENT_CONFIG diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/event/config/EventConfigurationProviderServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/event/config/EventConfigurationProviderServiceImpl.java index 6de751af8e..a90241777a 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/event/config/EventConfigurationProviderServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/event/config/EventConfigurationProviderServiceImpl.java @@ -239,6 +239,27 @@ public class EventConfigurationProviderServiceImpl implements EventConfiguration throw new EventConfigurationException(msg, e); } } + @Override + public void deleteGroups(List groupIdsToDelete) throws EventConfigurationException { + + try { + EventManagementDAOFactory.beginTransaction(); + if (!groupIdsToDelete.isEmpty()) { + eventConfigDAO.deleteEventGroupMappingRecordsByGroupIds(groupIdsToDelete); + } + EventManagementDAOFactory.commitTransaction(); + } catch (TransactionManagementException e) { + String msg = "Failed to start/open transaction to delete device event configurations"; + log.error(msg, e); + throw new EventConfigurationException(msg, e); + } catch (EventManagementDAOException e) { + EventManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while deleting event records"; + log.error(msg, e); + throw new EventConfigurationException(msg, e); + } + + } @Override public void createEventOperationTask(String eventType, String eventCode, EventMetaData eventMeta, int tenantId, diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java index b7c27db5ae..7cf9a55ce6 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java @@ -1527,8 +1527,12 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic @Override public boolean updateGeofence(GeofenceData geofenceData, int fenceId) - throws GeoLocationBasedServiceException, EventConfigurationException { + throws GeoLocationBasedServiceException { + int tenantId; + List groupIdsToDelete = new ArrayList<>(); + List groupIdsToAdd = new ArrayList<>(); + try { tenantId = DeviceManagementDAOUtil.getTenantId(); } catch (DeviceManagementDAOException e) { @@ -1543,8 +1547,6 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic int updatedRowCount = geofenceDAO.updateGeofence(geofenceData, fenceId); savedGroupIds = geofenceDAO.getGroupIdsOfGeoFence(fenceId); geofenceData.setId(fenceId); - List groupIdsToDelete = new ArrayList<>(); - List groupIdsToAdd = new ArrayList<>(); for (Integer savedGroupId : savedGroupIds) { if (!geofenceData.getGroupIds().contains(savedGroupId)) { groupIdsToDelete.add(savedGroupId); @@ -1573,6 +1575,10 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic } finally { EventManagementDAOFactory.closeConnection(); } + + if (!groupIdsToDelete.isEmpty()) { + this.deleteGeoFenceGroups(geofenceData, groupIdsToDelete); + } return true; } @@ -1606,8 +1612,6 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic List createdEventIds; int tenantId; - List groupIdsToDelete = new ArrayList<>(); - List savedGroupIds; try { tenantId = DeviceManagementDAOUtil.getTenantId(); setEventSource(geofenceData.getEventConfig()); @@ -1618,15 +1622,6 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic throw new GeoLocationBasedServiceException(msg); } createdEventIds = eventConfigService.updateEventsOfDeviceGroup(geofenceData.getEventConfig(), removedEventIdList, groupIds); - EventManagementDAOFactory.openConnection(); - savedGroupIds = geofenceDAO.getGroupIdsOfGeoFence(fenceId); - geofenceData.setId(fenceId); - for (Integer savedGroupId : savedGroupIds) { - if (!geofenceData.getGroupIds().contains(savedGroupId)) { - groupIdsToDelete.add(savedGroupId); - } - } - EventManagementDAOFactory.commitTransaction(); } catch (EventConfigurationException e) { String msg = "Error occurred while updating event configuration data"; log.error(msg, e); @@ -1635,11 +1630,6 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic String msg = "Error occurred while retrieving tenant id while update geofence data"; log.error(msg, e); throw new GeoLocationBasedServiceException(msg, e); - } catch (SQLException e) { - throw new RuntimeException(e); - } - finally { - EventManagementDAOFactory.closeConnection(); } if (log.isDebugEnabled()) { @@ -1666,11 +1656,6 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic log.debug("Update geofence event completed."); } try { - if (!groupIdsToDelete.isEmpty()) { - eventConfigService.createEventOperationTask(OperationMgtConstants.OperationCodes.EVENT_REVOKE, - DeviceManagementConstants.EventServices.GEOFENCE, new GeoFenceEventMeta(geofenceData), - tenantId, groupIdsToDelete); - } eventConfigService.createEventOperationTask(OperationMgtConstants.OperationCodes.EVENT_UPDATE, DeviceManagementConstants.EventServices.GEOFENCE, new GeoFenceEventMeta(geofenceData), tenantId, geofenceData.getGroupIds()); @@ -1824,4 +1809,37 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic throw new GeoLocationBasedServiceException(msg, e); } } + + /** + * + * @param geofenceData + * @param groupIdsToDelete + * @throws GeoLocationBasedServiceException + */ + private void deleteGeoFenceGroups(GeofenceData geofenceData, List groupIdsToDelete) + throws GeoLocationBasedServiceException { + int tenantId; + try { + tenantId = DeviceManagementDAOUtil.getTenantId(); + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while retrieving tenant id while get geofence data"; + log.error(msg, e); + throw new GeoLocationBasedServiceException(msg, e); + } + + try { + EventConfigurationProviderService eventConfigService = DeviceManagementDataHolder + .getInstance().getEventConfigurationService(); + + eventConfigService.deleteGroups(groupIdsToDelete); + + eventConfigService.createEventOperationTask(OperationMgtConstants.OperationCodes.EVENT_REVOKE, + DeviceManagementConstants.EventServices.GEOFENCE, new GeoFenceEventMeta(geofenceData), + tenantId, groupIdsToDelete); + } catch (EventConfigurationException e) { + String msg = "Failed to delete Geofence event configurations"; + log.error(msg, e); + throw new GeoLocationBasedServiceException(msg, e); + } + } }