cyclic dependency fixed

backup
Isuri Mendis 1 year ago
parent 05d1e9dd53
commit a67f9f1c4b

@ -26,18 +26,8 @@ import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.excep
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import java.sql.Connection; import java.sql.*;
import java.sql.PreparedStatement; import java.util.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import static io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.util.DeviceOrganizationDaoUtil.getDeviceFromResultSet; import static io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.util.DeviceOrganizationDaoUtil.getDeviceFromResultSet;
import static io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.util.DeviceOrganizationDaoUtil.loadDeviceOrganization; import static io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.util.DeviceOrganizationDaoUtil.loadDeviceOrganization;
@ -56,15 +46,21 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
public List<DeviceNode> getChildrenOfDeviceNode(DeviceNode node, int maxDepth, boolean includeDevice) public List<DeviceNode> getChildrenOfDeviceNode(DeviceNode node, int maxDepth, boolean includeDevice)
throws DeviceOrganizationMgtDAOException { throws DeviceOrganizationMgtDAOException {
List<DeviceNode> childNodes = new ArrayList<>(); List<DeviceNode> childNodes = new ArrayList<>();
// Set<Integer> visited = new HashSet<>(); Set<Integer> visited = new HashSet<>();
Set<Integer> twiseVisited = new HashSet<>();
try { try {
Connection conn = ConnectionManagerUtil.getDBConnection(); Connection conn = ConnectionManagerUtil.getDBConnection();
boolean parentAdded = false; // Flag to track whether the parent device has been added boolean parentAdded = false; // Flag to track whether the parent device has been added
getChildrenRecursive(node, maxDepth, getChildrenRecursive(node, maxDepth,
// visited, visited,
conn, childNodes, includeDevice, parentAdded); twiseVisited,
if (!includeDevice && !parentAdded) { conn, childNodes, includeDevice
, parentAdded
);
if (!includeDevice
&& !parentAdded
) {
childNodes.add(node); // Add the parent device if it hasn't been added and includeDevice is false. childNodes.add(node); // Add the parent device if it hasn't been added and includeDevice is false.
} }
return childNodes; return childNodes;
@ -82,17 +78,25 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
} }
private void getChildrenRecursive(DeviceNode node, int maxDepth, private void getChildrenRecursive(DeviceNode node, int maxDepth,
// Set<Integer> visited, Set<Integer> visited,
Set<Integer> twiseVisited,
Connection conn, Connection conn,
List<DeviceNode> childNodes, boolean includeDevice, boolean parentAdded) List<DeviceNode> childNodes, boolean includeDevice
, boolean parentAdded
)
throws SQLException { throws SQLException {
if (maxDepth <= 0 if (maxDepth <= 0) {
// || visited.contains(node.getDeviceId())
) {
return; return;
} }
if (twiseVisited.contains(node.getDeviceId())) {
return;
}
if (visited.contains(node.getDeviceId())) {
twiseVisited.add(node.getDeviceId());
}
// visited.add(node.getDeviceId()); visited.add(node.getDeviceId());
String sql = "SELECT D.ID, D.NAME, D.DESCRIPTION, D.DEVICE_IDENTIFICATION, DT.NAME AS DEVICE_TYPE_NAME " + String sql = "SELECT D.ID, D.NAME, D.DESCRIPTION, D.DEVICE_IDENTIFICATION, DT.NAME AS DEVICE_TYPE_NAME " +
"FROM DM_DEVICE D " + "FROM DM_DEVICE D " +
@ -107,14 +111,19 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
while (rs.next()) { while (rs.next()) {
DeviceNode child = getDeviceFromResultSet(rs); DeviceNode child = getDeviceFromResultSet(rs);
node.getChildren().add(child); node.getChildren().add(child);
if (includeDevice && !parentAdded) { if (includeDevice
&& !parentAdded
) {
childNodes.add(node); // Add the parent device only if includeDevice is true and it hasn't been added. childNodes.add(node); // Add the parent device only if includeDevice is true and it hasn't been added.
parentAdded = true; // Set the flag to true after adding the parent device. parentAdded = true; // Set the flag to true after adding the parent device.
} }
getChildrenRecursive(child, maxDepth - 1, getChildrenRecursive(child, maxDepth - 1,
// visited, visited,
conn, childNodes, includeDevice, parentAdded); twiseVisited,
conn, childNodes, includeDevice
, parentAdded
);
} }
} }
} }
@ -129,11 +138,13 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
List<DeviceNode> parentNodes = new ArrayList<>(); List<DeviceNode> parentNodes = new ArrayList<>();
Set<Integer> visited = new HashSet<>(); Set<Integer> visited = new HashSet<>();
Set<Integer> twiseVisited = new HashSet<>();
try { try {
Connection conn = ConnectionManagerUtil.getDBConnection(); Connection conn = ConnectionManagerUtil.getDBConnection();
boolean childAdded = false; boolean childAdded = false;
getParentsRecursive(node, maxDepth, getParentsRecursive(node, maxDepth,
// visited, visited,
twiseVisited,
conn, parentNodes, includeDevice, childAdded); conn, parentNodes, includeDevice, childAdded);
if (!includeDevice && !childAdded) { if (!includeDevice && !childAdded) {
parentNodes.add(node); parentNodes.add(node);
@ -153,16 +164,22 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
} }
private void getParentsRecursive(DeviceNode node, int maxDepth, private void getParentsRecursive(DeviceNode node, int maxDepth,
// Set<Integer> visited, Set<Integer> visited,
Set<Integer> twiseVisited,
Connection conn, Connection conn,
List<DeviceNode> parentNodes, boolean includeDevice, boolean childAdded) throws SQLException { List<DeviceNode> parentNodes, boolean includeDevice, boolean childAdded) throws SQLException {
if (maxDepth <= 0 if (maxDepth <= 0) {
// || visited.contains(node.getDeviceId()) return;
) { }
if (twiseVisited.contains(node.getDeviceId())) {
return; return;
} }
// visited.add(node.getDeviceId()); if (visited.contains(node.getDeviceId())) {
twiseVisited.add(node.getDeviceId());
}
visited.add(node.getDeviceId());
String sql = "SELECT D.ID, D.NAME, D.DESCRIPTION, D.DEVICE_IDENTIFICATION, DT.NAME AS DEVICE_TYPE_NAME " + String sql = "SELECT D.ID, D.NAME, D.DESCRIPTION, D.DEVICE_IDENTIFICATION, DT.NAME AS DEVICE_TYPE_NAME " +
"FROM DM_DEVICE D " + "FROM DM_DEVICE D " +
@ -181,7 +198,8 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
childAdded = true; childAdded = true;
} }
getParentsRecursive(parent, maxDepth - 1, getParentsRecursive(parent, maxDepth - 1,
// visited, visited,
twiseVisited,
conn, parentNodes, includeDevice, childAdded); conn, parentNodes, includeDevice, childAdded);
} }
} }

