diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/DeviceOrganizationDAO.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/DeviceOrganizationDAO.java index 329c837144..a58bd63a8f 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/DeviceOrganizationDAO.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/DeviceOrganizationDAO.java @@ -41,12 +41,14 @@ public interface DeviceOrganizationDAO { List getChildrenOf(DeviceNode node, int maxDepth, boolean includeDevice) throws DeviceOrganizationMgtDAOException; /** - * retrieve parent devices per particular device ID - * @param deviceID + * + * @param node + * @param maxDepth + * @param includeDevice * @return * @throws DeviceOrganizationMgtDAOException */ -// List getParentDevices(DeviceNode node) throws DeviceOrganizationMgtDAOException; + List getParentsOf(DeviceNode node, int maxDepth, boolean includeDevice) throws DeviceOrganizationMgtDAOException; /** * add a new reocrd to device organization table diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/impl/DeviceOrganizationDAOImpl.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/impl/DeviceOrganizationDAOImpl.java index a1a717cd63..e6de60513d 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/impl/DeviceOrganizationDAOImpl.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/impl/DeviceOrganizationDAOImpl.java @@ -50,30 +50,10 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { try { Connection conn = ConnectionManagerUtil.getDBConnection(); getChildrenRecursive(node, maxDepth, visited, conn, childNodes, includeDevice); + if(!includeDevice){ + childNodes.add(node); + } return childNodes; - // Prepare SQL query to retrieve children of the node using DM_DEVICE_ORGANIZATION -// String sql = "SELECT D.ID, D.NAME, D.DESCRIPTION, D.DEVICE_IDENTIFICATION, DT.NAME AS DEVICE_TYPE_NAME " + -// "FROM DM_DEVICE D " + -// "JOIN DM_DEVICE_ORGANIZATION DO ON D.ID = DO.DEVICE_ID " + -// "JOIN DM_DEVICE_TYPE DT ON D.DEVICE_TYPE_ID = DT.ID " + -// "WHERE DO.PARENT_DEVICE_ID = ?"; -// -// try (PreparedStatement stmt = conn.prepareStatement(sql)) { -// stmt.setInt(1, node.getDeviceId()); -// try (ResultSet rs = stmt.executeQuery()) { -// while (rs.next()) { -// DeviceNode child = getDeviceFromResultSet(rs); -// child.setChildren(childNodes); -// childNodes.add(child); -// -// if (maxDepth > 0) { -// List deviceChildren = getChildrenOf(child, maxDepth - 1, true); -// childNodes.addAll(deviceChildren); -// } -// } -// return childNodes; -// } -// } } catch (DBConnectionException e) { String msg = "Error occurred while obtaining DB connection to retrieve all child devices for " + "parent device ID " + @@ -108,11 +88,10 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { try (ResultSet rs = stmt.executeQuery()) { while (rs.next()) { DeviceNode child = getDeviceFromResultSet(rs); - child.setChildren(childNodes); -// if (includeDevice) { -// childNodes.add(node); // Add the parent device if includeDevice is true. -// } - childNodes.add(child); + node.getChildren().add(child); + if (includeDevice) { + childNodes.add(node); // Add the parent device if includeDevice is true. + } getChildrenRecursive(child, maxDepth - 1, visited, conn, childNodes, includeDevice); } @@ -120,40 +99,66 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { } } -// @Override -// public List getParentDevices(DeviceNode node) throws DeviceOrganizationMgtDAOException { -// List parentDevices = null; -// try { -// Connection conn = ConnectionManagerUtil.getDBConnection(); -// String sql = "SELECT do.PARENT_DEVICE_ID,d.ID, d.DESCRIPTION,d.DEVICE_IDENTIFICATION, d.NAME, t.NAME AS DEVICE_TYPE_NAME " + -// "FROM DM_DEVICE d, DM_DEVICE_TYPE t, DM_DEVICE_ORGANIZATION do " + -// "WHERE d.ID IN (SELECT ID FROM DM_DEVICE WHERE ID = ?) " + -// "AND d.ID = do.DEVICE_ID" + -// "AND d.DEVICE_TYPE_ID = t.ID"; -// -// try (PreparedStatement stmt = conn.prepareStatement(sql)) { -// stmt.setInt(1, node.getDeviceId()); -// try (ResultSet rs = stmt.executeQuery()) { -// parentDevices = new ArrayList<>(); -// while (rs.next()) { -// parentDevices.add(getDeviceFromResultSet(rs)); -// } -// return parentDevices; -// } -// } -// } catch (DBConnectionException e) { -// String msg = "Error occurred while obtaining DB connection to retrieve all parent devices for " + -// "child device ID " + -// deviceID; -// log.error(msg); -// throw new DeviceOrganizationMgtDAOException(msg, e); -// } catch (SQLException e) { -// String msg = "Error occurred while processing SQL to retrieve all parent devices for " + -// "child device ID" + deviceID; -// log.error(msg); -// throw new DeviceOrganizationMgtDAOException(msg, e); -// } -// } + @Override + public List getParentsOf(DeviceNode node, int maxDepth, boolean includeDevice) throws DeviceOrganizationMgtDAOException { + List parentNodes = new ArrayList<>(); + Set visited = new HashSet<>(); + try { + Connection conn = ConnectionManagerUtil.getDBConnection(); + getParentsRecursive(node, maxDepth, visited, conn, parentNodes, includeDevice); + if (!includeDevice) { + parentNodes.add(node); + } + return parentNodes; + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining DB connection to retrieve parent devices for " + + "device ID " + node.getDeviceId(); + log.error(msg); + throw new DeviceOrganizationMgtDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while processing SQL to retrieve parent devices for " + + "device ID " + node.getDeviceId(); + log.error(msg); + throw new DeviceOrganizationMgtDAOException(msg, e); + } + } + + private void getParentsRecursive(DeviceNode node, int maxDepth, Set visited, Connection conn, + List parentNodes, boolean includeDevice) throws SQLException { + if (maxDepth <= 0 || visited.contains(node.getDeviceId())) { + return; + } + + visited.add(node.getDeviceId()); + + String sql = "SELECT D.ID, D.NAME, D.DESCRIPTION, D.DEVICE_IDENTIFICATION, DT.NAME AS DEVICE_TYPE_NAME " + + "FROM DM_DEVICE D " + + "JOIN DM_DEVICE_ORGANIZATION DO ON D.ID = DO.PARENT_DEVICE_ID " + + "JOIN DM_DEVICE_TYPE DT ON D.DEVICE_TYPE_ID = DT.ID " + + "WHERE DO.DEVICE_ID = ?"; + + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, node.getDeviceId()); + + try (ResultSet rs = stmt.executeQuery()) { + while (rs.next()) { + DeviceNode parent = getDeviceFromResultSet(rs); + if (!includeDevice && parent.getDeviceId() == node.getDeviceId()) { + // Skip adding the current node as a parent when includeDevice is false + continue; + } + + node.getParents().add(parent); + + if (!parentNodes.contains(parent)) { + parentNodes.add(parent); // Add the parent device if it hasn't been added already. + } + + getParentsRecursive(parent, maxDepth - 1, visited, conn, parentNodes, includeDevice); + } + } + } + } @Override public boolean addDeviceOrganization(DeviceOrganization deviceOrganization) throws DeviceOrganizationMgtDAOException { diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/impl/DeviceOrganizationServiceImpl.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/impl/DeviceOrganizationServiceImpl.java index 9fd8634741..07a5571864 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/impl/DeviceOrganizationServiceImpl.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/impl/DeviceOrganizationServiceImpl.java @@ -50,13 +50,7 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService try { // Open a database connection ConnectionManagerUtil.openDBConnection(); - - List children = new ArrayList<>(); - if (maxDepth <= 0) { - return children; - } - children = deviceOrganizationDao.getChildrenOf(node, maxDepth, includeDevice); - return children; + return deviceOrganizationDao.getChildrenOf(node, maxDepth, includeDevice); } catch (DBConnectionException e) { String msg = "Error occurred while obtaining the database connection to retrieve child devices"; log.error(msg); @@ -71,53 +65,26 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService } } -// @Override -// public List getParentsOf(DeviceNode node, int maxDepth, boolean includeDevice) -// throws DeviceOrganizationMgtPluginException { -// try { -// // Open a database connection -// ConnectionManagerUtil.openDBConnection(); -// -// List parents = new ArrayList<>(); -// if (includeDevice) { -// parents.add(node); -// } -// retrieveParents(node, parents, 1, maxDepth); -// return parents; -// } catch (DBConnectionException e) { -// String msg = "Error occurred while obtaining the database connection to retrieve parent devices"; -// log.error(msg); -// throw new DeviceOrganizationMgtPluginException(msg, e); -// } catch (DeviceOrganizationMgtDAOException e) { -// String msg = "Error occurred in the database level while retrieving parent devices"; -// log.error(msg); -// throw new DeviceOrganizationMgtPluginException(msg, e); -// } finally { -// // Close the database connection -// ConnectionManagerUtil.closeDBConnection(); -// } -// } - -// private void retrieveParents(DeviceNode node, List result, int currentDepth, int maxDepth) -// throws DeviceOrganizationMgtDAOException { -// if (currentDepth > maxDepth) { -// return; -// } -// -// List parentDevices = deviceOrganizationDao.getParentDevices(node); -// -// for (Device parentDevice : parentDevices) { -// DeviceNode parentNode = new DeviceNode(); -// parentNode.setDeviceId(parentDevice.getId()); -// parentNode.setDevice(parentDevice); -// -// result.add(parentNode); -// -// if (currentDepth < maxDepth) { -// retrieveParents(parentNode, result, currentDepth + 1, maxDepth); -// } -// } -// } + @Override + public List getParentsOf(DeviceNode node, int maxDepth, boolean includeDevice) + throws DeviceOrganizationMgtPluginException { + try { + // Open a database connection + ConnectionManagerUtil.openDBConnection(); + return deviceOrganizationDao.getParentsOf(node, maxDepth, includeDevice); + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the database connection to retrieve parent devices"; + log.error(msg); + throw new DeviceOrganizationMgtPluginException(msg, e); + } catch (DeviceOrganizationMgtDAOException e) { + String msg = "Error occurred in the database level while retrieving parent devices"; + log.error(msg); + throw new DeviceOrganizationMgtPluginException(msg, e); + } finally { + // Close the database connection + ConnectionManagerUtil.closeDBConnection(); + } + } @Override public boolean addDeviceOrganization(DeviceOrganization deviceOrganization) diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/spi/DeviceOrganizationService.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/spi/DeviceOrganizationService.java index ac39c89af5..c5dc4a2149 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/spi/DeviceOrganizationService.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/spi/DeviceOrganizationService.java @@ -32,8 +32,8 @@ public interface DeviceOrganizationService { List getChildrenOf(DeviceNode node, int maxDepth, boolean includeDevice) throws DeviceOrganizationMgtPluginException; -// List getParentsOf(DeviceNode node, int maxDepth, boolean includeDevice) -// throws DeviceOrganizationMgtPluginException; + List getParentsOf(DeviceNode node, int maxDepth, boolean includeDevice) + throws DeviceOrganizationMgtPluginException; DeviceOrganization getDeviceOrganizationByID(int organizationId) throws DeviceOrganizationMgtPluginException; diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/DAOTest.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/DAOTest.java index 36e5062c47..2a84171a9a 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/DAOTest.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/DAOTest.java @@ -43,13 +43,20 @@ public class DAOTest extends BaseDeviceOrganizationTest { Assert.assertNotNull(childrenList, "Cannot be null"); } -// @Test(dependsOnMethods = "testAddDeviceOrganization") -// public void testGetParentsOf() throws DBConnectionException, DeviceOrganizationMgtDAOException { -// ConnectionManagerUtil.openDBConnection(); -// List parentList = deviceOrganizationDAO.getParentDevices(4); -// ConnectionManagerUtil.closeDBConnection(); -// Assert.assertNotNull(parentList, "Cannot be null"); -// } + @Test(dependsOnMethods = "testAddDeviceOrganization") + public void testGetParentsOf() throws DBConnectionException, DeviceOrganizationMgtDAOException { + ConnectionManagerUtil.openDBConnection(); + DeviceNode node = new DeviceNode(); + node.setDeviceId(4); + int maxDepth = 4; + boolean includeDevice = true; + List childrenList = deviceOrganizationDAO.getParentsOf(node, maxDepth, includeDevice); + ConnectionManagerUtil.closeDBConnection(); + log.info(childrenList.size()); + Assert.assertNotNull(childrenList, "Cannot be null"); + } + + @Test public void testAddDeviceOrganization() throws DBConnectionException, DeviceOrganizationMgtDAOException { @@ -72,6 +79,15 @@ public class DAOTest extends BaseDeviceOrganizationTest { boolean result1 = deviceOrganizationDAO.addDeviceOrganization(deviceOrganization1); ConnectionManagerUtil.closeDBConnection(); + DeviceOrganization deviceOrganization2 = new DeviceOrganization() { + }; + deviceOrganization1.setDeviceId(4); + deviceOrganization1.setParentDeviceId(2); + deviceOrganization1.setUpdateTime(new Date(System.currentTimeMillis())); + ConnectionManagerUtil.beginDBTransaction(); + boolean result2 = deviceOrganizationDAO.addDeviceOrganization(deviceOrganization1); + ConnectionManagerUtil.closeDBConnection(); + Assert.assertNotNull(result, "Cannot be null"); Assert.assertNotNull(result1, "Cannot be null"); diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/ServiceTest.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/ServiceTest.java index 94d5d7f770..13256f3a0f 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/ServiceTest.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/ServiceTest.java @@ -31,21 +31,21 @@ public class ServiceTest extends BaseDeviceOrganizationTest { public void testGetChildrenOf() throws DeviceOrganizationMgtPluginException { DeviceNode deviceNode = new DeviceNode(); - deviceNode.setDeviceId(3); + deviceNode.setDeviceId(2); List childrenList = deviceOrganizationService.getChildrenOf(deviceNode, 2, true); Assert.assertNotNull(childrenList, "Cannot be null"); } -// @Test(dependsOnMethods = "testAddDeviceOrganization") -// public void testGetParentsOf() throws DeviceOrganizationMgtPluginException { -// -// DeviceNode deviceNode = new DeviceNode(); -// deviceNode.setDeviceId(4); -// List parentList = deviceOrganizationService.getParentsOf(deviceNode, 2, false); -// -// Assert.assertNotNull(parentList, "Cannot be null"); -// } + @Test(dependsOnMethods = "testAddDeviceOrganization") + public void testGetParentsOf() throws DeviceOrganizationMgtPluginException { + + DeviceNode deviceNode = new DeviceNode(); + deviceNode.setDeviceId(4); + List parentList = deviceOrganizationService.getParentsOf(deviceNode, 2, true); + + Assert.assertNotNull(parentList, "Cannot be null"); + } @Test public void testAddDeviceOrganization() throws DeviceOrganizationMgtPluginException { @@ -57,6 +57,18 @@ public class ServiceTest extends BaseDeviceOrganizationTest { deviceOrganization.setParentDeviceId(3); deviceOrganization.setUpdateTime(new Date(System.currentTimeMillis())); boolean result = deviceOrganizationService.addDeviceOrganization(deviceOrganization); + DeviceOrganization deviceOrganization1 = new DeviceOrganization() { + }; + deviceOrganization.setDeviceId(3); + deviceOrganization.setParentDeviceId(2); + deviceOrganization.setUpdateTime(new Date(System.currentTimeMillis())); + boolean result1 = deviceOrganizationService.addDeviceOrganization(deviceOrganization); + DeviceOrganization deviceOrganization2 = new DeviceOrganization() { + }; + deviceOrganization.setDeviceId(4); + deviceOrganization.setParentDeviceId(2); + deviceOrganization.setUpdateTime(new Date(System.currentTimeMillis())); + boolean result2 = deviceOrganizationService.addDeviceOrganization(deviceOrganization); Assert.assertNotNull(result, "Cannot be null");