From 44c7d324de574f26fb255e70792098c70a956165 Mon Sep 17 00:00:00 2001 From: ThilinaPremachandra Date: Thu, 7 Sep 2023 11:29:10 +0530 Subject: [PATCH] Fix: delete group with GeoFence references issue --- .../core/dao/impl/AbstractGroupDAOImpl.java | 8 +++ .../src/test/resources/sql/h2.sql | 59 +++++++++++++++++++ 2 files changed, 67 insertions(+) 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/dao/impl/AbstractGroupDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java index 657924ca80..b9c655c4ba 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java @@ -526,6 +526,14 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { stmt.setInt(1, groupId); stmt.setInt(2, tenantId); stmt.executeUpdate(); + sql = "DELETE FROM DM_DEVICE_EVENT_GROUP_MAPPING WHERE GROUP_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, groupId); + stmt.executeUpdate(); + sql = "DELETE FROM DM_GEOFENCE_GROUP_MAPPING WHERE GROUP_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, groupId); + stmt.executeUpdate(); } catch (SQLException e) { throw new GroupManagementDAOException("Error occurred while removing mappings for group.'", e); } finally { diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/sql/h2.sql b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/sql/h2.sql index 4f83fa5b5e..57ee79b42c 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/sql/h2.sql +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/sql/h2.sql @@ -73,6 +73,65 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_GROUP_MAP ( REFERENCES DM_GROUP (ID) ON DELETE CASCADE ON UPDATE CASCADE ); +-- DM_GEOFENCE TABLE-- +CREATE TABLE IF NOT EXISTS DM_GEOFENCE +( + ID INT NOT NULL AUTO_INCREMENT, + FENCE_NAME VARCHAR(255) NOT NULL, + DESCRIPTION TEXT DEFAULT NULL, + LATITUDE DOUBLE DEFAULT NULL, + LONGITUDE DOUBLE DEFAULT NULL, + RADIUS DOUBLE DEFAULT NULL, + GEO_JSON TEXT DEFAULT NULL, + FENCE_SHAPE VARCHAR(100) DEFAULT NULL, + CREATED_TIMESTAMP TIMESTAMP NOT NULL, + OWNER VARCHAR(255) NOT NULL, + TENANT_ID INTEGER DEFAULT 0, + PRIMARY KEY (ID) +); +-- END OF DM_GEOFENCE TABLE-- + +-- DM_GEOFENCE_GROUP_MAPPING TABLE-- +CREATE TABLE IF NOT EXISTS DM_GEOFENCE_GROUP_MAPPING +( + ID INT NOT NULL AUTO_INCREMENT, + FENCE_ID INT NOT NULL, + GROUP_ID INT NOT NULL, + PRIMARY KEY (ID), + CONSTRAINT fk_dm_geofence_group_mapping_geofence FOREIGN KEY (FENCE_ID) REFERENCES + DM_GEOFENCE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, + CONSTRAINT fk_dm_geofence_group_mapping_group FOREIGN KEY (GROUP_ID) REFERENCES + DM_GROUP (ID) ON DELETE NO ACTION ON UPDATE NO ACTION +); +-- END OF DM_GEOFENCE_GROUP_MAPPING TABLE-- + +-- DM_DEVICE_EVENT TABLE -- +CREATE TABLE IF NOT EXISTS DM_DEVICE_EVENT +( + ID INT NOT NULL AUTO_INCREMENT, + EVENT_SOURCE VARCHAR(100) NOT NULL, + EVENT_LOGIC VARCHAR(100) NOT NULL, + ACTIONS TEXT DEFAULT NULL, + CREATED_TIMESTAMP TIMESTAMP NOT NULL, + TENANT_ID INTEGER DEFAULT 0, + PRIMARY KEY (ID) +); +-- END OF DM_DEVICE_EVENT TABLE -- + +-- DM_DEVICE_EVENT_GROUP_MAPPING TABLE-- +CREATE TABLE IF NOT EXISTS DM_DEVICE_EVENT_GROUP_MAPPING +( + ID INT NOT NULL AUTO_INCREMENT, + EVENT_ID INT NOT NULL, + GROUP_ID INT NOT NULL, + PRIMARY KEY (ID), + CONSTRAINT fk_dm_event_group_mapping_event FOREIGN KEY (EVENT_ID) REFERENCES + DM_DEVICE_EVENT (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, + CONSTRAINT fk_dm_event_group_mapping_group FOREIGN KEY (GROUP_ID) REFERENCES + DM_GROUP (ID) ON DELETE NO ACTION ON UPDATE NO ACTION +); +-- END OF DM_DEVICE_EVENT_GROUP_MAPPING TABLE-- + CREATE TABLE IF NOT EXISTS DM_OPERATION ( ID INTEGER AUTO_INCREMENT NOT NULL, TYPE VARCHAR(50) NOT NULL,