@ -30,6 +30,7 @@ import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.spi.D
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
@ -155,6 +156,7 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService
log.error("Device Organization already exists"); log.error("Device Organization already exists");
return false; return false;
} }
try { try {
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
boolean result = deviceOrganizationDao.addDeviceOrganization(deviceOrganization); boolean result = deviceOrganizationDao.addDeviceOrganization(deviceOrganization);
@ -188,6 +190,38 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService
} }
} }
private boolean isChildDevice(int parentDeviceID, Integer childDeviceID) {
// Base case: If childDeviceID is null or equal to parentDeviceID, it's not a child.
if (childDeviceID == null || childDeviceID == parentDeviceID) {
return false;
}
// Query your data source to find all devices that have parentDeviceID as their parent.
List<Integer> childDevices = getChildDevices(parentDeviceID);
// Check if childDeviceID is directly a child of parentDeviceID.
if (childDevices.contains(childDeviceID)) {
return true;
}
// Check if childDeviceID is indirectly a child by recursively checking its descendants.
for (Integer device : childDevices) {
if (isChildDevice(device, childDeviceID)) {
return true;
}
}
return false;
}
private List<Integer> getChildDevices(int parentDeviceID) {
List<Integer> childDevicesList = new ArrayList<>(); // Replace with actual data source query
// Add code here to populate childDevices based on your data source.
return childDevicesList;
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */

Loading…
Cancel
Save